Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 789ddcb

Browse files
committed
Add tgconstrrelid to stored Trigger structures, make RI trigger functions
depend on this rather than the trigger argument strings to locate the other relation to test. This makes RI triggers function properly in the presence of schemas and temp tables. Along the way, fix bogus lack of locking in RI triggers, handle quoting of names fully correctly, compute required sizes of query buffers with some semblance of accuracy.
1 parent 6a25cd6 commit 789ddcb

File tree

6 files changed

+370
-254
lines changed

6 files changed

+370
-254
lines changed

doc/src/sgml/trigger.sgml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/trigger.sgml,v 1.21 2002/03/22 19:20:32 petere Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/trigger.sgml,v 1.22 2002/04/01 22:36:06 tgl Exp $
33
-->
44

55
<chapter id="triggers">
@@ -374,6 +374,7 @@ typedef struct Trigger
374374
int16 tgtype;
375375
bool tgenabled;
376376
bool tgisconstraint;
377+
Oid tgconstrrelid;
377378
bool tgdeferrable;
378379
bool tginitdeferred;
379380
int16 tgnargs;

src/backend/commands/command.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.170 2002/04/01 04:35:38 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.171 2002/04/01 22:36:09 tgl Exp $
1212
*
1313
* NOTES
1414
* The PerformAddAttribute() code, like most of the relation
@@ -1639,9 +1639,6 @@ AlterTableAddConstraint(Oid myrelid,
16391639
!isTempNamespace(RelationGetNamespace(rel)))
16401640
elog(ERROR, "ALTER TABLE / ADD CONSTRAINT: Unable to reference temporary table from permanent table constraint.");
16411641

1642-
/* Don't need pkrel open anymore, but hold lock */
1643-
heap_close(pkrel, NoLock);
1644-
16451642
/*
16461643
* First we check for limited correctness of the
16471644
* constraint.
@@ -1651,34 +1648,30 @@ AlterTableAddConstraint(Oid myrelid,
16511648
* referenced relation, and that the column datatypes
16521649
* are comparable.
16531650
*
1654-
* Scan through each tuple, calling the RI_FKey_Match_Ins
1651+
* Scan through each tuple, calling RI_FKey_check_ins
16551652
* (insert trigger) as if that tuple had just been
16561653
* inserted. If any of those fail, it should
16571654
* elog(ERROR) and that's that.
16581655
*/
1659-
1660-
trig.tgoid = 0;
1656+
MemSet(&trig, 0, sizeof(trig));
1657+
trig.tgoid = InvalidOid;
16611658
if (fkconstraint->constr_name)
16621659
trig.tgname = fkconstraint->constr_name;
16631660
else
16641661
trig.tgname = "<unknown>";
1665-
trig.tgfoid = 0;
1666-
trig.tgtype = 0;
16671662
trig.tgenabled = TRUE;
16681663
trig.tgisconstraint = TRUE;
1669-
trig.tginitdeferred = FALSE;
1664+
trig.tgconstrrelid = RelationGetRelid(pkrel);
16701665
trig.tgdeferrable = FALSE;
1666+
trig.tginitdeferred = FALSE;
16711667

16721668
trig.tgargs = (char **) palloc(
16731669
sizeof(char *) * (4 + length(fkconstraint->fk_attrs)
16741670
+ length(fkconstraint->pk_attrs)));
16751671

1676-
if (fkconstraint->constr_name)
1677-
trig.tgargs[0] = fkconstraint->constr_name;
1678-
else
1679-
trig.tgargs[0] = "<unknown>";
1680-
trig.tgargs[1] = pstrdup(RelationGetRelationName(rel));
1681-
trig.tgargs[2] = fkconstraint->pktable->relname;
1672+
trig.tgargs[0] = trig.tgname;
1673+
trig.tgargs[1] = RelationGetRelationName(rel);
1674+
trig.tgargs[2] = RelationGetRelationName(pkrel);
16821675
trig.tgargs[3] = fkconstraint->match_type;
16831676
count = 4;
16841677
foreach(list, fkconstraint->fk_attrs)
@@ -1732,6 +1725,9 @@ AlterTableAddConstraint(Oid myrelid,
17321725
heap_endscan(scan);
17331726

17341727
pfree(trig.tgargs);
1728+
1729+
heap_close(pkrel, NoLock);
1730+
17351731
break;
17361732
}
17371733
default:

src/backend/commands/trigger.c

Lines changed: 4 additions & 1 deletion
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.110 2002/03/31 06:26:30 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/commands/trigger.c,v 1.111 2002/04/01 22:36:10 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -544,6 +544,7 @@ RelationBuildTriggers(Relation relation)
544544
build->tgtype = pg_trigger->tgtype;
545545
build->tgenabled = pg_trigger->tgenabled;
546546
build->tgisconstraint = pg_trigger->tgisconstraint;
547+
build->tgconstrrelid = pg_trigger->tgconstrrelid;
547548
build->tgdeferrable = pg_trigger->tgdeferrable;
548549
build->tginitdeferred = pg_trigger->tginitdeferred;
549550
build->tgnargs = pg_trigger->tgnargs;
@@ -763,6 +764,8 @@ equalTriggerDescs(TriggerDesc *trigdesc1, TriggerDesc *trigdesc2)
763764
return false;
764765
if (trig1->tgisconstraint != trig2->tgisconstraint)
765766
return false;
767+
if (trig1->tgconstrrelid != trig2->tgconstrrelid)
768+
return false;
766769
if (trig1->tgdeferrable != trig2->tgdeferrable)
767770
return false;
768771
if (trig1->tginitdeferred != trig2->tginitdeferred)

0 commit comments

Comments
 (0)