Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * pg_constraint.c
4 : : * routines to support manipulation of the pg_constraint relation
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/catalog/pg_constraint.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : : #include "postgres.h"
16 : :
17 : : #include "access/genam.h"
18 : : #include "access/gist.h"
19 : : #include "access/htup_details.h"
20 : : #include "access/sysattr.h"
21 : : #include "access/table.h"
22 : : #include "catalog/catalog.h"
23 : : #include "catalog/dependency.h"
24 : : #include "catalog/heap.h"
25 : : #include "catalog/indexing.h"
26 : : #include "catalog/objectaccess.h"
27 : : #include "catalog/pg_constraint.h"
28 : : #include "catalog/pg_operator.h"
29 : : #include "catalog/pg_type.h"
30 : : #include "commands/defrem.h"
31 : : #include "common/int.h"
32 : : #include "utils/array.h"
33 : : #include "utils/builtins.h"
34 : : #include "utils/fmgroids.h"
35 : : #include "utils/lsyscache.h"
36 : : #include "utils/rel.h"
37 : : #include "utils/syscache.h"
38 : :
39 : :
40 : : /*
41 : : * CreateConstraintEntry
42 : : * Create a constraint table entry.
43 : : *
44 : : * Subsidiary records (such as triggers or indexes to implement the
45 : : * constraint) are *not* created here. But we do make dependency links
46 : : * from the constraint to the things it depends on.
47 : : *
48 : : * The new constraint's OID is returned.
49 : : */
50 : : Oid
51 : 4026 : CreateConstraintEntry(const char *constraintName,
52 : : Oid constraintNamespace,
53 : : char constraintType,
54 : : bool isDeferrable,
55 : : bool isDeferred,
56 : : bool isEnforced,
57 : : bool isValidated,
58 : : Oid parentConstrId,
59 : : Oid relId,
60 : : const int16 *constraintKey,
61 : : int constraintNKeys,
62 : : int constraintNTotalKeys,
63 : : Oid domainId,
64 : : Oid indexRelId,
65 : : Oid foreignRelId,
66 : : const int16 *foreignKey,
67 : : const Oid *pfEqOp,
68 : : const Oid *ppEqOp,
69 : : const Oid *ffEqOp,
70 : : int foreignNKeys,
71 : : char foreignUpdateType,
72 : : char foreignDeleteType,
73 : : const int16 *fkDeleteSetCols,
74 : : int numFkDeleteSetCols,
75 : : char foreignMatchType,
76 : : const Oid *exclOp,
77 : : Node *conExpr,
78 : : const char *conBin,
79 : : bool conIsLocal,
80 : : int16 conInhCount,
81 : : bool conNoInherit,
82 : : bool conPeriod,
83 : : bool is_internal)
84 : : {
85 : 4026 : Relation conDesc;
86 : 4026 : Oid conOid;
87 : 4026 : HeapTuple tup;
88 : 4026 : bool nulls[Natts_pg_constraint];
89 : 4026 : Datum values[Natts_pg_constraint];
90 : 4026 : ArrayType *conkeyArray;
91 : 4026 : ArrayType *confkeyArray;
92 : 4026 : ArrayType *conpfeqopArray;
93 : 4026 : ArrayType *conppeqopArray;
94 : 4026 : ArrayType *conffeqopArray;
95 : 4026 : ArrayType *conexclopArray;
96 : 4026 : ArrayType *confdelsetcolsArray;
97 : 4026 : NameData cname;
98 : 4026 : int i;
99 : 4026 : ObjectAddress conobject;
100 : 4026 : ObjectAddresses *addrs_auto;
101 : 4026 : ObjectAddresses *addrs_normal;
102 : :
103 : : /* Only CHECK or FOREIGN KEY constraint can be not enforced */
104 [ + + + + : 4026 : Assert(isEnforced || constraintType == CONSTRAINT_CHECK ||
+ - ]
105 : : constraintType == CONSTRAINT_FOREIGN);
106 : : /* NOT ENFORCED constraint must be NOT VALID */
107 [ + + + - ]: 4026 : Assert(isEnforced || !isValidated);
108 : :
109 : 4026 : conDesc = table_open(ConstraintRelationId, RowExclusiveLock);
110 : :
111 [ + - ]: 4026 : Assert(constraintName);
112 : 4026 : namestrcpy(&cname, constraintName);
113 : :
114 : : /*
115 : : * Convert C arrays into Postgres arrays.
116 : : */
117 [ + + ]: 4026 : if (constraintNKeys > 0)
118 : : {
119 : 3908 : Datum *conkey;
120 : :
121 : 3908 : conkey = (Datum *) palloc(constraintNKeys * sizeof(Datum));
122 [ + + ]: 8377 : for (i = 0; i < constraintNKeys; i++)
123 : 4469 : conkey[i] = Int16GetDatum(constraintKey[i]);
124 : 3908 : conkeyArray = construct_array_builtin(conkey, constraintNKeys, INT2OID);
125 : 3908 : }
126 : : else
127 : 118 : conkeyArray = NULL;
128 : :
129 [ + + ]: 4026 : if (foreignNKeys > 0)
130 : : {
131 : 524 : Datum *fkdatums;
132 [ + - ]: 524 : int nkeys = Max(foreignNKeys, numFkDeleteSetCols);
133 : :
134 : 524 : fkdatums = (Datum *) palloc(nkeys * sizeof(Datum));
135 [ + + ]: 1238 : for (i = 0; i < foreignNKeys; i++)
136 : 714 : fkdatums[i] = Int16GetDatum(foreignKey[i]);
137 : 524 : confkeyArray = construct_array_builtin(fkdatums, foreignNKeys, INT2OID);
138 [ + + ]: 1238 : for (i = 0; i < foreignNKeys; i++)
139 : 714 : fkdatums[i] = ObjectIdGetDatum(pfEqOp[i]);
140 : 524 : conpfeqopArray = construct_array_builtin(fkdatums, foreignNKeys, OIDOID);
141 [ + + ]: 1238 : for (i = 0; i < foreignNKeys; i++)
142 : 714 : fkdatums[i] = ObjectIdGetDatum(ppEqOp[i]);
143 : 524 : conppeqopArray = construct_array_builtin(fkdatums, foreignNKeys, OIDOID);
144 [ + + ]: 1238 : for (i = 0; i < foreignNKeys; i++)
145 : 714 : fkdatums[i] = ObjectIdGetDatum(ffEqOp[i]);
146 : 524 : conffeqopArray = construct_array_builtin(fkdatums, foreignNKeys, OIDOID);
147 : :
148 [ + + ]: 524 : if (numFkDeleteSetCols > 0)
149 : : {
150 [ + + ]: 20 : for (i = 0; i < numFkDeleteSetCols; i++)
151 : 10 : fkdatums[i] = Int16GetDatum(fkDeleteSetCols[i]);
152 : 10 : confdelsetcolsArray = construct_array_builtin(fkdatums, numFkDeleteSetCols, INT2OID);
153 : 10 : }
154 : : else
155 : 514 : confdelsetcolsArray = NULL;
156 : 524 : }
157 : : else
158 : : {
159 : 3502 : confkeyArray = NULL;
160 : 3502 : conpfeqopArray = NULL;
161 : 3502 : conppeqopArray = NULL;
162 : 3502 : conffeqopArray = NULL;
163 : 3502 : confdelsetcolsArray = NULL;
164 : : }
165 : :
166 [ + + ]: 4026 : if (exclOp != NULL)
167 : : {
168 : 120 : Datum *opdatums;
169 : :
170 : 120 : opdatums = (Datum *) palloc(constraintNKeys * sizeof(Datum));
171 [ + + ]: 349 : for (i = 0; i < constraintNKeys; i++)
172 : 229 : opdatums[i] = ObjectIdGetDatum(exclOp[i]);
173 : 120 : conexclopArray = construct_array_builtin(opdatums, constraintNKeys, OIDOID);
174 : 120 : }
175 : : else
176 : 3906 : conexclopArray = NULL;
177 : :
178 : : /* initialize nulls and values */
179 [ + + ]: 116754 : for (i = 0; i < Natts_pg_constraint; i++)
180 : : {
181 : 112728 : nulls[i] = false;
182 : 112728 : values[i] = (Datum) 0;
183 : 112728 : }
184 : :
185 : 4026 : conOid = GetNewOidWithIndex(conDesc, ConstraintOidIndexId,
186 : : Anum_pg_constraint_oid);
187 : 4026 : values[Anum_pg_constraint_oid - 1] = ObjectIdGetDatum(conOid);
188 : 4026 : values[Anum_pg_constraint_conname - 1] = NameGetDatum(&cname);
189 : 4026 : values[Anum_pg_constraint_connamespace - 1] = ObjectIdGetDatum(constraintNamespace);
190 : 4026 : values[Anum_pg_constraint_contype - 1] = CharGetDatum(constraintType);
191 : 4026 : values[Anum_pg_constraint_condeferrable - 1] = BoolGetDatum(isDeferrable);
192 : 4026 : values[Anum_pg_constraint_condeferred - 1] = BoolGetDatum(isDeferred);
193 : 4026 : values[Anum_pg_constraint_conenforced - 1] = BoolGetDatum(isEnforced);
194 : 4026 : values[Anum_pg_constraint_convalidated - 1] = BoolGetDatum(isValidated);
195 : 4026 : values[Anum_pg_constraint_conrelid - 1] = ObjectIdGetDatum(relId);
196 : 4026 : values[Anum_pg_constraint_contypid - 1] = ObjectIdGetDatum(domainId);
197 : 4026 : values[Anum_pg_constraint_conindid - 1] = ObjectIdGetDatum(indexRelId);
198 : 4026 : values[Anum_pg_constraint_conparentid - 1] = ObjectIdGetDatum(parentConstrId);
199 : 4026 : values[Anum_pg_constraint_confrelid - 1] = ObjectIdGetDatum(foreignRelId);
200 : 4026 : values[Anum_pg_constraint_confupdtype - 1] = CharGetDatum(foreignUpdateType);
201 : 4026 : values[Anum_pg_constraint_confdeltype - 1] = CharGetDatum(foreignDeleteType);
202 : 4026 : values[Anum_pg_constraint_confmatchtype - 1] = CharGetDatum(foreignMatchType);
203 : 4026 : values[Anum_pg_constraint_conislocal - 1] = BoolGetDatum(conIsLocal);
204 : 4026 : values[Anum_pg_constraint_coninhcount - 1] = Int16GetDatum(conInhCount);
205 : 4026 : values[Anum_pg_constraint_connoinherit - 1] = BoolGetDatum(conNoInherit);
206 : 4026 : values[Anum_pg_constraint_conperiod - 1] = BoolGetDatum(conPeriod);
207 : :
208 [ + + ]: 4026 : if (conkeyArray)
209 : 3908 : values[Anum_pg_constraint_conkey - 1] = PointerGetDatum(conkeyArray);
210 : : else
211 : 118 : nulls[Anum_pg_constraint_conkey - 1] = true;
212 : :
213 [ + + ]: 4026 : if (confkeyArray)
214 : 524 : values[Anum_pg_constraint_confkey - 1] = PointerGetDatum(confkeyArray);
215 : : else
216 : 3502 : nulls[Anum_pg_constraint_confkey - 1] = true;
217 : :
218 [ + + ]: 4026 : if (conpfeqopArray)
219 : 524 : values[Anum_pg_constraint_conpfeqop - 1] = PointerGetDatum(conpfeqopArray);
220 : : else
221 : 3502 : nulls[Anum_pg_constraint_conpfeqop - 1] = true;
222 : :
223 [ + + ]: 4026 : if (conppeqopArray)
224 : 524 : values[Anum_pg_constraint_conppeqop - 1] = PointerGetDatum(conppeqopArray);
225 : : else
226 : 3502 : nulls[Anum_pg_constraint_conppeqop - 1] = true;
227 : :
228 [ + + ]: 4026 : if (conffeqopArray)
229 : 524 : values[Anum_pg_constraint_conffeqop - 1] = PointerGetDatum(conffeqopArray);
230 : : else
231 : 3502 : nulls[Anum_pg_constraint_conffeqop - 1] = true;
232 : :
233 [ + + ]: 4026 : if (confdelsetcolsArray)
234 : 10 : values[Anum_pg_constraint_confdelsetcols - 1] = PointerGetDatum(confdelsetcolsArray);
235 : : else
236 : 4016 : nulls[Anum_pg_constraint_confdelsetcols - 1] = true;
237 : :
238 [ + + ]: 4026 : if (conexclopArray)
239 : 120 : values[Anum_pg_constraint_conexclop - 1] = PointerGetDatum(conexclopArray);
240 : : else
241 : 3906 : nulls[Anum_pg_constraint_conexclop - 1] = true;
242 : :
243 [ + + ]: 4026 : if (conBin)
244 : 516 : values[Anum_pg_constraint_conbin - 1] = CStringGetTextDatum(conBin);
245 : : else
246 : 3510 : nulls[Anum_pg_constraint_conbin - 1] = true;
247 : :
248 : 4026 : tup = heap_form_tuple(RelationGetDescr(conDesc), values, nulls);
249 : :
250 : 4026 : CatalogTupleInsert(conDesc, tup);
251 : :
252 : 4026 : ObjectAddressSet(conobject, ConstraintRelationId, conOid);
253 : :
254 : 4026 : table_close(conDesc, RowExclusiveLock);
255 : :
256 : : /* Handle set of auto dependencies */
257 : 4026 : addrs_auto = new_object_addresses();
258 : :
259 [ + + ]: 4026 : if (OidIsValid(relId))
260 : : {
261 : : /*
262 : : * Register auto dependency from constraint to owning relation, or to
263 : : * specific column(s) if any are mentioned.
264 : : */
265 : 3936 : ObjectAddress relobject;
266 : :
267 [ + + ]: 3936 : if (constraintNTotalKeys > 0)
268 : : {
269 [ + + ]: 8415 : for (i = 0; i < constraintNTotalKeys; i++)
270 : : {
271 : 4507 : ObjectAddressSubSet(relobject, RelationRelationId, relId,
272 : : constraintKey[i]);
273 : 4507 : add_exact_object_address(&relobject, addrs_auto);
274 : 4507 : }
275 : 3908 : }
276 : : else
277 : : {
278 : 28 : ObjectAddressSet(relobject, RelationRelationId, relId);
279 : 28 : add_exact_object_address(&relobject, addrs_auto);
280 : : }
281 : 3936 : }
282 : :
283 [ + + ]: 4026 : if (OidIsValid(domainId))
284 : : {
285 : : /*
286 : : * Register auto dependency from constraint to owning domain
287 : : */
288 : 90 : ObjectAddress domobject;
289 : :
290 : 90 : ObjectAddressSet(domobject, TypeRelationId, domainId);
291 : 90 : add_exact_object_address(&domobject, addrs_auto);
292 : 90 : }
293 : :
294 : 4026 : record_object_address_dependencies(&conobject, addrs_auto,
295 : : DEPENDENCY_AUTO);
296 : 4026 : free_object_addresses(addrs_auto);
297 : :
298 : : /* Handle set of normal dependencies */
299 : 4026 : addrs_normal = new_object_addresses();
300 : :
301 [ + + ]: 4026 : if (OidIsValid(foreignRelId))
302 : : {
303 : : /*
304 : : * Register normal dependency from constraint to foreign relation, or
305 : : * to specific column(s) if any are mentioned.
306 : : */
307 : 524 : ObjectAddress relobject;
308 : :
309 [ + - ]: 524 : if (foreignNKeys > 0)
310 : : {
311 [ + + ]: 1238 : for (i = 0; i < foreignNKeys; i++)
312 : : {
313 : 714 : ObjectAddressSubSet(relobject, RelationRelationId,
314 : : foreignRelId, foreignKey[i]);
315 : 714 : add_exact_object_address(&relobject, addrs_normal);
316 : 714 : }
317 : 524 : }
318 : : else
319 : : {
320 : 0 : ObjectAddressSet(relobject, RelationRelationId, foreignRelId);
321 : 0 : add_exact_object_address(&relobject, addrs_normal);
322 : : }
323 : 524 : }
324 : :
325 [ + + + + ]: 4026 : if (OidIsValid(indexRelId) && constraintType == CONSTRAINT_FOREIGN)
326 : : {
327 : : /*
328 : : * Register normal dependency on the unique index that supports a
329 : : * foreign-key constraint. (Note: for indexes associated with unique
330 : : * or primary-key constraints, the dependency runs the other way, and
331 : : * is not made here.)
332 : : */
333 : 524 : ObjectAddress relobject;
334 : :
335 : 524 : ObjectAddressSet(relobject, RelationRelationId, indexRelId);
336 : 524 : add_exact_object_address(&relobject, addrs_normal);
337 : 524 : }
338 : :
339 [ + + ]: 4026 : if (foreignNKeys > 0)
340 : : {
341 : : /*
342 : : * Register normal dependencies on the equality operators that support
343 : : * a foreign-key constraint. If the PK and FK types are the same then
344 : : * all three operators for a column are the same; otherwise they are
345 : : * different.
346 : : */
347 : 524 : ObjectAddress oprobject;
348 : :
349 : 524 : oprobject.classId = OperatorRelationId;
350 : 524 : oprobject.objectSubId = 0;
351 : :
352 [ + + ]: 1238 : for (i = 0; i < foreignNKeys; i++)
353 : : {
354 : 714 : oprobject.objectId = pfEqOp[i];
355 : 714 : add_exact_object_address(&oprobject, addrs_normal);
356 [ + + ]: 714 : if (ppEqOp[i] != pfEqOp[i])
357 : : {
358 : 9 : oprobject.objectId = ppEqOp[i];
359 : 9 : add_exact_object_address(&oprobject, addrs_normal);
360 : 9 : }
361 [ + + ]: 714 : if (ffEqOp[i] != pfEqOp[i])
362 : : {
363 : 9 : oprobject.objectId = ffEqOp[i];
364 : 9 : add_exact_object_address(&oprobject, addrs_normal);
365 : 9 : }
366 : 714 : }
367 : 524 : }
368 : :
369 : 4026 : record_object_address_dependencies(&conobject, addrs_normal,
370 : : DEPENDENCY_NORMAL);
371 : 4026 : free_object_addresses(addrs_normal);
372 : :
373 : : /*
374 : : * We don't bother to register dependencies on the exclusion operators of
375 : : * an exclusion constraint. We assume they are members of the opclass
376 : : * supporting the index, so there's an indirect dependency via that. (This
377 : : * would be pretty dicey for cross-type operators, but exclusion operators
378 : : * can never be cross-type.)
379 : : */
380 : :
381 [ + + ]: 4026 : if (conExpr != NULL)
382 : : {
383 : : /*
384 : : * Register dependencies from constraint to objects mentioned in CHECK
385 : : * expression.
386 : : */
387 : 516 : recordDependencyOnSingleRelExpr(&conobject, conExpr, relId,
388 : : DEPENDENCY_NORMAL,
389 : : DEPENDENCY_NORMAL, false);
390 : 516 : }
391 : :
392 : : /* Post creation hook for new constraint */
393 [ + - ]: 4026 : InvokeObjectPostCreateHookArg(ConstraintRelationId, conOid, 0,
394 : : is_internal);
395 : :
396 : 8052 : return conOid;
397 : 4026 : }
398 : :
399 : : /*
400 : : * Test whether given name is currently used as a constraint name
401 : : * for the given object (relation or domain).
402 : : *
403 : : * This is used to decide whether to accept a user-specified constraint name.
404 : : * It is deliberately not the same test as ChooseConstraintName uses to decide
405 : : * whether an auto-generated name is OK: here, we will allow it unless there
406 : : * is an identical constraint name in use *on the same object*.
407 : : *
408 : : * NB: Caller should hold exclusive lock on the given object, else
409 : : * this test can be fooled by concurrent additions.
410 : : */
411 : : bool
412 : 1907 : ConstraintNameIsUsed(ConstraintCategory conCat, Oid objId,
413 : : const char *conname)
414 : : {
415 : 1907 : bool found;
416 : 1907 : Relation conDesc;
417 : 1907 : SysScanDesc conscan;
418 : 1907 : ScanKeyData skey[3];
419 : :
420 : 1907 : conDesc = table_open(ConstraintRelationId, AccessShareLock);
421 : :
422 : 3814 : ScanKeyInit(&skey[0],
423 : : Anum_pg_constraint_conrelid,
424 : : BTEqualStrategyNumber, F_OIDEQ,
425 [ + + ]: 1907 : ObjectIdGetDatum((conCat == CONSTRAINT_RELATION)
426 : 1875 : ? objId : InvalidOid));
427 : 3814 : ScanKeyInit(&skey[1],
428 : : Anum_pg_constraint_contypid,
429 : : BTEqualStrategyNumber, F_OIDEQ,
430 [ + + ]: 1907 : ObjectIdGetDatum((conCat == CONSTRAINT_DOMAIN)
431 : 32 : ? objId : InvalidOid));
432 : 3814 : ScanKeyInit(&skey[2],
433 : : Anum_pg_constraint_conname,
434 : : BTEqualStrategyNumber, F_NAMEEQ,
435 : 1907 : CStringGetDatum(conname));
436 : :
437 : 3814 : conscan = systable_beginscan(conDesc, ConstraintRelidTypidNameIndexId,
438 : 1907 : true, NULL, 3, skey);
439 : :
440 : : /* There can be at most one matching row */
441 : 1907 : found = (HeapTupleIsValid(systable_getnext(conscan)));
442 : :
443 : 1907 : systable_endscan(conscan);
444 : 1907 : table_close(conDesc, AccessShareLock);
445 : :
446 : 3814 : return found;
447 : 1907 : }
448 : :
449 : : /*
450 : : * Does any constraint of the given name exist in the given namespace?
451 : : *
452 : : * This is used for code that wants to match ChooseConstraintName's rule
453 : : * that we should avoid autogenerating duplicate constraint names within a
454 : : * namespace.
455 : : */
456 : : bool
457 : 868 : ConstraintNameExists(const char *conname, Oid namespaceid)
458 : : {
459 : 868 : bool found;
460 : 868 : Relation conDesc;
461 : 868 : SysScanDesc conscan;
462 : 868 : ScanKeyData skey[2];
463 : :
464 : 868 : conDesc = table_open(ConstraintRelationId, AccessShareLock);
465 : :
466 : 1736 : ScanKeyInit(&skey[0],
467 : : Anum_pg_constraint_conname,
468 : : BTEqualStrategyNumber, F_NAMEEQ,
469 : 868 : CStringGetDatum(conname));
470 : :
471 : 1736 : ScanKeyInit(&skey[1],
472 : : Anum_pg_constraint_connamespace,
473 : : BTEqualStrategyNumber, F_OIDEQ,
474 : 868 : ObjectIdGetDatum(namespaceid));
475 : :
476 : 1736 : conscan = systable_beginscan(conDesc, ConstraintNameNspIndexId, true,
477 : 868 : NULL, 2, skey);
478 : :
479 : 868 : found = (HeapTupleIsValid(systable_getnext(conscan)));
480 : :
481 : 868 : systable_endscan(conscan);
482 : 868 : table_close(conDesc, AccessShareLock);
483 : :
484 : 1736 : return found;
485 : 868 : }
486 : :
487 : : /*
488 : : * Select a nonconflicting name for a new constraint.
489 : : *
490 : : * The objective here is to choose a name that is unique within the
491 : : * specified namespace. Postgres does not require this, but the SQL
492 : : * spec does, and some apps depend on it. Therefore we avoid choosing
493 : : * default names that so conflict.
494 : : *
495 : : * name1, name2, and label are used the same way as for makeObjectName(),
496 : : * except that the label can't be NULL; digits will be appended to the label
497 : : * if needed to create a name that is unique within the specified namespace.
498 : : * If the given label is empty, we only consider names that include at least
499 : : * one added digit.
500 : : *
501 : : * 'others' can be a list of string names already chosen within the current
502 : : * command (but not yet reflected into the catalogs); we will not choose
503 : : * a duplicate of one of these either.
504 : : *
505 : : * Note: it is theoretically possible to get a collision anyway, if someone
506 : : * else chooses the same name concurrently. This is fairly unlikely to be
507 : : * a problem in practice, especially if one is holding an exclusive lock on
508 : : * the relation identified by name1.
509 : : *
510 : : * Returns a palloc'd string.
511 : : */
512 : : char *
513 : 1773 : ChooseConstraintName(const char *name1, const char *name2,
514 : : const char *label, Oid namespaceid,
515 : : List *others)
516 : : {
517 : 1773 : int pass = 0;
518 : 1773 : char *conname = NULL;
519 : 1773 : char modlabel[NAMEDATALEN];
520 : 1773 : Relation conDesc;
521 : 1773 : SysScanDesc conscan;
522 : 1773 : ScanKeyData skey[2];
523 : 1773 : bool found;
524 : 1773 : ListCell *l;
525 : :
526 : 1773 : conDesc = table_open(ConstraintRelationId, AccessShareLock);
527 : :
528 : : /* try the unmodified label first, unless it's empty */
529 [ + + ]: 1773 : if (label[0] != '\0')
530 : 1603 : strlcpy(modlabel, label, sizeof(modlabel));
531 : : else
532 : 170 : snprintf(modlabel, sizeof(modlabel), "%s%d", label, ++pass);
533 : :
534 : 2286 : for (;;)
535 : : {
536 : 2286 : conname = makeObjectName(name1, name2, modlabel);
537 : :
538 : 2286 : found = false;
539 : :
540 [ + + + + : 2581 : foreach(l, others)
+ + ]
541 : : {
542 [ + + ]: 295 : if (strcmp((char *) lfirst(l), conname) == 0)
543 : : {
544 : 3 : found = true;
545 : 3 : break;
546 : : }
547 : 292 : }
548 : :
549 [ + + ]: 2286 : if (!found)
550 : : {
551 : 4566 : ScanKeyInit(&skey[0],
552 : : Anum_pg_constraint_conname,
553 : : BTEqualStrategyNumber, F_NAMEEQ,
554 : 2283 : CStringGetDatum(conname));
555 : :
556 : 4566 : ScanKeyInit(&skey[1],
557 : : Anum_pg_constraint_connamespace,
558 : : BTEqualStrategyNumber, F_OIDEQ,
559 : 2283 : ObjectIdGetDatum(namespaceid));
560 : :
561 : 4566 : conscan = systable_beginscan(conDesc, ConstraintNameNspIndexId, true,
562 : 2283 : NULL, 2, skey);
563 : :
564 : 2283 : found = (HeapTupleIsValid(systable_getnext(conscan)));
565 : :
566 : 2283 : systable_endscan(conscan);
567 : 2283 : }
568 : :
569 [ + + ]: 2286 : if (!found)
570 : 1773 : break;
571 : :
572 : : /* found a conflict, so try a new name component */
573 : 513 : pfree(conname);
574 : 513 : snprintf(modlabel, sizeof(modlabel), "%s%d", label, ++pass);
575 : : }
576 : :
577 : 1773 : table_close(conDesc, AccessShareLock);
578 : :
579 : 3546 : return conname;
580 : 1773 : }
581 : :
582 : : /*
583 : : * Find and return a copy of the pg_constraint tuple that implements a
584 : : * (possibly not valid) not-null constraint for the given column of the
585 : : * given relation. If no such constraint exists, return NULL.
586 : : *
587 : : * XXX This would be easier if we had pg_attribute.notnullconstr with the OID
588 : : * of the constraint that implements the not-null constraint for that column.
589 : : * I'm not sure it's worth the catalog bloat and de-normalization, however.
590 : : */
591 : : HeapTuple
592 : 751 : findNotNullConstraintAttnum(Oid relid, AttrNumber attnum)
593 : : {
594 : 751 : Relation pg_constraint;
595 : 751 : HeapTuple conTup,
596 : 751 : retval = NULL;
597 : 751 : SysScanDesc scan;
598 : 751 : ScanKeyData key;
599 : :
600 : 751 : pg_constraint = table_open(ConstraintRelationId, AccessShareLock);
601 : 751 : ScanKeyInit(&key,
602 : : Anum_pg_constraint_conrelid,
603 : : BTEqualStrategyNumber, F_OIDEQ,
604 : 751 : ObjectIdGetDatum(relid));
605 : 751 : scan = systable_beginscan(pg_constraint, ConstraintRelidTypidNameIndexId,
606 : : true, NULL, 1, &key);
607 : :
608 [ + + ]: 1381 : while (HeapTupleIsValid(conTup = systable_getnext(scan)))
609 : : {
610 : 630 : Form_pg_constraint con = (Form_pg_constraint) GETSTRUCT(conTup);
611 : 630 : AttrNumber conkey;
612 : :
613 : : /*
614 : : * We're looking for a NOTNULL constraint with the column we're
615 : : * looking for as the sole element in conkey.
616 : : */
617 [ + + ]: 630 : if (con->contype != CONSTRAINT_NOTNULL)
618 : 201 : continue;
619 : :
620 : 429 : conkey = extractNotNullColumn(conTup);
621 [ + + ]: 429 : if (conkey != attnum)
622 : 249 : continue;
623 : :
624 : : /* Found it */
625 : 180 : retval = heap_copytuple(conTup);
626 : 180 : break;
627 [ - + ]: 630 : }
628 : :
629 : 751 : systable_endscan(scan);
630 : 751 : table_close(pg_constraint, AccessShareLock);
631 : :
632 : 1502 : return retval;
633 : 751 : }
634 : :
635 : : /*
636 : : * Find and return a copy of the pg_constraint tuple that implements a
637 : : * (possibly not valid) not-null constraint for the given column of the
638 : : * given relation.
639 : : * If no such column or no such constraint exists, return NULL.
640 : : */
641 : : HeapTuple
642 : 148 : findNotNullConstraint(Oid relid, const char *colname)
643 : : {
644 : 148 : AttrNumber attnum;
645 : :
646 : 148 : attnum = get_attnum(relid, colname);
647 [ + + ]: 148 : if (attnum <= InvalidAttrNumber)
648 : 6 : return NULL;
649 : :
650 : 142 : return findNotNullConstraintAttnum(relid, attnum);
651 : 148 : }
652 : :
653 : : /*
654 : : * Find and return the pg_constraint tuple that implements a validated
655 : : * not-null constraint for the given domain.
656 : : */
657 : : HeapTuple
658 : 2 : findDomainNotNullConstraint(Oid typid)
659 : : {
660 : 2 : Relation pg_constraint;
661 : 2 : HeapTuple conTup,
662 : 2 : retval = NULL;
663 : 2 : SysScanDesc scan;
664 : 2 : ScanKeyData key;
665 : :
666 : 2 : pg_constraint = table_open(ConstraintRelationId, AccessShareLock);
667 : 2 : ScanKeyInit(&key,
668 : : Anum_pg_constraint_contypid,
669 : : BTEqualStrategyNumber, F_OIDEQ,
670 : 2 : ObjectIdGetDatum(typid));
671 : 2 : scan = systable_beginscan(pg_constraint, ConstraintRelidTypidNameIndexId,
672 : : true, NULL, 1, &key);
673 : :
674 [ + + ]: 4 : while (HeapTupleIsValid(conTup = systable_getnext(scan)))
675 : : {
676 : 2 : Form_pg_constraint con = (Form_pg_constraint) GETSTRUCT(conTup);
677 : :
678 : : /*
679 : : * We're looking for a NOTNULL constraint that's marked validated.
680 : : */
681 [ - + ]: 2 : if (con->contype != CONSTRAINT_NOTNULL)
682 : 0 : continue;
683 [ + - ]: 2 : if (!con->convalidated)
684 : 0 : continue;
685 : :
686 : : /* Found it */
687 : 2 : retval = heap_copytuple(conTup);
688 : 2 : break;
689 [ - + ]: 2 : }
690 : :
691 : 2 : systable_endscan(scan);
692 : 2 : table_close(pg_constraint, AccessShareLock);
693 : :
694 : 4 : return retval;
695 : 2 : }
696 : :
697 : : /*
698 : : * Given a pg_constraint tuple for a not-null constraint, return the column
699 : : * number it is for.
700 : : */
701 : : AttrNumber
702 : 1675 : extractNotNullColumn(HeapTuple constrTup)
703 : : {
704 : 1675 : Datum adatum;
705 : 1675 : ArrayType *arr;
706 : :
707 : : /* only tuples for not-null constraints should be given */
708 [ + - ]: 1675 : Assert(((Form_pg_constraint) GETSTRUCT(constrTup))->contype == CONSTRAINT_NOTNULL);
709 : :
710 : 1675 : adatum = SysCacheGetAttrNotNull(CONSTROID, constrTup,
711 : : Anum_pg_constraint_conkey);
712 : 1675 : arr = DatumGetArrayTypeP(adatum); /* ensure not toasted */
713 [ + - ]: 1675 : if (ARR_NDIM(arr) != 1 ||
714 : 1675 : ARR_HASNULL(arr) ||
715 : 1675 : ARR_ELEMTYPE(arr) != INT2OID ||
716 : 1675 : ARR_DIMS(arr)[0] != 1)
717 [ # # # # ]: 0 : elog(ERROR, "conkey is not a 1-D smallint array");
718 : :
719 : : /* We leak the detoasted datum, but we don't care */
720 : :
721 [ - + ]: 1675 : return ((AttrNumber *) ARR_DATA_PTR(arr))[0];
722 : 1675 : }
723 : :
724 : : /*
725 : : * AdjustNotNullInheritance
726 : : * Adjust inheritance status for a single not-null constraint
727 : : *
728 : : * If no not-null constraint is found for the column, return false.
729 : : * Caller can create one.
730 : : *
731 : : * If a constraint exists but the connoinherit flag is not what the caller
732 : : * wants, throw an error about the incompatibility. If the desired
733 : : * constraint is valid but the existing constraint is not valid, also
734 : : * throw an error about that (the opposite case is acceptable).
735 : : *
736 : : * If everything checks out, we adjust conislocal/coninhcount and return
737 : : * true. If is_local is true we flip conislocal true, or do nothing if
738 : : * it's already true; otherwise we increment coninhcount by 1.
739 : : */
740 : : bool
741 : 440 : AdjustNotNullInheritance(Oid relid, AttrNumber attnum,
742 : : bool is_local, bool is_no_inherit, bool is_notvalid)
743 : : {
744 : 440 : HeapTuple tup;
745 : :
746 : 440 : tup = findNotNullConstraintAttnum(relid, attnum);
747 [ + + ]: 440 : if (HeapTupleIsValid(tup))
748 : : {
749 : 20 : Relation pg_constraint;
750 : 20 : Form_pg_constraint conform;
751 : 20 : bool changed = false;
752 : :
753 : 20 : pg_constraint = table_open(ConstraintRelationId, RowExclusiveLock);
754 : 20 : conform = (Form_pg_constraint) GETSTRUCT(tup);
755 : :
756 : : /*
757 : : * If the NO INHERIT flag we're asked for doesn't match what the
758 : : * existing constraint has, throw an error.
759 : : */
760 [ + + ]: 20 : if (is_no_inherit != conform->connoinherit)
761 [ + - + - ]: 5 : ereport(ERROR,
762 : : errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
763 : : errmsg("cannot change NO INHERIT status of NOT NULL constraint \"%s\" on relation \"%s\"",
764 : : NameStr(conform->conname), get_rel_name(relid)),
765 : : errhint("You might need to make the existing constraint inheritable using %s.",
766 : : "ALTER TABLE ... ALTER CONSTRAINT ... INHERIT"));
767 : :
768 : : /*
769 : : * Throw an error if the existing constraint is NOT VALID and caller
770 : : * wants a valid one.
771 : : */
772 [ + + + + ]: 15 : if (!is_notvalid && !conform->convalidated)
773 [ + - + - ]: 2 : ereport(ERROR,
774 : : errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
775 : : errmsg("incompatible NOT VALID constraint \"%s\" on relation \"%s\"",
776 : : NameStr(conform->conname), get_rel_name(relid)),
777 : : errhint("You might need to validate it using %s.",
778 : : "ALTER TABLE ... VALIDATE CONSTRAINT"));
779 : :
780 [ + + ]: 13 : if (!is_local)
781 : : {
782 [ + - + - ]: 18 : if (pg_add_s16_overflow(conform->coninhcount, 1,
783 : 9 : &conform->coninhcount))
784 [ # # # # ]: 0 : ereport(ERROR,
785 : : errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
786 : : errmsg("too many inheritance parents"));
787 : 9 : changed = true;
788 : 9 : }
789 [ + - ]: 4 : else if (!conform->conislocal)
790 : : {
791 : 0 : conform->conislocal = true;
792 : 0 : changed = true;
793 : 0 : }
794 : :
795 [ + + ]: 13 : if (changed)
796 : 9 : CatalogTupleUpdate(pg_constraint, &tup->t_self, tup);
797 : :
798 : 13 : table_close(pg_constraint, RowExclusiveLock);
799 : :
800 : 13 : return true;
801 : 13 : }
802 : :
803 : 420 : return false;
804 : 433 : }
805 : :
806 : : /*
807 : : * RelationGetNotNullConstraints
808 : : * Return the list of not-null constraints for the given rel
809 : : *
810 : : * Caller can request cooked constraints, or raw.
811 : : *
812 : : * This is seldom needed, so we just scan pg_constraint each time.
813 : : *
814 : : * 'include_noinh' determines whether to include NO INHERIT constraints or not.
815 : : */
816 : : List *
817 : 1641 : RelationGetNotNullConstraints(Oid relid, bool cooked, bool include_noinh)
818 : : {
819 : 1641 : List *notnulls = NIL;
820 : 1641 : Relation constrRel;
821 : 1641 : HeapTuple htup;
822 : 1641 : SysScanDesc conscan;
823 : 1641 : ScanKeyData skey;
824 : :
825 : 1641 : constrRel = table_open(ConstraintRelationId, AccessShareLock);
826 : 1641 : ScanKeyInit(&skey,
827 : : Anum_pg_constraint_conrelid,
828 : : BTEqualStrategyNumber, F_OIDEQ,
829 : 1641 : ObjectIdGetDatum(relid));
830 : 1641 : conscan = systable_beginscan(constrRel, ConstraintRelidTypidNameIndexId, true,
831 : : NULL, 1, &skey);
832 : :
833 [ + + ]: 2685 : while (HeapTupleIsValid(htup = systable_getnext(conscan)))
834 : : {
835 : 1044 : Form_pg_constraint conForm = (Form_pg_constraint) GETSTRUCT(htup);
836 : 1044 : AttrNumber colnum;
837 : :
838 [ + + ]: 1044 : if (conForm->contype != CONSTRAINT_NOTNULL)
839 : 557 : continue;
840 [ + + + + ]: 487 : if (conForm->connoinherit && !include_noinh)
841 : 10 : continue;
842 : :
843 : 477 : colnum = extractNotNullColumn(htup);
844 : :
845 [ + + ]: 477 : if (cooked)
846 : : {
847 : 383 : CookedConstraint *cooked;
848 : :
849 : 383 : cooked = palloc_object(CookedConstraint);
850 : :
851 : 383 : cooked->contype = CONSTR_NOTNULL;
852 : 383 : cooked->conoid = conForm->oid;
853 : 383 : cooked->name = pstrdup(NameStr(conForm->conname));
854 : 383 : cooked->attnum = colnum;
855 : 383 : cooked->expr = NULL;
856 : 383 : cooked->is_enforced = true;
857 : 383 : cooked->skip_validation = !conForm->convalidated;
858 : 383 : cooked->is_local = true;
859 : 383 : cooked->inhcount = 0;
860 : 383 : cooked->is_no_inherit = conForm->connoinherit;
861 : :
862 : 383 : notnulls = lappend(notnulls, cooked);
863 : 383 : }
864 : : else
865 : : {
866 : 94 : Constraint *constr;
867 : :
868 : 94 : constr = makeNode(Constraint);
869 : 94 : constr->contype = CONSTR_NOTNULL;
870 : 94 : constr->conname = pstrdup(NameStr(conForm->conname));
871 : 94 : constr->deferrable = false;
872 : 94 : constr->initdeferred = false;
873 : 94 : constr->location = -1;
874 : 94 : constr->keys = list_make1(makeString(get_attname(relid, colnum,
875 : : false)));
876 : 94 : constr->is_enforced = true;
877 : 94 : constr->skip_validation = !conForm->convalidated;
878 : 94 : constr->initially_valid = conForm->convalidated;
879 : 94 : constr->is_no_inherit = conForm->connoinherit;
880 : 94 : notnulls = lappend(notnulls, constr);
881 : 94 : }
882 [ - + + ]: 1044 : }
883 : :
884 : 1641 : systable_endscan(conscan);
885 : 1641 : table_close(constrRel, AccessShareLock);
886 : :
887 : 3282 : return notnulls;
888 : 1641 : }
889 : :
890 : :
891 : : /*
892 : : * Delete a single constraint record.
893 : : */
894 : : void
895 : 2997 : RemoveConstraintById(Oid conId)
896 : : {
897 : 2997 : Relation conDesc;
898 : 2997 : HeapTuple tup;
899 : 2997 : Form_pg_constraint con;
900 : :
901 : 2997 : conDesc = table_open(ConstraintRelationId, RowExclusiveLock);
902 : :
903 : 2997 : tup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(conId));
904 [ + - ]: 2997 : if (!HeapTupleIsValid(tup)) /* should not happen */
905 [ # # # # ]: 0 : elog(ERROR, "cache lookup failed for constraint %u", conId);
906 : 2997 : con = (Form_pg_constraint) GETSTRUCT(tup);
907 : :
908 : : /*
909 : : * Special processing depending on what the constraint is for.
910 : : */
911 [ + + ]: 2997 : if (OidIsValid(con->conrelid))
912 : : {
913 : 2940 : Relation rel;
914 : :
915 : : /*
916 : : * If the constraint is for a relation, open and exclusive-lock the
917 : : * relation it's for.
918 : : */
919 : 2940 : rel = table_open(con->conrelid, AccessExclusiveLock);
920 : :
921 : : /*
922 : : * We need to update the relchecks count if it is a check constraint
923 : : * being dropped. This update will force backends to rebuild relcache
924 : : * entries when we commit.
925 : : */
926 [ + + ]: 2940 : if (con->contype == CONSTRAINT_CHECK)
927 : : {
928 : 360 : Relation pgrel;
929 : 360 : HeapTuple relTup;
930 : 360 : Form_pg_class classForm;
931 : :
932 : 360 : pgrel = table_open(RelationRelationId, RowExclusiveLock);
933 : 360 : relTup = SearchSysCacheCopy1(RELOID,
934 : : ObjectIdGetDatum(con->conrelid));
935 [ + - ]: 360 : if (!HeapTupleIsValid(relTup))
936 [ # # # # ]: 0 : elog(ERROR, "cache lookup failed for relation %u",
937 : : con->conrelid);
938 : 360 : classForm = (Form_pg_class) GETSTRUCT(relTup);
939 : :
940 [ + - ]: 360 : if (classForm->relchecks > 0)
941 : 360 : classForm->relchecks--;
942 : : else
943 : : /* should not happen */
944 [ # # # # ]: 0 : elog(WARNING, "relation \"%s\" has relchecks = %d",
945 : : RelationGetRelationName(rel), classForm->relchecks);
946 : :
947 : 360 : CatalogTupleUpdate(pgrel, &relTup->t_self, relTup);
948 : :
949 : 360 : heap_freetuple(relTup);
950 : :
951 : 360 : table_close(pgrel, RowExclusiveLock);
952 : 360 : }
953 : :
954 : : /* Keep lock on constraint's rel until end of xact */
955 : 2940 : table_close(rel, NoLock);
956 : 2940 : }
957 [ + - ]: 57 : else if (OidIsValid(con->contypid))
958 : : {
959 : : /*
960 : : * XXX for now, do nothing special when dropping a domain constraint
961 : : *
962 : : * Probably there should be some form of locking on the domain type,
963 : : * but we have no such concept at the moment.
964 : : */
965 : 57 : }
966 : : else
967 [ # # # # ]: 0 : elog(ERROR, "constraint %u is not of a known type", conId);
968 : :
969 : : /* Fry the constraint itself */
970 : 2997 : CatalogTupleDelete(conDesc, &tup->t_self);
971 : :
972 : : /* Clean up */
973 : 2997 : ReleaseSysCache(tup);
974 : 2997 : table_close(conDesc, RowExclusiveLock);
975 : 2997 : }
976 : :
977 : : /*
978 : : * RenameConstraintById
979 : : * Rename a constraint.
980 : : *
981 : : * Note: this isn't intended to be a user-exposed function; it doesn't check
982 : : * permissions etc. Currently this is only invoked when renaming an index
983 : : * that is associated with a constraint, but it's made a little more general
984 : : * than that with the expectation of someday having ALTER TABLE RENAME
985 : : * CONSTRAINT.
986 : : */
987 : : void
988 : 16 : RenameConstraintById(Oid conId, const char *newname)
989 : : {
990 : 16 : Relation conDesc;
991 : 16 : HeapTuple tuple;
992 : 16 : Form_pg_constraint con;
993 : :
994 : 16 : conDesc = table_open(ConstraintRelationId, RowExclusiveLock);
995 : :
996 : 16 : tuple = SearchSysCacheCopy1(CONSTROID, ObjectIdGetDatum(conId));
997 [ + - ]: 16 : if (!HeapTupleIsValid(tuple))
998 [ # # # # ]: 0 : elog(ERROR, "cache lookup failed for constraint %u", conId);
999 : 16 : con = (Form_pg_constraint) GETSTRUCT(tuple);
1000 : :
1001 : : /*
1002 : : * For user-friendliness, check whether the name is already in use.
1003 : : */
1004 [ + + + - ]: 16 : if (OidIsValid(con->conrelid) &&
1005 : 15 : ConstraintNameIsUsed(CONSTRAINT_RELATION,
1006 : 15 : con->conrelid,
1007 : 15 : newname))
1008 [ # # # # ]: 0 : ereport(ERROR,
1009 : : (errcode(ERRCODE_DUPLICATE_OBJECT),
1010 : : errmsg("constraint \"%s\" for relation \"%s\" already exists",
1011 : : newname, get_rel_name(con->conrelid))));
1012 [ + + + - ]: 16 : if (OidIsValid(con->contypid) &&
1013 : 1 : ConstraintNameIsUsed(CONSTRAINT_DOMAIN,
1014 : 1 : con->contypid,
1015 : 1 : newname))
1016 [ # # # # ]: 0 : ereport(ERROR,
1017 : : (errcode(ERRCODE_DUPLICATE_OBJECT),
1018 : : errmsg("constraint \"%s\" for domain %s already exists",
1019 : : newname, format_type_be(con->contypid))));
1020 : :
1021 : : /* OK, do the rename --- tuple is a copy, so OK to scribble on it */
1022 : 16 : namestrcpy(&(con->conname), newname);
1023 : :
1024 : 16 : CatalogTupleUpdate(conDesc, &tuple->t_self, tuple);
1025 : :
1026 [ + - ]: 16 : InvokeObjectPostAlterHook(ConstraintRelationId, conId, 0);
1027 : :
1028 : 16 : heap_freetuple(tuple);
1029 : 16 : table_close(conDesc, RowExclusiveLock);
1030 : 16 : }
1031 : :
1032 : : /*
1033 : : * AlterConstraintNamespaces
1034 : : * Find any constraints belonging to the specified object,
1035 : : * and move them to the specified new namespace.
1036 : : *
1037 : : * isType indicates whether the owning object is a type or a relation.
1038 : : */
1039 : : void
1040 : 16 : AlterConstraintNamespaces(Oid ownerId, Oid oldNspId,
1041 : : Oid newNspId, bool isType, ObjectAddresses *objsMoved)
1042 : : {
1043 : 16 : Relation conRel;
1044 : 16 : ScanKeyData key[2];
1045 : 16 : SysScanDesc scan;
1046 : 16 : HeapTuple tup;
1047 : :
1048 : 16 : conRel = table_open(ConstraintRelationId, RowExclusiveLock);
1049 : :
1050 : 32 : ScanKeyInit(&key[0],
1051 : : Anum_pg_constraint_conrelid,
1052 : : BTEqualStrategyNumber, F_OIDEQ,
1053 [ + + ]: 16 : ObjectIdGetDatum(isType ? InvalidOid : ownerId));
1054 : 32 : ScanKeyInit(&key[1],
1055 : : Anum_pg_constraint_contypid,
1056 : : BTEqualStrategyNumber, F_OIDEQ,
1057 [ + + ]: 16 : ObjectIdGetDatum(isType ? ownerId : InvalidOid));
1058 : :
1059 : 32 : scan = systable_beginscan(conRel, ConstraintRelidTypidNameIndexId, true,
1060 : 16 : NULL, 2, key);
1061 : :
1062 [ + + ]: 39 : while (HeapTupleIsValid((tup = systable_getnext(scan))))
1063 : : {
1064 : 23 : Form_pg_constraint conform = (Form_pg_constraint) GETSTRUCT(tup);
1065 : 23 : ObjectAddress thisobj;
1066 : :
1067 : 23 : ObjectAddressSet(thisobj, ConstraintRelationId, conform->oid);
1068 : :
1069 [ - + ]: 23 : if (object_address_present(&thisobj, objsMoved))
1070 : 0 : continue;
1071 : :
1072 : : /* Don't update if the object is already part of the namespace */
1073 [ + - + + ]: 23 : if (conform->connamespace == oldNspId && oldNspId != newNspId)
1074 : : {
1075 : 18 : tup = heap_copytuple(tup);
1076 : 18 : conform = (Form_pg_constraint) GETSTRUCT(tup);
1077 : :
1078 : 18 : conform->connamespace = newNspId;
1079 : :
1080 : 18 : CatalogTupleUpdate(conRel, &tup->t_self, tup);
1081 : :
1082 : : /*
1083 : : * Note: currently, the constraint will not have its own
1084 : : * dependency on the namespace, so we don't need to do
1085 : : * changeDependencyFor().
1086 : : */
1087 : 18 : }
1088 : :
1089 [ + - ]: 23 : InvokeObjectPostAlterHook(ConstraintRelationId, thisobj.objectId, 0);
1090 : :
1091 : 23 : add_exact_object_address(&thisobj, objsMoved);
1092 [ - - + ]: 23 : }
1093 : :
1094 : 16 : systable_endscan(scan);
1095 : :
1096 : 16 : table_close(conRel, RowExclusiveLock);
1097 : 16 : }
1098 : :
1099 : : /*
1100 : : * ConstraintSetParentConstraint
1101 : : * Set a partition's constraint as child of its parent constraint,
1102 : : * or remove the linkage if parentConstrId is InvalidOid.
1103 : : *
1104 : : * This updates the constraint's pg_constraint row to show it as inherited, and
1105 : : * adds PARTITION dependencies to prevent the constraint from being deleted
1106 : : * on its own. Alternatively, reverse that.
1107 : : */
1108 : : void
1109 : 110 : ConstraintSetParentConstraint(Oid childConstrId,
1110 : : Oid parentConstrId,
1111 : : Oid childTableId)
1112 : : {
1113 : 110 : Relation constrRel;
1114 : 110 : Form_pg_constraint constrForm;
1115 : 110 : HeapTuple tuple,
1116 : : newtup;
1117 : 110 : ObjectAddress depender;
1118 : 110 : ObjectAddress referenced;
1119 : :
1120 : 110 : constrRel = table_open(ConstraintRelationId, RowExclusiveLock);
1121 : 110 : tuple = SearchSysCache1(CONSTROID, ObjectIdGetDatum(childConstrId));
1122 [ + - ]: 110 : if (!HeapTupleIsValid(tuple))
1123 [ # # # # ]: 0 : elog(ERROR, "cache lookup failed for constraint %u", childConstrId);
1124 : 110 : newtup = heap_copytuple(tuple);
1125 : 110 : constrForm = (Form_pg_constraint) GETSTRUCT(newtup);
1126 [ + + ]: 110 : if (OidIsValid(parentConstrId))
1127 : : {
1128 : : /* don't allow setting parent for a constraint that already has one */
1129 [ + - ]: 54 : Assert(constrForm->coninhcount == 0);
1130 [ + - ]: 54 : if (constrForm->conparentid != InvalidOid)
1131 [ # # # # ]: 0 : elog(ERROR, "constraint %u already has a parent constraint",
1132 : : childConstrId);
1133 : :
1134 : 54 : constrForm->conislocal = false;
1135 [ + - + - ]: 108 : if (pg_add_s16_overflow(constrForm->coninhcount, 1,
1136 : 54 : &constrForm->coninhcount))
1137 [ # # # # ]: 0 : ereport(ERROR,
1138 : : errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1139 : : errmsg("too many inheritance parents"));
1140 : :
1141 : 54 : constrForm->conparentid = parentConstrId;
1142 : :
1143 : 54 : CatalogTupleUpdate(constrRel, &tuple->t_self, newtup);
1144 : :
1145 : 54 : ObjectAddressSet(depender, ConstraintRelationId, childConstrId);
1146 : :
1147 : 54 : ObjectAddressSet(referenced, ConstraintRelationId, parentConstrId);
1148 : 54 : recordDependencyOn(&depender, &referenced, DEPENDENCY_PARTITION_PRI);
1149 : :
1150 : 54 : ObjectAddressSet(referenced, RelationRelationId, childTableId);
1151 : 54 : recordDependencyOn(&depender, &referenced, DEPENDENCY_PARTITION_SEC);
1152 : 54 : }
1153 : : else
1154 : : {
1155 : 56 : constrForm->coninhcount--;
1156 : 56 : constrForm->conislocal = true;
1157 : 56 : constrForm->conparentid = InvalidOid;
1158 : :
1159 : : /* Make sure there's no further inheritance. */
1160 [ + - ]: 56 : Assert(constrForm->coninhcount == 0);
1161 : :
1162 : 56 : CatalogTupleUpdate(constrRel, &tuple->t_self, newtup);
1163 : :
1164 : 56 : deleteDependencyRecordsForClass(ConstraintRelationId, childConstrId,
1165 : : ConstraintRelationId,
1166 : : DEPENDENCY_PARTITION_PRI);
1167 : 56 : deleteDependencyRecordsForClass(ConstraintRelationId, childConstrId,
1168 : : RelationRelationId,
1169 : : DEPENDENCY_PARTITION_SEC);
1170 : : }
1171 : :
1172 : 110 : ReleaseSysCache(tuple);
1173 : 110 : table_close(constrRel, RowExclusiveLock);
1174 : 110 : }
1175 : :
1176 : :
1177 : : /*
1178 : : * get_relation_constraint_oid
1179 : : * Find a constraint on the specified relation with the specified name.
1180 : : * Returns constraint's OID.
1181 : : */
1182 : : Oid
1183 : 91 : get_relation_constraint_oid(Oid relid, const char *conname, bool missing_ok)
1184 : : {
1185 : 91 : Relation pg_constraint;
1186 : 91 : HeapTuple tuple;
1187 : 91 : SysScanDesc scan;
1188 : 91 : ScanKeyData skey[3];
1189 : 91 : Oid conOid = InvalidOid;
1190 : :
1191 : 91 : pg_constraint = table_open(ConstraintRelationId, AccessShareLock);
1192 : :
1193 : 182 : ScanKeyInit(&skey[0],
1194 : : Anum_pg_constraint_conrelid,
1195 : : BTEqualStrategyNumber, F_OIDEQ,
1196 : 91 : ObjectIdGetDatum(relid));
1197 : 182 : ScanKeyInit(&skey[1],
1198 : : Anum_pg_constraint_contypid,
1199 : : BTEqualStrategyNumber, F_OIDEQ,
1200 : 91 : ObjectIdGetDatum(InvalidOid));
1201 : 182 : ScanKeyInit(&skey[2],
1202 : : Anum_pg_constraint_conname,
1203 : : BTEqualStrategyNumber, F_NAMEEQ,
1204 : 91 : CStringGetDatum(conname));
1205 : :
1206 : 182 : scan = systable_beginscan(pg_constraint, ConstraintRelidTypidNameIndexId, true,
1207 : 91 : NULL, 3, skey);
1208 : :
1209 : : /* There can be at most one matching row */
1210 [ + + ]: 91 : if (HeapTupleIsValid(tuple = systable_getnext(scan)))
1211 : 89 : conOid = ((Form_pg_constraint) GETSTRUCT(tuple))->oid;
1212 : :
1213 : 91 : systable_endscan(scan);
1214 : :
1215 : : /* If no such constraint exists, complain */
1216 [ + + - + ]: 91 : if (!OidIsValid(conOid) && !missing_ok)
1217 [ + - + - ]: 2 : ereport(ERROR,
1218 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
1219 : : errmsg("constraint \"%s\" for table \"%s\" does not exist",
1220 : : conname, get_rel_name(relid))));
1221 : :
1222 : 89 : table_close(pg_constraint, AccessShareLock);
1223 : :
1224 : 178 : return conOid;
1225 : 89 : }
1226 : :
1227 : : /*
1228 : : * get_relation_constraint_attnos
1229 : : * Find a constraint on the specified relation with the specified name
1230 : : * and return the constrained columns.
1231 : : *
1232 : : * Returns a Bitmapset of the column attnos of the constrained columns, with
1233 : : * attnos being offset by FirstLowInvalidHeapAttributeNumber so that system
1234 : : * columns can be represented.
1235 : : *
1236 : : * *constraintOid is set to the OID of the constraint, or InvalidOid on
1237 : : * failure.
1238 : : */
1239 : : Bitmapset *
1240 : 32 : get_relation_constraint_attnos(Oid relid, const char *conname,
1241 : : bool missing_ok, Oid *constraintOid)
1242 : : {
1243 : 32 : Bitmapset *conattnos = NULL;
1244 : 32 : Relation pg_constraint;
1245 : 32 : HeapTuple tuple;
1246 : 32 : SysScanDesc scan;
1247 : 32 : ScanKeyData skey[3];
1248 : :
1249 : : /* Set *constraintOid, to avoid complaints about uninitialized vars */
1250 : 32 : *constraintOid = InvalidOid;
1251 : :
1252 : 32 : pg_constraint = table_open(ConstraintRelationId, AccessShareLock);
1253 : :
1254 : 64 : ScanKeyInit(&skey[0],
1255 : : Anum_pg_constraint_conrelid,
1256 : : BTEqualStrategyNumber, F_OIDEQ,
1257 : 32 : ObjectIdGetDatum(relid));
1258 : 64 : ScanKeyInit(&skey[1],
1259 : : Anum_pg_constraint_contypid,
1260 : : BTEqualStrategyNumber, F_OIDEQ,
1261 : 32 : ObjectIdGetDatum(InvalidOid));
1262 : 64 : ScanKeyInit(&skey[2],
1263 : : Anum_pg_constraint_conname,
1264 : : BTEqualStrategyNumber, F_NAMEEQ,
1265 : 32 : CStringGetDatum(conname));
1266 : :
1267 : 64 : scan = systable_beginscan(pg_constraint, ConstraintRelidTypidNameIndexId, true,
1268 : 32 : NULL, 3, skey);
1269 : :
1270 : : /* There can be at most one matching row */
1271 [ - + ]: 32 : if (HeapTupleIsValid(tuple = systable_getnext(scan)))
1272 : : {
1273 : 32 : Datum adatum;
1274 : 32 : bool isNull;
1275 : :
1276 : 32 : *constraintOid = ((Form_pg_constraint) GETSTRUCT(tuple))->oid;
1277 : :
1278 : : /* Extract the conkey array, ie, attnums of constrained columns */
1279 : 64 : adatum = heap_getattr(tuple, Anum_pg_constraint_conkey,
1280 : 32 : RelationGetDescr(pg_constraint), &isNull);
1281 [ - + ]: 32 : if (!isNull)
1282 : : {
1283 : 32 : ArrayType *arr;
1284 : 32 : int numcols;
1285 : 32 : int16 *attnums;
1286 : 32 : int i;
1287 : :
1288 : 32 : arr = DatumGetArrayTypeP(adatum); /* ensure not toasted */
1289 : 32 : numcols = ARR_DIMS(arr)[0];
1290 [ + - ]: 32 : if (ARR_NDIM(arr) != 1 ||
1291 : 32 : numcols < 0 ||
1292 : 32 : ARR_HASNULL(arr) ||
1293 : 32 : ARR_ELEMTYPE(arr) != INT2OID)
1294 [ # # # # ]: 0 : elog(ERROR, "conkey is not a 1-D smallint array");
1295 [ - + ]: 32 : attnums = (int16 *) ARR_DATA_PTR(arr);
1296 : :
1297 : : /* Construct the result value */
1298 [ + + ]: 90 : for (i = 0; i < numcols; i++)
1299 : : {
1300 : 116 : conattnos = bms_add_member(conattnos,
1301 : 58 : attnums[i] - FirstLowInvalidHeapAttributeNumber);
1302 : 58 : }
1303 : 32 : }
1304 : 32 : }
1305 : :
1306 : 32 : systable_endscan(scan);
1307 : :
1308 : : /* If no such constraint exists, complain */
1309 [ - + # # ]: 32 : if (!OidIsValid(*constraintOid) && !missing_ok)
1310 [ # # # # ]: 0 : ereport(ERROR,
1311 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
1312 : : errmsg("constraint \"%s\" for table \"%s\" does not exist",
1313 : : conname, get_rel_name(relid))));
1314 : :
1315 : 32 : table_close(pg_constraint, AccessShareLock);
1316 : :
1317 : 64 : return conattnos;
1318 : 32 : }
1319 : :
1320 : : /*
1321 : : * Return the OID of the constraint enforced by the given index in the
1322 : : * given relation; or InvalidOid if no such index is cataloged.
1323 : : *
1324 : : * Much like get_constraint_index, this function is concerned only with the
1325 : : * one constraint that "owns" the given index. Therefore, constraints of
1326 : : * types other than unique, primary-key, and exclusion are ignored.
1327 : : */
1328 : : Oid
1329 : 305 : get_relation_idx_constraint_oid(Oid relationId, Oid indexId)
1330 : : {
1331 : 305 : Relation pg_constraint;
1332 : 305 : SysScanDesc scan;
1333 : 305 : ScanKeyData key;
1334 : 305 : HeapTuple tuple;
1335 : 305 : Oid constraintId = InvalidOid;
1336 : :
1337 : 305 : pg_constraint = table_open(ConstraintRelationId, AccessShareLock);
1338 : :
1339 : 305 : ScanKeyInit(&key,
1340 : : Anum_pg_constraint_conrelid,
1341 : : BTEqualStrategyNumber,
1342 : : F_OIDEQ,
1343 : 305 : ObjectIdGetDatum(relationId));
1344 : 305 : scan = systable_beginscan(pg_constraint, ConstraintRelidTypidNameIndexId,
1345 : : true, NULL, 1, &key);
1346 [ + + ]: 693 : while ((tuple = systable_getnext(scan)) != NULL)
1347 : : {
1348 : 388 : Form_pg_constraint constrForm;
1349 : :
1350 : 388 : constrForm = (Form_pg_constraint) GETSTRUCT(tuple);
1351 : :
1352 : : /* See above */
1353 [ + + ]: 388 : if (constrForm->contype != CONSTRAINT_PRIMARY &&
1354 [ + + + - ]: 237 : constrForm->contype != CONSTRAINT_UNIQUE &&
1355 : 229 : constrForm->contype != CONSTRAINT_EXCLUSION)
1356 : 229 : continue;
1357 : :
1358 [ + + ]: 159 : if (constrForm->conindid == indexId)
1359 : : {
1360 : 152 : constraintId = constrForm->oid;
1361 : 152 : break;
1362 : : }
1363 [ - + + ]: 388 : }
1364 : 305 : systable_endscan(scan);
1365 : :
1366 : 305 : table_close(pg_constraint, AccessShareLock);
1367 : 610 : return constraintId;
1368 : 305 : }
1369 : :
1370 : : /*
1371 : : * get_domain_constraint_oid
1372 : : * Find a constraint on the specified domain with the specified name.
1373 : : * Returns constraint's OID.
1374 : : */
1375 : : Oid
1376 : 11 : get_domain_constraint_oid(Oid typid, const char *conname, bool missing_ok)
1377 : : {
1378 : 11 : Relation pg_constraint;
1379 : 11 : HeapTuple tuple;
1380 : 11 : SysScanDesc scan;
1381 : 11 : ScanKeyData skey[3];
1382 : 11 : Oid conOid = InvalidOid;
1383 : :
1384 : 11 : pg_constraint = table_open(ConstraintRelationId, AccessShareLock);
1385 : :
1386 : 22 : ScanKeyInit(&skey[0],
1387 : : Anum_pg_constraint_conrelid,
1388 : : BTEqualStrategyNumber, F_OIDEQ,
1389 : 11 : ObjectIdGetDatum(InvalidOid));
1390 : 22 : ScanKeyInit(&skey[1],
1391 : : Anum_pg_constraint_contypid,
1392 : : BTEqualStrategyNumber, F_OIDEQ,
1393 : 11 : ObjectIdGetDatum(typid));
1394 : 22 : ScanKeyInit(&skey[2],
1395 : : Anum_pg_constraint_conname,
1396 : : BTEqualStrategyNumber, F_NAMEEQ,
1397 : 11 : CStringGetDatum(conname));
1398 : :
1399 : 22 : scan = systable_beginscan(pg_constraint, ConstraintRelidTypidNameIndexId, true,
1400 : 11 : NULL, 3, skey);
1401 : :
1402 : : /* There can be at most one matching row */
1403 [ + + ]: 11 : if (HeapTupleIsValid(tuple = systable_getnext(scan)))
1404 : 10 : conOid = ((Form_pg_constraint) GETSTRUCT(tuple))->oid;
1405 : :
1406 : 11 : systable_endscan(scan);
1407 : :
1408 : : /* If no such constraint exists, complain */
1409 [ + + - + ]: 11 : if (!OidIsValid(conOid) && !missing_ok)
1410 [ + - + - ]: 1 : ereport(ERROR,
1411 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
1412 : : errmsg("constraint \"%s\" for domain %s does not exist",
1413 : : conname, format_type_be(typid))));
1414 : :
1415 : 10 : table_close(pg_constraint, AccessShareLock);
1416 : :
1417 : 20 : return conOid;
1418 : 10 : }
1419 : :
1420 : : /*
1421 : : * get_primary_key_attnos
1422 : : * Identify the columns in a relation's primary key, if any.
1423 : : *
1424 : : * Returns a Bitmapset of the column attnos of the primary key's columns,
1425 : : * with attnos being offset by FirstLowInvalidHeapAttributeNumber so that
1426 : : * system columns can be represented.
1427 : : *
1428 : : * If there is no primary key, return NULL. We also return NULL if the pkey
1429 : : * constraint is deferrable and deferrableOk is false.
1430 : : *
1431 : : * *constraintOid is set to the OID of the pkey constraint, or InvalidOid
1432 : : * on failure.
1433 : : */
1434 : : Bitmapset *
1435 : 66 : get_primary_key_attnos(Oid relid, bool deferrableOk, Oid *constraintOid)
1436 : : {
1437 : 66 : Bitmapset *pkattnos = NULL;
1438 : 66 : Relation pg_constraint;
1439 : 66 : HeapTuple tuple;
1440 : 66 : SysScanDesc scan;
1441 : 66 : ScanKeyData skey[1];
1442 : :
1443 : : /* Set *constraintOid, to avoid complaints about uninitialized vars */
1444 : 66 : *constraintOid = InvalidOid;
1445 : :
1446 : : /* Scan pg_constraint for constraints of the target rel */
1447 : 66 : pg_constraint = table_open(ConstraintRelationId, AccessShareLock);
1448 : :
1449 : 132 : ScanKeyInit(&skey[0],
1450 : : Anum_pg_constraint_conrelid,
1451 : : BTEqualStrategyNumber, F_OIDEQ,
1452 : 66 : ObjectIdGetDatum(relid));
1453 : :
1454 : 132 : scan = systable_beginscan(pg_constraint, ConstraintRelidTypidNameIndexId, true,
1455 : 66 : NULL, 1, skey);
1456 : :
1457 [ + + ]: 173 : while (HeapTupleIsValid(tuple = systable_getnext(scan)))
1458 : : {
1459 : 107 : Form_pg_constraint con = (Form_pg_constraint) GETSTRUCT(tuple);
1460 : 107 : Datum adatum;
1461 : 107 : bool isNull;
1462 : 107 : ArrayType *arr;
1463 : 107 : int16 *attnums;
1464 : 107 : int numkeys;
1465 : 107 : int i;
1466 : :
1467 : : /* Skip constraints that are not PRIMARY KEYs */
1468 [ + + ]: 107 : if (con->contype != CONSTRAINT_PRIMARY)
1469 : 49 : continue;
1470 : :
1471 : : /*
1472 : : * If the primary key is deferrable, but we've been instructed to
1473 : : * ignore deferrable constraints, then we might as well give up
1474 : : * searching, since there can only be a single primary key on a table.
1475 : : */
1476 [ - + # # ]: 58 : if (con->condeferrable && !deferrableOk)
1477 : 0 : break;
1478 : :
1479 : : /* Extract the conkey array, ie, attnums of PK's columns */
1480 : 116 : adatum = heap_getattr(tuple, Anum_pg_constraint_conkey,
1481 : 58 : RelationGetDescr(pg_constraint), &isNull);
1482 [ + - ]: 58 : if (isNull)
1483 [ # # # # ]: 0 : elog(ERROR, "null conkey for constraint %u",
1484 : : ((Form_pg_constraint) GETSTRUCT(tuple))->oid);
1485 : 58 : arr = DatumGetArrayTypeP(adatum); /* ensure not toasted */
1486 : 58 : numkeys = ARR_DIMS(arr)[0];
1487 [ + - ]: 58 : if (ARR_NDIM(arr) != 1 ||
1488 : 58 : numkeys < 0 ||
1489 : 58 : ARR_HASNULL(arr) ||
1490 : 58 : ARR_ELEMTYPE(arr) != INT2OID)
1491 [ # # # # ]: 0 : elog(ERROR, "conkey is not a 1-D smallint array");
1492 [ - + ]: 58 : attnums = (int16 *) ARR_DATA_PTR(arr);
1493 : :
1494 : : /* Construct the result value */
1495 [ + + ]: 119 : for (i = 0; i < numkeys; i++)
1496 : : {
1497 : 122 : pkattnos = bms_add_member(pkattnos,
1498 : 61 : attnums[i] - FirstLowInvalidHeapAttributeNumber);
1499 : 61 : }
1500 : 58 : *constraintOid = ((Form_pg_constraint) GETSTRUCT(tuple))->oid;
1501 : :
1502 : : /* No need to search further */
1503 : 58 : break;
1504 [ - + ]: 107 : }
1505 : :
1506 : 66 : systable_endscan(scan);
1507 : :
1508 : 66 : table_close(pg_constraint, AccessShareLock);
1509 : :
1510 : 132 : return pkattnos;
1511 : 66 : }
1512 : :
1513 : : /*
1514 : : * Extract data from the pg_constraint tuple of a foreign-key constraint.
1515 : : *
1516 : : * All arguments save the first are output arguments. All output arguments
1517 : : * other than numfks, conkey and confkey can be passed as NULL if caller
1518 : : * doesn't need them.
1519 : : */
1520 : : void
1521 : 996 : DeconstructFkConstraintRow(HeapTuple tuple, int *numfks,
1522 : : AttrNumber *conkey, AttrNumber *confkey,
1523 : : Oid *pf_eq_oprs, Oid *pp_eq_oprs, Oid *ff_eq_oprs,
1524 : : int *num_fk_del_set_cols, AttrNumber *fk_del_set_cols)
1525 : : {
1526 : 996 : Datum adatum;
1527 : 996 : bool isNull;
1528 : 996 : ArrayType *arr;
1529 : 996 : int numkeys;
1530 : :
1531 : : /*
1532 : : * We expect the arrays to be 1-D arrays of the right types; verify that.
1533 : : * We don't need to use deconstruct_array() since the array data is just
1534 : : * going to look like a C array of values.
1535 : : */
1536 : 996 : adatum = SysCacheGetAttrNotNull(CONSTROID, tuple,
1537 : : Anum_pg_constraint_conkey);
1538 : 996 : arr = DatumGetArrayTypeP(adatum); /* ensure not toasted */
1539 [ + - ]: 996 : if (ARR_NDIM(arr) != 1 ||
1540 : 996 : ARR_HASNULL(arr) ||
1541 : 996 : ARR_ELEMTYPE(arr) != INT2OID)
1542 [ # # # # ]: 0 : elog(ERROR, "conkey is not a 1-D smallint array");
1543 : 996 : numkeys = ARR_DIMS(arr)[0];
1544 [ + - ]: 996 : if (numkeys <= 0 || numkeys > INDEX_MAX_KEYS)
1545 [ # # # # ]: 0 : elog(ERROR, "foreign key constraint cannot have %d columns", numkeys);
1546 [ - + ]: 996 : memcpy(conkey, ARR_DATA_PTR(arr), numkeys * sizeof(int16));
1547 [ - + ]: 996 : if (arr != DatumGetPointer(adatum))
1548 : 996 : pfree(arr); /* free de-toasted copy, if any */
1549 : :
1550 : 996 : adatum = SysCacheGetAttrNotNull(CONSTROID, tuple,
1551 : : Anum_pg_constraint_confkey);
1552 : 996 : arr = DatumGetArrayTypeP(adatum); /* ensure not toasted */
1553 [ + - ]: 996 : if (ARR_NDIM(arr) != 1 ||
1554 : 996 : ARR_DIMS(arr)[0] != numkeys ||
1555 : 996 : ARR_HASNULL(arr) ||
1556 : 996 : ARR_ELEMTYPE(arr) != INT2OID)
1557 [ # # # # ]: 0 : elog(ERROR, "confkey is not a 1-D smallint array");
1558 [ - + ]: 996 : memcpy(confkey, ARR_DATA_PTR(arr), numkeys * sizeof(int16));
1559 [ + - ]: 996 : if (arr != DatumGetPointer(adatum))
1560 : 996 : pfree(arr); /* free de-toasted copy, if any */
1561 : :
1562 [ - + ]: 996 : if (pf_eq_oprs)
1563 : : {
1564 : 996 : adatum = SysCacheGetAttrNotNull(CONSTROID, tuple,
1565 : : Anum_pg_constraint_conpfeqop);
1566 : 996 : arr = DatumGetArrayTypeP(adatum); /* ensure not toasted */
1567 : : /* see TryReuseForeignKey if you change the test below */
1568 [ + - ]: 996 : if (ARR_NDIM(arr) != 1 ||
1569 : 996 : ARR_DIMS(arr)[0] != numkeys ||
1570 : 996 : ARR_HASNULL(arr) ||
1571 : 996 : ARR_ELEMTYPE(arr) != OIDOID)
1572 [ # # # # ]: 0 : elog(ERROR, "conpfeqop is not a 1-D Oid array");
1573 [ - + ]: 996 : memcpy(pf_eq_oprs, ARR_DATA_PTR(arr), numkeys * sizeof(Oid));
1574 [ - + ]: 996 : if (arr != DatumGetPointer(adatum))
1575 : 996 : pfree(arr); /* free de-toasted copy, if any */
1576 : 996 : }
1577 : :
1578 [ + + ]: 996 : if (pp_eq_oprs)
1579 : : {
1580 : 480 : adatum = SysCacheGetAttrNotNull(CONSTROID, tuple,
1581 : : Anum_pg_constraint_conppeqop);
1582 : 480 : arr = DatumGetArrayTypeP(adatum); /* ensure not toasted */
1583 [ + - ]: 480 : if (ARR_NDIM(arr) != 1 ||
1584 : 480 : ARR_DIMS(arr)[0] != numkeys ||
1585 : 480 : ARR_HASNULL(arr) ||
1586 : 480 : ARR_ELEMTYPE(arr) != OIDOID)
1587 [ # # # # ]: 0 : elog(ERROR, "conppeqop is not a 1-D Oid array");
1588 [ + - ]: 480 : memcpy(pp_eq_oprs, ARR_DATA_PTR(arr), numkeys * sizeof(Oid));
1589 [ - + ]: 480 : if (arr != DatumGetPointer(adatum))
1590 : 480 : pfree(arr); /* free de-toasted copy, if any */
1591 : 480 : }
1592 : :
1593 [ + + ]: 996 : if (ff_eq_oprs)
1594 : : {
1595 : 480 : adatum = SysCacheGetAttrNotNull(CONSTROID, tuple,
1596 : : Anum_pg_constraint_conffeqop);
1597 : 480 : arr = DatumGetArrayTypeP(adatum); /* ensure not toasted */
1598 [ + - ]: 480 : if (ARR_NDIM(arr) != 1 ||
1599 : 480 : ARR_DIMS(arr)[0] != numkeys ||
1600 : 480 : ARR_HASNULL(arr) ||
1601 : 480 : ARR_ELEMTYPE(arr) != OIDOID)
1602 [ # # # # ]: 0 : elog(ERROR, "conffeqop is not a 1-D Oid array");
1603 [ - + ]: 480 : memcpy(ff_eq_oprs, ARR_DATA_PTR(arr), numkeys * sizeof(Oid));
1604 [ - + ]: 480 : if (arr != DatumGetPointer(adatum))
1605 : 480 : pfree(arr); /* free de-toasted copy, if any */
1606 : 480 : }
1607 : :
1608 [ + + ]: 996 : if (fk_del_set_cols)
1609 : : {
1610 : 480 : adatum = SysCacheGetAttr(CONSTROID, tuple,
1611 : : Anum_pg_constraint_confdelsetcols, &isNull);
1612 [ + + ]: 480 : if (isNull)
1613 : : {
1614 : 467 : *num_fk_del_set_cols = 0;
1615 : 467 : }
1616 : : else
1617 : : {
1618 : 13 : int num_delete_cols;
1619 : :
1620 : 13 : arr = DatumGetArrayTypeP(adatum); /* ensure not toasted */
1621 [ + - ]: 13 : if (ARR_NDIM(arr) != 1 ||
1622 : 13 : ARR_HASNULL(arr) ||
1623 : 13 : ARR_ELEMTYPE(arr) != INT2OID)
1624 [ # # # # ]: 0 : elog(ERROR, "confdelsetcols is not a 1-D smallint array");
1625 : 13 : num_delete_cols = ARR_DIMS(arr)[0];
1626 [ - + ]: 13 : memcpy(fk_del_set_cols, ARR_DATA_PTR(arr), num_delete_cols * sizeof(int16));
1627 [ - + ]: 13 : if (arr != DatumGetPointer(adatum))
1628 : 13 : pfree(arr); /* free de-toasted copy, if any */
1629 : :
1630 : 13 : *num_fk_del_set_cols = num_delete_cols;
1631 : 13 : }
1632 : 480 : }
1633 : :
1634 : 996 : *numfks = numkeys;
1635 : 996 : }
1636 : :
1637 : : /*
1638 : : * FindFKPeriodOpers -
1639 : : *
1640 : : * Looks up the operator oids used for the PERIOD part of a temporal foreign key.
1641 : : * The opclass should be the opclass of that PERIOD element.
1642 : : * Everything else is an output: containedbyoperoid is the ContainedBy operator for
1643 : : * types matching the PERIOD element.
1644 : : * aggedcontainedbyoperoid is also a ContainedBy operator,
1645 : : * but one whose rhs is a multirange.
1646 : : * That way foreign keys can compare fkattr <@ range_agg(pkattr).
1647 : : * intersectoperoid is used by NO ACTION constraints to trim the range being considered
1648 : : * to just what was updated/deleted.
1649 : : */
1650 : : void
1651 : 49 : FindFKPeriodOpers(Oid opclass,
1652 : : Oid *containedbyoperoid,
1653 : : Oid *aggedcontainedbyoperoid,
1654 : : Oid *intersectoperoid)
1655 : : {
1656 : 49 : Oid opfamily = InvalidOid;
1657 : 49 : Oid opcintype = InvalidOid;
1658 : 49 : StrategyNumber strat;
1659 : :
1660 : : /* Make sure we have a range or multirange. */
1661 [ + - ]: 49 : if (get_opclass_opfamily_and_input_type(opclass, &opfamily, &opcintype))
1662 : : {
1663 [ + + + - ]: 49 : if (opcintype != ANYRANGEOID && opcintype != ANYMULTIRANGEOID)
1664 [ # # # # ]: 0 : ereport(ERROR,
1665 : : errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1666 : : errmsg("invalid type for PERIOD part of foreign key"),
1667 : : errdetail("Only range and multirange are supported."));
1668 : :
1669 : 49 : }
1670 : : else
1671 [ # # # # ]: 0 : elog(ERROR, "cache lookup failed for opclass %u", opclass);
1672 : :
1673 : : /*
1674 : : * Look up the ContainedBy operator whose lhs and rhs are the opclass's
1675 : : * type. We use this to optimize RI checks: if the new value includes all
1676 : : * of the old value, then we can treat the attribute as if it didn't
1677 : : * change, and skip the RI check.
1678 : : */
1679 : 98 : GetOperatorFromCompareType(opclass,
1680 : : InvalidOid,
1681 : : COMPARE_CONTAINED_BY,
1682 : 49 : containedbyoperoid,
1683 : : &strat);
1684 : :
1685 : : /*
1686 : : * Now look up the ContainedBy operator. Its left arg must be the type of
1687 : : * the column (or rather of the opclass). Its right arg must match the
1688 : : * return type of the support proc.
1689 : : */
1690 : 98 : GetOperatorFromCompareType(opclass,
1691 : : ANYMULTIRANGEOID,
1692 : : COMPARE_CONTAINED_BY,
1693 : 49 : aggedcontainedbyoperoid,
1694 : : &strat);
1695 : :
1696 [ + + - ]: 49 : switch (opcintype)
1697 : : {
1698 : : case ANYRANGEOID:
1699 : 26 : *intersectoperoid = OID_RANGE_INTERSECT_RANGE_OP;
1700 : 26 : break;
1701 : : case ANYMULTIRANGEOID:
1702 : 23 : *intersectoperoid = OID_MULTIRANGE_INTERSECT_MULTIRANGE_OP;
1703 : 23 : break;
1704 : : default:
1705 [ # # # # ]: 0 : elog(ERROR, "unexpected opcintype: %u", opcintype);
1706 : 0 : }
1707 : 49 : }
1708 : :
1709 : : /*
1710 : : * Determine whether a relation can be proven functionally dependent on
1711 : : * a set of grouping columns. If so, return true and add the pg_constraint
1712 : : * OIDs of the constraints needed for the proof to the *constraintDeps list.
1713 : : *
1714 : : * grouping_columns is a list of grouping expressions, in which columns of
1715 : : * the rel of interest are Vars with the indicated varno/varlevelsup.
1716 : : *
1717 : : * Currently we only check to see if the rel has a primary key that is a
1718 : : * subset of the grouping_columns. We could also use plain unique constraints
1719 : : * if all their columns are known not null, but there's a problem: we need
1720 : : * to be able to represent the not-null-ness as part of the constraints added
1721 : : * to *constraintDeps. FIXME whenever not-null constraints get represented
1722 : : * in pg_constraint.
1723 : : */
1724 : : bool
1725 : 66 : check_functional_grouping(Oid relid,
1726 : : Index varno, Index varlevelsup,
1727 : : List *grouping_columns,
1728 : : List **constraintDeps)
1729 : : {
1730 : 66 : Bitmapset *pkattnos;
1731 : 66 : Bitmapset *groupbyattnos;
1732 : 66 : Oid constraintOid;
1733 : 66 : ListCell *gl;
1734 : :
1735 : : /* If the rel has no PK, then we can't prove functional dependency */
1736 : 66 : pkattnos = get_primary_key_attnos(relid, false, &constraintOid);
1737 [ + + ]: 66 : if (pkattnos == NULL)
1738 : 8 : return false;
1739 : :
1740 : : /* Identify all the rel's columns that appear in grouping_columns */
1741 : 58 : groupbyattnos = NULL;
1742 [ + - + + : 130 : foreach(gl, grouping_columns)
+ + ]
1743 : : {
1744 : 72 : Var *gvar = (Var *) lfirst(gl);
1745 : :
1746 [ + - ]: 72 : if (IsA(gvar, Var) &&
1747 [ + + - + ]: 72 : gvar->varno == varno &&
1748 : 45 : gvar->varlevelsup == varlevelsup)
1749 : 90 : groupbyattnos = bms_add_member(groupbyattnos,
1750 : 45 : gvar->varattno - FirstLowInvalidHeapAttributeNumber);
1751 : 72 : }
1752 : :
1753 [ + + ]: 58 : if (bms_is_subset(pkattnos, groupbyattnos))
1754 : : {
1755 : : /* The PK is a subset of grouping_columns, so we win */
1756 : 27 : *constraintDeps = lappend_oid(*constraintDeps, constraintOid);
1757 : 27 : return true;
1758 : : }
1759 : :
1760 : 31 : return false;
1761 : 66 : }
|