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

Commit9a915e5

Browse files
committed
Improve the handling of SET CONSTRAINTS commands by having them search
pg_constraint before searching pg_trigger. This allows saner handling ofcorner cases; in particular we now say "constraint is not deferrable"rather than "constraint does not exist" when the command is applied toa constraint that's inherently non-deferrable. Per a gripe several monthsago from hubert depesz lubaczewski.To make this work without breaking user-defined constraint triggers,we have to add entries for them to pg_constraint. However, in returnwe can remove the pgconstrname column from pg_constraint, which representsa fairly sizable space savings. I also replaced the tgisconstraint columnwith tgisinternal; the old meaning of tgisconstraint can now be had bytesting for nonzero tgconstraint, while there is no other way to getthe old meaning of nonzero tgconstraint, namely that the trigger wasinternally generated rather than being user-created.In passing, fix an old misstatement in the docs and comments, namely thatpg_trigger.tgdeferrable is exactly redundant with pg_constraint.condeferrable.Actually, we mark RI action triggers as nondeferrable even when they belong toa nominally deferrable FK constraint. The SET CONSTRAINTS code now relies onthat instead of hard-coding a list of exception OIDs.
1 parentee3b418 commit9a915e5

File tree

17 files changed

+293
-235
lines changed

17 files changed

+293
-235
lines changed

‎doc/src/sgml/catalogs.sgml

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/catalogs.sgml,v 2.217 2010/01/10 01:23:08 rhaas Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/catalogs.sgml,v 2.218 2010/01/17 22:56:21 tgl Exp $ -->
22
<!--
33
Documentation of the system catalogs, directed toward PostgreSQL developers
44
-->
@@ -1721,6 +1721,11 @@
17211721
catalog, not here.
17221722
</para>
17231723

1724+
<para>
1725+
User-defined constraint triggers (created with <command>CREATE CONSTRAINT
1726+
TRIGGER</>) also give rise to an entry in this table.
1727+
</para>
1728+
17241729
<para>
17251730
Check constraints on domains are stored here, too.
17261731
</para>
@@ -1764,6 +1769,7 @@
17641769
<literal>f</> = foreign key constraint,
17651770
<literal>p</> = primary key constraint,
17661771
<literal>u</> = unique constraint,
1772+
<literal>t</> = constraint trigger,
17671773
<literal>x</> = exclusion constraint
17681774
</entry>
17691775
</row>
@@ -1873,7 +1879,8 @@
18731879
<entry><structfield>conkey</structfield></entry>
18741880
<entry><type>int2[]</type></entry>
18751881
<entry><literal><link linkend="catalog-pg-attribute"><structname>pg_attribute</structname></link>.attnum</></entry>
1876-
<entry>If a table constraint (including a foreign key), list of the constrained columns</entry>
1882+
<entry>If a table constraint (including foreign keys, but not constraint
1883+
triggers), list of the constrained columns</entry>
18771884
</row>
18781885

18791886
<row>
@@ -4826,17 +4833,11 @@
48264833
</row>
48274834

48284835
<row>
4829-
<entry><structfield>tgisconstraint</structfield></entry>
4836+
<entry><structfield>tgisinternal</structfield></entry>
48304837
<entry><type>bool</type></entry>
48314838
<entry></entry>
4832-
<entry>True if trigger is a <quote>constraint trigger</></entry>
4833-
</row>
4834-
4835-
<row>
4836-
<entry><structfield>tgconstrname</structfield></entry>
4837-
<entry><type>name</type></entry>
4838-
<entry></entry>
4839-
<entry>Constraint name, if a constraint trigger</entry>
4839+
<entry>True if trigger is internally generated (usually, to enforce
4840+
the constraint identified by <structfield>tgconstraint</>)</entry>
48404841
</row>
48414842

48424843
<row>
@@ -4857,7 +4858,7 @@
48574858
<entry><structfield>tgconstraint</structfield></entry>
48584859
<entry><type>oid</type></entry>
48594860
<entry><literal><link linkend="catalog-pg-constraint"><structname>pg_constraint</structname></link>.oid</literal></entry>
4860-
<entry>The <structname>pg_constraint</> entryowning the trigger, if any</entry>
4861+
<entry>The <structname>pg_constraint</> entryassociated with the trigger, if any</entry>
48614862
</row>
48624863

48634864
<row>
@@ -4919,13 +4920,12 @@
49194920
<note>
49204921
<para>
49214922
When <structfield>tgconstraint</> is nonzero,
4922-
<structfield>tgisconstraint</> must be true, and
4923-
<structfield>tgconstrname</>, <structfield>tgconstrrelid</>,
4924-
<structfield>tgconstrindid</>,
4925-
<structfield>tgdeferrable</>, <structfield>tginitdeferred</> are redundant
4926-
with the referenced <structname>pg_constraint</> entry. The reason we
4927-
keep these fields is that we support <quote>stand-alone</> constraint
4928-
triggers with no corresponding <structname>pg_constraint</> entry.
4923+
<structfield>tgconstrrelid</>, <structfield>tgconstrindid</>,
4924+
<structfield>tgdeferrable</>, and <structfield>tginitdeferred</> are
4925+
largely redundant with the referenced <structname>pg_constraint</> entry.
4926+
However, it is possible for a non-deferrable trigger to be associated
4927+
with a deferrable constraint: foreign key constraints can have some
4928+
deferrable and some non-deferrable triggers.
49294929
</para>
49304930
</note>
49314931

‎doc/src/sgml/trigger.sgml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/trigger.sgml,v 1.61 2009/11/23 21:41:20 petere Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/trigger.sgml,v 1.62 2010/01/17 22:56:21 tgl Exp $ -->
22

33
<chapter id="triggers">
44
<title>Triggers</title>
@@ -506,7 +506,7 @@ typedef struct Trigger
506506
Oid tgfoid;
507507
int16 tgtype;
508508
bool tgenabled;
509-
booltgisconstraint;
509+
booltgisinternal;
510510
Oid tgconstrrelid;
511511
Oid tgconstrindid;
512512
Oid tgconstraint;

‎src/backend/catalog/index.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/catalog/index.c,v 1.329 2010/01/06 03:03:58 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/catalog/index.c,v 1.330 2010/01/17 22:56:21 tgl Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -825,7 +825,8 @@ index_create(Oid heapRelationId,
825825
-1);
826826

827827
trigger=makeNode(CreateTrigStmt);
828-
trigger->trigname=pstrdup(indexRelationName);
828+
trigger->trigname= (isprimary ?"PK_ConstraintTrigger" :
829+
"Unique_ConstraintTrigger");
829830
trigger->relation=heapRel;
830831
trigger->funcname=SystemFuncName("unique_key_recheck");
831832
trigger->args=NIL;
@@ -840,9 +841,7 @@ index_create(Oid heapRelationId,
840841
trigger->constrrel=NULL;
841842

842843
(void)CreateTrigger(trigger,NULL,conOid,indexRelationId,
843-
isprimary ?"PK_ConstraintTrigger" :
844-
"Unique_ConstraintTrigger",
845-
false);
844+
true);
846845
}
847846
}
848847
else

‎src/backend/catalog/information_schema.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* Copyright (c) 2003-2010, PostgreSQL Global Development Group
66
*
7-
* $PostgreSQL: pgsql/src/backend/catalog/information_schema.sql,v 1.63 2010/01/02 16:57:36 momjian Exp $
7+
* $PostgreSQL: pgsql/src/backend/catalog/information_schema.sql,v 1.64 2010/01/17 22:56:21 tgl Exp $
88
*/
99

1010
/*
@@ -1666,7 +1666,7 @@ CREATE VIEW table_constraints AS
16661666

16671667
WHEREnc.oid=c.connamespaceANDnr.oid=r.relnamespace
16681668
ANDc.conrelid=r.oid
1669-
ANDc.contype<>'x'-- ignore nonstandard exclusion constraints
1669+
ANDc.contypeNOTIN ('t','x')-- ignore nonstandard constraints
16701670
ANDr.relkind='r'
16711671
AND (NOT pg_is_other_temp_schema(nr.oid))
16721672
AND (pg_has_role(r.relowner,'USAGE')
@@ -1868,7 +1868,7 @@ CREATE VIEW triggered_update_columns AS
18681868
ANDc.oid=t.tgrelid
18691869
ANDt.oid=ta.tgoid
18701870
AND (a.attrelid,a.attnum)= (t.tgrelid,ta.tgattnum)
1871-
AND NOTt.tgisconstraint
1871+
AND NOTt.tgisinternal
18721872
AND (NOT pg_is_other_temp_schema(n.oid))
18731873
AND (pg_has_role(c.relowner,'USAGE')
18741874
-- SELECT privilege omitted, per SQL standard
@@ -1953,7 +1953,7 @@ CREATE VIEW triggers AS
19531953
WHEREn.oid=c.relnamespace
19541954
ANDc.oid=t.tgrelid
19551955
ANDt.tgtype &em.num<>0
1956-
AND NOTt.tgisconstraint
1956+
AND NOTt.tgisinternal
19571957
AND (NOT pg_is_other_temp_schema(n.oid))
19581958
AND (pg_has_role(c.relowner,'USAGE')
19591959
-- SELECT privilege omitted, per SQL standard

‎src/backend/commands/tablecmds.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.315 2010/01/15 09:19:01 heikki Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.316 2010/01/17 22:56:21 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -5338,7 +5338,7 @@ validateForeignKeyConstraint(Constraint *fkconstraint,
53385338
trig.tgoid=InvalidOid;
53395339
trig.tgname=fkconstraint->conname;
53405340
trig.tgenabled=TRIGGER_FIRES_ON_ORIGIN;
5341-
trig.tgisconstraint= TRUE;
5341+
trig.tgisinternal= TRUE;
53425342
trig.tgconstrrelid=RelationGetRelid(pkrel);
53435343
trig.tgconstrindid=pkindOid;
53445344
trig.tgconstraint=constraintOid;
@@ -5399,7 +5399,7 @@ CreateFKCheckTrigger(RangeVar *myRel, Constraint *fkconstraint,
53995399
CreateTrigStmt*fk_trigger;
54005400

54015401
fk_trigger=makeNode(CreateTrigStmt);
5402-
fk_trigger->trigname=fkconstraint->conname;
5402+
fk_trigger->trigname="RI_ConstraintTrigger";
54035403
fk_trigger->relation=myRel;
54045404
fk_trigger->before= false;
54055405
fk_trigger->row= true;
@@ -5424,8 +5424,7 @@ CreateFKCheckTrigger(RangeVar *myRel, Constraint *fkconstraint,
54245424
fk_trigger->constrrel=fkconstraint->pktable;
54255425
fk_trigger->args=NIL;
54265426

5427-
(void)CreateTrigger(fk_trigger,NULL,constraintOid,indexOid,
5428-
"RI_ConstraintTrigger", false);
5427+
(void)CreateTrigger(fk_trigger,NULL,constraintOid,indexOid, true);
54295428

54305429
/* Make changes-so-far visible */
54315430
CommandCounterIncrement();
@@ -5463,7 +5462,7 @@ createForeignKeyTriggers(Relation rel, Constraint *fkconstraint,
54635462
* DELETE action on the referenced table.
54645463
*/
54655464
fk_trigger=makeNode(CreateTrigStmt);
5466-
fk_trigger->trigname=fkconstraint->conname;
5465+
fk_trigger->trigname="RI_ConstraintTrigger";
54675466
fk_trigger->relation=fkconstraint->pktable;
54685467
fk_trigger->before= false;
54695468
fk_trigger->row= true;
@@ -5506,8 +5505,7 @@ createForeignKeyTriggers(Relation rel, Constraint *fkconstraint,
55065505
}
55075506
fk_trigger->args=NIL;
55085507

5509-
(void)CreateTrigger(fk_trigger,NULL,constraintOid,indexOid,
5510-
"RI_ConstraintTrigger", false);
5508+
(void)CreateTrigger(fk_trigger,NULL,constraintOid,indexOid, true);
55115509

55125510
/* Make changes-so-far visible */
55135511
CommandCounterIncrement();
@@ -5517,7 +5515,7 @@ createForeignKeyTriggers(Relation rel, Constraint *fkconstraint,
55175515
* UPDATE action on the referenced table.
55185516
*/
55195517
fk_trigger=makeNode(CreateTrigStmt);
5520-
fk_trigger->trigname=fkconstraint->conname;
5518+
fk_trigger->trigname="RI_ConstraintTrigger";
55215519
fk_trigger->relation=fkconstraint->pktable;
55225520
fk_trigger->before= false;
55235521
fk_trigger->row= true;
@@ -5560,8 +5558,7 @@ createForeignKeyTriggers(Relation rel, Constraint *fkconstraint,
55605558
}
55615559
fk_trigger->args=NIL;
55625560

5563-
(void)CreateTrigger(fk_trigger,NULL,constraintOid,indexOid,
5564-
"RI_ConstraintTrigger", false);
5561+
(void)CreateTrigger(fk_trigger,NULL,constraintOid,indexOid, true);
55655562
}
55665563

55675564
/*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp