88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteDefine.c,v 1.51 2000/09/1204:49 :09momjian Exp $
11+ * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteDefine.c,v 1.52 2000/09/1220:38 :09tgl Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -200,40 +200,16 @@ DefineQueryRewrite(RuleStmt *stmt)
200200foreach (l ,action )
201201{
202202query = (Query * )lfirst (l );
203- if (query -> resultRelation == 1 )
203+ if (query -> resultRelation == PRS2_OLD_VARNO )
204204{
205205elog (ERROR ,"rule actions on OLD currently not supported"
206206"\n\tuse views or triggers instead" );
207207}
208- if (query -> resultRelation == 2 )
208+ if (query -> resultRelation == PRS2_NEW_VARNO )
209209{
210210elog (ERROR ,"rule actions on NEW currently not supported"
211211"\n\tuse triggers instead" );
212212}
213-
214- if (event_relation -> rd_rel -> relkind != RELKIND_VIEW )
215- {
216- HeapScanDesc scanDesc ;
217- HeapTuple tuple ;
218- /*
219- * A relation is about to become a view.
220- * check that the relation is empty because
221- * the storage for the relation is going to
222- * be deleted.
223- */
224-
225- scanDesc = heap_beginscan (event_relation ,0 ,SnapshotNow ,0 ,NULL );
226- tuple = heap_getnext (scanDesc ,0 );
227- if (HeapTupleIsValid (tuple ))
228- elog (ERROR ,"relation %s is not empty. Cannot convert to view" ,event_obj -> relname );
229-
230- /* don't need heap_freetuple because we never got a valid tuple */
231- heap_endscan (scanDesc );
232-
233-
234- RelisBecomingView = true;
235- }
236-
237213}
238214
239215/*
@@ -333,14 +309,36 @@ DefineQueryRewrite(RuleStmt *stmt)
333309/*
334310 * ... and finally the rule must be named _RETviewname.
335311 */
336-
337312expected_name = MakeRetrieveViewRuleName (event_obj -> relname );
338313if (strcmp (expected_name ,stmt -> rulename )!= 0 )
339314{
340315elog (ERROR ,"view rule for %s must be named %s" ,
341316event_obj -> relname ,expected_name );
342317}
343318pfree (expected_name );
319+
320+ /*
321+ * Are we converting a relation to a view?
322+ *
323+ * If so, check that the relation is empty because the storage
324+ * for the relation is going to be deleted.
325+ */
326+ if (event_relation -> rd_rel -> relkind != RELKIND_VIEW )
327+ {
328+ HeapScanDesc scanDesc ;
329+ HeapTuple tuple ;
330+
331+ scanDesc = heap_beginscan (event_relation ,0 ,SnapshotNow ,0 ,NULL );
332+ tuple = heap_getnext (scanDesc ,0 );
333+ if (HeapTupleIsValid (tuple ))
334+ elog (ERROR ,"Relation \"%s\" is not empty. Cannot convert it to view" ,
335+ event_obj -> relname );
336+
337+ /* don't need heap_freetuple because we never got a valid tuple */
338+ heap_endscan (scanDesc );
339+
340+ RelisBecomingView = true;
341+ }
344342}
345343
346344/*
@@ -363,7 +361,7 @@ DefineQueryRewrite(RuleStmt *stmt)
363361is_instead ,event_attype );
364362
365363/* discard rule if it's null action and not INSTEAD; it's a no-op */
366- if (action != NULL || is_instead )
364+ if (action != NIL || is_instead )
367365{
368366Relation relationRelation ;
369367HeapTuple tuple ;
@@ -382,8 +380,8 @@ DefineQueryRewrite(RuleStmt *stmt)
382380
383381/*
384382 * Set pg_class 'relhasrules' field TRUE for event relation.
385- *Also modify the 'relkind' field to show that the relation is
386- * now a view.
383+ *If appropriate, also modify the 'relkind' field to show that
384+ *the relation is now a view.
387385 *
388386 * Important side effect: an SI notice is broadcast to force all
389387 * backends (including me!) to update relcache entries with the new
@@ -398,8 +396,8 @@ DefineQueryRewrite(RuleStmt *stmt)
398396 */
399397relationRelation = heap_openr (RelationRelationName ,RowExclusiveLock );
400398tuple = SearchSysCacheTupleCopy (RELOID ,
401- ObjectIdGetDatum (ev_relid ),
402- 0 ,0 ,0 );
399+ ObjectIdGetDatum (ev_relid ),
400+ 0 ,0 ,0 );
403401Assert (HeapTupleIsValid (tuple ));
404402
405403/* Do the update */
@@ -420,12 +418,12 @@ DefineQueryRewrite(RuleStmt *stmt)
420418
421419/*
422420 * IF the relation is becoming a view, delete the storage
423- * files associated with it.
421+ * files associated with it. NB: we had better have AccessExclusiveLock
422+ * to do this ...
424423 */
425424if (RelisBecomingView )
426425smgrunlink (DEFAULT_SMGR ,event_relation );
427426
428-
429427/* Close rel, but keep lock till commit... */
430428heap_close (event_relation ,NoLock );
431429}