8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.293 2009/07/29 20:56:18 tgl Exp $
11
+ * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.294 2009/07/30 02:45:36 tgl Exp $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
@@ -154,7 +154,7 @@ typedef struct NewConstraint
154
154
Oid refrelid ; /* PK rel, if FOREIGN */
155
155
Oid refindid ; /* OID of PK's index, if FOREIGN */
156
156
Oid conid ; /* OID of pg_constraint entry, if FOREIGN */
157
- Node * qual ; /* Check expr or FkConstraint struct */
157
+ Node * qual ; /* Check expr or CONSTR_FOREIGN Constraint */
158
158
List * qualstate ; /* Execution state for CHECK */
159
159
} NewConstraint ;
160
160
@@ -247,10 +247,10 @@ static Oid transformFkeyCheckAttrs(Relation pkrel,
247
247
int numattrs , int16 * attnums ,
248
248
Oid * opclasses );
249
249
static void checkFkeyPermissions (Relation rel , int16 * attnums , int natts );
250
- static void validateForeignKeyConstraint (FkConstraint * fkconstraint ,
250
+ static void validateForeignKeyConstraint (Constraint * fkconstraint ,
251
251
Relation rel , Relation pkrel ,
252
252
Oid pkindOid , Oid constraintOid );
253
- static void createForeignKeyTriggers (Relation rel , FkConstraint * fkconstraint ,
253
+ static void createForeignKeyTriggers (Relation rel , Constraint * fkconstraint ,
254
254
Oid constraintOid , Oid indexOid );
255
255
static void ATController (Relation rel , List * cmds , bool recurse );
256
256
static void ATPrepCmd (List * * wqueue , Relation rel , AlterTableCmd * cmd ,
@@ -293,13 +293,13 @@ static void ATExecAddIndex(AlteredTableInfo *tab, Relation rel,
293
293
IndexStmt * stmt , bool is_rebuild );
294
294
static void ATExecAddConstraint (List * * wqueue ,
295
295
AlteredTableInfo * tab , Relation rel ,
296
- Node * newConstraint , bool recurse );
296
+ Constraint * newConstraint , bool recurse );
297
297
static void ATAddCheckConstraint (List * * wqueue ,
298
298
AlteredTableInfo * tab , Relation rel ,
299
299
Constraint * constr ,
300
300
bool recurse , bool recursing );
301
301
static void ATAddForeignKeyConstraint (AlteredTableInfo * tab , Relation rel ,
302
- FkConstraint * fkconstraint );
302
+ Constraint * fkconstraint );
303
303
static void ATExecDropConstraint (Relation rel , const char * constrName ,
304
304
DropBehavior behavior ,
305
305
bool recurse , bool recursing ,
@@ -2637,10 +2637,12 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab, Relation rel,
2637
2637
ATExecAddIndex (tab , rel , (IndexStmt * ) cmd -> def , true);
2638
2638
break ;
2639
2639
case AT_AddConstraint : /* ADD CONSTRAINT */
2640
- ATExecAddConstraint (wqueue , tab , rel , cmd -> def , false);
2640
+ ATExecAddConstraint (wqueue , tab , rel , (Constraint * ) cmd -> def ,
2641
+ false);
2641
2642
break ;
2642
2643
case AT_AddConstraintRecurse : /* ADD CONSTRAINT with recursion */
2643
- ATExecAddConstraint (wqueue , tab , rel , cmd -> def , true);
2644
+ ATExecAddConstraint (wqueue , tab , rel , (Constraint * ) cmd -> def ,
2645
+ true);
2644
2646
break ;
2645
2647
case AT_DropConstraint : /* DROP CONSTRAINT */
2646
2648
ATExecDropConstraint (rel , cmd -> name , cmd -> behavior ,
@@ -2905,7 +2907,7 @@ ATRewriteTables(List **wqueue)
2905
2907
2906
2908
if (con -> contype == CONSTR_FOREIGN )
2907
2909
{
2908
- FkConstraint * fkconstraint = (FkConstraint * ) con -> qual ;
2910
+ Constraint * fkconstraint = (Constraint * ) con -> qual ;
2909
2911
Relation refrel ;
2910
2912
2911
2913
if (rel == NULL )
@@ -4405,69 +4407,55 @@ ATExecAddIndex(AlteredTableInfo *tab, Relation rel,
4405
4407
*/
4406
4408
static void
4407
4409
ATExecAddConstraint (List * * wqueue , AlteredTableInfo * tab , Relation rel ,
4408
- Node * newConstraint , bool recurse )
4410
+ Constraint * newConstraint , bool recurse )
4409
4411
{
4410
- switch (nodeTag (newConstraint ))
4412
+ Assert (IsA (newConstraint , Constraint ));
4413
+
4414
+ /*
4415
+ * Currently, we only expect to see CONSTR_CHECK and CONSTR_FOREIGN nodes
4416
+ * arriving here (see the preprocessing done in parse_utilcmd.c). Use a
4417
+ * switch anyway to make it easier to add more code later.
4418
+ */
4419
+ switch (newConstraint -> contype )
4411
4420
{
4412
- case T_Constraint :
4413
- {
4414
- Constraint * constr = (Constraint * ) newConstraint ;
4421
+ case CONSTR_CHECK :
4422
+ ATAddCheckConstraint (wqueue , tab , rel ,
4423
+ newConstraint , recurse , false);
4424
+ break ;
4415
4425
4416
- /*
4417
- * Currently, we only expect to see CONSTR_CHECK nodes
4418
- * arriving here (see the preprocessing done in
4419
- * parse_utilcmd.c). Use a switch anyway to make it easier to
4420
- * add more code later.
4421
- */
4422
- switch (constr -> contype )
4423
- {
4424
- case CONSTR_CHECK :
4425
- ATAddCheckConstraint (wqueue , tab , rel ,
4426
- constr , recurse , false);
4427
- break ;
4428
- default :
4429
- elog (ERROR , "unrecognized constraint type: %d" ,
4430
- (int ) constr -> contype );
4431
- }
4432
- break ;
4433
- }
4434
- case T_FkConstraint :
4426
+ case CONSTR_FOREIGN :
4427
+ /*
4428
+ * Note that we currently never recurse for FK constraints, so
4429
+ * the "recurse" flag is silently ignored.
4430
+ *
4431
+ * Assign or validate constraint name
4432
+ */
4433
+ if (newConstraint -> conname )
4435
4434
{
4436
- FkConstraint * fkconstraint = (FkConstraint * ) newConstraint ;
4437
-
4438
- /*
4439
- * Note that we currently never recurse for FK constraints, so
4440
- * the "recurse" flag is silently ignored.
4441
- *
4442
- * Assign or validate constraint name
4443
- */
4444
- if (fkconstraint -> constr_name )
4445
- {
4446
- if (ConstraintNameIsUsed (CONSTRAINT_RELATION ,
4447
- RelationGetRelid (rel ),
4448
- RelationGetNamespace (rel ),
4449
- fkconstraint -> constr_name ))
4450
- ereport (ERROR ,
4451
- (errcode (ERRCODE_DUPLICATE_OBJECT ),
4452
- errmsg ("constraint \"%s\" for relation \"%s\" already exists" ,
4453
- fkconstraint -> constr_name ,
4454
- RelationGetRelationName (rel ))));
4455
- }
4456
- else
4457
- fkconstraint -> constr_name =
4458
- ChooseConstraintName (RelationGetRelationName (rel ),
4459
- strVal (linitial (fkconstraint -> fk_attrs )),
4460
- "fkey" ,
4461
- RelationGetNamespace (rel ),
4462
- NIL );
4463
-
4464
- ATAddForeignKeyConstraint (tab , rel , fkconstraint );
4465
-
4466
- break ;
4435
+ if (ConstraintNameIsUsed (CONSTRAINT_RELATION ,
4436
+ RelationGetRelid (rel ),
4437
+ RelationGetNamespace (rel ),
4438
+ newConstraint -> conname ))
4439
+ ereport (ERROR ,
4440
+ (errcode (ERRCODE_DUPLICATE_OBJECT ),
4441
+ errmsg ("constraint \"%s\" for relation \"%s\" already exists" ,
4442
+ newConstraint -> conname ,
4443
+ RelationGetRelationName (rel ))));
4467
4444
}
4445
+ else
4446
+ newConstraint -> conname =
4447
+ ChooseConstraintName (RelationGetRelationName (rel ),
4448
+ strVal (linitial (newConstraint -> fk_attrs )),
4449
+ "fkey" ,
4450
+ RelationGetNamespace (rel ),
4451
+ NIL );
4452
+
4453
+ ATAddForeignKeyConstraint (tab , rel , newConstraint );
4454
+ break ;
4455
+
4468
4456
default :
4469
- elog (ERROR , "unrecognized node type: %d" ,
4470
- (int ) nodeTag ( newConstraint ) );
4457
+ elog (ERROR , "unrecognized constraint type: %d" ,
4458
+ (int ) newConstraint -> contype );
4471
4459
}
4472
4460
}
4473
4461
@@ -4526,12 +4514,12 @@ ATAddCheckConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
4526
4514
tab -> constraints = lappend (tab -> constraints , newcon );
4527
4515
4528
4516
/* Save the actually assigned name if it was defaulted */
4529
- if (constr -> name == NULL )
4530
- constr -> name = ccon -> name ;
4517
+ if (constr -> conname == NULL )
4518
+ constr -> conname = ccon -> name ;
4531
4519
}
4532
4520
4533
4521
/* At this point we must have a locked-down name to use */
4534
- Assert (constr -> name != NULL );
4522
+ Assert (constr -> conname != NULL );
4535
4523
4536
4524
/* Advance command counter in case same table is visited multiple times */
4537
4525
CommandCounterIncrement ();
@@ -4583,7 +4571,7 @@ ATAddCheckConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
4583
4571
*/
4584
4572
static void
4585
4573
ATAddForeignKeyConstraint (AlteredTableInfo * tab , Relation rel ,
4586
- FkConstraint * fkconstraint )
4574
+ Constraint * fkconstraint )
4587
4575
{
4588
4576
Relation pkrel ;
4589
4577
int16 pkattnum [INDEX_MAX_KEYS ];
@@ -4798,7 +4786,7 @@ ATAddForeignKeyConstraint(AlteredTableInfo *tab, Relation rel,
4798
4786
(errcode (ERRCODE_DATATYPE_MISMATCH ),
4799
4787
errmsg ("foreign key constraint \"%s\" "
4800
4788
"cannot be implemented" ,
4801
- fkconstraint -> constr_name ),
4789
+ fkconstraint -> conname ),
4802
4790
errdetail ("Key columns \"%s\" and \"%s\" "
4803
4791
"are of incompatible types: %s and %s." ,
4804
4792
strVal (list_nth (fkconstraint -> fk_attrs , i )),
@@ -4814,7 +4802,7 @@ ATAddForeignKeyConstraint(AlteredTableInfo *tab, Relation rel,
4814
4802
/*
4815
4803
* Record the FK constraint in pg_constraint.
4816
4804
*/
4817
- constrOid = CreateConstraintEntry (fkconstraint -> constr_name ,
4805
+ constrOid = CreateConstraintEntry (fkconstraint -> conname ,
4818
4806
RelationGetNamespace (rel ),
4819
4807
CONSTRAINT_FOREIGN ,
4820
4808
fkconstraint -> deferrable ,
@@ -4854,7 +4842,7 @@ ATAddForeignKeyConstraint(AlteredTableInfo *tab, Relation rel,
4854
4842
NewConstraint * newcon ;
4855
4843
4856
4844
newcon = (NewConstraint * ) palloc0 (sizeof (NewConstraint ));
4857
- newcon -> name = fkconstraint -> constr_name ;
4845
+ newcon -> name = fkconstraint -> conname ;
4858
4846
newcon -> contype = CONSTR_FOREIGN ;
4859
4847
newcon -> refrelid = RelationGetRelid (pkrel );
4860
4848
newcon -> refindid = indexOid ;
@@ -5156,7 +5144,7 @@ checkFkeyPermissions(Relation rel, int16 *attnums, int natts)
5156
5144
* Caller must have opened and locked both relations.
5157
5145
*/
5158
5146
static void
5159
- validateForeignKeyConstraint (FkConstraint * fkconstraint ,
5147
+ validateForeignKeyConstraint (Constraint * fkconstraint ,
5160
5148
Relation rel ,
5161
5149
Relation pkrel ,
5162
5150
Oid pkindOid ,
@@ -5171,7 +5159,7 @@ validateForeignKeyConstraint(FkConstraint *fkconstraint,
5171
5159
*/
5172
5160
MemSet (& trig , 0 , sizeof (trig ));
5173
5161
trig .tgoid = InvalidOid ;
5174
- trig .tgname = fkconstraint -> constr_name ;
5162
+ trig .tgname = fkconstraint -> conname ;
5175
5163
trig .tgenabled = TRIGGER_FIRES_ON_ORIGIN ;
5176
5164
trig .tgisconstraint = TRUE;
5177
5165
trig .tgconstrrelid = RelationGetRelid (pkrel );
@@ -5228,13 +5216,13 @@ validateForeignKeyConstraint(FkConstraint *fkconstraint,
5228
5216
}
5229
5217
5230
5218
static void
5231
- CreateFKCheckTrigger (RangeVar * myRel , FkConstraint * fkconstraint ,
5219
+ CreateFKCheckTrigger (RangeVar * myRel , Constraint * fkconstraint ,
5232
5220
Oid constraintOid , Oid indexOid , bool on_insert )
5233
5221
{
5234
5222
CreateTrigStmt * fk_trigger ;
5235
5223
5236
5224
fk_trigger = makeNode (CreateTrigStmt );
5237
- fk_trigger -> trigname = fkconstraint -> constr_name ;
5225
+ fk_trigger -> trigname = fkconstraint -> conname ;
5238
5226
fk_trigger -> relation = myRel ;
5239
5227
fk_trigger -> before = false;
5240
5228
fk_trigger -> row = true;
@@ -5268,7 +5256,7 @@ CreateFKCheckTrigger(RangeVar *myRel, FkConstraint *fkconstraint,
5268
5256
* Create the triggers that implement an FK constraint.
5269
5257
*/
5270
5258
static void
5271
- createForeignKeyTriggers (Relation rel , FkConstraint * fkconstraint ,
5259
+ createForeignKeyTriggers (Relation rel , Constraint * fkconstraint ,
5272
5260
Oid constraintOid , Oid indexOid )
5273
5261
{
5274
5262
RangeVar * myRel ;
@@ -5296,7 +5284,7 @@ createForeignKeyTriggers(Relation rel, FkConstraint *fkconstraint,
5296
5284
* DELETE action on the referenced table.
5297
5285
*/
5298
5286
fk_trigger = makeNode (CreateTrigStmt );
5299
- fk_trigger -> trigname = fkconstraint -> constr_name ;
5287
+ fk_trigger -> trigname = fkconstraint -> conname ;
5300
5288
fk_trigger -> relation = fkconstraint -> pktable ;
5301
5289
fk_trigger -> before = false;
5302
5290
fk_trigger -> row = true;
@@ -5348,7 +5336,7 @@ createForeignKeyTriggers(Relation rel, FkConstraint *fkconstraint,
5348
5336
* UPDATE action on the referenced table.
5349
5337
*/
5350
5338
fk_trigger = makeNode (CreateTrigStmt );
5351
- fk_trigger -> trigname = fkconstraint -> constr_name ;
5339
+ fk_trigger -> trigname = fkconstraint -> conname ;
5352
5340
fk_trigger -> relation = fkconstraint -> pktable ;
5353
5341
fk_trigger -> before = false;
5354
5342
fk_trigger -> row = true;
0 commit comments