PostgreSQL Source Code  git master
heap.h File Reference
Include dependency graph for heap.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  RawColumnDefault
 
struct  CookedConstraint
 

Macros

#define CHKATYPE_ANYARRAY   0x01 /* allow ANYARRAY */
 
#define CHKATYPE_ANYRECORD   0x02 /* allow RECORD and RECORD[] */
 
#define CHKATYPE_IS_PARTKEY   0x04 /* attname is part key # not column */
 

Typedefs

typedef struct RawColumnDefault RawColumnDefault
 
typedef struct CookedConstraint CookedConstraint
 

Functions

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)
 
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 heap_drop_with_catalog (Oid relid)
 
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 InsertPgAttributeTuples (Relation pg_attribute_rel, TupleDesc tupdesc, Oid new_rel_oid, const Datum *attoptions, CatalogIndexState indstate)
 
void InsertPgClassTuple (Relation pg_class_desc, Relation new_rel_desc, Oid new_rel_oid, Datum relacl, Datum reloptions)
 
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 *additional_notnulls)
 
void RelationClearMissing (Relation rel)
 
void SetAttrMissing (Oid relid, char *attname, char *value)
 
NodecookDefault (ParseState *pstate, Node *raw_default, Oid atttypid, int32 atttypmod, const char *attname, char attgenerated)
 
void DeleteRelationTuple (Oid relid)
 
void DeleteAttributeTuples (Oid relid)
 
void DeleteSystemAttributeTuples (Oid relid)
 
void RemoveAttributeById (Oid relid, AttrNumber attnum)
 
void CopyStatistics (Oid fromrelid, Oid torelid)
 
void RemoveStatistics (Oid relid, AttrNumber attnum)
 
const FormData_pg_attributeSystemAttributeDefinition (AttrNumber attno)
 
const FormData_pg_attributeSystemAttributeByName (const char *attname)
 
void CheckAttributeNamesTypes (TupleDesc tupdesc, char relkind, int flags)
 
void CheckAttributeType (const char *attname, Oid atttypid, Oid attcollation, List *containing_rowtypes, int flags)
 
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)
 

Macro Definition Documentation

◆ CHKATYPE_ANYARRAY

#define CHKATYPE_ANYARRAY   0x01 /* allow ANYARRAY */

Definition at line 23 of file heap.h.

◆ CHKATYPE_ANYRECORD

#define CHKATYPE_ANYRECORD   0x02 /* allow RECORD and RECORD[] */

Definition at line 24 of file heap.h.

◆ CHKATYPE_IS_PARTKEY

#define CHKATYPE_IS_PARTKEY   0x04 /* attname is part key # not column */

Definition at line 25 of file heap.h.

Typedef Documentation

◆ CookedConstraint

◆ RawColumnDefault

Function Documentation

◆ AddRelationNewConstraints()

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

Definition at line 2290 of file heap.c.

2297 {
2298  List *cookedConstraints = NIL;
2299  TupleDesc tupleDesc;
2300  TupleConstr *oldconstr;
2301  int numoldchecks;
2302  ParseState *pstate;
2303  ParseNamespaceItem *nsitem;
2304  int numchecks;
2305  List *checknames;
2306  List *nnnames;
2307  ListCell *cell;
2308  Node *expr;
2309  CookedConstraint *cooked;
2310 
2311  /*
2312  * Get info about existing constraints.
2313  */
2314  tupleDesc = RelationGetDescr(rel);
2315  oldconstr = tupleDesc->constr;
2316  if (oldconstr)
2317  numoldchecks = oldconstr->num_check;
2318  else
2319  numoldchecks = 0;
2320 
2321  /*
2322  * Create a dummy ParseState and insert the target relation as its sole
2323  * rangetable entry. We need a ParseState for transformExpr.
2324  */
2325  pstate = make_parsestate(NULL);
2326  pstate->p_sourcetext = queryString;
2327  nsitem = addRangeTableEntryForRelation(pstate,
2328  rel,
2330  NULL,
2331  false,
2332  true);
2333  addNSItemToQuery(pstate, nsitem, true, true, true);
2334 
2335  /*
2336  * Process column default expressions.
2337  */
2338  foreach(cell, newColDefaults)
2339  {
2340  RawColumnDefault *colDef = (RawColumnDefault *) lfirst(cell);
2341  Form_pg_attribute atp = TupleDescAttr(rel->rd_att, colDef->attnum - 1);
2342  Oid defOid;
2343 
2344  expr = cookDefault(pstate, colDef->raw_default,
2345  atp->atttypid, atp->atttypmod,
2346  NameStr(atp->attname),
2347  atp->attgenerated);
2348 
2349  /*
2350  * If the expression is just a NULL constant, we do not bother to make
2351  * an explicit pg_attrdef entry, since the default behavior is
2352  * equivalent. This applies to column defaults, but not for
2353  * generation expressions.
2354  *
2355  * Note a nonobvious property of this test: if the column is of a
2356  * domain type, what we'll get is not a bare null Const but a
2357  * CoerceToDomain expr, so we will not discard the default. This is
2358  * critical because the column default needs to be retained to
2359  * override any default that the domain might have.
2360  */
2361  if (expr == NULL ||
2362  (!colDef->generated &&
2363  IsA(expr, Const) &&
2364  castNode(Const, expr)->constisnull))
2365  continue;
2366 
2367  /* If the DEFAULT is volatile we cannot use a missing value */
2368  if (colDef->missingMode && contain_volatile_functions((Node *) expr))
2369  colDef->missingMode = false;
2370 
2371  defOid = StoreAttrDefault(rel, colDef->attnum, expr, is_internal,
2372  colDef->missingMode);
2373 
2374  cooked = (CookedConstraint *) palloc(sizeof(CookedConstraint));
2375  cooked->contype = CONSTR_DEFAULT;
2376  cooked->conoid = defOid;
2377  cooked->name = NULL;
2378  cooked->attnum = colDef->attnum;
2379  cooked->expr = expr;
2380  cooked->skip_validation = false;
2381  cooked->is_local = is_local;
2382  cooked->inhcount = is_local ? 0 : 1;
2383  cooked->is_no_inherit = false;
2384  cookedConstraints = lappend(cookedConstraints, cooked);
2385  }
2386 
2387  /*
2388  * Process constraint expressions.
2389  */
2390  numchecks = numoldchecks;
2391  checknames = NIL;
2392  nnnames = NIL;
2393  foreach(cell, newConstraints)
2394  {
2395  Constraint *cdef = (Constraint *) lfirst(cell);
2396  Oid constrOid;
2397 
2398  if (cdef->contype == CONSTR_CHECK)
2399  {
2400  char *ccname;
2401 
2402  if (cdef->raw_expr != NULL)
2403  {
2404  Assert(cdef->cooked_expr == NULL);
2405 
2406  /*
2407  * Transform raw parsetree to executable expression, and
2408  * verify it's valid as a CHECK constraint.
2409  */
2410  expr = cookConstraint(pstate, cdef->raw_expr,
2412  }
2413  else
2414  {
2415  Assert(cdef->cooked_expr != NULL);
2416 
2417  /*
2418  * Here, we assume the parser will only pass us valid CHECK
2419  * expressions, so we do no particular checking.
2420  */
2421  expr = stringToNode(cdef->cooked_expr);
2422  }
2423 
2424  /*
2425  * Check name uniqueness, or generate a name if none was given.
2426  */
2427  if (cdef->conname != NULL)
2428  {
2429  ListCell *cell2;
2430 
2431  ccname = cdef->conname;
2432  /* Check against other new constraints */
2433  /* Needed because we don't do CommandCounterIncrement in loop */
2434  foreach(cell2, checknames)
2435  {
2436  if (strcmp((char *) lfirst(cell2), ccname) == 0)
2437  ereport(ERROR,
2439  errmsg("check constraint \"%s\" already exists",
2440  ccname)));
2441  }
2442 
2443  /* save name for future checks */
2444  checknames = lappend(checknames, ccname);
2445 
2446  /*
2447  * Check against pre-existing constraints. If we are allowed
2448  * to merge with an existing constraint, there's no more to do
2449  * here. (We omit the duplicate constraint from the result,
2450  * which is what ATAddCheckConstraint wants.)
2451  */
2452  if (MergeWithExistingConstraint(rel, ccname, expr,
2453  allow_merge, is_local,
2454  cdef->initially_valid,
2455  cdef->is_no_inherit))
2456  continue;
2457  }
2458  else
2459  {
2460  /*
2461  * When generating a name, we want to create "tab_col_check"
2462  * for a column constraint and "tab_check" for a table
2463  * constraint. We no longer have any info about the syntactic
2464  * positioning of the constraint phrase, so we approximate
2465  * this by seeing whether the expression references more than
2466  * one column. (If the user played by the rules, the result
2467  * is the same...)
2468  *
2469  * Note: pull_var_clause() doesn't descend into sublinks, but
2470  * we eliminated those above; and anyway this only needs to be
2471  * an approximate answer.
2472  */
2473  List *vars;
2474  char *colname;
2475 
2476  vars = pull_var_clause(expr, 0);
2477 
2478  /* eliminate duplicates */
2479  vars = list_union(NIL, vars);
2480 
2481  if (list_length(vars) == 1)
2482  colname = get_attname(RelationGetRelid(rel),
2483  ((Var *) linitial(vars))->varattno,
2484  true);
2485  else
2486  colname = NULL;
2487 
2489  colname,
2490  "check",
2491  RelationGetNamespace(rel),
2492  checknames);
2493 
2494  /* save name for future checks */
2495  checknames = lappend(checknames, ccname);
2496  }
2497 
2498  /*
2499  * OK, store it.
2500  */
2501  constrOid =
2502  StoreRelCheck(rel, ccname, expr, cdef->initially_valid, is_local,
2503  is_local ? 0 : 1, cdef->is_no_inherit, is_internal);
2504 
2505  numchecks++;
2506 
2507  cooked = (CookedConstraint *) palloc(sizeof(CookedConstraint));
2508  cooked->contype = CONSTR_CHECK;
2509  cooked->conoid = constrOid;
2510  cooked->name = ccname;
2511  cooked->attnum = 0;
2512  cooked->expr = expr;
2513  cooked->skip_validation = cdef->skip_validation;
2514  cooked->is_local = is_local;
2515  cooked->inhcount = is_local ? 0 : 1;
2516  cooked->is_no_inherit = cdef->is_no_inherit;
2517  cookedConstraints = lappend(cookedConstraints, cooked);
2518  }
2519  else if (cdef->contype == CONSTR_NOTNULL)
2520  {
2521  CookedConstraint *nncooked;
2522  AttrNumber colnum;
2523  char *nnname;
2524 
2525  /* Determine which column to modify */
2526  colnum = get_attnum(RelationGetRelid(rel), strVal(linitial(cdef->keys)));
2527  if (colnum == InvalidAttrNumber) /* shouldn't happen */
2528  elog(ERROR, "cache lookup failed for attribute \"%s\" of relation %u",
2529  strVal(linitial(cdef->keys)), RelationGetRelid(rel));
2530 
2531  /*
2532  * If the column already has a not-null constraint, we need only
2533  * update its catalog status and we're done.
2534  */
2536  cdef->inhcount, cdef->is_no_inherit))
2537  continue;
2538 
2539  /*
2540  * If a constraint name is specified, check that it isn't already
2541  * used. Otherwise, choose a non-conflicting one ourselves.
2542  */
2543  if (cdef->conname)
2544  {
2546  RelationGetRelid(rel),
2547  cdef->conname))
2548  ereport(ERROR,
2550  errmsg("constraint \"%s\" for relation \"%s\" already exists",
2551  cdef->conname, RelationGetRelationName(rel)));
2552  nnname = cdef->conname;
2553  }
2554  else
2556  strVal(linitial(cdef->keys)),
2557  "not_null",
2558  RelationGetNamespace(rel),
2559  nnnames);
2560  nnnames = lappend(nnnames, nnname);
2561 
2562  constrOid =
2563  StoreRelNotNull(rel, nnname, colnum,
2564  cdef->initially_valid,
2565  cdef->inhcount == 0,
2566  cdef->inhcount,
2567  cdef->is_no_inherit);
2568 
2569  nncooked = (CookedConstraint *) palloc(sizeof(CookedConstraint));
2570  nncooked->contype = CONSTR_NOTNULL;
2571  nncooked->conoid = constrOid;
2572  nncooked->name = nnname;
2573  nncooked->attnum = colnum;
2574  nncooked->expr = NULL;
2575  nncooked->skip_validation = cdef->skip_validation;
2576  nncooked->is_local = is_local;
2577  nncooked->inhcount = cdef->inhcount;
2578  nncooked->is_no_inherit = cdef->is_no_inherit;
2579 
2580  cookedConstraints = lappend(cookedConstraints, nncooked);
2581  }
2582  }
2583 
2584  /*
2585  * Update the count of constraints in the relation's pg_class tuple. We do
2586  * this even if there was no change, in order to ensure that an SI update
2587  * message is sent out for the pg_class tuple, which will force other
2588  * backends to rebuild their relcache entries for the rel. (This is
2589  * critical if we added defaults but not constraints.)
2590  */
2591  SetRelationNumChecks(rel, numchecks);
2592 
2593  return cookedConstraints;
2594 }
int16 AttrNumber
Definition: attnum.h:21
#define InvalidAttrNumber
Definition: attnum.h:23
#define NameStr(name)
Definition: c.h:735
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:483
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
static Oid StoreRelCheck(Relation rel, const char *ccname, Node *expr, bool is_validated, bool is_local, int inhcount, bool is_no_inherit, bool is_internal)
Definition: heap.c:2051
static bool MergeWithExistingConstraint(Relation rel, const char *ccname, Node *expr, bool allow_merge, bool is_local, bool is_initially_valid, bool is_no_inherit)
Definition: heap.c:2607
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:2156
Node * cookDefault(ParseState *pstate, Node *raw_default, Oid atttypid, int32 atttypmod, const char *attname, char attgenerated)
Definition: heap.c:3072
static void SetRelationNumChecks(Relation rel, int numchecks)
Definition: heap.c:2978
static Node * cookConstraint(ParseState *pstate, Node *raw_constraint, char *relname)
Definition: heap.c:3147
Assert(fmt[strlen(fmt) - 1] !='\n')
List * list_union(const List *list1, const List *list2)
Definition: list.c:1065
List * lappend(List *list, void *datum)
Definition: list.c:338
#define AccessShareLock
Definition: lockdefs.h:36
AttrNumber get_attnum(Oid relid, const char *attname)
Definition: lsyscache.c:857
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition: lsyscache.c:826
void * palloc(Size size)
Definition: mcxt.c:1226
#define IsA(nodeptr, _type_)
Definition: nodes.h:179
#define castNode(_type_, nodeptr)
Definition: nodes.h:197
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:44
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
Definition: parsenodes.h:2564
@ CONSTR_NOTNULL
Definition: parsenodes.h:2563
@ CONSTR_CHECK
Definition: parsenodes.h:2567
Oid StoreAttrDefault(Relation rel, AttrNumber attnum, Node *expr, bool is_internal, bool add_column_mode)
Definition: pg_attrdef.c:46
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
char * ChooseConstraintName(const char *name1, const char *name2, const char *label, Oid namespaceid, List *others)
bool ConstraintNameIsUsed(ConstraintCategory conCat, Oid objId, const char *conname)
bool AdjustNotNullInheritance1(Oid relid, AttrNumber attnum, int count, bool is_no_inherit)
@ CONSTRAINT_RELATION
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
#define linitial(l)
Definition: pg_list.h:178
unsigned int Oid
Definition: postgres_ext.h:31
void * stringToNode(const char *str)
Definition: read.c:90
#define RelationGetRelid(relation)
Definition: rel.h:504
#define RelationGetDescr(relation)
Definition: rel.h:530
#define RelationGetRelationName(relation)
Definition: rel.h:538
#define RelationGetNamespace(relation)
Definition: rel.h:545
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
List * keys
Definition: parsenodes.h:2614
ConstrType contype
Definition: parsenodes.h:2595
bool is_no_inherit
Definition: parsenodes.h:2604
char * cooked_expr
Definition: parsenodes.h:2606
bool initially_valid
Definition: parsenodes.h:2646
bool skip_validation
Definition: parsenodes.h:2645
Node * raw_expr
Definition: parsenodes.h:2605
char * conname
Definition: parsenodes.h:2598
Oid conoid
Definition: heap.h:39
char * name
Definition: heap.h:40
AttrNumber attnum
Definition: heap.h:41
bool skip_validation
Definition: heap.h:43
bool is_no_inherit
Definition: heap.h:46
int inhcount
Definition: heap.h:45
bool is_local
Definition: heap.h:44
ConstrType contype
Definition: heap.h:37
Node * expr
Definition: heap.h:42
Definition: pg_list.h:54
Definition: nodes.h:129
const char * p_sourcetext
Definition: parse_node.h:192
Node * raw_default
Definition: heap.h:30
AttrNumber attnum
Definition: heap.h:29
char generated
Definition: heap.h:32
bool missingMode
Definition: heap.h:31
TupleDesc rd_att
Definition: rel.h:112
uint16 num_check
Definition: tupdesc.h:43
TupleConstr * constr
Definition: tupdesc.h:85
Definition: primnodes.h:226
Definition: regcomp.c:281
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define strVal(v)
Definition: value.h:82
List * pull_var_clause(Node *node, int flags)
Definition: var.c:607

References AccessShareLock, addNSItemToQuery(), addRangeTableEntryForRelation(), AdjustNotNullInheritance1(), Assert(), RawColumnDefault::attnum, CookedConstraint::attnum, castNode, ChooseConstraintName(), Constraint::conname, CookedConstraint::conoid, TupleDescData::constr, CONSTR_CHECK, CONSTR_DEFAULT, CONSTR_NOTNULL, CONSTRAINT_RELATION, ConstraintNameIsUsed(), contain_volatile_functions(), CookedConstraint::contype, Constraint::contype, cookConstraint(), cookDefault(), Constraint::cooked_expr, elog(), ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, CookedConstraint::expr, RawColumnDefault::generated, get_attname(), get_attnum(), CookedConstraint::inhcount, Constraint::inhcount, Constraint::initially_valid, InvalidAttrNumber, CookedConstraint::is_local, CookedConstraint::is_no_inherit, Constraint::is_no_inherit, IsA, Constraint::keys, lappend(), lfirst, linitial, list_length(), list_union(), make_parsestate(), MergeWithExistingConstraint(), RawColumnDefault::missingMode, CookedConstraint::name, NameStr, NIL, TupleConstr::num_check, ParseState::p_sourcetext, palloc(), pull_var_clause(), RawColumnDefault::raw_default, Constraint::raw_expr, RelationData::rd_att, RelationGetDescr, RelationGetNamespace, RelationGetRelationName, RelationGetRelid, SetRelationNumChecks(), CookedConstraint::skip_validation, Constraint::skip_validation, StoreAttrDefault(), StoreRelCheck(), StoreRelNotNull(), stringToNode(), strVal, and TupleDescAttr.

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

◆ AddRelationNotNullConstraints()

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

Definition at line 2785 of file heap.c.

2787 {
2788  List *givennames;
2789  List *nnnames;
2790  List *nncols = NIL;
2791  ListCell *lc;
2792 
2793  /*
2794  * We track two lists of names: nnnames keeps all the constraint names,
2795  * givennames tracks user-generated names. The distinction is important,
2796  * because we must raise error for user-generated name conflicts, but for
2797  * system-generated name conflicts we just generate another.
2798  */
2799  nnnames = NIL;
2800  givennames = NIL;
2801 
2802  /*
2803  * First, create all not-null constraints that are directly specified by
2804  * the user. Note that inheritance might have given us another source for
2805  * each, so we must scan the old_notnulls list and increment inhcount for
2806  * each element with identical attnum. We delete from there any element
2807  * that we process.
2808  */
2809  foreach(lc, constraints)
2810  {
2811  Constraint *constr = lfirst_node(Constraint, lc);
2813  char *conname;
2814  bool is_local = true;
2815  int inhcount = 0;
2816  ListCell *lc2;
2817 
2818  Assert(constr->contype == CONSTR_NOTNULL);
2819 
2821  strVal(linitial(constr->keys)));
2822 
2823  /*
2824  * Search in the list of inherited constraints for any entries on the
2825  * same column.
2826  */
2827  foreach(lc2, old_notnulls)
2828  {
2829  CookedConstraint *old = (CookedConstraint *) lfirst(lc2);
2830 
2831  if (old->attnum == attnum)
2832  {
2833  /*
2834  * If we get a constraint from the parent, having a local NO
2835  * INHERIT one doesn't work.
2836  */
2837  if (constr->is_no_inherit)
2838  ereport(ERROR,
2839  (errcode(ERRCODE_DATATYPE_MISMATCH),
2840  errmsg("cannot define not-null constraint on column \"%s\" with NO INHERIT",
2841  strVal(linitial(constr->keys))),
2842  errdetail("The column has an inherited not-null constraint.")));
2843 
2844  inhcount++;
2845  old_notnulls = foreach_delete_current(old_notnulls, lc2);
2846  }
2847  }
2848 
2849  /*
2850  * Determine a constraint name, which may have been specified by the
2851  * user, or raise an error if a conflict exists with another
2852  * user-specified name.
2853  */
2854  if (constr->conname)
2855  {
2856  foreach(lc2, givennames)
2857  {
2858  if (strcmp(lfirst(lc2), constr->conname) == 0)
2859  ereport(ERROR,
2861  errmsg("constraint \"%s\" for relation \"%s\" already exists",
2862  constr->conname,
2863  RelationGetRelationName(rel)));
2864  }
2865 
2866  conname = constr->conname;
2867  givennames = lappend(givennames, conname);
2868  }
2869  else
2872  attnum, false),
2873  "not_null",
2874  RelationGetNamespace(rel),
2875  nnnames);
2876  nnnames = lappend(nnnames, conname);
2877 
2878  StoreRelNotNull(rel, conname,
2879  attnum, true, is_local,
2880  inhcount, constr->is_no_inherit);
2881 
2882  nncols = lappend_int(nncols, attnum);
2883  }
2884 
2885  /*
2886  * If any column remains in the old_notnulls list, we must create a not-
2887  * null constraint marked not-local. Because multiple parents could
2888  * specify a not-null constraint for the same column, we must count how
2889  * many there are and add to the original inhcount accordingly, deleting
2890  * elements we've already processed. We sort the list to make it easy.
2891  *
2892  * We don't use foreach() here because we have two nested loops over the
2893  * constraint list, with possible element deletions in the inner one. If
2894  * we used foreach_delete_current() it could only fix up the state of one
2895  * of the loops, so it seems cleaner to use looping over list indexes for
2896  * both loops. Note that any deletion will happen beyond where the outer
2897  * loop is, so its index never needs adjustment.
2898  */
2899  list_sort(old_notnulls, list_cookedconstr_attnum_cmp);
2900  for (int outerpos = 0; outerpos < list_length(old_notnulls); outerpos++)
2901  {
2902  CookedConstraint *cooked;
2903  char *conname = NULL;
2904  int add_inhcount = 0;
2905  ListCell *lc2;
2906 
2907  cooked = (CookedConstraint *) list_nth(old_notnulls, outerpos);
2908  Assert(cooked->contype == CONSTR_NOTNULL);
2909 
2910  /*
2911  * Preserve the first non-conflicting constraint name we come across,
2912  * if any
2913  */
2914  if (conname == NULL && cooked->name)
2915  conname = cooked->name;
2916 
2917  for (int restpos = outerpos + 1; restpos < list_length(old_notnulls);)
2918  {
2919  CookedConstraint *other;
2920 
2921  other = (CookedConstraint *) list_nth(old_notnulls, restpos);
2922  if (other->attnum == cooked->attnum)
2923  {
2924  if (conname == NULL && other->name)
2925  conname = other->name;
2926 
2927  add_inhcount++;
2928  old_notnulls = list_delete_nth_cell(old_notnulls, restpos);
2929  }
2930  else
2931  restpos++;
2932  }
2933 
2934  /* If we got a name, make sure it isn't one we've already used */
2935  if (conname != NULL)
2936  {
2937  foreach(lc2, nnnames)
2938  {
2939  if (strcmp(lfirst(lc2), conname) == 0)
2940  {
2941  conname = NULL;
2942  break;
2943  }
2944  }
2945  }
2946 
2947  /* and choose a name, if needed */
2948  if (conname == NULL)
2951  cooked->attnum, false),
2952  "not_null",
2953  RelationGetNamespace(rel),
2954  nnnames);
2955  nnnames = lappend(nnnames, conname);
2956 
2957  StoreRelNotNull(rel, conname, cooked->attnum, true,
2958  cooked->is_local, cooked->inhcount + add_inhcount,
2959  cooked->is_no_inherit);
2960 
2961  nncols = lappend_int(nncols, cooked->attnum);
2962  }
2963 
2964  return nncols;
2965 }
int errdetail(const char *fmt,...)
Definition: elog.c:1202
static int list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
Definition: heap.c:2753
void list_sort(List *list, list_sort_comparator cmp)
Definition: list.c:1673
List * lappend_int(List *list, int datum)
Definition: list.c:356
List * list_delete_nth_cell(List *list, int n)
Definition: list.c:766
int16 attnum
Definition: pg_attribute.h:74
#define lfirst_node(type, lc)
Definition: pg_list.h:176
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
#define foreach_delete_current(lst, cell)
Definition: pg_list.h:390

References Assert(), CookedConstraint::attnum, attnum, ChooseConstraintName(), Constraint::conname, CONSTR_NOTNULL, CookedConstraint::contype, Constraint::contype, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errdetail(), errmsg(), ERROR, foreach_delete_current, get_attname(), get_attnum(), CookedConstraint::inhcount, CookedConstraint::is_local, CookedConstraint::is_no_inherit, Constraint::is_no_inherit, Constraint::keys, lappend(), lappend_int(), lfirst, lfirst_node, linitial, list_cookedconstr_attnum_cmp(), list_delete_nth_cell(), list_length(), list_nth(), list_sort(), CookedConstraint::name, NIL, RelationGetNamespace, RelationGetRelationName, RelationGetRelid, StoreRelNotNull(), and strVal.

Referenced by DefineRelation().

◆ CheckAttributeNamesTypes()

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

Definition at line 455 of file heap.c.

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

References attname, CheckAttributeType(), ereport, errcode(), errmsg(), ERROR, 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 547 of file heap.c.

551 {
552  char att_typtype = get_typtype(atttypid);
553  Oid att_typelem;
554 
555  if (att_typtype == TYPTYPE_PSEUDO)
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)
572  ereport(ERROR,
573  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
574  /* translator: first %s is an integer not a name */
575  errmsg("partition key column %s has pseudo-type %s",
576  attname, format_type_be(atttypid))));
577  else
578  ereport(ERROR,
579  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
580  errmsg("column \"%s\" has pseudo-type %s",
581  attname, format_type_be(atttypid))));
582  }
583  }
584  else if (att_typtype == TYPTYPE_DOMAIN)
585  {
586  /*
587  * If it's a domain, recurse to check its base type.
588  */
589  CheckAttributeType(attname, getBaseType(atttypid), attcollation,
590  containing_rowtypes,
591  flags);
592  }
593  else if (att_typtype == TYPTYPE_COMPOSITE)
594  {
595  /*
596  * For a composite type, recurse into its attributes.
597  */
598  Relation relation;
599  TupleDesc tupdesc;
600  int i;
601 
602  /*
603  * Check for self-containment. Eventually we might be able to allow
604  * this (just return without complaint, if so) but it's not clear how
605  * many other places would require anti-recursion defenses before it
606  * would be safe to allow tables to contain their own rowtype.
607  */
608  if (list_member_oid(containing_rowtypes, atttypid))
609  ereport(ERROR,
610  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
611  errmsg("composite type %s cannot be made a member of itself",
612  format_type_be(atttypid))));
613 
614  containing_rowtypes = lappend_oid(containing_rowtypes, atttypid);
615 
616  relation = relation_open(get_typ_typrelid(atttypid), AccessShareLock);
617 
618  tupdesc = RelationGetDescr(relation);
619 
620  for (i = 0; i < tupdesc->natts; i++)
621  {
622  Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
623 
624  if (attr->attisdropped)
625  continue;
626  CheckAttributeType(NameStr(attr->attname),
627  attr->atttypid, attr->attcollation,
628  containing_rowtypes,
629  flags & ~CHKATYPE_IS_PARTKEY);
630  }
631 
632  relation_close(relation, AccessShareLock);
633 
634  containing_rowtypes = list_delete_last(containing_rowtypes);
635  }
636  else if (att_typtype == TYPTYPE_RANGE)
637  {
638  /*
639  * If it's a range, recurse to check its subtype.
640  */
642  get_range_collation(atttypid),
643  containing_rowtypes,
644  flags);
645  }
646  else if (OidIsValid((att_typelem = get_element_type(atttypid))))
647  {
648  /*
649  * Must recurse into array types, too, in case they are composite.
650  */
651  CheckAttributeType(attname, att_typelem, attcollation,
652  containing_rowtypes,
653  flags);
654  }
655 
656  /*
657  * This might not be strictly invalid per SQL standard, but it is pretty
658  * useless, and it cannot be dumped, so we must disallow it.
659  */
660  if (!OidIsValid(attcollation) && type_is_collatable(atttypid))
661  {
662  if (flags & CHKATYPE_IS_PARTKEY)
663  ereport(ERROR,
664  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
665  /* translator: first %s is an integer not a name */
666  errmsg("no collation was derived for partition key column %s with collatable type %s",
667  attname, format_type_be(atttypid)),
668  errhint("Use the COLLATE clause to set the collation explicitly.")));
669  else
670  ereport(ERROR,
671  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
672  errmsg("no collation was derived for column \"%s\" with collatable type %s",
673  attname, format_type_be(atttypid)),
674  errhint("Use the COLLATE clause to set the collation explicitly.")));
675  }
676 }
#define OidIsValid(objectId)
Definition: c.h:764
int errhint(const char *fmt,...)
Definition: elog.c:1316
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
#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:374
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:721
List * list_delete_last(List *list)
Definition: list.c:956
Oid get_range_subtype(Oid rangeOid)
Definition: lsyscache.c:3389
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2741
Oid get_range_collation(Oid rangeOid)
Definition: lsyscache.c:3415
bool type_is_collatable(Oid typid)
Definition: lsyscache.c:3063
Oid get_typ_typrelid(Oid typid)
Definition: lsyscache.c:2713
char get_typtype(Oid typid)
Definition: lsyscache.c:2611
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2503
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48

References AccessShareLock, attname, CHKATYPE_ANYARRAY, CHKATYPE_ANYRECORD, CHKATYPE_IS_PARTKEY, ereport, errcode(), errhint(), errmsg(), ERROR, 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(), ComputePartitionAttrs(), and ConstructTupleDescriptor().

◆ cookDefault()

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

Definition at line 3072 of file heap.c.

3078 {
3079  Node *expr;
3080 
3081  Assert(raw_default != NULL);
3082 
3083  /*
3084  * Transform raw parsetree to executable expression.
3085  */
3086  expr = transformExpr(pstate, raw_default, attgenerated ? EXPR_KIND_GENERATED_COLUMN : EXPR_KIND_COLUMN_DEFAULT);
3087 
3088  if (attgenerated)
3089  {
3090  check_nested_generated(pstate, expr);
3091 
3092  if (contain_mutable_functions(expr))
3093  ereport(ERROR,
3094  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
3095  errmsg("generation expression is not immutable")));
3096  }
3097  else
3098  {
3099  /*
3100  * For a default expression, transformExpr() should have rejected
3101  * column references.
3102  */
3103  Assert(!contain_var_clause(expr));
3104  }
3105 
3106  /*
3107  * Coerce the expression to the correct type and typmod, if given. This
3108  * should match the parser's processing of non-defaulted expressions ---
3109  * see transformAssignedExpr().
3110  */
3111  if (OidIsValid(atttypid))
3112  {
3113  Oid type_id = exprType(expr);
3114 
3115  expr = coerce_to_target_type(pstate, expr, type_id,
3116  atttypid, atttypmod,
3119  -1);
3120  if (expr == NULL)
3121  ereport(ERROR,
3122  (errcode(ERRCODE_DATATYPE_MISMATCH),
3123  errmsg("column \"%s\" is of type %s"
3124  " but default expression is of type %s",
3125  attname,
3126  format_type_be(atttypid),
3127  format_type_be(type_id)),
3128  errhint("You will need to rewrite or cast the expression.")));
3129  }
3130 
3131  /*
3132  * Finally, take care of collations in the finished expression.
3133  */
3134  assign_expr_collations(pstate, expr);
3135 
3136  return expr;
3137 }
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:367
static void check_nested_generated(ParseState *pstate, Node *node)
Definition: heap.c:3054
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:43
Node * coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype, Oid targettype, int32 targettypmod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:78
void assign_expr_collations(ParseState *pstate, Node *expr)
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:110
@ EXPR_KIND_COLUMN_DEFAULT
Definition: parse_node.h:69
@ EXPR_KIND_GENERATED_COLUMN
Definition: parse_node.h:82
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:663
@ COERCION_ASSIGNMENT
Definition: primnodes.h:642
bool contain_var_clause(Node *node)
Definition: var.c:403

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

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

◆ CopyStatistics()

void CopyStatistics ( Oid  fromrelid,
Oid  torelid 
)

Definition at line 3185 of file heap.c.

3186 {
3187  HeapTuple tup;
3188  SysScanDesc scan;
3189  ScanKeyData key[1];
3190  Relation statrel;
3191  CatalogIndexState indstate = NULL;
3192 
3193  statrel = table_open(StatisticRelationId, RowExclusiveLock);
3194 
3195  /* Now search for stat records */
3196  ScanKeyInit(&key[0],
3197  Anum_pg_statistic_starelid,
3198  BTEqualStrategyNumber, F_OIDEQ,
3199  ObjectIdGetDatum(fromrelid));
3200 
3201  scan = systable_beginscan(statrel, StatisticRelidAttnumInhIndexId,
3202  true, NULL, 1, key);
3203 
3204  while (HeapTupleIsValid((tup = systable_getnext(scan))))
3205  {
3206  Form_pg_statistic statform;
3207 
3208  /* make a modifiable copy */
3209  tup = heap_copytuple(tup);
3210  statform = (Form_pg_statistic) GETSTRUCT(tup);
3211 
3212  /* update the copy of the tuple and insert it */
3213  statform->starelid = torelid;
3214 
3215  /* fetch index information when we know we need it */
3216  if (indstate == NULL)
3217  indstate = CatalogOpenIndexes(statrel);
3218 
3219  CatalogTupleInsertWithInfo(statrel, tup, indstate);
3220 
3221  heap_freetuple(tup);
3222  }
3223 
3224  systable_endscan(scan);
3225 
3226  if (indstate != NULL)
3227  CatalogCloseIndexes(indstate);
3228  table_close(statrel, RowExclusiveLock);
3229 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:599
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:506
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:387
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:768
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1426
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
void CatalogTupleInsertWithInfo(Relation heapRel, HeapTuple tup, CatalogIndexState indstate)
Definition: indexing.c:256
void CatalogCloseIndexes(CatalogIndexState indstate)
Definition: indexing.c:61
CatalogIndexState CatalogOpenIndexes(Relation heapRel)
Definition: indexing.c:43
#define RowExclusiveLock
Definition: lockdefs.h:38
FormData_pg_statistic * Form_pg_statistic
Definition: pg_statistic.h:135
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define BTEqualStrategyNumber
Definition: stratnum.h:31
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40

References BTEqualStrategyNumber, CatalogCloseIndexes(), CatalogOpenIndexes(), CatalogTupleInsertWithInfo(), GETSTRUCT, heap_copytuple(), heap_freetuple(), HeapTupleIsValid, sort-test::key, 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 1565 of file heap.c.

1566 {
1567  Relation attrel;
1568  SysScanDesc scan;
1569  ScanKeyData key[1];
1570  HeapTuple atttup;
1571 
1572  /* Grab an appropriate lock on the pg_attribute relation */
1573  attrel = table_open(AttributeRelationId, RowExclusiveLock);
1574 
1575  /* Use the index to scan only attributes of the target relation */
1576  ScanKeyInit(&key[0],
1577  Anum_pg_attribute_attrelid,
1578  BTEqualStrategyNumber, F_OIDEQ,
1579  ObjectIdGetDatum(relid));
1580 
1581  scan = systable_beginscan(attrel, AttributeRelidNumIndexId, true,
1582  NULL, 1, key);
1583 
1584  /* Delete all the matching tuples */
1585  while ((atttup = systable_getnext(scan)) != NULL)
1586  CatalogTupleDelete(attrel, &atttup->t_self);
1587 
1588  /* Clean up after the scan */
1589  systable_endscan(scan);
1590  table_close(attrel, RowExclusiveLock);
1591 }
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:365
ItemPointerData t_self
Definition: htup.h:65

References BTEqualStrategyNumber, CatalogTupleDelete(), sort-test::key, ObjectIdGetDatum(), RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by heap_drop_with_catalog(), and index_drop().

◆ DeleteRelationTuple()

void DeleteRelationTuple ( Oid  relid)

Definition at line 1536 of file heap.c.

1537 {
1538  Relation pg_class_desc;
1539  HeapTuple tup;
1540 
1541  /* Grab an appropriate lock on the pg_class relation */
1542  pg_class_desc = table_open(RelationRelationId, RowExclusiveLock);
1543 
1544  tup = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
1545  if (!HeapTupleIsValid(tup))
1546  elog(ERROR, "cache lookup failed for relation %u", relid);
1547 
1548  /* delete the relation tuple from pg_class, and finish up */
1549  CatalogTupleDelete(pg_class_desc, &tup->t_self);
1550 
1551  ReleaseSysCache(tup);
1552 
1553  table_close(pg_class_desc, RowExclusiveLock);
1554 }
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:868
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:820
@ RELOID
Definition: syscache.h:89

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

Referenced by heap_drop_with_catalog(), and index_drop().

◆ DeleteSystemAttributeTuples()

void DeleteSystemAttributeTuples ( Oid  relid)

Definition at line 1602 of file heap.c.

1603 {
1604  Relation attrel;
1605  SysScanDesc scan;
1606  ScanKeyData key[2];
1607  HeapTuple atttup;
1608 
1609  /* Grab an appropriate lock on the pg_attribute relation */
1610  attrel = table_open(AttributeRelationId, RowExclusiveLock);
1611 
1612  /* Use the index to scan only system attributes of the target relation */
1613  ScanKeyInit(&key[0],
1614  Anum_pg_attribute_attrelid,
1615  BTEqualStrategyNumber, F_OIDEQ,
1616  ObjectIdGetDatum(relid));
1617  ScanKeyInit(&key[1],
1618  Anum_pg_attribute_attnum,
1619  BTLessEqualStrategyNumber, F_INT2LE,
1620  Int16GetDatum(0));
1621 
1622  scan = systable_beginscan(attrel, AttributeRelidNumIndexId, true,
1623  NULL, 2, key);
1624 
1625  /* Delete all the matching tuples */
1626  while ((atttup = systable_getnext(scan)) != NULL)
1627  CatalogTupleDelete(attrel, &atttup->t_self);
1628 
1629  /* Clean up after the scan */
1630  systable_endscan(scan);
1631  table_close(attrel, RowExclusiveLock);
1632 }
static Datum Int16GetDatum(int16 X)
Definition: postgres.h:172
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30

References BTEqualStrategyNumber, BTLessEqualStrategyNumber, CatalogTupleDelete(), Int16GetDatum(), sort-test::key, ObjectIdGetDatum(), RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, 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 288 of file heap.c.

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

References Assert(), ereport, errcode(), errdetail(), errmsg(), ERROR, 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 1091 of file heap.c.

1112 {
1113  Relation pg_class_desc;
1114  Relation new_rel_desc;
1115  Acl *relacl;
1116  Oid existing_relid;
1117  Oid old_type_oid;
1118  Oid new_type_oid;
1119 
1120  /* By default set to InvalidOid unless overridden by binary-upgrade */
1121  RelFileNumber relfilenumber = InvalidRelFileNumber;
1122  TransactionId relfrozenxid;
1123  MultiXactId relminmxid;
1124 
1125  pg_class_desc = table_open(RelationRelationId, RowExclusiveLock);
1126 
1127  /*
1128  * sanity checks
1129  */
1131 
1132  /*
1133  * Validate proposed tupdesc for the desired relkind. If
1134  * allow_system_table_mods is on, allow ANYARRAY to be used; this is a
1135  * hack to allow creating pg_statistic and cloning it during VACUUM FULL.
1136  */
1137  CheckAttributeNamesTypes(tupdesc, relkind,
1138  allow_system_table_mods ? CHKATYPE_ANYARRAY : 0);
1139 
1140  /*
1141  * This would fail later on anyway, if the relation already exists. But
1142  * by catching it here we can emit a nicer error message.
1143  */
1144  existing_relid = get_relname_relid(relname, relnamespace);
1145  if (existing_relid != InvalidOid)
1146  ereport(ERROR,
1147  (errcode(ERRCODE_DUPLICATE_TABLE),
1148  errmsg("relation \"%s\" already exists", relname)));
1149 
1150  /*
1151  * Since we are going to create a rowtype as well, also check for
1152  * collision with an existing type name. If there is one and it's an
1153  * autogenerated array, we can rename it out of the way; otherwise we can
1154  * at least give a good error message.
1155  */
1156  old_type_oid = GetSysCacheOid2(TYPENAMENSP, Anum_pg_type_oid,
1158  ObjectIdGetDatum(relnamespace));
1159  if (OidIsValid(old_type_oid))
1160  {
1161  if (!moveArrayTypeName(old_type_oid, relname, relnamespace))
1162  ereport(ERROR,
1164  errmsg("type \"%s\" already exists", relname),
1165  errhint("A relation has an associated type of the same name, "
1166  "so you must use a name that doesn't conflict "
1167  "with any existing type.")));
1168  }
1169 
1170  /*
1171  * Shared relations must be in pg_global (last-ditch check)
1172  */
1173  if (shared_relation && reltablespace != GLOBALTABLESPACE_OID)
1174  elog(ERROR, "shared relations must be placed in pg_global tablespace");
1175 
1176  /*
1177  * Allocate an OID for the relation, unless we were told what to use.
1178  *
1179  * The OID will be the relfilenumber as well, so make sure it doesn't
1180  * collide with either pg_class OIDs or existing physical files.
1181  */
1182  if (!OidIsValid(relid))
1183  {
1184  /* Use binary-upgrade override for pg_class.oid and relfilenumber */
1185  if (IsBinaryUpgrade)
1186  {
1187  /*
1188  * Indexes are not supported here; they use
1189  * binary_upgrade_next_index_pg_class_oid.
1190  */
1191  Assert(relkind != RELKIND_INDEX);
1192  Assert(relkind != RELKIND_PARTITIONED_INDEX);
1193 
1194  if (relkind == RELKIND_TOASTVALUE)
1195  {
1196  /* There might be no TOAST table, so we have to test for it. */
1198  {
1201 
1203  ereport(ERROR,
1204  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1205  errmsg("toast relfilenumber value not set when in binary upgrade mode")));
1206 
1209  }
1210  }
1211  else
1212  {
1214  ereport(ERROR,
1215  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1216  errmsg("pg_class heap OID value not set when in binary upgrade mode")));
1217 
1220 
1221  if (RELKIND_HAS_STORAGE(relkind))
1222  {
1224  ereport(ERROR,
1225  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1226  errmsg("relfilenumber value not set when in binary upgrade mode")));
1227 
1230  }
1231  }
1232  }
1233 
1234  if (!OidIsValid(relid))
1235  relid = GetNewRelFileNumber(reltablespace, pg_class_desc,
1236  relpersistence);
1237  }
1238 
1239  /*
1240  * Determine the relation's initial permissions.
1241  */
1242  if (use_user_acl)
1243  {
1244  switch (relkind)
1245  {
1246  case RELKIND_RELATION:
1247  case RELKIND_VIEW:
1248  case RELKIND_MATVIEW:
1249  case RELKIND_FOREIGN_TABLE:
1250  case RELKIND_PARTITIONED_TABLE:
1251  relacl = get_user_default_acl(OBJECT_TABLE, ownerid,
1252  relnamespace);
1253  break;
1254  case RELKIND_SEQUENCE:
1255  relacl = get_user_default_acl(OBJECT_SEQUENCE, ownerid,
1256  relnamespace);
1257  break;
1258  default:
1259  relacl = NULL;
1260  break;
1261  }
1262  }
1263  else
1264  relacl = NULL;
1265 
1266  /*
1267  * Create the relcache entry (mostly dummy at this point) and the physical
1268  * disk file. (If we fail further down, it's the smgr's responsibility to
1269  * remove the disk file again.)
1270  *
1271  * NB: Note that passing create_storage = true is correct even for binary
1272  * upgrade. The storage we create here will be replaced later, but we
1273  * need to have something on disk in the meanwhile.
1274  */
1275  new_rel_desc = heap_create(relname,
1276  relnamespace,
1277  reltablespace,
1278  relid,
1279  relfilenumber,
1280  accessmtd,
1281  tupdesc,
1282  relkind,
1283  relpersistence,
1284  shared_relation,
1285  mapped_relation,
1286  allow_system_table_mods,
1287  &relfrozenxid,
1288  &relminmxid,
1289  true);
1290 
1291  Assert(relid == RelationGetRelid(new_rel_desc));
1292 
1293  new_rel_desc->rd_rel->relrewrite = relrewrite;
1294 
1295  /*
1296  * Decide whether to create a pg_type entry for the relation's rowtype.
1297  * These types are made except where the use of a relation as such is an
1298  * implementation detail: toast tables, sequences and indexes.
1299  */
1300  if (!(relkind == RELKIND_SEQUENCE ||
1301  relkind == RELKIND_TOASTVALUE ||
1302  relkind == RELKIND_INDEX ||
1303  relkind == RELKIND_PARTITIONED_INDEX))
1304  {
1305  Oid new_array_oid;
1306  ObjectAddress new_type_addr;
1307  char *relarrayname;
1308 
1309  /*
1310  * We'll make an array over the composite type, too. For largely
1311  * historical reasons, the array type's OID is assigned first.
1312  */
1313  new_array_oid = AssignTypeArrayOid();
1314 
1315  /*
1316  * Make the pg_type entry for the composite type. The OID of the
1317  * composite type can be preselected by the caller, but if reltypeid
1318  * is InvalidOid, we'll generate a new OID for it.
1319  *
1320  * NOTE: we could get a unique-index failure here, in case someone
1321  * else is creating the same type name in parallel but hadn't
1322  * committed yet when we checked for a duplicate name above.
1323  */
1324  new_type_addr = AddNewRelationType(relname,
1325  relnamespace,
1326  relid,
1327  relkind,
1328  ownerid,
1329  reltypeid,
1330  new_array_oid);
1331  new_type_oid = new_type_addr.objectId;
1332  if (typaddress)
1333  *typaddress = new_type_addr;
1334 
1335  /* Now create the array type. */
1336  relarrayname = makeArrayTypeName(relname, relnamespace);
1337 
1338  TypeCreate(new_array_oid, /* force the type's OID to this */
1339  relarrayname, /* Array type name */
1340  relnamespace, /* Same namespace as parent */
1341  InvalidOid, /* Not composite, no relationOid */
1342  0, /* relkind, also N/A here */
1343  ownerid, /* owner's ID */
1344  -1, /* Internal size (varlena) */
1345  TYPTYPE_BASE, /* Not composite - typelem is */
1346  TYPCATEGORY_ARRAY, /* type-category (array) */
1347  false, /* array types are never preferred */
1348  DEFAULT_TYPDELIM, /* default array delimiter */
1349  F_ARRAY_IN, /* array input proc */
1350  F_ARRAY_OUT, /* array output proc */
1351  F_ARRAY_RECV, /* array recv (bin) proc */
1352  F_ARRAY_SEND, /* array send (bin) proc */
1353  InvalidOid, /* typmodin procedure - none */
1354  InvalidOid, /* typmodout procedure - none */
1355  F_ARRAY_TYPANALYZE, /* array analyze procedure */
1356  F_ARRAY_SUBSCRIPT_HANDLER, /* array subscript procedure */
1357  new_type_oid, /* array element type - the rowtype */
1358  true, /* yes, this is an array type */
1359  InvalidOid, /* this has no array type */
1360  InvalidOid, /* domain base type - irrelevant */
1361  NULL, /* default value - none */
1362  NULL, /* default binary representation */
1363  false, /* passed by reference */
1364  TYPALIGN_DOUBLE, /* alignment - must be the largest! */
1365  TYPSTORAGE_EXTENDED, /* fully TOASTable */
1366  -1, /* typmod */
1367  0, /* array dimensions for typBaseType */
1368  false, /* Type NOT NULL */
1369  InvalidOid); /* rowtypes never have a collation */
1370 
1371  pfree(relarrayname);
1372  }
1373  else
1374  {
1375  /* Caller should not be expecting a type to be created. */
1376  Assert(reltypeid == InvalidOid);
1377  Assert(typaddress == NULL);
1378 
1379  new_type_oid = InvalidOid;
1380  }
1381 
1382  /*
1383  * now create an entry in pg_class for the relation.
1384  *
1385  * NOTE: we could get a unique-index failure here, in case someone else is
1386  * creating the same relation name in parallel but hadn't committed yet
1387  * when we checked for a duplicate name above.
1388  */
1389  AddNewRelationTuple(pg_class_desc,
1390  new_rel_desc,
1391  relid,
1392  new_type_oid,
1393  reloftypeid,
1394  ownerid,
1395  relkind,
1396  relfrozenxid,
1397  relminmxid,
1398  PointerGetDatum(relacl),
1399  reloptions);
1400 
1401  /*
1402  * now add tuples to pg_attribute for the attributes in our new relation.
1403  */
1404  AddNewAttributeTuples(relid, new_rel_desc->rd_att, relkind);
1405 
1406  /*
1407  * Make a dependency link to force the relation to be deleted if its
1408  * namespace is. Also make a dependency link to its owner, as well as
1409  * dependencies for any roles mentioned in the default ACL.
1410  *
1411  * For composite types, these dependencies are tracked for the pg_type
1412  * entry, so we needn't record them here. Likewise, TOAST tables don't
1413  * need a namespace dependency (they live in a pinned namespace) nor an
1414  * owner dependency (they depend indirectly through the parent table), nor
1415  * should they have any ACL entries. The same applies for extension
1416  * dependencies.
1417  *
1418  * Also, skip this in bootstrap mode, since we don't make dependencies
1419  * while bootstrapping.
1420  */
1421  if (relkind != RELKIND_COMPOSITE_TYPE &&
1422  relkind != RELKIND_TOASTVALUE &&
1424  {
1425  ObjectAddress myself,
1426  referenced;
1427  ObjectAddresses *addrs;
1428 
1429  ObjectAddressSet(myself, RelationRelationId, relid);
1430 
1431  recordDependencyOnOwner(RelationRelationId, relid, ownerid);
1432 
1433  recordDependencyOnNewAcl(RelationRelationId, relid, 0, ownerid, relacl);
1434 
1435  recordDependencyOnCurrentExtension(&myself, false);
1436 
1437  addrs = new_object_addresses();
1438 
1439  ObjectAddressSet(referenced, NamespaceRelationId, relnamespace);
1440  add_exact_object_address(&referenced, addrs);
1441 
1442  if (reloftypeid)
1443  {
1444  ObjectAddressSet(referenced, TypeRelationId, reloftypeid);
1445  add_exact_object_address(&referenced, addrs);
1446  }
1447 
1448  /*
1449  * Make a dependency link to force the relation to be deleted if its
1450  * access method is.
1451  *
1452  * No need to add an explicit dependency for the toast table, as the
1453  * main table depends on it.
1454  */
1455  if (RELKIND_HAS_TABLE_AM(relkind) && relkind != RELKIND_TOASTVALUE)
1456  {
1457  ObjectAddressSet(referenced, AccessMethodRelationId, accessmtd);
1458  add_exact_object_address(&referenced, addrs);
1459  }
1460 
1462  free_object_addresses(addrs);
1463  }
1464 
1465  /* Post creation hook for new relation */
1466  InvokeObjectPostCreateHookArg(RelationRelationId, relid, 0, is_internal);
1467 
1468  /*
1469  * Store any supplied constraints and defaults.
1470  *
1471  * NB: this may do a CommandCounterIncrement and rebuild the relcache
1472  * entry, so the relation must be valid and self-consistent at this point.
1473  * In particular, there are not yet constraints and defaults anywhere.
1474  */
1475  StoreConstraints(new_rel_desc, cooked_constraints, is_internal);
1476 
1477  /*
1478  * If there's a special on-commit action, remember it
1479  */
1480  if (oncommit != ONCOMMIT_NOOP)
1481  register_on_commit_action(relid, oncommit);
1482 
1483  /*
1484  * ok, the relation has been cataloged, so close our relations and return
1485  * the OID of the newly created relation.
1486  */
1487  table_close(new_rel_desc, NoLock); /* do not unlock till end of xact */
1488  table_close(pg_class_desc, RowExclusiveLock);
1489 
1490  return relid;
1491 }
void recordDependencyOnNewAcl(Oid classId, Oid objectId, int32 objsubId, Oid ownerId, Acl *acl)
Definition: aclchk.c:4192
Acl * get_user_default_acl(ObjectType objtype, Oid ownerId, Oid nsp_oid)
Definition: aclchk.c:4116
TransactionId MultiXactId
Definition: c.h:651
uint32 TransactionId
Definition: c.h:641
RelFileNumber GetNewRelFileNumber(Oid reltablespace, Relation pg_class, char relpersistence)
Definition: catalog.c:502
void record_object_address_dependencies(const ObjectAddress *depender, ObjectAddresses *referenced, DependencyType behavior)
Definition: dependency.c:2790
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2532
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
Definition: dependency.c:2581
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2821
@ DEPENDENCY_NORMAL
Definition: dependency.h:33
bool IsBinaryUpgrade
Definition: globals.c:114
static void StoreConstraints(Relation rel, List *cooked_constraints, bool is_internal)
Definition: heap.c:2208
static void AddNewAttributeTuples(Oid new_rel_oid, TupleDesc tupdesc, char relkind)
Definition: heap.c:804
RelFileNumber binary_upgrade_next_heap_pg_class_relfilenumber
Definition: heap.c:81
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:955
RelFileNumber binary_upgrade_next_toast_pg_class_relfilenumber
Definition: heap.c:82
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:1013
void CheckAttributeNamesTypes(TupleDesc tupdesc, char relkind, int flags)
Definition: heap.c:455
Oid binary_upgrade_next_toast_pg_class_oid
Definition: heap.c:80
Oid binary_upgrade_next_heap_pg_class_oid
Definition: heap.c:79
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:288
#define NoLock
Definition: lockdefs.h:34
Oid get_relname_relid(const char *relname, Oid relnamespace)
Definition: lsyscache.c:1889
void pfree(void *pointer)
Definition: mcxt.c:1456
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:414
#define InvokeObjectPostCreateHookArg(classId, objectId, subId, is_internal)
Definition: objectaccess.h:175
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
@ OBJECT_SEQUENCE
Definition: parsenodes.h:2157
@ OBJECT_TABLE
Definition: parsenodes.h:2161
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:192
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:165
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:196
char * makeArrayTypeName(const char *typeName, Oid typeNamespace)
Definition: pg_type.c:817
bool moveArrayTypeName(Oid typeOid, const char *typeName, Oid typeNamespace)
Definition: pg_type.c:882
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:350
@ ONCOMMIT_NOOP
Definition: primnodes.h:49
Oid RelFileNumber
Definition: relpath.h:25
#define InvalidRelFileNumber
Definition: relpath.h:26
@ TYPENAMENSP
Definition: syscache.h:113
#define GetSysCacheOid2(cacheId, oidcol, key1, key2)
Definition: syscache.h:202
void register_on_commit_action(Oid relid, OnCommitAction action)
Definition: tablecmds.c:17425
Oid AssignTypeArrayOid(void)
Definition: typecmds.c:2400
#define DEFAULT_TYPDELIM
Definition: typecmds.h:22

References 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, free_object_addresses(), get_relname_relid(), get_user_default_acl(), GetNewRelFileNumber(), GetSysCacheOid2, heap_create(), InvalidOid, InvalidRelFileNumber, InvokeObjectPostCreateHookArg, IsBinaryUpgrade, IsBootstrapProcessingMode, IsNormalProcessingMode, makeArrayTypeName(), moveArrayTypeName(), new_object_addresses(), NoLock, OBJECT_SEQUENCE, OBJECT_TABLE, ObjectAddressSet, ObjectAddress::objectId, ObjectIdGetDatum(), OidIsValid, ONCOMMIT_NOOP, pfree(), PointerGetDatum(), RelationData::rd_att, RelationData::rd_rel, record_object_address_dependencies(), recordDependencyOnCurrentExtension(), recordDependencyOnNewAcl(), recordDependencyOnOwner(), register_on_commit_action(), RelationGetRelid, RelFileNumberIsValid, relname, RowExclusiveLock, StoreConstraints(), table_close(), table_open(), TypeCreate(), and TYPENAMENSP.

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

◆ heap_drop_with_catalog()

void heap_drop_with_catalog ( Oid  relid)

Definition at line 1742 of file heap.c.

1743 {
1744  Relation rel;
1745  HeapTuple tuple;
1746  Oid parentOid = InvalidOid,
1747  defaultPartOid = InvalidOid;
1748 
1749  /*
1750  * To drop a partition safely, we must grab exclusive lock on its parent,
1751  * because another backend might be about to execute a query on the parent
1752  * table. If it relies on previously cached partition descriptor, then it
1753  * could attempt to access the just-dropped relation as its partition. We
1754  * must therefore take a table lock strong enough to prevent all queries
1755  * on the table from proceeding until we commit and send out a
1756  * shared-cache-inval notice that will make them update their partition
1757  * descriptors.
1758  */
1759  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
1760  if (!HeapTupleIsValid(tuple))
1761  elog(ERROR, "cache lookup failed for relation %u", relid);
1762  if (((Form_pg_class) GETSTRUCT(tuple))->relispartition)
1763  {
1764  /*
1765  * We have to lock the parent if the partition is being detached,
1766  * because it's possible that some query still has a partition
1767  * descriptor that includes this partition.
1768  */
1769  parentOid = get_partition_parent(relid, true);
1771 
1772  /*
1773  * If this is not the default partition, dropping it will change the
1774  * default partition's partition constraint, so we must lock it.
1775  */
1776  defaultPartOid = get_default_partition_oid(parentOid);
1777  if (OidIsValid(defaultPartOid) && relid != defaultPartOid)
1778  LockRelationOid(defaultPartOid, AccessExclusiveLock);
1779  }
1780 
1781  ReleaseSysCache(tuple);
1782 
1783  /*
1784  * Open and lock the relation.
1785  */
1786  rel = relation_open(relid, AccessExclusiveLock);
1787 
1788  /*
1789  * There can no longer be anyone *else* touching the relation, but we
1790  * might still have open queries or cursors, or pending trigger events, in
1791  * our own session.
1792  */
1793  CheckTableNotInUse(rel, "DROP TABLE");
1794 
1795  /*
1796  * This effectively deletes all rows in the table, and may be done in a
1797  * serializable transaction. In that case we must record a rw-conflict in
1798  * to this transaction from each transaction holding a predicate lock on
1799  * the table.
1800  */
1802 
1803  /*
1804  * Delete pg_foreign_table tuple first.
1805  */
1806  if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
1807  {
1808  Relation ftrel;
1809  HeapTuple fttuple;
1810 
1811  ftrel = table_open(ForeignTableRelationId, RowExclusiveLock);
1812 
1814  if (!HeapTupleIsValid(fttuple))
1815  elog(ERROR, "cache lookup failed for foreign table %u", relid);
1816 
1817  CatalogTupleDelete(ftrel, &fttuple->t_self);
1818 
1819  ReleaseSysCache(fttuple);
1820  table_close(ftrel, RowExclusiveLock);
1821  }
1822 
1823  /*
1824  * If a partitioned table, delete the pg_partitioned_table tuple.
1825  */
1826  if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1828 
1829  /*
1830  * If the relation being dropped is the default partition itself,
1831  * invalidate its entry in pg_partitioned_table.
1832  */
1833  if (relid == defaultPartOid)
1835 
1836  /*
1837  * Schedule unlinking of the relation's physical files at commit.
1838  */
1839  if (RELKIND_HAS_STORAGE(rel->rd_rel->relkind))
1840  RelationDropStorage(rel);
1841 
1842  /* ensure that stats are dropped if transaction commits */
1843  pgstat_drop_relation(rel);
1844 
1845  /*
1846  * Close relcache entry, but *keep* AccessExclusiveLock on the relation
1847  * until transaction commit. This ensures no one else will try to do
1848  * something with the doomed relation.
1849  */
1850  relation_close(rel, NoLock);
1851 
1852  /*
1853  * Remove any associated relation synchronization states.
1854  */
1856 
1857  /*
1858  * Forget any ON COMMIT action for the rel
1859  */
1860  remove_on_commit_action(relid);
1861 
1862  /*
1863  * Flush the relation from the relcache. We want to do this before
1864  * starting to remove catalog entries, just to be certain that no relcache
1865  * entry rebuild will happen partway through. (That should not really
1866  * matter, since we don't do CommandCounterIncrement here, but let's be
1867  * safe.)
1868  */
1869  RelationForgetRelation(relid);
1870 
1871  /*
1872  * remove inheritance information
1873  */
1875 
1876  /*
1877  * delete statistics
1878  */
1879  RemoveStatistics(relid, 0);
1880 
1881  /*
1882  * delete attribute tuples
1883  */
1884  DeleteAttributeTuples(relid);
1885 
1886  /*
1887  * delete relation tuple
1888  */
1889  DeleteRelationTuple(relid);
1890 
1891  if (OidIsValid(parentOid))
1892  {
1893  /*
1894  * If this is not the default partition, the partition constraint of
1895  * the default partition has changed to include the portion of the key
1896  * space previously covered by the dropped partition.
1897  */
1898  if (OidIsValid(defaultPartOid) && relid != defaultPartOid)
1899  CacheInvalidateRelcacheByRelid(defaultPartOid);
1900 
1901  /*
1902  * Invalidate the parent's relcache so that the partition is no longer
1903  * included in its partition descriptor.
1904  */
1905  CacheInvalidateRelcacheByRelid(parentOid);
1906  /* keep the lock */
1907  }
1908 }
void DeleteRelationTuple(Oid relid)
Definition: heap.c:1536
void DeleteAttributeTuples(Oid relid)
Definition: heap.c:1565
void RemoveStatistics(Oid relid, AttrNumber attnum)
Definition: heap.c:3238
static void RelationRemoveInheritance(Oid relid)
Definition: heap.c:1503
void RemovePartitionKeyByRelId(Oid relid)
Definition: heap.c:3765
void CacheInvalidateRelcacheByRelid(Oid relid)
Definition: inval.c:1422
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:109
#define AccessExclusiveLock
Definition: lockdefs.h:43
void update_default_partition_oid(Oid parentId, Oid defaultPartId)
Definition: partition.c:339
Oid get_default_partition_oid(Oid parentId)
Definition: partition.c:314
Oid get_partition_parent(Oid relid, bool even_if_detached)
Definition: partition.c:54
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
void RemoveSubscriptionRel(Oid subid, Oid relid)
void pgstat_drop_relation(Relation rel)
void CheckTableForSerializableConflictIn(Relation relation)
Definition: predicate.c:4353
void RelationForgetRelation(Oid rid)
Definition: relcache.c:2834
void RelationDropStorage(Relation rel)
Definition: storage.c:205
@ FOREIGNTABLEREL
Definition: syscache.h:65
void CheckTableNotInUse(Relation rel, const char *stmt)
Definition: tablecmds.c:4183
void remove_on_commit_action(Oid relid)
Definition: tablecmds.c:17461

References AccessExclusiveLock, CacheInvalidateRelcacheByRelid(), CatalogTupleDelete(), CheckTableForSerializableConflictIn(), CheckTableNotInUse(), DeleteAttributeTuples(), DeleteRelationTuple(), elog(), ERROR, FOREIGNTABLEREL, 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(), RELOID, remove_on_commit_action(), RemovePartitionKeyByRelId(), RemoveStatistics(), RemoveSubscriptionRel(), RowExclusiveLock, SearchSysCache1(), HeapTupleData::t_self, table_close(), table_open(), and update_default_partition_oid().

Referenced by doDeletion().

◆ heap_truncate()

void heap_truncate ( List relids)

Definition at line 3333 of file heap.c.

3334 {
3335  List *relations = NIL;
3336  ListCell *cell;
3337 
3338  /* Open relations for processing, and grab exclusive access on each */
3339  foreach(cell, relids)
3340  {
3341  Oid rid = lfirst_oid(cell);
3342  Relation rel;
3343 
3344  rel = table_open(rid, AccessExclusiveLock);
3345  relations = lappend(relations, rel);
3346  }
3347 
3348  /* Don't allow truncate on tables that are referenced by foreign keys */
3349  heap_truncate_check_FKs(relations, true);
3350 
3351  /* OK to do it */
3352  foreach(cell, relations)
3353  {
3354  Relation rel = lfirst(cell);
3355 
3356  /* Truncate the relation */
3357  heap_truncate_one_rel(rel);
3358 
3359  /* Close the relation, but keep exclusive lock on it until commit */
3360  table_close(rel, NoLock);
3361  }
3362 }
void heap_truncate_check_FKs(List *relations, bool tempTables)
Definition: heap.c:3418
void heap_truncate_one_rel(Relation rel)
Definition: heap.c:3374
#define lfirst_oid(lc)
Definition: pg_list.h:174

References AccessExclusiveLock, 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 3418 of file heap.c.

3419 {
3420  List *oids = NIL;
3421  List *dependents;
3422  ListCell *cell;
3423 
3424  /*
3425  * Build a list of OIDs of the interesting relations.
3426  *
3427  * If a relation has no triggers, then it can neither have FKs nor be
3428  * referenced by a FK from another table, so we can ignore it. For
3429  * partitioned tables, FKs have no triggers, so we must include them
3430  * anyway.
3431  */
3432  foreach(cell, relations)
3433  {
3434  Relation rel = lfirst(cell);
3435 
3436  if (rel->rd_rel->relhastriggers ||
3437  rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
3438  oids = lappend_oid(oids, RelationGetRelid(rel));
3439  }
3440 
3441  /*
3442  * Fast path: if no relation has triggers, none has FKs either.
3443  */
3444  if (oids == NIL)
3445  return;
3446 
3447  /*
3448  * Otherwise, must scan pg_constraint. We make one pass with all the
3449  * relations considered; if this finds nothing, then all is well.
3450  */
3451  dependents = heap_truncate_find_FKs(oids);
3452  if (dependents == NIL)
3453  return;
3454 
3455  /*
3456  * Otherwise we repeat the scan once per relation to identify a particular
3457  * pair of relations to complain about. This is pretty slow, but
3458  * performance shouldn't matter much in a failure path. The reason for
3459  * doing things this way is to ensure that the message produced is not
3460  * dependent on chance row locations within pg_constraint.
3461  */
3462  foreach(cell, oids)
3463  {
3464  Oid relid = lfirst_oid(cell);
3465  ListCell *cell2;
3466 
3467  dependents = heap_truncate_find_FKs(list_make1_oid(relid));
3468 
3469  foreach(cell2, dependents)
3470  {
3471  Oid relid2 = lfirst_oid(cell2);
3472 
3473  if (!list_member_oid(oids, relid2))
3474  {
3475  char *relname = get_rel_name(relid);
3476  char *relname2 = get_rel_name(relid2);
3477 
3478  if (tempTables)
3479  ereport(ERROR,
3480  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3481  errmsg("unsupported ON COMMIT and foreign key combination"),
3482  errdetail("Table \"%s\" references \"%s\", but they do not have the same ON COMMIT setting.",
3483  relname2, relname)));
3484  else
3485  ereport(ERROR,
3486  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3487  errmsg("cannot truncate a table referenced in a foreign key constraint"),
3488  errdetail("Table \"%s\" references \"%s\".",
3489  relname2, relname),
3490  errhint("Truncate table \"%s\" at the same time, "
3491  "or use TRUNCATE ... CASCADE.",
3492  relname2)));
3493  }
3494  }
3495  }
3496 }
List * heap_truncate_find_FKs(List *relationIds)
Definition: heap.c:3513
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1932
#define list_make1_oid(x1)
Definition: pg_list.h:242

References ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, 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 3513 of file heap.c.

3514 {
3515  List *result = NIL;
3516  List *oids;
3517  List *parent_cons;
3518  ListCell *cell;
3519  ScanKeyData key;
3520  Relation fkeyRel;
3521  SysScanDesc fkeyScan;
3522  HeapTuple tuple;
3523  bool restart;
3524 
3525  oids = list_copy(relationIds);
3526 
3527  /*
3528  * Must scan pg_constraint. Right now, it is a seqscan because there is
3529  * no available index on confrelid.
3530  */
3531  fkeyRel = table_open(ConstraintRelationId, AccessShareLock);
3532 
3533 restart:
3534  restart = false;
3535  parent_cons = NIL;
3536 
3537  fkeyScan = systable_beginscan(fkeyRel, InvalidOid, false,
3538  NULL, 0, NULL);
3539 
3540  while (HeapTupleIsValid(tuple = systable_getnext(fkeyScan)))
3541  {
3543 
3544  /* Not a foreign key */
3545  if (con->contype != CONSTRAINT_FOREIGN)
3546  continue;
3547 
3548  /* Not referencing one of our list of tables */
3549  if (!list_member_oid(oids, con->confrelid))
3550  continue;
3551 
3552  /*
3553  * If this constraint has a parent constraint which we have not seen
3554  * yet, keep track of it for the second loop, below. Tracking parent
3555  * constraints allows us to climb up to the top-level constraint and
3556  * look for all possible relations referencing the partitioned table.
3557  */
3558  if (OidIsValid(con->conparentid) &&
3559  !list_member_oid(parent_cons, con->conparentid))
3560  parent_cons = lappend_oid(parent_cons, con->conparentid);
3561 
3562  /*
3563  * Add referencer to result, unless present in input list. (Don't
3564  * worry about dupes: we'll fix that below).
3565  */
3566  if (!list_member_oid(relationIds, con->conrelid))
3567  result = lappend_oid(result, con->conrelid);
3568  }
3569 
3570  systable_endscan(fkeyScan);
3571 
3572  /*
3573  * Process each parent constraint we found to add the list of referenced
3574  * relations by them to the oids list. If we do add any new such
3575  * relations, redo the first loop above. Also, if we see that the parent
3576  * constraint in turn has a parent, add that so that we process all
3577  * relations in a single additional pass.
3578  */
3579  foreach(cell, parent_cons)
3580  {
3581  Oid parent = lfirst_oid(cell);
3582 
3583  ScanKeyInit(&key,
3584  Anum_pg_constraint_oid,
3585  BTEqualStrategyNumber, F_OIDEQ,
3586  ObjectIdGetDatum(parent));
3587 
3588  fkeyScan = systable_beginscan(fkeyRel, ConstraintOidIndexId,
3589  true, NULL, 1, &key);
3590 
3591  tuple = systable_getnext(fkeyScan);
3592  if (HeapTupleIsValid(tuple))
3593  {
3595 
3596  /*
3597  * pg_constraint rows always appear for partitioned hierarchies
3598  * this way: on the each side of the constraint, one row appears
3599  * for each partition that points to the top-most table on the
3600  * other side.
3601  *
3602  * Because of this arrangement, we can correctly catch all
3603  * relevant relations by adding to 'parent_cons' all rows with
3604  * valid conparentid, and to the 'oids' list all rows with a zero
3605  * conparentid. If any oids are added to 'oids', redo the first
3606  * loop above by setting 'restart'.
3607  */
3608  if (OidIsValid(con->conparentid))
3609  parent_cons = list_append_unique_oid(parent_cons,
3610  con->conparentid);
3611  else if (!list_member_oid(oids, con->confrelid))
3612  {
3613  oids = lappend_oid(oids, con->confrelid);
3614  restart = true;
3615  }
3616  }
3617 
3618  systable_endscan(fkeyScan);
3619  }
3620 
3621  list_free(parent_cons);
3622  if (restart)
3623  goto restart;
3624 
3625  table_close(fkeyRel, AccessShareLock);
3626  list_free(oids);
3627 
3628  /* Now sort and de-duplicate the result list */
3629  list_sort(result, list_oid_cmp);
3630  list_deduplicate_oid(result);
3631 
3632  return result;
3633 }
List * list_copy(const List *oldlist)
Definition: list.c:1572
void list_deduplicate_oid(List *list)
Definition: list.c:1494
int list_oid_cmp(const ListCell *p1, const ListCell *p2)
Definition: list.c:1706
void list_free(List *list)
Definition: list.c:1545
List * list_append_unique_oid(List *list, Oid datum)
Definition: list.c:1379
FormData_pg_constraint * Form_pg_constraint

References AccessShareLock, BTEqualStrategyNumber, GETSTRUCT, HeapTupleIsValid, InvalidOid, sort-test::key, 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 3374 of file heap.c.

3375 {
3376  Oid toastrelid;
3377 
3378  /*
3379  * Truncate the relation. Partitioned tables have no storage, so there is
3380  * nothing to do for them here.
3381  */
3382  if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
3383  return;
3384 
3385  /* Truncate the underlying relation */
3387 
3388  /* If the relation has indexes, truncate the indexes too */
3390 
3391  /* If there is a toast table, truncate that too */
3392  toastrelid = rel->rd_rel->reltoastrelid;
3393  if (OidIsValid(toastrelid))
3394  {
3395  Relation toastrel = table_open(toastrelid, AccessExclusiveLock);
3396 
3398  RelationTruncateIndexes(toastrel);
3399  /* keep the lock... */
3400  table_close(toastrel, NoLock);
3401  }
3402 }
static void RelationTruncateIndexes(Relation heapRelation)
Definition: heap.c:3285
static void table_relation_nontransactional_truncate(Relation rel)
Definition: tableam.h:1634

References AccessExclusiveLock, 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 Datum attoptions,
CatalogIndexState  indstate 
)

Definition at line 697 of file heap.c.

702 {
703  TupleTableSlot **slot;
704  TupleDesc td;
705  int nslots;
706  int natts = 0;
707  int slotCount = 0;
708  bool close_index = false;
709 
710  td = RelationGetDescr(pg_attribute_rel);
711 
712  /* Initialize the number of slots to use */
713  nslots = Min(tupdesc->natts,
715  slot = palloc(sizeof(TupleTableSlot *) * nslots);
716  for (int i = 0; i < nslots; i++)
718 
719  while (natts < tupdesc->natts)
720  {
721  Form_pg_attribute attrs = TupleDescAttr(tupdesc, natts);
722 
723  ExecClearTuple(slot[slotCount]);
724 
725  memset(slot[slotCount]->tts_isnull, false,
726  slot[slotCount]->tts_tupleDescriptor->natts * sizeof(bool));
727 
728  if (new_rel_oid != InvalidOid)
729  slot[slotCount]->tts_values[Anum_pg_attribute_attrelid - 1] = ObjectIdGetDatum(new_rel_oid);
730  else
731  slot[slotCount]->tts_values[Anum_pg_attribute_attrelid - 1] = ObjectIdGetDatum(attrs->attrelid);
732 
733  slot[slotCount]->tts_values[Anum_pg_attribute_attname - 1] = NameGetDatum(&attrs->attname);
734  slot[slotCount]->tts_values[Anum_pg_attribute_atttypid - 1] = ObjectIdGetDatum(attrs->atttypid);
735  slot[slotCount]->tts_values[Anum_pg_attribute_attlen - 1] = Int16GetDatum(attrs->attlen);
736  slot[slotCount]->tts_values[Anum_pg_attribute_attnum - 1] = Int16GetDatum(attrs->attnum);
737  slot[slotCount]->tts_values[Anum_pg_attribute_attcacheoff - 1] = Int32GetDatum(-1);
738  slot[slotCount]->tts_values[Anum_pg_attribute_atttypmod - 1] = Int32GetDatum(attrs->atttypmod);
739  slot[slotCount]->tts_values[Anum_pg_attribute_attndims - 1] = Int16GetDatum(attrs->attndims);
740  slot[slotCount]->tts_values[Anum_pg_attribute_attbyval - 1] = BoolGetDatum(attrs->attbyval);
741  slot[slotCount]->tts_values[Anum_pg_attribute_attalign - 1] = CharGetDatum(attrs->attalign);
742  slot[slotCount]->tts_values[Anum_pg_attribute_attstorage - 1] = CharGetDatum(attrs->attstorage);
743  slot[slotCount]->tts_values[Anum_pg_attribute_attcompression - 1] = CharGetDatum(attrs->attcompression);
744  slot[slotCount]->tts_values[Anum_pg_attribute_attnotnull - 1] = BoolGetDatum(attrs->attnotnull);
745  slot[slotCount]->tts_values[Anum_pg_attribute_atthasdef - 1] = BoolGetDatum(attrs->atthasdef);
746  slot[slotCount]->tts_values[Anum_pg_attribute_atthasmissing - 1] = BoolGetDatum(attrs->atthasmissing);
747  slot[slotCount]->tts_values[Anum_pg_attribute_attidentity - 1] = CharGetDatum(attrs->attidentity);
748  slot[slotCount]->tts_values[Anum_pg_attribute_attgenerated - 1] = CharGetDatum(attrs->attgenerated);
749  slot[slotCount]->tts_values[Anum_pg_attribute_attisdropped - 1] = BoolGetDatum(attrs->attisdropped);
750  slot[slotCount]->tts_values[Anum_pg_attribute_attislocal - 1] = BoolGetDatum(attrs->attislocal);
751  slot[slotCount]->tts_values[Anum_pg_attribute_attinhcount - 1] = Int16GetDatum(attrs->attinhcount);
752  slot[slotCount]->tts_values[Anum_pg_attribute_attstattarget - 1] = Int16GetDatum(attrs->attstattarget);
753  slot[slotCount]->tts_values[Anum_pg_attribute_attcollation - 1] = ObjectIdGetDatum(attrs->attcollation);
754  if (attoptions && attoptions[natts] != (Datum) 0)
755  slot[slotCount]->tts_values[Anum_pg_attribute_attoptions - 1] = attoptions[natts];
756  else
757  slot[slotCount]->tts_isnull[Anum_pg_attribute_attoptions - 1] = true;
758 
759  /* start out with empty permissions and empty options */
760  slot[slotCount]->tts_isnull[Anum_pg_attribute_attacl - 1] = true;
761  slot[slotCount]->tts_isnull[Anum_pg_attribute_attfdwoptions - 1] = true;
762  slot[slotCount]->tts_isnull[Anum_pg_attribute_attmissingval - 1] = true;
763 
764  ExecStoreVirtualTuple(slot[slotCount]);
765  slotCount++;
766 
767  /*
768  * If slots are full or the end of processing has been reached, insert
769  * a batch of tuples.
770  */
771  if (slotCount == nslots || natts == tupdesc->natts - 1)
772  {
773  /* fetch index info only when we know we need it */
774  if (!indstate)
775  {
776  indstate = CatalogOpenIndexes(pg_attribute_rel);
777  close_index = true;
778  }
779 
780  /* insert the new tuples and update the indexes */
781  CatalogTuplesMultiInsertWithInfo(pg_attribute_rel, slot, slotCount,
782  indstate);
783  slotCount = 0;
784  }
785 
786  natts++;
787  }
788 
789  if (close_index)
790  CatalogCloseIndexes(indstate);
791  for (int i = 0; i < nslots; i++)
793  pfree(slot);
794 }
#define Min(x, y)
Definition: c.h:993
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1553
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1255
const TupleTableSlotOps TTSOpsHeapTuple
Definition: execTuples.c:84
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1239
void CatalogTuplesMultiInsertWithInfo(Relation heapRel, TupleTableSlot **slot, int ntuples, CatalogIndexState indstate)
Definition: indexing.c:273
#define MAX_CATALOG_MULTI_INSERT_BYTES
Definition: indexing.h:33
FormData_pg_attribute
Definition: pg_attribute.h:193
uintptr_t Datum
Definition: postgres.h:64
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102
static Datum NameGetDatum(const NameData *X)
Definition: postgres.h:373
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
static Datum CharGetDatum(char X)
Definition: postgres.h:122
bool * tts_isnull
Definition: tuptable.h:127
Datum * tts_values
Definition: tuptable.h:125
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:432

References BoolGetDatum(), CatalogCloseIndexes(), CatalogOpenIndexes(), CatalogTuplesMultiInsertWithInfo(), CharGetDatum(), ExecClearTuple(), ExecDropSingleTupleTableSlot(), ExecStoreVirtualTuple(), FormData_pg_attribute, i, Int16GetDatum(), Int32GetDatum(), InvalidOid, MakeSingleTupleTableSlot(), MAX_CATALOG_MULTI_INSERT_BYTES, Min, NameGetDatum(), TupleDescData::natts, ObjectIdGetDatum(), palloc(), 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 882 of file heap.c.

887 {
888  Form_pg_class rd_rel = new_rel_desc->rd_rel;
889  Datum values[Natts_pg_class];
890  bool nulls[Natts_pg_class];
891  HeapTuple tup;
892 
893  /* This is a tad tedious, but way cleaner than what we used to do... */
894  memset(values, 0, sizeof(values));
895  memset(nulls, false, sizeof(nulls));
896 
897  values[Anum_pg_class_oid - 1] = ObjectIdGetDatum(new_rel_oid);
898  values[Anum_pg_class_relname - 1] = NameGetDatum(&rd_rel->relname);
899  values[Anum_pg_class_relnamespace - 1] = ObjectIdGetDatum(rd_rel->relnamespace);
900  values[Anum_pg_class_reltype - 1] = ObjectIdGetDatum(rd_rel->reltype);
901  values[Anum_pg_class_reloftype - 1] = ObjectIdGetDatum(rd_rel->reloftype);
902  values[Anum_pg_class_relowner - 1] = ObjectIdGetDatum(rd_rel->relowner);
903  values[Anum_pg_class_relam - 1] = ObjectIdGetDatum(rd_rel->relam);
904  values[Anum_pg_class_relfilenode - 1] = ObjectIdGetDatum(rd_rel->relfilenode);
905  values[Anum_pg_class_reltablespace - 1] = ObjectIdGetDatum(rd_rel->reltablespace);
906  values[Anum_pg_class_relpages - 1] = Int32GetDatum(rd_rel->relpages);
907  values[Anum_pg_class_reltuples - 1] = Float4GetDatum(rd_rel->reltuples);
908  values[Anum_pg_class_relallvisible - 1] = Int32GetDatum(rd_rel->relallvisible);
909  values[Anum_pg_class_reltoastrelid - 1] = ObjectIdGetDatum(rd_rel->reltoastrelid);
910  values[Anum_pg_class_relhasindex - 1] = BoolGetDatum(rd_rel->relhasindex);
911  values[Anum_pg_class_relisshared - 1] = BoolGetDatum(rd_rel->relisshared);
912  values[Anum_pg_class_relpersistence - 1] = CharGetDatum(rd_rel->relpersistence);
913  values[Anum_pg_class_relkind - 1] = CharGetDatum(rd_rel->relkind);
914  values[Anum_pg_class_relnatts - 1] = Int16GetDatum(rd_rel->relnatts);
915  values[Anum_pg_class_relchecks - 1] = Int16GetDatum(rd_rel->relchecks);
916  values[Anum_pg_class_relhasrules - 1] = BoolGetDatum(rd_rel->relhasrules);
917  values[Anum_pg_class_relhastriggers - 1] = BoolGetDatum(rd_rel->relhastriggers);
918  values[Anum_pg_class_relrowsecurity - 1] = BoolGetDatum(rd_rel->relrowsecurity);
919  values[Anum_pg_class_relforcerowsecurity - 1] = BoolGetDatum(rd_rel->relforcerowsecurity);
920  values[Anum_pg_class_relhassubclass - 1] = BoolGetDatum(rd_rel->relhassubclass);
921  values[Anum_pg_class_relispopulated - 1] = BoolGetDatum(rd_rel->relispopulated);
922  values[Anum_pg_class_relreplident - 1] = CharGetDatum(rd_rel->relreplident);
923  values[Anum_pg_class_relispartition - 1] = BoolGetDatum(rd_rel->relispartition);
924  values[Anum_pg_class_relrewrite - 1] = ObjectIdGetDatum(rd_rel->relrewrite);
925  values[Anum_pg_class_relfrozenxid - 1] = TransactionIdGetDatum(rd_rel->relfrozenxid);
926  values[Anum_pg_class_relminmxid - 1] = MultiXactIdGetDatum(rd_rel->relminmxid);
927  if (relacl != (Datum) 0)
928  values[Anum_pg_class_relacl - 1] = relacl;
929  else
930  nulls[Anum_pg_class_relacl - 1] = true;
931  if (reloptions != (Datum) 0)
932  values[Anum_pg_class_reloptions - 1] = reloptions;
933  else
934  nulls[Anum_pg_class_reloptions - 1] = true;
935 
936  /* relpartbound is set by updating this tuple, if necessary */
937  nulls[Anum_pg_class_relpartbound - 1] = true;
938 
939  tup = heap_form_tuple(RelationGetDescr(pg_class_desc), values, nulls);
940 
941  /* finally insert the new tuple, update the indexes, and clean up */
942  CatalogTupleInsert(pg_class_desc, tup);
943 
944  heap_freetuple(tup);
945 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1108
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:233
static Datum Float4GetDatum(float4 X)
Definition: postgres.h:475
static Datum TransactionIdGetDatum(TransactionId X)
Definition: postgres.h:272
static Datum MultiXactIdGetDatum(MultiXactId X)
Definition: postgres.h:282

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

Referenced by AddNewRelationTuple(), and index_create().

◆ RelationClearMissing()

void RelationClearMissing ( Relation  rel)

Definition at line 1922 of file heap.c.

1923 {
1924  Relation attr_rel;
1925  Oid relid = RelationGetRelid(rel);
1926  int natts = RelationGetNumberOfAttributes(rel);
1927  int attnum;
1928  Datum repl_val[Natts_pg_attribute];
1929  bool repl_null[Natts_pg_attribute];
1930  bool repl_repl[Natts_pg_attribute];
1931  Form_pg_attribute attrtuple;
1932  HeapTuple tuple,
1933  newtuple;
1934 
1935  memset(repl_val, 0, sizeof(repl_val));
1936  memset(repl_null, false, sizeof(repl_null));
1937  memset(repl_repl, false, sizeof(repl_repl));
1938 
1939  repl_val[Anum_pg_attribute_atthasmissing - 1] = BoolGetDatum(false);
1940  repl_null[Anum_pg_attribute_attmissingval - 1] = true;
1941 
1942  repl_repl[Anum_pg_attribute_atthasmissing - 1] = true;
1943  repl_repl[Anum_pg_attribute_attmissingval - 1] = true;
1944 
1945 
1946  /* Get a lock on pg_attribute */
1947  attr_rel = table_open(AttributeRelationId, RowExclusiveLock);
1948 
1949  /* process each non-system attribute, including any dropped columns */
1950  for (attnum = 1; attnum <= natts; attnum++)
1951  {
1952  tuple = SearchSysCache2(ATTNUM,
1953  ObjectIdGetDatum(relid),
1955  if (!HeapTupleIsValid(tuple)) /* shouldn't happen */
1956  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
1957  attnum, relid);
1958 
1959  attrtuple = (Form_pg_attribute) GETSTRUCT(tuple);
1960 
1961  /* ignore any where atthasmissing is not true */
1962  if (attrtuple->atthasmissing)
1963  {
1964  newtuple = heap_modify_tuple(tuple, RelationGetDescr(attr_rel),
1965  repl_val, repl_null, repl_repl);
1966 
1967  CatalogTupleUpdate(attr_rel, &newtuple->t_self, newtuple);
1968 
1969  heap_freetuple(newtuple);
1970  }
1971 
1972  ReleaseSysCache(tuple);
1973  }
1974 
1975  /*
1976  * Our update of the pg_attribute rows will force a relcache rebuild, so
1977  * there's nothing else to do here.
1978  */
1979  table_close(attr_rel, RowExclusiveLock);
1980 }
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1201
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:313
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:510
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:831
@ ATTNUM
Definition: syscache.h:41

References attnum, ATTNUM, BoolGetDatum(), CatalogTupleUpdate(), elog(), ERROR, 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(), and finish_heap_swap().

◆ RemoveAttributeById()

void RemoveAttributeById ( Oid  relid,
AttrNumber  attnum 
)

Definition at line 1643 of file heap.c.

1644 {
1645  Relation rel;
1646  Relation attr_rel;
1647  HeapTuple tuple;
1648  Form_pg_attribute attStruct;
1649  char newattname[NAMEDATALEN];
1650 
1651  /*
1652  * Grab an exclusive lock on the target table, which we will NOT release
1653  * until end of transaction. (In the simple case where we are directly
1654  * dropping this column, ATExecDropColumn already did this ... but when
1655  * cascading from a drop of some other object, we may not have any lock.)
1656  */
1657  rel = relation_open(relid, AccessExclusiveLock);
1658 
1659  attr_rel = table_open(AttributeRelationId, RowExclusiveLock);
1660 
1661  tuple = SearchSysCacheCopy2(ATTNUM,
1662  ObjectIdGetDatum(relid),
1664  if (!HeapTupleIsValid(tuple)) /* shouldn't happen */
1665  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
1666  attnum, relid);
1667  attStruct = (Form_pg_attribute) GETSTRUCT(tuple);
1668 
1669  /* Mark the attribute as dropped */
1670  attStruct->attisdropped = true;
1671 
1672  /*
1673  * Set the type OID to invalid. A dropped attribute's type link cannot be
1674  * relied on (once the attribute is dropped, the type might be too).
1675  * Fortunately we do not need the type row --- the only really essential
1676  * information is the type's typlen and typalign, which are preserved in
1677  * the attribute's attlen and attalign. We set atttypid to zero here as a
1678  * means of catching code that incorrectly expects it to be valid.
1679  */
1680  attStruct->atttypid = InvalidOid;
1681 
1682  /* Remove any not-null constraint the column may have */
1683  attStruct->attnotnull = false;
1684 
1685  /* We don't want to keep stats for it anymore */
1686  attStruct->attstattarget = 0;
1687 
1688  /* Unset this so no one tries to look up the generation expression */
1689  attStruct->attgenerated = '\0';
1690 
1691  /*
1692  * Change the column name to something that isn't likely to conflict
1693  */
1694  snprintf(newattname, sizeof(newattname),
1695  "........pg.dropped.%d........", attnum);
1696  namestrcpy(&(attStruct->attname), newattname);
1697 
1698  /* clear the missing value if any */
1699  if (attStruct->atthasmissing)
1700  {
1701  Datum valuesAtt[Natts_pg_attribute] = {0};
1702  bool nullsAtt[Natts_pg_attribute] = {0};
1703  bool replacesAtt[Natts_pg_attribute] = {0};
1704 
1705  /* update the tuple - set atthasmissing and attmissingval */
1706  valuesAtt[Anum_pg_attribute_atthasmissing - 1] =
1707  BoolGetDatum(false);
1708  replacesAtt[Anum_pg_attribute_atthasmissing - 1] = true;
1709  valuesAtt[Anum_pg_attribute_attmissingval - 1] = (Datum) 0;
1710  nullsAtt[Anum_pg_attribute_attmissingval - 1] = true;
1711  replacesAtt[Anum_pg_attribute_attmissingval - 1] = true;
1712 
1713  tuple = heap_modify_tuple(tuple, RelationGetDescr(attr_rel),
1714  valuesAtt, nullsAtt, replacesAtt);
1715  }
1716 
1717  CatalogTupleUpdate(attr_rel, &tuple->t_self, tuple);
1718 
1719  /*
1720  * Because updating the pg_attribute row will trigger a relcache flush for
1721  * the target relation, we need not do anything else to notify other
1722  * backends of the change.
1723  */
1724 
1725  table_close(attr_rel, RowExclusiveLock);
1726 
1727  RemoveStatistics(relid, attnum);
1728 
1729  relation_close(rel, NoLock);
1730 }
void namestrcpy(Name name, const char *str)
Definition: name.c:233
#define NAMEDATALEN
#define snprintf
Definition: port.h:238
#define SearchSysCacheCopy2(cacheId, key1, key2)
Definition: syscache.h:184

References AccessExclusiveLock, attnum, ATTNUM, BoolGetDatum(), CatalogTupleUpdate(), elog(), ERROR, 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 3765 of file heap.c.

3766 {
3767  Relation rel;
3768  HeapTuple tuple;
3769 
3770  rel = table_open(PartitionedRelationId, RowExclusiveLock);
3771 
3772  tuple = SearchSysCache1(PARTRELID, ObjectIdGetDatum(relid));
3773  if (!HeapTupleIsValid(tuple))
3774  elog(ERROR, "cache lookup failed for partition key of relation %u",
3775  relid);
3776 
3777  CatalogTupleDelete(rel, &tuple->t_self);
3778 
3779  ReleaseSysCache(tuple);
3781 }
@ PARTRELID
Definition: syscache.h:77

References CatalogTupleDelete(), elog(), ERROR, HeapTupleIsValid, ObjectIdGetDatum(), PARTRELID, 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 3238 of file heap.c.

3239 {
3240  Relation pgstatistic;
3241  SysScanDesc scan;
3242  ScanKeyData key[2];
3243  int nkeys;
3244  HeapTuple tuple;
3245 
3246  pgstatistic = table_open(StatisticRelationId, RowExclusiveLock);
3247 
3248  ScanKeyInit(&key[0],
3249  Anum_pg_statistic_starelid,
3250  BTEqualStrategyNumber, F_OIDEQ,
3251  ObjectIdGetDatum(relid));
3252 
3253  if (attnum == 0)
3254  nkeys = 1;
3255  else
3256  {
3257  ScanKeyInit(&key[1],
3258  Anum_pg_statistic_staattnum,
3259  BTEqualStrategyNumber, F_INT2EQ,
3261  nkeys = 2;
3262  }
3263 
3264  scan = systable_beginscan(pgstatistic, StatisticRelidAttnumInhIndexId, true,
3265  NULL, nkeys, key);
3266 
3267  /* we must loop even when attnum != 0, in case of inherited stats */
3268  while (HeapTupleIsValid(tuple = systable_getnext(scan)))
3269  CatalogTupleDelete(pgstatistic, &tuple->t_self);
3270 
3271  systable_endscan(scan);
3272 
3273  table_close(pgstatistic, RowExclusiveLock);
3274 }

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

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

◆ SetAttrMissing()

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

Definition at line 1990 of file heap.c.

1991 {
1992  Datum valuesAtt[Natts_pg_attribute] = {0};
1993  bool nullsAtt[Natts_pg_attribute] = {0};
1994  bool replacesAtt[Natts_pg_attribute] = {0};
1995  Datum missingval;
1996  Form_pg_attribute attStruct;
1997  Relation attrrel,
1998  tablerel;
1999  HeapTuple atttup,
2000  newtup;
2001 
2002  /* lock the table the attribute belongs to */
2003  tablerel = table_open(relid, AccessExclusiveLock);
2004 
2005  /* Don't do anything unless it's a plain table */
2006  if (tablerel->rd_rel->relkind != RELKIND_RELATION)
2007  {
2008  table_close(tablerel, AccessExclusiveLock);
2009  return;
2010  }
2011 
2012  /* Lock the attribute row and get the data */
2013  attrrel = table_open(AttributeRelationId, RowExclusiveLock);
2014  atttup = SearchSysCacheAttName(relid, attname);
2015  if (!HeapTupleIsValid(atttup))
2016  elog(ERROR, "cache lookup failed for attribute %s of relation %u",
2017  attname, relid);
2018  attStruct = (Form_pg_attribute) GETSTRUCT(atttup);
2019 
2020  /* get an array value from the value string */
2021  missingval = OidFunctionCall3(F_ARRAY_IN,
2023  ObjectIdGetDatum(attStruct->atttypid),
2024  Int32GetDatum(attStruct->atttypmod));
2025 
2026  /* update the tuple - set atthasmissing and attmissingval */
2027  valuesAtt[Anum_pg_attribute_atthasmissing - 1] = BoolGetDatum(true);
2028  replacesAtt[Anum_pg_attribute_atthasmissing - 1] = true;
2029  valuesAtt[Anum_pg_attribute_attmissingval - 1] = missingval;
2030  replacesAtt[Anum_pg_attribute_attmissingval - 1] = true;
2031 
2032  newtup = heap_modify_tuple(atttup, RelationGetDescr(attrrel),
2033  valuesAtt, nullsAtt, replacesAtt);
2034  CatalogTupleUpdate(attrrel, &newtup->t_self, newtup);
2035 
2036  /* clean up */
2037  ReleaseSysCache(atttup);
2038  table_close(attrrel, RowExclusiveLock);
2039  table_close(tablerel, AccessExclusiveLock);
2040 }
#define OidFunctionCall3(functionId, arg1, arg2, arg3)
Definition: fmgr.h:684
static struct @148 value
HeapTuple SearchSysCacheAttName(Oid relid, const char *attname)
Definition: syscache.c:961

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

Referenced by binary_upgrade_set_missing_value().

◆ StorePartitionBound()

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

Definition at line 3796 of file heap.c.

3797 {
3798  Relation classRel;
3799  HeapTuple tuple,
3800  newtuple;
3801  Datum new_val[Natts_pg_class];
3802  bool new_null[Natts_pg_class],
3803  new_repl[Natts_pg_class];
3804  Oid defaultPartOid;
3805 
3806  /* Update pg_class tuple */
3807  classRel = table_open(RelationRelationId, RowExclusiveLock);
3808  tuple = SearchSysCacheCopy1(RELOID,
3810  if (!HeapTupleIsValid(tuple))
3811  elog(ERROR, "cache lookup failed for relation %u",
3812  RelationGetRelid(rel));
3813 
3814 #ifdef USE_ASSERT_CHECKING
3815  {
3816  Form_pg_class classForm;
3817  bool isnull;
3818 
3819  classForm = (Form_pg_class) GETSTRUCT(tuple);
3820  Assert(!classForm->relispartition);
3821  (void) SysCacheGetAttr(RELOID, tuple, Anum_pg_class_relpartbound,
3822  &isnull);
3823  Assert(isnull);
3824  }
3825 #endif
3826 
3827  /* Fill in relpartbound value */
3828  memset(new_val, 0, sizeof(new_val));
3829  memset(new_null, false, sizeof(new_null));
3830  memset(new_repl, false, sizeof(new_repl));
3831  new_val[Anum_pg_class_relpartbound - 1] = CStringGetTextDatum(nodeToString(bound));
3832  new_null[Anum_pg_class_relpartbound - 1] = false;
3833  new_repl[Anum_pg_class_relpartbound - 1] = true;
3834  newtuple = heap_modify_tuple(tuple, RelationGetDescr(classRel),
3835  new_val, new_null, new_repl);
3836  /* Also set the flag */
3837  ((Form_pg_class) GETSTRUCT(newtuple))->relispartition = true;
3838  CatalogTupleUpdate(classRel, &newtuple->t_self, newtuple);
3839  heap_freetuple(newtuple);
3840  table_close(classRel, RowExclusiveLock);
3841 
3842  /*
3843  * If we're storing bounds for the default partition, update
3844  * pg_partitioned_table too.
3845  */
3846  if (bound->is_default)
3848  RelationGetRelid(rel));
3849 
3850  /* Make these updates visible */
3852 
3853  /*
3854  * The partition constraint for the default partition depends on the
3855  * partition bounds of every other partition, so we must invalidate the
3856  * relcache entry for that partition every time a partition is added or
3857  * removed.
3858  */
3859  defaultPartOid =
3861  if (OidIsValid(defaultPartOid))
3862  CacheInvalidateRelcacheByRelid(defaultPartOid);
3863 
3864  CacheInvalidateRelcache(parent);
3865 }
#define CStringGetTextDatum(s)
Definition: builtins.h:94
void CacheInvalidateRelcache(Relation relation)
Definition: inval.c:1363
char * nodeToString(const void *obj)
Definition: outfuncs.c:883
PartitionDesc RelationGetPartitionDesc(Relation rel, bool omit_detached)
Definition: partdesc.c:72
Oid get_default_oid_from_partdesc(PartitionDesc partdesc)
Definition: partdesc.c:461
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1081
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:182
void CommandCounterIncrement(void)
Definition: xact.c:1078

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

Referenced by ATExecAttachPartition(), and DefineRelation().

◆ StorePartitionKey()

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

Definition at line 3640 of file heap.c.

3647 {
3648  int i;
3649  int2vector *partattrs_vec;
3650  oidvector *partopclass_vec;
3651  oidvector *partcollation_vec;
3652  Datum partexprDatum;
3653  Relation pg_partitioned_table;
3654  HeapTuple tuple;
3655  Datum values[Natts_pg_partitioned_table];
3656  bool nulls[Natts_pg_partitioned_table] = {0};
3657  ObjectAddress myself;
3658  ObjectAddress referenced;
3659  ObjectAddresses *addrs;
3660 
3661  Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
3662 
3663  /* Copy the partition attribute numbers, opclass OIDs into arrays */
3664  partattrs_vec = buildint2vector(partattrs, partnatts);
3665  partopclass_vec = buildoidvector(partopclass, partnatts);
3666  partcollation_vec = buildoidvector(partcollation, partnatts);
3667 
3668  /* Convert the expressions (if any) to a text datum */
3669  if (partexprs)
3670  {
3671  char *exprString;
3672 
3673  exprString = nodeToString(partexprs);
3674  partexprDatum = CStringGetTextDatum(exprString);
3675  pfree(exprString);
3676  }
3677  else
3678  partexprDatum = (Datum) 0;
3679 
3680  pg_partitioned_table = table_open(PartitionedRelationId, RowExclusiveLock);
3681 
3682  /* Only this can ever be NULL */
3683  if (!partexprDatum)
3684  nulls[Anum_pg_partitioned_table_partexprs - 1] = true;
3685 
3686  values[Anum_pg_partitioned_table_partrelid - 1] = ObjectIdGetDatum(RelationGetRelid(rel));
3687  values[Anum_pg_partitioned_table_partstrat - 1] = CharGetDatum(strategy);
3688  values[Anum_pg_partitioned_table_partnatts - 1] = Int16GetDatum(partnatts);
3689  values[Anum_pg_partitioned_table_partdefid - 1] = ObjectIdGetDatum(InvalidOid);
3690  values[Anum_pg_partitioned_table_partattrs - 1] = PointerGetDatum(partattrs_vec);
3691  values[Anum_pg_partitioned_table_partclass - 1] = PointerGetDatum(partopclass_vec);
3692  values[Anum_pg_partitioned_table_partcollation - 1] = PointerGetDatum(partcollation_vec);
3693  values[Anum_pg_partitioned_table_partexprs - 1] = partexprDatum;
3694 
3695  tuple = heap_form_tuple(RelationGetDescr(pg_partitioned_table), values, nulls);
3696 
3697  CatalogTupleInsert(pg_partitioned_table, tuple);
3698  table_close(pg_partitioned_table, RowExclusiveLock);
3699 
3700  /* Mark this relation as dependent on a few things as follows */
3701  addrs = new_object_addresses();
3702  ObjectAddressSet(myself, RelationRelationId, RelationGetRelid(rel));
3703 
3704  /* Operator class and collation per key column */
3705  for (i = 0; i < partnatts; i++)
3706  {
3707  ObjectAddressSet(referenced, OperatorClassRelationId, partopclass[i]);
3708  add_exact_object_address(&referenced, addrs);
3709 
3710  /* The default collation is pinned, so don't bother recording it */
3711  if (OidIsValid(partcollation[i]) &&
3712  partcollation[i] != DEFAULT_COLLATION_OID)
3713  {
3714  ObjectAddressSet(referenced, CollationRelationId, partcollation[i]);
3715  add_exact_object_address(&referenced, addrs);
3716  }
3717  }
3718 
3720  free_object_addresses(addrs);
3721 
3722  /*
3723  * The partitioning columns are made internally dependent on the table,
3724  * because we cannot drop any of them without dropping the whole table.
3725  * (ATExecDropColumn independently enforces that, but it's not bulletproof
3726  * so we need the dependencies too.)
3727  */
3728  for (i = 0; i < partnatts; i++)
3729  {
3730  if (partattrs[i] == 0)
3731  continue; /* ignore expressions here */
3732 
3733  ObjectAddressSubSet(referenced, RelationRelationId,
3734  RelationGetRelid(rel), partattrs[i]);
3735  recordDependencyOn(&referenced, &myself, DEPENDENCY_INTERNAL);
3736  }
3737 
3738  /*
3739  * Also consider anything mentioned in partition expressions. External
3740  * references (e.g. functions) get NORMAL dependencies. Table columns
3741  * mentioned in the expressions are handled the same as plain partitioning
3742  * columns, i.e. they become internally dependent on the whole table.
3743  */
3744  if (partexprs)
3746  (Node *) partexprs,
3747  RelationGetRelid(rel),
3750  true /* reverse the self-deps */ );
3751 
3752  /*
3753  * We must invalidate the relcache so that the next
3754  * CommandCounterIncrement() will cause the same to be rebuilt using the
3755  * information in just created catalog entry.
3756  */
3758 }
void recordDependencyOnSingleRelExpr(const ObjectAddress *depender, Node *expr, Oid relId, DependencyType behavior, DependencyType self_behavior, bool reverse_self)
Definition: dependency.c:1645
@ DEPENDENCY_INTERNAL
Definition: dependency.h:35
int2vector * buildint2vector(const int16 *int2s, int n)
Definition: int.c:114
#define ObjectAddressSubSet(addr, class_id, object_id, object_sub_id)
Definition: objectaddress.h:33
oidvector * buildoidvector(const Oid *oids, int n)
Definition: oid.c:86
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:44
Definition: c.h:704
Definition: c.h:715

References add_exact_object_address(), Assert(), buildint2vector(), buildoidvector(), CacheInvalidateRelcache(), CatalogTupleInsert(), CharGetDatum(), CStringGetTextDatum, DEPENDENCY_INTERNAL, DEPENDENCY_NORMAL, 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().

◆ SystemAttributeByName()

const FormData_pg_attribute* SystemAttributeByName ( const char *  attname)

Definition at line 251 of file heap.c.

252 {
253  int j;
254 
255  for (j = 0; j < (int) lengthof(SysAtt); j++)
256  {
257  const FormData_pg_attribute *att = SysAtt[j];
258 
259  if (strcmp(NameStr(att->attname), attname) == 0)
260  return att;
261  }
262 
263  return NULL;
264 }
#define lengthof(array)
Definition: c.h:777
static const FormData_pg_attribute *const SysAtt[]
Definition: heap.c:231

References attname, 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 239 of file heap.c.

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

References elog(), ERROR, lengthof, and SysAtt.

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