PostgreSQL Source Code git master
Loading...
Searching...
No Matches
heap.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/multixact.h"
#include "access/relation.h"
#include "access/table.h"
#include "access/tableam.h"
#include "catalog/binary_upgrade.h"
#include "catalog/catalog.h"
#include "catalog/heap.h"
#include "catalog/index.h"
#include "catalog/objectaccess.h"
#include "catalog/partition.h"
#include "catalog/pg_am.h"
#include "catalog/pg_attrdef.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_foreign_table.h"
#include "catalog/pg_inherits.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_opclass.h"
#include "catalog/pg_partitioned_table.h"
#include "catalog/pg_statistic.h"
#include "catalog/pg_subscription_rel.h"
#include "catalog/pg_tablespace.h"
#include "catalog/pg_type.h"
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
#include "parser/parse_coerce.h"
#include "parser/parse_collate.h"
#include "parser/parse_expr.h"
#include "parser/parse_relation.h"
#include "parser/parsetree.h"
#include "partitioning/partdesc.h"
#include "pgstat.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/inval.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
Include dependency graph for heap.c:

Go to the source code of this file.

Functions

static void AddNewRelationTuple (Relation pg_class_desc, Relation new_rel_desc, Oid new_rel_oid, Oid new_type_oid, Oid reloftype, Oid relowner, char relkind, TransactionId relfrozenxid, TransactionId relminmxid, Datum relacl, Datum reloptions)
 
static ObjectAddress AddNewRelationType (const char *typeName, Oid typeNamespace, Oid new_rel_oid, char new_rel_kind, Oid ownerid, Oid new_row_type, Oid new_array_type)
 
static void RelationRemoveInheritance (Oid relid)
 
static Oid StoreRelCheck (Relation rel, const char *ccname, Node *expr, bool is_enforced, bool is_validated, bool is_local, int16 inhcount, bool is_no_inherit, bool is_internal)
 
static void StoreConstraints (Relation rel, List *cooked_constraints, bool is_internal)
 
static bool MergeWithExistingConstraint (Relation rel, const char *ccname, Node *expr, bool allow_merge, bool is_local, bool is_enforced, bool is_initially_valid, bool is_no_inherit)
 
static void SetRelationNumChecks (Relation rel, int numchecks)
 
static NodecookConstraint (ParseState *pstate, Node *raw_constraint, char *relname)
 
const FormData_pg_attributeSystemAttributeDefinition (AttrNumber attno)
 
const FormData_pg_attributeSystemAttributeByName (const char *attname)
 
Relation heap_create (const char *relname, Oid relnamespace, Oid reltablespace, Oid relid, RelFileNumber relfilenumber, Oid accessmtd, TupleDesc tupDesc, char relkind, char relpersistence, bool shared_relation, bool mapped_relation, bool allow_system_table_mods, TransactionId *relfrozenxid, MultiXactId *relminmxid, bool create_storage)
 
void CheckAttributeNamesTypes (TupleDesc tupdesc, char relkind, int flags)
 
void CheckAttributeType (const char *attname, Oid atttypid, Oid attcollation, List *containing_rowtypes, int flags)
 
void InsertPgAttributeTuples (Relation pg_attribute_rel, TupleDesc tupdesc, Oid new_rel_oid, const FormExtraData_pg_attribute tupdesc_extra[], CatalogIndexState indstate)
 
static void AddNewAttributeTuples (Oid new_rel_oid, TupleDesc tupdesc, char relkind)
 
void InsertPgClassTuple (Relation pg_class_desc, Relation new_rel_desc, Oid new_rel_oid, Datum relacl, Datum reloptions)
 
Oid heap_create_with_catalog (const char *relname, Oid relnamespace, Oid reltablespace, Oid relid, Oid reltypeid, Oid reloftypeid, Oid ownerid, Oid accessmtd, TupleDesc tupdesc, List *cooked_constraints, char relkind, char relpersistence, bool shared_relation, bool mapped_relation, OnCommitAction oncommit, Datum reloptions, bool use_user_acl, bool allow_system_table_mods, bool is_internal, Oid relrewrite, ObjectAddress *typaddress)
 
void DeleteRelationTuple (Oid relid)
 
void DeleteAttributeTuples (Oid relid)
 
void DeleteSystemAttributeTuples (Oid relid)
 
void RemoveAttributeById (Oid relid, AttrNumber attnum)
 
void heap_drop_with_catalog (Oid relid)
 
void RelationClearMissing (Relation rel)
 
void StoreAttrMissingVal (Relation rel, AttrNumber attnum, Datum missingval)
 
void SetAttrMissing (Oid relid, char *attname, char *value)
 
static Oid StoreRelNotNull (Relation rel, const char *nnname, AttrNumber attnum, bool is_validated, bool is_local, int inhcount, bool is_no_inherit)
 
ListAddRelationNewConstraints (Relation rel, List *newColDefaults, List *newConstraints, bool allow_merge, bool is_local, bool is_internal, const char *queryString)
 
ListAddRelationNotNullConstraints (Relation rel, List *constraints, List *old_notnulls)
 
static bool check_nested_generated_walker (Node *node, void *context)
 
static void check_nested_generated (ParseState *pstate, Node *node)
 
static bool contains_user_functions_checker (Oid func_id, void *context)
 
static bool check_virtual_generated_security_walker (Node *node, void *context)
 
static void check_virtual_generated_security (ParseState *pstate, Node *node)
 
NodecookDefault (ParseState *pstate, Node *raw_default, Oid atttypid, int32 atttypmod, const char *attname, char attgenerated)
 
void CopyStatistics (Oid fromrelid, Oid torelid)
 
void RemoveStatistics (Oid relid, AttrNumber attnum)
 
static void RelationTruncateIndexes (Relation heapRelation)
 
void heap_truncate (List *relids)
 
void heap_truncate_one_rel (Relation rel)
 
void heap_truncate_check_FKs (List *relations, bool tempTables)
 
Listheap_truncate_find_FKs (List *relationIds)
 
void StorePartitionKey (Relation rel, char strategy, int16 partnatts, AttrNumber *partattrs, List *partexprs, Oid *partopclass, Oid *partcollation)
 
void RemovePartitionKeyByRelId (Oid relid)
 
void StorePartitionBound (Relation rel, Relation parent, PartitionBoundSpec *bound)
 

Variables

Oid binary_upgrade_next_heap_pg_class_oid = InvalidOid
 
Oid binary_upgrade_next_toast_pg_class_oid = InvalidOid
 
RelFileNumber binary_upgrade_next_heap_pg_class_relfilenumber = InvalidRelFileNumber
 
RelFileNumber binary_upgrade_next_toast_pg_class_relfilenumber = InvalidRelFileNumber
 
static const FormData_pg_attribute a1
 
static const FormData_pg_attribute a2
 
static const FormData_pg_attribute a3
 
static const FormData_pg_attribute a4
 
static const FormData_pg_attribute a5
 
static const FormData_pg_attribute a6
 
static const FormData_pg_attribute *const SysAtt [] = {&a1, &a2, &a3, &a4, &a5, &a6}
 

Function Documentation

◆ AddNewAttributeTuples()

static void AddNewAttributeTuples ( Oid  new_rel_oid,
TupleDesc  tupdesc,
char  relkind 
)
static

Definition at line 834 of file heap.c.

837{
838 Relation rel;
840 int natts = tupdesc->natts;
843
844 /*
845 * open pg_attribute and its indexes.
846 */
848
850
852
853 /* add dependencies on their datatypes and collations */
854 for (int i = 0; i < natts; i++)
855 {
856 Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
857
858 /* Add dependency info */
862
863 /* The default collation is pinned, so don't bother recording it */
864 if (OidIsValid(attr->attcollation) &&
865 attr->attcollation != DEFAULT_COLLATION_OID)
866 {
868 attr->attcollation);
870 }
871 }
872
873 /*
874 * Next we add the system attributes. Skip all for a view or type
875 * relation. We don't bother with making datatype dependencies here,
876 * since presumably all these types are pinned.
877 */
878 if (relkind != RELKIND_VIEW && relkind != RELKIND_COMPOSITE_TYPE)
879 {
880 TupleDesc td;
881
883
885 FreeTupleDesc(td);
886 }
887
888 /*
889 * clean up
890 */
892
894}
#define lengthof(array)
Definition c.h:803
#define OidIsValid(objectId)
Definition c.h:788
@ DEPENDENCY_NORMAL
Definition dependency.h:33
static const FormData_pg_attribute *const SysAtt[]
Definition heap.c:228
void InsertPgAttributeTuples(Relation pg_attribute_rel, TupleDesc tupdesc, Oid new_rel_oid, const FormExtraData_pg_attribute tupdesc_extra[], CatalogIndexState indstate)
Definition heap.c:717
void CatalogCloseIndexes(CatalogIndexState indstate)
Definition indexing.c:61
CatalogIndexState CatalogOpenIndexes(Relation heapRel)
Definition indexing.c:43
int i
Definition isn.c:77
#define RowExclusiveLock
Definition lockdefs.h:38
#define ObjectAddressSet(addr, class_id, object_id)
#define ObjectAddressSubSet(addr, class_id, object_id, object_sub_id)
FormData_pg_attribute
FormData_pg_attribute * Form_pg_attribute
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition pg_depend.c:45
static int fb(int x)
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40
void FreeTupleDesc(TupleDesc tupdesc)
Definition tupdesc.c:485
TupleDesc CreateTupleDesc(int natts, Form_pg_attribute *attrs)
Definition tupdesc.c:212
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:160

References CatalogCloseIndexes(), CatalogOpenIndexes(), CreateTupleDesc(), DEPENDENCY_NORMAL, fb(), FormData_pg_attribute, FreeTupleDesc(), i, InsertPgAttributeTuples(), lengthof, TupleDescData::natts, ObjectAddressSet, ObjectAddressSubSet, OidIsValid, recordDependencyOn(), RowExclusiveLock, SysAtt, table_close(), table_open(), and TupleDescAttr().

Referenced by heap_create_with_catalog().

◆ AddNewRelationTuple()

static void AddNewRelationTuple ( Relation  pg_class_desc,
Relation  new_rel_desc,
Oid  new_rel_oid,
Oid  new_type_oid,
Oid  reloftype,
Oid  relowner,
char  relkind,
TransactionId  relfrozenxid,
TransactionId  relminmxid,
Datum  relacl,
Datum  reloptions 
)
static

Definition at line 984 of file heap.c.

995{
997
998 /*
999 * first we update some of the information in our uncataloged relation's
1000 * relation descriptor.
1001 */
1002 new_rel_reltup = new_rel_desc->rd_rel;
1003
1004 /* The relation is empty */
1005 new_rel_reltup->relpages = 0;
1006 new_rel_reltup->reltuples = -1;
1007 new_rel_reltup->relallvisible = 0;
1008 new_rel_reltup->relallfrozen = 0;
1009
1010 /* Sequences always have a known size */
1011 if (relkind == RELKIND_SEQUENCE)
1012 {
1013 new_rel_reltup->relpages = 1;
1014 new_rel_reltup->reltuples = 1;
1015 }
1016
1017 new_rel_reltup->relfrozenxid = relfrozenxid;
1018 new_rel_reltup->relminmxid = relminmxid;
1019 new_rel_reltup->relowner = relowner;
1020 new_rel_reltup->reltype = new_type_oid;
1021 new_rel_reltup->reloftype = reloftype;
1022
1023 /* relispartition is always set by updating this tuple later */
1024 new_rel_reltup->relispartition = false;
1025
1026 /* fill rd_att's type ID with something sane even if reltype is zero */
1027 new_rel_desc->rd_att->tdtypeid = new_type_oid ? new_type_oid : RECORDOID;
1028 new_rel_desc->rd_att->tdtypmod = -1;
1029
1030 /* Now build and insert the tuple */
1032 relacl, reloptions);
1033}
void InsertPgClassTuple(Relation pg_class_desc, Relation new_rel_desc, Oid new_rel_oid, Datum relacl, Datum reloptions)
Definition heap.c:910
FormData_pg_class * Form_pg_class
Definition pg_class.h:156

References fb(), and InsertPgClassTuple().

Referenced by heap_create_with_catalog().

◆ AddNewRelationType()

static ObjectAddress AddNewRelationType ( const char typeName,
Oid  typeNamespace,
Oid  new_rel_oid,
char  new_rel_kind,
Oid  ownerid,
Oid  new_row_type,
Oid  new_array_type 
)
static

Definition at line 1043 of file heap.c.

1050{
1051 return
1052 TypeCreate(new_row_type, /* optional predetermined OID */
1053 typeName, /* type name */
1054 typeNamespace, /* type namespace */
1055 new_rel_oid, /* relation oid */
1056 new_rel_kind, /* relation kind */
1057 ownerid, /* owner's ID */
1058 -1, /* internal size (varlena) */
1059 TYPTYPE_COMPOSITE, /* type-type (composite) */
1060 TYPCATEGORY_COMPOSITE, /* type-category (ditto) */
1061 false, /* composite types are never preferred */
1062 DEFAULT_TYPDELIM, /* default array delimiter */
1063 F_RECORD_IN, /* input procedure */
1064 F_RECORD_OUT, /* output procedure */
1065 F_RECORD_RECV, /* receive procedure */
1066 F_RECORD_SEND, /* send procedure */
1067 InvalidOid, /* typmodin procedure - none */
1068 InvalidOid, /* typmodout procedure - none */
1069 InvalidOid, /* analyze procedure - default */
1070 InvalidOid, /* subscript procedure - none */
1071 InvalidOid, /* array element type - irrelevant */
1072 false, /* this is not an array type */
1073 new_array_type, /* array type if any */
1074 InvalidOid, /* domain base type - irrelevant */
1075 NULL, /* default value - none */
1076 NULL, /* default binary representation */
1077 false, /* passed by reference */
1078 TYPALIGN_DOUBLE, /* alignment - must be the largest! */
1079 TYPSTORAGE_EXTENDED, /* fully TOASTable */
1080 -1, /* typmod */
1081 0, /* array dimensions for typBaseType */
1082 false, /* Type NOT NULL */
1083 InvalidOid); /* rowtypes never have a collation */
1084}
ObjectAddress TypeCreate(Oid newTypeOid, const char *typeName, Oid typeNamespace, Oid relationOid, char relationKind, Oid ownerId, int16 internalSize, char typeType, char typeCategory, bool typePreferred, char typDelim, Oid inputProcedure, Oid outputProcedure, Oid receiveProcedure, Oid sendProcedure, Oid typmodinProcedure, Oid typmodoutProcedure, Oid analyzeProcedure, Oid subscriptProcedure, Oid elementType, bool isImplicitArray, Oid arrayType, Oid baseType, const char *defaultTypeValue, char *defaultTypeBin, bool passedByValue, char alignment, char storage, int32 typeMod, int32 typNDims, bool typeNotNull, Oid typeCollation)
Definition pg_type.c:195
#define InvalidOid
#define DEFAULT_TYPDELIM
Definition typecmds.h:22

References DEFAULT_TYPDELIM, fb(), InvalidOid, and TypeCreate().

Referenced by heap_create_with_catalog().

◆ AddRelationNewConstraints()

List * AddRelationNewConstraints ( Relation  rel,
List newColDefaults,
List newConstraints,
bool  allow_merge,
bool  is_local,
bool  is_internal,
const char queryString 
)

Definition at line 2385 of file heap.c.

2392{
2396 int numoldchecks;
2397 ParseState *pstate;
2399 int numchecks;
2401 List *nnnames;
2402 Node *expr;
2404
2405 /*
2406 * Get info about existing constraints.
2407 */
2409 oldconstr = tupleDesc->constr;
2410 if (oldconstr)
2411 numoldchecks = oldconstr->num_check;
2412 else
2413 numoldchecks = 0;
2414
2415 /*
2416 * Create a dummy ParseState and insert the target relation as its sole
2417 * rangetable entry. We need a ParseState for transformExpr.
2418 */
2419 pstate = make_parsestate(NULL);
2420 pstate->p_sourcetext = queryString;
2422 rel,
2424 NULL,
2425 false,
2426 true);
2427 addNSItemToQuery(pstate, nsitem, true, true, true);
2428
2429 /*
2430 * Process column default expressions.
2431 */
2433 {
2434 Form_pg_attribute atp = TupleDescAttr(rel->rd_att, colDef->attnum - 1);
2435 Oid defOid;
2436
2437 expr = cookDefault(pstate, colDef->raw_default,
2438 atp->atttypid, atp->atttypmod,
2439 NameStr(atp->attname),
2440 atp->attgenerated);
2441
2442 /*
2443 * If the expression is just a NULL constant, we do not bother to make
2444 * an explicit pg_attrdef entry, since the default behavior is
2445 * equivalent. This applies to column defaults, but not for
2446 * generation expressions.
2447 *
2448 * Note a nonobvious property of this test: if the column is of a
2449 * domain type, what we'll get is not a bare null Const but a
2450 * CoerceToDomain expr, so we will not discard the default. This is
2451 * critical because the column default needs to be retained to
2452 * override any default that the domain might have.
2453 */
2454 if (expr == NULL ||
2455 (!colDef->generated &&
2456 IsA(expr, Const) &&
2457 castNode(Const, expr)->constisnull))
2458 continue;
2459
2460 defOid = StoreAttrDefault(rel, colDef->attnum, expr, is_internal);
2461
2463 cooked->contype = CONSTR_DEFAULT;
2464 cooked->conoid = defOid;
2465 cooked->name = NULL;
2466 cooked->attnum = colDef->attnum;
2467 cooked->expr = expr;
2468 cooked->is_enforced = true;
2469 cooked->skip_validation = false;
2470 cooked->is_local = is_local;
2471 cooked->inhcount = is_local ? 0 : 1;
2472 cooked->is_no_inherit = false;
2474 }
2475
2476 /*
2477 * Process constraint expressions.
2478 */
2480 checknames = NIL;
2481 nnnames = NIL;
2483 {
2484 Oid constrOid;
2485
2486 if (cdef->contype == CONSTR_CHECK)
2487 {
2488 char *ccname;
2489
2490 if (cdef->raw_expr != NULL)
2491 {
2492 Assert(cdef->cooked_expr == NULL);
2493
2494 /*
2495 * Transform raw parsetree to executable expression, and
2496 * verify it's valid as a CHECK constraint.
2497 */
2498 expr = cookConstraint(pstate, cdef->raw_expr,
2500 }
2501 else
2502 {
2503 Assert(cdef->cooked_expr != NULL);
2504
2505 /*
2506 * Here, we assume the parser will only pass us valid CHECK
2507 * expressions, so we do no particular checking.
2508 */
2509 expr = stringToNode(cdef->cooked_expr);
2510 }
2511
2512 /*
2513 * Check name uniqueness, or generate a name if none was given.
2514 */
2515 if (cdef->conname != NULL)
2516 {
2517 ccname = cdef->conname;
2518 /* Check against other new constraints */
2519 /* Needed because we don't do CommandCounterIncrement in loop */
2521 {
2522 if (strcmp(chkname, ccname) == 0)
2523 ereport(ERROR,
2525 errmsg("check constraint \"%s\" already exists",
2526 ccname)));
2527 }
2528
2529 /* save name for future checks */
2530 checknames = lappend(checknames, ccname);
2531
2532 /*
2533 * Check against pre-existing constraints. If we are allowed
2534 * to merge with an existing constraint, there's no more to do
2535 * here. (We omit the duplicate constraint from the result,
2536 * which is what ATAddCheckNNConstraint wants.)
2537 */
2538 if (MergeWithExistingConstraint(rel, ccname, expr,
2539 allow_merge, is_local,
2540 cdef->is_enforced,
2541 cdef->initially_valid,
2542 cdef->is_no_inherit))
2543 continue;
2544 }
2545 else
2546 {
2547 /*
2548 * When generating a name, we want to create "tab_col_check"
2549 * for a column constraint and "tab_check" for a table
2550 * constraint. We no longer have any info about the syntactic
2551 * positioning of the constraint phrase, so we approximate
2552 * this by seeing whether the expression references more than
2553 * one column. (If the user played by the rules, the result
2554 * is the same...)
2555 *
2556 * Note: pull_var_clause() doesn't descend into sublinks, but
2557 * we eliminated those above; and anyway this only needs to be
2558 * an approximate answer.
2559 */
2560 List *vars;
2561 char *colname;
2562
2563 vars = pull_var_clause(expr, 0);
2564
2565 /* eliminate duplicates */
2566 vars = list_union(NIL, vars);
2567
2568 if (list_length(vars) == 1)
2569 colname = get_attname(RelationGetRelid(rel),
2570 ((Var *) linitial(vars))->varattno,
2571 true);
2572 else
2573 colname = NULL;
2574
2576 colname,
2577 "check",
2579 checknames);
2580
2581 /* save name for future checks */
2582 checknames = lappend(checknames, ccname);
2583 }
2584
2585 /*
2586 * OK, store it.
2587 */
2588 constrOid =
2589 StoreRelCheck(rel, ccname, expr, cdef->is_enforced,
2590 cdef->initially_valid, is_local,
2591 is_local ? 0 : 1, cdef->is_no_inherit,
2592 is_internal);
2593
2594 numchecks++;
2595
2597 cooked->contype = CONSTR_CHECK;
2598 cooked->conoid = constrOid;
2599 cooked->name = ccname;
2600 cooked->attnum = 0;
2601 cooked->expr = expr;
2602 cooked->is_enforced = cdef->is_enforced;
2603 cooked->skip_validation = cdef->skip_validation;
2604 cooked->is_local = is_local;
2605 cooked->inhcount = is_local ? 0 : 1;
2606 cooked->is_no_inherit = cdef->is_no_inherit;
2608 }
2609 else if (cdef->contype == CONSTR_NOTNULL)
2610 {
2612 AttrNumber colnum;
2613 int16 inhcount = is_local ? 0 : 1;
2614 char *nnname;
2615
2616 /* Determine which column to modify */
2617 colnum = get_attnum(RelationGetRelid(rel), strVal(linitial(cdef->keys)));
2618 if (colnum == InvalidAttrNumber)
2619 ereport(ERROR,
2621 errmsg("column \"%s\" of relation \"%s\" does not exist",
2623 if (colnum < InvalidAttrNumber)
2624 ereport(ERROR,
2626 errmsg("cannot add not-null constraint on system column \"%s\"",
2627 strVal(linitial(cdef->keys))));
2628
2629 Assert(cdef->initially_valid != cdef->skip_validation);
2630
2631 /*
2632 * If the column already has a not-null constraint, we don't want
2633 * to add another one; adjust inheritance status as needed. This
2634 * also checks whether the existing constraint matches the
2635 * requested validity.
2636 */
2638 cdef->conname,
2639 is_local, cdef->is_no_inherit,
2640 cdef->skip_validation))
2641 continue;
2642
2643 /*
2644 * If a constraint name is specified, check that it isn't already
2645 * used. Otherwise, choose a non-conflicting one ourselves.
2646 */
2647 if (cdef->conname)
2648 {
2650 RelationGetRelid(rel),
2651 cdef->conname))
2652 ereport(ERROR,
2654 errmsg("constraint \"%s\" for relation \"%s\" already exists",
2655 cdef->conname, RelationGetRelationName(rel)));
2656 nnname = cdef->conname;
2657 }
2658 else
2660 strVal(linitial(cdef->keys)),
2661 "not_null",
2663 nnnames);
2665
2666 constrOid =
2667 StoreRelNotNull(rel, nnname, colnum,
2668 cdef->initially_valid,
2669 is_local,
2670 inhcount,
2671 cdef->is_no_inherit);
2672
2674 nncooked->contype = CONSTR_NOTNULL;
2675 nncooked->conoid = constrOid;
2676 nncooked->name = nnname;
2677 nncooked->attnum = colnum;
2678 nncooked->expr = NULL;
2679 nncooked->is_enforced = true;
2680 nncooked->skip_validation = cdef->skip_validation;
2681 nncooked->is_local = is_local;
2682 nncooked->inhcount = inhcount;
2683 nncooked->is_no_inherit = cdef->is_no_inherit;
2684
2686 }
2687 }
2688
2689 /*
2690 * Update the count of constraints in the relation's pg_class tuple. We do
2691 * this even if there was no change, in order to ensure that an SI update
2692 * message is sent out for the pg_class tuple, which will force other
2693 * backends to rebuild their relcache entries for the rel. (This is
2694 * critical if we added defaults but not constraints.)
2695 */
2697
2698 return cookedConstraints;
2699}
int16 AttrNumber
Definition attnum.h:21
#define InvalidAttrNumber
Definition attnum.h:23
#define NameStr(name)
Definition c.h:765
#define Assert(condition)
Definition c.h:873
int16_t int16
Definition c.h:541
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
#define palloc_object(type)
Definition fe_memutils.h:74
static Oid StoreRelCheck(Relation rel, const char *ccname, Node *expr, bool is_enforced, bool is_validated, bool is_local, int16 inhcount, bool is_no_inherit, bool is_internal)
Definition heap.c:2147
static bool MergeWithExistingConstraint(Relation rel, const char *ccname, Node *expr, bool allow_merge, bool is_local, bool is_enforced, bool is_initially_valid, bool is_no_inherit)
Definition heap.c:2712
static Oid StoreRelNotNull(Relation rel, const char *nnname, AttrNumber attnum, bool is_validated, bool is_local, int inhcount, bool is_no_inherit)
Definition heap.c:2254
static void SetRelationNumChecks(Relation rel, int numchecks)
Definition heap.c:3147
Node * cookDefault(ParseState *pstate, Node *raw_default, Oid atttypid, int32 atttypmod, const char *attname, char attgenerated)
Definition heap.c:3321
static Node * cookConstraint(ParseState *pstate, Node *raw_constraint, char *relname)
Definition heap.c:3402
List * lappend(List *list, void *datum)
Definition list.c:339
List * list_union(const List *list1, const List *list2)
Definition list.c:1066
#define AccessShareLock
Definition lockdefs.h:36
AttrNumber get_attnum(Oid relid, const char *attname)
Definition lsyscache.c:934
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition lsyscache.c:903
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define castNode(_type_, nodeptr)
Definition nodes.h:182
ParseState * make_parsestate(ParseState *parentParseState)
Definition parse_node.c:39
ParseNamespaceItem * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, int lockmode, Alias *alias, bool inh, bool inFromCl)
void addNSItemToQuery(ParseState *pstate, ParseNamespaceItem *nsitem, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
@ CONSTR_DEFAULT
@ CONSTR_NOTNULL
@ CONSTR_CHECK
Oid StoreAttrDefault(Relation rel, AttrNumber attnum, Node *expr, bool is_internal)
Definition pg_attrdef.c:37
bool ConstraintNameIsUsed(ConstraintCategory conCat, Oid objId, const char *conname)
bool AdjustNotNullInheritance(Oid relid, AttrNumber attnum, const char *new_conname, bool is_local, bool is_no_inherit, bool is_notvalid)
char * ChooseConstraintName(const char *name1, const char *name2, const char *label, Oid namespaceid, List *others)
@ CONSTRAINT_RELATION
static int list_length(const List *l)
Definition pg_list.h:152
#define NIL
Definition pg_list.h:68
#define foreach_ptr(type, var, lst)
Definition pg_list.h:469
#define linitial(l)
Definition pg_list.h:178
#define foreach_node(type, var, lst)
Definition pg_list.h:496
unsigned int Oid
void * stringToNode(const char *str)
Definition read.c:90
#define RelationGetRelid(relation)
Definition rel.h:514
#define RelationGetDescr(relation)
Definition rel.h:540
#define RelationGetRelationName(relation)
Definition rel.h:548
#define RelationGetNamespace(relation)
Definition rel.h:555
#define ERRCODE_DUPLICATE_OBJECT
Definition streamutil.c:30
Definition pg_list.h:54
Definition nodes.h:135
const char * p_sourcetext
Definition parse_node.h:195
TupleDesc rd_att
Definition rel.h:112
#define strVal(v)
Definition value.h:82
List * pull_var_clause(Node *node, int flags)
Definition var.c:653

References AccessShareLock, addNSItemToQuery(), addRangeTableEntryForRelation(), AdjustNotNullInheritance(), Assert, castNode, ChooseConstraintName(), CONSTR_CHECK, CONSTR_DEFAULT, CONSTR_NOTNULL, CONSTRAINT_RELATION, ConstraintNameIsUsed(), cookConstraint(), cookDefault(), ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, fb(), foreach_node, foreach_ptr, get_attname(), get_attnum(), InvalidAttrNumber, IsA, lappend(), linitial, list_length(), list_union(), make_parsestate(), MergeWithExistingConstraint(), NameStr, NIL, ParseState::p_sourcetext, palloc_object, pull_var_clause(), RelationData::rd_att, RelationGetDescr, RelationGetNamespace, RelationGetRelationName, RelationGetRelid, SetRelationNumChecks(), StoreAttrDefault(), StoreRelCheck(), StoreRelNotNull(), stringToNode(), strVal, and TupleDescAttr().

Referenced by ATAddCheckNNConstraint(), ATExecAddColumn(), ATExecColumnDefault(), ATExecSetExpression(), ATExecSetNotNull(), createTableConstraints(), and DefineRelation().

◆ AddRelationNotNullConstraints()

List * AddRelationNotNullConstraints ( Relation  rel,
List constraints,
List old_notnulls 
)

Definition at line 2895 of file heap.c.

2897{
2899 List *nnnames;
2900 List *nncols = NIL;
2901
2902 /*
2903 * We track two lists of names: nnnames keeps all the constraint names,
2904 * givennames tracks user-generated names. The distinction is important,
2905 * because we must raise error for user-generated name conflicts, but for
2906 * system-generated name conflicts we just generate another.
2907 */
2908 nnnames = NIL;
2909 givennames = NIL;
2910
2911 /*
2912 * First, create all not-null constraints that are directly specified by
2913 * the user. Note that inheritance might have given us another source for
2914 * each, so we must scan the old_notnulls list and increment inhcount for
2915 * each element with identical attnum. We delete from there any element
2916 * that we process.
2917 *
2918 * We don't use foreach() here because we have two nested loops over the
2919 * constraint list, with possible element deletions in the inner one. If
2920 * we used foreach_delete_current() it could only fix up the state of one
2921 * of the loops, so it seems cleaner to use looping over list indexes for
2922 * both loops. Note that any deletion will happen beyond where the outer
2923 * loop is, so its index never needs adjustment.
2924 */
2925 for (int outerpos = 0; outerpos < list_length(constraints); outerpos++)
2926 {
2927 Constraint *constr;
2929 char *conname;
2930 int inhcount = 0;
2931
2932 constr = list_nth_node(Constraint, constraints, outerpos);
2933
2934 Assert(constr->contype == CONSTR_NOTNULL);
2935
2937 strVal(linitial(constr->keys)));
2939 ereport(ERROR,
2941 errmsg("column \"%s\" of relation \"%s\" does not exist",
2942 strVal(linitial(constr->keys)),
2945 ereport(ERROR,
2947 errmsg("cannot add not-null constraint on system column \"%s\"",
2948 strVal(linitial(constr->keys))));
2949
2950 /*
2951 * A column can only have one not-null constraint, so discard any
2952 * additional ones that appear for columns we already saw; but check
2953 * that the NO INHERIT flags match.
2954 */
2955 for (int restpos = outerpos + 1; restpos < list_length(constraints);)
2956 {
2958
2959 other = list_nth_node(Constraint, constraints, restpos);
2960 if (strcmp(strVal(linitial(constr->keys)),
2961 strVal(linitial(other->keys))) == 0)
2962 {
2963 if (other->is_no_inherit != constr->is_no_inherit)
2964 ereport(ERROR,
2966 errmsg("conflicting NO INHERIT declaration for not-null constraint on column \"%s\"",
2967 strVal(linitial(constr->keys))));
2968
2969 /*
2970 * Preserve constraint name if one is specified, but raise an
2971 * error if conflicting ones are specified.
2972 */
2973 if (other->conname)
2974 {
2975 if (!constr->conname)
2976 constr->conname = pstrdup(other->conname);
2977 else if (strcmp(constr->conname, other->conname) != 0)
2978 ereport(ERROR,
2980 errmsg("conflicting not-null constraint names \"%s\" and \"%s\"",
2981 constr->conname, other->conname));
2982 }
2983
2984 /* XXX do we need to verify any other fields? */
2985 constraints = list_delete_nth_cell(constraints, restpos);
2986 }
2987 else
2988 restpos++;
2989 }
2990
2991 /*
2992 * Search in the list of inherited constraints for any entries on the
2993 * same column; determine an inheritance count from that. Also, if at
2994 * least one parent has a constraint for this column, then we must not
2995 * accept a user specification for a NO INHERIT one. Any constraint
2996 * from parents that we process here is deleted from the list: we no
2997 * longer need to process it in the loop below.
2998 */
3000 {
3001 if (old->attnum == attnum)
3002 {
3003 /*
3004 * If we get a constraint from the parent, having a local NO
3005 * INHERIT one doesn't work.
3006 */
3007 if (constr->is_no_inherit)
3008 ereport(ERROR,
3010 errmsg("cannot define not-null constraint with NO INHERIT on column \"%s\"",
3011 strVal(linitial(constr->keys))),
3012 errdetail("The column has an inherited not-null constraint.")));
3013
3014 inhcount++;
3016 }
3017 }
3018
3019 /*
3020 * Determine a constraint name, which may have been specified by the
3021 * user, or raise an error if a conflict exists with another
3022 * user-specified name.
3023 */
3024 if (constr->conname)
3025 {
3027 {
3028 if (strcmp(thisname, constr->conname) == 0)
3029 ereport(ERROR,
3031 errmsg("constraint \"%s\" for relation \"%s\" already exists",
3032 constr->conname,
3034 }
3035
3036 conname = constr->conname;
3037 givennames = lappend(givennames, conname);
3038 }
3039 else
3042 attnum, false),
3043 "not_null",
3045 nnnames);
3046 nnnames = lappend(nnnames, conname);
3047
3048 StoreRelNotNull(rel, conname,
3049 attnum, true, true,
3050 inhcount, constr->is_no_inherit);
3051
3053 }
3054
3055 /*
3056 * If any column remains in the old_notnulls list, we must create a not-
3057 * null constraint marked not-local for that column. Because multiple
3058 * parents could specify a not-null constraint for the same column, we
3059 * must count how many there are and set an appropriate inhcount
3060 * accordingly, deleting elements we've already processed.
3061 *
3062 * We don't use foreach() here because we have two nested loops over the
3063 * constraint list, with possible element deletions in the inner one. If
3064 * we used foreach_delete_current() it could only fix up the state of one
3065 * of the loops, so it seems cleaner to use looping over list indexes for
3066 * both loops. Note that any deletion will happen beyond where the outer
3067 * loop is, so its index never needs adjustment.
3068 */
3070 {
3072 char *conname = NULL;
3073 int inhcount = 1;
3074
3076 Assert(cooked->contype == CONSTR_NOTNULL);
3077 Assert(cooked->name);
3078
3079 /*
3080 * Preserve the first non-conflicting constraint name we come across.
3081 */
3082 if (conname == NULL)
3083 conname = cooked->name;
3084
3085 for (int restpos = outerpos + 1; restpos < list_length(old_notnulls);)
3086 {
3088
3090 Assert(other->name);
3091 if (other->attnum == cooked->attnum)
3092 {
3093 if (conname == NULL)
3094 conname = other->name;
3095
3096 inhcount++;
3098 }
3099 else
3100 restpos++;
3101 }
3102
3103 /* If we got a name, make sure it isn't one we've already used */
3104 if (conname != NULL)
3105 {
3107 {
3108 if (strcmp(thisname, conname) == 0)
3109 {
3110 conname = NULL;
3111 break;
3112 }
3113 }
3114 }
3115
3116 /* and choose a name, if needed */
3117 if (conname == NULL)
3120 cooked->attnum, false),
3121 "not_null",
3123 nnnames);
3124 nnnames = lappend(nnnames, conname);
3125
3126 /* ignore the origin constraint's is_local and inhcount */
3127 StoreRelNotNull(rel, conname, cooked->attnum, true,
3128 false, inhcount, false);
3129
3130 nncols = lappend_int(nncols, cooked->attnum);
3131 }
3132
3133 return nncols;
3134}
int errdetail(const char *fmt,...)
Definition elog.c:1216
List * list_delete_nth_cell(List *list, int n)
Definition list.c:767
List * lappend_int(List *list, int datum)
Definition list.c:357
char * pstrdup(const char *in)
Definition mcxt.c:1781
int16 attnum
#define foreach_delete_current(lst, var_or_cell)
Definition pg_list.h:391
static void * list_nth(const List *list, int n)
Definition pg_list.h:299
#define list_nth_node(type, list, n)
Definition pg_list.h:327
List * keys
ConstrType contype
bool is_no_inherit
char * conname

References Assert, attnum, ChooseConstraintName(), Constraint::conname, CONSTR_NOTNULL, Constraint::contype, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errdetail(), errmsg(), ERROR, fb(), foreach_delete_current, foreach_ptr, get_attname(), get_attnum(), InvalidAttrNumber, Constraint::is_no_inherit, Constraint::keys, lappend(), lappend_int(), linitial, list_delete_nth_cell(), list_length(), list_nth(), list_nth_node, NIL, pstrdup(), RelationGetNamespace, RelationGetRelationName, RelationGetRelid, StoreRelNotNull(), and strVal.

Referenced by DefineRelation().

◆ check_nested_generated()

static void check_nested_generated ( ParseState pstate,
Node node 
)
static

Definition at line 3223 of file heap.c.

3224{
3225 check_nested_generated_walker(node, pstate);
3226}
static bool check_nested_generated_walker(Node *node, void *context)
Definition heap.c:3181

References check_nested_generated_walker().

Referenced by cookDefault().

◆ check_nested_generated_walker()

static bool check_nested_generated_walker ( Node node,
void context 
)
static

Definition at line 3181 of file heap.c.

3182{
3183 ParseState *pstate = context;
3184
3185 if (node == NULL)
3186 return false;
3187 else if (IsA(node, Var))
3188 {
3189 Var *var = (Var *) node;
3190 Oid relid;
3192
3193 relid = rt_fetch(var->varno, pstate->p_rtable)->relid;
3194 if (!OidIsValid(relid))
3195 return false; /* XXX shouldn't we raise an error? */
3196
3197 attnum = var->varattno;
3198
3199 if (attnum > 0 && get_attgenerated(relid, attnum))
3200 ereport(ERROR,
3202 errmsg("cannot use generated column \"%s\" in column generation expression",
3203 get_attname(relid, attnum, false)),
3204 errdetail("A generated column cannot reference another generated column."),
3205 parser_errposition(pstate, var->location)));
3206 /* A whole-row Var is necessarily self-referential, so forbid it */
3207 if (attnum == 0)
3208 ereport(ERROR,
3210 errmsg("cannot use whole-row variable in column generation expression"),
3211 errdetail("This would cause the generated column to depend on its own value."),
3212 parser_errposition(pstate, var->location)));
3213 /* System columns were already checked in the parser */
3214
3215 return false;
3216 }
3217 else
3219 context);
3220}
char get_attgenerated(Oid relid, AttrNumber attnum)
Definition lsyscache.c:964
#define expression_tree_walker(n, w, c)
Definition nodeFuncs.h:153
int parser_errposition(ParseState *pstate, int location)
Definition parse_node.c:106
#define rt_fetch(rangetable_index, rangetable)
Definition parsetree.h:31
List * p_rtable
Definition parse_node.h:196
ParseLoc location
Definition primnodes.h:310
AttrNumber varattno
Definition primnodes.h:274
int varno
Definition primnodes.h:269

References attnum, check_nested_generated_walker(), ereport, errcode(), errdetail(), errmsg(), ERROR, expression_tree_walker, fb(), get_attgenerated(), get_attname(), IsA, Var::location, OidIsValid, ParseState::p_rtable, parser_errposition(), rt_fetch, Var::varattno, and Var::varno.

Referenced by check_nested_generated(), and check_nested_generated_walker().

◆ check_virtual_generated_security()

static void check_virtual_generated_security ( ParseState pstate,
Node node 
)
static

Definition at line 3303 of file heap.c.

3304{
3306}
static bool check_virtual_generated_security_walker(Node *node, void *context)
Definition heap.c:3266

References check_virtual_generated_security_walker().

Referenced by cookDefault().

◆ check_virtual_generated_security_walker()

static bool check_virtual_generated_security_walker ( Node node,
void context 
)
static

Definition at line 3266 of file heap.c.

3267{
3268 ParseState *pstate = context;
3269
3270 if (node == NULL)
3271 return false;
3272
3273 if (!IsA(node, List))
3274 {
3276 ereport(ERROR,
3278 errmsg("generation expression uses user-defined function"),
3279 errdetail("Virtual generated columns that make use of user-defined functions are not yet supported."),
3280 parser_errposition(pstate, exprLocation(node)));
3281
3282 /*
3283 * check_functions_in_node() doesn't check some node types (see
3284 * comment there). We handle CoerceToDomain and MinMaxExpr by
3285 * checking for built-in types. The other listed node types cannot
3286 * call user-definable SQL-visible functions.
3287 *
3288 * We furthermore need this type check to handle built-in, immutable
3289 * polymorphic functions such as array_eq().
3290 */
3291 if (exprType(node) >= FirstUnpinnedObjectId)
3292 ereport(ERROR,
3294 errmsg("generation expression uses user-defined type"),
3295 errdetail("Virtual generated columns that make use of user-defined types are not yet supported."),
3296 parser_errposition(pstate, exprLocation(node)));
3297 }
3298
3300}
static bool contains_user_functions_checker(Oid func_id, void *context)
Definition heap.c:3255
Oid exprType(const Node *expr)
Definition nodeFuncs.c:42
bool check_functions_in_node(Node *node, check_function_callback checker, void *context)
Definition nodeFuncs.c:1909
int exprLocation(const Node *expr)
Definition nodeFuncs.c:1384
#define FirstUnpinnedObjectId
Definition transam.h:196

References check_functions_in_node(), check_virtual_generated_security_walker(), contains_user_functions_checker(), ereport, errcode(), errdetail(), errmsg(), ERROR, expression_tree_walker, exprLocation(), exprType(), fb(), FirstUnpinnedObjectId, IsA, and parser_errposition().

Referenced by check_virtual_generated_security(), and check_virtual_generated_security_walker().

◆ CheckAttributeNamesTypes()

void CheckAttributeNamesTypes ( TupleDesc  tupdesc,
char  relkind,
int  flags 
)

Definition at line 452 of file heap.c.

454{
455 int i;
456 int j;
457 int natts = tupdesc->natts;
458
459 /* Sanity check on column count */
463 errmsg("tables can have at most %d columns",
465
466 /*
467 * first check for collision with system attribute names
468 *
469 * Skip this for a view or type relation, since those don't have system
470 * attributes.
471 */
472 if (relkind != RELKIND_VIEW && relkind != RELKIND_COMPOSITE_TYPE)
473 {
474 for (i = 0; i < natts; i++)
475 {
476 Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
477
478 if (SystemAttributeByName(NameStr(attr->attname)) != NULL)
481 errmsg("column name \"%s\" conflicts with a system column name",
482 NameStr(attr->attname))));
483 }
484 }
485
486 /*
487 * next check for repeated attribute names
488 */
489 for (i = 1; i < natts; i++)
490 {
491 for (j = 0; j < i; j++)
492 {
493 if (strcmp(NameStr(TupleDescAttr(tupdesc, j)->attname),
494 NameStr(TupleDescAttr(tupdesc, i)->attname)) == 0)
497 errmsg("column name \"%s\" specified more than once",
498 NameStr(TupleDescAttr(tupdesc, j)->attname))));
499 }
500 }
501
502 /*
503 * next check the attribute types
504 */
505 for (i = 0; i < natts; i++)
506 {
508 TupleDescAttr(tupdesc, i)->atttypid,
509 TupleDescAttr(tupdesc, i)->attcollation,
510 NIL, /* assume we're creating a new rowtype */
511 flags | (TupleDescAttr(tupdesc, i)->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL ? CHKATYPE_IS_VIRTUAL : 0));
512 }
513}
void CheckAttributeType(const char *attname, Oid atttypid, Oid attcollation, List *containing_rowtypes, int flags)
Definition heap.c:544
const FormData_pg_attribute * SystemAttributeByName(const char *attname)
Definition heap.c:248
#define CHKATYPE_IS_VIRTUAL
Definition heap.h:26
#define MaxHeapAttributeNumber
int j
Definition isn.c:78
NameData attname

References attname, CheckAttributeType(), CHKATYPE_IS_VIRTUAL, ereport, errcode(), errmsg(), ERROR, fb(), i, j, MaxHeapAttributeNumber, NameStr, TupleDescData::natts, NIL, SystemAttributeByName(), and TupleDescAttr().

Referenced by addRangeTableEntryForFunction(), and heap_create_with_catalog().

◆ CheckAttributeType()

void CheckAttributeType ( const char attname,
Oid  atttypid,
Oid  attcollation,
List containing_rowtypes,
int  flags 
)

Definition at line 544 of file heap.c.

548{
551
552 /* since this function recurses, it could be driven to stack overflow */
554
556 {
557 /*
558 * We disallow pseudo-type columns, with the exception of ANYARRAY,
559 * RECORD, and RECORD[] when the caller says that those are OK.
560 *
561 * We don't need to worry about recursive containment for RECORD and
562 * RECORD[] because (a) no named composite type should be allowed to
563 * contain those, and (b) two "anonymous" record types couldn't be
564 * considered to be the same type, so infinite recursion isn't
565 * possible.
566 */
567 if (!((atttypid == ANYARRAYOID && (flags & CHKATYPE_ANYARRAY)) ||
568 (atttypid == RECORDOID && (flags & CHKATYPE_ANYRECORD)) ||
569 (atttypid == RECORDARRAYOID && (flags & CHKATYPE_ANYRECORD))))
570 {
571 if (flags & CHKATYPE_IS_PARTKEY)
574 /* translator: first %s is an integer not a name */
575 errmsg("partition key column %s has pseudo-type %s",
577 else
580 errmsg("column \"%s\" has pseudo-type %s",
582 }
583 }
584 else if (att_typtype == TYPTYPE_DOMAIN)
585 {
586 /*
587 * Prevent virtual generated columns from having a domain type. We
588 * would have to enforce domain constraints when columns underlying
589 * the generated column change. This could possibly be implemented,
590 * but it's not.
591 */
592 if (flags & CHKATYPE_IS_VIRTUAL)
595 errmsg("virtual generated column \"%s\" cannot have a domain type", attname));
596
597 /*
598 * If it's a domain, recurse to check its base type.
599 */
602 flags);
603 }
604 else if (att_typtype == TYPTYPE_COMPOSITE)
605 {
606 /*
607 * For a composite type, recurse into its attributes.
608 */
609 Relation relation;
610 TupleDesc tupdesc;
611 int i;
612
613 /*
614 * Check for self-containment. Eventually we might be able to allow
615 * this (just return without complaint, if so) but it's not clear how
616 * many other places would require anti-recursion defenses before it
617 * would be safe to allow tables to contain their own rowtype.
618 */
622 errmsg("composite type %s cannot be made a member of itself",
624
626
628
629 tupdesc = RelationGetDescr(relation);
630
631 for (i = 0; i < tupdesc->natts; i++)
632 {
633 Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
634
635 if (attr->attisdropped)
636 continue;
637 CheckAttributeType(NameStr(attr->attname),
638 attr->atttypid, attr->attcollation,
640 flags & ~CHKATYPE_IS_PARTKEY);
641 }
642
644
646 }
647 else if (att_typtype == TYPTYPE_RANGE)
648 {
649 /*
650 * If it's a range, recurse to check its subtype.
651 */
655 flags);
656 }
658 {
659 /*
660 * Must recurse into array types, too, in case they are composite.
661 */
664 flags);
665 }
666
667 /*
668 * For consistency with check_virtual_generated_security().
669 */
673 errmsg("virtual generated column \"%s\" cannot have a user-defined type", attname),
674 errdetail("Virtual generated columns that make use of user-defined types are not yet supported."));
675
676 /*
677 * This might not be strictly invalid per SQL standard, but it is pretty
678 * useless, and it cannot be dumped, so we must disallow it.
679 */
680 if (!OidIsValid(attcollation) && type_is_collatable(atttypid))
681 {
682 if (flags & CHKATYPE_IS_PARTKEY)
685 /* translator: first %s is an integer not a name */
686 errmsg("no collation was derived for partition key column %s with collatable type %s",
688 errhint("Use the COLLATE clause to set the collation explicitly.")));
689 else
692 errmsg("no collation was derived for column \"%s\" with collatable type %s",
694 errhint("Use the COLLATE clause to set the collation explicitly.")));
695 }
696}
int errhint(const char *fmt,...)
Definition elog.c:1330
char * format_type_be(Oid type_oid)
#define CHKATYPE_ANYRECORD
Definition heap.h:24
#define CHKATYPE_ANYARRAY
Definition heap.h:23
#define CHKATYPE_IS_PARTKEY
Definition heap.h:25
List * lappend_oid(List *list, Oid datum)
Definition list.c:375
List * list_delete_last(List *list)
Definition list.c:957
bool list_member_oid(const List *list, Oid datum)
Definition list.c:722
Oid get_range_subtype(Oid rangeOid)
Definition lsyscache.c:3557
Oid get_element_type(Oid typid)
Definition lsyscache.c:2909
Oid get_range_collation(Oid rangeOid)
Definition lsyscache.c:3583
bool type_is_collatable(Oid typid)
Definition lsyscache.c:3231
Oid get_typ_typrelid(Oid typid)
Definition lsyscache.c:2881
char get_typtype(Oid typid)
Definition lsyscache.c:2779
Oid getBaseType(Oid typid)
Definition lsyscache.c:2671
void relation_close(Relation relation, LOCKMODE lockmode)
Definition relation.c:205
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition relation.c:47
void check_stack_depth(void)
Definition stack_depth.c:95

References AccessShareLock, attname, check_stack_depth(), CheckAttributeType(), CHKATYPE_ANYARRAY, CHKATYPE_ANYRECORD, CHKATYPE_IS_PARTKEY, CHKATYPE_IS_VIRTUAL, ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, fb(), FirstUnpinnedObjectId, format_type_be(), get_element_type(), get_range_collation(), get_range_subtype(), get_typ_typrelid(), get_typtype(), getBaseType(), i, lappend_oid(), list_delete_last(), list_member_oid(), NameStr, TupleDescData::natts, OidIsValid, relation_close(), relation_open(), RelationGetDescr, TupleDescAttr(), and type_is_collatable().

Referenced by ATExecAddColumn(), ATPrepAlterColumnType(), CheckAttributeNamesTypes(), CheckAttributeType(), ComputePartitionAttrs(), and ConstructTupleDescriptor().

◆ contains_user_functions_checker()

static bool contains_user_functions_checker ( Oid  func_id,
void context 
)
static

Definition at line 3255 of file heap.c.

3256{
3257 return (func_id >= FirstUnpinnedObjectId);
3258}

References fb(), and FirstUnpinnedObjectId.

Referenced by check_virtual_generated_security_walker().

◆ cookConstraint()

static Node * cookConstraint ( ParseState pstate,
Node raw_constraint,
char relname 
)
static

Definition at line 3402 of file heap.c.

3405{
3406 Node *expr;
3407
3408 /*
3409 * Transform raw parsetree to executable expression.
3410 */
3412
3413 /*
3414 * Make sure it yields a boolean result.
3415 */
3416 expr = coerce_to_boolean(pstate, expr, "CHECK");
3417
3418 /*
3419 * Take care of collations.
3420 */
3421 assign_expr_collations(pstate, expr);
3422
3423 /*
3424 * Make sure no outside relations are referred to (this is probably dead
3425 * code now that add_missing_from is history).
3426 */
3427 if (list_length(pstate->p_rtable) != 1)
3428 ereport(ERROR,
3430 errmsg("only table \"%s\" can be referenced in check constraint",
3431 relname)));
3432
3433 return expr;
3434}
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)
void assign_expr_collations(ParseState *pstate, Node *expr)
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition parse_expr.c:120
@ EXPR_KIND_CHECK_CONSTRAINT
Definition parse_node.h:68
NameData relname
Definition pg_class.h:38

References assign_expr_collations(), coerce_to_boolean(), ereport, errcode(), errmsg(), ERROR, EXPR_KIND_CHECK_CONSTRAINT, fb(), list_length(), ParseState::p_rtable, relname, and transformExpr().

Referenced by AddRelationNewConstraints().

◆ cookDefault()

Node * cookDefault ( ParseState pstate,
Node raw_default,
Oid  atttypid,
int32  atttypmod,
const char attname,
char  attgenerated 
)

Definition at line 3321 of file heap.c.

3327{
3328 Node *expr;
3329
3330 Assert(raw_default != NULL);
3331
3332 /*
3333 * Transform raw parsetree to executable expression.
3334 */
3335 expr = transformExpr(pstate, raw_default, attgenerated ? EXPR_KIND_GENERATED_COLUMN : EXPR_KIND_COLUMN_DEFAULT);
3336
3337 if (attgenerated)
3338 {
3339 /* Disallow refs to other generated columns */
3340 check_nested_generated(pstate, expr);
3341
3342 /* Disallow mutable functions */
3344 ereport(ERROR,
3346 errmsg("generation expression is not immutable")));
3347
3348 /* Check security of expressions for virtual generated column */
3349 if (attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
3351 }
3352 else
3353 {
3354 /*
3355 * For a default expression, transformExpr() should have rejected
3356 * column references.
3357 */
3358 Assert(!contain_var_clause(expr));
3359 }
3360
3361 /*
3362 * Coerce the expression to the correct type and typmod, if given. This
3363 * should match the parser's processing of non-defaulted expressions ---
3364 * see transformAssignedExpr().
3365 */
3366 if (OidIsValid(atttypid))
3367 {
3368 Oid type_id = exprType(expr);
3369
3370 expr = coerce_to_target_type(pstate, expr, type_id,
3371 atttypid, atttypmod,
3374 -1);
3375 if (expr == NULL)
3376 ereport(ERROR,
3378 errmsg("column \"%s\" is of type %s"
3379 " but default expression is of type %s",
3380 attname,
3382 format_type_be(type_id)),
3383 errhint("You will need to rewrite or cast the expression.")));
3384 }
3385
3386 /*
3387 * Finally, take care of collations in the finished expression.
3388 */
3389 assign_expr_collations(pstate, expr);
3390
3391 return expr;
3392}
bool contain_mutable_functions_after_planning(Expr *expr)
Definition clauses.c:499
static void check_virtual_generated_security(ParseState *pstate, Node *node)
Definition heap.c:3303
static void check_nested_generated(ParseState *pstate, Node *node)
Definition heap.c:3223
Node * coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype, Oid targettype, int32 targettypmod, CoercionContext ccontext, CoercionForm cformat, int location)
@ EXPR_KIND_COLUMN_DEFAULT
Definition parse_node.h:70
@ EXPR_KIND_GENERATED_COLUMN
Definition parse_node.h:83
@ COERCE_IMPLICIT_CAST
Definition primnodes.h:768
@ COERCION_ASSIGNMENT
Definition primnodes.h:747
bool contain_var_clause(Node *node)
Definition var.c:406

References Assert, assign_expr_collations(), attname, check_nested_generated(), check_virtual_generated_security(), COERCE_IMPLICIT_CAST, coerce_to_target_type(), COERCION_ASSIGNMENT, contain_mutable_functions_after_planning(), contain_var_clause(), ereport, errcode(), errhint(), errmsg(), ERROR, EXPR_KIND_COLUMN_DEFAULT, EXPR_KIND_GENERATED_COLUMN, exprType(), fb(), format_type_be(), OidIsValid, and transformExpr().

Referenced by AddRelationNewConstraints(), AlterDomainDefault(), and DefineDomain().

◆ CopyStatistics()

void CopyStatistics ( Oid  fromrelid,
Oid  torelid 
)

Definition at line 3440 of file heap.c.

3441{
3442 HeapTuple tup;
3443 SysScanDesc scan;
3444 ScanKeyData key[1];
3447
3449
3450 /* Now search for stat records */
3451 ScanKeyInit(&key[0],
3455
3457 true, NULL, 1, key);
3458
3459 while (HeapTupleIsValid((tup = systable_getnext(scan))))
3460 {
3462
3463 /* make a modifiable copy */
3466
3467 /* update the copy of the tuple and insert it */
3468 statform->starelid = torelid;
3469
3470 /* fetch index information when we know we need it */
3471 if (indstate == NULL)
3473
3475
3477 }
3478
3479 systable_endscan(scan);
3480
3481 if (indstate != NULL)
3484}
void systable_endscan(SysScanDesc sysscan)
Definition genam.c:603
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition genam.c:514
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition genam.c:388
HeapTuple heap_copytuple(HeapTuple tuple)
Definition heaptuple.c:778
void heap_freetuple(HeapTuple htup)
Definition heaptuple.c:1435
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
void CatalogTupleInsertWithInfo(Relation heapRel, HeapTuple tup, CatalogIndexState indstate)
Definition indexing.c:256
FormData_pg_statistic * Form_pg_statistic
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition scankey.c:76
#define BTEqualStrategyNumber
Definition stratnum.h:31

References BTEqualStrategyNumber, CatalogCloseIndexes(), CatalogOpenIndexes(), CatalogTupleInsertWithInfo(), fb(), GETSTRUCT(), heap_copytuple(), heap_freetuple(), HeapTupleIsValid, ObjectIdGetDatum(), RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by index_concurrently_swap().

◆ DeleteAttributeTuples()

void DeleteAttributeTuples ( Oid  relid)

Definition at line 1605 of file heap.c.

1606{
1608 SysScanDesc scan;
1609 ScanKeyData key[1];
1611
1612 /* Grab an appropriate lock on the pg_attribute relation */
1614
1615 /* Use the index to scan only attributes of the target relation */
1616 ScanKeyInit(&key[0],
1619 ObjectIdGetDatum(relid));
1620
1622 NULL, 1, key);
1623
1624 /* Delete all the matching tuples */
1625 while ((atttup = systable_getnext(scan)) != NULL)
1626 CatalogTupleDelete(attrel, &atttup->t_self);
1627
1628 /* Clean up after the scan */
1629 systable_endscan(scan);
1631}
void CatalogTupleDelete(Relation heapRel, const ItemPointerData *tid)
Definition indexing.c:365

References BTEqualStrategyNumber, CatalogTupleDelete(), fb(), ObjectIdGetDatum(), RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by heap_drop_with_catalog(), and index_drop().

◆ DeleteRelationTuple()

void DeleteRelationTuple ( Oid  relid)

Definition at line 1576 of file heap.c.

1577{
1579 HeapTuple tup;
1580
1581 /* Grab an appropriate lock on the pg_class relation */
1583
1585 if (!HeapTupleIsValid(tup))
1586 elog(ERROR, "cache lookup failed for relation %u", relid);
1587
1588 /* delete the relation tuple from pg_class, and finish up */
1590
1592
1594}
#define elog(elevel,...)
Definition elog.h:226
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:264
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition syscache.c:220

References CatalogTupleDelete(), elog, ERROR, fb(), HeapTupleIsValid, ObjectIdGetDatum(), ReleaseSysCache(), RowExclusiveLock, SearchSysCache1(), table_close(), and table_open().

Referenced by heap_drop_with_catalog(), and index_drop().

◆ DeleteSystemAttributeTuples()

void DeleteSystemAttributeTuples ( Oid  relid)

Definition at line 1642 of file heap.c.

1643{
1645 SysScanDesc scan;
1646 ScanKeyData key[2];
1648
1649 /* Grab an appropriate lock on the pg_attribute relation */
1651
1652 /* Use the index to scan only system attributes of the target relation */
1653 ScanKeyInit(&key[0],
1656 ObjectIdGetDatum(relid));
1657 ScanKeyInit(&key[1],
1660 Int16GetDatum(0));
1661
1663 NULL, 2, key);
1664
1665 /* Delete all the matching tuples */
1666 while ((atttup = systable_getnext(scan)) != NULL)
1667 CatalogTupleDelete(attrel, &atttup->t_self);
1668
1669 /* Clean up after the scan */
1670 systable_endscan(scan);
1672}
static Datum Int16GetDatum(int16 X)
Definition postgres.h:182
#define BTLessEqualStrategyNumber
Definition stratnum.h:30

References BTEqualStrategyNumber, BTLessEqualStrategyNumber, CatalogTupleDelete(), fb(), Int16GetDatum(), ObjectIdGetDatum(), RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

◆ heap_create()

Relation heap_create ( const char relname,
Oid  relnamespace,
Oid  reltablespace,
Oid  relid,
RelFileNumber  relfilenumber,
Oid  accessmtd,
TupleDesc  tupDesc,
char  relkind,
char  relpersistence,
bool  shared_relation,
bool  mapped_relation,
bool  allow_system_table_mods,
TransactionId relfrozenxid,
MultiXactId relminmxid,
bool  create_storage 
)

Definition at line 285 of file heap.c.

300{
301 Relation rel;
302
303 /* The caller must have provided an OID for the relation. */
304 Assert(OidIsValid(relid));
305
306 /*
307 * Don't allow creating relations in pg_catalog directly, even though it
308 * is allowed to move user defined relations there. Semantics with search
309 * paths including pg_catalog are too confusing for now.
310 *
311 * But allow creating indexes on relations in pg_catalog even if
312 * allow_system_table_mods = off, upper layers already guarantee it's on a
313 * user defined relation, not a system one.
314 */
316 ((IsCatalogNamespace(relnamespace) && relkind != RELKIND_INDEX) ||
317 IsToastNamespace(relnamespace)) &&
321 errmsg("permission denied to create \"%s.%s\"",
322 get_namespace_name(relnamespace), relname),
323 errdetail("System catalog modifications are currently disallowed.")));
324
325 *relfrozenxid = InvalidTransactionId;
326 *relminmxid = InvalidMultiXactId;
327
328 /*
329 * Force reltablespace to zero if the relation kind does not support
330 * tablespaces. This is mainly just for cleanliness' sake.
331 */
332 if (!RELKIND_HAS_TABLESPACE(relkind))
333 reltablespace = InvalidOid;
334
335 /* Don't create storage for relkinds without physical storage. */
336 if (!RELKIND_HAS_STORAGE(relkind))
337 create_storage = false;
338 else
339 {
340 /*
341 * If relfilenumber is unspecified by the caller then create storage
342 * with oid same as relid.
343 */
344 if (!RelFileNumberIsValid(relfilenumber))
345 relfilenumber = relid;
346 }
347
348 /*
349 * Never allow a pg_class entry to explicitly specify the database's
350 * default tablespace in reltablespace; force it to zero instead. This
351 * ensures that if the database is cloned with a different default
352 * tablespace, the pg_class entry will still match where CREATE DATABASE
353 * will put the physically copied relation.
354 *
355 * Yes, this is a bit of a hack.
356 */
357 if (reltablespace == MyDatabaseTableSpace)
358 reltablespace = InvalidOid;
359
360 /*
361 * build the relcache entry.
362 */
364 relnamespace,
365 tupDesc,
366 relid,
367 accessmtd,
368 relfilenumber,
369 reltablespace,
372 relpersistence,
373 relkind);
374
375 /*
376 * Have the storage manager create the relation's disk file, if needed.
377 *
378 * For tables, the AM callback creates both the main and the init fork.
379 * For others, only the main fork is created; the other forks will be
380 * created on demand.
381 */
382 if (create_storage)
383 {
384 if (RELKIND_HAS_TABLE_AM(rel->rd_rel->relkind))
386 relpersistence,
387 relfrozenxid, relminmxid);
388 else if (RELKIND_HAS_STORAGE(rel->rd_rel->relkind))
389 RelationCreateStorage(rel->rd_locator, relpersistence, true);
390 else
391 Assert(false);
392 }
393
394 /*
395 * If a tablespace is specified, removal of that tablespace is normally
396 * protected by the existence of a physical file; but for relations with
397 * no files, add a pg_shdepend entry to account for that.
398 */
399 if (!create_storage && reltablespace != InvalidOid)
401 reltablespace);
402
403 /* ensure that stats are dropped if transaction aborts */
405
406 return rel;
407}
bool IsToastNamespace(Oid namespaceId)
Definition catalog.c:261
bool IsCatalogNamespace(Oid namespaceId)
Definition catalog.c:243
Oid MyDatabaseTableSpace
Definition globals.c:96
char * get_namespace_name(Oid nspid)
Definition lsyscache.c:3516
#define IsNormalProcessingMode()
Definition miscadmin.h:479
#define InvalidMultiXactId
Definition multixact.h:25
void recordDependencyOnTablespace(Oid classId, Oid objectId, Oid tablespace)
void pgstat_create_relation(Relation rel)
Relation RelationBuildLocalRelation(const char *relname, Oid relnamespace, TupleDesc tupDesc, Oid relid, Oid accessmtd, RelFileNumber relfilenumber, Oid reltablespace, bool shared_relation, bool mapped_relation, char relpersistence, char relkind)
Definition relcache.c:3510
#define RelFileNumberIsValid(relnumber)
Definition relpath.h:27
SMgrRelation RelationCreateStorage(RelFileLocator rlocator, char relpersistence, bool register_delete)
Definition storage.c:122
RelFileLocator rd_locator
Definition rel.h:57
Form_pg_class rd_rel
Definition rel.h:111
static void table_relation_set_new_filelocator(Relation rel, const RelFileLocator *newrlocator, char persistence, TransactionId *freezeXid, MultiXactId *minmulti)
Definition tableam.h:1612
#define InvalidTransactionId
Definition transam.h:31

References Assert, ereport, errcode(), errdetail(), errmsg(), ERROR, fb(), get_namespace_name(), InvalidMultiXactId, InvalidOid, InvalidTransactionId, IsCatalogNamespace(), IsNormalProcessingMode, IsToastNamespace(), MyDatabaseTableSpace, OidIsValid, pgstat_create_relation(), RelationData::rd_locator, RelationData::rd_rel, recordDependencyOnTablespace(), RelationBuildLocalRelation(), RelationCreateStorage(), RelFileNumberIsValid, relname, and table_relation_set_new_filelocator().

Referenced by heap_create_with_catalog(), and index_create().

◆ heap_create_with_catalog()

Oid heap_create_with_catalog ( const char relname,
Oid  relnamespace,
Oid  reltablespace,
Oid  relid,
Oid  reltypeid,
Oid  reloftypeid,
Oid  ownerid,
Oid  accessmtd,
TupleDesc  tupdesc,
List cooked_constraints,
char  relkind,
char  relpersistence,
bool  shared_relation,
bool  mapped_relation,
OnCommitAction  oncommit,
Datum  reloptions,
bool  use_user_acl,
bool  allow_system_table_mods,
bool  is_internal,
Oid  relrewrite,
ObjectAddress typaddress 
)

Definition at line 1122 of file heap.c.

1143{
1146 Acl *relacl;
1150
1151 /* By default set to InvalidOid unless overridden by binary-upgrade */
1152 RelFileNumber relfilenumber = InvalidRelFileNumber;
1153 TransactionId relfrozenxid;
1154 MultiXactId relminmxid;
1155
1157
1158 /*
1159 * sanity checks
1160 */
1162
1163 /*
1164 * Validate proposed tupdesc for the desired relkind. If
1165 * allow_system_table_mods is on, allow ANYARRAY to be used; this is a
1166 * hack to allow creating pg_statistic and cloning it during VACUUM FULL.
1167 */
1168 CheckAttributeNamesTypes(tupdesc, relkind,
1170
1171 /*
1172 * This would fail later on anyway, if the relation already exists. But
1173 * by catching it here we can emit a nicer error message.
1174 */
1175 existing_relid = get_relname_relid(relname, relnamespace);
1177 ereport(ERROR,
1179 errmsg("relation \"%s\" already exists", relname)));
1180
1181 /*
1182 * Since we are going to create a rowtype as well, also check for
1183 * collision with an existing type name. If there is one and it's an
1184 * autogenerated array, we can rename it out of the way; otherwise we can
1185 * at least give a good error message.
1186 */
1189 ObjectIdGetDatum(relnamespace));
1191 {
1192 if (!moveArrayTypeName(old_type_oid, relname, relnamespace))
1193 ereport(ERROR,
1195 errmsg("type \"%s\" already exists", relname),
1196 errhint("A relation has an associated type of the same name, "
1197 "so you must use a name that doesn't conflict "
1198 "with any existing type.")));
1199 }
1200
1201 /*
1202 * Shared relations must be in pg_global (last-ditch check)
1203 */
1204 if (shared_relation && reltablespace != GLOBALTABLESPACE_OID)
1205 elog(ERROR, "shared relations must be placed in pg_global tablespace");
1206
1207 /*
1208 * Allocate an OID for the relation, unless we were told what to use.
1209 *
1210 * The OID will be the relfilenumber as well, so make sure it doesn't
1211 * collide with either pg_class OIDs or existing physical files.
1212 */
1213 if (!OidIsValid(relid))
1214 {
1215 /* Use binary-upgrade override for pg_class.oid and relfilenumber */
1216 if (IsBinaryUpgrade)
1217 {
1218 /*
1219 * Indexes are not supported here; they use
1220 * binary_upgrade_next_index_pg_class_oid.
1221 */
1222 Assert(relkind != RELKIND_INDEX);
1224
1225 if (relkind == RELKIND_TOASTVALUE)
1226 {
1227 /* There might be no TOAST table, so we have to test for it. */
1229 {
1232
1234 ereport(ERROR,
1236 errmsg("toast relfilenumber value not set when in binary upgrade mode")));
1237
1240 }
1241 }
1242 else
1243 {
1245 ereport(ERROR,
1247 errmsg("pg_class heap OID value not set when in binary upgrade mode")));
1248
1251
1252 if (RELKIND_HAS_STORAGE(relkind))
1253 {
1255 ereport(ERROR,
1257 errmsg("relfilenumber value not set when in binary upgrade mode")));
1258
1261 }
1262 }
1263 }
1264
1265 if (!OidIsValid(relid))
1266 relid = GetNewRelFileNumber(reltablespace, pg_class_desc,
1267 relpersistence);
1268 }
1269
1270 /*
1271 * Other sessions' catalog scans can't find this until we commit. Hence,
1272 * it doesn't hurt to hold AccessExclusiveLock. Do it here so callers
1273 * can't accidentally vary in their lock mode or acquisition timing.
1274 */
1276
1277 /*
1278 * Determine the relation's initial permissions.
1279 */
1280 if (use_user_acl)
1281 {
1282 switch (relkind)
1283 {
1284 case RELKIND_RELATION:
1285 case RELKIND_VIEW:
1286 case RELKIND_MATVIEW:
1290 relnamespace);
1291 break;
1292 case RELKIND_SEQUENCE:
1294 relnamespace);
1295 break;
1296 default:
1297 relacl = NULL;
1298 break;
1299 }
1300 }
1301 else
1302 relacl = NULL;
1303
1304 /*
1305 * Create the relcache entry (mostly dummy at this point) and the physical
1306 * disk file. (If we fail further down, it's the smgr's responsibility to
1307 * remove the disk file again.)
1308 *
1309 * NB: Note that passing create_storage = true is correct even for binary
1310 * upgrade. The storage we create here will be replaced later, but we
1311 * need to have something on disk in the meanwhile.
1312 */
1314 relnamespace,
1315 reltablespace,
1316 relid,
1317 relfilenumber,
1318 accessmtd,
1319 tupdesc,
1320 relkind,
1321 relpersistence,
1325 &relfrozenxid,
1326 &relminmxid,
1327 true);
1328
1330
1331 new_rel_desc->rd_rel->relrewrite = relrewrite;
1332
1333 /*
1334 * Decide whether to create a pg_type entry for the relation's rowtype.
1335 * These types are made except where the use of a relation as such is an
1336 * implementation detail: toast tables, sequences and indexes.
1337 */
1338 if (!(relkind == RELKIND_SEQUENCE ||
1339 relkind == RELKIND_TOASTVALUE ||
1340 relkind == RELKIND_INDEX ||
1341 relkind == RELKIND_PARTITIONED_INDEX))
1342 {
1345 char *relarrayname;
1346
1347 /*
1348 * We'll make an array over the composite type, too. For largely
1349 * historical reasons, the array type's OID is assigned first.
1350 */
1352
1353 /*
1354 * Make the pg_type entry for the composite type. The OID of the
1355 * composite type can be preselected by the caller, but if reltypeid
1356 * is InvalidOid, we'll generate a new OID for it.
1357 *
1358 * NOTE: we could get a unique-index failure here, in case someone
1359 * else is creating the same type name in parallel but hadn't
1360 * committed yet when we checked for a duplicate name above.
1361 */
1363 relnamespace,
1364 relid,
1365 relkind,
1366 ownerid,
1367 reltypeid,
1369 new_type_oid = new_type_addr.objectId;
1370 if (typaddress)
1372
1373 /* Now create the array type. */
1374 relarrayname = makeArrayTypeName(relname, relnamespace);
1375
1376 TypeCreate(new_array_oid, /* force the type's OID to this */
1377 relarrayname, /* Array type name */
1378 relnamespace, /* Same namespace as parent */
1379 InvalidOid, /* Not composite, no relationOid */
1380 0, /* relkind, also N/A here */
1381 ownerid, /* owner's ID */
1382 -1, /* Internal size (varlena) */
1383 TYPTYPE_BASE, /* Not composite - typelem is */
1384 TYPCATEGORY_ARRAY, /* type-category (array) */
1385 false, /* array types are never preferred */
1386 DEFAULT_TYPDELIM, /* default array delimiter */
1387 F_ARRAY_IN, /* array input proc */
1388 F_ARRAY_OUT, /* array output proc */
1389 F_ARRAY_RECV, /* array recv (bin) proc */
1390 F_ARRAY_SEND, /* array send (bin) proc */
1391 InvalidOid, /* typmodin procedure - none */
1392 InvalidOid, /* typmodout procedure - none */
1393 F_ARRAY_TYPANALYZE, /* array analyze procedure */
1394 F_ARRAY_SUBSCRIPT_HANDLER, /* array subscript procedure */
1395 new_type_oid, /* array element type - the rowtype */
1396 true, /* yes, this is an array type */
1397 InvalidOid, /* this has no array type */
1398 InvalidOid, /* domain base type - irrelevant */
1399 NULL, /* default value - none */
1400 NULL, /* default binary representation */
1401 false, /* passed by reference */
1402 TYPALIGN_DOUBLE, /* alignment - must be the largest! */
1403 TYPSTORAGE_EXTENDED, /* fully TOASTable */
1404 -1, /* typmod */
1405 0, /* array dimensions for typBaseType */
1406 false, /* Type NOT NULL */
1407 InvalidOid); /* rowtypes never have a collation */
1408
1410 }
1411 else
1412 {
1413 /* Caller should not be expecting a type to be created. */
1416
1418 }
1419
1420 /*
1421 * now create an entry in pg_class for the relation.
1422 *
1423 * NOTE: we could get a unique-index failure here, in case someone else is
1424 * creating the same relation name in parallel but hadn't committed yet
1425 * when we checked for a duplicate name above.
1426 */
1429 relid,
1432 ownerid,
1433 relkind,
1434 relfrozenxid,
1435 relminmxid,
1437 reloptions);
1438
1439 /*
1440 * now add tuples to pg_attribute for the attributes in our new relation.
1441 */
1442 AddNewAttributeTuples(relid, new_rel_desc->rd_att, relkind);
1443
1444 /*
1445 * Make a dependency link to force the relation to be deleted if its
1446 * namespace is. Also make a dependency link to its owner, as well as
1447 * dependencies for any roles mentioned in the default ACL.
1448 *
1449 * For composite types, these dependencies are tracked for the pg_type
1450 * entry, so we needn't record them here. Likewise, TOAST tables don't
1451 * need a namespace dependency (they live in a pinned namespace) nor an
1452 * owner dependency (they depend indirectly through the parent table), nor
1453 * should they have any ACL entries. The same applies for extension
1454 * dependencies.
1455 *
1456 * Also, skip this in bootstrap mode, since we don't make dependencies
1457 * while bootstrapping.
1458 */
1459 if (relkind != RELKIND_COMPOSITE_TYPE &&
1460 relkind != RELKIND_TOASTVALUE &&
1462 {
1464 referenced;
1465 ObjectAddresses *addrs;
1466
1468
1470
1472
1474
1475 addrs = new_object_addresses();
1476
1479
1480 if (reloftypeid)
1481 {
1484 }
1485
1486 /*
1487 * Make a dependency link to force the relation to be deleted if its
1488 * access method is.
1489 *
1490 * No need to add an explicit dependency for the toast table, as the
1491 * main table depends on it. Partitioned tables may not have an
1492 * access method set.
1493 */
1494 if ((RELKIND_HAS_TABLE_AM(relkind) && relkind != RELKIND_TOASTVALUE) ||
1496 {
1499 }
1500
1502 free_object_addresses(addrs);
1503 }
1504
1505 /* Post creation hook for new relation */
1506 InvokeObjectPostCreateHookArg(RelationRelationId, relid, 0, is_internal);
1507
1508 /*
1509 * Store any supplied CHECK constraints and defaults.
1510 *
1511 * NB: this may do a CommandCounterIncrement and rebuild the relcache
1512 * entry, so the relation must be valid and self-consistent at this point.
1513 * In particular, there are not yet constraints and defaults anywhere.
1514 */
1516
1517 /*
1518 * If there's a special on-commit action, remember it
1519 */
1520 if (oncommit != ONCOMMIT_NOOP)
1521 register_on_commit_action(relid, oncommit);
1522
1523 /*
1524 * ok, the relation has been cataloged, so close our relations and return
1525 * the OID of the newly created relation.
1526 */
1527 table_close(new_rel_desc, NoLock); /* do not unlock till end of xact */
1529
1530 return relid;
1531}
void recordDependencyOnNewAcl(Oid classId, Oid objectId, int32 objsubId, Oid ownerId, Acl *acl)
Definition aclchk.c:4327
Acl * get_user_default_acl(ObjectType objtype, Oid ownerId, Oid nsp_oid)
Definition aclchk.c:4247
TransactionId MultiXactId
Definition c.h:676
uint32 TransactionId
Definition c.h:666
RelFileNumber GetNewRelFileNumber(Oid reltablespace, Relation pg_class, char relpersistence)
Definition catalog.c:557
void record_object_address_dependencies(const ObjectAddress *depender, ObjectAddresses *referenced, DependencyType behavior)
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
ObjectAddresses * new_object_addresses(void)
void free_object_addresses(ObjectAddresses *addrs)
bool IsBinaryUpgrade
Definition globals.c:121
static void StoreConstraints(Relation rel, List *cooked_constraints, bool is_internal)
Definition heap.c:2310
static void AddNewAttributeTuples(Oid new_rel_oid, TupleDesc tupdesc, char relkind)
Definition heap.c:834
RelFileNumber binary_upgrade_next_heap_pg_class_relfilenumber
Definition heap.c:83
static void AddNewRelationTuple(Relation pg_class_desc, Relation new_rel_desc, Oid new_rel_oid, Oid new_type_oid, Oid reloftype, Oid relowner, char relkind, TransactionId relfrozenxid, TransactionId relminmxid, Datum relacl, Datum reloptions)
Definition heap.c:984
RelFileNumber binary_upgrade_next_toast_pg_class_relfilenumber
Definition heap.c:84
static ObjectAddress AddNewRelationType(const char *typeName, Oid typeNamespace, Oid new_rel_oid, char new_rel_kind, Oid ownerid, Oid new_row_type, Oid new_array_type)
Definition heap.c:1043
void CheckAttributeNamesTypes(TupleDesc tupdesc, char relkind, int flags)
Definition heap.c:452
Oid binary_upgrade_next_toast_pg_class_oid
Definition heap.c:82
Oid binary_upgrade_next_heap_pg_class_oid
Definition heap.c:81
Relation heap_create(const char *relname, Oid relnamespace, Oid reltablespace, Oid relid, RelFileNumber relfilenumber, Oid accessmtd, TupleDesc tupDesc, char relkind, char relpersistence, bool shared_relation, bool mapped_relation, bool allow_system_table_mods, TransactionId *relfrozenxid, MultiXactId *relminmxid, bool create_storage)
Definition heap.c:285
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition lmgr.c:107
#define NoLock
Definition lockdefs.h:34
#define AccessExclusiveLock
Definition lockdefs.h:43
Oid get_relname_relid(const char *relname, Oid relnamespace)
Definition lsyscache.c:2035
void pfree(void *pointer)
Definition mcxt.c:1616
#define IsBootstrapProcessingMode()
Definition miscadmin.h:477
#define InvokeObjectPostCreateHookArg(classId, objectId, subId, is_internal)
@ OBJECT_SEQUENCE
@ OBJECT_TABLE
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition pg_depend.c:193
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
bool moveArrayTypeName(Oid typeOid, const char *typeName, Oid typeNamespace)
Definition pg_type.c:903
char * makeArrayTypeName(const char *typeName, Oid typeNamespace)
Definition pg_type.c:838
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
static Datum CStringGetDatum(const char *X)
Definition postgres.h:380
@ ONCOMMIT_NOOP
Definition primnodes.h:58
Oid RelFileNumber
Definition relpath.h:25
#define InvalidRelFileNumber
Definition relpath.h:26
#define GetSysCacheOid2(cacheId, oidcol, key1, key2)
Definition syscache.h:111
void register_on_commit_action(Oid relid, OnCommitAction action)
Oid AssignTypeArrayOid(void)
Definition typecmds.c:2473

References AccessExclusiveLock, add_exact_object_address(), AddNewAttributeTuples(), AddNewRelationTuple(), AddNewRelationType(), Assert, AssignTypeArrayOid(), binary_upgrade_next_heap_pg_class_oid, binary_upgrade_next_heap_pg_class_relfilenumber, binary_upgrade_next_toast_pg_class_oid, binary_upgrade_next_toast_pg_class_relfilenumber, CheckAttributeNamesTypes(), CHKATYPE_ANYARRAY, CStringGetDatum(), DEFAULT_TYPDELIM, DEPENDENCY_NORMAL, elog, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errhint(), errmsg(), ERROR, fb(), free_object_addresses(), get_relname_relid(), get_user_default_acl(), GetNewRelFileNumber(), GetSysCacheOid2, heap_create(), InvalidOid, InvalidRelFileNumber, InvokeObjectPostCreateHookArg, IsBinaryUpgrade, IsBootstrapProcessingMode, IsNormalProcessingMode, LockRelationOid(), makeArrayTypeName(), moveArrayTypeName(), new_object_addresses(), NoLock, OBJECT_SEQUENCE, OBJECT_TABLE, ObjectAddressSet, ObjectIdGetDatum(), OidIsValid, ONCOMMIT_NOOP, pfree(), PointerGetDatum(), record_object_address_dependencies(), recordDependencyOnCurrentExtension(), recordDependencyOnNewAcl(), recordDependencyOnOwner(), register_on_commit_action(), RelationGetRelid, RelFileNumberIsValid, relname, RowExclusiveLock, StoreConstraints(), table_close(), table_open(), and TypeCreate().

Referenced by create_toast_table(), createPartitionTable(), DefineRelation(), and make_new_heap().

◆ heap_drop_with_catalog()

void heap_drop_with_catalog ( Oid  relid)

Definition at line 1784 of file heap.c.

1785{
1786 Relation rel;
1787 HeapTuple tuple;
1790
1791 /*
1792 * To drop a partition safely, we must grab exclusive lock on its parent,
1793 * because another backend might be about to execute a query on the parent
1794 * table. If it relies on previously cached partition descriptor, then it
1795 * could attempt to access the just-dropped relation as its partition. We
1796 * must therefore take a table lock strong enough to prevent all queries
1797 * on the table from proceeding until we commit and send out a
1798 * shared-cache-inval notice that will make them update their partition
1799 * descriptors.
1800 */
1801 tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
1802 if (!HeapTupleIsValid(tuple))
1803 elog(ERROR, "cache lookup failed for relation %u", relid);
1804 if (((Form_pg_class) GETSTRUCT(tuple))->relispartition)
1805 {
1806 /*
1807 * We have to lock the parent if the partition is being detached,
1808 * because it's possible that some query still has a partition
1809 * descriptor that includes this partition.
1810 */
1811 parentOid = get_partition_parent(relid, true);
1813
1814 /*
1815 * If this is not the default partition, dropping it will change the
1816 * default partition's partition constraint, so we must lock it.
1817 */
1819 if (OidIsValid(defaultPartOid) && relid != defaultPartOid)
1821 }
1822
1823 ReleaseSysCache(tuple);
1824
1825 /*
1826 * Open and lock the relation.
1827 */
1828 rel = relation_open(relid, AccessExclusiveLock);
1829
1830 /*
1831 * There can no longer be anyone *else* touching the relation, but we
1832 * might still have open queries or cursors, or pending trigger events, in
1833 * our own session.
1834 */
1835 CheckTableNotInUse(rel, "DROP TABLE");
1836
1837 /*
1838 * This effectively deletes all rows in the table, and may be done in a
1839 * serializable transaction. In that case we must record a rw-conflict in
1840 * to this transaction from each transaction holding a predicate lock on
1841 * the table.
1842 */
1844
1845 /*
1846 * Delete pg_foreign_table tuple first.
1847 */
1848 if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
1849 {
1852
1854
1857 elog(ERROR, "cache lookup failed for foreign table %u", relid);
1858
1859 CatalogTupleDelete(ftrel, &fttuple->t_self);
1860
1863 }
1864
1865 /*
1866 * If a partitioned table, delete the pg_partitioned_table tuple.
1867 */
1868 if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1870
1871 /*
1872 * If the relation being dropped is the default partition itself,
1873 * invalidate its entry in pg_partitioned_table.
1874 */
1875 if (relid == defaultPartOid)
1877
1878 /*
1879 * Schedule unlinking of the relation's physical files at commit.
1880 */
1881 if (RELKIND_HAS_STORAGE(rel->rd_rel->relkind))
1883
1884 /* ensure that stats are dropped if transaction commits */
1886
1887 /*
1888 * Close relcache entry, but *keep* AccessExclusiveLock on the relation
1889 * until transaction commit. This ensures no one else will try to do
1890 * something with the doomed relation.
1891 */
1892 relation_close(rel, NoLock);
1893
1894 /*
1895 * Remove any associated relation synchronization states.
1896 */
1898
1899 /*
1900 * Forget any ON COMMIT action for the rel
1901 */
1903
1904 /*
1905 * Flush the relation from the relcache. We want to do this before
1906 * starting to remove catalog entries, just to be certain that no relcache
1907 * entry rebuild will happen partway through. (That should not really
1908 * matter, since we don't do CommandCounterIncrement here, but let's be
1909 * safe.)
1910 */
1912
1913 /*
1914 * remove inheritance information
1915 */
1917
1918 /*
1919 * delete statistics
1920 */
1921 RemoveStatistics(relid, 0);
1922
1923 /*
1924 * delete attribute tuples
1925 */
1926 DeleteAttributeTuples(relid);
1927
1928 /*
1929 * delete relation tuple
1930 */
1931 DeleteRelationTuple(relid);
1932
1933 if (OidIsValid(parentOid))
1934 {
1935 /*
1936 * If this is not the default partition, the partition constraint of
1937 * the default partition has changed to include the portion of the key
1938 * space previously covered by the dropped partition.
1939 */
1940 if (OidIsValid(defaultPartOid) && relid != defaultPartOid)
1942
1943 /*
1944 * Invalidate the parent's relcache so that the partition is no longer
1945 * included in its partition descriptor.
1946 */
1948 /* keep the lock */
1949 }
1950}
void DeleteRelationTuple(Oid relid)
Definition heap.c:1576
void DeleteAttributeTuples(Oid relid)
Definition heap.c:1605
void RemoveStatistics(Oid relid, AttrNumber attnum)
Definition heap.c:3493
static void RelationRemoveInheritance(Oid relid)
Definition heap.c:1543
void RemovePartitionKeyByRelId(Oid relid)
Definition heap.c:4020
void CacheInvalidateRelcacheByRelid(Oid relid)
Definition inval.c:1691
void update_default_partition_oid(Oid parentId, Oid defaultPartId)
Definition partition.c:340
Oid get_default_partition_oid(Oid parentId)
Definition partition.c:315
Oid get_partition_parent(Oid relid, bool even_if_detached)
Definition partition.c:53
void RemoveSubscriptionRel(Oid subid, Oid relid)
void pgstat_drop_relation(Relation rel)
void CheckTableForSerializableConflictIn(Relation relation)
Definition predicate.c:4417
void RelationForgetRelation(Oid rid)
Definition relcache.c:2888
void RelationDropStorage(Relation rel)
Definition storage.c:207
void CheckTableNotInUse(Relation rel, const char *stmt)
Definition tablecmds.c:4414
void remove_on_commit_action(Oid relid)

References AccessExclusiveLock, CacheInvalidateRelcacheByRelid(), CatalogTupleDelete(), CheckTableForSerializableConflictIn(), CheckTableNotInUse(), DeleteAttributeTuples(), DeleteRelationTuple(), elog, ERROR, fb(), get_default_partition_oid(), get_partition_parent(), GETSTRUCT(), HeapTupleIsValid, InvalidOid, LockRelationOid(), NoLock, ObjectIdGetDatum(), OidIsValid, pgstat_drop_relation(), RelationData::rd_rel, relation_close(), relation_open(), RelationDropStorage(), RelationForgetRelation(), RelationRemoveInheritance(), ReleaseSysCache(), remove_on_commit_action(), RemovePartitionKeyByRelId(), RemoveStatistics(), RemoveSubscriptionRel(), RowExclusiveLock, SearchSysCache1(), table_close(), table_open(), and update_default_partition_oid().

Referenced by doDeletion().

◆ heap_truncate()

void heap_truncate ( List relids)

Definition at line 3588 of file heap.c.

3589{
3590 List *relations = NIL;
3591 ListCell *cell;
3592
3593 /* Open relations for processing, and grab exclusive access on each */
3594 foreach(cell, relids)
3595 {
3596 Oid rid = lfirst_oid(cell);
3597 Relation rel;
3598
3600 relations = lappend(relations, rel);
3601 }
3602
3603 /* Don't allow truncate on tables that are referenced by foreign keys */
3604 heap_truncate_check_FKs(relations, true);
3605
3606 /* OK to do it */
3607 foreach(cell, relations)
3608 {
3609 Relation rel = lfirst(cell);
3610
3611 /* Truncate the relation */
3613
3614 /* Close the relation, but keep exclusive lock on it until commit */
3615 table_close(rel, NoLock);
3616 }
3617}
void heap_truncate_check_FKs(List *relations, bool tempTables)
Definition heap.c:3673
void heap_truncate_one_rel(Relation rel)
Definition heap.c:3629
#define lfirst(lc)
Definition pg_list.h:172
#define lfirst_oid(lc)
Definition pg_list.h:174

References AccessExclusiveLock, fb(), heap_truncate_check_FKs(), heap_truncate_one_rel(), lappend(), lfirst, lfirst_oid, NIL, NoLock, table_close(), and table_open().

Referenced by PreCommit_on_commit_actions().

◆ heap_truncate_check_FKs()

void heap_truncate_check_FKs ( List relations,
bool  tempTables 
)

Definition at line 3673 of file heap.c.

3674{
3675 List *oids = NIL;
3677 ListCell *cell;
3678
3679 /*
3680 * Build a list of OIDs of the interesting relations.
3681 *
3682 * If a relation has no triggers, then it can neither have FKs nor be
3683 * referenced by a FK from another table, so we can ignore it. For
3684 * partitioned tables, FKs have no triggers, so we must include them
3685 * anyway.
3686 */
3687 foreach(cell, relations)
3688 {
3689 Relation rel = lfirst(cell);
3690
3691 if (rel->rd_rel->relhastriggers ||
3692 rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
3693 oids = lappend_oid(oids, RelationGetRelid(rel));
3694 }
3695
3696 /*
3697 * Fast path: if no relation has triggers, none has FKs either.
3698 */
3699 if (oids == NIL)
3700 return;
3701
3702 /*
3703 * Otherwise, must scan pg_constraint. We make one pass with all the
3704 * relations considered; if this finds nothing, then all is well.
3705 */
3707 if (dependents == NIL)
3708 return;
3709
3710 /*
3711 * Otherwise we repeat the scan once per relation to identify a particular
3712 * pair of relations to complain about. This is pretty slow, but
3713 * performance shouldn't matter much in a failure path. The reason for
3714 * doing things this way is to ensure that the message produced is not
3715 * dependent on chance row locations within pg_constraint.
3716 */
3717 foreach(cell, oids)
3718 {
3719 Oid relid = lfirst_oid(cell);
3720 ListCell *cell2;
3721
3723
3724 foreach(cell2, dependents)
3725 {
3727
3728 if (!list_member_oid(oids, relid2))
3729 {
3730 char *relname = get_rel_name(relid);
3731 char *relname2 = get_rel_name(relid2);
3732
3733 if (tempTables)
3734 ereport(ERROR,
3736 errmsg("unsupported ON COMMIT and foreign key combination"),
3737 errdetail("Table \"%s\" references \"%s\", but they do not have the same ON COMMIT setting.",
3738 relname2, relname)));
3739 else
3740 ereport(ERROR,
3742 errmsg("cannot truncate a table referenced in a foreign key constraint"),
3743 errdetail("Table \"%s\" references \"%s\".",
3744 relname2, relname),
3745 errhint("Truncate table \"%s\" at the same time, "
3746 "or use TRUNCATE ... CASCADE.",
3747 relname2)));
3748 }
3749 }
3750 }
3751}
List * heap_truncate_find_FKs(List *relationIds)
Definition heap.c:3768
char * get_rel_name(Oid relid)
Definition lsyscache.c:2078
#define list_make1_oid(x1)
Definition pg_list.h:242

References ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, fb(), get_rel_name(), heap_truncate_find_FKs(), lappend_oid(), lfirst, lfirst_oid, list_make1_oid, list_member_oid(), NIL, RelationData::rd_rel, RelationGetRelid, and relname.

Referenced by ExecuteTruncateGuts(), and heap_truncate().

◆ heap_truncate_find_FKs()

List * heap_truncate_find_FKs ( List relationIds)

Definition at line 3768 of file heap.c.

3769{
3770 List *result = NIL;
3771 List *oids;
3773 ListCell *cell;
3777 HeapTuple tuple;
3778 bool restart;
3779
3780 oids = list_copy(relationIds);
3781
3782 /*
3783 * Must scan pg_constraint. Right now, it is a seqscan because there is
3784 * no available index on confrelid.
3785 */
3787
3788restart:
3789 restart = false;
3790 parent_cons = NIL;
3791
3793 NULL, 0, NULL);
3794
3796 {
3798
3799 /* Not a foreign key */
3800 if (con->contype != CONSTRAINT_FOREIGN)
3801 continue;
3802
3803 /* Not referencing one of our list of tables */
3804 if (!list_member_oid(oids, con->confrelid))
3805 continue;
3806
3807 /*
3808 * If this constraint has a parent constraint which we have not seen
3809 * yet, keep track of it for the second loop, below. Tracking parent
3810 * constraints allows us to climb up to the top-level constraint and
3811 * look for all possible relations referencing the partitioned table.
3812 */
3813 if (OidIsValid(con->conparentid) &&
3814 !list_member_oid(parent_cons, con->conparentid))
3815 parent_cons = lappend_oid(parent_cons, con->conparentid);
3816
3817 /*
3818 * Add referencer to result, unless present in input list. (Don't
3819 * worry about dupes: we'll fix that below).
3820 */
3821 if (!list_member_oid(relationIds, con->conrelid))
3822 result = lappend_oid(result, con->conrelid);
3823 }
3824
3826
3827 /*
3828 * Process each parent constraint we found to add the list of referenced
3829 * relations by them to the oids list. If we do add any new such
3830 * relations, redo the first loop above. Also, if we see that the parent
3831 * constraint in turn has a parent, add that so that we process all
3832 * relations in a single additional pass.
3833 */
3834 foreach(cell, parent_cons)
3835 {
3836 Oid parent = lfirst_oid(cell);
3837
3838 ScanKeyInit(&key,
3841 ObjectIdGetDatum(parent));
3842
3844 true, NULL, 1, &key);
3845
3846 tuple = systable_getnext(fkeyScan);
3847 if (HeapTupleIsValid(tuple))
3848 {
3850
3851 /*
3852 * pg_constraint rows always appear for partitioned hierarchies
3853 * this way: on the each side of the constraint, one row appears
3854 * for each partition that points to the top-most table on the
3855 * other side.
3856 *
3857 * Because of this arrangement, we can correctly catch all
3858 * relevant relations by adding to 'parent_cons' all rows with
3859 * valid conparentid, and to the 'oids' list all rows with a zero
3860 * conparentid. If any oids are added to 'oids', redo the first
3861 * loop above by setting 'restart'.
3862 */
3863 if (OidIsValid(con->conparentid))
3865 con->conparentid);
3866 else if (!list_member_oid(oids, con->confrelid))
3867 {
3868 oids = lappend_oid(oids, con->confrelid);
3869 restart = true;
3870 }
3871 }
3872
3874 }
3875
3877 if (restart)
3878 goto restart;
3879
3881 list_free(oids);
3882
3883 /* Now sort and de-duplicate the result list */
3884 list_sort(result, list_oid_cmp);
3885 list_deduplicate_oid(result);
3886
3887 return result;
3888}
void list_sort(List *list, list_sort_comparator cmp)
Definition list.c:1674
List * list_copy(const List *oldlist)
Definition list.c:1573
List * list_append_unique_oid(List *list, Oid datum)
Definition list.c:1380
void list_deduplicate_oid(List *list)
Definition list.c:1495
int list_oid_cmp(const ListCell *p1, const ListCell *p2)
Definition list.c:1703
void list_free(List *list)
Definition list.c:1546
FormData_pg_constraint * Form_pg_constraint

References AccessShareLock, BTEqualStrategyNumber, fb(), GETSTRUCT(), HeapTupleIsValid, InvalidOid, lappend_oid(), lfirst_oid, list_append_unique_oid(), list_copy(), list_deduplicate_oid(), list_free(), list_member_oid(), list_oid_cmp(), list_sort(), NIL, ObjectIdGetDatum(), OidIsValid, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by ExecuteTruncateGuts(), and heap_truncate_check_FKs().

◆ heap_truncate_one_rel()

void heap_truncate_one_rel ( Relation  rel)

Definition at line 3629 of file heap.c.

3630{
3632
3633 /*
3634 * Truncate the relation. Partitioned tables have no storage, so there is
3635 * nothing to do for them here.
3636 */
3637 if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
3638 return;
3639
3640 /* Truncate the underlying relation */
3642
3643 /* If the relation has indexes, truncate the indexes too */
3645
3646 /* If there is a toast table, truncate that too */
3647 toastrelid = rel->rd_rel->reltoastrelid;
3649 {
3651
3654 /* keep the lock... */
3656 }
3657}
static void RelationTruncateIndexes(Relation heapRelation)
Definition heap.c:3540
static void table_relation_nontransactional_truncate(Relation rel)
Definition tableam.h:1630

References AccessExclusiveLock, fb(), NoLock, OidIsValid, RelationData::rd_rel, RelationTruncateIndexes(), table_close(), table_open(), and table_relation_nontransactional_truncate().

Referenced by ExecuteTruncateGuts(), and heap_truncate().

◆ InsertPgAttributeTuples()

void InsertPgAttributeTuples ( Relation  pg_attribute_rel,
TupleDesc  tupdesc,
Oid  new_rel_oid,
const FormExtraData_pg_attribute  tupdesc_extra[],
CatalogIndexState  indstate 
)

Definition at line 717 of file heap.c.

722{
723 TupleTableSlot **slot;
724 TupleDesc td;
725 int nslots;
726 int natts = 0;
727 int slotCount = 0;
728 bool close_index = false;
729
731
732 /* Initialize the number of slots to use */
733 nslots = Min(tupdesc->natts,
735 slot = palloc_array(TupleTableSlot *, nslots);
736 for (int i = 0; i < nslots; i++)
738
739 while (natts < tupdesc->natts)
740 {
741 Form_pg_attribute attrs = TupleDescAttr(tupdesc, natts);
743
745
746 memset(slot[slotCount]->tts_isnull, false,
747 slot[slotCount]->tts_tupleDescriptor->natts * sizeof(bool));
748
749 if (new_rel_oid != InvalidOid)
751 else
753
773 if (attrs_extra)
774 {
775 slot[slotCount]->tts_values[Anum_pg_attribute_attstattarget - 1] = attrs_extra->attstattarget.value;
776 slot[slotCount]->tts_isnull[Anum_pg_attribute_attstattarget - 1] = attrs_extra->attstattarget.isnull;
777
778 slot[slotCount]->tts_values[Anum_pg_attribute_attoptions - 1] = attrs_extra->attoptions.value;
779 slot[slotCount]->tts_isnull[Anum_pg_attribute_attoptions - 1] = attrs_extra->attoptions.isnull;
780 }
781 else
782 {
785 }
786
787 /*
788 * The remaining fields are not set for new columns.
789 */
793
795 slotCount++;
796
797 /*
798 * If slots are full or the end of processing has been reached, insert
799 * a batch of tuples.
800 */
801 if (slotCount == nslots || natts == tupdesc->natts - 1)
802 {
803 /* fetch index info only when we know we need it */
804 if (!indstate)
805 {
807 close_index = true;
808 }
809
810 /* insert the new tuples and update the indexes */
812 indstate);
813 slotCount = 0;
814 }
815
816 natts++;
817 }
818
819 if (close_index)
821 for (int i = 0; i < nslots; i++)
823 pfree(slot);
824}
#define Min(x, y)
Definition c.h:997
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
const TupleTableSlotOps TTSOpsHeapTuple
Definition execTuples.c:85
#define palloc_array(type, count)
Definition fe_memutils.h:76
void CatalogTuplesMultiInsertWithInfo(Relation heapRel, TupleTableSlot **slot, int ntuples, CatalogIndexState indstate)
Definition indexing.c:273
#define MAX_CATALOG_MULTI_INSERT_BYTES
Definition indexing.h:33
static Datum BoolGetDatum(bool X)
Definition postgres.h:112
static Datum NameGetDatum(const NameData *X)
Definition postgres.h:403
static Datum Int32GetDatum(int32 X)
Definition postgres.h:222
static Datum CharGetDatum(char X)
Definition postgres.h:132
bool * tts_isnull
Definition tuptable.h:126
Datum * tts_values
Definition tuptable.h:124
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition tuptable.h:457

References BoolGetDatum(), CatalogCloseIndexes(), CatalogOpenIndexes(), CatalogTuplesMultiInsertWithInfo(), CharGetDatum(), ExecClearTuple(), ExecDropSingleTupleTableSlot(), ExecStoreVirtualTuple(), fb(), FormData_pg_attribute, i, Int16GetDatum(), Int32GetDatum(), InvalidOid, MakeSingleTupleTableSlot(), MAX_CATALOG_MULTI_INSERT_BYTES, Min, NameGetDatum(), TupleDescData::natts, ObjectIdGetDatum(), palloc_array, pfree(), RelationGetDescr, TupleTableSlot::tts_isnull, TupleTableSlot::tts_values, TTSOpsHeapTuple, and TupleDescAttr().

Referenced by AddNewAttributeTuples(), AppendAttributeTuples(), and ATExecAddColumn().

◆ InsertPgClassTuple()

void InsertPgClassTuple ( Relation  pg_class_desc,
Relation  new_rel_desc,
Oid  new_rel_oid,
Datum  relacl,
Datum  reloptions 
)

Definition at line 910 of file heap.c.

915{
916 Form_pg_class rd_rel = new_rel_desc->rd_rel;
918 bool nulls[Natts_pg_class];
920
921 /* This is a tad tedious, but way cleaner than what we used to do... */
922 memset(values, 0, sizeof(values));
923 memset(nulls, false, sizeof(nulls));
924
926 values[Anum_pg_class_relname - 1] = NameGetDatum(&rd_rel->relname);
927 values[Anum_pg_class_relnamespace - 1] = ObjectIdGetDatum(rd_rel->relnamespace);
928 values[Anum_pg_class_reltype - 1] = ObjectIdGetDatum(rd_rel->reltype);
929 values[Anum_pg_class_reloftype - 1] = ObjectIdGetDatum(rd_rel->reloftype);
930 values[Anum_pg_class_relowner - 1] = ObjectIdGetDatum(rd_rel->relowner);
931 values[Anum_pg_class_relam - 1] = ObjectIdGetDatum(rd_rel->relam);
932 values[Anum_pg_class_relfilenode - 1] = ObjectIdGetDatum(rd_rel->relfilenode);
933 values[Anum_pg_class_reltablespace - 1] = ObjectIdGetDatum(rd_rel->reltablespace);
934 values[Anum_pg_class_relpages - 1] = Int32GetDatum(rd_rel->relpages);
935 values[Anum_pg_class_reltuples - 1] = Float4GetDatum(rd_rel->reltuples);
936 values[Anum_pg_class_relallvisible - 1] = Int32GetDatum(rd_rel->relallvisible);
937 values[Anum_pg_class_relallfrozen - 1] = Int32GetDatum(rd_rel->relallfrozen);
938 values[Anum_pg_class_reltoastrelid - 1] = ObjectIdGetDatum(rd_rel->reltoastrelid);
939 values[Anum_pg_class_relhasindex - 1] = BoolGetDatum(rd_rel->relhasindex);
940 values[Anum_pg_class_relisshared - 1] = BoolGetDatum(rd_rel->relisshared);
941 values[Anum_pg_class_relpersistence - 1] = CharGetDatum(rd_rel->relpersistence);
942 values[Anum_pg_class_relkind - 1] = CharGetDatum(rd_rel->relkind);
943 values[Anum_pg_class_relnatts - 1] = Int16GetDatum(rd_rel->relnatts);
944 values[Anum_pg_class_relchecks - 1] = Int16GetDatum(rd_rel->relchecks);
945 values[Anum_pg_class_relhasrules - 1] = BoolGetDatum(rd_rel->relhasrules);
946 values[Anum_pg_class_relhastriggers - 1] = BoolGetDatum(rd_rel->relhastriggers);
947 values[Anum_pg_class_relrowsecurity - 1] = BoolGetDatum(rd_rel->relrowsecurity);
948 values[Anum_pg_class_relforcerowsecurity - 1] = BoolGetDatum(rd_rel->relforcerowsecurity);
949 values[Anum_pg_class_relhassubclass - 1] = BoolGetDatum(rd_rel->relhassubclass);
950 values[Anum_pg_class_relispopulated - 1] = BoolGetDatum(rd_rel->relispopulated);
951 values[Anum_pg_class_relreplident - 1] = CharGetDatum(rd_rel->relreplident);
952 values[Anum_pg_class_relispartition - 1] = BoolGetDatum(rd_rel->relispartition);
953 values[Anum_pg_class_relrewrite - 1] = ObjectIdGetDatum(rd_rel->relrewrite);
954 values[Anum_pg_class_relfrozenxid - 1] = TransactionIdGetDatum(rd_rel->relfrozenxid);
955 values[Anum_pg_class_relminmxid - 1] = MultiXactIdGetDatum(rd_rel->relminmxid);
956 if (relacl != (Datum) 0)
958 else
959 nulls[Anum_pg_class_relacl - 1] = true;
960 if (reloptions != (Datum) 0)
961 values[Anum_pg_class_reloptions - 1] = reloptions;
962 else
963 nulls[Anum_pg_class_reloptions - 1] = true;
964
965 /* relpartbound is set by updating this tuple, if necessary */
966 nulls[Anum_pg_class_relpartbound - 1] = true;
967
969
970 /* finally insert the new tuple, update the indexes, and clean up */
972
974}
static Datum values[MAXATTR]
Definition bootstrap.c:155
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition heaptuple.c:1117
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition indexing.c:233
static Datum Float4GetDatum(float4 X)
Definition postgres.h:478
static Datum TransactionIdGetDatum(TransactionId X)
Definition postgres.h:302
static Datum MultiXactIdGetDatum(MultiXactId X)
Definition postgres.h:312
uint64_t Datum
Definition postgres.h:70

References BoolGetDatum(), CatalogTupleInsert(), CharGetDatum(), fb(), Float4GetDatum(), heap_form_tuple(), heap_freetuple(), Int16GetDatum(), Int32GetDatum(), MultiXactIdGetDatum(), NameGetDatum(), ObjectIdGetDatum(), RelationGetDescr, TransactionIdGetDatum(), and values.

Referenced by AddNewRelationTuple(), and index_create().

◆ MergeWithExistingConstraint()

static bool MergeWithExistingConstraint ( Relation  rel,
const char ccname,
Node expr,
bool  allow_merge,
bool  is_local,
bool  is_enforced,
bool  is_initially_valid,
bool  is_no_inherit 
)
static

Definition at line 2712 of file heap.c.

2717{
2718 bool found;
2721 ScanKeyData skey[3];
2722 HeapTuple tup;
2723
2724 /* Search for a pg_constraint entry with same name and relation */
2726
2727 found = false;
2728
2729 ScanKeyInit(&skey[0],
2733 ScanKeyInit(&skey[1],
2737 ScanKeyInit(&skey[2],
2740 CStringGetDatum(ccname));
2741
2743 NULL, 3, skey);
2744
2745 /* There can be at most one matching row */
2747 {
2749
2750 /* Found it. Conflicts if not identical check constraint */
2751 if (con->contype == CONSTRAINT_CHECK)
2752 {
2753 Datum val;
2754 bool isnull;
2755
2758 conDesc->rd_att, &isnull);
2759 if (isnull)
2760 elog(ERROR, "null conbin for rel %s",
2763 found = true;
2764 }
2765
2766 /*
2767 * If the existing constraint is purely inherited (no local
2768 * definition) then interpret addition of a local constraint as a
2769 * legal merge. This allows ALTER ADD CONSTRAINT on parent and child
2770 * tables to be given in either order with same end state. However if
2771 * the relation is a partition, all inherited constraints are always
2772 * non-local, including those that were merged.
2773 */
2774 if (is_local && !con->conislocal && !rel->rd_rel->relispartition)
2775 allow_merge = true;
2776
2777 if (!found || !allow_merge)
2778 ereport(ERROR,
2780 errmsg("constraint \"%s\" for relation \"%s\" already exists",
2781 ccname, RelationGetRelationName(rel))));
2782
2783 /* If the child constraint is "no inherit" then cannot merge */
2784 if (con->connoinherit)
2785 ereport(ERROR,
2787 errmsg("constraint \"%s\" conflicts with non-inherited constraint on relation \"%s\"",
2788 ccname, RelationGetRelationName(rel))));
2789
2790 /*
2791 * Must not change an existing inherited constraint to "no inherit"
2792 * status. That's because inherited constraints should be able to
2793 * propagate to lower-level children.
2794 */
2795 if (con->coninhcount > 0 && is_no_inherit)
2796 ereport(ERROR,
2798 errmsg("constraint \"%s\" conflicts with inherited constraint on relation \"%s\"",
2799 ccname, RelationGetRelationName(rel))));
2800
2801 /*
2802 * If the child constraint is "not valid" then cannot merge with a
2803 * valid parent constraint.
2804 */
2805 if (is_initially_valid && con->conenforced && !con->convalidated)
2806 ereport(ERROR,
2808 errmsg("constraint \"%s\" conflicts with NOT VALID constraint on relation \"%s\"",
2809 ccname, RelationGetRelationName(rel))));
2810
2811 /*
2812 * A non-enforced child constraint cannot be merged with an enforced
2813 * parent constraint. However, the reverse is allowed, where the child
2814 * constraint is enforced.
2815 */
2816 if ((!is_local && is_enforced && !con->conenforced) ||
2817 (is_local && !is_enforced && con->conenforced))
2818 ereport(ERROR,
2820 errmsg("constraint \"%s\" conflicts with NOT ENFORCED constraint on relation \"%s\"",
2821 ccname, RelationGetRelationName(rel))));
2822
2823 /* OK to update the tuple */
2825 (errmsg("merging constraint \"%s\" with inherited definition",
2826 ccname)));
2827
2830
2831 /*
2832 * In case of partitions, an inherited constraint must be inherited
2833 * only once since it cannot have multiple parents and it is never
2834 * considered local.
2835 */
2836 if (rel->rd_rel->relispartition)
2837 {
2838 con->coninhcount = 1;
2839 con->conislocal = false;
2840 }
2841 else
2842 {
2843 if (is_local)
2844 con->conislocal = true;
2845 else if (pg_add_s16_overflow(con->coninhcount, 1,
2846 &con->coninhcount))
2847 ereport(ERROR,
2849 errmsg("too many inheritance parents"));
2850 }
2851
2852 if (is_no_inherit)
2853 {
2854 Assert(is_local);
2855 con->connoinherit = true;
2856 }
2857
2858 /*
2859 * If the child constraint is required to be enforced while the parent
2860 * constraint is not, this should be allowed by marking the child
2861 * constraint as enforced. In the reverse case, an error would have
2862 * already been thrown before reaching this point.
2863 */
2864 if (is_enforced && !con->conenforced)
2865 {
2866 Assert(is_local);
2867 con->conenforced = true;
2868 con->convalidated = true;
2869 }
2870
2871 CatalogTupleUpdate(conDesc, &tup->t_self, tup);
2872 }
2873
2876
2877 return found;
2878}
#define TextDatumGetCString(d)
Definition builtins.h:98
#define NOTICE
Definition elog.h:35
bool equal(const void *a, const void *b)
Definition equalfuncs.c:223
static Datum fastgetattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
void CatalogTupleUpdate(Relation heapRel, const ItemPointerData *otid, HeapTuple tup)
Definition indexing.c:313
long val
Definition informix.c:689
static bool pg_add_s16_overflow(int16 a, int16 b, int16 *result)
Definition int.h:67

References Assert, BTEqualStrategyNumber, CatalogTupleUpdate(), CStringGetDatum(), elog, equal(), ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, fastgetattr(), fb(), GETSTRUCT(), heap_copytuple(), HeapTupleIsValid, InvalidOid, NOTICE, ObjectIdGetDatum(), pg_add_s16_overflow(), RelationData::rd_rel, RelationGetRelationName, RelationGetRelid, RowExclusiveLock, ScanKeyInit(), stringToNode(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), table_open(), TextDatumGetCString, and val.

Referenced by AddRelationNewConstraints().

◆ RelationClearMissing()

void RelationClearMissing ( Relation  rel)

Definition at line 1964 of file heap.c.

1965{
1967 Oid relid = RelationGetRelid(rel);
1968 int natts = RelationGetNumberOfAttributes(rel);
1969 int attnum;
1974 HeapTuple tuple,
1975 newtuple;
1976
1977 memset(repl_val, 0, sizeof(repl_val));
1978 memset(repl_null, false, sizeof(repl_null));
1979 memset(repl_repl, false, sizeof(repl_repl));
1980
1983
1986
1987
1988 /* Get a lock on pg_attribute */
1990
1991 /* process each non-system attribute, including any dropped columns */
1992 for (attnum = 1; attnum <= natts; attnum++)
1993 {
1994 tuple = SearchSysCache2(ATTNUM,
1995 ObjectIdGetDatum(relid),
1997 if (!HeapTupleIsValid(tuple)) /* shouldn't happen */
1998 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
1999 attnum, relid);
2000
2002
2003 /* ignore any where atthasmissing is not true */
2004 if (attrtuple->atthasmissing)
2005 {
2006 newtuple = heap_modify_tuple(tuple, RelationGetDescr(attr_rel),
2008
2009 CatalogTupleUpdate(attr_rel, &newtuple->t_self, newtuple);
2010
2011 heap_freetuple(newtuple);
2012 }
2013
2014 ReleaseSysCache(tuple);
2015 }
2016
2017 /*
2018 * Our update of the pg_attribute rows will force a relcache rebuild, so
2019 * there's nothing else to do here.
2020 */
2022}
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
Definition heaptuple.c:1210
#define RelationGetNumberOfAttributes(relation)
Definition rel.h:520
ItemPointerData t_self
Definition htup.h:65
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition syscache.c:230

References attnum, BoolGetDatum(), CatalogTupleUpdate(), elog, ERROR, fb(), GETSTRUCT(), heap_freetuple(), heap_modify_tuple(), HeapTupleIsValid, Int16GetDatum(), ObjectIdGetDatum(), RelationGetDescr, RelationGetNumberOfAttributes, RelationGetRelid, ReleaseSysCache(), RowExclusiveLock, SearchSysCache2(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by ATExecAlterColumnType(), ATExecSetExpression(), and finish_heap_swap().

◆ RelationRemoveInheritance()

◆ RelationTruncateIndexes()

static void RelationTruncateIndexes ( Relation  heapRelation)
static

Definition at line 3540 of file heap.c.

3541{
3543
3544 /* Ask the relcache to produce a list of the indexes of the rel */
3545 foreach(indlist, RelationGetIndexList(heapRelation))
3546 {
3549 IndexInfo *indexInfo;
3550
3551 /* Open the index relation; use exclusive lock, just to be sure */
3553
3554 /*
3555 * Fetch info needed for index_build. Since we know there are no
3556 * tuples that actually need indexing, we can use a dummy IndexInfo.
3557 * This is slightly cheaper to build, but the real point is to avoid
3558 * possibly running user-defined code in index expressions or
3559 * predicates. We might be getting invoked during ON COMMIT
3560 * processing, and we don't want to run any such code then.
3561 */
3562 indexInfo = BuildDummyIndexInfo(currentIndex);
3563
3564 /*
3565 * Now truncate the actual file (and discard buffers).
3566 */
3568
3569 /* Initialize the index and rebuild */
3570 /* Note: we do not need to re-establish pkey setting */
3571 index_build(heapRelation, currentIndex, indexInfo, true, false);
3572
3573 /* We're done with this index */
3575 }
3576}
IndexInfo * BuildDummyIndexInfo(Relation index)
Definition index.c:2486
void index_build(Relation heapRelation, Relation indexRelation, IndexInfo *indexInfo, bool isreindex, bool parallel)
Definition index.c:3000
void index_close(Relation relation, LOCKMODE lockmode)
Definition indexam.c:177
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition indexam.c:133
List * RelationGetIndexList(Relation relation)
Definition relcache.c:4831
void RelationTruncate(Relation rel, BlockNumber nblocks)
Definition storage.c:289

References AccessExclusiveLock, BuildDummyIndexInfo(), fb(), index_build(), index_close(), index_open(), lfirst_oid, NoLock, RelationGetIndexList(), and RelationTruncate().

Referenced by heap_truncate_one_rel().

◆ RemoveAttributeById()

void RemoveAttributeById ( Oid  relid,
AttrNumber  attnum 
)

Definition at line 1683 of file heap.c.

1684{
1685 Relation rel;
1687 HeapTuple tuple;
1689 char newattname[NAMEDATALEN];
1691 bool nullsAtt[Natts_pg_attribute] = {0};
1692 bool replacesAtt[Natts_pg_attribute] = {0};
1693
1694 /*
1695 * Grab an exclusive lock on the target table, which we will NOT release
1696 * until end of transaction. (In the simple case where we are directly
1697 * dropping this column, ATExecDropColumn already did this ... but when
1698 * cascading from a drop of some other object, we may not have any lock.)
1699 */
1700 rel = relation_open(relid, AccessExclusiveLock);
1701
1703
1705 ObjectIdGetDatum(relid),
1707 if (!HeapTupleIsValid(tuple)) /* shouldn't happen */
1708 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
1709 attnum, relid);
1711
1712 /* Mark the attribute as dropped */
1713 attStruct->attisdropped = true;
1714
1715 /*
1716 * Set the type OID to invalid. A dropped attribute's type link cannot be
1717 * relied on (once the attribute is dropped, the type might be too).
1718 * Fortunately we do not need the type row --- the only really essential
1719 * information is the type's typlen and typalign, which are preserved in
1720 * the attribute's attlen and attalign. We set atttypid to zero here as a
1721 * means of catching code that incorrectly expects it to be valid.
1722 */
1723 attStruct->atttypid = InvalidOid;
1724
1725 /* Remove any not-null constraint the column may have */
1726 attStruct->attnotnull = false;
1727
1728 /* Unset this so no one tries to look up the generation expression */
1729 attStruct->attgenerated = '\0';
1730
1731 /*
1732 * Change the column name to something that isn't likely to conflict
1733 */
1735 "........pg.dropped.%d........", attnum);
1736 namestrcpy(&(attStruct->attname), newattname);
1737
1738 /* Clear the missing value */
1739 attStruct->atthasmissing = false;
1742
1743 /*
1744 * Clear the other nullable fields. This saves some space in pg_attribute
1745 * and removes no longer useful information.
1746 */
1755
1758
1759 CatalogTupleUpdate(attr_rel, &tuple->t_self, tuple);
1760
1761 /*
1762 * Because updating the pg_attribute row will trigger a relcache flush for
1763 * the target relation, we need not do anything else to notify other
1764 * backends of the change.
1765 */
1766
1768
1769 RemoveStatistics(relid, attnum);
1770
1771 relation_close(rel, NoLock);
1772}
void namestrcpy(Name name, const char *str)
Definition name.c:233
#define NAMEDATALEN
#define snprintf
Definition port.h:260
#define SearchSysCacheCopy2(cacheId, key1, key2)
Definition syscache.h:93

References AccessExclusiveLock, attnum, CatalogTupleUpdate(), elog, ERROR, fb(), GETSTRUCT(), heap_modify_tuple(), HeapTupleIsValid, Int16GetDatum(), InvalidOid, NAMEDATALEN, namestrcpy(), NoLock, ObjectIdGetDatum(), relation_close(), relation_open(), RelationGetDescr, RemoveStatistics(), RowExclusiveLock, SearchSysCacheCopy2, snprintf, HeapTupleData::t_self, table_close(), and table_open().

Referenced by doDeletion().

◆ RemovePartitionKeyByRelId()

void RemovePartitionKeyByRelId ( Oid  relid)

Definition at line 4020 of file heap.c.

4021{
4022 Relation rel;
4023 HeapTuple tuple;
4024
4026
4028 if (!HeapTupleIsValid(tuple))
4029 elog(ERROR, "cache lookup failed for partition key of relation %u",
4030 relid);
4031
4032 CatalogTupleDelete(rel, &tuple->t_self);
4033
4034 ReleaseSysCache(tuple);
4036}

References CatalogTupleDelete(), elog, ERROR, fb(), HeapTupleIsValid, ObjectIdGetDatum(), ReleaseSysCache(), RowExclusiveLock, SearchSysCache1(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by heap_drop_with_catalog().

◆ RemoveStatistics()

void RemoveStatistics ( Oid  relid,
AttrNumber  attnum 
)

Definition at line 3493 of file heap.c.

3494{
3496 SysScanDesc scan;
3497 ScanKeyData key[2];
3498 int nkeys;
3499 HeapTuple tuple;
3500
3502
3503 ScanKeyInit(&key[0],
3506 ObjectIdGetDatum(relid));
3507
3508 if (attnum == 0)
3509 nkeys = 1;
3510 else
3511 {
3512 ScanKeyInit(&key[1],
3516 nkeys = 2;
3517 }
3518
3520 NULL, nkeys, key);
3521
3522 /* we must loop even when attnum != 0, in case of inherited stats */
3523 while (HeapTupleIsValid(tuple = systable_getnext(scan)))
3525
3526 systable_endscan(scan);
3527
3529}

References attnum, BTEqualStrategyNumber, CatalogTupleDelete(), fb(), HeapTupleIsValid, Int16GetDatum(), ObjectIdGetDatum(), RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by ATExecAlterColumnType(), ATExecSetExpression(), heap_drop_with_catalog(), index_drop(), and RemoveAttributeById().

◆ SetAttrMissing()

void SetAttrMissing ( Oid  relid,
char attname,
char value 
)

Definition at line 2086 of file heap.c.

2087{
2089 bool nullsAtt[Natts_pg_attribute] = {0};
2090 bool replacesAtt[Natts_pg_attribute] = {0};
2094 tablerel;
2096 newtup;
2097
2098 /* lock the table the attribute belongs to */
2100
2101 /* Don't do anything unless it's a plain table */
2102 if (tablerel->rd_rel->relkind != RELKIND_RELATION)
2103 {
2105 return;
2106 }
2107
2108 /* Lock the attribute row and get the data */
2112 elog(ERROR, "cache lookup failed for attribute %s of relation %u",
2113 attname, relid);
2115
2116 /* get an array value from the value string */
2119 ObjectIdGetDatum(attStruct->atttypid),
2120 Int32GetDatum(attStruct->atttypmod));
2121
2122 /* update the tuple - set atthasmissing and attmissingval */
2127
2131
2132 /* clean up */
2136}
#define OidFunctionCall3(functionId, arg1, arg2, arg3)
Definition fmgr.h:726
static struct @172 value
HeapTuple SearchSysCacheAttName(Oid relid, const char *attname)
Definition syscache.c:475

References AccessExclusiveLock, attname, BoolGetDatum(), CatalogTupleUpdate(), CStringGetDatum(), elog, ERROR, fb(), GETSTRUCT(), heap_modify_tuple(), HeapTupleIsValid, Int32GetDatum(), ObjectIdGetDatum(), OidFunctionCall3, RelationGetDescr, ReleaseSysCache(), RowExclusiveLock, SearchSysCacheAttName(), table_close(), table_open(), and value.

Referenced by binary_upgrade_set_missing_value().

◆ SetRelationNumChecks()

static void SetRelationNumChecks ( Relation  rel,
int  numchecks 
)
static

Definition at line 3147 of file heap.c.

3148{
3152
3157 elog(ERROR, "cache lookup failed for relation %u",
3158 RelationGetRelid(rel));
3160
3161 if (relStruct->relchecks != numchecks)
3162 {
3163 relStruct->relchecks = numchecks;
3164
3166 }
3167 else
3168 {
3169 /* Skip the disk update, but force relcache inval anyway */
3171 }
3172
3175}
void CacheInvalidateRelcache(Relation relation)
Definition inval.c:1635
#define SearchSysCacheCopy1(cacheId, key1)
Definition syscache.h:91

References CacheInvalidateRelcache(), CatalogTupleUpdate(), elog, ERROR, fb(), GETSTRUCT(), heap_freetuple(), HeapTupleIsValid, ObjectIdGetDatum(), RelationGetRelid, RowExclusiveLock, SearchSysCacheCopy1, table_close(), and table_open().

Referenced by AddRelationNewConstraints(), and StoreConstraints().

◆ StoreAttrMissingVal()

void StoreAttrMissingVal ( Relation  rel,
AttrNumber  attnum,
Datum  missingval 
)

Definition at line 2030 of file heap.c.

2031{
2033 bool nullsAtt[Natts_pg_attribute] = {0};
2034 bool replacesAtt[Natts_pg_attribute] = {0};
2038 newtup;
2039
2040 /* This is only supported for plain tables */
2041 Assert(rel->rd_rel->relkind == RELKIND_RELATION);
2042
2043 /* Fetch the pg_attribute row */
2045
2049 if (!HeapTupleIsValid(atttup)) /* shouldn't happen */
2050 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
2051 attnum, RelationGetRelid(rel));
2053
2054 /* Make a one-element array containing the value */
2056 1,
2057 attStruct->atttypid,
2058 attStruct->attlen,
2059 attStruct->attbyval,
2060 attStruct->attalign));
2061
2062 /* Update the pg_attribute row */
2065
2068
2072
2073 /* clean up */
2076}
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)

References Assert, attnum, BoolGetDatum(), CatalogTupleUpdate(), construct_array(), elog, ERROR, fb(), GETSTRUCT(), heap_modify_tuple(), HeapTupleIsValid, Int16GetDatum(), ObjectIdGetDatum(), PointerGetDatum(), RelationData::rd_rel, RelationGetDescr, RelationGetRelid, ReleaseSysCache(), RowExclusiveLock, SearchSysCache2(), table_close(), and table_open().

Referenced by ATExecAddColumn().

◆ StoreConstraints()

static void StoreConstraints ( Relation  rel,
List cooked_constraints,
bool  is_internal 
)
static

Definition at line 2310 of file heap.c.

2311{
2312 int numchecks = 0;
2313 ListCell *lc;
2314
2315 if (cooked_constraints == NIL)
2316 return; /* nothing to do */
2317
2318 /*
2319 * Deparsing of constraint expressions will fail unless the just-created
2320 * pg_attribute tuples for this relation are made visible. So, bump the
2321 * command counter. CAUTION: this will cause a relcache entry rebuild.
2322 */
2324
2325 foreach(lc, cooked_constraints)
2326 {
2328
2329 switch (con->contype)
2330 {
2331 case CONSTR_DEFAULT:
2332 con->conoid = StoreAttrDefault(rel, con->attnum, con->expr,
2333 is_internal);
2334 break;
2335 case CONSTR_CHECK:
2336 con->conoid =
2337 StoreRelCheck(rel, con->name, con->expr,
2338 con->is_enforced, !con->skip_validation,
2339 con->is_local, con->inhcount,
2340 con->is_no_inherit, is_internal);
2341 numchecks++;
2342 break;
2343
2344 default:
2345 elog(ERROR, "unrecognized constraint type: %d",
2346 (int) con->contype);
2347 }
2348 }
2349
2350 if (numchecks > 0)
2352}
char * name
Definition heap.h:40
AttrNumber attnum
Definition heap.h:41
bool skip_validation
Definition heap.h:44
bool is_enforced
Definition heap.h:43
bool is_no_inherit
Definition heap.h:47
int16 inhcount
Definition heap.h:46
bool is_local
Definition heap.h:45
ConstrType contype
Definition heap.h:37
Node * expr
Definition heap.h:42
void CommandCounterIncrement(void)
Definition xact.c:1101

References CookedConstraint::attnum, CommandCounterIncrement(), CookedConstraint::conoid, CONSTR_CHECK, CONSTR_DEFAULT, CookedConstraint::contype, elog, ERROR, CookedConstraint::expr, fb(), CookedConstraint::inhcount, CookedConstraint::is_enforced, CookedConstraint::is_local, CookedConstraint::is_no_inherit, lfirst, CookedConstraint::name, NIL, SetRelationNumChecks(), CookedConstraint::skip_validation, StoreAttrDefault(), and StoreRelCheck().

Referenced by heap_create_with_catalog().

◆ StorePartitionBound()

void StorePartitionBound ( Relation  rel,
Relation  parent,
PartitionBoundSpec bound 
)

Definition at line 4051 of file heap.c.

4052{
4054 HeapTuple tuple,
4055 newtuple;
4060
4061 /* Update pg_class tuple */
4065 if (!HeapTupleIsValid(tuple))
4066 elog(ERROR, "cache lookup failed for relation %u",
4067 RelationGetRelid(rel));
4068
4069#ifdef USE_ASSERT_CHECKING
4070 {
4072 bool isnull;
4073
4075 Assert(!classForm->relispartition);
4077 &isnull);
4078 Assert(isnull);
4079 }
4080#endif
4081
4082 /* Fill in relpartbound value */
4083 memset(new_val, 0, sizeof(new_val));
4084 memset(new_null, false, sizeof(new_null));
4085 memset(new_repl, false, sizeof(new_repl));
4089 newtuple = heap_modify_tuple(tuple, RelationGetDescr(classRel),
4091 /* Also set the flag */
4092 ((Form_pg_class) GETSTRUCT(newtuple))->relispartition = true;
4093
4094 /*
4095 * We already checked for no inheritance children, but reset
4096 * relhassubclass in case it was left over.
4097 */
4098 if (rel->rd_rel->relkind == RELKIND_RELATION && rel->rd_rel->relhassubclass)
4099 ((Form_pg_class) GETSTRUCT(newtuple))->relhassubclass = false;
4100
4101 CatalogTupleUpdate(classRel, &newtuple->t_self, newtuple);
4102 heap_freetuple(newtuple);
4104
4105 /*
4106 * If we're storing bounds for the default partition, update
4107 * pg_partitioned_table too.
4108 */
4109 if (bound->is_default)
4111 RelationGetRelid(rel));
4112
4113 /* Make these updates visible */
4115
4116 /*
4117 * The partition constraint for the default partition depends on the
4118 * partition bounds of every other partition, so we must invalidate the
4119 * relcache entry for that partition every time a partition is added or
4120 * removed.
4121 */
4126
4128}
#define CStringGetTextDatum(s)
Definition builtins.h:97
char * nodeToString(const void *obj)
Definition outfuncs.c:802
PartitionDesc RelationGetPartitionDesc(Relation rel, bool omit_detached)
Definition partdesc.c:71
Oid get_default_oid_from_partdesc(PartitionDesc partdesc)
Definition partdesc.c:501
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition syscache.c:595

References Assert, CacheInvalidateRelcache(), CacheInvalidateRelcacheByRelid(), CatalogTupleUpdate(), CommandCounterIncrement(), CStringGetTextDatum, elog, ERROR, fb(), get_default_oid_from_partdesc(), GETSTRUCT(), heap_freetuple(), heap_modify_tuple(), HeapTupleIsValid, PartitionBoundSpec::is_default, nodeToString(), ObjectIdGetDatum(), OidIsValid, RelationData::rd_rel, RelationGetDescr, RelationGetPartitionDesc(), RelationGetRelid, RowExclusiveLock, SearchSysCacheCopy1, SysCacheGetAttr(), HeapTupleData::t_self, table_close(), table_open(), and update_default_partition_oid().

Referenced by attachPartitionTable(), and DefineRelation().

◆ StorePartitionKey()

void StorePartitionKey ( Relation  rel,
char  strategy,
int16  partnatts,
AttrNumber partattrs,
List partexprs,
Oid partopclass,
Oid partcollation 
)

Definition at line 3895 of file heap.c.

3902{
3903 int i;
3909 HeapTuple tuple;
3911 bool nulls[Natts_pg_partitioned_table] = {0};
3914 ObjectAddresses *addrs;
3915
3916 Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
3917
3918 /* Copy the partition attribute numbers, opclass OIDs into arrays */
3919 partattrs_vec = buildint2vector(partattrs, partnatts);
3921 partcollation_vec = buildoidvector(partcollation, partnatts);
3922
3923 /* Convert the expressions (if any) to a text datum */
3924 if (partexprs)
3925 {
3926 char *exprString;
3927
3928 exprString = nodeToString(partexprs);
3931 }
3932 else
3933 partexprDatum = (Datum) 0;
3934
3936
3937 /* Only this can ever be NULL */
3938 if (!partexprDatum)
3939 nulls[Anum_pg_partitioned_table_partexprs - 1] = true;
3940
3949
3951
3954
3955 /* Mark this relation as dependent on a few things as follows */
3956 addrs = new_object_addresses();
3958
3959 /* Operator class and collation per key column */
3960 for (i = 0; i < partnatts; i++)
3961 {
3964
3965 /* The default collation is pinned, so don't bother recording it */
3966 if (OidIsValid(partcollation[i]) &&
3967 partcollation[i] != DEFAULT_COLLATION_OID)
3968 {
3971 }
3972 }
3973
3975 free_object_addresses(addrs);
3976
3977 /*
3978 * The partitioning columns are made internally dependent on the table,
3979 * because we cannot drop any of them without dropping the whole table.
3980 * (ATExecDropColumn independently enforces that, but it's not bulletproof
3981 * so we need the dependencies too.)
3982 */
3983 for (i = 0; i < partnatts; i++)
3984 {
3985 if (partattrs[i] == 0)
3986 continue; /* ignore expressions here */
3987
3989 RelationGetRelid(rel), partattrs[i]);
3991 }
3992
3993 /*
3994 * Also consider anything mentioned in partition expressions. External
3995 * references (e.g. functions) get NORMAL dependencies. Table columns
3996 * mentioned in the expressions are handled the same as plain partitioning
3997 * columns, i.e. they become internally dependent on the whole table.
3998 */
3999 if (partexprs)
4001 (Node *) partexprs,
4002 RelationGetRelid(rel),
4005 true /* reverse the self-deps */ );
4006
4007 /*
4008 * We must invalidate the relcache so that the next
4009 * CommandCounterIncrement() will cause the same to be rebuilt using the
4010 * information in just created catalog entry.
4011 */
4013}
void recordDependencyOnSingleRelExpr(const ObjectAddress *depender, Node *expr, Oid relId, DependencyType behavior, DependencyType self_behavior, bool reverse_self)
@ DEPENDENCY_INTERNAL
Definition dependency.h:35
int2vector * buildint2vector(const int16 *int2s, int n)
Definition int.c:114
oidvector * buildoidvector(const Oid *oids, int n)
Definition oid.c:87
Definition c.h:745

References add_exact_object_address(), Assert, buildint2vector(), buildoidvector(), CacheInvalidateRelcache(), CatalogTupleInsert(), CharGetDatum(), CStringGetTextDatum, DEPENDENCY_INTERNAL, DEPENDENCY_NORMAL, fb(), free_object_addresses(), heap_form_tuple(), i, Int16GetDatum(), InvalidOid, new_object_addresses(), nodeToString(), ObjectAddressSet, ObjectAddressSubSet, ObjectIdGetDatum(), OidIsValid, pfree(), PointerGetDatum(), RelationData::rd_rel, record_object_address_dependencies(), recordDependencyOn(), recordDependencyOnSingleRelExpr(), RelationGetDescr, RelationGetRelid, RowExclusiveLock, table_close(), table_open(), and values.

Referenced by DefineRelation().

◆ StoreRelCheck()

static Oid StoreRelCheck ( Relation  rel,
const char ccname,
Node expr,
bool  is_enforced,
bool  is_validated,
bool  is_local,
int16  inhcount,
bool  is_no_inherit,
bool  is_internal 
)
static

Definition at line 2147 of file heap.c.

2150{
2151 char *ccbin;
2152 List *varList;
2153 int keycount;
2154 int16 *attNos;
2155 Oid constrOid;
2156
2157 /*
2158 * Flatten expression to string form for storage.
2159 */
2160 ccbin = nodeToString(expr);
2161
2162 /*
2163 * Find columns of rel that are used in expr
2164 *
2165 * NB: pull_var_clause is okay here only because we don't allow subselects
2166 * in check constraints; it would fail to examine the contents of
2167 * subselects.
2168 */
2169 varList = pull_var_clause(expr, 0);
2171
2172 if (keycount > 0)
2173 {
2174 ListCell *vl;
2175 int i = 0;
2176
2177 attNos = (int16 *) palloc(keycount * sizeof(int16));
2178 foreach(vl, varList)
2179 {
2180 Var *var = (Var *) lfirst(vl);
2181 int j;
2182
2183 for (j = 0; j < i; j++)
2184 if (attNos[j] == var->varattno)
2185 break;
2186 if (j == i)
2187 attNos[i++] = var->varattno;
2188 }
2189 keycount = i;
2190 }
2191 else
2192 attNos = NULL;
2193
2194 /*
2195 * Partitioned tables do not contain any rows themselves, so a NO INHERIT
2196 * constraint makes no sense.
2197 */
2198 if (is_no_inherit &&
2199 rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
2200 ereport(ERROR,
2202 errmsg("cannot add NO INHERIT constraint to partitioned table \"%s\"",
2204
2205 /*
2206 * Create the Check Constraint
2207 */
2208 constrOid =
2209 CreateConstraintEntry(ccname, /* Constraint Name */
2210 RelationGetNamespace(rel), /* namespace */
2211 CONSTRAINT_CHECK, /* Constraint Type */
2212 false, /* Is Deferrable */
2213 false, /* Is Deferred */
2214 is_enforced, /* Is Enforced */
2216 InvalidOid, /* no parent constraint */
2217 RelationGetRelid(rel), /* relation */
2218 attNos, /* attrs in the constraint */
2219 keycount, /* # key attrs in the constraint */
2220 keycount, /* # total attrs in the constraint */
2221 InvalidOid, /* not a domain constraint */
2222 InvalidOid, /* no associated index */
2223 InvalidOid, /* Foreign key fields */
2224 NULL,
2225 NULL,
2226 NULL,
2227 NULL,
2228 0,
2229 ' ',
2230 ' ',
2231 NULL,
2232 0,
2233 ' ',
2234 NULL, /* not an exclusion constraint */
2235 expr, /* Tree form of check constraint */
2236 ccbin, /* Binary form of check constraint */
2237 is_local, /* conislocal */
2238 inhcount, /* coninhcount */
2239 is_no_inherit, /* connoinherit */
2240 false, /* conperiod */
2241 is_internal); /* internally constructed? */
2242
2243 pfree(ccbin);
2244
2245 return constrOid;
2246}
void * palloc(Size size)
Definition mcxt.c:1387
Oid CreateConstraintEntry(const char *constraintName, Oid constraintNamespace, char constraintType, bool isDeferrable, bool isDeferred, bool isEnforced, bool isValidated, Oid parentConstrId, Oid relId, const int16 *constraintKey, int constraintNKeys, int constraintNTotalKeys, Oid domainId, Oid indexRelId, Oid foreignRelId, const int16 *foreignKey, const Oid *pfEqOp, const Oid *ppEqOp, const Oid *ffEqOp, int foreignNKeys, char foreignUpdateType, char foreignDeleteType, const int16 *fkDeleteSetCols, int numFkDeleteSetCols, char foreignMatchType, const Oid *exclOp, Node *conExpr, const char *conBin, bool conIsLocal, int16 conInhCount, bool conNoInherit, bool conPeriod, bool is_internal)

References CreateConstraintEntry(), ereport, errcode(), errmsg(), ERROR, fb(), i, InvalidOid, j, lfirst, list_length(), nodeToString(), palloc(), pfree(), pull_var_clause(), RelationData::rd_rel, RelationGetNamespace, RelationGetRelationName, RelationGetRelid, and Var::varattno.

Referenced by AddRelationNewConstraints(), and StoreConstraints().

◆ StoreRelNotNull()

static Oid StoreRelNotNull ( Relation  rel,
const char nnname,
AttrNumber  attnum,
bool  is_validated,
bool  is_local,
int  inhcount,
bool  is_no_inherit 
)
static

Definition at line 2254 of file heap.c.

2257{
2258 Oid constrOid;
2259
2261
2262 constrOid =
2266 false,
2267 false,
2268 true, /* Is Enforced */
2270 InvalidOid,
2271 RelationGetRelid(rel),
2272 &attnum,
2273 1,
2274 1,
2275 InvalidOid, /* not a domain constraint */
2276 InvalidOid, /* no associated index */
2277 InvalidOid, /* Foreign key fields */
2278 NULL,
2279 NULL,
2280 NULL,
2281 NULL,
2282 0,
2283 ' ',
2284 ' ',
2285 NULL,
2286 0,
2287 ' ',
2288 NULL, /* not an exclusion constraint */
2289 NULL,
2290 NULL,
2291 is_local,
2292 inhcount,
2293 is_no_inherit,
2294 false,
2295 false);
2296 return constrOid;
2297}

References Assert, attnum, CreateConstraintEntry(), fb(), InvalidAttrNumber, InvalidOid, RelationGetNamespace, and RelationGetRelid.

Referenced by AddRelationNewConstraints(), and AddRelationNotNullConstraints().

◆ SystemAttributeByName()

const FormData_pg_attribute * SystemAttributeByName ( const char attname)

Definition at line 248 of file heap.c.

249{
250 int j;
251
252 for (j = 0; j < (int) lengthof(SysAtt); j++)
253 {
255
256 if (strcmp(NameStr(att->attname), attname) == 0)
257 return att;
258 }
259
260 return NULL;
261}

References attname, fb(), FormData_pg_attribute, j, lengthof, NameStr, and SysAtt.

Referenced by CheckAttributeNamesTypes(), expanded_record_lookup_field(), specialAttNum(), SPI_fnumber(), and transformIndexConstraint().

◆ SystemAttributeDefinition()

const FormData_pg_attribute * SystemAttributeDefinition ( AttrNumber  attno)

Definition at line 236 of file heap.c.

237{
238 if (attno >= 0 || attno < -(int) lengthof(SysAtt))
239 elog(ERROR, "invalid system attribute number %d", attno);
240 return SysAtt[-attno - 1];
241}

References elog, ERROR, lengthof, and SysAtt.

Referenced by attnumAttName(), attnumTypeId(), build_index_tlist(), scanNSItemForColumn(), SPI_fname(), SPI_gettype(), SPI_gettypeid(), SPI_getvalue(), and transformIndexConstraint().

Variable Documentation

◆ a1

Initial value:
= {
.attname = {"ctid"},
.atttypid = TIDOID,
.attlen = sizeof(ItemPointerData),
.atttypmod = -1,
.attbyval = false,
.attnotnull = true,
.attislocal = true,
}
char attstorage
bool attbyval
char attalign
bool attnotnull
#define SelfItemPointerAttributeNumber
Definition sysattr.h:21

Definition at line 144 of file heap.c.

144 {
145 .attname = {"ctid"},
146 .atttypid = TIDOID,
147 .attlen = sizeof(ItemPointerData),
149 .atttypmod = -1,
150 .attbyval = false,
153 .attnotnull = true,
154 .attislocal = true,
155};

Referenced by aclitem_eq(), aclitem_match(), aclitemComparator(), brin_minmax_multi_distance_float4(), brin_minmax_multi_distance_float8(), brin_minmax_multi_distance_int2(), brin_minmax_multi_distance_int4(), brin_minmax_multi_distance_int8(), brin_minmax_multi_distance_numeric(), brin_minmax_multi_distance_uuid(), build_distances(), cmpaffix(), deccall2(), deccall3(), distance_1D(), entryIndexByFrequencyCmp(), evalLazyFunc(), float8_qsort_cmp(), inet_merge(), inet_same_family(), int4gcd_internal(), int8gcd_internal(), macaddr8_cmp(), macaddr8_cmp_internal(), macaddr8_eq(), macaddr8_ge(), macaddr8_gt(), macaddr8_le(), macaddr8_lt(), macaddr8_ne(), macaddr_cmp(), macaddr_cmp_internal(), macaddr_eq(), macaddr_ge(), macaddr_gt(), macaddr_le(), macaddr_lt(), macaddr_ne(), MergeAffix(), network_cmp(), network_cmp_internal(), network_eq(), network_ge(), network_gt(), network_larger(), network_le(), network_lt(), network_ne(), network_overlap(), network_smaller(), network_sub(), network_subeq(), network_sup(), network_supeq(), packArcInfoCmp(), range_bound_qsort_cmp(), and sqrt_var().

◆ a2

Initial value:
= {
.attname = {"xmin"},
.atttypid = XIDOID,
.attlen = sizeof(TransactionId),
.atttypmod = -1,
.attbyval = true,
.attnotnull = true,
.attislocal = true,
}
#define MinTransactionIdAttributeNumber
Definition sysattr.h:22

Definition at line 157 of file heap.c.

157 {
158 .attname = {"xmin"},
159 .atttypid = XIDOID,
160 .attlen = sizeof(TransactionId),
162 .atttypmod = -1,
163 .attbyval = true,
166 .attnotnull = true,
167 .attislocal = true,
168};

Referenced by aclitem_eq(), aclitem_match(), aclitemComparator(), brin_minmax_multi_distance_float4(), brin_minmax_multi_distance_float8(), brin_minmax_multi_distance_int2(), brin_minmax_multi_distance_int4(), brin_minmax_multi_distance_int8(), brin_minmax_multi_distance_numeric(), brin_minmax_multi_distance_uuid(), build_distances(), clonesuccessorstates(), cmpaffix(), deccall2(), deccall3(), distance_1D(), entryIndexByFrequencyCmp(), evalLazyFunc(), float8_qsort_cmp(), inet_merge(), inet_same_family(), int4gcd_internal(), int8gcd_internal(), macaddr8_cmp(), macaddr8_cmp_internal(), macaddr8_eq(), macaddr8_ge(), macaddr8_gt(), macaddr8_le(), macaddr8_lt(), macaddr8_ne(), macaddr_cmp(), macaddr_cmp_internal(), macaddr_eq(), macaddr_ge(), macaddr_gt(), macaddr_le(), macaddr_lt(), macaddr_ne(), MergeAffix(), network_cmp(), network_cmp_internal(), network_eq(), network_ge(), network_gt(), network_larger(), network_le(), network_lt(), network_ne(), network_overlap(), network_smaller(), network_sub(), network_subeq(), network_sup(), network_supeq(), packArcInfoCmp(), and range_bound_qsort_cmp().

◆ a3

Initial value:
= {
.attname = {"cmin"},
.atttypid = CIDOID,
.attlen = sizeof(CommandId),
.atttypmod = -1,
.attbyval = true,
.attnotnull = true,
.attislocal = true,
}
uint32 CommandId
Definition c.h:680
#define MinCommandIdAttributeNumber
Definition sysattr.h:23

Definition at line 170 of file heap.c.

170 {
171 .attname = {"cmin"},
172 .atttypid = CIDOID,
173 .attlen = sizeof(CommandId),
175 .atttypmod = -1,
176 .attbyval = true,
179 .attnotnull = true,
180 .attislocal = true,
181};

◆ a4

Initial value:
= {
.attname = {"xmax"},
.atttypid = XIDOID,
.attlen = sizeof(TransactionId),
.atttypmod = -1,
.attbyval = true,
.attnotnull = true,
.attislocal = true,
}
#define MaxTransactionIdAttributeNumber
Definition sysattr.h:24

Definition at line 183 of file heap.c.

183 {
184 .attname = {"xmax"},
185 .atttypid = XIDOID,
186 .attlen = sizeof(TransactionId),
188 .atttypmod = -1,
189 .attbyval = true,
192 .attnotnull = true,
193 .attislocal = true,
194};

◆ a5

Initial value:
= {
.attname = {"cmax"},
.atttypid = CIDOID,
.attlen = sizeof(CommandId),
.atttypmod = -1,
.attbyval = true,
.attnotnull = true,
.attislocal = true,
}
#define MaxCommandIdAttributeNumber
Definition sysattr.h:25

Definition at line 196 of file heap.c.

196 {
197 .attname = {"cmax"},
198 .atttypid = CIDOID,
199 .attlen = sizeof(CommandId),
201 .atttypmod = -1,
202 .attbyval = true,
205 .attnotnull = true,
206 .attislocal = true,
207};

◆ a6

Initial value:
= {
.attname = {"tableoid"},
.atttypid = OIDOID,
.attlen = sizeof(Oid),
.atttypmod = -1,
.attbyval = true,
.attnotnull = true,
.attislocal = true,
}
#define TableOidAttributeNumber
Definition sysattr.h:26

Definition at line 215 of file heap.c.

215 {
216 .attname = {"tableoid"},
217 .atttypid = OIDOID,
218 .attlen = sizeof(Oid),
220 .atttypmod = -1,
221 .attbyval = true,
224 .attnotnull = true,
225 .attislocal = true,
226};

◆ binary_upgrade_next_heap_pg_class_oid

Oid binary_upgrade_next_heap_pg_class_oid = InvalidOid

Definition at line 81 of file heap.c.

Referenced by binary_upgrade_set_next_heap_pg_class_oid(), and heap_create_with_catalog().

◆ binary_upgrade_next_heap_pg_class_relfilenumber

RelFileNumber binary_upgrade_next_heap_pg_class_relfilenumber = InvalidRelFileNumber

◆ binary_upgrade_next_toast_pg_class_oid

Oid binary_upgrade_next_toast_pg_class_oid = InvalidOid

◆ binary_upgrade_next_toast_pg_class_relfilenumber

RelFileNumber binary_upgrade_next_toast_pg_class_relfilenumber = InvalidRelFileNumber

Definition at line 84 of file heap.c.

Referenced by binary_upgrade_set_next_toast_relfilenode(), and heap_create_with_catalog().

◆ SysAtt

const FormData_pg_attribute* const SysAtt[] = {&a1, &a2, &a3, &a4, &a5, &a6}
static

Definition at line 228 of file heap.c.

228{&a1, &a2, &a3, &a4, &a5, &a6};
static const FormData_pg_attribute a4
Definition heap.c:183
static const FormData_pg_attribute a1
Definition heap.c:144
static const FormData_pg_attribute a6
Definition heap.c:215
static const FormData_pg_attribute a3
Definition heap.c:170
static const FormData_pg_attribute a2
Definition heap.c:157
static const FormData_pg_attribute a5
Definition heap.c:196

Referenced by AddNewAttributeTuples(), SystemAttributeByName(), and SystemAttributeDefinition().