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

Commitaab47ba

Browse files
committed
Hack to make it possible to load CREATE CONSTRAINT TRIGGER commands that
are missing the FROM clause (due to a long-ago pg_dump bug). Patch byStephan Szabo, minor tweaking by Tom Lane.
1 parenteb97884 commitaab47ba

File tree

2 files changed

+120
-6
lines changed

2 files changed

+120
-6
lines changed

‎src/backend/commands/trigger.c

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/commands/trigger.c,v 1.133 2002/09/23 22:57:44 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/commands/trigger.c,v 1.134 2002/10/03 21:06:23 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -28,6 +28,7 @@
2828
#include"commands/trigger.h"
2929
#include"executor/executor.h"
3030
#include"miscadmin.h"
31+
#include"nodes/makefuncs.h"
3132
#include"parser/parse_func.h"
3233
#include"utils/acl.h"
3334
#include"utils/builtins.h"
@@ -83,16 +84,54 @@ CreateTrigger(CreateTrigStmt *stmt, bool forConstraint)
8384
charconstrtrigname[NAMEDATALEN];
8485
char*trigname;
8586
char*constrname;
86-
Oidconstrrelid;
87+
Oidconstrrelid=InvalidOid;
8788
ObjectAddressmyself,
8889
referenced;
8990

9091
rel=heap_openrv(stmt->relation,AccessExclusiveLock);
9192

9293
if (stmt->constrrel!=NULL)
9394
constrrelid=RangeVarGetRelid(stmt->constrrel, false);
94-
else
95-
constrrelid=InvalidOid;
95+
elseif (stmt->isconstraint)
96+
{
97+
/*
98+
* If this trigger is a constraint (and a foreign key one)
99+
* then we really need a constrrelid. Since we don't have one,
100+
* we'll try to generate one from the argument information.
101+
*
102+
* This is really just a workaround for a long-ago pg_dump bug
103+
* that omitted the FROM clause in dumped CREATE CONSTRAINT TRIGGER
104+
* commands. We don't want to bomb out completely here if we can't
105+
* determine the correct relation, because that would prevent loading
106+
* the dump file. Instead, NOTICE here and ERROR in the trigger.
107+
*/
108+
boolneedconstrrelid= false;
109+
void*elem=NULL;
110+
111+
if (strncmp(strVal(llast(stmt->funcname)),"RI_FKey_check_",14)==0)
112+
{
113+
/* A trigger on FK table. */
114+
needconstrrelid= true;
115+
if (length(stmt->args)>RI_PK_RELNAME_ARGNO)
116+
elem=nth(RI_PK_RELNAME_ARGNO,stmt->args);
117+
}
118+
elseif (strncmp(strVal(llast(stmt->funcname)),"RI_FKey_",8)==0)
119+
{
120+
/* A trigger on PK table. */
121+
needconstrrelid= true;
122+
if (length(stmt->args)>RI_FK_RELNAME_ARGNO)
123+
elem=nth(RI_FK_RELNAME_ARGNO,stmt->args);
124+
}
125+
if (elem!=NULL)
126+
{
127+
RangeVar*rel=makeRangeVar(NULL,strVal(elem));
128+
129+
constrrelid=RangeVarGetRelid(rel, true);
130+
}
131+
if (needconstrrelid&&constrrelid==InvalidOid)
132+
elog(NOTICE,"Unable to find table for constraint \"%s\"",
133+
stmt->trigname);
134+
}
96135

97136
if (rel->rd_rel->relkind!=RELKIND_RELATION)
98137
elog(ERROR,"CreateTrigger: relation \"%s\" is not a table",

‎src/backend/utils/adt/ri_triggers.c

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
1919
*
20-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ri_triggers.c,v 1.42 2002/09/04 20:31:28 momjian Exp $
20+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ri_triggers.c,v 1.43 2002/10/03 21:06:23 tgl Exp $
2121
*
2222
* ----------
2323
*/
@@ -207,9 +207,18 @@ RI_FKey_check(PG_FUNCTION_ARGS)
207207
*
208208
* pk_rel is opened in RowShareLock mode since that's what our eventual
209209
* SELECT FOR UPDATE will get on it.
210+
*
211+
* Error check here is needed because of ancient pg_dump bug; see notes
212+
* in CreateTrigger().
210213
*/
211-
fk_rel=trigdata->tg_relation;
214+
if (!OidIsValid(trigdata->tg_trigger->tgconstrrelid))
215+
elog(ERROR,"No target table given for trigger \"%s\" on \"%s\""
216+
"\n\tRemove these RI triggers and do ALTER TABLE ADD CONSTRAINT",
217+
trigdata->tg_trigger->tgname,
218+
RelationGetRelationName(trigdata->tg_relation));
219+
212220
pk_rel=heap_open(trigdata->tg_trigger->tgconstrrelid,RowShareLock);
221+
fk_rel=trigdata->tg_relation;
213222
if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
214223
{
215224
old_row=trigdata->tg_trigtuple;
@@ -745,6 +754,12 @@ RI_FKey_noaction_del(PG_FUNCTION_ARGS)
745754
* fk_rel is opened in RowShareLock mode since that's what our eventual
746755
* SELECT FOR UPDATE will get on it.
747756
*/
757+
if (!OidIsValid(trigdata->tg_trigger->tgconstrrelid))
758+
elog(ERROR,"No target table given for trigger \"%s\" on \"%s\""
759+
"\n\tRemove these RI triggers and do ALTER TABLE ADD CONSTRAINT",
760+
trigdata->tg_trigger->tgname,
761+
RelationGetRelationName(trigdata->tg_relation));
762+
748763
fk_rel=heap_open(trigdata->tg_trigger->tgconstrrelid,RowShareLock);
749764
pk_rel=trigdata->tg_relation;
750765
old_row=trigdata->tg_trigtuple;
@@ -969,6 +984,12 @@ RI_FKey_noaction_upd(PG_FUNCTION_ARGS)
969984
* fk_rel is opened in RowShareLock mode since that's what our eventual
970985
* SELECT FOR UPDATE will get on it.
971986
*/
987+
if (!OidIsValid(trigdata->tg_trigger->tgconstrrelid))
988+
elog(ERROR,"No target table given for trigger \"%s\" on \"%s\""
989+
"\n\tRemove these RI triggers and do ALTER TABLE ADD CONSTRAINT",
990+
trigdata->tg_trigger->tgname,
991+
RelationGetRelationName(trigdata->tg_relation));
992+
972993
fk_rel=heap_open(trigdata->tg_trigger->tgconstrrelid,RowShareLock);
973994
pk_rel=trigdata->tg_relation;
974995
new_row=trigdata->tg_newtuple;
@@ -1199,6 +1220,12 @@ RI_FKey_cascade_del(PG_FUNCTION_ARGS)
11991220
* fk_rel is opened in RowExclusiveLock mode since that's what our
12001221
* eventual DELETE will get on it.
12011222
*/
1223+
if (!OidIsValid(trigdata->tg_trigger->tgconstrrelid))
1224+
elog(ERROR,"No target table given for trigger \"%s\" on \"%s\""
1225+
"\n\tRemove these RI triggers and do ALTER TABLE ADD CONSTRAINT",
1226+
trigdata->tg_trigger->tgname,
1227+
RelationGetRelationName(trigdata->tg_relation));
1228+
12021229
fk_rel=heap_open(trigdata->tg_trigger->tgconstrrelid,RowExclusiveLock);
12031230
pk_rel=trigdata->tg_relation;
12041231
old_row=trigdata->tg_trigtuple;
@@ -1401,6 +1428,12 @@ RI_FKey_cascade_upd(PG_FUNCTION_ARGS)
14011428
* fk_rel is opened in RowExclusiveLock mode since that's what our
14021429
* eventual UPDATE will get on it.
14031430
*/
1431+
if (!OidIsValid(trigdata->tg_trigger->tgconstrrelid))
1432+
elog(ERROR,"No target table given for trigger \"%s\" on \"%s\""
1433+
"\n\tRemove these RI triggers and do ALTER TABLE ADD CONSTRAINT",
1434+
trigdata->tg_trigger->tgname,
1435+
RelationGetRelationName(trigdata->tg_relation));
1436+
14041437
fk_rel=heap_open(trigdata->tg_trigger->tgconstrrelid,RowExclusiveLock);
14051438
pk_rel=trigdata->tg_relation;
14061439
new_row=trigdata->tg_newtuple;
@@ -1639,6 +1672,12 @@ RI_FKey_restrict_del(PG_FUNCTION_ARGS)
16391672
* fk_rel is opened in RowShareLock mode since that's what our eventual
16401673
* SELECT FOR UPDATE will get on it.
16411674
*/
1675+
if (!OidIsValid(trigdata->tg_trigger->tgconstrrelid))
1676+
elog(ERROR,"No target table given for trigger \"%s\" on \"%s\""
1677+
"\n\tRemove these RI triggers and do ALTER TABLE ADD CONSTRAINT",
1678+
trigdata->tg_trigger->tgname,
1679+
RelationGetRelationName(trigdata->tg_relation));
1680+
16421681
fk_rel=heap_open(trigdata->tg_trigger->tgconstrrelid,RowShareLock);
16431682
pk_rel=trigdata->tg_relation;
16441683
old_row=trigdata->tg_trigtuple;
@@ -1856,6 +1895,12 @@ RI_FKey_restrict_upd(PG_FUNCTION_ARGS)
18561895
* fk_rel is opened in RowShareLock mode since that's what our eventual
18571896
* SELECT FOR UPDATE will get on it.
18581897
*/
1898+
if (!OidIsValid(trigdata->tg_trigger->tgconstrrelid))
1899+
elog(ERROR,"No target table given for trigger \"%s\" on \"%s\""
1900+
"\n\tRemove these RI triggers and do ALTER TABLE ADD CONSTRAINT",
1901+
trigdata->tg_trigger->tgname,
1902+
RelationGetRelationName(trigdata->tg_relation));
1903+
18591904
fk_rel=heap_open(trigdata->tg_trigger->tgconstrrelid,RowShareLock);
18601905
pk_rel=trigdata->tg_relation;
18611906
new_row=trigdata->tg_newtuple;
@@ -2078,6 +2123,12 @@ RI_FKey_setnull_del(PG_FUNCTION_ARGS)
20782123
* fk_rel is opened in RowExclusiveLock mode since that's what our
20792124
* eventual UPDATE will get on it.
20802125
*/
2126+
if (!OidIsValid(trigdata->tg_trigger->tgconstrrelid))
2127+
elog(ERROR,"No target table given for trigger \"%s\" on \"%s\""
2128+
"\n\tRemove these RI triggers and do ALTER TABLE ADD CONSTRAINT",
2129+
trigdata->tg_trigger->tgname,
2130+
RelationGetRelationName(trigdata->tg_relation));
2131+
20812132
fk_rel=heap_open(trigdata->tg_trigger->tgconstrrelid,RowExclusiveLock);
20822133
pk_rel=trigdata->tg_relation;
20832134
old_row=trigdata->tg_trigtuple;
@@ -2291,6 +2342,12 @@ RI_FKey_setnull_upd(PG_FUNCTION_ARGS)
22912342
* fk_rel is opened in RowExclusiveLock mode since that's what our
22922343
* eventual UPDATE will get on it.
22932344
*/
2345+
if (!OidIsValid(trigdata->tg_trigger->tgconstrrelid))
2346+
elog(ERROR,"No target table given for trigger \"%s\" on \"%s\""
2347+
"\n\tRemove these RI triggers and do ALTER TABLE ADD CONSTRAINT",
2348+
trigdata->tg_trigger->tgname,
2349+
RelationGetRelationName(trigdata->tg_relation));
2350+
22942351
fk_rel=heap_open(trigdata->tg_trigger->tgconstrrelid,RowExclusiveLock);
22952352
pk_rel=trigdata->tg_relation;
22962353
new_row=trigdata->tg_newtuple;
@@ -2550,6 +2607,12 @@ RI_FKey_setdefault_del(PG_FUNCTION_ARGS)
25502607
* fk_rel is opened in RowExclusiveLock mode since that's what our
25512608
* eventual UPDATE will get on it.
25522609
*/
2610+
if (!OidIsValid(trigdata->tg_trigger->tgconstrrelid))
2611+
elog(ERROR,"No target table given for trigger \"%s\" on \"%s\""
2612+
"\n\tRemove these RI triggers and do ALTER TABLE ADD CONSTRAINT",
2613+
trigdata->tg_trigger->tgname,
2614+
RelationGetRelationName(trigdata->tg_relation));
2615+
25532616
fk_rel=heap_open(trigdata->tg_trigger->tgconstrrelid,RowExclusiveLock);
25542617
pk_rel=trigdata->tg_relation;
25552618
old_row=trigdata->tg_trigtuple;
@@ -2806,6 +2869,12 @@ RI_FKey_setdefault_upd(PG_FUNCTION_ARGS)
28062869
* fk_rel is opened in RowExclusiveLock mode since that's what our
28072870
* eventual UPDATE will get on it.
28082871
*/
2872+
if (!OidIsValid(trigdata->tg_trigger->tgconstrrelid))
2873+
elog(ERROR,"No target table given for trigger \"%s\" on \"%s\""
2874+
"\n\tRemove these RI triggers and do ALTER TABLE ADD CONSTRAINT",
2875+
trigdata->tg_trigger->tgname,
2876+
RelationGetRelationName(trigdata->tg_relation));
2877+
28092878
fk_rel=heap_open(trigdata->tg_trigger->tgconstrrelid,RowExclusiveLock);
28102879
pk_rel=trigdata->tg_relation;
28112880
new_row=trigdata->tg_newtuple;
@@ -3070,6 +3139,12 @@ RI_FKey_keyequal_upd(TriggerData *trigdata)
30703139
*
30713140
* Use minimal locking for fk_rel here.
30723141
*/
3142+
if (!OidIsValid(trigdata->tg_trigger->tgconstrrelid))
3143+
elog(ERROR,"No target table given for trigger \"%s\" on \"%s\""
3144+
"\n\tRemove these RI triggers and do ALTER TABLE ADD CONSTRAINT",
3145+
trigdata->tg_trigger->tgname,
3146+
RelationGetRelationName(trigdata->tg_relation));
3147+
30733148
fk_rel=heap_open(trigdata->tg_trigger->tgconstrrelid,AccessShareLock);
30743149
pk_rel=trigdata->tg_relation;
30753150
new_row=trigdata->tg_newtuple;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp