1 /*-------------------------------------------------------------------------
4 * code to create and destroy POSTGRES heap relations
6 * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/catalog/heap.c
15 * heap_create() - Create an uncataloged heap relation
16 * heap_create_with_catalog() - Create a cataloged relation
17 * heap_drop_with_catalog() - Removes named relation from catalogs
20 * this code taken from access/heap/create.c, which contains
21 * the old heap_create_with_catalog, amcreate, and amdestroy.
22 * those routines will soon call these routines using the function
24 * just like the poorly named "NewXXX" routines do. The
25 * "New" routines are all going to die soon, once and for all!
28 *-------------------------------------------------------------------------
32 #include "access/genam.h"
33 #include "access/multixact.h"
34 #include "access/relation.h"
35 #include "access/table.h"
36 #include "access/tableam.h"
37 #include "catalog/binary_upgrade.h"
38 #include "catalog/catalog.h"
39 #include "catalog/heap.h"
40 #include "catalog/index.h"
41 #include "catalog/objectaccess.h"
42 #include "catalog/partition.h"
43 #include "catalog/pg_am.h"
44 #include "catalog/pg_attrdef.h"
45 #include "catalog/pg_collation.h"
46 #include "catalog/pg_constraint.h"
47 #include "catalog/pg_foreign_table.h"
48 #include "catalog/pg_inherits.h"
49 #include "catalog/pg_namespace.h"
50 #include "catalog/pg_opclass.h"
51 #include "catalog/pg_partitioned_table.h"
52 #include "catalog/pg_statistic.h"
53 #include "catalog/pg_subscription_rel.h"
54 #include "catalog/pg_tablespace.h"
55 #include "catalog/pg_type.h"
56 #include "catalog/storage.h"
57 #include "commands/tablecmds.h"
58 #include "commands/typecmds.h"
59 #include "common/int.h"
60 #include "miscadmin.h"
61 #include "nodes/nodeFuncs.h"
62 #include "optimizer/optimizer.h"
63 #include "parser/parse_coerce.h"
64 #include "parser/parse_collate.h"
65 #include "parser/parse_expr.h"
66 #include "parser/parse_relation.h"
67 #include "parser/parsetree.h"
68 #include "partitioning/partdesc.h"
70 #include "storage/lmgr.h"
71 #include "storage/predicate.h"
72 #include "utils/builtins.h"
73 #include "utils/fmgroids.h"
74 #include "utils/inval.h"
75 #include "utils/lsyscache.h"
76 #include "utils/syscache.h"
79 /* Potentially set by pg_upgrade_support functions */
80 Oid binary_upgrade_next_heap_pg_class_oid
= InvalidOid
;
81 Oid binary_upgrade_next_toast_pg_class_oid
= InvalidOid
;
82 RelFileNumber binary_upgrade_next_heap_pg_class_relfilenumber
= InvalidRelFileNumber
;
83 RelFileNumber binary_upgrade_next_toast_pg_class_relfilenumber
= InvalidRelFileNumber
;
85 static void AddNewRelationTuple(Relation pg_class_desc
,
86 Relation new_rel_desc
,
92 TransactionId relfrozenxid
,
93 TransactionId relminmxid
,
96 static ObjectAddress
AddNewRelationType(const char *typeName
,
103 static void RelationRemoveInheritance(Oid relid
);
104 static Oid
StoreRelCheck(Relation rel
, const char *ccname
, Node
*expr
,
105 bool is_validated
, bool is_local
, int inhcount
,
106 bool is_no_inherit
, bool is_internal
);
107 static void StoreConstraints(Relation rel
, List
*cooked_constraints
,
109 static bool MergeWithExistingConstraint(Relation rel
, const char *ccname
, Node
*expr
,
110 bool allow_merge
, bool is_local
,
111 bool is_initially_valid
,
113 static void SetRelationNumChecks(Relation rel
, int numchecks
);
114 static Node
*cookConstraint(ParseState
*pstate
,
115 Node
*raw_constraint
,
119 /* ----------------------------------------------------------------
120 * XXX UGLY HARD CODED BADNESS FOLLOWS XXX
122 * these should all be moved to someplace in the lib/catalog
123 * module, if not obliterated first.
124 * ----------------------------------------------------------------
130 * Should the system special case these attributes in the future?
131 * Advantage: consume much less space in the ATTRIBUTE relation.
132 * Disadvantage: special cases will be all over the place.
136 * The initializers below do not include trailing variable length fields,
137 * but that's OK - we're never going to reference anything beyond the
138 * fixed-size portion of the structure anyway. Fields that can default
139 * to zeroes are also not mentioned.
142 static const FormData_pg_attribute a1
= {
145 .attlen
= sizeof(ItemPointerData
),
146 .attnum
= SelfItemPointerAttributeNumber
,
150 .attalign
= TYPALIGN_SHORT
,
151 .attstorage
= TYPSTORAGE_PLAIN
,
156 static const FormData_pg_attribute a2
= {
159 .attlen
= sizeof(TransactionId
),
160 .attnum
= MinTransactionIdAttributeNumber
,
164 .attalign
= TYPALIGN_INT
,
165 .attstorage
= TYPSTORAGE_PLAIN
,
170 static const FormData_pg_attribute a3
= {
173 .attlen
= sizeof(CommandId
),
174 .attnum
= MinCommandIdAttributeNumber
,
178 .attalign
= TYPALIGN_INT
,
179 .attstorage
= TYPSTORAGE_PLAIN
,
184 static const FormData_pg_attribute a4
= {
187 .attlen
= sizeof(TransactionId
),
188 .attnum
= MaxTransactionIdAttributeNumber
,
192 .attalign
= TYPALIGN_INT
,
193 .attstorage
= TYPSTORAGE_PLAIN
,
198 static const FormData_pg_attribute a5
= {
201 .attlen
= sizeof(CommandId
),
202 .attnum
= MaxCommandIdAttributeNumber
,
206 .attalign
= TYPALIGN_INT
,
207 .attstorage
= TYPSTORAGE_PLAIN
,
213 * We decided to call this attribute "tableoid" rather than say
214 * "classoid" on the basis that in the future there may be more than one
215 * table of a particular class/type. In any case table is still the word
218 static const FormData_pg_attribute a6
= {
219 .attname
= {"tableoid"},
221 .attlen
= sizeof(Oid
),
222 .attnum
= TableOidAttributeNumber
,
226 .attalign
= TYPALIGN_INT
,
227 .attstorage
= TYPSTORAGE_PLAIN
,
232 static const FormData_pg_attribute
*const SysAtt
[] = {&a1
, &a2
, &a3
, &a4
, &a5
, &a6
};
235 * This function returns a Form_pg_attribute pointer for a system attribute.
236 * Note that we elog if the presented attno is invalid, which would only
237 * happen if there's a problem upstream.
239 const FormData_pg_attribute
*
240 SystemAttributeDefinition(AttrNumber attno
)
242 if (attno
>= 0 || attno
< -(int) lengthof(SysAtt
))
243 elog(ERROR
, "invalid system attribute number %d", attno
);
244 return SysAtt
[-attno
- 1];
248 * If the given name is a system attribute name, return a Form_pg_attribute
249 * pointer for a prototype definition. If not, return NULL.
251 const FormData_pg_attribute
*
252 SystemAttributeByName(const char *attname
)
256 for (j
= 0; j
< (int) lengthof(SysAtt
); j
++)
258 const FormData_pg_attribute
*att
= SysAtt
[j
];
260 if (strcmp(NameStr(att
->attname
), attname
) == 0)
268 /* ----------------------------------------------------------------
269 * XXX END OF UGLY HARD CODED BADNESS XXX
270 * ---------------------------------------------------------------- */
273 /* ----------------------------------------------------------------
274 * heap_create - Create an uncataloged heap relation
276 * Note API change: the caller must now always provide the OID
277 * to use for the relation. The relfilenumber may be (and in
278 * the simplest cases is) left unspecified.
280 * create_storage indicates whether or not to create the storage.
281 * However, even if create_storage is true, no storage will be
282 * created if the relkind is one that doesn't have storage.
284 * rel->rd_rel is initialized by RelationBuildLocalRelation,
285 * and is mostly zeroes at return.
286 * ----------------------------------------------------------------
289 heap_create(const char *relname
,
293 RelFileNumber relfilenumber
,
298 bool shared_relation
,
299 bool mapped_relation
,
300 bool allow_system_table_mods
,
301 TransactionId
*relfrozenxid
,
302 MultiXactId
*relminmxid
,
307 /* The caller must have provided an OID for the relation. */
308 Assert(OidIsValid(relid
));
311 * Don't allow creating relations in pg_catalog directly, even though it
312 * is allowed to move user defined relations there. Semantics with search
313 * paths including pg_catalog are too confusing for now.
315 * But allow creating indexes on relations in pg_catalog even if
316 * allow_system_table_mods = off, upper layers already guarantee it's on a
317 * user defined relation, not a system one.
319 if (!allow_system_table_mods
&&
320 ((IsCatalogNamespace(relnamespace
) && relkind
!= RELKIND_INDEX
) ||
321 IsToastNamespace(relnamespace
)) &&
322 IsNormalProcessingMode())
324 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE
),
325 errmsg("permission denied to create \"%s.%s\"",
326 get_namespace_name(relnamespace
), relname
),
327 errdetail("System catalog modifications are currently disallowed.")));
329 *relfrozenxid
= InvalidTransactionId
;
330 *relminmxid
= InvalidMultiXactId
;
333 * Force reltablespace to zero if the relation kind does not support
334 * tablespaces. This is mainly just for cleanliness' sake.
336 if (!RELKIND_HAS_TABLESPACE(relkind
))
337 reltablespace
= InvalidOid
;
339 /* Don't create storage for relkinds without physical storage. */
340 if (!RELKIND_HAS_STORAGE(relkind
))
341 create_storage
= false;
345 * If relfilenumber is unspecified by the caller then create storage
346 * with oid same as relid.
348 if (!RelFileNumberIsValid(relfilenumber
))
349 relfilenumber
= relid
;
353 * Never allow a pg_class entry to explicitly specify the database's
354 * default tablespace in reltablespace; force it to zero instead. This
355 * ensures that if the database is cloned with a different default
356 * tablespace, the pg_class entry will still match where CREATE DATABASE
357 * will put the physically copied relation.
359 * Yes, this is a bit of a hack.
361 if (reltablespace
== MyDatabaseTableSpace
)
362 reltablespace
= InvalidOid
;
365 * build the relcache entry.
367 rel
= RelationBuildLocalRelation(relname
,
380 * Have the storage manager create the relation's disk file, if needed.
382 * For tables, the AM callback creates both the main and the init fork.
383 * For others, only the main fork is created; the other forks will be
388 if (RELKIND_HAS_TABLE_AM(rel
->rd_rel
->relkind
))
389 table_relation_set_new_filelocator(rel
, &rel
->rd_locator
,
391 relfrozenxid
, relminmxid
);
392 else if (RELKIND_HAS_STORAGE(rel
->rd_rel
->relkind
))
393 RelationCreateStorage(rel
->rd_locator
, relpersistence
, true);
399 * If a tablespace is specified, removal of that tablespace is normally
400 * protected by the existence of a physical file; but for relations with
401 * no files, add a pg_shdepend entry to account for that.
403 if (!create_storage
&& reltablespace
!= InvalidOid
)
404 recordDependencyOnTablespace(RelationRelationId
, relid
,
407 /* ensure that stats are dropped if transaction aborts */
408 pgstat_create_relation(rel
);
413 /* ----------------------------------------------------------------
414 * heap_create_with_catalog - Create a cataloged relation
416 * this is done in multiple steps:
418 * 1) CheckAttributeNamesTypes() is used to make certain the tuple
419 * descriptor contains a valid set of attribute names and types
421 * 2) pg_class is opened and get_relname_relid()
422 * performs a scan to ensure that no relation with the
423 * same name already exists.
425 * 3) heap_create() is called to create the new relation on disk.
427 * 4) TypeCreate() is called to define a new type corresponding
428 * to the new relation.
430 * 5) AddNewRelationTuple() is called to register the
431 * relation in pg_class.
433 * 6) AddNewAttributeTuples() is called to register the
434 * new relation's schema in pg_attribute.
436 * 7) StoreConstraints is called () - vadim 08/22/97
438 * 8) the relations are closed and the new relation's oid
441 * ----------------------------------------------------------------
444 /* --------------------------------
445 * CheckAttributeNamesTypes
447 * this is used to make certain the tuple descriptor contains a
448 * valid set of attribute names and datatypes. a problem simply
449 * generates ereport(ERROR) which aborts the current transaction.
451 * relkind is the relkind of the relation to be created.
452 * flags controls which datatypes are allowed, cf CheckAttributeType.
453 * --------------------------------
456 CheckAttributeNamesTypes(TupleDesc tupdesc
, char relkind
,
461 int natts
= tupdesc
->natts
;
463 /* Sanity check on column count */
464 if (natts
< 0 || natts
> MaxHeapAttributeNumber
)
466 (errcode(ERRCODE_TOO_MANY_COLUMNS
),
467 errmsg("tables can have at most %d columns",
468 MaxHeapAttributeNumber
)));
471 * first check for collision with system attribute names
473 * Skip this for a view or type relation, since those don't have system
476 if (relkind
!= RELKIND_VIEW
&& relkind
!= RELKIND_COMPOSITE_TYPE
)
478 for (i
= 0; i
< natts
; i
++)
480 Form_pg_attribute attr
= TupleDescAttr(tupdesc
, i
);
482 if (SystemAttributeByName(NameStr(attr
->attname
)) != NULL
)
484 (errcode(ERRCODE_DUPLICATE_COLUMN
),
485 errmsg("column name \"%s\" conflicts with a system column name",
486 NameStr(attr
->attname
))));
491 * next check for repeated attribute names
493 for (i
= 1; i
< natts
; i
++)
495 for (j
= 0; j
< i
; j
++)
497 if (strcmp(NameStr(TupleDescAttr(tupdesc
, j
)->attname
),
498 NameStr(TupleDescAttr(tupdesc
, i
)->attname
)) == 0)
500 (errcode(ERRCODE_DUPLICATE_COLUMN
),
501 errmsg("column name \"%s\" specified more than once",
502 NameStr(TupleDescAttr(tupdesc
, j
)->attname
))));
507 * next check the attribute types
509 for (i
= 0; i
< natts
; i
++)
511 CheckAttributeType(NameStr(TupleDescAttr(tupdesc
, i
)->attname
),
512 TupleDescAttr(tupdesc
, i
)->atttypid
,
513 TupleDescAttr(tupdesc
, i
)->attcollation
,
514 NIL
, /* assume we're creating a new rowtype */
519 /* --------------------------------
522 * Verify that the proposed datatype of an attribute is legal.
523 * This is needed mainly because there are types (and pseudo-types)
524 * in the catalogs that we do not support as elements of real tuples.
525 * We also check some other properties required of a table column.
527 * If the attribute is being proposed for addition to an existing table or
528 * composite type, pass a one-element list of the rowtype OID as
529 * containing_rowtypes. When checking a to-be-created rowtype, it's
530 * sufficient to pass NIL, because there could not be any recursive reference
531 * to a not-yet-existing rowtype.
533 * flags is a bitmask controlling which datatypes we allow. For the most
534 * part, pseudo-types are disallowed as attribute types, but there are some
535 * exceptions: ANYARRAYOID, RECORDOID, and RECORDARRAYOID can be allowed
536 * in some cases. (This works because values of those type classes are
537 * self-identifying to some extent. However, RECORDOID and RECORDARRAYOID
538 * are reliably identifiable only within a session, since the identity info
539 * may use a typmod that is only locally assigned. The caller is expected
540 * to know whether these cases are safe.)
542 * flags can also control the phrasing of the error messages. If
543 * CHKATYPE_IS_PARTKEY is specified, "attname" should be a partition key
544 * column number as text, not a real column name.
545 * --------------------------------
548 CheckAttributeType(const char *attname
,
549 Oid atttypid
, Oid attcollation
,
550 List
*containing_rowtypes
,
553 char att_typtype
= get_typtype(atttypid
);
556 /* since this function recurses, it could be driven to stack overflow */
559 if (att_typtype
== TYPTYPE_PSEUDO
)
562 * We disallow pseudo-type columns, with the exception of ANYARRAY,
563 * RECORD, and RECORD[] when the caller says that those are OK.
565 * We don't need to worry about recursive containment for RECORD and
566 * RECORD[] because (a) no named composite type should be allowed to
567 * contain those, and (b) two "anonymous" record types couldn't be
568 * considered to be the same type, so infinite recursion isn't
571 if (!((atttypid
== ANYARRAYOID
&& (flags
& CHKATYPE_ANYARRAY
)) ||
572 (atttypid
== RECORDOID
&& (flags
& CHKATYPE_ANYRECORD
)) ||
573 (atttypid
== RECORDARRAYOID
&& (flags
& CHKATYPE_ANYRECORD
))))
575 if (flags
& CHKATYPE_IS_PARTKEY
)
577 (errcode(ERRCODE_INVALID_TABLE_DEFINITION
),
578 /* translator: first %s is an integer not a name */
579 errmsg("partition key column %s has pseudo-type %s",
580 attname
, format_type_be(atttypid
))));
583 (errcode(ERRCODE_INVALID_TABLE_DEFINITION
),
584 errmsg("column \"%s\" has pseudo-type %s",
585 attname
, format_type_be(atttypid
))));
588 else if (att_typtype
== TYPTYPE_DOMAIN
)
591 * If it's a domain, recurse to check its base type.
593 CheckAttributeType(attname
, getBaseType(atttypid
), attcollation
,
597 else if (att_typtype
== TYPTYPE_COMPOSITE
)
600 * For a composite type, recurse into its attributes.
607 * Check for self-containment. Eventually we might be able to allow
608 * this (just return without complaint, if so) but it's not clear how
609 * many other places would require anti-recursion defenses before it
610 * would be safe to allow tables to contain their own rowtype.
612 if (list_member_oid(containing_rowtypes
, atttypid
))
614 (errcode(ERRCODE_INVALID_TABLE_DEFINITION
),
615 errmsg("composite type %s cannot be made a member of itself",
616 format_type_be(atttypid
))));
618 containing_rowtypes
= lappend_oid(containing_rowtypes
, atttypid
);
620 relation
= relation_open(get_typ_typrelid(atttypid
), AccessShareLock
);
622 tupdesc
= RelationGetDescr(relation
);
624 for (i
= 0; i
< tupdesc
->natts
; i
++)
626 Form_pg_attribute attr
= TupleDescAttr(tupdesc
, i
);
628 if (attr
->attisdropped
)
630 CheckAttributeType(NameStr(attr
->attname
),
631 attr
->atttypid
, attr
->attcollation
,
633 flags
& ~CHKATYPE_IS_PARTKEY
);
636 relation_close(relation
, AccessShareLock
);
638 containing_rowtypes
= list_delete_last(containing_rowtypes
);
640 else if (att_typtype
== TYPTYPE_RANGE
)
643 * If it's a range, recurse to check its subtype.
645 CheckAttributeType(attname
, get_range_subtype(atttypid
),
646 get_range_collation(atttypid
),
650 else if (OidIsValid((att_typelem
= get_element_type(atttypid
))))
653 * Must recurse into array types, too, in case they are composite.
655 CheckAttributeType(attname
, att_typelem
, attcollation
,
661 * This might not be strictly invalid per SQL standard, but it is pretty
662 * useless, and it cannot be dumped, so we must disallow it.
664 if (!OidIsValid(attcollation
) && type_is_collatable(atttypid
))
666 if (flags
& CHKATYPE_IS_PARTKEY
)
668 (errcode(ERRCODE_INVALID_TABLE_DEFINITION
),
669 /* translator: first %s is an integer not a name */
670 errmsg("no collation was derived for partition key column %s with collatable type %s",
671 attname
, format_type_be(atttypid
)),
672 errhint("Use the COLLATE clause to set the collation explicitly.")));
675 (errcode(ERRCODE_INVALID_TABLE_DEFINITION
),
676 errmsg("no collation was derived for column \"%s\" with collatable type %s",
677 attname
, format_type_be(atttypid
)),
678 errhint("Use the COLLATE clause to set the collation explicitly.")));
683 * InsertPgAttributeTuples
684 * Construct and insert a set of tuples in pg_attribute.
686 * Caller has already opened and locked pg_attribute. tupdesc contains the
687 * attributes to insert. attcacheoff is always initialized to -1.
688 * tupdesc_extra supplies the values for certain variable-length/nullable
689 * pg_attribute fields and must contain the same number of elements as tupdesc
690 * or be NULL. The other variable-length fields of pg_attribute are always
691 * initialized to null values.
693 * indstate is the index state for CatalogTupleInsertWithInfo. It can be
694 * passed as NULL, in which case we'll fetch the necessary info. (Don't do
695 * this when inserting multiple attributes, because it's a tad more
698 * new_rel_oid is the relation OID assigned to the attributes inserted.
699 * If set to InvalidOid, the relation OID from tupdesc is used instead.
702 InsertPgAttributeTuples(Relation pg_attribute_rel
,
705 const FormExtraData_pg_attribute tupdesc_extra
[],
706 CatalogIndexState indstate
)
708 TupleTableSlot
**slot
;
713 bool close_index
= false;
715 td
= RelationGetDescr(pg_attribute_rel
);
717 /* Initialize the number of slots to use */
718 nslots
= Min(tupdesc
->natts
,
719 (MAX_CATALOG_MULTI_INSERT_BYTES
/ sizeof(FormData_pg_attribute
)));
720 slot
= palloc(sizeof(TupleTableSlot
*) * nslots
);
721 for (int i
= 0; i
< nslots
; i
++)
722 slot
[i
] = MakeSingleTupleTableSlot(td
, &TTSOpsHeapTuple
);
724 while (natts
< tupdesc
->natts
)
726 Form_pg_attribute attrs
= TupleDescAttr(tupdesc
, natts
);
727 const FormExtraData_pg_attribute
*attrs_extra
= tupdesc_extra
? &tupdesc_extra
[natts
] : NULL
;
729 ExecClearTuple(slot
[slotCount
]);
731 memset(slot
[slotCount
]->tts_isnull
, false,
732 slot
[slotCount
]->tts_tupleDescriptor
->natts
* sizeof(bool));
734 if (new_rel_oid
!= InvalidOid
)
735 slot
[slotCount
]->tts_values
[Anum_pg_attribute_attrelid
- 1] = ObjectIdGetDatum(new_rel_oid
);
737 slot
[slotCount
]->tts_values
[Anum_pg_attribute_attrelid
- 1] = ObjectIdGetDatum(attrs
->attrelid
);
739 slot
[slotCount
]->tts_values
[Anum_pg_attribute_attname
- 1] = NameGetDatum(&attrs
->attname
);
740 slot
[slotCount
]->tts_values
[Anum_pg_attribute_atttypid
- 1] = ObjectIdGetDatum(attrs
->atttypid
);
741 slot
[slotCount
]->tts_values
[Anum_pg_attribute_attlen
- 1] = Int16GetDatum(attrs
->attlen
);
742 slot
[slotCount
]->tts_values
[Anum_pg_attribute_attnum
- 1] = Int16GetDatum(attrs
->attnum
);
743 slot
[slotCount
]->tts_values
[Anum_pg_attribute_attcacheoff
- 1] = Int32GetDatum(-1);
744 slot
[slotCount
]->tts_values
[Anum_pg_attribute_atttypmod
- 1] = Int32GetDatum(attrs
->atttypmod
);
745 slot
[slotCount
]->tts_values
[Anum_pg_attribute_attndims
- 1] = Int16GetDatum(attrs
->attndims
);
746 slot
[slotCount
]->tts_values
[Anum_pg_attribute_attbyval
- 1] = BoolGetDatum(attrs
->attbyval
);
747 slot
[slotCount
]->tts_values
[Anum_pg_attribute_attalign
- 1] = CharGetDatum(attrs
->attalign
);
748 slot
[slotCount
]->tts_values
[Anum_pg_attribute_attstorage
- 1] = CharGetDatum(attrs
->attstorage
);
749 slot
[slotCount
]->tts_values
[Anum_pg_attribute_attcompression
- 1] = CharGetDatum(attrs
->attcompression
);
750 slot
[slotCount
]->tts_values
[Anum_pg_attribute_attnotnull
- 1] = BoolGetDatum(attrs
->attnotnull
);
751 slot
[slotCount
]->tts_values
[Anum_pg_attribute_atthasdef
- 1] = BoolGetDatum(attrs
->atthasdef
);
752 slot
[slotCount
]->tts_values
[Anum_pg_attribute_atthasmissing
- 1] = BoolGetDatum(attrs
->atthasmissing
);
753 slot
[slotCount
]->tts_values
[Anum_pg_attribute_attidentity
- 1] = CharGetDatum(attrs
->attidentity
);
754 slot
[slotCount
]->tts_values
[Anum_pg_attribute_attgenerated
- 1] = CharGetDatum(attrs
->attgenerated
);
755 slot
[slotCount
]->tts_values
[Anum_pg_attribute_attisdropped
- 1] = BoolGetDatum(attrs
->attisdropped
);
756 slot
[slotCount
]->tts_values
[Anum_pg_attribute_attislocal
- 1] = BoolGetDatum(attrs
->attislocal
);
757 slot
[slotCount
]->tts_values
[Anum_pg_attribute_attinhcount
- 1] = Int16GetDatum(attrs
->attinhcount
);
758 slot
[slotCount
]->tts_values
[Anum_pg_attribute_attcollation
- 1] = ObjectIdGetDatum(attrs
->attcollation
);
761 slot
[slotCount
]->tts_values
[Anum_pg_attribute_attstattarget
- 1] = attrs_extra
->attstattarget
.value
;
762 slot
[slotCount
]->tts_isnull
[Anum_pg_attribute_attstattarget
- 1] = attrs_extra
->attstattarget
.isnull
;
764 slot
[slotCount
]->tts_values
[Anum_pg_attribute_attoptions
- 1] = attrs_extra
->attoptions
.value
;
765 slot
[slotCount
]->tts_isnull
[Anum_pg_attribute_attoptions
- 1] = attrs_extra
->attoptions
.isnull
;
769 slot
[slotCount
]->tts_isnull
[Anum_pg_attribute_attstattarget
- 1] = true;
770 slot
[slotCount
]->tts_isnull
[Anum_pg_attribute_attoptions
- 1] = true;
774 * The remaining fields are not set for new columns.
776 slot
[slotCount
]->tts_isnull
[Anum_pg_attribute_attacl
- 1] = true;
777 slot
[slotCount
]->tts_isnull
[Anum_pg_attribute_attfdwoptions
- 1] = true;
778 slot
[slotCount
]->tts_isnull
[Anum_pg_attribute_attmissingval
- 1] = true;
780 ExecStoreVirtualTuple(slot
[slotCount
]);
784 * If slots are full or the end of processing has been reached, insert
787 if (slotCount
== nslots
|| natts
== tupdesc
->natts
- 1)
789 /* fetch index info only when we know we need it */
792 indstate
= CatalogOpenIndexes(pg_attribute_rel
);
796 /* insert the new tuples and update the indexes */
797 CatalogTuplesMultiInsertWithInfo(pg_attribute_rel
, slot
, slotCount
,
806 CatalogCloseIndexes(indstate
);
807 for (int i
= 0; i
< nslots
; i
++)
808 ExecDropSingleTupleTableSlot(slot
[i
]);
812 /* --------------------------------
813 * AddNewAttributeTuples
815 * this registers the new relation's schema by adding
816 * tuples to pg_attribute.
817 * --------------------------------
820 AddNewAttributeTuples(Oid new_rel_oid
,
825 CatalogIndexState indstate
;
826 int natts
= tupdesc
->natts
;
827 ObjectAddress myself
,
831 * open pg_attribute and its indexes.
833 rel
= table_open(AttributeRelationId
, RowExclusiveLock
);
835 indstate
= CatalogOpenIndexes(rel
);
837 InsertPgAttributeTuples(rel
, tupdesc
, new_rel_oid
, NULL
, indstate
);
839 /* add dependencies on their datatypes and collations */
840 for (int i
= 0; i
< natts
; i
++)
842 /* Add dependency info */
843 ObjectAddressSubSet(myself
, RelationRelationId
, new_rel_oid
, i
+ 1);
844 ObjectAddressSet(referenced
, TypeRelationId
,
845 tupdesc
->attrs
[i
].atttypid
);
846 recordDependencyOn(&myself
, &referenced
, DEPENDENCY_NORMAL
);
848 /* The default collation is pinned, so don't bother recording it */
849 if (OidIsValid(tupdesc
->attrs
[i
].attcollation
) &&
850 tupdesc
->attrs
[i
].attcollation
!= DEFAULT_COLLATION_OID
)
852 ObjectAddressSet(referenced
, CollationRelationId
,
853 tupdesc
->attrs
[i
].attcollation
);
854 recordDependencyOn(&myself
, &referenced
, DEPENDENCY_NORMAL
);
859 * Next we add the system attributes. Skip all for a view or type
860 * relation. We don't bother with making datatype dependencies here,
861 * since presumably all these types are pinned.
863 if (relkind
!= RELKIND_VIEW
&& relkind
!= RELKIND_COMPOSITE_TYPE
)
867 td
= CreateTupleDesc(lengthof(SysAtt
), (FormData_pg_attribute
**) &SysAtt
);
869 InsertPgAttributeTuples(rel
, td
, new_rel_oid
, NULL
, indstate
);
876 CatalogCloseIndexes(indstate
);
878 table_close(rel
, RowExclusiveLock
);
881 /* --------------------------------
884 * Construct and insert a new tuple in pg_class.
886 * Caller has already opened and locked pg_class.
887 * Tuple data is taken from new_rel_desc->rd_rel, except for the
888 * variable-width fields which are not present in a cached reldesc.
889 * relacl and reloptions are passed in Datum form (to avoid having
890 * to reference the data types in heap.h). Pass (Datum) 0 to set them
892 * --------------------------------
895 InsertPgClassTuple(Relation pg_class_desc
,
896 Relation new_rel_desc
,
901 Form_pg_class rd_rel
= new_rel_desc
->rd_rel
;
902 Datum values
[Natts_pg_class
];
903 bool nulls
[Natts_pg_class
];
906 /* This is a tad tedious, but way cleaner than what we used to do... */
907 memset(values
, 0, sizeof(values
));
908 memset(nulls
, false, sizeof(nulls
));
910 values
[Anum_pg_class_oid
- 1] = ObjectIdGetDatum(new_rel_oid
);
911 values
[Anum_pg_class_relname
- 1] = NameGetDatum(&rd_rel
->relname
);
912 values
[Anum_pg_class_relnamespace
- 1] = ObjectIdGetDatum(rd_rel
->relnamespace
);
913 values
[Anum_pg_class_reltype
- 1] = ObjectIdGetDatum(rd_rel
->reltype
);
914 values
[Anum_pg_class_reloftype
- 1] = ObjectIdGetDatum(rd_rel
->reloftype
);
915 values
[Anum_pg_class_relowner
- 1] = ObjectIdGetDatum(rd_rel
->relowner
);
916 values
[Anum_pg_class_relam
- 1] = ObjectIdGetDatum(rd_rel
->relam
);
917 values
[Anum_pg_class_relfilenode
- 1] = ObjectIdGetDatum(rd_rel
->relfilenode
);
918 values
[Anum_pg_class_reltablespace
- 1] = ObjectIdGetDatum(rd_rel
->reltablespace
);
919 values
[Anum_pg_class_relpages
- 1] = Int32GetDatum(rd_rel
->relpages
);
920 values
[Anum_pg_class_reltuples
- 1] = Float4GetDatum(rd_rel
->reltuples
);
921 values
[Anum_pg_class_relallvisible
- 1] = Int32GetDatum(rd_rel
->relallvisible
);
922 values
[Anum_pg_class_reltoastrelid
- 1] = ObjectIdGetDatum(rd_rel
->reltoastrelid
);
923 values
[Anum_pg_class_relhasindex
- 1] = BoolGetDatum(rd_rel
->relhasindex
);
924 values
[Anum_pg_class_relisshared
- 1] = BoolGetDatum(rd_rel
->relisshared
);
925 values
[Anum_pg_class_relpersistence
- 1] = CharGetDatum(rd_rel
->relpersistence
);
926 values
[Anum_pg_class_relkind
- 1] = CharGetDatum(rd_rel
->relkind
);
927 values
[Anum_pg_class_relnatts
- 1] = Int16GetDatum(rd_rel
->relnatts
);
928 values
[Anum_pg_class_relchecks
- 1] = Int16GetDatum(rd_rel
->relchecks
);
929 values
[Anum_pg_class_relhasrules
- 1] = BoolGetDatum(rd_rel
->relhasrules
);
930 values
[Anum_pg_class_relhastriggers
- 1] = BoolGetDatum(rd_rel
->relhastriggers
);
931 values
[Anum_pg_class_relrowsecurity
- 1] = BoolGetDatum(rd_rel
->relrowsecurity
);
932 values
[Anum_pg_class_relforcerowsecurity
- 1] = BoolGetDatum(rd_rel
->relforcerowsecurity
);
933 values
[Anum_pg_class_relhassubclass
- 1] = BoolGetDatum(rd_rel
->relhassubclass
);
934 values
[Anum_pg_class_relispopulated
- 1] = BoolGetDatum(rd_rel
->relispopulated
);
935 values
[Anum_pg_class_relreplident
- 1] = CharGetDatum(rd_rel
->relreplident
);
936 values
[Anum_pg_class_relispartition
- 1] = BoolGetDatum(rd_rel
->relispartition
);
937 values
[Anum_pg_class_relrewrite
- 1] = ObjectIdGetDatum(rd_rel
->relrewrite
);
938 values
[Anum_pg_class_relfrozenxid
- 1] = TransactionIdGetDatum(rd_rel
->relfrozenxid
);
939 values
[Anum_pg_class_relminmxid
- 1] = MultiXactIdGetDatum(rd_rel
->relminmxid
);
940 if (relacl
!= (Datum
) 0)
941 values
[Anum_pg_class_relacl
- 1] = relacl
;
943 nulls
[Anum_pg_class_relacl
- 1] = true;
944 if (reloptions
!= (Datum
) 0)
945 values
[Anum_pg_class_reloptions
- 1] = reloptions
;
947 nulls
[Anum_pg_class_reloptions
- 1] = true;
949 /* relpartbound is set by updating this tuple, if necessary */
950 nulls
[Anum_pg_class_relpartbound
- 1] = true;
952 tup
= heap_form_tuple(RelationGetDescr(pg_class_desc
), values
, nulls
);
954 /* finally insert the new tuple, update the indexes, and clean up */
955 CatalogTupleInsert(pg_class_desc
, tup
);
960 /* --------------------------------
961 * AddNewRelationTuple
963 * this registers the new relation in the catalogs by
964 * adding a tuple to pg_class.
965 * --------------------------------
968 AddNewRelationTuple(Relation pg_class_desc
,
969 Relation new_rel_desc
,
975 TransactionId relfrozenxid
,
976 TransactionId relminmxid
,
980 Form_pg_class new_rel_reltup
;
983 * first we update some of the information in our uncataloged relation's
984 * relation descriptor.
986 new_rel_reltup
= new_rel_desc
->rd_rel
;
988 /* The relation is empty */
989 new_rel_reltup
->relpages
= 0;
990 new_rel_reltup
->reltuples
= -1;
991 new_rel_reltup
->relallvisible
= 0;
993 /* Sequences always have a known size */
994 if (relkind
== RELKIND_SEQUENCE
)
996 new_rel_reltup
->relpages
= 1;
997 new_rel_reltup
->reltuples
= 1;
1000 new_rel_reltup
->relfrozenxid
= relfrozenxid
;
1001 new_rel_reltup
->relminmxid
= relminmxid
;
1002 new_rel_reltup
->relowner
= relowner
;
1003 new_rel_reltup
->reltype
= new_type_oid
;
1004 new_rel_reltup
->reloftype
= reloftype
;
1006 /* relispartition is always set by updating this tuple later */
1007 new_rel_reltup
->relispartition
= false;
1009 /* fill rd_att's type ID with something sane even if reltype is zero */
1010 new_rel_desc
->rd_att
->tdtypeid
= new_type_oid
? new_type_oid
: RECORDOID
;
1011 new_rel_desc
->rd_att
->tdtypmod
= -1;
1013 /* Now build and insert the tuple */
1014 InsertPgClassTuple(pg_class_desc
, new_rel_desc
, new_rel_oid
,
1015 relacl
, reloptions
);
1019 /* --------------------------------
1020 * AddNewRelationType -
1022 * define a composite type corresponding to the new relation
1023 * --------------------------------
1025 static ObjectAddress
1026 AddNewRelationType(const char *typeName
,
1035 TypeCreate(new_row_type
, /* optional predetermined OID */
1036 typeName
, /* type name */
1037 typeNamespace
, /* type namespace */
1038 new_rel_oid
, /* relation oid */
1039 new_rel_kind
, /* relation kind */
1040 ownerid
, /* owner's ID */
1041 -1, /* internal size (varlena) */
1042 TYPTYPE_COMPOSITE
, /* type-type (composite) */
1043 TYPCATEGORY_COMPOSITE
, /* type-category (ditto) */
1044 false, /* composite types are never preferred */
1045 DEFAULT_TYPDELIM
, /* default array delimiter */
1046 F_RECORD_IN
, /* input procedure */
1047 F_RECORD_OUT
, /* output procedure */
1048 F_RECORD_RECV
, /* receive procedure */
1049 F_RECORD_SEND
, /* send procedure */
1050 InvalidOid
, /* typmodin procedure - none */
1051 InvalidOid
, /* typmodout procedure - none */
1052 InvalidOid
, /* analyze procedure - default */
1053 InvalidOid
, /* subscript procedure - none */
1054 InvalidOid
, /* array element type - irrelevant */
1055 false, /* this is not an array type */
1056 new_array_type
, /* array type if any */
1057 InvalidOid
, /* domain base type - irrelevant */
1058 NULL
, /* default value - none */
1059 NULL
, /* default binary representation */
1060 false, /* passed by reference */
1061 TYPALIGN_DOUBLE
, /* alignment - must be the largest! */
1062 TYPSTORAGE_EXTENDED
, /* fully TOASTable */
1064 0, /* array dimensions for typBaseType */
1065 false, /* Type NOT NULL */
1066 InvalidOid
); /* rowtypes never have a collation */
1069 /* --------------------------------
1070 * heap_create_with_catalog
1072 * creates a new cataloged relation. see comments above.
1075 * relname: name to give to new rel
1076 * relnamespace: OID of namespace it goes in
1077 * reltablespace: OID of tablespace it goes in
1078 * relid: OID to assign to new rel, or InvalidOid to select a new OID
1079 * reltypeid: OID to assign to rel's rowtype, or InvalidOid to select one
1080 * reloftypeid: if a typed table, OID of underlying type; else InvalidOid
1081 * ownerid: OID of new rel's owner
1082 * accessmtd: OID of new rel's access method
1083 * tupdesc: tuple descriptor (source of column definitions)
1084 * cooked_constraints: list of precooked check constraints and defaults
1085 * relkind: relkind for new rel
1086 * relpersistence: rel's persistence status (permanent, temp, or unlogged)
1087 * shared_relation: true if it's to be a shared relation
1088 * mapped_relation: true if the relation will use the relfilenumber map
1089 * oncommit: ON COMMIT marking (only relevant if it's a temp table)
1090 * reloptions: reloptions in Datum form, or (Datum) 0 if none
1091 * use_user_acl: true if should look for user-defined default permissions;
1092 * if false, relacl is always set NULL
1093 * allow_system_table_mods: true to allow creation in system namespaces
1094 * is_internal: is this a system-generated catalog?
1096 * Output parameters:
1097 * typaddress: if not null, gets the object address of the new pg_type entry
1098 * (this must be null if the relkind is one that doesn't get a pg_type entry)
1100 * Returns the OID of the new relation
1101 * --------------------------------
1104 heap_create_with_catalog(const char *relname
,
1113 List
*cooked_constraints
,
1115 char relpersistence
,
1116 bool shared_relation
,
1117 bool mapped_relation
,
1118 OnCommitAction oncommit
,
1121 bool allow_system_table_mods
,
1124 ObjectAddress
*typaddress
)
1126 Relation pg_class_desc
;
1127 Relation new_rel_desc
;
1133 /* By default set to InvalidOid unless overridden by binary-upgrade */
1134 RelFileNumber relfilenumber
= InvalidRelFileNumber
;
1135 TransactionId relfrozenxid
;
1136 MultiXactId relminmxid
;
1138 pg_class_desc
= table_open(RelationRelationId
, RowExclusiveLock
);
1143 Assert(IsNormalProcessingMode() || IsBootstrapProcessingMode());
1146 * Validate proposed tupdesc for the desired relkind. If
1147 * allow_system_table_mods is on, allow ANYARRAY to be used; this is a
1148 * hack to allow creating pg_statistic and cloning it during VACUUM FULL.
1150 CheckAttributeNamesTypes(tupdesc
, relkind
,
1151 allow_system_table_mods
? CHKATYPE_ANYARRAY
: 0);
1154 * This would fail later on anyway, if the relation already exists. But
1155 * by catching it here we can emit a nicer error message.
1157 existing_relid
= get_relname_relid(relname
, relnamespace
);
1158 if (existing_relid
!= InvalidOid
)
1160 (errcode(ERRCODE_DUPLICATE_TABLE
),
1161 errmsg("relation \"%s\" already exists", relname
)));
1164 * Since we are going to create a rowtype as well, also check for
1165 * collision with an existing type name. If there is one and it's an
1166 * autogenerated array, we can rename it out of the way; otherwise we can
1167 * at least give a good error message.
1169 old_type_oid
= GetSysCacheOid2(TYPENAMENSP
, Anum_pg_type_oid
,
1170 CStringGetDatum(relname
),
1171 ObjectIdGetDatum(relnamespace
));
1172 if (OidIsValid(old_type_oid
))
1174 if (!moveArrayTypeName(old_type_oid
, relname
, relnamespace
))
1176 (errcode(ERRCODE_DUPLICATE_OBJECT
),
1177 errmsg("type \"%s\" already exists", relname
),
1178 errhint("A relation has an associated type of the same name, "
1179 "so you must use a name that doesn't conflict "
1180 "with any existing type.")));
1184 * Shared relations must be in pg_global (last-ditch check)
1186 if (shared_relation
&& reltablespace
!= GLOBALTABLESPACE_OID
)
1187 elog(ERROR
, "shared relations must be placed in pg_global tablespace");
1190 * Allocate an OID for the relation, unless we were told what to use.
1192 * The OID will be the relfilenumber as well, so make sure it doesn't
1193 * collide with either pg_class OIDs or existing physical files.
1195 if (!OidIsValid(relid
))
1197 /* Use binary-upgrade override for pg_class.oid and relfilenumber */
1198 if (IsBinaryUpgrade
)
1201 * Indexes are not supported here; they use
1202 * binary_upgrade_next_index_pg_class_oid.
1204 Assert(relkind
!= RELKIND_INDEX
);
1205 Assert(relkind
!= RELKIND_PARTITIONED_INDEX
);
1207 if (relkind
== RELKIND_TOASTVALUE
)
1209 /* There might be no TOAST table, so we have to test for it. */
1210 if (OidIsValid(binary_upgrade_next_toast_pg_class_oid
))
1212 relid
= binary_upgrade_next_toast_pg_class_oid
;
1213 binary_upgrade_next_toast_pg_class_oid
= InvalidOid
;
1215 if (!RelFileNumberIsValid(binary_upgrade_next_toast_pg_class_relfilenumber
))
1217 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
1218 errmsg("toast relfilenumber value not set when in binary upgrade mode")));
1220 relfilenumber
= binary_upgrade_next_toast_pg_class_relfilenumber
;
1221 binary_upgrade_next_toast_pg_class_relfilenumber
= InvalidRelFileNumber
;
1226 if (!OidIsValid(binary_upgrade_next_heap_pg_class_oid
))
1228 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
1229 errmsg("pg_class heap OID value not set when in binary upgrade mode")));
1231 relid
= binary_upgrade_next_heap_pg_class_oid
;
1232 binary_upgrade_next_heap_pg_class_oid
= InvalidOid
;
1234 if (RELKIND_HAS_STORAGE(relkind
))
1236 if (!RelFileNumberIsValid(binary_upgrade_next_heap_pg_class_relfilenumber
))
1238 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
1239 errmsg("relfilenumber value not set when in binary upgrade mode")));
1241 relfilenumber
= binary_upgrade_next_heap_pg_class_relfilenumber
;
1242 binary_upgrade_next_heap_pg_class_relfilenumber
= InvalidRelFileNumber
;
1247 if (!OidIsValid(relid
))
1248 relid
= GetNewRelFileNumber(reltablespace
, pg_class_desc
,
1253 * Determine the relation's initial permissions.
1259 case RELKIND_RELATION
:
1261 case RELKIND_MATVIEW
:
1262 case RELKIND_FOREIGN_TABLE
:
1263 case RELKIND_PARTITIONED_TABLE
:
1264 relacl
= get_user_default_acl(OBJECT_TABLE
, ownerid
,
1267 case RELKIND_SEQUENCE
:
1268 relacl
= get_user_default_acl(OBJECT_SEQUENCE
, ownerid
,
1280 * Create the relcache entry (mostly dummy at this point) and the physical
1281 * disk file. (If we fail further down, it's the smgr's responsibility to
1282 * remove the disk file again.)
1284 * NB: Note that passing create_storage = true is correct even for binary
1285 * upgrade. The storage we create here will be replaced later, but we
1286 * need to have something on disk in the meanwhile.
1288 new_rel_desc
= heap_create(relname
,
1299 allow_system_table_mods
,
1304 Assert(relid
== RelationGetRelid(new_rel_desc
));
1306 new_rel_desc
->rd_rel
->relrewrite
= relrewrite
;
1309 * Decide whether to create a pg_type entry for the relation's rowtype.
1310 * These types are made except where the use of a relation as such is an
1311 * implementation detail: toast tables, sequences and indexes.
1313 if (!(relkind
== RELKIND_SEQUENCE
||
1314 relkind
== RELKIND_TOASTVALUE
||
1315 relkind
== RELKIND_INDEX
||
1316 relkind
== RELKIND_PARTITIONED_INDEX
))
1319 ObjectAddress new_type_addr
;
1323 * We'll make an array over the composite type, too. For largely
1324 * historical reasons, the array type's OID is assigned first.
1326 new_array_oid
= AssignTypeArrayOid();
1329 * Make the pg_type entry for the composite type. The OID of the
1330 * composite type can be preselected by the caller, but if reltypeid
1331 * is InvalidOid, we'll generate a new OID for it.
1333 * NOTE: we could get a unique-index failure here, in case someone
1334 * else is creating the same type name in parallel but hadn't
1335 * committed yet when we checked for a duplicate name above.
1337 new_type_addr
= AddNewRelationType(relname
,
1344 new_type_oid
= new_type_addr
.objectId
;
1346 *typaddress
= new_type_addr
;
1348 /* Now create the array type. */
1349 relarrayname
= makeArrayTypeName(relname
, relnamespace
);
1351 TypeCreate(new_array_oid
, /* force the type's OID to this */
1352 relarrayname
, /* Array type name */
1353 relnamespace
, /* Same namespace as parent */
1354 InvalidOid
, /* Not composite, no relationOid */
1355 0, /* relkind, also N/A here */
1356 ownerid
, /* owner's ID */
1357 -1, /* Internal size (varlena) */
1358 TYPTYPE_BASE
, /* Not composite - typelem is */
1359 TYPCATEGORY_ARRAY
, /* type-category (array) */
1360 false, /* array types are never preferred */
1361 DEFAULT_TYPDELIM
, /* default array delimiter */
1362 F_ARRAY_IN
, /* array input proc */
1363 F_ARRAY_OUT
, /* array output proc */
1364 F_ARRAY_RECV
, /* array recv (bin) proc */
1365 F_ARRAY_SEND
, /* array send (bin) proc */
1366 InvalidOid
, /* typmodin procedure - none */
1367 InvalidOid
, /* typmodout procedure - none */
1368 F_ARRAY_TYPANALYZE
, /* array analyze procedure */
1369 F_ARRAY_SUBSCRIPT_HANDLER
, /* array subscript procedure */
1370 new_type_oid
, /* array element type - the rowtype */
1371 true, /* yes, this is an array type */
1372 InvalidOid
, /* this has no array type */
1373 InvalidOid
, /* domain base type - irrelevant */
1374 NULL
, /* default value - none */
1375 NULL
, /* default binary representation */
1376 false, /* passed by reference */
1377 TYPALIGN_DOUBLE
, /* alignment - must be the largest! */
1378 TYPSTORAGE_EXTENDED
, /* fully TOASTable */
1380 0, /* array dimensions for typBaseType */
1381 false, /* Type NOT NULL */
1382 InvalidOid
); /* rowtypes never have a collation */
1384 pfree(relarrayname
);
1388 /* Caller should not be expecting a type to be created. */
1389 Assert(reltypeid
== InvalidOid
);
1390 Assert(typaddress
== NULL
);
1392 new_type_oid
= InvalidOid
;
1396 * now create an entry in pg_class for the relation.
1398 * NOTE: we could get a unique-index failure here, in case someone else is
1399 * creating the same relation name in parallel but hadn't committed yet
1400 * when we checked for a duplicate name above.
1402 AddNewRelationTuple(pg_class_desc
,
1411 PointerGetDatum(relacl
),
1415 * now add tuples to pg_attribute for the attributes in our new relation.
1417 AddNewAttributeTuples(relid
, new_rel_desc
->rd_att
, relkind
);
1420 * Make a dependency link to force the relation to be deleted if its
1421 * namespace is. Also make a dependency link to its owner, as well as
1422 * dependencies for any roles mentioned in the default ACL.
1424 * For composite types, these dependencies are tracked for the pg_type
1425 * entry, so we needn't record them here. Likewise, TOAST tables don't
1426 * need a namespace dependency (they live in a pinned namespace) nor an
1427 * owner dependency (they depend indirectly through the parent table), nor
1428 * should they have any ACL entries. The same applies for extension
1431 * Also, skip this in bootstrap mode, since we don't make dependencies
1432 * while bootstrapping.
1434 if (relkind
!= RELKIND_COMPOSITE_TYPE
&&
1435 relkind
!= RELKIND_TOASTVALUE
&&
1436 !IsBootstrapProcessingMode())
1438 ObjectAddress myself
,
1440 ObjectAddresses
*addrs
;
1442 ObjectAddressSet(myself
, RelationRelationId
, relid
);
1444 recordDependencyOnOwner(RelationRelationId
, relid
, ownerid
);
1446 recordDependencyOnNewAcl(RelationRelationId
, relid
, 0, ownerid
, relacl
);
1448 recordDependencyOnCurrentExtension(&myself
, false);
1450 addrs
= new_object_addresses();
1452 ObjectAddressSet(referenced
, NamespaceRelationId
, relnamespace
);
1453 add_exact_object_address(&referenced
, addrs
);
1457 ObjectAddressSet(referenced
, TypeRelationId
, reloftypeid
);
1458 add_exact_object_address(&referenced
, addrs
);
1462 * Make a dependency link to force the relation to be deleted if its
1465 * No need to add an explicit dependency for the toast table, as the
1466 * main table depends on it.
1468 if (RELKIND_HAS_TABLE_AM(relkind
) && relkind
!= RELKIND_TOASTVALUE
)
1470 ObjectAddressSet(referenced
, AccessMethodRelationId
, accessmtd
);
1471 add_exact_object_address(&referenced
, addrs
);
1474 record_object_address_dependencies(&myself
, addrs
, DEPENDENCY_NORMAL
);
1475 free_object_addresses(addrs
);
1478 /* Post creation hook for new relation */
1479 InvokeObjectPostCreateHookArg(RelationRelationId
, relid
, 0, is_internal
);
1482 * Store any supplied constraints and defaults.
1484 * NB: this may do a CommandCounterIncrement and rebuild the relcache
1485 * entry, so the relation must be valid and self-consistent at this point.
1486 * In particular, there are not yet constraints and defaults anywhere.
1488 StoreConstraints(new_rel_desc
, cooked_constraints
, is_internal
);
1491 * If there's a special on-commit action, remember it
1493 if (oncommit
!= ONCOMMIT_NOOP
)
1494 register_on_commit_action(relid
, oncommit
);
1497 * ok, the relation has been cataloged, so close our relations and return
1498 * the OID of the newly created relation.
1500 table_close(new_rel_desc
, NoLock
); /* do not unlock till end of xact */
1501 table_close(pg_class_desc
, RowExclusiveLock
);
1507 * RelationRemoveInheritance
1509 * Formerly, this routine checked for child relations and aborted the
1510 * deletion if any were found. Now we rely on the dependency mechanism
1511 * to check for or delete child relations. By the time we get here,
1512 * there are no children and we need only remove any pg_inherits rows
1513 * linking this relation to its parent(s).
1516 RelationRemoveInheritance(Oid relid
)
1518 Relation catalogRelation
;
1523 catalogRelation
= table_open(InheritsRelationId
, RowExclusiveLock
);
1526 Anum_pg_inherits_inhrelid
,
1527 BTEqualStrategyNumber
, F_OIDEQ
,
1528 ObjectIdGetDatum(relid
));
1530 scan
= systable_beginscan(catalogRelation
, InheritsRelidSeqnoIndexId
, true,
1533 while (HeapTupleIsValid(tuple
= systable_getnext(scan
)))
1534 CatalogTupleDelete(catalogRelation
, &tuple
->t_self
);
1536 systable_endscan(scan
);
1537 table_close(catalogRelation
, RowExclusiveLock
);
1541 * DeleteRelationTuple
1543 * Remove pg_class row for the given relid.
1545 * Note: this is shared by relation deletion and index deletion. It's
1546 * not intended for use anyplace else.
1549 DeleteRelationTuple(Oid relid
)
1551 Relation pg_class_desc
;
1554 /* Grab an appropriate lock on the pg_class relation */
1555 pg_class_desc
= table_open(RelationRelationId
, RowExclusiveLock
);
1557 tup
= SearchSysCache1(RELOID
, ObjectIdGetDatum(relid
));
1558 if (!HeapTupleIsValid(tup
))
1559 elog(ERROR
, "cache lookup failed for relation %u", relid
);
1561 /* delete the relation tuple from pg_class, and finish up */
1562 CatalogTupleDelete(pg_class_desc
, &tup
->t_self
);
1564 ReleaseSysCache(tup
);
1566 table_close(pg_class_desc
, RowExclusiveLock
);
1570 * DeleteAttributeTuples
1572 * Remove pg_attribute rows for the given relid.
1574 * Note: this is shared by relation deletion and index deletion. It's
1575 * not intended for use anyplace else.
1578 DeleteAttributeTuples(Oid relid
)
1585 /* Grab an appropriate lock on the pg_attribute relation */
1586 attrel
= table_open(AttributeRelationId
, RowExclusiveLock
);
1588 /* Use the index to scan only attributes of the target relation */
1589 ScanKeyInit(&key
[0],
1590 Anum_pg_attribute_attrelid
,
1591 BTEqualStrategyNumber
, F_OIDEQ
,
1592 ObjectIdGetDatum(relid
));
1594 scan
= systable_beginscan(attrel
, AttributeRelidNumIndexId
, true,
1597 /* Delete all the matching tuples */
1598 while ((atttup
= systable_getnext(scan
)) != NULL
)
1599 CatalogTupleDelete(attrel
, &atttup
->t_self
);
1601 /* Clean up after the scan */
1602 systable_endscan(scan
);
1603 table_close(attrel
, RowExclusiveLock
);
1607 * DeleteSystemAttributeTuples
1609 * Remove pg_attribute rows for system columns of the given relid.
1611 * Note: this is only used when converting a table to a view. Views don't
1612 * have system columns, so we should remove them from pg_attribute.
1615 DeleteSystemAttributeTuples(Oid relid
)
1622 /* Grab an appropriate lock on the pg_attribute relation */
1623 attrel
= table_open(AttributeRelationId
, RowExclusiveLock
);
1625 /* Use the index to scan only system attributes of the target relation */
1626 ScanKeyInit(&key
[0],
1627 Anum_pg_attribute_attrelid
,
1628 BTEqualStrategyNumber
, F_OIDEQ
,
1629 ObjectIdGetDatum(relid
));
1630 ScanKeyInit(&key
[1],
1631 Anum_pg_attribute_attnum
,
1632 BTLessEqualStrategyNumber
, F_INT2LE
,
1635 scan
= systable_beginscan(attrel
, AttributeRelidNumIndexId
, true,
1638 /* Delete all the matching tuples */
1639 while ((atttup
= systable_getnext(scan
)) != NULL
)
1640 CatalogTupleDelete(attrel
, &atttup
->t_self
);
1642 /* Clean up after the scan */
1643 systable_endscan(scan
);
1644 table_close(attrel
, RowExclusiveLock
);
1648 * RemoveAttributeById
1650 * This is the guts of ALTER TABLE DROP COLUMN: actually mark the attribute
1651 * deleted in pg_attribute. We also remove pg_statistic entries for it.
1652 * (Everything else needed, such as getting rid of any pg_attrdef entry,
1653 * is handled by dependency.c.)
1656 RemoveAttributeById(Oid relid
, AttrNumber attnum
)
1661 Form_pg_attribute attStruct
;
1662 char newattname
[NAMEDATALEN
];
1663 Datum valuesAtt
[Natts_pg_attribute
] = {0};
1664 bool nullsAtt
[Natts_pg_attribute
] = {0};
1665 bool replacesAtt
[Natts_pg_attribute
] = {0};
1668 * Grab an exclusive lock on the target table, which we will NOT release
1669 * until end of transaction. (In the simple case where we are directly
1670 * dropping this column, ATExecDropColumn already did this ... but when
1671 * cascading from a drop of some other object, we may not have any lock.)
1673 rel
= relation_open(relid
, AccessExclusiveLock
);
1675 attr_rel
= table_open(AttributeRelationId
, RowExclusiveLock
);
1677 tuple
= SearchSysCacheCopy2(ATTNUM
,
1678 ObjectIdGetDatum(relid
),
1679 Int16GetDatum(attnum
));
1680 if (!HeapTupleIsValid(tuple
)) /* shouldn't happen */
1681 elog(ERROR
, "cache lookup failed for attribute %d of relation %u",
1683 attStruct
= (Form_pg_attribute
) GETSTRUCT(tuple
);
1685 /* Mark the attribute as dropped */
1686 attStruct
->attisdropped
= true;
1689 * Set the type OID to invalid. A dropped attribute's type link cannot be
1690 * relied on (once the attribute is dropped, the type might be too).
1691 * Fortunately we do not need the type row --- the only really essential
1692 * information is the type's typlen and typalign, which are preserved in
1693 * the attribute's attlen and attalign. We set atttypid to zero here as a
1694 * means of catching code that incorrectly expects it to be valid.
1696 attStruct
->atttypid
= InvalidOid
;
1698 /* Remove any not-null constraint the column may have */
1699 attStruct
->attnotnull
= false;
1701 /* Unset this so no one tries to look up the generation expression */
1702 attStruct
->attgenerated
= '\0';
1705 * Change the column name to something that isn't likely to conflict
1707 snprintf(newattname
, sizeof(newattname
),
1708 "........pg.dropped.%d........", attnum
);
1709 namestrcpy(&(attStruct
->attname
), newattname
);
1711 /* Clear the missing value */
1712 attStruct
->atthasmissing
= false;
1713 nullsAtt
[Anum_pg_attribute_attmissingval
- 1] = true;
1714 replacesAtt
[Anum_pg_attribute_attmissingval
- 1] = true;
1717 * Clear the other nullable fields. This saves some space in pg_attribute
1718 * and removes no longer useful information.
1720 nullsAtt
[Anum_pg_attribute_attstattarget
- 1] = true;
1721 replacesAtt
[Anum_pg_attribute_attstattarget
- 1] = true;
1722 nullsAtt
[Anum_pg_attribute_attacl
- 1] = true;
1723 replacesAtt
[Anum_pg_attribute_attacl
- 1] = true;
1724 nullsAtt
[Anum_pg_attribute_attoptions
- 1] = true;
1725 replacesAtt
[Anum_pg_attribute_attoptions
- 1] = true;
1726 nullsAtt
[Anum_pg_attribute_attfdwoptions
- 1] = true;
1727 replacesAtt
[Anum_pg_attribute_attfdwoptions
- 1] = true;
1729 tuple
= heap_modify_tuple(tuple
, RelationGetDescr(attr_rel
),
1730 valuesAtt
, nullsAtt
, replacesAtt
);
1732 CatalogTupleUpdate(attr_rel
, &tuple
->t_self
, tuple
);
1735 * Because updating the pg_attribute row will trigger a relcache flush for
1736 * the target relation, we need not do anything else to notify other
1737 * backends of the change.
1740 table_close(attr_rel
, RowExclusiveLock
);
1742 RemoveStatistics(relid
, attnum
);
1744 relation_close(rel
, NoLock
);
1748 * heap_drop_with_catalog - removes specified relation from catalogs
1750 * Note that this routine is not responsible for dropping objects that are
1751 * linked to the pg_class entry via dependencies (for example, indexes and
1752 * constraints). Those are deleted by the dependency-tracing logic in
1753 * dependency.c before control gets here. In general, therefore, this routine
1754 * should never be called directly; go through performDeletion() instead.
1757 heap_drop_with_catalog(Oid relid
)
1761 Oid parentOid
= InvalidOid
,
1762 defaultPartOid
= InvalidOid
;
1765 * To drop a partition safely, we must grab exclusive lock on its parent,
1766 * because another backend might be about to execute a query on the parent
1767 * table. If it relies on previously cached partition descriptor, then it
1768 * could attempt to access the just-dropped relation as its partition. We
1769 * must therefore take a table lock strong enough to prevent all queries
1770 * on the table from proceeding until we commit and send out a
1771 * shared-cache-inval notice that will make them update their partition
1774 tuple
= SearchSysCache1(RELOID
, ObjectIdGetDatum(relid
));
1775 if (!HeapTupleIsValid(tuple
))
1776 elog(ERROR
, "cache lookup failed for relation %u", relid
);
1777 if (((Form_pg_class
) GETSTRUCT(tuple
))->relispartition
)
1780 * We have to lock the parent if the partition is being detached,
1781 * because it's possible that some query still has a partition
1782 * descriptor that includes this partition.
1784 parentOid
= get_partition_parent(relid
, true);
1785 LockRelationOid(parentOid
, AccessExclusiveLock
);
1788 * If this is not the default partition, dropping it will change the
1789 * default partition's partition constraint, so we must lock it.
1791 defaultPartOid
= get_default_partition_oid(parentOid
);
1792 if (OidIsValid(defaultPartOid
) && relid
!= defaultPartOid
)
1793 LockRelationOid(defaultPartOid
, AccessExclusiveLock
);
1796 ReleaseSysCache(tuple
);
1799 * Open and lock the relation.
1801 rel
= relation_open(relid
, AccessExclusiveLock
);
1804 * There can no longer be anyone *else* touching the relation, but we
1805 * might still have open queries or cursors, or pending trigger events, in
1808 CheckTableNotInUse(rel
, "DROP TABLE");
1811 * This effectively deletes all rows in the table, and may be done in a
1812 * serializable transaction. In that case we must record a rw-conflict in
1813 * to this transaction from each transaction holding a predicate lock on
1816 CheckTableForSerializableConflictIn(rel
);
1819 * Delete pg_foreign_table tuple first.
1821 if (rel
->rd_rel
->relkind
== RELKIND_FOREIGN_TABLE
)
1826 ftrel
= table_open(ForeignTableRelationId
, RowExclusiveLock
);
1828 fttuple
= SearchSysCache1(FOREIGNTABLEREL
, ObjectIdGetDatum(relid
));
1829 if (!HeapTupleIsValid(fttuple
))
1830 elog(ERROR
, "cache lookup failed for foreign table %u", relid
);
1832 CatalogTupleDelete(ftrel
, &fttuple
->t_self
);
1834 ReleaseSysCache(fttuple
);
1835 table_close(ftrel
, RowExclusiveLock
);
1839 * If a partitioned table, delete the pg_partitioned_table tuple.
1841 if (rel
->rd_rel
->relkind
== RELKIND_PARTITIONED_TABLE
)
1842 RemovePartitionKeyByRelId(relid
);
1845 * If the relation being dropped is the default partition itself,
1846 * invalidate its entry in pg_partitioned_table.
1848 if (relid
== defaultPartOid
)
1849 update_default_partition_oid(parentOid
, InvalidOid
);
1852 * Schedule unlinking of the relation's physical files at commit.
1854 if (RELKIND_HAS_STORAGE(rel
->rd_rel
->relkind
))
1855 RelationDropStorage(rel
);
1857 /* ensure that stats are dropped if transaction commits */
1858 pgstat_drop_relation(rel
);
1861 * Close relcache entry, but *keep* AccessExclusiveLock on the relation
1862 * until transaction commit. This ensures no one else will try to do
1863 * something with the doomed relation.
1865 relation_close(rel
, NoLock
);
1868 * Remove any associated relation synchronization states.
1870 RemoveSubscriptionRel(InvalidOid
, relid
);
1873 * Forget any ON COMMIT action for the rel
1875 remove_on_commit_action(relid
);
1878 * Flush the relation from the relcache. We want to do this before
1879 * starting to remove catalog entries, just to be certain that no relcache
1880 * entry rebuild will happen partway through. (That should not really
1881 * matter, since we don't do CommandCounterIncrement here, but let's be
1884 RelationForgetRelation(relid
);
1887 * remove inheritance information
1889 RelationRemoveInheritance(relid
);
1894 RemoveStatistics(relid
, 0);
1897 * delete attribute tuples
1899 DeleteAttributeTuples(relid
);
1902 * delete relation tuple
1904 DeleteRelationTuple(relid
);
1906 if (OidIsValid(parentOid
))
1909 * If this is not the default partition, the partition constraint of
1910 * the default partition has changed to include the portion of the key
1911 * space previously covered by the dropped partition.
1913 if (OidIsValid(defaultPartOid
) && relid
!= defaultPartOid
)
1914 CacheInvalidateRelcacheByRelid(defaultPartOid
);
1917 * Invalidate the parent's relcache so that the partition is no longer
1918 * included in its partition descriptor.
1920 CacheInvalidateRelcacheByRelid(parentOid
);
1927 * RelationClearMissing
1929 * Set atthasmissing and attmissingval to false/null for all attributes
1930 * where they are currently set. This can be safely and usefully done if
1931 * the table is rewritten (e.g. by VACUUM FULL or CLUSTER) where we know there
1932 * are no rows left with less than a full complement of attributes.
1934 * The caller must have an AccessExclusive lock on the relation.
1937 RelationClearMissing(Relation rel
)
1940 Oid relid
= RelationGetRelid(rel
);
1941 int natts
= RelationGetNumberOfAttributes(rel
);
1943 Datum repl_val
[Natts_pg_attribute
];
1944 bool repl_null
[Natts_pg_attribute
];
1945 bool repl_repl
[Natts_pg_attribute
];
1946 Form_pg_attribute attrtuple
;
1950 memset(repl_val
, 0, sizeof(repl_val
));
1951 memset(repl_null
, false, sizeof(repl_null
));
1952 memset(repl_repl
, false, sizeof(repl_repl
));
1954 repl_val
[Anum_pg_attribute_atthasmissing
- 1] = BoolGetDatum(false);
1955 repl_null
[Anum_pg_attribute_attmissingval
- 1] = true;
1957 repl_repl
[Anum_pg_attribute_atthasmissing
- 1] = true;
1958 repl_repl
[Anum_pg_attribute_attmissingval
- 1] = true;
1961 /* Get a lock on pg_attribute */
1962 attr_rel
= table_open(AttributeRelationId
, RowExclusiveLock
);
1964 /* process each non-system attribute, including any dropped columns */
1965 for (attnum
= 1; attnum
<= natts
; attnum
++)
1967 tuple
= SearchSysCache2(ATTNUM
,
1968 ObjectIdGetDatum(relid
),
1969 Int16GetDatum(attnum
));
1970 if (!HeapTupleIsValid(tuple
)) /* shouldn't happen */
1971 elog(ERROR
, "cache lookup failed for attribute %d of relation %u",
1974 attrtuple
= (Form_pg_attribute
) GETSTRUCT(tuple
);
1976 /* ignore any where atthasmissing is not true */
1977 if (attrtuple
->atthasmissing
)
1979 newtuple
= heap_modify_tuple(tuple
, RelationGetDescr(attr_rel
),
1980 repl_val
, repl_null
, repl_repl
);
1982 CatalogTupleUpdate(attr_rel
, &newtuple
->t_self
, newtuple
);
1984 heap_freetuple(newtuple
);
1987 ReleaseSysCache(tuple
);
1991 * Our update of the pg_attribute rows will force a relcache rebuild, so
1992 * there's nothing else to do here.
1994 table_close(attr_rel
, RowExclusiveLock
);
2000 * Set the missing value of a single attribute. This should only be used by
2001 * binary upgrade. Takes an AccessExclusive lock on the relation owning the
2005 SetAttrMissing(Oid relid
, char *attname
, char *value
)
2007 Datum valuesAtt
[Natts_pg_attribute
] = {0};
2008 bool nullsAtt
[Natts_pg_attribute
] = {0};
2009 bool replacesAtt
[Natts_pg_attribute
] = {0};
2011 Form_pg_attribute attStruct
;
2017 /* lock the table the attribute belongs to */
2018 tablerel
= table_open(relid
, AccessExclusiveLock
);
2020 /* Don't do anything unless it's a plain table */
2021 if (tablerel
->rd_rel
->relkind
!= RELKIND_RELATION
)
2023 table_close(tablerel
, AccessExclusiveLock
);
2027 /* Lock the attribute row and get the data */
2028 attrrel
= table_open(AttributeRelationId
, RowExclusiveLock
);
2029 atttup
= SearchSysCacheAttName(relid
, attname
);
2030 if (!HeapTupleIsValid(atttup
))
2031 elog(ERROR
, "cache lookup failed for attribute %s of relation %u",
2033 attStruct
= (Form_pg_attribute
) GETSTRUCT(atttup
);
2035 /* get an array value from the value string */
2036 missingval
= OidFunctionCall3(F_ARRAY_IN
,
2037 CStringGetDatum(value
),
2038 ObjectIdGetDatum(attStruct
->atttypid
),
2039 Int32GetDatum(attStruct
->atttypmod
));
2041 /* update the tuple - set atthasmissing and attmissingval */
2042 valuesAtt
[Anum_pg_attribute_atthasmissing
- 1] = BoolGetDatum(true);
2043 replacesAtt
[Anum_pg_attribute_atthasmissing
- 1] = true;
2044 valuesAtt
[Anum_pg_attribute_attmissingval
- 1] = missingval
;
2045 replacesAtt
[Anum_pg_attribute_attmissingval
- 1] = true;
2047 newtup
= heap_modify_tuple(atttup
, RelationGetDescr(attrrel
),
2048 valuesAtt
, nullsAtt
, replacesAtt
);
2049 CatalogTupleUpdate(attrrel
, &newtup
->t_self
, newtup
);
2052 ReleaseSysCache(atttup
);
2053 table_close(attrrel
, RowExclusiveLock
);
2054 table_close(tablerel
, AccessExclusiveLock
);
2058 * Store a check-constraint expression for the given relation.
2060 * Caller is responsible for updating the count of constraints
2061 * in the pg_class entry for the relation.
2063 * The OID of the new constraint is returned.
2066 StoreRelCheck(Relation rel
, const char *ccname
, Node
*expr
,
2067 bool is_validated
, bool is_local
, int inhcount
,
2068 bool is_no_inherit
, bool is_internal
)
2077 * Flatten expression to string form for storage.
2079 ccbin
= nodeToString(expr
);
2082 * Find columns of rel that are used in expr
2084 * NB: pull_var_clause is okay here only because we don't allow subselects
2085 * in check constraints; it would fail to examine the contents of
2088 varList
= pull_var_clause(expr
, 0);
2089 keycount
= list_length(varList
);
2096 attNos
= (int16
*) palloc(keycount
* sizeof(int16
));
2097 foreach(vl
, varList
)
2099 Var
*var
= (Var
*) lfirst(vl
);
2102 for (j
= 0; j
< i
; j
++)
2103 if (attNos
[j
] == var
->varattno
)
2106 attNos
[i
++] = var
->varattno
;
2114 * Partitioned tables do not contain any rows themselves, so a NO INHERIT
2115 * constraint makes no sense.
2117 if (is_no_inherit
&&
2118 rel
->rd_rel
->relkind
== RELKIND_PARTITIONED_TABLE
)
2120 (errcode(ERRCODE_INVALID_TABLE_DEFINITION
),
2121 errmsg("cannot add NO INHERIT constraint to partitioned table \"%s\"",
2122 RelationGetRelationName(rel
))));
2125 * Create the Check Constraint
2128 CreateConstraintEntry(ccname
, /* Constraint Name */
2129 RelationGetNamespace(rel
), /* namespace */
2130 CONSTRAINT_CHECK
, /* Constraint Type */
2131 false, /* Is Deferrable */
2132 false, /* Is Deferred */
2134 InvalidOid
, /* no parent constraint */
2135 RelationGetRelid(rel
), /* relation */
2136 attNos
, /* attrs in the constraint */
2137 keycount
, /* # key attrs in the constraint */
2138 keycount
, /* # total attrs in the constraint */
2139 InvalidOid
, /* not a domain constraint */
2140 InvalidOid
, /* no associated index */
2141 InvalidOid
, /* Foreign key fields */
2152 NULL
, /* not an exclusion constraint */
2153 expr
, /* Tree form of check constraint */
2154 ccbin
, /* Binary form of check constraint */
2155 is_local
, /* conislocal */
2156 inhcount
, /* coninhcount */
2157 is_no_inherit
, /* connoinherit */
2158 false, /* conperiod */
2159 is_internal
); /* internally constructed? */
2167 * Store a not-null constraint for the given relation
2169 * The OID of the new constraint is returned.
2172 StoreRelNotNull(Relation rel
, const char *nnname
, AttrNumber attnum
,
2173 bool is_validated
, bool is_local
, int inhcount
,
2179 CreateConstraintEntry(nnname
,
2180 RelationGetNamespace(rel
),
2186 RelationGetRelid(rel
),
2190 InvalidOid
, /* not a domain constraint */
2191 InvalidOid
, /* no associated index */
2192 InvalidOid
, /* Foreign key fields */
2203 NULL
, /* not an exclusion constraint */
2209 false, /* conperiod */
2215 * Store defaults and constraints (passed as a list of CookedConstraint).
2217 * Each CookedConstraint struct is modified to store the new catalog tuple OID.
2219 * NOTE: only pre-cooked expressions will be passed this way, which is to
2220 * say expressions inherited from an existing relation. Newly parsed
2221 * expressions can be added later, by direct calls to StoreAttrDefault
2222 * and StoreRelCheck (see AddRelationNewConstraints()).
2225 StoreConstraints(Relation rel
, List
*cooked_constraints
, bool is_internal
)
2230 if (cooked_constraints
== NIL
)
2231 return; /* nothing to do */
2234 * Deparsing of constraint expressions will fail unless the just-created
2235 * pg_attribute tuples for this relation are made visible. So, bump the
2236 * command counter. CAUTION: this will cause a relcache entry rebuild.
2238 CommandCounterIncrement();
2240 foreach(lc
, cooked_constraints
)
2242 CookedConstraint
*con
= (CookedConstraint
*) lfirst(lc
);
2244 switch (con
->contype
)
2246 case CONSTR_DEFAULT
:
2247 con
->conoid
= StoreAttrDefault(rel
, con
->attnum
, con
->expr
,
2248 is_internal
, false);
2252 StoreRelCheck(rel
, con
->name
, con
->expr
,
2253 !con
->skip_validation
, con
->is_local
,
2254 con
->inhcount
, con
->is_no_inherit
,
2259 case CONSTR_NOTNULL
:
2261 StoreRelNotNull(rel
, con
->name
, con
->attnum
,
2262 !con
->skip_validation
, con
->is_local
,
2263 con
->inhcount
, con
->is_no_inherit
);
2267 elog(ERROR
, "unrecognized constraint type: %d",
2268 (int) con
->contype
);
2273 SetRelationNumChecks(rel
, numchecks
);
2277 * AddRelationNewConstraints
2279 * Add new column default expressions and/or constraint check expressions
2280 * to an existing relation. This is defined to do both for efficiency in
2281 * DefineRelation, but of course you can do just one or the other by passing
2284 * rel: relation to be modified
2285 * newColDefaults: list of RawColumnDefault structures
2286 * newConstraints: list of Constraint nodes
2287 * allow_merge: true if check constraints may be merged with existing ones
2288 * is_local: true if definition is local, false if it's inherited
2289 * is_internal: true if result of some internal process, not a user request
2290 * queryString: used during expression transformation of default values and
2291 * cooked CHECK constraints
2293 * All entries in newColDefaults will be processed. Entries in newConstraints
2294 * will be processed only if they are CONSTR_CHECK type.
2296 * Returns a list of CookedConstraint nodes that shows the cooked form of
2297 * the default and constraint expressions added to the relation.
2299 * NB: caller should have opened rel with some self-conflicting lock mode,
2300 * and should hold that lock till end of transaction; for normal cases that'll
2301 * be AccessExclusiveLock, but if caller knows that the constraint is already
2302 * enforced by some other means, it can be ShareUpdateExclusiveLock. Also, we
2303 * assume the caller has done a CommandCounterIncrement if necessary to make
2304 * the relation's catalog tuples visible.
2307 AddRelationNewConstraints(Relation rel
,
2308 List
*newColDefaults
,
2309 List
*newConstraints
,
2313 const char *queryString
)
2315 List
*cookedConstraints
= NIL
;
2316 TupleDesc tupleDesc
;
2317 TupleConstr
*oldconstr
;
2320 ParseNamespaceItem
*nsitem
;
2326 CookedConstraint
*cooked
;
2329 * Get info about existing constraints.
2331 tupleDesc
= RelationGetDescr(rel
);
2332 oldconstr
= tupleDesc
->constr
;
2334 numoldchecks
= oldconstr
->num_check
;
2339 * Create a dummy ParseState and insert the target relation as its sole
2340 * rangetable entry. We need a ParseState for transformExpr.
2342 pstate
= make_parsestate(NULL
);
2343 pstate
->p_sourcetext
= queryString
;
2344 nsitem
= addRangeTableEntryForRelation(pstate
,
2350 addNSItemToQuery(pstate
, nsitem
, true, true, true);
2353 * Process column default expressions.
2355 foreach(cell
, newColDefaults
)
2357 RawColumnDefault
*colDef
= (RawColumnDefault
*) lfirst(cell
);
2358 Form_pg_attribute atp
= TupleDescAttr(rel
->rd_att
, colDef
->attnum
- 1);
2361 expr
= cookDefault(pstate
, colDef
->raw_default
,
2362 atp
->atttypid
, atp
->atttypmod
,
2363 NameStr(atp
->attname
),
2367 * If the expression is just a NULL constant, we do not bother to make
2368 * an explicit pg_attrdef entry, since the default behavior is
2369 * equivalent. This applies to column defaults, but not for
2370 * generation expressions.
2372 * Note a nonobvious property of this test: if the column is of a
2373 * domain type, what we'll get is not a bare null Const but a
2374 * CoerceToDomain expr, so we will not discard the default. This is
2375 * critical because the column default needs to be retained to
2376 * override any default that the domain might have.
2379 (!colDef
->generated
&&
2381 castNode(Const
, expr
)->constisnull
))
2384 /* If the DEFAULT is volatile we cannot use a missing value */
2385 if (colDef
->missingMode
&&
2386 contain_volatile_functions_after_planning((Expr
*) expr
))
2387 colDef
->missingMode
= false;
2389 defOid
= StoreAttrDefault(rel
, colDef
->attnum
, expr
, is_internal
,
2390 colDef
->missingMode
);
2392 cooked
= (CookedConstraint
*) palloc(sizeof(CookedConstraint
));
2393 cooked
->contype
= CONSTR_DEFAULT
;
2394 cooked
->conoid
= defOid
;
2395 cooked
->name
= NULL
;
2396 cooked
->attnum
= colDef
->attnum
;
2397 cooked
->expr
= expr
;
2398 cooked
->skip_validation
= false;
2399 cooked
->is_local
= is_local
;
2400 cooked
->inhcount
= is_local
? 0 : 1;
2401 cooked
->is_no_inherit
= false;
2402 cookedConstraints
= lappend(cookedConstraints
, cooked
);
2406 * Process constraint expressions.
2408 numchecks
= numoldchecks
;
2411 foreach(cell
, newConstraints
)
2413 Constraint
*cdef
= (Constraint
*) lfirst(cell
);
2416 if (cdef
->contype
== CONSTR_CHECK
)
2420 if (cdef
->raw_expr
!= NULL
)
2422 Assert(cdef
->cooked_expr
== NULL
);
2425 * Transform raw parsetree to executable expression, and
2426 * verify it's valid as a CHECK constraint.
2428 expr
= cookConstraint(pstate
, cdef
->raw_expr
,
2429 RelationGetRelationName(rel
));
2433 Assert(cdef
->cooked_expr
!= NULL
);
2436 * Here, we assume the parser will only pass us valid CHECK
2437 * expressions, so we do no particular checking.
2439 expr
= stringToNode(cdef
->cooked_expr
);
2443 * Check name uniqueness, or generate a name if none was given.
2445 if (cdef
->conname
!= NULL
)
2449 ccname
= cdef
->conname
;
2450 /* Check against other new constraints */
2451 /* Needed because we don't do CommandCounterIncrement in loop */
2452 foreach(cell2
, checknames
)
2454 if (strcmp((char *) lfirst(cell2
), ccname
) == 0)
2456 (errcode(ERRCODE_DUPLICATE_OBJECT
),
2457 errmsg("check constraint \"%s\" already exists",
2461 /* save name for future checks */
2462 checknames
= lappend(checknames
, ccname
);
2465 * Check against pre-existing constraints. If we are allowed
2466 * to merge with an existing constraint, there's no more to do
2467 * here. (We omit the duplicate constraint from the result,
2468 * which is what ATAddCheckConstraint wants.)
2470 if (MergeWithExistingConstraint(rel
, ccname
, expr
,
2471 allow_merge
, is_local
,
2472 cdef
->initially_valid
,
2473 cdef
->is_no_inherit
))
2479 * When generating a name, we want to create "tab_col_check"
2480 * for a column constraint and "tab_check" for a table
2481 * constraint. We no longer have any info about the syntactic
2482 * positioning of the constraint phrase, so we approximate
2483 * this by seeing whether the expression references more than
2484 * one column. (If the user played by the rules, the result
2487 * Note: pull_var_clause() doesn't descend into sublinks, but
2488 * we eliminated those above; and anyway this only needs to be
2489 * an approximate answer.
2494 vars
= pull_var_clause(expr
, 0);
2496 /* eliminate duplicates */
2497 vars
= list_union(NIL
, vars
);
2499 if (list_length(vars
) == 1)
2500 colname
= get_attname(RelationGetRelid(rel
),
2501 ((Var
*) linitial(vars
))->varattno
,
2506 ccname
= ChooseConstraintName(RelationGetRelationName(rel
),
2509 RelationGetNamespace(rel
),
2512 /* save name for future checks */
2513 checknames
= lappend(checknames
, ccname
);
2520 StoreRelCheck(rel
, ccname
, expr
, cdef
->initially_valid
, is_local
,
2521 is_local
? 0 : 1, cdef
->is_no_inherit
, is_internal
);
2525 cooked
= (CookedConstraint
*) palloc(sizeof(CookedConstraint
));
2526 cooked
->contype
= CONSTR_CHECK
;
2527 cooked
->conoid
= constrOid
;
2528 cooked
->name
= ccname
;
2530 cooked
->expr
= expr
;
2531 cooked
->skip_validation
= cdef
->skip_validation
;
2532 cooked
->is_local
= is_local
;
2533 cooked
->inhcount
= is_local
? 0 : 1;
2534 cooked
->is_no_inherit
= cdef
->is_no_inherit
;
2535 cookedConstraints
= lappend(cookedConstraints
, cooked
);
2537 else if (cdef
->contype
== CONSTR_NOTNULL
)
2539 CookedConstraint
*nncooked
;
2544 /* Determine which column to modify */
2545 colnum
= get_attnum(RelationGetRelid(rel
), strVal(linitial(cdef
->keys
)));
2546 if (colnum
== InvalidAttrNumber
) /* shouldn't happen */
2547 elog(ERROR
, "cache lookup failed for attribute \"%s\" of relation %u",
2548 strVal(linitial(cdef
->keys
)), RelationGetRelid(rel
));
2551 * If the column already has an inheritable not-null constraint,
2552 * we need only adjust its inheritance status and we're done. If
2553 * the constraint is there but marked NO INHERIT, then it is
2554 * updated in the same way, but we also recurse to the children
2555 * (if any) to add the constraint there as well.
2557 existing
= AdjustNotNullInheritance1(RelationGetRelid(rel
), colnum
,
2558 cdef
->inhcount
, cdef
->is_no_inherit
);
2560 continue; /* all done! */
2561 else if (existing
== -1)
2565 children
= find_inheritance_children(RelationGetRelid(rel
), NoLock
);
2566 foreach_oid(childoid
, children
)
2568 Relation childrel
= table_open(childoid
, NoLock
);
2570 AddRelationNewConstraints(childrel
,
2572 list_make1(copyObject(cdef
)),
2577 /* these constraints are not in the return list -- good? */
2579 table_close(childrel
, NoLock
);
2586 * If a constraint name is specified, check that it isn't already
2587 * used. Otherwise, choose a non-conflicting one ourselves.
2591 if (ConstraintNameIsUsed(CONSTRAINT_RELATION
,
2592 RelationGetRelid(rel
),
2595 errcode(ERRCODE_DUPLICATE_OBJECT
),
2596 errmsg("constraint \"%s\" for relation \"%s\" already exists",
2597 cdef
->conname
, RelationGetRelationName(rel
)));
2598 nnname
= cdef
->conname
;
2601 nnname
= ChooseConstraintName(RelationGetRelationName(rel
),
2602 strVal(linitial(cdef
->keys
)),
2604 RelationGetNamespace(rel
),
2606 nnnames
= lappend(nnnames
, nnname
);
2609 StoreRelNotNull(rel
, nnname
, colnum
,
2610 cdef
->initially_valid
,
2611 cdef
->inhcount
== 0,
2613 cdef
->is_no_inherit
);
2615 nncooked
= (CookedConstraint
*) palloc(sizeof(CookedConstraint
));
2616 nncooked
->contype
= CONSTR_NOTNULL
;
2617 nncooked
->conoid
= constrOid
;
2618 nncooked
->name
= nnname
;
2619 nncooked
->attnum
= colnum
;
2620 nncooked
->expr
= NULL
;
2621 nncooked
->skip_validation
= cdef
->skip_validation
;
2622 nncooked
->is_local
= is_local
;
2623 nncooked
->inhcount
= cdef
->inhcount
;
2624 nncooked
->is_no_inherit
= cdef
->is_no_inherit
;
2626 cookedConstraints
= lappend(cookedConstraints
, nncooked
);
2631 * Update the count of constraints in the relation's pg_class tuple. We do
2632 * this even if there was no change, in order to ensure that an SI update
2633 * message is sent out for the pg_class tuple, which will force other
2634 * backends to rebuild their relcache entries for the rel. (This is
2635 * critical if we added defaults but not constraints.)
2637 SetRelationNumChecks(rel
, numchecks
);
2639 return cookedConstraints
;
2643 * Check for a pre-existing check constraint that conflicts with a proposed
2644 * new one, and either adjust its conislocal/coninhcount settings or throw
2647 * Returns true if merged (constraint is a duplicate), or false if it's
2648 * got a so-far-unique name, or throws error if conflict.
2650 * XXX See MergeConstraintsIntoExisting too if you change this code.
2653 MergeWithExistingConstraint(Relation rel
, const char *ccname
, Node
*expr
,
2654 bool allow_merge
, bool is_local
,
2655 bool is_initially_valid
,
2660 SysScanDesc conscan
;
2661 ScanKeyData skey
[3];
2664 /* Search for a pg_constraint entry with same name and relation */
2665 conDesc
= table_open(ConstraintRelationId
, RowExclusiveLock
);
2669 ScanKeyInit(&skey
[0],
2670 Anum_pg_constraint_conrelid
,
2671 BTEqualStrategyNumber
, F_OIDEQ
,
2672 ObjectIdGetDatum(RelationGetRelid(rel
)));
2673 ScanKeyInit(&skey
[1],
2674 Anum_pg_constraint_contypid
,
2675 BTEqualStrategyNumber
, F_OIDEQ
,
2676 ObjectIdGetDatum(InvalidOid
));
2677 ScanKeyInit(&skey
[2],
2678 Anum_pg_constraint_conname
,
2679 BTEqualStrategyNumber
, F_NAMEEQ
,
2680 CStringGetDatum(ccname
));
2682 conscan
= systable_beginscan(conDesc
, ConstraintRelidTypidNameIndexId
, true,
2685 /* There can be at most one matching row */
2686 if (HeapTupleIsValid(tup
= systable_getnext(conscan
)))
2688 Form_pg_constraint con
= (Form_pg_constraint
) GETSTRUCT(tup
);
2690 /* Found it. Conflicts if not identical check constraint */
2691 if (con
->contype
== CONSTRAINT_CHECK
)
2696 val
= fastgetattr(tup
,
2697 Anum_pg_constraint_conbin
,
2698 conDesc
->rd_att
, &isnull
);
2700 elog(ERROR
, "null conbin for rel %s",
2701 RelationGetRelationName(rel
));
2702 if (equal(expr
, stringToNode(TextDatumGetCString(val
))))
2707 * If the existing constraint is purely inherited (no local
2708 * definition) then interpret addition of a local constraint as a
2709 * legal merge. This allows ALTER ADD CONSTRAINT on parent and child
2710 * tables to be given in either order with same end state. However if
2711 * the relation is a partition, all inherited constraints are always
2712 * non-local, including those that were merged.
2714 if (is_local
&& !con
->conislocal
&& !rel
->rd_rel
->relispartition
)
2717 if (!found
|| !allow_merge
)
2719 (errcode(ERRCODE_DUPLICATE_OBJECT
),
2720 errmsg("constraint \"%s\" for relation \"%s\" already exists",
2721 ccname
, RelationGetRelationName(rel
))));
2723 /* If the child constraint is "no inherit" then cannot merge */
2724 if (con
->connoinherit
)
2726 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION
),
2727 errmsg("constraint \"%s\" conflicts with non-inherited constraint on relation \"%s\"",
2728 ccname
, RelationGetRelationName(rel
))));
2731 * Must not change an existing inherited constraint to "no inherit"
2732 * status. That's because inherited constraints should be able to
2733 * propagate to lower-level children.
2735 if (con
->coninhcount
> 0 && is_no_inherit
)
2737 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION
),
2738 errmsg("constraint \"%s\" conflicts with inherited constraint on relation \"%s\"",
2739 ccname
, RelationGetRelationName(rel
))));
2742 * If the child constraint is "not valid" then cannot merge with a
2743 * valid parent constraint.
2745 if (is_initially_valid
&& !con
->convalidated
)
2747 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION
),
2748 errmsg("constraint \"%s\" conflicts with NOT VALID constraint on relation \"%s\"",
2749 ccname
, RelationGetRelationName(rel
))));
2751 /* OK to update the tuple */
2753 (errmsg("merging constraint \"%s\" with inherited definition",
2756 tup
= heap_copytuple(tup
);
2757 con
= (Form_pg_constraint
) GETSTRUCT(tup
);
2760 * In case of partitions, an inherited constraint must be inherited
2761 * only once since it cannot have multiple parents and it is never
2764 if (rel
->rd_rel
->relispartition
)
2766 con
->coninhcount
= 1;
2767 con
->conislocal
= false;
2772 con
->conislocal
= true;
2776 if (con
->coninhcount
< 0)
2778 errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED
),
2779 errmsg("too many inheritance parents"));
2785 con
->connoinherit
= true;
2788 CatalogTupleUpdate(conDesc
, &tup
->t_self
, tup
);
2791 systable_endscan(conscan
);
2792 table_close(conDesc
, RowExclusiveLock
);
2797 /* list_sort comparator to sort CookedConstraint by attnum */
2799 list_cookedconstr_attnum_cmp(const ListCell
*p1
, const ListCell
*p2
)
2801 AttrNumber v1
= ((CookedConstraint
*) lfirst(p1
))->attnum
;
2802 AttrNumber v2
= ((CookedConstraint
*) lfirst(p2
))->attnum
;
2804 return pg_cmp_s16(v1
, v2
);
2808 * Create the not-null constraints when creating a new relation
2810 * These come from two sources: the 'constraints' list (of Constraint) is
2811 * specified directly by the user; the 'old_notnulls' list (of
2812 * CookedConstraint) comes from inheritance. We create one constraint
2813 * for each column, giving priority to user-specified ones, and setting
2814 * inhcount according to how many parents cause each column to get a
2815 * not-null constraint. If a user-specified name clashes with another
2816 * user-specified name, an error is raised.
2818 * Note that inherited constraints have two shapes: those coming from another
2819 * not-null constraint in the parent, which have a name already, and those
2820 * coming from a primary key in the parent, which don't. Any name specified
2821 * in a parent is disregarded in case of a conflict.
2823 * Returns a list of AttrNumber for columns that need to have the attnotnull
2827 AddRelationNotNullConstraints(Relation rel
, List
*constraints
,
2836 * We track two lists of names: nnnames keeps all the constraint names,
2837 * givennames tracks user-generated names. The distinction is important,
2838 * because we must raise error for user-generated name conflicts, but for
2839 * system-generated name conflicts we just generate another.
2845 * First, create all not-null constraints that are directly specified by
2846 * the user. Note that inheritance might have given us another source for
2847 * each, so we must scan the old_notnulls list and increment inhcount for
2848 * each element with identical attnum. We delete from there any element
2851 foreach(lc
, constraints
)
2853 Constraint
*constr
= lfirst_node(Constraint
, lc
);
2856 bool is_local
= true;
2860 Assert(constr
->contype
== CONSTR_NOTNULL
);
2862 attnum
= get_attnum(RelationGetRelid(rel
),
2863 strVal(linitial(constr
->keys
)));
2866 * Search in the list of inherited constraints for any entries on the
2869 foreach(lc2
, old_notnulls
)
2871 CookedConstraint
*old
= (CookedConstraint
*) lfirst(lc2
);
2873 if (old
->attnum
== attnum
)
2876 * If we get a constraint from the parent, having a local NO
2877 * INHERIT one doesn't work.
2879 if (constr
->is_no_inherit
)
2881 (errcode(ERRCODE_DATATYPE_MISMATCH
),
2882 errmsg("cannot define not-null constraint on column \"%s\" with NO INHERIT",
2883 strVal(linitial(constr
->keys
))),
2884 errdetail("The column has an inherited not-null constraint.")));
2887 old_notnulls
= foreach_delete_current(old_notnulls
, lc2
);
2892 * Determine a constraint name, which may have been specified by the
2893 * user, or raise an error if a conflict exists with another
2894 * user-specified name.
2896 if (constr
->conname
)
2898 foreach(lc2
, givennames
)
2900 if (strcmp(lfirst(lc2
), constr
->conname
) == 0)
2902 errcode(ERRCODE_DUPLICATE_OBJECT
),
2903 errmsg("constraint \"%s\" for relation \"%s\" already exists",
2905 RelationGetRelationName(rel
)));
2908 conname
= constr
->conname
;
2909 givennames
= lappend(givennames
, conname
);
2912 conname
= ChooseConstraintName(RelationGetRelationName(rel
),
2913 get_attname(RelationGetRelid(rel
),
2916 RelationGetNamespace(rel
),
2918 nnnames
= lappend(nnnames
, conname
);
2920 StoreRelNotNull(rel
, conname
,
2921 attnum
, true, is_local
,
2922 inhcount
, constr
->is_no_inherit
);
2924 nncols
= lappend_int(nncols
, attnum
);
2928 * If any column remains in the old_notnulls list, we must create a not-
2929 * null constraint marked not-local. Because multiple parents could
2930 * specify a not-null constraint for the same column, we must count how
2931 * many there are and add to the original inhcount accordingly, deleting
2932 * elements we've already processed. We sort the list to make it easy.
2934 * We don't use foreach() here because we have two nested loops over the
2935 * constraint list, with possible element deletions in the inner one. If
2936 * we used foreach_delete_current() it could only fix up the state of one
2937 * of the loops, so it seems cleaner to use looping over list indexes for
2938 * both loops. Note that any deletion will happen beyond where the outer
2939 * loop is, so its index never needs adjustment.
2941 list_sort(old_notnulls
, list_cookedconstr_attnum_cmp
);
2942 for (int outerpos
= 0; outerpos
< list_length(old_notnulls
); outerpos
++)
2944 CookedConstraint
*cooked
;
2945 char *conname
= NULL
;
2946 int add_inhcount
= 0;
2949 cooked
= (CookedConstraint
*) list_nth(old_notnulls
, outerpos
);
2950 Assert(cooked
->contype
== CONSTR_NOTNULL
);
2953 * Preserve the first non-conflicting constraint name we come across,
2956 if (conname
== NULL
&& cooked
->name
)
2957 conname
= cooked
->name
;
2959 for (int restpos
= outerpos
+ 1; restpos
< list_length(old_notnulls
);)
2961 CookedConstraint
*other
;
2963 other
= (CookedConstraint
*) list_nth(old_notnulls
, restpos
);
2964 if (other
->attnum
== cooked
->attnum
)
2966 if (conname
== NULL
&& other
->name
)
2967 conname
= other
->name
;
2970 old_notnulls
= list_delete_nth_cell(old_notnulls
, restpos
);
2976 /* If we got a name, make sure it isn't one we've already used */
2977 if (conname
!= NULL
)
2979 foreach(lc2
, nnnames
)
2981 if (strcmp(lfirst(lc2
), conname
) == 0)
2989 /* and choose a name, if needed */
2990 if (conname
== NULL
)
2991 conname
= ChooseConstraintName(RelationGetRelationName(rel
),
2992 get_attname(RelationGetRelid(rel
),
2993 cooked
->attnum
, false),
2995 RelationGetNamespace(rel
),
2997 nnnames
= lappend(nnnames
, conname
);
2999 StoreRelNotNull(rel
, conname
, cooked
->attnum
, true,
3000 cooked
->is_local
, cooked
->inhcount
+ add_inhcount
,
3001 cooked
->is_no_inherit
);
3003 nncols
= lappend_int(nncols
, cooked
->attnum
);
3010 * Update the count of constraints in the relation's pg_class tuple.
3012 * Caller had better hold exclusive lock on the relation.
3014 * An important side effect is that a SI update message will be sent out for
3015 * the pg_class tuple, which will force other backends to rebuild their
3016 * relcache entries for the rel. Also, this backend will rebuild its
3017 * own relcache entry at the next CommandCounterIncrement.
3020 SetRelationNumChecks(Relation rel
, int numchecks
)
3024 Form_pg_class relStruct
;
3026 relrel
= table_open(RelationRelationId
, RowExclusiveLock
);
3027 reltup
= SearchSysCacheCopy1(RELOID
,
3028 ObjectIdGetDatum(RelationGetRelid(rel
)));
3029 if (!HeapTupleIsValid(reltup
))
3030 elog(ERROR
, "cache lookup failed for relation %u",
3031 RelationGetRelid(rel
));
3032 relStruct
= (Form_pg_class
) GETSTRUCT(reltup
);
3034 if (relStruct
->relchecks
!= numchecks
)
3036 relStruct
->relchecks
= numchecks
;
3038 CatalogTupleUpdate(relrel
, &reltup
->t_self
, reltup
);
3042 /* Skip the disk update, but force relcache inval anyway */
3043 CacheInvalidateRelcache(rel
);
3046 heap_freetuple(reltup
);
3047 table_close(relrel
, RowExclusiveLock
);
3051 * Check for references to generated columns
3054 check_nested_generated_walker(Node
*node
, void *context
)
3056 ParseState
*pstate
= context
;
3060 else if (IsA(node
, Var
))
3062 Var
*var
= (Var
*) node
;
3066 relid
= rt_fetch(var
->varno
, pstate
->p_rtable
)->relid
;
3067 if (!OidIsValid(relid
))
3068 return false; /* XXX shouldn't we raise an error? */
3070 attnum
= var
->varattno
;
3072 if (attnum
> 0 && get_attgenerated(relid
, attnum
))
3074 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION
),
3075 errmsg("cannot use generated column \"%s\" in column generation expression",
3076 get_attname(relid
, attnum
, false)),
3077 errdetail("A generated column cannot reference another generated column."),
3078 parser_errposition(pstate
, var
->location
)));
3079 /* A whole-row Var is necessarily self-referential, so forbid it */
3082 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION
),
3083 errmsg("cannot use whole-row variable in column generation expression"),
3084 errdetail("This would cause the generated column to depend on its own value."),
3085 parser_errposition(pstate
, var
->location
)));
3086 /* System columns were already checked in the parser */
3091 return expression_tree_walker(node
, check_nested_generated_walker
,
3096 check_nested_generated(ParseState
*pstate
, Node
*node
)
3098 check_nested_generated_walker(node
, pstate
);
3102 * Take a raw default and convert it to a cooked format ready for
3105 * Parse state should be set up to recognize any vars that might appear
3106 * in the expression. (Even though we plan to reject vars, it's more
3107 * user-friendly to give the correct error message than "unknown var".)
3109 * If atttypid is not InvalidOid, coerce the expression to the specified
3110 * type (and typmod atttypmod). attname is only needed in this case:
3111 * it is used in the error message, if any.
3114 cookDefault(ParseState
*pstate
,
3118 const char *attname
,
3123 Assert(raw_default
!= NULL
);
3126 * Transform raw parsetree to executable expression.
3128 expr
= transformExpr(pstate
, raw_default
, attgenerated
? EXPR_KIND_GENERATED_COLUMN
: EXPR_KIND_COLUMN_DEFAULT
);
3132 /* Disallow refs to other generated columns */
3133 check_nested_generated(pstate
, expr
);
3135 /* Disallow mutable functions */
3136 if (contain_mutable_functions_after_planning((Expr
*) expr
))
3138 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION
),
3139 errmsg("generation expression is not immutable")));
3144 * For a default expression, transformExpr() should have rejected
3145 * column references.
3147 Assert(!contain_var_clause(expr
));
3151 * Coerce the expression to the correct type and typmod, if given. This
3152 * should match the parser's processing of non-defaulted expressions ---
3153 * see transformAssignedExpr().
3155 if (OidIsValid(atttypid
))
3157 Oid type_id
= exprType(expr
);
3159 expr
= coerce_to_target_type(pstate
, expr
, type_id
,
3160 atttypid
, atttypmod
,
3161 COERCION_ASSIGNMENT
,
3162 COERCE_IMPLICIT_CAST
,
3166 (errcode(ERRCODE_DATATYPE_MISMATCH
),
3167 errmsg("column \"%s\" is of type %s"
3168 " but default expression is of type %s",
3170 format_type_be(atttypid
),
3171 format_type_be(type_id
)),
3172 errhint("You will need to rewrite or cast the expression.")));
3176 * Finally, take care of collations in the finished expression.
3178 assign_expr_collations(pstate
, expr
);
3184 * Take a raw CHECK constraint expression and convert it to a cooked format
3185 * ready for storage.
3187 * Parse state must be set up to recognize any vars that might appear
3188 * in the expression.
3191 cookConstraint(ParseState
*pstate
,
3192 Node
*raw_constraint
,
3198 * Transform raw parsetree to executable expression.
3200 expr
= transformExpr(pstate
, raw_constraint
, EXPR_KIND_CHECK_CONSTRAINT
);
3203 * Make sure it yields a boolean result.
3205 expr
= coerce_to_boolean(pstate
, expr
, "CHECK");
3208 * Take care of collations.
3210 assign_expr_collations(pstate
, expr
);
3213 * Make sure no outside relations are referred to (this is probably dead
3214 * code now that add_missing_from is history).
3216 if (list_length(pstate
->p_rtable
) != 1)
3218 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE
),
3219 errmsg("only table \"%s\" can be referenced in check constraint",
3226 * CopyStatistics --- copy entries in pg_statistic from one rel to another
3229 CopyStatistics(Oid fromrelid
, Oid torelid
)
3235 CatalogIndexState indstate
= NULL
;
3237 statrel
= table_open(StatisticRelationId
, RowExclusiveLock
);
3239 /* Now search for stat records */
3240 ScanKeyInit(&key
[0],
3241 Anum_pg_statistic_starelid
,
3242 BTEqualStrategyNumber
, F_OIDEQ
,
3243 ObjectIdGetDatum(fromrelid
));
3245 scan
= systable_beginscan(statrel
, StatisticRelidAttnumInhIndexId
,
3246 true, NULL
, 1, key
);
3248 while (HeapTupleIsValid((tup
= systable_getnext(scan
))))
3250 Form_pg_statistic statform
;
3252 /* make a modifiable copy */
3253 tup
= heap_copytuple(tup
);
3254 statform
= (Form_pg_statistic
) GETSTRUCT(tup
);
3256 /* update the copy of the tuple and insert it */
3257 statform
->starelid
= torelid
;
3259 /* fetch index information when we know we need it */
3260 if (indstate
== NULL
)
3261 indstate
= CatalogOpenIndexes(statrel
);
3263 CatalogTupleInsertWithInfo(statrel
, tup
, indstate
);
3265 heap_freetuple(tup
);
3268 systable_endscan(scan
);
3270 if (indstate
!= NULL
)
3271 CatalogCloseIndexes(indstate
);
3272 table_close(statrel
, RowExclusiveLock
);
3276 * RemoveStatistics --- remove entries in pg_statistic for a rel or column
3278 * If attnum is zero, remove all entries for rel; else remove only the one(s)
3282 RemoveStatistics(Oid relid
, AttrNumber attnum
)
3284 Relation pgstatistic
;
3290 pgstatistic
= table_open(StatisticRelationId
, RowExclusiveLock
);
3292 ScanKeyInit(&key
[0],
3293 Anum_pg_statistic_starelid
,
3294 BTEqualStrategyNumber
, F_OIDEQ
,
3295 ObjectIdGetDatum(relid
));
3301 ScanKeyInit(&key
[1],
3302 Anum_pg_statistic_staattnum
,
3303 BTEqualStrategyNumber
, F_INT2EQ
,
3304 Int16GetDatum(attnum
));
3308 scan
= systable_beginscan(pgstatistic
, StatisticRelidAttnumInhIndexId
, true,
3311 /* we must loop even when attnum != 0, in case of inherited stats */
3312 while (HeapTupleIsValid(tuple
= systable_getnext(scan
)))
3313 CatalogTupleDelete(pgstatistic
, &tuple
->t_self
);
3315 systable_endscan(scan
);
3317 table_close(pgstatistic
, RowExclusiveLock
);
3322 * RelationTruncateIndexes - truncate all indexes associated
3323 * with the heap relation to zero tuples.
3325 * The routine will truncate and then reconstruct the indexes on
3326 * the specified relation. Caller must hold exclusive lock on rel.
3329 RelationTruncateIndexes(Relation heapRelation
)
3333 /* Ask the relcache to produce a list of the indexes of the rel */
3334 foreach(indlist
, RelationGetIndexList(heapRelation
))
3336 Oid indexId
= lfirst_oid(indlist
);
3337 Relation currentIndex
;
3338 IndexInfo
*indexInfo
;
3340 /* Open the index relation; use exclusive lock, just to be sure */
3341 currentIndex
= index_open(indexId
, AccessExclusiveLock
);
3344 * Fetch info needed for index_build. Since we know there are no
3345 * tuples that actually need indexing, we can use a dummy IndexInfo.
3346 * This is slightly cheaper to build, but the real point is to avoid
3347 * possibly running user-defined code in index expressions or
3348 * predicates. We might be getting invoked during ON COMMIT
3349 * processing, and we don't want to run any such code then.
3351 indexInfo
= BuildDummyIndexInfo(currentIndex
);
3354 * Now truncate the actual file (and discard buffers).
3356 RelationTruncate(currentIndex
, 0);
3358 /* Initialize the index and rebuild */
3359 /* Note: we do not need to re-establish pkey setting */
3360 index_build(heapRelation
, currentIndex
, indexInfo
, true, false);
3362 /* We're done with this index */
3363 index_close(currentIndex
, NoLock
);
3370 * This routine deletes all data within all the specified relations.
3372 * This is not transaction-safe! There is another, transaction-safe
3373 * implementation in commands/tablecmds.c. We now use this only for
3374 * ON COMMIT truncation of temporary tables, where it doesn't matter.
3377 heap_truncate(List
*relids
)
3379 List
*relations
= NIL
;
3382 /* Open relations for processing, and grab exclusive access on each */
3383 foreach(cell
, relids
)
3385 Oid rid
= lfirst_oid(cell
);
3388 rel
= table_open(rid
, AccessExclusiveLock
);
3389 relations
= lappend(relations
, rel
);
3392 /* Don't allow truncate on tables that are referenced by foreign keys */
3393 heap_truncate_check_FKs(relations
, true);
3396 foreach(cell
, relations
)
3398 Relation rel
= lfirst(cell
);
3400 /* Truncate the relation */
3401 heap_truncate_one_rel(rel
);
3403 /* Close the relation, but keep exclusive lock on it until commit */
3404 table_close(rel
, NoLock
);
3409 * heap_truncate_one_rel
3411 * This routine deletes all data within the specified relation.
3413 * This is not transaction-safe, because the truncation is done immediately
3414 * and cannot be rolled back later. Caller is responsible for having
3415 * checked permissions etc, and must have obtained AccessExclusiveLock.
3418 heap_truncate_one_rel(Relation rel
)
3423 * Truncate the relation. Partitioned tables have no storage, so there is
3424 * nothing to do for them here.
3426 if (rel
->rd_rel
->relkind
== RELKIND_PARTITIONED_TABLE
)
3429 /* Truncate the underlying relation */
3430 table_relation_nontransactional_truncate(rel
);
3432 /* If the relation has indexes, truncate the indexes too */
3433 RelationTruncateIndexes(rel
);
3435 /* If there is a toast table, truncate that too */
3436 toastrelid
= rel
->rd_rel
->reltoastrelid
;
3437 if (OidIsValid(toastrelid
))
3439 Relation toastrel
= table_open(toastrelid
, AccessExclusiveLock
);
3441 table_relation_nontransactional_truncate(toastrel
);
3442 RelationTruncateIndexes(toastrel
);
3443 /* keep the lock... */
3444 table_close(toastrel
, NoLock
);
3449 * heap_truncate_check_FKs
3450 * Check for foreign keys referencing a list of relations that
3451 * are to be truncated, and raise error if there are any
3453 * We disallow such FKs (except self-referential ones) since the whole point
3454 * of TRUNCATE is to not scan the individual rows to be thrown away.
3456 * This is split out so it can be shared by both implementations of truncate.
3457 * Caller should already hold a suitable lock on the relations.
3459 * tempTables is only used to select an appropriate error message.
3462 heap_truncate_check_FKs(List
*relations
, bool tempTables
)
3469 * Build a list of OIDs of the interesting relations.
3471 * If a relation has no triggers, then it can neither have FKs nor be
3472 * referenced by a FK from another table, so we can ignore it. For
3473 * partitioned tables, FKs have no triggers, so we must include them
3476 foreach(cell
, relations
)
3478 Relation rel
= lfirst(cell
);
3480 if (rel
->rd_rel
->relhastriggers
||
3481 rel
->rd_rel
->relkind
== RELKIND_PARTITIONED_TABLE
)
3482 oids
= lappend_oid(oids
, RelationGetRelid(rel
));
3486 * Fast path: if no relation has triggers, none has FKs either.
3492 * Otherwise, must scan pg_constraint. We make one pass with all the
3493 * relations considered; if this finds nothing, then all is well.
3495 dependents
= heap_truncate_find_FKs(oids
);
3496 if (dependents
== NIL
)
3500 * Otherwise we repeat the scan once per relation to identify a particular
3501 * pair of relations to complain about. This is pretty slow, but
3502 * performance shouldn't matter much in a failure path. The reason for
3503 * doing things this way is to ensure that the message produced is not
3504 * dependent on chance row locations within pg_constraint.
3508 Oid relid
= lfirst_oid(cell
);
3511 dependents
= heap_truncate_find_FKs(list_make1_oid(relid
));
3513 foreach(cell2
, dependents
)
3515 Oid relid2
= lfirst_oid(cell2
);
3517 if (!list_member_oid(oids
, relid2
))
3519 char *relname
= get_rel_name(relid
);
3520 char *relname2
= get_rel_name(relid2
);
3524 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
3525 errmsg("unsupported ON COMMIT and foreign key combination"),
3526 errdetail("Table \"%s\" references \"%s\", but they do not have the same ON COMMIT setting.",
3527 relname2
, relname
)));
3530 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
3531 errmsg("cannot truncate a table referenced in a foreign key constraint"),
3532 errdetail("Table \"%s\" references \"%s\".",
3534 errhint("Truncate table \"%s\" at the same time, "
3535 "or use TRUNCATE ... CASCADE.",
3543 * heap_truncate_find_FKs
3544 * Find relations having foreign keys referencing any of the given rels
3546 * Input and result are both lists of relation OIDs. The result contains
3547 * no duplicates, does *not* include any rels that were already in the input
3548 * list, and is sorted in OID order. (The last property is enforced mainly
3549 * to guarantee consistent behavior in the regression tests; we don't want
3550 * behavior to change depending on chance locations of rows in pg_constraint.)
3552 * Note: caller should already have appropriate lock on all rels mentioned
3553 * in relationIds. Since adding or dropping an FK requires exclusive lock
3554 * on both rels, this ensures that the answer will be stable.
3557 heap_truncate_find_FKs(List
*relationIds
)
3565 SysScanDesc fkeyScan
;
3569 oids
= list_copy(relationIds
);
3572 * Must scan pg_constraint. Right now, it is a seqscan because there is
3573 * no available index on confrelid.
3575 fkeyRel
= table_open(ConstraintRelationId
, AccessShareLock
);
3581 fkeyScan
= systable_beginscan(fkeyRel
, InvalidOid
, false,
3584 while (HeapTupleIsValid(tuple
= systable_getnext(fkeyScan
)))
3586 Form_pg_constraint con
= (Form_pg_constraint
) GETSTRUCT(tuple
);
3588 /* Not a foreign key */
3589 if (con
->contype
!= CONSTRAINT_FOREIGN
)
3592 /* Not referencing one of our list of tables */
3593 if (!list_member_oid(oids
, con
->confrelid
))
3597 * If this constraint has a parent constraint which we have not seen
3598 * yet, keep track of it for the second loop, below. Tracking parent
3599 * constraints allows us to climb up to the top-level constraint and
3600 * look for all possible relations referencing the partitioned table.
3602 if (OidIsValid(con
->conparentid
) &&
3603 !list_member_oid(parent_cons
, con
->conparentid
))
3604 parent_cons
= lappend_oid(parent_cons
, con
->conparentid
);
3607 * Add referencer to result, unless present in input list. (Don't
3608 * worry about dupes: we'll fix that below).
3610 if (!list_member_oid(relationIds
, con
->conrelid
))
3611 result
= lappend_oid(result
, con
->conrelid
);
3614 systable_endscan(fkeyScan
);
3617 * Process each parent constraint we found to add the list of referenced
3618 * relations by them to the oids list. If we do add any new such
3619 * relations, redo the first loop above. Also, if we see that the parent
3620 * constraint in turn has a parent, add that so that we process all
3621 * relations in a single additional pass.
3623 foreach(cell
, parent_cons
)
3625 Oid parent
= lfirst_oid(cell
);
3628 Anum_pg_constraint_oid
,
3629 BTEqualStrategyNumber
, F_OIDEQ
,
3630 ObjectIdGetDatum(parent
));
3632 fkeyScan
= systable_beginscan(fkeyRel
, ConstraintOidIndexId
,
3633 true, NULL
, 1, &key
);
3635 tuple
= systable_getnext(fkeyScan
);
3636 if (HeapTupleIsValid(tuple
))
3638 Form_pg_constraint con
= (Form_pg_constraint
) GETSTRUCT(tuple
);
3641 * pg_constraint rows always appear for partitioned hierarchies
3642 * this way: on the each side of the constraint, one row appears
3643 * for each partition that points to the top-most table on the
3646 * Because of this arrangement, we can correctly catch all
3647 * relevant relations by adding to 'parent_cons' all rows with
3648 * valid conparentid, and to the 'oids' list all rows with a zero
3649 * conparentid. If any oids are added to 'oids', redo the first
3650 * loop above by setting 'restart'.
3652 if (OidIsValid(con
->conparentid
))
3653 parent_cons
= list_append_unique_oid(parent_cons
,
3655 else if (!list_member_oid(oids
, con
->confrelid
))
3657 oids
= lappend_oid(oids
, con
->confrelid
);
3662 systable_endscan(fkeyScan
);
3665 list_free(parent_cons
);
3669 table_close(fkeyRel
, AccessShareLock
);
3672 /* Now sort and de-duplicate the result list */
3673 list_sort(result
, list_oid_cmp
);
3674 list_deduplicate_oid(result
);
3681 * Store information about the partition key rel into the catalog
3684 StorePartitionKey(Relation rel
,
3687 AttrNumber
*partattrs
,
3693 int2vector
*partattrs_vec
;
3694 oidvector
*partopclass_vec
;
3695 oidvector
*partcollation_vec
;
3696 Datum partexprDatum
;
3697 Relation pg_partitioned_table
;
3699 Datum values
[Natts_pg_partitioned_table
];
3700 bool nulls
[Natts_pg_partitioned_table
] = {0};
3701 ObjectAddress myself
;
3702 ObjectAddress referenced
;
3703 ObjectAddresses
*addrs
;
3705 Assert(rel
->rd_rel
->relkind
== RELKIND_PARTITIONED_TABLE
);
3707 /* Copy the partition attribute numbers, opclass OIDs into arrays */
3708 partattrs_vec
= buildint2vector(partattrs
, partnatts
);
3709 partopclass_vec
= buildoidvector(partopclass
, partnatts
);
3710 partcollation_vec
= buildoidvector(partcollation
, partnatts
);
3712 /* Convert the expressions (if any) to a text datum */
3717 exprString
= nodeToString(partexprs
);
3718 partexprDatum
= CStringGetTextDatum(exprString
);
3722 partexprDatum
= (Datum
) 0;
3724 pg_partitioned_table
= table_open(PartitionedRelationId
, RowExclusiveLock
);
3726 /* Only this can ever be NULL */
3728 nulls
[Anum_pg_partitioned_table_partexprs
- 1] = true;
3730 values
[Anum_pg_partitioned_table_partrelid
- 1] = ObjectIdGetDatum(RelationGetRelid(rel
));
3731 values
[Anum_pg_partitioned_table_partstrat
- 1] = CharGetDatum(strategy
);
3732 values
[Anum_pg_partitioned_table_partnatts
- 1] = Int16GetDatum(partnatts
);
3733 values
[Anum_pg_partitioned_table_partdefid
- 1] = ObjectIdGetDatum(InvalidOid
);
3734 values
[Anum_pg_partitioned_table_partattrs
- 1] = PointerGetDatum(partattrs_vec
);
3735 values
[Anum_pg_partitioned_table_partclass
- 1] = PointerGetDatum(partopclass_vec
);
3736 values
[Anum_pg_partitioned_table_partcollation
- 1] = PointerGetDatum(partcollation_vec
);
3737 values
[Anum_pg_partitioned_table_partexprs
- 1] = partexprDatum
;
3739 tuple
= heap_form_tuple(RelationGetDescr(pg_partitioned_table
), values
, nulls
);
3741 CatalogTupleInsert(pg_partitioned_table
, tuple
);
3742 table_close(pg_partitioned_table
, RowExclusiveLock
);
3744 /* Mark this relation as dependent on a few things as follows */
3745 addrs
= new_object_addresses();
3746 ObjectAddressSet(myself
, RelationRelationId
, RelationGetRelid(rel
));
3748 /* Operator class and collation per key column */
3749 for (i
= 0; i
< partnatts
; i
++)
3751 ObjectAddressSet(referenced
, OperatorClassRelationId
, partopclass
[i
]);
3752 add_exact_object_address(&referenced
, addrs
);
3754 /* The default collation is pinned, so don't bother recording it */
3755 if (OidIsValid(partcollation
[i
]) &&
3756 partcollation
[i
] != DEFAULT_COLLATION_OID
)
3758 ObjectAddressSet(referenced
, CollationRelationId
, partcollation
[i
]);
3759 add_exact_object_address(&referenced
, addrs
);
3763 record_object_address_dependencies(&myself
, addrs
, DEPENDENCY_NORMAL
);
3764 free_object_addresses(addrs
);
3767 * The partitioning columns are made internally dependent on the table,
3768 * because we cannot drop any of them without dropping the whole table.
3769 * (ATExecDropColumn independently enforces that, but it's not bulletproof
3770 * so we need the dependencies too.)
3772 for (i
= 0; i
< partnatts
; i
++)
3774 if (partattrs
[i
] == 0)
3775 continue; /* ignore expressions here */
3777 ObjectAddressSubSet(referenced
, RelationRelationId
,
3778 RelationGetRelid(rel
), partattrs
[i
]);
3779 recordDependencyOn(&referenced
, &myself
, DEPENDENCY_INTERNAL
);
3783 * Also consider anything mentioned in partition expressions. External
3784 * references (e.g. functions) get NORMAL dependencies. Table columns
3785 * mentioned in the expressions are handled the same as plain partitioning
3786 * columns, i.e. they become internally dependent on the whole table.
3789 recordDependencyOnSingleRelExpr(&myself
,
3791 RelationGetRelid(rel
),
3793 DEPENDENCY_INTERNAL
,
3794 true /* reverse the self-deps */ );
3797 * We must invalidate the relcache so that the next
3798 * CommandCounterIncrement() will cause the same to be rebuilt using the
3799 * information in just created catalog entry.
3801 CacheInvalidateRelcache(rel
);
3805 * RemovePartitionKeyByRelId
3806 * Remove pg_partitioned_table entry for a relation
3809 RemovePartitionKeyByRelId(Oid relid
)
3814 rel
= table_open(PartitionedRelationId
, RowExclusiveLock
);
3816 tuple
= SearchSysCache1(PARTRELID
, ObjectIdGetDatum(relid
));
3817 if (!HeapTupleIsValid(tuple
))
3818 elog(ERROR
, "cache lookup failed for partition key of relation %u",
3821 CatalogTupleDelete(rel
, &tuple
->t_self
);
3823 ReleaseSysCache(tuple
);
3824 table_close(rel
, RowExclusiveLock
);
3828 * StorePartitionBound
3829 * Update pg_class tuple of rel to store the partition bound and set
3830 * relispartition to true
3832 * If this is the default partition, also update the default partition OID in
3833 * pg_partitioned_table.
3835 * Also, invalidate the parent's relcache, so that the next rebuild will load
3836 * the new partition's info into its partition descriptor. If there is a
3837 * default partition, we must invalidate its relcache entry as well.
3840 StorePartitionBound(Relation rel
, Relation parent
, PartitionBoundSpec
*bound
)
3845 Datum new_val
[Natts_pg_class
];
3846 bool new_null
[Natts_pg_class
],
3847 new_repl
[Natts_pg_class
];
3850 /* Update pg_class tuple */
3851 classRel
= table_open(RelationRelationId
, RowExclusiveLock
);
3852 tuple
= SearchSysCacheCopy1(RELOID
,
3853 ObjectIdGetDatum(RelationGetRelid(rel
)));
3854 if (!HeapTupleIsValid(tuple
))
3855 elog(ERROR
, "cache lookup failed for relation %u",
3856 RelationGetRelid(rel
));
3858 #ifdef USE_ASSERT_CHECKING
3860 Form_pg_class classForm
;
3863 classForm
= (Form_pg_class
) GETSTRUCT(tuple
);
3864 Assert(!classForm
->relispartition
);
3865 (void) SysCacheGetAttr(RELOID
, tuple
, Anum_pg_class_relpartbound
,
3871 /* Fill in relpartbound value */
3872 memset(new_val
, 0, sizeof(new_val
));
3873 memset(new_null
, false, sizeof(new_null
));
3874 memset(new_repl
, false, sizeof(new_repl
));
3875 new_val
[Anum_pg_class_relpartbound
- 1] = CStringGetTextDatum(nodeToString(bound
));
3876 new_null
[Anum_pg_class_relpartbound
- 1] = false;
3877 new_repl
[Anum_pg_class_relpartbound
- 1] = true;
3878 newtuple
= heap_modify_tuple(tuple
, RelationGetDescr(classRel
),
3879 new_val
, new_null
, new_repl
);
3880 /* Also set the flag */
3881 ((Form_pg_class
) GETSTRUCT(newtuple
))->relispartition
= true;
3882 CatalogTupleUpdate(classRel
, &newtuple
->t_self
, newtuple
);
3883 heap_freetuple(newtuple
);
3884 table_close(classRel
, RowExclusiveLock
);
3887 * If we're storing bounds for the default partition, update
3888 * pg_partitioned_table too.
3890 if (bound
->is_default
)
3891 update_default_partition_oid(RelationGetRelid(parent
),
3892 RelationGetRelid(rel
));
3894 /* Make these updates visible */
3895 CommandCounterIncrement();
3898 * The partition constraint for the default partition depends on the
3899 * partition bounds of every other partition, so we must invalidate the
3900 * relcache entry for that partition every time a partition is added or
3904 get_default_oid_from_partdesc(RelationGetPartitionDesc(parent
, true));
3905 if (OidIsValid(defaultPartOid
))
3906 CacheInvalidateRelcacheByRelid(defaultPartOid
);
3908 CacheInvalidateRelcache(parent
);