Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit39ee0f5

Browse files
committed
Fix relation-to-view conversion so that it doesn't try to convert a plain
relation to a view when you create an ON INSERT/UPDATE/DELETE rule ...
1 parent367bc8f commit39ee0f5

File tree

1 file changed

+33
-35
lines changed

1 file changed

+33
-35
lines changed

‎src/backend/rewrite/rewriteDefine.c

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
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)
200200
foreach(l,action)
201201
{
202202
query= (Query*)lfirst(l);
203-
if (query->resultRelation==1)
203+
if (query->resultRelation==PRS2_OLD_VARNO)
204204
{
205205
elog(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
{
210210
elog(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-
HeapScanDescscanDesc;
217-
HeapTupletuple;
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-
337312
expected_name=MakeRetrieveViewRuleName(event_obj->relname);
338313
if (strcmp(expected_name,stmt->rulename)!=0)
339314
{
340315
elog(ERROR,"view rule for %s must be named %s",
341316
event_obj->relname,expected_name);
342317
}
343318
pfree(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+
HeapScanDescscanDesc;
329+
HeapTupletuple;
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)
363361
is_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
{
368366
RelationrelationRelation;
369367
HeapTupletuple;
@@ -382,8 +380,8 @@ DefineQueryRewrite(RuleStmt *stmt)
382380

383381
/*
384382
* Set pg_class 'relhasrules' field TRUE for event relation.
385-
*Alsomodify the 'relkind' field to show that the relation is
386-
* now a view.
383+
*If appropriate, alsomodify the 'relkind' field to show that
384+
*the relation isnow 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
*/
399397
relationRelation=heap_openr(RelationRelationName,RowExclusiveLock);
400398
tuple=SearchSysCacheTupleCopy(RELOID,
401-
ObjectIdGetDatum(ev_relid),
402-
0,0,0);
399+
ObjectIdGetDatum(ev_relid),
400+
0,0,0);
403401
Assert(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
*/
425424
if (RelisBecomingView)
426425
smgrunlink(DEFAULT_SMGR,event_relation);
427426

428-
429427
/* Close rel, but keep lock till commit... */
430428
heap_close(event_relation,NoLock);
431429
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp