8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.296 2009/08/07 15:27:56 tgl Exp $
11
+ * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.297 2009/08/12 23:00:12 tgl Exp $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
@@ -5117,6 +5117,7 @@ transformFkeyCheckAttrs(Relation pkrel,
5117
5117
{
5118
5118
Oid indexoid = InvalidOid ;
5119
5119
bool found = false;
5120
+ bool found_deferrable = false;
5120
5121
List * indexoidlist ;
5121
5122
ListCell * indexoidscan ;
5122
5123
@@ -5143,12 +5144,11 @@ transformFkeyCheckAttrs(Relation pkrel,
5143
5144
indexStruct = (Form_pg_index ) GETSTRUCT (indexTuple );
5144
5145
5145
5146
/*
5146
- * Must have the right number of columns; must be unique (non
5147
- * deferrable) and not a partial index; forget it if there are any
5148
- * expressions, too
5147
+ * Must have the right number of columns; must be unique and not a
5148
+ * partial index; forget it if there are any expressions, too
5149
5149
*/
5150
5150
if (indexStruct -> indnatts == numattrs &&
5151
- indexStruct -> indisunique && indexStruct -> indimmediate &&
5151
+ indexStruct -> indisunique &&
5152
5152
heap_attisnull (indexTuple , Anum_pg_index_indpred ) &&
5153
5153
heap_attisnull (indexTuple , Anum_pg_index_indexprs ))
5154
5154
{
@@ -5198,17 +5198,40 @@ transformFkeyCheckAttrs(Relation pkrel,
5198
5198
break ;
5199
5199
}
5200
5200
}
5201
+
5202
+ /*
5203
+ * Refuse to use a deferrable unique/primary key. This is per
5204
+ * SQL spec, and there would be a lot of interesting semantic
5205
+ * problems if we tried to allow it.
5206
+ */
5207
+ if (found && !indexStruct -> indimmediate )
5208
+ {
5209
+ /*
5210
+ * Remember that we found an otherwise matching index, so
5211
+ * that we can generate a more appropriate error message.
5212
+ */
5213
+ found_deferrable = true;
5214
+ found = false;
5215
+ }
5201
5216
}
5202
5217
ReleaseSysCache (indexTuple );
5203
5218
if (found )
5204
5219
break ;
5205
5220
}
5206
5221
5207
5222
if (!found )
5208
- ereport (ERROR ,
5209
- (errcode (ERRCODE_INVALID_FOREIGN_KEY ),
5210
- errmsg ("there is no unique constraint matching given keys for referenced table \"%s\"" ,
5211
- RelationGetRelationName (pkrel ))));
5223
+ {
5224
+ if (found_deferrable )
5225
+ ereport (ERROR ,
5226
+ (errcode (ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE ),
5227
+ errmsg ("cannot use a deferrable unique constraint for referenced table \"%s\"" ,
5228
+ RelationGetRelationName (pkrel ))));
5229
+ else
5230
+ ereport (ERROR ,
5231
+ (errcode (ERRCODE_INVALID_FOREIGN_KEY ),
5232
+ errmsg ("there is no unique constraint matching given keys for referenced table \"%s\"" ,
5233
+ RelationGetRelationName (pkrel ))));
5234
+ }
5212
5235
5213
5236
list_free (indexoidlist );
5214
5237
0 commit comments