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

Commitf99b75b

Browse files
author
Neil Conway
committed
Create separate ON INSERT and ON UPDATE triggers on tables with foreign
keys, rather than a single trigger for both events. This should not changefunctionality, but it is more consistent: previously, there were triggerfunctions for both "check_insert" and "check_update", but the former wasused for both events.Bump catalog version number (not strictly necessary, but best to becautious).
1 parent0832fb7 commitf99b75b

File tree

2 files changed

+62
-40
lines changed

2 files changed

+62
-40
lines changed

‎src/backend/commands/tablecmds.c

Lines changed: 60 additions & 38 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.157 2005/05/10 13:16:26 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.158 2005/05/30 06:52:38 neilc Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -4380,52 +4380,33 @@ validateForeignKeyConstraint(FkConstraint *fkconstraint,
43804380
pfree(trig.tgargs);
43814381
}
43824382

4383-
/*
4384-
* Create the triggers that implement an FK constraint.
4385-
*/
43864383
staticvoid
4387-
createForeignKeyTriggers(Relationrel,FkConstraint*fkconstraint,
4388-
OidconstrOid)
4384+
CreateFKCheckTrigger(RangeVar*myRel,FkConstraint*fkconstraint,
4385+
ObjectAddress*constrobj,ObjectAddress*trigobj,
4386+
boolon_insert)
43894387
{
4390-
RangeVar*myRel;
43914388
CreateTrigStmt*fk_trigger;
43924389
ListCell*fk_attr;
43934390
ListCell*pk_attr;
4394-
ObjectAddresstrigobj,
4395-
constrobj;
4396-
4397-
/*
4398-
* Reconstruct a RangeVar for my relation (not passed in,
4399-
* unfortunately).
4400-
*/
4401-
myRel=makeRangeVar(get_namespace_name(RelationGetNamespace(rel)),
4402-
pstrdup(RelationGetRelationName(rel)));
44034391

4404-
/*
4405-
* Preset objectAddress fields
4406-
*/
4407-
constrobj.classId=ConstraintRelationId;
4408-
constrobj.objectId=constrOid;
4409-
constrobj.objectSubId=0;
4410-
trigobj.classId=TriggerRelationId;
4411-
trigobj.objectSubId=0;
4412-
4413-
/* Make changes-so-far visible */
4414-
CommandCounterIncrement();
4415-
4416-
/*
4417-
* Build and execute a CREATE CONSTRAINT TRIGGER statement for the
4418-
* CHECK action.
4419-
*/
44204392
fk_trigger=makeNode(CreateTrigStmt);
44214393
fk_trigger->trigname=fkconstraint->constr_name;
44224394
fk_trigger->relation=myRel;
4423-
fk_trigger->funcname=SystemFuncName("RI_FKey_check_ins");
44244395
fk_trigger->before= false;
44254396
fk_trigger->row= true;
4426-
fk_trigger->actions[0]='i';
4427-
fk_trigger->actions[1]='u';
4428-
fk_trigger->actions[2]='\0';
4397+
4398+
/* Either ON INSERT or ON UPDATE */
4399+
if (on_insert)
4400+
{
4401+
fk_trigger->funcname=SystemFuncName("RI_FKey_check_ins");
4402+
fk_trigger->actions[0]='i';
4403+
}
4404+
else
4405+
{
4406+
fk_trigger->funcname=SystemFuncName("RI_FKey_check_upd");
4407+
fk_trigger->actions[0]='u';
4408+
}
4409+
fk_trigger->actions[1]='\0';
44294410

44304411
fk_trigger->isconstraint= true;
44314412
fk_trigger->deferrable=fkconstraint->deferrable;
@@ -4453,13 +4434,54 @@ createForeignKeyTriggers(Relation rel, FkConstraint *fkconstraint,
44534434
fk_trigger->args=lappend(fk_trigger->args,lfirst(pk_attr));
44544435
}
44554436

4456-
trigobj.objectId=CreateTrigger(fk_trigger, true);
4437+
trigobj->objectId=CreateTrigger(fk_trigger, true);
44574438

44584439
/* Register dependency from trigger to constraint */
4459-
recordDependencyOn(&trigobj,&constrobj,DEPENDENCY_INTERNAL);
4440+
recordDependencyOn(trigobj,constrobj,DEPENDENCY_INTERNAL);
44604441

44614442
/* Make changes-so-far visible */
44624443
CommandCounterIncrement();
4444+
}
4445+
4446+
/*
4447+
* Create the triggers that implement an FK constraint.
4448+
*/
4449+
staticvoid
4450+
createForeignKeyTriggers(Relationrel,FkConstraint*fkconstraint,
4451+
OidconstrOid)
4452+
{
4453+
RangeVar*myRel;
4454+
CreateTrigStmt*fk_trigger;
4455+
ListCell*fk_attr;
4456+
ListCell*pk_attr;
4457+
ObjectAddresstrigobj,
4458+
constrobj;
4459+
4460+
/*
4461+
* Reconstruct a RangeVar for my relation (not passed in,
4462+
* unfortunately).
4463+
*/
4464+
myRel=makeRangeVar(get_namespace_name(RelationGetNamespace(rel)),
4465+
pstrdup(RelationGetRelationName(rel)));
4466+
4467+
/*
4468+
* Preset objectAddress fields
4469+
*/
4470+
constrobj.classId=ConstraintRelationId;
4471+
constrobj.objectId=constrOid;
4472+
constrobj.objectSubId=0;
4473+
trigobj.classId=TriggerRelationId;
4474+
trigobj.objectSubId=0;
4475+
4476+
/* Make changes-so-far visible */
4477+
CommandCounterIncrement();
4478+
4479+
/*
4480+
* Build and execute a CREATE CONSTRAINT TRIGGER statement for the
4481+
* CHECK action for both INSERTs and UPDATEs on the referencing table.
4482+
*/
4483+
CreateFKCheckTrigger(myRel,fkconstraint,&constrobj,&trigobj, true);
4484+
CreateFKCheckTrigger(myRel,fkconstraint,&constrobj,&trigobj, false);
44634485

44644486
/*
44654487
* Build and execute a CREATE CONSTRAINT TRIGGER statement for the ON

‎src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.270 2005/05/3001:20:50 tgl Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.271 2005/05/3006:52:38 neilc Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/*yyyymmddN */
56-
#defineCATALOG_VERSION_NO200505291
56+
#defineCATALOG_VERSION_NO200505301
5757

5858
#endif

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp