PostgreSQL Source Code  git master
heap.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * heap.c
4  * code to create and destroy POSTGRES heap relations
5  *
6  * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/catalog/heap.c
12  *
13  *
14  * INTERFACE ROUTINES
15  * heap_create() - Create an uncataloged heap relation
16  * heap_create_with_catalog() - Create a cataloged relation
17  * heap_drop_with_catalog() - Removes named relation from catalogs
18  *
19  * NOTES
20  * this code taken from access/heap/create.c, which contains
21  * the old heap_create_with_catalog, amcreate, and amdestroy.
22  * those routines will soon call these routines using the function
23  * manager,
24  * just like the poorly named "NewXXX" routines do. The
25  * "New" routines are all going to die soon, once and for all!
26  * -cim 1/13/91
27  *
28  *-------------------------------------------------------------------------
29  */
30 #include "postgres.h"
31 
32 #include "access/genam.h"
33 #include "access/multixact.h"
34 #include "access/relation.h"
35 #include "access/table.h"
36 #include "access/tableam.h"
37 #include "catalog/binary_upgrade.h"
38 #include "catalog/catalog.h"
39 #include "catalog/heap.h"
40 #include "catalog/index.h"
41 #include "catalog/objectaccess.h"
42 #include "catalog/partition.h"
43 #include "catalog/pg_am.h"
44 #include "catalog/pg_attrdef.h"
45 #include "catalog/pg_collation.h"
46 #include "catalog/pg_constraint.h"
48 #include "catalog/pg_inherits.h"
49 #include "catalog/pg_namespace.h"
50 #include "catalog/pg_opclass.h"
52 #include "catalog/pg_statistic.h"
54 #include "catalog/pg_tablespace.h"
55 #include "catalog/pg_type.h"
56 #include "catalog/storage.h"
57 #include "commands/tablecmds.h"
58 #include "commands/typecmds.h"
59 #include "miscadmin.h"
60 #include "nodes/nodeFuncs.h"
61 #include "optimizer/optimizer.h"
62 #include "parser/parse_coerce.h"
63 #include "parser/parse_collate.h"
64 #include "parser/parse_expr.h"
65 #include "parser/parse_relation.h"
66 #include "parser/parsetree.h"
67 #include "partitioning/partdesc.h"
68 #include "pgstat.h"
69 #include "storage/lmgr.h"
70 #include "storage/predicate.h"
71 #include "utils/builtins.h"
72 #include "utils/fmgroids.h"
73 #include "utils/inval.h"
74 #include "utils/lsyscache.h"
75 #include "utils/syscache.h"
76 
77 
78 /* Potentially set by pg_upgrade_support functions */
83 
84 static void AddNewRelationTuple(Relation pg_class_desc,
85  Relation new_rel_desc,
86  Oid new_rel_oid,
87  Oid new_type_oid,
88  Oid reloftype,
89  Oid relowner,
90  char relkind,
91  TransactionId relfrozenxid,
92  TransactionId relminmxid,
93  Datum relacl,
94  Datum reloptions);
95 static ObjectAddress AddNewRelationType(const char *typeName,
96  Oid typeNamespace,
97  Oid new_rel_oid,
98  char new_rel_kind,
99  Oid ownerid,
100  Oid new_row_type,
101  Oid new_array_type);
102 static void RelationRemoveInheritance(Oid relid);
103 static Oid StoreRelCheck(Relation rel, const char *ccname, Node *expr,
104  bool is_validated, bool is_local, int inhcount,
105  bool is_no_inherit, bool is_internal);
106 static void StoreConstraints(Relation rel, List *cooked_constraints,
107  bool is_internal);
108 static bool MergeWithExistingConstraint(Relation rel, const char *ccname, Node *expr,
109  bool allow_merge, bool is_local,
110  bool is_initially_valid,
111  bool is_no_inherit);
112 static void SetRelationNumChecks(Relation rel, int numchecks);
113 static Node *cookConstraint(ParseState *pstate,
114  Node *raw_constraint,
115  char *relname);
116 
117 
118 /* ----------------------------------------------------------------
119  * XXX UGLY HARD CODED BADNESS FOLLOWS XXX
120  *
121  * these should all be moved to someplace in the lib/catalog
122  * module, if not obliterated first.
123  * ----------------------------------------------------------------
124  */
125 
126 
127 /*
128  * Note:
129  * Should the system special case these attributes in the future?
130  * Advantage: consume much less space in the ATTRIBUTE relation.
131  * Disadvantage: special cases will be all over the place.
132  */
133 
134 /*
135  * The initializers below do not include trailing variable length fields,
136  * but that's OK - we're never going to reference anything beyond the
137  * fixed-size portion of the structure anyway. Fields that can default
138  * to zeroes are also not mentioned.
139  */
140 
141 static const FormData_pg_attribute a1 = {
142  .attname = {"ctid"},
143  .atttypid = TIDOID,
144  .attlen = sizeof(ItemPointerData),
146  .attcacheoff = -1,
147  .atttypmod = -1,
148  .attbyval = false,
149  .attalign = TYPALIGN_SHORT,
150  .attstorage = TYPSTORAGE_PLAIN,
151  .attnotnull = true,
152  .attislocal = true,
153 };
154 
155 static const FormData_pg_attribute a2 = {
156  .attname = {"xmin"},
157  .atttypid = XIDOID,
158  .attlen = sizeof(TransactionId),
160  .attcacheoff = -1,
161  .atttypmod = -1,
162  .attbyval = true,
163  .attalign = TYPALIGN_INT,
164  .attstorage = TYPSTORAGE_PLAIN,
165  .attnotnull = true,
166  .attislocal = true,
167 };
168 
169 static const FormData_pg_attribute a3 = {
170  .attname = {"cmin"},
171  .atttypid = CIDOID,
172  .attlen = sizeof(CommandId),
174  .attcacheoff = -1,
175  .atttypmod = -1,
176  .attbyval = true,
177  .attalign = TYPALIGN_INT,
178  .attstorage = TYPSTORAGE_PLAIN,
179  .attnotnull = true,
180  .attislocal = true,
181 };
182 
183 static const FormData_pg_attribute a4 = {
184  .attname = {"xmax"},
185  .atttypid = XIDOID,
186  .attlen = sizeof(TransactionId),
188  .attcacheoff = -1,
189  .atttypmod = -1,
190  .attbyval = true,
191  .attalign = TYPALIGN_INT,
192  .attstorage = TYPSTORAGE_PLAIN,
193  .attnotnull = true,
194  .attislocal = true,
195 };
196 
197 static const FormData_pg_attribute a5 = {
198  .attname = {"cmax"},
199  .atttypid = CIDOID,
200  .attlen = sizeof(CommandId),
202  .attcacheoff = -1,
203  .atttypmod = -1,
204  .attbyval = true,
205  .attalign = TYPALIGN_INT,
206  .attstorage = TYPSTORAGE_PLAIN,
207  .attnotnull = true,
208  .attislocal = true,
209 };
210 
211 /*
212  * We decided to call this attribute "tableoid" rather than say
213  * "classoid" on the basis that in the future there may be more than one
214  * table of a particular class/type. In any case table is still the word
215  * used in SQL.
216  */
217 static const FormData_pg_attribute a6 = {
218  .attname = {"tableoid"},
219  .atttypid = OIDOID,
220  .attlen = sizeof(Oid),
222  .attcacheoff = -1,
223  .atttypmod = -1,
224  .attbyval = true,
225  .attalign = TYPALIGN_INT,
226  .attstorage = TYPSTORAGE_PLAIN,
227  .attnotnull = true,
228  .attislocal = true,
229 };
230 
231 static const FormData_pg_attribute *SysAtt[] = {&a1, &a2, &a3, &a4, &a5, &a6};
232 
233 /*
234  * This function returns a Form_pg_attribute pointer for a system attribute.
235  * Note that we elog if the presented attno is invalid, which would only
236  * happen if there's a problem upstream.
237  */
238 const FormData_pg_attribute *
240 {
241  if (attno >= 0 || attno < -(int) lengthof(SysAtt))
242  elog(ERROR, "invalid system attribute number %d", attno);
243  return SysAtt[-attno - 1];
244 }
245 
246 /*
247  * If the given name is a system attribute name, return a Form_pg_attribute
248  * pointer for a prototype definition. If not, return NULL.
249  */
250 const FormData_pg_attribute *
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 }
265 
266 
267 /* ----------------------------------------------------------------
268  * XXX END OF UGLY HARD CODED BADNESS XXX
269  * ---------------------------------------------------------------- */
270 
271 
272 /* ----------------------------------------------------------------
273  * heap_create - Create an uncataloged heap relation
274  *
275  * Note API change: the caller must now always provide the OID
276  * to use for the relation. The relfilenode may be (and in
277  * the simplest cases is) left unspecified.
278  *
279  * create_storage indicates whether or not to create the storage.
280  * However, even if create_storage is true, no storage will be
281  * created if the relkind is one that doesn't have storage.
282  *
283  * rel->rd_rel is initialized by RelationBuildLocalRelation,
284  * and is mostly zeroes at return.
285  * ----------------------------------------------------------------
286  */
287 Relation
288 heap_create(const char *relname,
289  Oid relnamespace,
290  Oid reltablespace,
291  Oid relid,
292  Oid relfilenode,
293  Oid accessmtd,
294  TupleDesc tupDesc,
295  char relkind,
296  char relpersistence,
297  bool shared_relation,
298  bool mapped_relation,
299  bool allow_system_table_mods,
300  TransactionId *relfrozenxid,
301  MultiXactId *relminmxid,
302  bool create_storage)
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 relfilenode is unspecified by the caller then create storage
345  * with oid same as relid.
346  */
347  if (!OidIsValid(relfilenode))
348  relfilenode = 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  relfilenode,
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_node, 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  return rel;
407 }
408 
409 /* ----------------------------------------------------------------
410  * heap_create_with_catalog - Create a cataloged relation
411  *
412  * this is done in multiple steps:
413  *
414  * 1) CheckAttributeNamesTypes() is used to make certain the tuple
415  * descriptor contains a valid set of attribute names and types
416  *
417  * 2) pg_class is opened and get_relname_relid()
418  * performs a scan to ensure that no relation with the
419  * same name already exists.
420  *
421  * 3) heap_create() is called to create the new relation on disk.
422  *
423  * 4) TypeCreate() is called to define a new type corresponding
424  * to the new relation.
425  *
426  * 5) AddNewRelationTuple() is called to register the
427  * relation in pg_class.
428  *
429  * 6) AddNewAttributeTuples() is called to register the
430  * new relation's schema in pg_attribute.
431  *
432  * 7) StoreConstraints is called () - vadim 08/22/97
433  *
434  * 8) the relations are closed and the new relation's oid
435  * is returned.
436  *
437  * ----------------------------------------------------------------
438  */
439 
440 /* --------------------------------
441  * CheckAttributeNamesTypes
442  *
443  * this is used to make certain the tuple descriptor contains a
444  * valid set of attribute names and datatypes. a problem simply
445  * generates ereport(ERROR) which aborts the current transaction.
446  *
447  * relkind is the relkind of the relation to be created.
448  * flags controls which datatypes are allowed, cf CheckAttributeType.
449  * --------------------------------
450  */
451 void
452 CheckAttributeNamesTypes(TupleDesc tupdesc, char relkind,
453  int flags)
454 {
455  int i;
456  int j;
457  int natts = tupdesc->natts;
458 
459  /* Sanity check on column count */
460  if (natts < 0 || natts > MaxHeapAttributeNumber)
461  ereport(ERROR,
462  (errcode(ERRCODE_TOO_MANY_COLUMNS),
463  errmsg("tables can have at most %d columns",
465 
466  /*
467  * first check for collision with system attribute names
468  *
469  * Skip this for a view or type relation, since those don't have system
470  * attributes.
471  */
472  if (relkind != RELKIND_VIEW && relkind != RELKIND_COMPOSITE_TYPE)
473  {
474  for (i = 0; i < natts; i++)
475  {
476  Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
477 
478  if (SystemAttributeByName(NameStr(attr->attname)) != NULL)
479  ereport(ERROR,
480  (errcode(ERRCODE_DUPLICATE_COLUMN),
481  errmsg("column name \"%s\" conflicts with a system column name",
482  NameStr(attr->attname))));
483  }
484  }
485 
486  /*
487  * next check for repeated attribute names
488  */
489  for (i = 1; i < natts; i++)
490  {
491  for (j = 0; j < i; j++)
492  {
493  if (strcmp(NameStr(TupleDescAttr(tupdesc, j)->attname),
494  NameStr(TupleDescAttr(tupdesc, i)->attname)) == 0)
495  ereport(ERROR,
496  (errcode(ERRCODE_DUPLICATE_COLUMN),
497  errmsg("column name \"%s\" specified more than once",
498  NameStr(TupleDescAttr(tupdesc, j)->attname))));
499  }
500  }
501 
502  /*
503  * next check the attribute types
504  */
505  for (i = 0; i < natts; i++)
506  {
508  TupleDescAttr(tupdesc, i)->atttypid,
509  TupleDescAttr(tupdesc, i)->attcollation,
510  NIL, /* assume we're creating a new rowtype */
511  flags);
512  }
513 }
514 
515 /* --------------------------------
516  * CheckAttributeType
517  *
518  * Verify that the proposed datatype of an attribute is legal.
519  * This is needed mainly because there are types (and pseudo-types)
520  * in the catalogs that we do not support as elements of real tuples.
521  * We also check some other properties required of a table column.
522  *
523  * If the attribute is being proposed for addition to an existing table or
524  * composite type, pass a one-element list of the rowtype OID as
525  * containing_rowtypes. When checking a to-be-created rowtype, it's
526  * sufficient to pass NIL, because there could not be any recursive reference
527  * to a not-yet-existing rowtype.
528  *
529  * flags is a bitmask controlling which datatypes we allow. For the most
530  * part, pseudo-types are disallowed as attribute types, but there are some
531  * exceptions: ANYARRAYOID, RECORDOID, and RECORDARRAYOID can be allowed
532  * in some cases. (This works because values of those type classes are
533  * self-identifying to some extent. However, RECORDOID and RECORDARRAYOID
534  * are reliably identifiable only within a session, since the identity info
535  * may use a typmod that is only locally assigned. The caller is expected
536  * to know whether these cases are safe.)
537  *
538  * flags can also control the phrasing of the error messages. If
539  * CHKATYPE_IS_PARTKEY is specified, "attname" should be a partition key
540  * column number as text, not a real column name.
541  * --------------------------------
542  */
543 void
545  Oid atttypid, Oid attcollation,
546  List *containing_rowtypes,
547  int flags)
548 {
549  char att_typtype = get_typtype(atttypid);
550  Oid att_typelem;
551 
552  if (att_typtype == TYPTYPE_PSEUDO)
553  {
554  /*
555  * We disallow pseudo-type columns, with the exception of ANYARRAY,
556  * RECORD, and RECORD[] when the caller says that those are OK.
557  *
558  * We don't need to worry about recursive containment for RECORD and
559  * RECORD[] because (a) no named composite type should be allowed to
560  * contain those, and (b) two "anonymous" record types couldn't be
561  * considered to be the same type, so infinite recursion isn't
562  * possible.
563  */
564  if (!((atttypid == ANYARRAYOID && (flags & CHKATYPE_ANYARRAY)) ||
565  (atttypid == RECORDOID && (flags & CHKATYPE_ANYRECORD)) ||
566  (atttypid == RECORDARRAYOID && (flags & CHKATYPE_ANYRECORD))))
567  {
568  if (flags & CHKATYPE_IS_PARTKEY)
569  ereport(ERROR,
570  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
571  /* translator: first %s is an integer not a name */
572  errmsg("partition key column %s has pseudo-type %s",
573  attname, format_type_be(atttypid))));
574  else
575  ereport(ERROR,
576  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
577  errmsg("column \"%s\" has pseudo-type %s",
578  attname, format_type_be(atttypid))));
579  }
580  }
581  else if (att_typtype == TYPTYPE_DOMAIN)
582  {
583  /*
584  * If it's a domain, recurse to check its base type.
585  */
586  CheckAttributeType(attname, getBaseType(atttypid), attcollation,
587  containing_rowtypes,
588  flags);
589  }
590  else if (att_typtype == TYPTYPE_COMPOSITE)
591  {
592  /*
593  * For a composite type, recurse into its attributes.
594  */
595  Relation relation;
596  TupleDesc tupdesc;
597  int i;
598 
599  /*
600  * Check for self-containment. Eventually we might be able to allow
601  * this (just return without complaint, if so) but it's not clear how
602  * many other places would require anti-recursion defenses before it
603  * would be safe to allow tables to contain their own rowtype.
604  */
605  if (list_member_oid(containing_rowtypes, atttypid))
606  ereport(ERROR,
607  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
608  errmsg("composite type %s cannot be made a member of itself",
609  format_type_be(atttypid))));
610 
611  containing_rowtypes = lappend_oid(containing_rowtypes, atttypid);
612 
613  relation = relation_open(get_typ_typrelid(atttypid), AccessShareLock);
614 
615  tupdesc = RelationGetDescr(relation);
616 
617  for (i = 0; i < tupdesc->natts; i++)
618  {
619  Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
620 
621  if (attr->attisdropped)
622  continue;
623  CheckAttributeType(NameStr(attr->attname),
624  attr->atttypid, attr->attcollation,
625  containing_rowtypes,
626  flags & ~CHKATYPE_IS_PARTKEY);
627  }
628 
629  relation_close(relation, AccessShareLock);
630 
631  containing_rowtypes = list_delete_last(containing_rowtypes);
632  }
633  else if (att_typtype == TYPTYPE_RANGE)
634  {
635  /*
636  * If it's a range, recurse to check its subtype.
637  */
639  get_range_collation(atttypid),
640  containing_rowtypes,
641  flags);
642  }
643  else if (OidIsValid((att_typelem = get_element_type(atttypid))))
644  {
645  /*
646  * Must recurse into array types, too, in case they are composite.
647  */
648  CheckAttributeType(attname, att_typelem, attcollation,
649  containing_rowtypes,
650  flags);
651  }
652 
653  /*
654  * This might not be strictly invalid per SQL standard, but it is pretty
655  * useless, and it cannot be dumped, so we must disallow it.
656  */
657  if (!OidIsValid(attcollation) && type_is_collatable(atttypid))
658  {
659  if (flags & CHKATYPE_IS_PARTKEY)
660  ereport(ERROR,
661  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
662  /* translator: first %s is an integer not a name */
663  errmsg("no collation was derived for partition key column %s with collatable type %s",
664  attname, format_type_be(atttypid)),
665  errhint("Use the COLLATE clause to set the collation explicitly.")));
666  else
667  ereport(ERROR,
668  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
669  errmsg("no collation was derived for column \"%s\" with collatable type %s",
670  attname, format_type_be(atttypid)),
671  errhint("Use the COLLATE clause to set the collation explicitly.")));
672  }
673 }
674 
675 /*
676  * InsertPgAttributeTuples
677  * Construct and insert a set of tuples in pg_attribute.
678  *
679  * Caller has already opened and locked pg_attribute. tupdesc contains the
680  * attributes to insert. attcacheoff is always initialized to -1. attoptions
681  * supplies the values for the attoptions fields and must contain the same
682  * number of elements as tupdesc or be NULL. The other variable-length fields
683  * of pg_attribute are always initialized to null values.
684  *
685  * indstate is the index state for CatalogTupleInsertWithInfo. It can be
686  * passed as NULL, in which case we'll fetch the necessary info. (Don't do
687  * this when inserting multiple attributes, because it's a tad more
688  * expensive.)
689  *
690  * new_rel_oid is the relation OID assigned to the attributes inserted.
691  * If set to InvalidOid, the relation OID from tupdesc is used instead.
692  */
693 void
695  TupleDesc tupdesc,
696  Oid new_rel_oid,
697  Datum *attoptions,
698  CatalogIndexState indstate)
699 {
700  TupleTableSlot **slot;
701  TupleDesc td;
702  int nslots;
703  int natts = 0;
704  int slotCount = 0;
705  bool close_index = false;
706 
707  td = RelationGetDescr(pg_attribute_rel);
708 
709  /* Initialize the number of slots to use */
710  nslots = Min(tupdesc->natts,
712  slot = palloc(sizeof(TupleTableSlot *) * nslots);
713  for (int i = 0; i < nslots; i++)
715 
716  while (natts < tupdesc->natts)
717  {
718  Form_pg_attribute attrs = TupleDescAttr(tupdesc, natts);
719 
720  ExecClearTuple(slot[slotCount]);
721 
722  memset(slot[slotCount]->tts_isnull, false,
723  slot[slotCount]->tts_tupleDescriptor->natts * sizeof(bool));
724 
725  if (new_rel_oid != InvalidOid)
726  slot[slotCount]->tts_values[Anum_pg_attribute_attrelid - 1] = ObjectIdGetDatum(new_rel_oid);
727  else
728  slot[slotCount]->tts_values[Anum_pg_attribute_attrelid - 1] = ObjectIdGetDatum(attrs->attrelid);
729 
730  slot[slotCount]->tts_values[Anum_pg_attribute_attname - 1] = NameGetDatum(&attrs->attname);
731  slot[slotCount]->tts_values[Anum_pg_attribute_atttypid - 1] = ObjectIdGetDatum(attrs->atttypid);
732  slot[slotCount]->tts_values[Anum_pg_attribute_attstattarget - 1] = Int32GetDatum(attrs->attstattarget);
733  slot[slotCount]->tts_values[Anum_pg_attribute_attlen - 1] = Int16GetDatum(attrs->attlen);
734  slot[slotCount]->tts_values[Anum_pg_attribute_attnum - 1] = Int16GetDatum(attrs->attnum);
735  slot[slotCount]->tts_values[Anum_pg_attribute_attndims - 1] = Int32GetDatum(attrs->attndims);
736  slot[slotCount]->tts_values[Anum_pg_attribute_attcacheoff - 1] = Int32GetDatum(-1);
737  slot[slotCount]->tts_values[Anum_pg_attribute_atttypmod - 1] = Int32GetDatum(attrs->atttypmod);
738  slot[slotCount]->tts_values[Anum_pg_attribute_attbyval - 1] = BoolGetDatum(attrs->attbyval);
739  slot[slotCount]->tts_values[Anum_pg_attribute_attalign - 1] = CharGetDatum(attrs->attalign);
740  slot[slotCount]->tts_values[Anum_pg_attribute_attstorage - 1] = CharGetDatum(attrs->attstorage);
741  slot[slotCount]->tts_values[Anum_pg_attribute_attcompression - 1] = CharGetDatum(attrs->attcompression);
742  slot[slotCount]->tts_values[Anum_pg_attribute_attnotnull - 1] = BoolGetDatum(attrs->attnotnull);
743  slot[slotCount]->tts_values[Anum_pg_attribute_atthasdef - 1] = BoolGetDatum(attrs->atthasdef);
744  slot[slotCount]->tts_values[Anum_pg_attribute_atthasmissing - 1] = BoolGetDatum(attrs->atthasmissing);
745  slot[slotCount]->tts_values[Anum_pg_attribute_attidentity - 1] = CharGetDatum(attrs->attidentity);
746  slot[slotCount]->tts_values[Anum_pg_attribute_attgenerated - 1] = CharGetDatum(attrs->attgenerated);
747  slot[slotCount]->tts_values[Anum_pg_attribute_attisdropped - 1] = BoolGetDatum(attrs->attisdropped);
748  slot[slotCount]->tts_values[Anum_pg_attribute_attislocal - 1] = BoolGetDatum(attrs->attislocal);
749  slot[slotCount]->tts_values[Anum_pg_attribute_attinhcount - 1] = Int32GetDatum(attrs->attinhcount);
750  slot[slotCount]->tts_values[Anum_pg_attribute_attcollation - 1] = ObjectIdGetDatum(attrs->attcollation);
751  if (attoptions && attoptions[natts] != (Datum) 0)
752  slot[slotCount]->tts_values[Anum_pg_attribute_attoptions - 1] = attoptions[natts];
753  else
754  slot[slotCount]->tts_isnull[Anum_pg_attribute_attoptions - 1] = true;
755 
756  /* start out with empty permissions and empty options */
757  slot[slotCount]->tts_isnull[Anum_pg_attribute_attacl - 1] = true;
758  slot[slotCount]->tts_isnull[Anum_pg_attribute_attfdwoptions - 1] = true;
759  slot[slotCount]->tts_isnull[Anum_pg_attribute_attmissingval - 1] = true;
760 
761  ExecStoreVirtualTuple(slot[slotCount]);
762  slotCount++;
763 
764  /*
765  * If slots are full or the end of processing has been reached, insert
766  * a batch of tuples.
767  */
768  if (slotCount == nslots || natts == tupdesc->natts - 1)
769  {
770  /* fetch index info only when we know we need it */
771  if (!indstate)
772  {
773  indstate = CatalogOpenIndexes(pg_attribute_rel);
774  close_index = true;
775  }
776 
777  /* insert the new tuples and update the indexes */
778  CatalogTuplesMultiInsertWithInfo(pg_attribute_rel, slot, slotCount,
779  indstate);
780  slotCount = 0;
781  }
782 
783  natts++;
784  }
785 
786  if (close_index)
787  CatalogCloseIndexes(indstate);
788  for (int i = 0; i < nslots; i++)
790  pfree(slot);
791 }
792 
793 /* --------------------------------
794  * AddNewAttributeTuples
795  *
796  * this registers the new relation's schema by adding
797  * tuples to pg_attribute.
798  * --------------------------------
799  */
800 static void
802  TupleDesc tupdesc,
803  char relkind)
804 {
805  Relation rel;
806  CatalogIndexState indstate;
807  int natts = tupdesc->natts;
808  ObjectAddress myself,
809  referenced;
810 
811  /*
812  * open pg_attribute and its indexes.
813  */
814  rel = table_open(AttributeRelationId, RowExclusiveLock);
815 
816  indstate = CatalogOpenIndexes(rel);
817 
818  /* set stats detail level to a sane default */
819  for (int i = 0; i < natts; i++)
820  tupdesc->attrs[i].attstattarget = -1;
821  InsertPgAttributeTuples(rel, tupdesc, new_rel_oid, NULL, indstate);
822 
823  /* add dependencies on their datatypes and collations */
824  for (int i = 0; i < natts; i++)
825  {
826  /* Add dependency info */
827  ObjectAddressSubSet(myself, RelationRelationId, new_rel_oid, i + 1);
828  ObjectAddressSet(referenced, TypeRelationId,
829  tupdesc->attrs[i].atttypid);
830  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
831 
832  /* The default collation is pinned, so don't bother recording it */
833  if (OidIsValid(tupdesc->attrs[i].attcollation) &&
834  tupdesc->attrs[i].attcollation != DEFAULT_COLLATION_OID)
835  {
836  ObjectAddressSet(referenced, CollationRelationId,
837  tupdesc->attrs[i].attcollation);
838  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
839  }
840  }
841 
842  /*
843  * Next we add the system attributes. Skip OID if rel has no OIDs. Skip
844  * all for a view or type relation. We don't bother with making datatype
845  * dependencies here, since presumably all these types are pinned.
846  */
847  if (relkind != RELKIND_VIEW && relkind != RELKIND_COMPOSITE_TYPE)
848  {
849  TupleDesc td;
850 
852 
853  InsertPgAttributeTuples(rel, td, new_rel_oid, NULL, indstate);
854  FreeTupleDesc(td);
855  }
856 
857  /*
858  * clean up
859  */
860  CatalogCloseIndexes(indstate);
861 
863 }
864 
865 /* --------------------------------
866  * InsertPgClassTuple
867  *
868  * Construct and insert a new tuple in pg_class.
869  *
870  * Caller has already opened and locked pg_class.
871  * Tuple data is taken from new_rel_desc->rd_rel, except for the
872  * variable-width fields which are not present in a cached reldesc.
873  * relacl and reloptions are passed in Datum form (to avoid having
874  * to reference the data types in heap.h). Pass (Datum) 0 to set them
875  * to NULL.
876  * --------------------------------
877  */
878 void
880  Relation new_rel_desc,
881  Oid new_rel_oid,
882  Datum relacl,
883  Datum reloptions)
884 {
885  Form_pg_class rd_rel = new_rel_desc->rd_rel;
886  Datum values[Natts_pg_class];
887  bool nulls[Natts_pg_class];
888  HeapTuple tup;
889 
890  /* This is a tad tedious, but way cleaner than what we used to do... */
891  memset(values, 0, sizeof(values));
892  memset(nulls, false, sizeof(nulls));
893 
894  values[Anum_pg_class_oid - 1] = ObjectIdGetDatum(new_rel_oid);
895  values[Anum_pg_class_relname - 1] = NameGetDatum(&rd_rel->relname);
896  values[Anum_pg_class_relnamespace - 1] = ObjectIdGetDatum(rd_rel->relnamespace);
897  values[Anum_pg_class_reltype - 1] = ObjectIdGetDatum(rd_rel->reltype);
898  values[Anum_pg_class_reloftype - 1] = ObjectIdGetDatum(rd_rel->reloftype);
899  values[Anum_pg_class_relowner - 1] = ObjectIdGetDatum(rd_rel->relowner);
900  values[Anum_pg_class_relam - 1] = ObjectIdGetDatum(rd_rel->relam);
901  values[Anum_pg_class_relfilenode - 1] = ObjectIdGetDatum(rd_rel->relfilenode);
902  values[Anum_pg_class_reltablespace - 1] = ObjectIdGetDatum(rd_rel->reltablespace);
903  values[Anum_pg_class_relpages - 1] = Int32GetDatum(rd_rel->relpages);
904  values[Anum_pg_class_reltuples - 1] = Float4GetDatum(rd_rel->reltuples);
905  values[Anum_pg_class_relallvisible - 1] = Int32GetDatum(rd_rel->relallvisible);
906  values[Anum_pg_class_reltoastrelid - 1] = ObjectIdGetDatum(rd_rel->reltoastrelid);
907  values[Anum_pg_class_relhasindex - 1] = BoolGetDatum(rd_rel->relhasindex);
908  values[Anum_pg_class_relisshared - 1] = BoolGetDatum(rd_rel->relisshared);
909  values[Anum_pg_class_relpersistence - 1] = CharGetDatum(rd_rel->relpersistence);
910  values[Anum_pg_class_relkind - 1] = CharGetDatum(rd_rel->relkind);
911  values[Anum_pg_class_relnatts - 1] = Int16GetDatum(rd_rel->relnatts);
912  values[Anum_pg_class_relchecks - 1] = Int16GetDatum(rd_rel->relchecks);
913  values[Anum_pg_class_relhasrules - 1] = BoolGetDatum(rd_rel->relhasrules);
914  values[Anum_pg_class_relhastriggers - 1] = BoolGetDatum(rd_rel->relhastriggers);
915  values[Anum_pg_class_relrowsecurity - 1] = BoolGetDatum(rd_rel->relrowsecurity);
916  values[Anum_pg_class_relforcerowsecurity - 1] = BoolGetDatum(rd_rel->relforcerowsecurity);
917  values[Anum_pg_class_relhassubclass - 1] = BoolGetDatum(rd_rel->relhassubclass);
918  values[Anum_pg_class_relispopulated - 1] = BoolGetDatum(rd_rel->relispopulated);
919  values[Anum_pg_class_relreplident - 1] = CharGetDatum(rd_rel->relreplident);
920  values[Anum_pg_class_relispartition - 1] = BoolGetDatum(rd_rel->relispartition);
921  values[Anum_pg_class_relrewrite - 1] = ObjectIdGetDatum(rd_rel->relrewrite);
922  values[Anum_pg_class_relfrozenxid - 1] = TransactionIdGetDatum(rd_rel->relfrozenxid);
923  values[Anum_pg_class_relminmxid - 1] = MultiXactIdGetDatum(rd_rel->relminmxid);
924  if (relacl != (Datum) 0)
925  values[Anum_pg_class_relacl - 1] = relacl;
926  else
927  nulls[Anum_pg_class_relacl - 1] = true;
928  if (reloptions != (Datum) 0)
929  values[Anum_pg_class_reloptions - 1] = reloptions;
930  else
931  nulls[Anum_pg_class_reloptions - 1] = true;
932 
933  /* relpartbound is set by updating this tuple, if necessary */
934  nulls[Anum_pg_class_relpartbound - 1] = true;
935 
936  tup = heap_form_tuple(RelationGetDescr(pg_class_desc), values, nulls);
937 
938  /* finally insert the new tuple, update the indexes, and clean up */
939  CatalogTupleInsert(pg_class_desc, tup);
940 
941  heap_freetuple(tup);
942 }
943 
944 /* --------------------------------
945  * AddNewRelationTuple
946  *
947  * this registers the new relation in the catalogs by
948  * adding a tuple to pg_class.
949  * --------------------------------
950  */
951 static void
953  Relation new_rel_desc,
954  Oid new_rel_oid,
955  Oid new_type_oid,
956  Oid reloftype,
957  Oid relowner,
958  char relkind,
959  TransactionId relfrozenxid,
960  TransactionId relminmxid,
961  Datum relacl,
962  Datum reloptions)
963 {
964  Form_pg_class new_rel_reltup;
965 
966  /*
967  * first we update some of the information in our uncataloged relation's
968  * relation descriptor.
969  */
970  new_rel_reltup = new_rel_desc->rd_rel;
971 
972  /* The relation is empty */
973  new_rel_reltup->relpages = 0;
974  new_rel_reltup->reltuples = -1;
975  new_rel_reltup->relallvisible = 0;
976 
977  /* Sequences always have a known size */
978  if (relkind == RELKIND_SEQUENCE)
979  {
980  new_rel_reltup->relpages = 1;
981  new_rel_reltup->reltuples = 1;
982  }
983 
984  new_rel_reltup->relfrozenxid = relfrozenxid;
985  new_rel_reltup->relminmxid = relminmxid;
986  new_rel_reltup->relowner = relowner;
987  new_rel_reltup->reltype = new_type_oid;
988  new_rel_reltup->reloftype = reloftype;
989 
990  /* relispartition is always set by updating this tuple later */
991  new_rel_reltup->relispartition = false;
992 
993  /* fill rd_att's type ID with something sane even if reltype is zero */
994  new_rel_desc->rd_att->tdtypeid = new_type_oid ? new_type_oid : RECORDOID;
995  new_rel_desc->rd_att->tdtypmod = -1;
996 
997  /* Now build and insert the tuple */
998  InsertPgClassTuple(pg_class_desc, new_rel_desc, new_rel_oid,
999  relacl, reloptions);
1000 }
1001 
1002 
1003 /* --------------------------------
1004  * AddNewRelationType -
1005  *
1006  * define a composite type corresponding to the new relation
1007  * --------------------------------
1008  */
1009 static ObjectAddress
1010 AddNewRelationType(const char *typeName,
1011  Oid typeNamespace,
1012  Oid new_rel_oid,
1013  char new_rel_kind,
1014  Oid ownerid,
1015  Oid new_row_type,
1016  Oid new_array_type)
1017 {
1018  return
1019  TypeCreate(new_row_type, /* optional predetermined OID */
1020  typeName, /* type name */
1021  typeNamespace, /* type namespace */
1022  new_rel_oid, /* relation oid */
1023  new_rel_kind, /* relation kind */
1024  ownerid, /* owner's ID */
1025  -1, /* internal size (varlena) */
1026  TYPTYPE_COMPOSITE, /* type-type (composite) */
1027  TYPCATEGORY_COMPOSITE, /* type-category (ditto) */
1028  false, /* composite types are never preferred */
1029  DEFAULT_TYPDELIM, /* default array delimiter */
1030  F_RECORD_IN, /* input procedure */
1031  F_RECORD_OUT, /* output procedure */
1032  F_RECORD_RECV, /* receive procedure */
1033  F_RECORD_SEND, /* send procedure */
1034  InvalidOid, /* typmodin procedure - none */
1035  InvalidOid, /* typmodout procedure - none */
1036  InvalidOid, /* analyze procedure - default */
1037  InvalidOid, /* subscript procedure - none */
1038  InvalidOid, /* array element type - irrelevant */
1039  false, /* this is not an array type */
1040  new_array_type, /* array type if any */
1041  InvalidOid, /* domain base type - irrelevant */
1042  NULL, /* default value - none */
1043  NULL, /* default binary representation */
1044  false, /* passed by reference */
1045  TYPALIGN_DOUBLE, /* alignment - must be the largest! */
1046  TYPSTORAGE_EXTENDED, /* fully TOASTable */
1047  -1, /* typmod */
1048  0, /* array dimensions for typBaseType */
1049  false, /* Type NOT NULL */
1050  InvalidOid); /* rowtypes never have a collation */
1051 }
1052 
1053 /* --------------------------------
1054  * heap_create_with_catalog
1055  *
1056  * creates a new cataloged relation. see comments above.
1057  *
1058  * Arguments:
1059  * relname: name to give to new rel
1060  * relnamespace: OID of namespace it goes in
1061  * reltablespace: OID of tablespace it goes in
1062  * relid: OID to assign to new rel, or InvalidOid to select a new OID
1063  * reltypeid: OID to assign to rel's rowtype, or InvalidOid to select one
1064  * reloftypeid: if a typed table, OID of underlying type; else InvalidOid
1065  * ownerid: OID of new rel's owner
1066  * accessmtd: OID of new rel's access method
1067  * tupdesc: tuple descriptor (source of column definitions)
1068  * cooked_constraints: list of precooked check constraints and defaults
1069  * relkind: relkind for new rel
1070  * relpersistence: rel's persistence status (permanent, temp, or unlogged)
1071  * shared_relation: true if it's to be a shared relation
1072  * mapped_relation: true if the relation will use the relfilenode map
1073  * oncommit: ON COMMIT marking (only relevant if it's a temp table)
1074  * reloptions: reloptions in Datum form, or (Datum) 0 if none
1075  * use_user_acl: true if should look for user-defined default permissions;
1076  * if false, relacl is always set NULL
1077  * allow_system_table_mods: true to allow creation in system namespaces
1078  * is_internal: is this a system-generated catalog?
1079  *
1080  * Output parameters:
1081  * typaddress: if not null, gets the object address of the new pg_type entry
1082  * (this must be null if the relkind is one that doesn't get a pg_type entry)
1083  *
1084  * Returns the OID of the new relation
1085  * --------------------------------
1086  */
1087 Oid
1089  Oid relnamespace,
1090  Oid reltablespace,
1091  Oid relid,
1092  Oid reltypeid,
1093  Oid reloftypeid,
1094  Oid ownerid,
1095  Oid accessmtd,
1096  TupleDesc tupdesc,
1097  List *cooked_constraints,
1098  char relkind,
1099  char relpersistence,
1100  bool shared_relation,
1101  bool mapped_relation,
1102  OnCommitAction oncommit,
1103  Datum reloptions,
1104  bool use_user_acl,
1105  bool allow_system_table_mods,
1106  bool is_internal,
1107  Oid relrewrite,
1108  ObjectAddress *typaddress)
1109 {
1110  Relation pg_class_desc;
1111  Relation new_rel_desc;
1112  Acl *relacl;
1113  Oid existing_relid;
1114  Oid old_type_oid;
1115  Oid new_type_oid;
1116 
1117  /* By default set to InvalidOid unless overridden by binary-upgrade */
1118  Oid relfilenode = InvalidOid;
1119  TransactionId relfrozenxid;
1120  MultiXactId relminmxid;
1121 
1122  pg_class_desc = table_open(RelationRelationId, RowExclusiveLock);
1123 
1124  /*
1125  * sanity checks
1126  */
1128 
1129  /*
1130  * Validate proposed tupdesc for the desired relkind. If
1131  * allow_system_table_mods is on, allow ANYARRAY to be used; this is a
1132  * hack to allow creating pg_statistic and cloning it during VACUUM FULL.
1133  */
1134  CheckAttributeNamesTypes(tupdesc, relkind,
1135  allow_system_table_mods ? CHKATYPE_ANYARRAY : 0);
1136 
1137  /*
1138  * This would fail later on anyway, if the relation already exists. But
1139  * by catching it here we can emit a nicer error message.
1140  */
1141  existing_relid = get_relname_relid(relname, relnamespace);
1142  if (existing_relid != InvalidOid)
1143  ereport(ERROR,
1144  (errcode(ERRCODE_DUPLICATE_TABLE),
1145  errmsg("relation \"%s\" already exists", relname)));
1146 
1147  /*
1148  * Since we are going to create a rowtype as well, also check for
1149  * collision with an existing type name. If there is one and it's an
1150  * autogenerated array, we can rename it out of the way; otherwise we can
1151  * at least give a good error message.
1152  */
1153  old_type_oid = GetSysCacheOid2(TYPENAMENSP, Anum_pg_type_oid,
1155  ObjectIdGetDatum(relnamespace));
1156  if (OidIsValid(old_type_oid))
1157  {
1158  if (!moveArrayTypeName(old_type_oid, relname, relnamespace))
1159  ereport(ERROR,
1161  errmsg("type \"%s\" already exists", relname),
1162  errhint("A relation has an associated type of the same name, "
1163  "so you must use a name that doesn't conflict "
1164  "with any existing type.")));
1165  }
1166 
1167  /*
1168  * Shared relations must be in pg_global (last-ditch check)
1169  */
1170  if (shared_relation && reltablespace != GLOBALTABLESPACE_OID)
1171  elog(ERROR, "shared relations must be placed in pg_global tablespace");
1172 
1173  /*
1174  * Allocate an OID for the relation, unless we were told what to use.
1175  *
1176  * The OID will be the relfilenode as well, so make sure it doesn't
1177  * collide with either pg_class OIDs or existing physical files.
1178  */
1179  if (!OidIsValid(relid))
1180  {
1181  /* Use binary-upgrade override for pg_class.oid and relfilenode */
1182  if (IsBinaryUpgrade)
1183  {
1184  /*
1185  * Indexes are not supported here; they use
1186  * binary_upgrade_next_index_pg_class_oid.
1187  */
1188  Assert(relkind != RELKIND_INDEX);
1189  Assert(relkind != RELKIND_PARTITIONED_INDEX);
1190 
1191  if (relkind == RELKIND_TOASTVALUE)
1192  {
1193  /* There might be no TOAST table, so we have to test for it. */
1195  {
1198 
1200  ereport(ERROR,
1201  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1202  errmsg("toast relfilenode value not set when in binary upgrade mode")));
1203 
1206  }
1207  }
1208  else
1209  {
1211  ereport(ERROR,
1212  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1213  errmsg("pg_class heap OID value not set when in binary upgrade mode")));
1214 
1217 
1218  if (RELKIND_HAS_STORAGE(relkind))
1219  {
1221  ereport(ERROR,
1222  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1223  errmsg("relfilenode value not set when in binary upgrade mode")));
1224 
1227  }
1228  }
1229  }
1230 
1231  if (!OidIsValid(relid))
1232  relid = GetNewRelFileNode(reltablespace, pg_class_desc,
1233  relpersistence);
1234  }
1235 
1236  /*
1237  * Determine the relation's initial permissions.
1238  */
1239  if (use_user_acl)
1240  {
1241  switch (relkind)
1242  {
1243  case RELKIND_RELATION:
1244  case RELKIND_VIEW:
1245  case RELKIND_MATVIEW:
1246  case RELKIND_FOREIGN_TABLE:
1247  case RELKIND_PARTITIONED_TABLE:
1248  relacl = get_user_default_acl(OBJECT_TABLE, ownerid,
1249  relnamespace);
1250  break;
1251  case RELKIND_SEQUENCE:
1252  relacl = get_user_default_acl(OBJECT_SEQUENCE, ownerid,
1253  relnamespace);
1254  break;
1255  default:
1256  relacl = NULL;
1257  break;
1258  }
1259  }
1260  else
1261  relacl = NULL;
1262 
1263  /*
1264  * Create the relcache entry (mostly dummy at this point) and the physical
1265  * disk file. (If we fail further down, it's the smgr's responsibility to
1266  * remove the disk file again.)
1267  *
1268  * NB: Note that passing create_storage = true is correct even for binary
1269  * upgrade. The storage we create here will be replaced later, but we
1270  * need to have something on disk in the meanwhile.
1271  */
1272  new_rel_desc = heap_create(relname,
1273  relnamespace,
1274  reltablespace,
1275  relid,
1276  relfilenode,
1277  accessmtd,
1278  tupdesc,
1279  relkind,
1280  relpersistence,
1281  shared_relation,
1282  mapped_relation,
1283  allow_system_table_mods,
1284  &relfrozenxid,
1285  &relminmxid,
1286  true);
1287 
1288  Assert(relid == RelationGetRelid(new_rel_desc));
1289 
1290  new_rel_desc->rd_rel->relrewrite = relrewrite;
1291 
1292  /*
1293  * Decide whether to create a pg_type entry for the relation's rowtype.
1294  * These types are made except where the use of a relation as such is an
1295  * implementation detail: toast tables, sequences and indexes.
1296  */
1297  if (!(relkind == RELKIND_SEQUENCE ||
1298  relkind == RELKIND_TOASTVALUE ||
1299  relkind == RELKIND_INDEX ||
1300  relkind == RELKIND_PARTITIONED_INDEX))
1301  {
1302  Oid new_array_oid;
1303  ObjectAddress new_type_addr;
1304  char *relarrayname;
1305 
1306  /*
1307  * We'll make an array over the composite type, too. For largely
1308  * historical reasons, the array type's OID is assigned first.
1309  */
1310  new_array_oid = AssignTypeArrayOid();
1311 
1312  /*
1313  * Make the pg_type entry for the composite type. The OID of the
1314  * composite type can be preselected by the caller, but if reltypeid
1315  * is InvalidOid, we'll generate a new OID for it.
1316  *
1317  * NOTE: we could get a unique-index failure here, in case someone
1318  * else is creating the same type name in parallel but hadn't
1319  * committed yet when we checked for a duplicate name above.
1320  */
1321  new_type_addr = AddNewRelationType(relname,
1322  relnamespace,
1323  relid,
1324  relkind,
1325  ownerid,
1326  reltypeid,
1327  new_array_oid);
1328  new_type_oid = new_type_addr.objectId;
1329  if (typaddress)
1330  *typaddress = new_type_addr;
1331 
1332  /* Now create the array type. */
1333  relarrayname = makeArrayTypeName(relname, relnamespace);
1334 
1335  TypeCreate(new_array_oid, /* force the type's OID to this */
1336  relarrayname, /* Array type name */
1337  relnamespace, /* Same namespace as parent */
1338  InvalidOid, /* Not composite, no relationOid */
1339  0, /* relkind, also N/A here */
1340  ownerid, /* owner's ID */
1341  -1, /* Internal size (varlena) */
1342  TYPTYPE_BASE, /* Not composite - typelem is */
1343  TYPCATEGORY_ARRAY, /* type-category (array) */
1344  false, /* array types are never preferred */
1345  DEFAULT_TYPDELIM, /* default array delimiter */
1346  F_ARRAY_IN, /* array input proc */
1347  F_ARRAY_OUT, /* array output proc */
1348  F_ARRAY_RECV, /* array recv (bin) proc */
1349  F_ARRAY_SEND, /* array send (bin) proc */
1350  InvalidOid, /* typmodin procedure - none */
1351  InvalidOid, /* typmodout procedure - none */
1352  F_ARRAY_TYPANALYZE, /* array analyze procedure */
1353  F_ARRAY_SUBSCRIPT_HANDLER, /* array subscript procedure */
1354  new_type_oid, /* array element type - the rowtype */
1355  true, /* yes, this is an array type */
1356  InvalidOid, /* this has no array type */
1357  InvalidOid, /* domain base type - irrelevant */
1358  NULL, /* default value - none */
1359  NULL, /* default binary representation */
1360  false, /* passed by reference */
1361  TYPALIGN_DOUBLE, /* alignment - must be the largest! */
1362  TYPSTORAGE_EXTENDED, /* fully TOASTable */
1363  -1, /* typmod */
1364  0, /* array dimensions for typBaseType */
1365  false, /* Type NOT NULL */
1366  InvalidOid); /* rowtypes never have a collation */
1367 
1368  pfree(relarrayname);
1369  }
1370  else
1371  {
1372  /* Caller should not be expecting a type to be created. */
1373  Assert(reltypeid == InvalidOid);
1374  Assert(typaddress == NULL);
1375 
1376  new_type_oid = InvalidOid;
1377  }
1378 
1379  /*
1380  * now create an entry in pg_class for the relation.
1381  *
1382  * NOTE: we could get a unique-index failure here, in case someone else is
1383  * creating the same relation name in parallel but hadn't committed yet
1384  * when we checked for a duplicate name above.
1385  */
1386  AddNewRelationTuple(pg_class_desc,
1387  new_rel_desc,
1388  relid,
1389  new_type_oid,
1390  reloftypeid,
1391  ownerid,
1392  relkind,
1393  relfrozenxid,
1394  relminmxid,
1395  PointerGetDatum(relacl),
1396  reloptions);
1397 
1398  /*
1399  * now add tuples to pg_attribute for the attributes in our new relation.
1400  */
1401  AddNewAttributeTuples(relid, new_rel_desc->rd_att, relkind);
1402 
1403  /*
1404  * Make a dependency link to force the relation to be deleted if its
1405  * namespace is. Also make a dependency link to its owner, as well as
1406  * dependencies for any roles mentioned in the default ACL.
1407  *
1408  * For composite types, these dependencies are tracked for the pg_type
1409  * entry, so we needn't record them here. Likewise, TOAST tables don't
1410  * need a namespace dependency (they live in a pinned namespace) nor an
1411  * owner dependency (they depend indirectly through the parent table), nor
1412  * should they have any ACL entries. The same applies for extension
1413  * dependencies.
1414  *
1415  * Also, skip this in bootstrap mode, since we don't make dependencies
1416  * while bootstrapping.
1417  */
1418  if (relkind != RELKIND_COMPOSITE_TYPE &&
1419  relkind != RELKIND_TOASTVALUE &&
1421  {
1422  ObjectAddress myself,
1423  referenced;
1424  ObjectAddresses *addrs;
1425 
1426  ObjectAddressSet(myself, RelationRelationId, relid);
1427 
1428  recordDependencyOnOwner(RelationRelationId, relid, ownerid);
1429 
1430  recordDependencyOnNewAcl(RelationRelationId, relid, 0, ownerid, relacl);
1431 
1432  recordDependencyOnCurrentExtension(&myself, false);
1433 
1434  addrs = new_object_addresses();
1435 
1436  ObjectAddressSet(referenced, NamespaceRelationId, relnamespace);
1437  add_exact_object_address(&referenced, addrs);
1438 
1439  if (reloftypeid)
1440  {
1441  ObjectAddressSet(referenced, TypeRelationId, reloftypeid);
1442  add_exact_object_address(&referenced, addrs);
1443  }
1444 
1445  /*
1446  * Make a dependency link to force the relation to be deleted if its
1447  * access method is.
1448  *
1449  * No need to add an explicit dependency for the toast table, as the
1450  * main table depends on it.
1451  */
1452  if (RELKIND_HAS_TABLE_AM(relkind) && relkind != RELKIND_TOASTVALUE)
1453  {
1454  ObjectAddressSet(referenced, AccessMethodRelationId, accessmtd);
1455  add_exact_object_address(&referenced, addrs);
1456  }
1457 
1459  free_object_addresses(addrs);
1460  }
1461 
1462  /* Post creation hook for new relation */
1463  InvokeObjectPostCreateHookArg(RelationRelationId, relid, 0, is_internal);
1464 
1465  /*
1466  * Store any supplied constraints and defaults.
1467  *
1468  * NB: this may do a CommandCounterIncrement and rebuild the relcache
1469  * entry, so the relation must be valid and self-consistent at this point.
1470  * In particular, there are not yet constraints and defaults anywhere.
1471  */
1472  StoreConstraints(new_rel_desc, cooked_constraints, is_internal);
1473 
1474  /*
1475  * If there's a special on-commit action, remember it
1476  */
1477  if (oncommit != ONCOMMIT_NOOP)
1478  register_on_commit_action(relid, oncommit);
1479 
1480  /* ensure that stats are dropped if transaction aborts */
1481  pgstat_create_relation(new_rel_desc);
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 }
1492 
1493 /*
1494  * RelationRemoveInheritance
1495  *
1496  * Formerly, this routine checked for child relations and aborted the
1497  * deletion if any were found. Now we rely on the dependency mechanism
1498  * to check for or delete child relations. By the time we get here,
1499  * there are no children and we need only remove any pg_inherits rows
1500  * linking this relation to its parent(s).
1501  */
1502 static void
1504 {
1505  Relation catalogRelation;
1506  SysScanDesc scan;
1507  ScanKeyData key;
1508  HeapTuple tuple;
1509 
1510  catalogRelation = table_open(InheritsRelationId, RowExclusiveLock);
1511 
1512  ScanKeyInit(&key,
1513  Anum_pg_inherits_inhrelid,
1514  BTEqualStrategyNumber, F_OIDEQ,
1515  ObjectIdGetDatum(relid));
1516 
1517  scan = systable_beginscan(catalogRelation, InheritsRelidSeqnoIndexId, true,
1518  NULL, 1, &key);
1519 
1520  while (HeapTupleIsValid(tuple = systable_getnext(scan)))
1521  CatalogTupleDelete(catalogRelation, &tuple->t_self);
1522 
1523  systable_endscan(scan);
1524  table_close(catalogRelation, RowExclusiveLock);
1525 }
1526 
1527 /*
1528  * DeleteRelationTuple
1529  *
1530  * Remove pg_class row for the given relid.
1531  *
1532  * Note: this is shared by relation deletion and index deletion. It's
1533  * not intended for use anyplace else.
1534  */
1535 void
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 }
1555 
1556 /*
1557  * DeleteAttributeTuples
1558  *
1559  * Remove pg_attribute rows for the given relid.
1560  *
1561  * Note: this is shared by relation deletion and index deletion. It's
1562  * not intended for use anyplace else.
1563  */
1564 void
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 }
1592 
1593 /*
1594  * DeleteSystemAttributeTuples
1595  *
1596  * Remove pg_attribute rows for system columns of the given relid.
1597  *
1598  * Note: this is only used when converting a table to a view. Views don't
1599  * have system columns, so we should remove them from pg_attribute.
1600  */
1601 void
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 }
1633 
1634 /*
1635  * RemoveAttributeById
1636  *
1637  * This is the guts of ALTER TABLE DROP COLUMN: actually mark the attribute
1638  * deleted in pg_attribute. We also remove pg_statistic entries for it.
1639  * (Everything else needed, such as getting rid of any pg_attrdef entry,
1640  * is handled by dependency.c.)
1641  */
1642 void
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  if (attnum < 0)
1670  {
1671  /* System attribute (probably OID) ... just delete the row */
1672 
1673  CatalogTupleDelete(attr_rel, &tuple->t_self);
1674  }
1675  else
1676  {
1677  /* Dropping user attributes is lots harder */
1678 
1679  /* Mark the attribute as dropped */
1680  attStruct->attisdropped = true;
1681 
1682  /*
1683  * Set the type OID to invalid. A dropped attribute's type link
1684  * cannot be relied on (once the attribute is dropped, the type might
1685  * be too). Fortunately we do not need the type row --- the only
1686  * really essential information is the type's typlen and typalign,
1687  * which are preserved in the attribute's attlen and attalign. We set
1688  * atttypid to zero here as a means of catching code that incorrectly
1689  * expects it to be valid.
1690  */
1691  attStruct->atttypid = InvalidOid;
1692 
1693  /* Remove any NOT NULL constraint the column may have */
1694  attStruct->attnotnull = false;
1695 
1696  /* We don't want to keep stats for it anymore */
1697  attStruct->attstattarget = 0;
1698 
1699  /* Unset this so no one tries to look up the generation expression */
1700  attStruct->attgenerated = '\0';
1701 
1702  /*
1703  * Change the column name to something that isn't likely to conflict
1704  */
1705  snprintf(newattname, sizeof(newattname),
1706  "........pg.dropped.%d........", attnum);
1707  namestrcpy(&(attStruct->attname), newattname);
1708 
1709  /* clear the missing value if any */
1710  if (attStruct->atthasmissing)
1711  {
1712  Datum valuesAtt[Natts_pg_attribute];
1713  bool nullsAtt[Natts_pg_attribute];
1714  bool replacesAtt[Natts_pg_attribute];
1715 
1716  /* update the tuple - set atthasmissing and attmissingval */
1717  MemSet(valuesAtt, 0, sizeof(valuesAtt));
1718  MemSet(nullsAtt, false, sizeof(nullsAtt));
1719  MemSet(replacesAtt, false, sizeof(replacesAtt));
1720 
1721  valuesAtt[Anum_pg_attribute_atthasmissing - 1] =
1722  BoolGetDatum(false);
1723  replacesAtt[Anum_pg_attribute_atthasmissing - 1] = true;
1724  valuesAtt[Anum_pg_attribute_attmissingval - 1] = (Datum) 0;
1725  nullsAtt[Anum_pg_attribute_attmissingval - 1] = true;
1726  replacesAtt[Anum_pg_attribute_attmissingval - 1] = true;
1727 
1728  tuple = heap_modify_tuple(tuple, RelationGetDescr(attr_rel),
1729  valuesAtt, nullsAtt, replacesAtt);
1730  }
1731 
1732  CatalogTupleUpdate(attr_rel, &tuple->t_self, tuple);
1733  }
1734 
1735  /*
1736  * Because updating the pg_attribute row will trigger a relcache flush for
1737  * the target relation, we need not do anything else to notify other
1738  * backends of the change.
1739  */
1740 
1741  table_close(attr_rel, RowExclusiveLock);
1742 
1743  if (attnum > 0)
1744  RemoveStatistics(relid, attnum);
1745 
1746  relation_close(rel, NoLock);
1747 }
1748 
1749 /*
1750  * heap_drop_with_catalog - removes specified relation from catalogs
1751  *
1752  * Note that this routine is not responsible for dropping objects that are
1753  * linked to the pg_class entry via dependencies (for example, indexes and
1754  * constraints). Those are deleted by the dependency-tracing logic in
1755  * dependency.c before control gets here. In general, therefore, this routine
1756  * should never be called directly; go through performDeletion() instead.
1757  */
1758 void
1760 {
1761  Relation rel;
1762  HeapTuple tuple;
1763  Oid parentOid = InvalidOid,
1764  defaultPartOid = InvalidOid;
1765 
1766  /*
1767  * To drop a partition safely, we must grab exclusive lock on its parent,
1768  * because another backend might be about to execute a query on the parent
1769  * table. If it relies on previously cached partition descriptor, then it
1770  * could attempt to access the just-dropped relation as its partition. We
1771  * must therefore take a table lock strong enough to prevent all queries
1772  * on the table from proceeding until we commit and send out a
1773  * shared-cache-inval notice that will make them update their partition
1774  * descriptors.
1775  */
1776  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
1777  if (!HeapTupleIsValid(tuple))
1778  elog(ERROR, "cache lookup failed for relation %u", relid);
1779  if (((Form_pg_class) GETSTRUCT(tuple))->relispartition)
1780  {
1781  /*
1782  * We have to lock the parent if the partition is being detached,
1783  * because it's possible that some query still has a partition
1784  * descriptor that includes this partition.
1785  */
1786  parentOid = get_partition_parent(relid, true);
1788 
1789  /*
1790  * If this is not the default partition, dropping it will change the
1791  * default partition's partition constraint, so we must lock it.
1792  */
1793  defaultPartOid = get_default_partition_oid(parentOid);
1794  if (OidIsValid(defaultPartOid) && relid != defaultPartOid)
1795  LockRelationOid(defaultPartOid, AccessExclusiveLock);
1796  }
1797 
1798  ReleaseSysCache(tuple);
1799 
1800  /*
1801  * Open and lock the relation.
1802  */
1803  rel = relation_open(relid, AccessExclusiveLock);
1804 
1805  /*
1806  * There can no longer be anyone *else* touching the relation, but we
1807  * might still have open queries or cursors, or pending trigger events, in
1808  * our own session.
1809  */
1810  CheckTableNotInUse(rel, "DROP TABLE");
1811 
1812  /*
1813  * This effectively deletes all rows in the table, and may be done in a
1814  * serializable transaction. In that case we must record a rw-conflict in
1815  * to this transaction from each transaction holding a predicate lock on
1816  * the table.
1817  */
1819 
1820  /*
1821  * Delete pg_foreign_table tuple first.
1822  */
1823  if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
1824  {
1825  Relation rel;
1826  HeapTuple tuple;
1827 
1828  rel = table_open(ForeignTableRelationId, RowExclusiveLock);
1829 
1831  if (!HeapTupleIsValid(tuple))
1832  elog(ERROR, "cache lookup failed for foreign table %u", relid);
1833 
1834  CatalogTupleDelete(rel, &tuple->t_self);
1835 
1836  ReleaseSysCache(tuple);
1838  }
1839 
1840  /*
1841  * If a partitioned table, delete the pg_partitioned_table tuple.
1842  */
1843  if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1845 
1846  /*
1847  * If the relation being dropped is the default partition itself,
1848  * invalidate its entry in pg_partitioned_table.
1849  */
1850  if (relid == defaultPartOid)
1852 
1853  /*
1854  * Schedule unlinking of the relation's physical files at commit.
1855  */
1856  if (RELKIND_HAS_STORAGE(rel->rd_rel->relkind))
1857  RelationDropStorage(rel);
1858 
1859  /* ensure that stats are dropped if transaction commits */
1860  pgstat_drop_relation(rel);
1861 
1862  /*
1863  * Close relcache entry, but *keep* AccessExclusiveLock on the relation
1864  * until transaction commit. This ensures no one else will try to do
1865  * something with the doomed relation.
1866  */
1867  relation_close(rel, NoLock);
1868 
1869  /*
1870  * Remove any associated relation synchronization states.
1871  */
1873 
1874  /*
1875  * Forget any ON COMMIT action for the rel
1876  */
1877  remove_on_commit_action(relid);
1878 
1879  /*
1880  * Flush the relation from the relcache. We want to do this before
1881  * starting to remove catalog entries, just to be certain that no relcache
1882  * entry rebuild will happen partway through. (That should not really
1883  * matter, since we don't do CommandCounterIncrement here, but let's be
1884  * safe.)
1885  */
1886  RelationForgetRelation(relid);
1887 
1888  /*
1889  * remove inheritance information
1890  */
1892 
1893  /*
1894  * delete statistics
1895  */
1896  RemoveStatistics(relid, 0);
1897 
1898  /*
1899  * delete attribute tuples
1900  */
1901  DeleteAttributeTuples(relid);
1902 
1903  /*
1904  * delete relation tuple
1905  */
1906  DeleteRelationTuple(relid);
1907 
1908  if (OidIsValid(parentOid))
1909  {
1910  /*
1911  * If this is not the default partition, the partition constraint of
1912  * the default partition has changed to include the portion of the key
1913  * space previously covered by the dropped partition.
1914  */
1915  if (OidIsValid(defaultPartOid) && relid != defaultPartOid)
1916  CacheInvalidateRelcacheByRelid(defaultPartOid);
1917 
1918  /*
1919  * Invalidate the parent's relcache so that the partition is no longer
1920  * included in its partition descriptor.
1921  */
1922  CacheInvalidateRelcacheByRelid(parentOid);
1923  /* keep the lock */
1924  }
1925 }
1926 
1927 
1928 /*
1929  * RelationClearMissing
1930  *
1931  * Set atthasmissing and attmissingval to false/null for all attributes
1932  * where they are currently set. This can be safely and usefully done if
1933  * the table is rewritten (e.g. by VACUUM FULL or CLUSTER) where we know there
1934  * are no rows left with less than a full complement of attributes.
1935  *
1936  * The caller must have an AccessExclusive lock on the relation.
1937  */
1938 void
1940 {
1941  Relation attr_rel;
1942  Oid relid = RelationGetRelid(rel);
1943  int natts = RelationGetNumberOfAttributes(rel);
1944  int attnum;
1945  Datum repl_val[Natts_pg_attribute];
1946  bool repl_null[Natts_pg_attribute];
1947  bool repl_repl[Natts_pg_attribute];
1948  Form_pg_attribute attrtuple;
1949  HeapTuple tuple,
1950  newtuple;
1951 
1952  memset(repl_val, 0, sizeof(repl_val));
1953  memset(repl_null, false, sizeof(repl_null));
1954  memset(repl_repl, false, sizeof(repl_repl));
1955 
1956  repl_val[Anum_pg_attribute_atthasmissing - 1] = BoolGetDatum(false);
1957  repl_null[Anum_pg_attribute_attmissingval - 1] = true;
1958 
1959  repl_repl[Anum_pg_attribute_atthasmissing - 1] = true;
1960  repl_repl[Anum_pg_attribute_attmissingval - 1] = true;
1961 
1962 
1963  /* Get a lock on pg_attribute */
1964  attr_rel = table_open(AttributeRelationId, RowExclusiveLock);
1965 
1966  /* process each non-system attribute, including any dropped columns */
1967  for (attnum = 1; attnum <= natts; attnum++)
1968  {
1969  tuple = SearchSysCache2(ATTNUM,
1970  ObjectIdGetDatum(relid),
1972  if (!HeapTupleIsValid(tuple)) /* shouldn't happen */
1973  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
1974  attnum, relid);
1975 
1976  attrtuple = (Form_pg_attribute) GETSTRUCT(tuple);
1977 
1978  /* ignore any where atthasmissing is not true */
1979  if (attrtuple->atthasmissing)
1980  {
1981  newtuple = heap_modify_tuple(tuple, RelationGetDescr(attr_rel),
1982  repl_val, repl_null, repl_repl);
1983 
1984  CatalogTupleUpdate(attr_rel, &newtuple->t_self, newtuple);
1985 
1986  heap_freetuple(newtuple);
1987  }
1988 
1989  ReleaseSysCache(tuple);
1990  }
1991 
1992  /*
1993  * Our update of the pg_attribute rows will force a relcache rebuild, so
1994  * there's nothing else to do here.
1995  */
1996  table_close(attr_rel, RowExclusiveLock);
1997 }
1998 
1999 /*
2000  * SetAttrMissing
2001  *
2002  * Set the missing value of a single attribute. This should only be used by
2003  * binary upgrade. Takes an AccessExclusive lock on the relation owning the
2004  * attribute.
2005  */
2006 void
2007 SetAttrMissing(Oid relid, char *attname, char *value)
2008 {
2009  Datum valuesAtt[Natts_pg_attribute];
2010  bool nullsAtt[Natts_pg_attribute];
2011  bool replacesAtt[Natts_pg_attribute];
2012  Datum missingval;
2013  Form_pg_attribute attStruct;
2014  Relation attrrel,
2015  tablerel;
2016  HeapTuple atttup,
2017  newtup;
2018 
2019  /* lock the table the attribute belongs to */
2020  tablerel = table_open(relid, AccessExclusiveLock);
2021 
2022  /* Don't do anything unless it's a plain table */
2023  if (tablerel->rd_rel->relkind != RELKIND_RELATION)
2024  {
2025  table_close(tablerel, AccessExclusiveLock);
2026  return;
2027  }
2028 
2029  /* Lock the attribute row and get the data */
2030  attrrel = table_open(AttributeRelationId, RowExclusiveLock);
2031  atttup = SearchSysCacheAttName(relid, attname);
2032  if (!HeapTupleIsValid(atttup))
2033  elog(ERROR, "cache lookup failed for attribute %s of relation %u",
2034  attname, relid);
2035  attStruct = (Form_pg_attribute) GETSTRUCT(atttup);
2036 
2037  /* get an array value from the value string */
2038  missingval = OidFunctionCall3(F_ARRAY_IN,
2040  ObjectIdGetDatum(attStruct->atttypid),
2041  Int32GetDatum(attStruct->atttypmod));
2042 
2043  /* update the tuple - set atthasmissing and attmissingval */
2044  MemSet(valuesAtt, 0, sizeof(valuesAtt));
2045  MemSet(nullsAtt, false, sizeof(nullsAtt));
2046  MemSet(replacesAtt, false, sizeof(replacesAtt));
2047 
2048  valuesAtt[Anum_pg_attribute_atthasmissing - 1] = BoolGetDatum(true);
2049  replacesAtt[Anum_pg_attribute_atthasmissing - 1] = true;
2050  valuesAtt[Anum_pg_attribute_attmissingval - 1] = missingval;
2051  replacesAtt[Anum_pg_attribute_attmissingval - 1] = true;
2052 
2053  newtup = heap_modify_tuple(atttup, RelationGetDescr(attrrel),
2054  valuesAtt, nullsAtt, replacesAtt);
2055  CatalogTupleUpdate(attrrel, &newtup->t_self, newtup);
2056 
2057  /* clean up */
2058  ReleaseSysCache(atttup);
2059  table_close(attrrel, RowExclusiveLock);
2060  table_close(tablerel, AccessExclusiveLock);
2061 }
2062 
2063 /*
2064  * Store a check-constraint expression for the given relation.
2065  *
2066  * Caller is responsible for updating the count of constraints
2067  * in the pg_class entry for the relation.
2068  *
2069  * The OID of the new constraint is returned.
2070  */
2071 static Oid
2072 StoreRelCheck(Relation rel, const char *ccname, Node *expr,
2073  bool is_validated, bool is_local, int inhcount,
2074  bool is_no_inherit, bool is_internal)
2075 {
2076  char *ccbin;
2077  List *varList;
2078  int keycount;
2079  int16 *attNos;
2080  Oid constrOid;
2081 
2082  /*
2083  * Flatten expression to string form for storage.
2084  */
2085  ccbin = nodeToString(expr);
2086 
2087  /*
2088  * Find columns of rel that are used in expr
2089  *
2090  * NB: pull_var_clause is okay here only because we don't allow subselects
2091  * in check constraints; it would fail to examine the contents of
2092  * subselects.
2093  */
2094  varList = pull_var_clause(expr, 0);
2095  keycount = list_length(varList);
2096 
2097  if (keycount > 0)
2098  {
2099  ListCell *vl;
2100  int i = 0;
2101 
2102  attNos = (int16 *) palloc(keycount * sizeof(int16));
2103  foreach(vl, varList)
2104  {
2105  Var *var = (Var *) lfirst(vl);
2106  int j;
2107 
2108  for (j = 0; j < i; j++)
2109  if (attNos[j] == var->varattno)
2110  break;
2111  if (j == i)
2112  attNos[i++] = var->varattno;
2113  }
2114  keycount = i;
2115  }
2116  else
2117  attNos = NULL;
2118 
2119  /*
2120  * Partitioned tables do not contain any rows themselves, so a NO INHERIT
2121  * constraint makes no sense.
2122  */
2123  if (is_no_inherit &&
2124  rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
2125  ereport(ERROR,
2126  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
2127  errmsg("cannot add NO INHERIT constraint to partitioned table \"%s\"",
2128  RelationGetRelationName(rel))));
2129 
2130  /*
2131  * Create the Check Constraint
2132  */
2133  constrOid =
2134  CreateConstraintEntry(ccname, /* Constraint Name */
2135  RelationGetNamespace(rel), /* namespace */
2136  CONSTRAINT_CHECK, /* Constraint Type */
2137  false, /* Is Deferrable */
2138  false, /* Is Deferred */
2139  is_validated,
2140  InvalidOid, /* no parent constraint */
2141  RelationGetRelid(rel), /* relation */
2142  attNos, /* attrs in the constraint */
2143  keycount, /* # key attrs in the constraint */
2144  keycount, /* # total attrs in the constraint */
2145  InvalidOid, /* not a domain constraint */
2146  InvalidOid, /* no associated index */
2147  InvalidOid, /* Foreign key fields */
2148  NULL,
2149  NULL,
2150  NULL,
2151  NULL,
2152  0,
2153  ' ',
2154  ' ',
2155  NULL,
2156  0,
2157  ' ',
2158  NULL, /* not an exclusion constraint */
2159  expr, /* Tree form of check constraint */
2160  ccbin, /* Binary form of check constraint */
2161  is_local, /* conislocal */
2162  inhcount, /* coninhcount */
2163  is_no_inherit, /* connoinherit */
2164  is_internal); /* internally constructed? */
2165 
2166  pfree(ccbin);
2167 
2168  return constrOid;
2169 }
2170 
2171 /*
2172  * Store defaults and constraints (passed as a list of CookedConstraint).
2173  *
2174  * Each CookedConstraint struct is modified to store the new catalog tuple OID.
2175  *
2176  * NOTE: only pre-cooked expressions will be passed this way, which is to
2177  * say expressions inherited from an existing relation. Newly parsed
2178  * expressions can be added later, by direct calls to StoreAttrDefault
2179  * and StoreRelCheck (see AddRelationNewConstraints()).
2180  */
2181 static void
2182 StoreConstraints(Relation rel, List *cooked_constraints, bool is_internal)
2183 {
2184  int numchecks = 0;
2185  ListCell *lc;
2186 
2187  if (cooked_constraints == NIL)
2188  return; /* nothing to do */
2189 
2190  /*
2191  * Deparsing of constraint expressions will fail unless the just-created
2192  * pg_attribute tuples for this relation are made visible. So, bump the
2193  * command counter. CAUTION: this will cause a relcache entry rebuild.
2194  */
2196 
2197  foreach(lc, cooked_constraints)
2198  {
2199  CookedConstraint *con = (CookedConstraint *) lfirst(lc);
2200 
2201  switch (con->contype)
2202  {
2203  case CONSTR_DEFAULT:
2204  con->conoid = StoreAttrDefault(rel, con->attnum, con->expr,
2205  is_internal, false);
2206  break;
2207  case CONSTR_CHECK:
2208  con->conoid =
2209  StoreRelCheck(rel, con->name, con->expr,
2210  !con->skip_validation, con->is_local,
2211  con->inhcount, con->is_no_inherit,
2212  is_internal);
2213  numchecks++;
2214  break;
2215  default:
2216  elog(ERROR, "unrecognized constraint type: %d",
2217  (int) con->contype);
2218  }
2219  }
2220 
2221  if (numchecks > 0)
2222  SetRelationNumChecks(rel, numchecks);
2223 }
2224 
2225 /*
2226  * AddRelationNewConstraints
2227  *
2228  * Add new column default expressions and/or constraint check expressions
2229  * to an existing relation. This is defined to do both for efficiency in
2230  * DefineRelation, but of course you can do just one or the other by passing
2231  * empty lists.
2232  *
2233  * rel: relation to be modified
2234  * newColDefaults: list of RawColumnDefault structures
2235  * newConstraints: list of Constraint nodes
2236  * allow_merge: true if check constraints may be merged with existing ones
2237  * is_local: true if definition is local, false if it's inherited
2238  * is_internal: true if result of some internal process, not a user request
2239  *
2240  * All entries in newColDefaults will be processed. Entries in newConstraints
2241  * will be processed only if they are CONSTR_CHECK type.
2242  *
2243  * Returns a list of CookedConstraint nodes that shows the cooked form of
2244  * the default and constraint expressions added to the relation.
2245  *
2246  * NB: caller should have opened rel with some self-conflicting lock mode,
2247  * and should hold that lock till end of transaction; for normal cases that'll
2248  * be AccessExclusiveLock, but if caller knows that the constraint is already
2249  * enforced by some other means, it can be ShareUpdateExclusiveLock. Also, we
2250  * assume the caller has done a CommandCounterIncrement if necessary to make
2251  * the relation's catalog tuples visible.
2252  */
2253 List *
2255  List *newColDefaults,
2256  List *newConstraints,
2257  bool allow_merge,
2258  bool is_local,
2259  bool is_internal,
2260  const char *queryString)
2261 {
2262  List *cookedConstraints = NIL;
2263  TupleDesc tupleDesc;
2264  TupleConstr *oldconstr;
2265  int numoldchecks;
2266  ParseState *pstate;
2267  ParseNamespaceItem *nsitem;
2268  int numchecks;
2269  List *checknames;
2270  ListCell *cell;
2271  Node *expr;
2272  CookedConstraint *cooked;
2273 
2274  /*
2275  * Get info about existing constraints.
2276  */
2277  tupleDesc = RelationGetDescr(rel);
2278  oldconstr = tupleDesc->constr;
2279  if (oldconstr)
2280  numoldchecks = oldconstr->num_check;
2281  else
2282  numoldchecks = 0;
2283 
2284  /*
2285  * Create a dummy ParseState and insert the target relation as its sole
2286  * rangetable entry. We need a ParseState for transformExpr.
2287  */
2288  pstate = make_parsestate(NULL);
2289  pstate->p_sourcetext = queryString;
2290  nsitem = addRangeTableEntryForRelation(pstate,
2291  rel,
2293  NULL,
2294  false,
2295  true);
2296  addNSItemToQuery(pstate, nsitem, true, true, true);
2297 
2298  /*
2299  * Process column default expressions.
2300  */
2301  foreach(cell, newColDefaults)
2302  {
2303  RawColumnDefault *colDef = (RawColumnDefault *) lfirst(cell);
2304  Form_pg_attribute atp = TupleDescAttr(rel->rd_att, colDef->attnum - 1);
2305  Oid defOid;
2306 
2307  expr = cookDefault(pstate, colDef->raw_default,
2308  atp->atttypid, atp->atttypmod,
2309  NameStr(atp->attname),
2310  atp->attgenerated);
2311 
2312  /*
2313  * If the expression is just a NULL constant, we do not bother to make
2314  * an explicit pg_attrdef entry, since the default behavior is
2315  * equivalent. This applies to column defaults, but not for
2316  * generation expressions.
2317  *
2318  * Note a nonobvious property of this test: if the column is of a
2319  * domain type, what we'll get is not a bare null Const but a
2320  * CoerceToDomain expr, so we will not discard the default. This is
2321  * critical because the column default needs to be retained to
2322  * override any default that the domain might have.
2323  */
2324  if (expr == NULL ||
2325  (!colDef->generated &&
2326  IsA(expr, Const) &&
2327  castNode(Const, expr)->constisnull))
2328  continue;
2329 
2330  /* If the DEFAULT is volatile we cannot use a missing value */
2331  if (colDef->missingMode && contain_volatile_functions((Node *) expr))
2332  colDef->missingMode = false;
2333 
2334  defOid = StoreAttrDefault(rel, colDef->attnum, expr, is_internal,
2335  colDef->missingMode);
2336 
2337  cooked = (CookedConstraint *) palloc(sizeof(CookedConstraint));
2338  cooked->contype = CONSTR_DEFAULT;
2339  cooked->conoid = defOid;
2340  cooked->name = NULL;
2341  cooked->attnum = colDef->attnum;
2342  cooked->expr = expr;
2343  cooked->skip_validation = false;
2344  cooked->is_local = is_local;
2345  cooked->inhcount = is_local ? 0 : 1;
2346  cooked->is_no_inherit = false;
2347  cookedConstraints = lappend(cookedConstraints, cooked);
2348  }
2349 
2350  /*
2351  * Process constraint expressions.
2352  */
2353  numchecks = numoldchecks;
2354  checknames = NIL;
2355  foreach(cell, newConstraints)
2356  {
2357  Constraint *cdef = (Constraint *) lfirst(cell);
2358  char *ccname;
2359  Oid constrOid;
2360 
2361  if (cdef->contype != CONSTR_CHECK)
2362  continue;
2363 
2364  if (cdef->raw_expr != NULL)
2365  {
2366  Assert(cdef->cooked_expr == NULL);
2367 
2368  /*
2369  * Transform raw parsetree to executable expression, and verify
2370  * it's valid as a CHECK constraint.
2371  */
2372  expr = cookConstraint(pstate, cdef->raw_expr,
2374  }
2375  else
2376  {
2377  Assert(cdef->cooked_expr != NULL);
2378 
2379  /*
2380  * Here, we assume the parser will only pass us valid CHECK
2381  * expressions, so we do no particular checking.
2382  */
2383  expr = stringToNode(cdef->cooked_expr);
2384  }
2385 
2386  /*
2387  * Check name uniqueness, or generate a name if none was given.
2388  */
2389  if (cdef->conname != NULL)
2390  {
2391  ListCell *cell2;
2392 
2393  ccname = cdef->conname;
2394  /* Check against other new constraints */
2395  /* Needed because we don't do CommandCounterIncrement in loop */
2396  foreach(cell2, checknames)
2397  {
2398  if (strcmp((char *) lfirst(cell2), ccname) == 0)
2399  ereport(ERROR,
2401  errmsg("check constraint \"%s\" already exists",
2402  ccname)));
2403  }
2404 
2405  /* save name for future checks */
2406  checknames = lappend(checknames, ccname);
2407 
2408  /*
2409  * Check against pre-existing constraints. If we are allowed to
2410  * merge with an existing constraint, there's no more to do here.
2411  * (We omit the duplicate constraint from the result, which is
2412  * what ATAddCheckConstraint wants.)
2413  */
2414  if (MergeWithExistingConstraint(rel, ccname, expr,
2415  allow_merge, is_local,
2416  cdef->initially_valid,
2417  cdef->is_no_inherit))
2418  continue;
2419  }
2420  else
2421  {
2422  /*
2423  * When generating a name, we want to create "tab_col_check" for a
2424  * column constraint and "tab_check" for a table constraint. We
2425  * no longer have any info about the syntactic positioning of the
2426  * constraint phrase, so we approximate this by seeing whether the
2427  * expression references more than one column. (If the user
2428  * played by the rules, the result is the same...)
2429  *
2430  * Note: pull_var_clause() doesn't descend into sublinks, but we
2431  * eliminated those above; and anyway this only needs to be an
2432  * approximate answer.
2433  */
2434  List *vars;
2435  char *colname;
2436 
2437  vars = pull_var_clause(expr, 0);
2438 
2439  /* eliminate duplicates */
2440  vars = list_union(NIL, vars);
2441 
2442  if (list_length(vars) == 1)
2443  colname = get_attname(RelationGetRelid(rel),
2444  ((Var *) linitial(vars))->varattno,
2445  true);
2446  else
2447  colname = NULL;
2448 
2450  colname,
2451  "check",
2452  RelationGetNamespace(rel),
2453  checknames);
2454 
2455  /* save name for future checks */
2456  checknames = lappend(checknames, ccname);
2457  }
2458 
2459  /*
2460  * OK, store it.
2461  */
2462  constrOid =
2463  StoreRelCheck(rel, ccname, expr, cdef->initially_valid, is_local,
2464  is_local ? 0 : 1, cdef->is_no_inherit, is_internal);
2465 
2466  numchecks++;
2467 
2468  cooked = (CookedConstraint *) palloc(sizeof(CookedConstraint));
2469  cooked->contype = CONSTR_CHECK;
2470  cooked->conoid = constrOid;
2471  cooked->name = ccname;
2472  cooked->attnum = 0;
2473  cooked->expr = expr;
2474  cooked->skip_validation = cdef->skip_validation;
2475  cooked->is_local = is_local;
2476  cooked->inhcount = is_local ? 0 : 1;
2477  cooked->is_no_inherit = cdef->is_no_inherit;
2478  cookedConstraints = lappend(cookedConstraints, cooked);
2479  }
2480 
2481  /*
2482  * Update the count of constraints in the relation's pg_class tuple. We do
2483  * this even if there was no change, in order to ensure that an SI update
2484  * message is sent out for the pg_class tuple, which will force other
2485  * backends to rebuild their relcache entries for the rel. (This is
2486  * critical if we added defaults but not constraints.)
2487  */
2488  SetRelationNumChecks(rel, numchecks);
2489 
2490  return cookedConstraints;
2491 }
2492 
2493 /*
2494  * Check for a pre-existing check constraint that conflicts with a proposed
2495  * new one, and either adjust its conislocal/coninhcount settings or throw
2496  * error as needed.
2497  *
2498  * Returns true if merged (constraint is a duplicate), or false if it's
2499  * got a so-far-unique name, or throws error if conflict.
2500  *
2501  * XXX See MergeConstraintsIntoExisting too if you change this code.
2502  */
2503 static bool
2504 MergeWithExistingConstraint(Relation rel, const char *ccname, Node *expr,
2505  bool allow_merge, bool is_local,
2506  bool is_initially_valid,
2507  bool is_no_inherit)
2508 {
2509  bool found;
2510  Relation conDesc;
2511  SysScanDesc conscan;
2512  ScanKeyData skey[3];
2513  HeapTuple tup;
2514 
2515  /* Search for a pg_constraint entry with same name and relation */
2516  conDesc = table_open(ConstraintRelationId, RowExclusiveLock);
2517 
2518  found = false;
2519 
2520  ScanKeyInit(&skey[0],
2521  Anum_pg_constraint_conrelid,
2522  BTEqualStrategyNumber, F_OIDEQ,
2524  ScanKeyInit(&skey[1],
2525  Anum_pg_constraint_contypid,
2526  BTEqualStrategyNumber, F_OIDEQ,
2528  ScanKeyInit(&skey[2],
2529  Anum_pg_constraint_conname,
2530  BTEqualStrategyNumber, F_NAMEEQ,
2531  CStringGetDatum(ccname));
2532 
2533  conscan = systable_beginscan(conDesc, ConstraintRelidTypidNameIndexId, true,
2534  NULL, 3, skey);
2535 
2536  /* There can be at most one matching row */
2537  if (HeapTupleIsValid(tup = systable_getnext(conscan)))
2538  {
2540 
2541  /* Found it. Conflicts if not identical check constraint */
2542  if (con->contype == CONSTRAINT_CHECK)
2543  {
2544  Datum val;
2545  bool isnull;
2546 
2547  val = fastgetattr(tup,
2548  Anum_pg_constraint_conbin,
2549  conDesc->rd_att, &isnull);
2550  if (isnull)
2551  elog(ERROR, "null conbin for rel %s",
2554  found = true;
2555  }
2556 
2557  /*
2558  * If the existing constraint is purely inherited (no local
2559  * definition) then interpret addition of a local constraint as a
2560  * legal merge. This allows ALTER ADD CONSTRAINT on parent and child
2561  * tables to be given in either order with same end state. However if
2562  * the relation is a partition, all inherited constraints are always
2563  * non-local, including those that were merged.
2564  */
2565  if (is_local && !con->conislocal && !rel->rd_rel->relispartition)
2566  allow_merge = true;
2567 
2568  if (!found || !allow_merge)
2569  ereport(ERROR,
2571  errmsg("constraint \"%s\" for relation \"%s\" already exists",
2572  ccname, RelationGetRelationName(rel))));
2573 
2574  /* If the child constraint is "no inherit" then cannot merge */
2575  if (con->connoinherit)
2576  ereport(ERROR,
2577  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2578  errmsg("constraint \"%s\" conflicts with non-inherited constraint on relation \"%s\"",
2579  ccname, RelationGetRelationName(rel))));
2580 
2581  /*
2582  * Must not change an existing inherited constraint to "no inherit"
2583  * status. That's because inherited constraints should be able to
2584  * propagate to lower-level children.
2585  */
2586  if (con->coninhcount > 0 && is_no_inherit)
2587  ereport(ERROR,
2588  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2589  errmsg("constraint \"%s\" conflicts with inherited constraint on relation \"%s\"",
2590  ccname, RelationGetRelationName(rel))));
2591 
2592  /*
2593  * If the child constraint is "not valid" then cannot merge with a
2594  * valid parent constraint.
2595  */
2596  if (is_initially_valid && !con->convalidated)
2597  ereport(ERROR,
2598  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2599  errmsg("constraint \"%s\" conflicts with NOT VALID constraint on relation \"%s\"",
2600  ccname, RelationGetRelationName(rel))));
2601 
2602  /* OK to update the tuple */
2603  ereport(NOTICE,
2604  (errmsg("merging constraint \"%s\" with inherited definition",
2605  ccname)));
2606 
2607  tup = heap_copytuple(tup);
2608  con = (Form_pg_constraint) GETSTRUCT(tup);
2609 
2610  /*
2611  * In case of partitions, an inherited constraint must be inherited
2612  * only once since it cannot have multiple parents and it is never
2613  * considered local.
2614  */
2615  if (rel->rd_rel->relispartition)
2616  {
2617  con->coninhcount = 1;
2618  con->conislocal = false;
2619  }
2620  else
2621  {
2622  if (is_local)
2623  con->conislocal = true;
2624  else
2625  con->coninhcount++;
2626  }
2627 
2628  if (is_no_inherit)
2629  {
2630  Assert(is_local);
2631  con->connoinherit = true;
2632  }
2633 
2634  CatalogTupleUpdate(conDesc, &tup->t_self, tup);
2635  }
2636 
2637  systable_endscan(conscan);
2638  table_close(conDesc, RowExclusiveLock);
2639 
2640  return found;
2641 }
2642 
2643 /*
2644  * Update the count of constraints in the relation's pg_class tuple.
2645  *
2646  * Caller had better hold exclusive lock on the relation.
2647  *
2648  * An important side effect is that a SI update message will be sent out for
2649  * the pg_class tuple, which will force other backends to rebuild their
2650  * relcache entries for the rel. Also, this backend will rebuild its
2651  * own relcache entry at the next CommandCounterIncrement.
2652  */
2653 static void
2654 SetRelationNumChecks(Relation rel, int numchecks)
2655 {
2656  Relation relrel;
2657  HeapTuple reltup;
2658  Form_pg_class relStruct;
2659 
2660  relrel = table_open(RelationRelationId, RowExclusiveLock);
2661  reltup = SearchSysCacheCopy1(RELOID,
2663  if (!HeapTupleIsValid(reltup))
2664  elog(ERROR, "cache lookup failed for relation %u",
2665  RelationGetRelid(rel));
2666  relStruct = (Form_pg_class) GETSTRUCT(reltup);
2667 
2668  if (relStruct->relchecks != numchecks)
2669  {
2670  relStruct->relchecks = numchecks;
2671 
2672  CatalogTupleUpdate(relrel, &reltup->t_self, reltup);
2673  }
2674  else
2675  {
2676  /* Skip the disk update, but force relcache inval anyway */
2678  }
2679 
2680  heap_freetuple(reltup);
2681  table_close(relrel, RowExclusiveLock);
2682 }
2683 
2684 /*
2685  * Check for references to generated columns
2686  */
2687 static bool
2689 {
2690  ParseState *pstate = context;
2691 
2692  if (node == NULL)
2693  return false;
2694  else if (IsA(node, Var))
2695  {
2696  Var *var = (Var *) node;
2697  Oid relid;
2699 
2700  relid = rt_fetch(var->varno, pstate->p_rtable)->relid;
2701  if (!OidIsValid(relid))
2702  return false; /* XXX shouldn't we raise an error? */
2703 
2704  attnum = var->varattno;
2705 
2706  if (attnum > 0 && get_attgenerated(relid, attnum))
2707  ereport(ERROR,
2708  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2709  errmsg("cannot use generated column \"%s\" in column generation expression",
2710  get_attname(relid, attnum, false)),
2711  errdetail("A generated column cannot reference another generated column."),
2712  parser_errposition(pstate, var->location)));
2713  /* A whole-row Var is necessarily self-referential, so forbid it */
2714  if (attnum == 0)
2715  ereport(ERROR,
2716  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2717  errmsg("cannot use whole-row variable in column generation expression"),
2718  errdetail("This would cause the generated column to depend on its own value."),
2719  parser_errposition(pstate, var->location)));
2720  /* System columns were already checked in the parser */
2721 
2722  return false;
2723  }
2724  else
2726  (void *) context);
2727 }
2728 
2729 static void
2731 {
2732  check_nested_generated_walker(node, pstate);
2733 }
2734 
2735 /*
2736  * Take a raw default and convert it to a cooked format ready for
2737  * storage.
2738  *
2739  * Parse state should be set up to recognize any vars that might appear
2740  * in the expression. (Even though we plan to reject vars, it's more
2741  * user-friendly to give the correct error message than "unknown var".)
2742  *
2743  * If atttypid is not InvalidOid, coerce the expression to the specified
2744  * type (and typmod atttypmod). attname is only needed in this case:
2745  * it is used in the error message, if any.
2746  */
2747 Node *
2749  Node *raw_default,
2750  Oid atttypid,
2751  int32 atttypmod,
2752  const char *attname,
2753  char attgenerated)
2754 {
2755  Node *expr;
2756 
2757  Assert(raw_default != NULL);
2758 
2759  /*
2760  * Transform raw parsetree to executable expression.
2761  */
2762  expr = transformExpr(pstate, raw_default, attgenerated ? EXPR_KIND_GENERATED_COLUMN : EXPR_KIND_COLUMN_DEFAULT);
2763 
2764  if (attgenerated)
2765  {
2766  check_nested_generated(pstate, expr);
2767 
2768  if (contain_mutable_functions(expr))
2769  ereport(ERROR,
2770  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2771  errmsg("generation expression is not immutable")));
2772  }
2773  else
2774  {
2775  /*
2776  * For a default expression, transformExpr() should have rejected
2777  * column references.
2778  */
2779  Assert(!contain_var_clause(expr));
2780  }
2781 
2782  /*
2783  * Coerce the expression to the correct type and typmod, if given. This
2784  * should match the parser's processing of non-defaulted expressions ---
2785  * see transformAssignedExpr().
2786  */
2787  if (OidIsValid(atttypid))
2788  {
2789  Oid type_id = exprType(expr);
2790 
2791  expr = coerce_to_target_type(pstate, expr, type_id,
2792  atttypid, atttypmod,
2795  -1);
2796  if (expr == NULL)
2797  ereport(ERROR,
2798  (errcode(ERRCODE_DATATYPE_MISMATCH),
2799  errmsg("column \"%s\" is of type %s"
2800  " but default expression is of type %s",
2801  attname,
2802  format_type_be(atttypid),
2803  format_type_be(type_id)),
2804  errhint("You will need to rewrite or cast the expression.")));
2805  }
2806 
2807  /*
2808  * Finally, take care of collations in the finished expression.
2809  */
2810  assign_expr_collations(pstate, expr);
2811 
2812  return expr;
2813 }
2814 
2815 /*
2816  * Take a raw CHECK constraint expression and convert it to a cooked format
2817  * ready for storage.
2818  *
2819  * Parse state must be set up to recognize any vars that might appear
2820  * in the expression.
2821  */
2822 static Node *
2824  Node *raw_constraint,
2825  char *relname)
2826 {
2827  Node *expr;
2828 
2829  /*
2830  * Transform raw parsetree to executable expression.
2831  */
2832  expr = transformExpr(pstate, raw_constraint, EXPR_KIND_CHECK_CONSTRAINT);
2833 
2834  /*
2835  * Make sure it yields a boolean result.
2836  */
2837  expr = coerce_to_boolean(pstate, expr, "CHECK");
2838 
2839  /*
2840  * Take care of collations.
2841  */
2842  assign_expr_collations(pstate, expr);
2843 
2844  /*
2845  * Make sure no outside relations are referred to (this is probably dead
2846  * code now that add_missing_from is history).
2847  */
2848  if (list_length(pstate->p_rtable) != 1)
2849  ereport(ERROR,
2850  (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2851  errmsg("only table \"%s\" can be referenced in check constraint",
2852  relname)));
2853 
2854  return expr;
2855 }
2856 
2857 /*
2858  * CopyStatistics --- copy entries in pg_statistic from one rel to another
2859  */
2860 void
2861 CopyStatistics(Oid fromrelid, Oid torelid)
2862 {
2863  HeapTuple tup;
2864  SysScanDesc scan;
2865  ScanKeyData key[1];
2866  Relation statrel;
2867 
2868  statrel = table_open(StatisticRelationId, RowExclusiveLock);
2869 
2870  /* Now search for stat records */
2871  ScanKeyInit(&key[0],
2872  Anum_pg_statistic_starelid,
2873  BTEqualStrategyNumber, F_OIDEQ,
2874  ObjectIdGetDatum(fromrelid));
2875 
2876  scan = systable_beginscan(statrel, StatisticRelidAttnumInhIndexId,
2877  true, NULL, 1, key);
2878 
2879  while (HeapTupleIsValid((tup = systable_getnext(scan))))
2880  {
2881  Form_pg_statistic statform;
2882 
2883  /* make a modifiable copy */
2884  tup = heap_copytuple(tup);
2885  statform = (Form_pg_statistic) GETSTRUCT(tup);
2886 
2887  /* update the copy of the tuple and insert it */
2888  statform->starelid = torelid;
2889  CatalogTupleInsert(statrel, tup);
2890 
2891  heap_freetuple(tup);
2892  }
2893 
2894  systable_endscan(scan);
2895 
2896  table_close(statrel, RowExclusiveLock);
2897 }
2898 
2899 /*
2900  * RemoveStatistics --- remove entries in pg_statistic for a rel or column
2901  *
2902  * If attnum is zero, remove all entries for rel; else remove only the one(s)
2903  * for that column.
2904  */
2905 void
2907 {
2908  Relation pgstatistic;
2909  SysScanDesc scan;
2910  ScanKeyData key[2];
2911  int nkeys;
2912  HeapTuple tuple;
2913 
2914  pgstatistic = table_open(StatisticRelationId, RowExclusiveLock);
2915 
2916  ScanKeyInit(&key[0],
2917  Anum_pg_statistic_starelid,
2918  BTEqualStrategyNumber, F_OIDEQ,
2919  ObjectIdGetDatum(relid));
2920 
2921  if (attnum == 0)
2922  nkeys = 1;
2923  else
2924  {
2925  ScanKeyInit(&key[1],
2926  Anum_pg_statistic_staattnum,
2927  BTEqualStrategyNumber, F_INT2EQ,
2929  nkeys = 2;
2930  }
2931 
2932  scan = systable_beginscan(pgstatistic, StatisticRelidAttnumInhIndexId, true,
2933  NULL, nkeys, key);
2934 
2935  /* we must loop even when attnum != 0, in case of inherited stats */
2936  while (HeapTupleIsValid(tuple = systable_getnext(scan)))
2937  CatalogTupleDelete(pgstatistic, &tuple->t_self);
2938 
2939  systable_endscan(scan);
2940 
2941  table_close(pgstatistic, RowExclusiveLock);
2942 }
2943 
2944 
2945 /*
2946  * RelationTruncateIndexes - truncate all indexes associated
2947  * with the heap relation to zero tuples.
2948  *
2949  * The routine will truncate and then reconstruct the indexes on
2950  * the specified relation. Caller must hold exclusive lock on rel.
2951  */
2952 static void
2954 {
2955  ListCell *indlist;
2956 
2957  /* Ask the relcache to produce a list of the indexes of the rel */
2958  foreach(indlist, RelationGetIndexList(heapRelation))
2959  {
2960  Oid indexId = lfirst_oid(indlist);
2961  Relation currentIndex;
2962  IndexInfo *indexInfo;
2963 
2964  /* Open the index relation; use exclusive lock, just to be sure */
2965  currentIndex = index_open(indexId, AccessExclusiveLock);
2966 
2967  /*
2968  * Fetch info needed for index_build. Since we know there are no
2969  * tuples that actually need indexing, we can use a dummy IndexInfo.
2970  * This is slightly cheaper to build, but the real point is to avoid
2971  * possibly running user-defined code in index expressions or
2972  * predicates. We might be getting invoked during ON COMMIT
2973  * processing, and we don't want to run any such code then.
2974  */
2975  indexInfo = BuildDummyIndexInfo(currentIndex);
2976 
2977  /*
2978  * Now truncate the actual file (and discard buffers).
2979  */
2980  RelationTruncate(currentIndex, 0);
2981 
2982  /* Initialize the index and rebuild */
2983  /* Note: we do not need to re-establish pkey setting */
2984  index_build(heapRelation, currentIndex, indexInfo, true, false);
2985 
2986  /* We're done with this index */
2987  index_close(currentIndex, NoLock);
2988  }
2989 }
2990 
2991 /*
2992  * heap_truncate
2993  *
2994  * This routine deletes all data within all the specified relations.
2995  *
2996  * This is not transaction-safe! There is another, transaction-safe
2997  * implementation in commands/tablecmds.c. We now use this only for
2998  * ON COMMIT truncation of temporary tables, where it doesn't matter.
2999  */
3000 void
3002 {
3003  List *relations = NIL;
3004  ListCell *cell;
3005 
3006  /* Open relations for processing, and grab exclusive access on each */
3007  foreach(cell, relids)
3008  {
3009  Oid rid = lfirst_oid(cell);
3010  Relation rel;
3011 
3012  rel = table_open(rid, AccessExclusiveLock);
3013  relations = lappend(relations, rel);
3014  }
3015 
3016  /* Don't allow truncate on tables that are referenced by foreign keys */
3017  heap_truncate_check_FKs(relations, true);
3018 
3019  /* OK to do it */
3020  foreach(cell, relations)
3021  {
3022  Relation rel = lfirst(cell);
3023 
3024  /* Truncate the relation */
3025  heap_truncate_one_rel(rel);
3026 
3027  /* Close the relation, but keep exclusive lock on it until commit */
3028  table_close(rel, NoLock);
3029  }
3030 }
3031 
3032 /*
3033  * heap_truncate_one_rel
3034  *
3035  * This routine deletes all data within the specified relation.
3036  *
3037  * This is not transaction-safe, because the truncation is done immediately
3038  * and cannot be rolled back later. Caller is responsible for having
3039  * checked permissions etc, and must have obtained AccessExclusiveLock.
3040  */
3041 void
3043 {
3044  Oid toastrelid;
3045 
3046  /*
3047  * Truncate the relation. Partitioned tables have no storage, so there is
3048  * nothing to do for them here.
3049  */
3050  if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
3051  return;
3052 
3053  /* Truncate the underlying relation */
3055 
3056  /* If the relation has indexes, truncate the indexes too */
3058 
3059  /* If there is a toast table, truncate that too */
3060  toastrelid = rel->rd_rel->reltoastrelid;
3061  if (OidIsValid(toastrelid))
3062  {
3063  Relation toastrel = table_open(toastrelid, AccessExclusiveLock);
3064 
3066  RelationTruncateIndexes(toastrel);
3067  /* keep the lock... */
3068  table_close(toastrel, NoLock);
3069  }
3070 }
3071 
3072 /*
3073  * heap_truncate_check_FKs
3074  * Check for foreign keys referencing a list of relations that
3075  * are to be truncated, and raise error if there are any
3076  *
3077  * We disallow such FKs (except self-referential ones) since the whole point
3078  * of TRUNCATE is to not scan the individual rows to be thrown away.
3079  *
3080  * This is split out so it can be shared by both implementations of truncate.
3081  * Caller should already hold a suitable lock on the relations.
3082  *
3083  * tempTables is only used to select an appropriate error message.
3084  */
3085 void
3086 heap_truncate_check_FKs(List *relations, bool tempTables)
3087 {
3088  List *oids = NIL;
3089  List *dependents;
3090  ListCell *cell;
3091 
3092  /*
3093  * Build a list of OIDs of the interesting relations.
3094  *
3095  * If a relation has no triggers, then it can neither have FKs nor be
3096  * referenced by a FK from another table, so we can ignore it. For
3097  * partitioned tables, FKs have no triggers, so we must include them
3098  * anyway.
3099  */
3100  foreach(cell, relations)
3101  {
3102  Relation rel = lfirst(cell);
3103 
3104  if (rel->rd_rel->relhastriggers ||
3105  rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
3106  oids = lappend_oid(oids, RelationGetRelid(rel));
3107  }
3108 
3109  /*
3110  * Fast path: if no relation has triggers, none has FKs either.
3111  */
3112  if (oids == NIL)
3113  return;
3114 
3115  /*
3116  * Otherwise, must scan pg_constraint. We make one pass with all the
3117  * relations considered; if this finds nothing, then all is well.
3118  */
3119  dependents = heap_truncate_find_FKs(oids);
3120  if (dependents == NIL)
3121  return;
3122 
3123  /*
3124  * Otherwise we repeat the scan once per relation to identify a particular
3125  * pair of relations to complain about. This is pretty slow, but
3126  * performance shouldn't matter much in a failure path. The reason for
3127  * doing things this way is to ensure that the message produced is not
3128  * dependent on chance row locations within pg_constraint.
3129  */
3130  foreach(cell, oids)
3131  {
3132  Oid relid = lfirst_oid(cell);
3133  ListCell *cell2;
3134 
3135  dependents = heap_truncate_find_FKs(list_make1_oid(relid));
3136 
3137  foreach(cell2, dependents)
3138  {
3139  Oid relid2 = lfirst_oid(cell2);
3140 
3141  if (!list_member_oid(oids, relid2))
3142  {
3143  char *relname = get_rel_name(relid);
3144  char *relname2 = get_rel_name(relid2);
3145 
3146  if (tempTables)
3147  ereport(ERROR,
3148  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3149  errmsg("unsupported ON COMMIT and foreign key combination"),
3150  errdetail("Table \"%s\" references \"%s\", but they do not have the same ON COMMIT setting.",
3151  relname2, relname)));
3152  else
3153  ereport(ERROR,
3154  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3155  errmsg("cannot truncate a table referenced in a foreign key constraint"),
3156  errdetail("Table \"%s\" references \"%s\".",
3157  relname2, relname),
3158  errhint("Truncate table \"%s\" at the same time, "
3159  "or use TRUNCATE ... CASCADE.",
3160  relname2)));
3161  }
3162  }
3163  }
3164 }
3165 
3166 /*
3167  * heap_truncate_find_FKs
3168  * Find relations having foreign keys referencing any of the given rels
3169  *
3170  * Input and result are both lists of relation OIDs. The result contains
3171  * no duplicates, does *not* include any rels that were already in the input
3172  * list, and is sorted in OID order. (The last property is enforced mainly
3173  * to guarantee consistent behavior in the regression tests; we don't want
3174  * behavior to change depending on chance locations of rows in pg_constraint.)
3175  *
3176  * Note: caller should already have appropriate lock on all rels mentioned
3177  * in relationIds. Since adding or dropping an FK requires exclusive lock
3178  * on both rels, this ensures that the answer will be stable.
3179  */
3180 List *
3182 {
3183  List *result = NIL;
3184  List *oids;
3185  List *parent_cons;
3186  ListCell *cell;
3187  ScanKeyData key;
3188  Relation fkeyRel;
3189  SysScanDesc fkeyScan;
3190  HeapTuple tuple;
3191  bool restart;
3192 
3193  oids = list_copy(relationIds);
3194 
3195  /*
3196  * Must scan pg_constraint. Right now, it is a seqscan because there is
3197  * no available index on confrelid.
3198  */
3199  fkeyRel = table_open(ConstraintRelationId, AccessShareLock);
3200 
3201 restart:
3202  restart = false;
3203  parent_cons = NIL;
3204 
3205  fkeyScan = systable_beginscan(fkeyRel, InvalidOid, false,
3206  NULL, 0, NULL);
3207 
3208  while (HeapTupleIsValid(tuple = systable_getnext(fkeyScan)))
3209  {
3211 
3212  /* Not a foreign key */
3213  if (con->contype != CONSTRAINT_FOREIGN)
3214  continue;
3215 
3216  /* Not referencing one of our list of tables */
3217  if (!list_member_oid(oids, con->confrelid))
3218  continue;
3219 
3220  /*
3221  * If this constraint has a parent constraint which we have not seen
3222  * yet, keep track of it for the second loop, below. Tracking parent
3223  * constraints allows us to climb up to the top-level constraint and
3224  * look for all possible relations referencing the partitioned table.
3225  */
3226  if (OidIsValid(con->conparentid) &&
3227  !list_member_oid(parent_cons, con->conparentid))
3228  parent_cons = lappend_oid(parent_cons, con->conparentid);
3229 
3230  /*
3231  * Add referencer to result, unless present in input list. (Don't
3232  * worry about dupes: we'll fix that below).
3233  */
3234  if (!list_member_oid(relationIds, con->conrelid))
3235  result = lappend_oid(result, con->conrelid);
3236  }
3237 
3238  systable_endscan(fkeyScan);
3239 
3240  /*
3241  * Process each parent constraint we found to add the list of referenced
3242  * relations by them to the oids list. If we do add any new such
3243  * relations, redo the first loop above. Also, if we see that the parent
3244  * constraint in turn has a parent, add that so that we process all
3245  * relations in a single additional pass.
3246  */
3247  foreach(cell, parent_cons)
3248  {
3249  Oid parent = lfirst_oid(cell);
3250 
3251  ScanKeyInit(&key,
3252  Anum_pg_constraint_oid,
3253  BTEqualStrategyNumber, F_OIDEQ,
3254  ObjectIdGetDatum(parent));
3255 
3256  fkeyScan = systable_beginscan(fkeyRel, ConstraintOidIndexId,
3257  true, NULL, 1, &key);
3258 
3259  tuple = systable_getnext(fkeyScan);
3260  if (HeapTupleIsValid(tuple))
3261  {
3263 
3264  /*
3265  * pg_constraint rows always appear for partitioned hierarchies
3266  * this way: on the each side of the constraint, one row appears
3267  * for each partition that points to the top-most table on the
3268  * other side.
3269  *
3270  * Because of this arrangement, we can correctly catch all
3271  * relevant relations by adding to 'parent_cons' all rows with
3272  * valid conparentid, and to the 'oids' list all rows with a zero
3273  * conparentid. If any oids are added to 'oids', redo the first
3274  * loop above by setting 'restart'.
3275  */
3276  if (OidIsValid(con->conparentid))
3277  parent_cons = list_append_unique_oid(parent_cons,
3278  con->conparentid);
3279  else if (!list_member_oid(oids, con->confrelid))
3280  {
3281  oids = lappend_oid(oids, con->confrelid);
3282  restart = true;
3283  }
3284  }
3285 
3286  systable_endscan(fkeyScan);
3287  }
3288 
3289  list_free(parent_cons);
3290  if (restart)
3291  goto restart;
3292 
3293  table_close(fkeyRel, AccessShareLock);
3294  list_free(oids);
3295 
3296  /* Now sort and de-duplicate the result list */
3297  list_sort(result, list_oid_cmp);
3298  list_deduplicate_oid(result);
3299 
3300  return result;
3301 }
3302 
3303 /*
3304  * StorePartitionKey
3305  * Store information about the partition key rel into the catalog
3306  */
3307 void
3309  char strategy,
3310  int16 partnatts,
3311  AttrNumber *partattrs,
3312  List *partexprs,
3313  Oid *partopclass,
3314  Oid *partcollation)
3315 {
3316  int i;
3317  int2vector *partattrs_vec;
3318  oidvector *partopclass_vec;
3319  oidvector *partcollation_vec;
3320  Datum partexprDatum;
3321  Relation pg_partitioned_table;
3322  HeapTuple tuple;
3323  Datum values[Natts_pg_partitioned_table];
3324  bool nulls[Natts_pg_partitioned_table];
3325  ObjectAddress myself;
3326  ObjectAddress referenced;
3327  ObjectAddresses *addrs;
3328 
3329  Assert(rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE);
3330 
3331  /* Copy the partition attribute numbers, opclass OIDs into arrays */
3332  partattrs_vec = buildint2vector(partattrs, partnatts);
3333  partopclass_vec = buildoidvector(partopclass, partnatts);
3334  partcollation_vec = buildoidvector(partcollation, partnatts);
3335 
3336  /* Convert the expressions (if any) to a text datum */
3337  if (partexprs)
3338  {
3339  char *exprString;
3340 
3341  exprString = nodeToString(partexprs);
3342  partexprDatum = CStringGetTextDatum(exprString);
3343  pfree(exprString);
3344  }
3345  else
3346  partexprDatum = (Datum) 0;
3347 
3348  pg_partitioned_table = table_open(PartitionedRelationId, RowExclusiveLock);
3349 
3350  MemSet(nulls, false, sizeof(nulls));
3351 
3352  /* Only this can ever be NULL */
3353  if (!partexprDatum)
3354  nulls[Anum_pg_partitioned_table_partexprs - 1] = true;
3355 
3356  values[Anum_pg_partitioned_table_partrelid - 1] = ObjectIdGetDatum(RelationGetRelid(rel));
3357  values[Anum_pg_partitioned_table_partstrat - 1] = CharGetDatum(strategy);
3358  values[Anum_pg_partitioned_table_partnatts - 1] = Int16GetDatum(partnatts);
3359  values[Anum_pg_partitioned_table_partdefid - 1] = ObjectIdGetDatum(InvalidOid);
3360  values[Anum_pg_partitioned_table_partattrs - 1] = PointerGetDatum(partattrs_vec);
3361  values[Anum_pg_partitioned_table_partclass - 1] = PointerGetDatum(partopclass_vec);
3362  values[Anum_pg_partitioned_table_partcollation - 1] = PointerGetDatum(partcollation_vec);
3363  values[Anum_pg_partitioned_table_partexprs - 1] = partexprDatum;
3364 
3365  tuple = heap_form_tuple(RelationGetDescr(pg_partitioned_table), values, nulls);
3366 
3367  CatalogTupleInsert(pg_partitioned_table, tuple);
3368  table_close(pg_partitioned_table, RowExclusiveLock);
3369 
3370  /* Mark this relation as dependent on a few things as follows */
3371  addrs = new_object_addresses();
3372  ObjectAddressSet(myself, RelationRelationId, RelationGetRelid(rel));
3373 
3374  /* Operator class and collation per key column */
3375  for (i = 0; i < partnatts; i++)
3376  {
3377  ObjectAddressSet(referenced, OperatorClassRelationId, partopclass[i]);
3378  add_exact_object_address(&referenced, addrs);
3379 
3380  /* The default collation is pinned, so don't bother recording it */
3381  if (OidIsValid(partcollation[i]) &&
3382  partcollation[i] != DEFAULT_COLLATION_OID)
3383  {
3384  ObjectAddressSet(referenced, CollationRelationId, partcollation[i]);
3385  add_exact_object_address(&referenced, addrs);
3386  }
3387  }
3388 
3390  free_object_addresses(addrs);
3391 
3392  /*
3393  * The partitioning columns are made internally dependent on the table,
3394  * because we cannot drop any of them without dropping the whole table.
3395  * (ATExecDropColumn independently enforces that, but it's not bulletproof
3396  * so we need the dependencies too.)
3397  */
3398  for (i = 0; i < partnatts; i++)
3399  {
3400  if (partattrs[i] == 0)
3401  continue; /* ignore expressions here */
3402 
3403  ObjectAddressSubSet(referenced, RelationRelationId,
3404  RelationGetRelid(rel), partattrs[i]);
3405  recordDependencyOn(&referenced, &myself, DEPENDENCY_INTERNAL);
3406  }
3407 
3408  /*
3409  * Also consider anything mentioned in partition expressions. External
3410  * references (e.g. functions) get NORMAL dependencies. Table columns
3411  * mentioned in the expressions are handled the same as plain partitioning
3412  * columns, i.e. they become internally dependent on the whole table.
3413  */
3414  if (partexprs)
3416  (Node *) partexprs,
3417  RelationGetRelid(rel),
3420  true /* reverse the self-deps */ );
3421 
3422  /*
3423  * We must invalidate the relcache so that the next
3424  * CommandCounterIncrement() will cause the same to be rebuilt using the
3425  * information in just created catalog entry.
3426  */
3428 }
3429 
3430 /*
3431  * RemovePartitionKeyByRelId
3432  * Remove pg_partitioned_table entry for a relation
3433  */
3434 void
3436 {
3437  Relation rel;
3438  HeapTuple tuple;
3439 
3440  rel = table_open(PartitionedRelationId, RowExclusiveLock);
3441 
3442  tuple = SearchSysCache1(PARTRELID, ObjectIdGetDatum(relid));
3443  if (!HeapTupleIsValid(tuple))
3444  elog(ERROR, "cache lookup failed for partition key of relation %u",
3445  relid);
3446 
3447  CatalogTupleDelete(rel, &tuple->t_self);
3448 
3449  ReleaseSysCache(tuple);
3451 }
3452 
3453 /*
3454  * StorePartitionBound
3455  * Update pg_class tuple of rel to store the partition bound and set
3456  * relispartition to true
3457  *
3458  * If this is the default partition, also update the default partition OID in
3459  * pg_partitioned_table.
3460  *
3461  * Also, invalidate the parent's relcache, so that the next rebuild will load
3462  * the new partition's info into its partition descriptor. If there is a
3463  * default partition, we must invalidate its relcache entry as well.
3464  */
3465 void
3467 {
3468  Relation classRel;
3469  HeapTuple tuple,
3470  newtuple;
3471  Datum new_val[Natts_pg_class];
3472  bool new_null[Natts_pg_class],
3473  new_repl[Natts_pg_class];
3474  Oid defaultPartOid;
3475 
3476  /* Update pg_class tuple */
3477  classRel = table_open(RelationRelationId, RowExclusiveLock);
3478  tuple = SearchSysCacheCopy1(RELOID,
3480  if (!HeapTupleIsValid(tuple))
3481  elog(ERROR, "cache lookup failed for relation %u",
3482  RelationGetRelid(rel));
3483 
3484 #ifdef USE_ASSERT_CHECKING
3485  {
3486  Form_pg_class classForm;
3487  bool isnull;
3488 
3489  classForm = (Form_pg_class) GETSTRUCT(tuple);
3490  Assert(!classForm->relispartition);
3491  (void) SysCacheGetAttr(RELOID, tuple, Anum_pg_class_relpartbound,
3492  &isnull);
3493  Assert(isnull);
3494  }
3495 #endif
3496 
3497  /* Fill in relpartbound value */
3498  memset(new_val, 0, sizeof(new_val));
3499  memset(new_null, false, sizeof(new_null));
3500  memset(new_repl, false, sizeof(new_repl));
3501  new_val[Anum_pg_class_relpartbound - 1] = CStringGetTextDatum(nodeToString(bound));
3502  new_null[Anum_pg_class_relpartbound - 1] = false;
3503  new_repl[Anum_pg_class_relpartbound - 1] = true;
3504  newtuple = heap_modify_tuple(tuple, RelationGetDescr(classRel),
3505  new_val, new_null, new_repl);
3506  /* Also set the flag */
3507  ((Form_pg_class) GETSTRUCT(newtuple))->relispartition = true;
3508  CatalogTupleUpdate(classRel, &newtuple->t_self, newtuple);
3509  heap_freetuple(newtuple);
3510  table_close(classRel, RowExclusiveLock);
3511 
3512  /*
3513  * If we're storing bounds for the default partition, update
3514  * pg_partitioned_table too.
3515  */
3516  if (bound->is_default)
3518  RelationGetRelid(rel));
3519 
3520  /* Make these updates visible */
3522 
3523  /*
3524  * The partition constraint for the default partition depends on the
3525  * partition bounds of every other partition, so we must invalidate the
3526  * relcache entry for that partition every time a partition is added or
3527  * removed.
3528  */
3529  defaultPartOid =
3531  if (OidIsValid(defaultPartOid))
3532  CacheInvalidateRelcacheByRelid(defaultPartOid);
3533 
3534  CacheInvalidateRelcache(parent);
3535 }
void recordDependencyOnNewAcl(Oid classId, Oid objectId, int32 objsubId, Oid ownerId, Acl *acl)
Definition: aclchk.c:5950
Acl * get_user_default_acl(ObjectType objtype, Oid ownerId, Oid nsp_oid)
Definition: aclchk.c:5874
int16 AttrNumber
Definition: attnum.h:21
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define CStringGetTextDatum(s)
Definition: builtins.h:85
#define TextDatumGetCString(d)
Definition: builtins.h:86
#define NameStr(name)
Definition: c.h:681
#define Min(x, y)
Definition: c.h:986
signed short int16
Definition: c.h:428
signed int int32
Definition: c.h:429
TransactionId MultiXactId
Definition: c.h:597
#define lengthof(array)
Definition: c.h:734
#define MemSet(start, val, len)
Definition: c.h:1008
uint32 CommandId
Definition: c.h:601
uint32 TransactionId
Definition: c.h:587
#define OidIsValid(objectId)
Definition: c.h:710
bool IsToastNamespace(Oid namespaceId)
Definition: catalog.c:202
Oid GetNewRelFileNode(Oid reltablespace, Relation pg_class, char relpersistence)
Definition: catalog.c:500
bool IsCatalogNamespace(Oid namespaceId)
Definition: catalog.c:184
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:368
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:496
void record_object_address_dependencies(const ObjectAddress *depender, ObjectAddresses *referenced, DependencyType behavior)
Definition: dependency.c:2711
void recordDependencyOnSingleRelExpr(const ObjectAddress *depender, Node *expr, Oid relId, DependencyType behavior, DependencyType self_behavior, bool reverse_self)
Definition: dependency.c:1631
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2447
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
Definition: dependency.c:2502
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2742
@ DEPENDENCY_INTERNAL
Definition: dependency.h:35
@ DEPENDENCY_NORMAL
Definition: dependency.h:33
int errdetail(const char *fmt,...)
Definition: elog.c:1037
int errhint(const char *fmt,...)
Definition: elog.c:1151
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
#define NOTICE
Definition: elog.h:29
#define ereport(elevel,...)
Definition: elog.h:143
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3564
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1552
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1254
const TupleTableSlotOps TTSOpsHeapTuple
Definition: execTuples.c:84
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1238
#define OidFunctionCall3(functionId, arg1, arg2, arg3)
Definition: fmgr.h:673
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:598
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:505
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:386
bool IsBinaryUpgrade
Definition: globals.c:114
Oid MyDatabaseTableSpace
Definition: globals.c:91
void RemoveAttributeById(Oid relid, AttrNumber attnum)
Definition: heap.c:1643
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:2072
void RelationClearMissing(Relation rel)
Definition: heap.c:1939
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:2504
void SetAttrMissing(Oid relid, char *attname, char *value)
Definition: heap.c:2007
void InsertPgAttributeTuples(Relation pg_attribute_rel, TupleDesc tupdesc, Oid new_rel_oid, Datum *attoptions, CatalogIndexState indstate)
Definition: heap.c:694
void DeleteSystemAttributeTuples(Oid relid)
Definition: heap.c:1602
void StorePartitionKey(Relation rel, char strategy, int16 partnatts, AttrNumber *partattrs, List *partexprs, Oid *partopclass, Oid *partcollation)
Definition: heap.c:3308
static void StoreConstraints(Relation rel, List *cooked_constraints, bool is_internal)
Definition: heap.c:2182
static void AddNewAttributeTuples(Oid new_rel_oid, TupleDesc tupdesc, char relkind)
Definition: heap.c:801
List * heap_truncate_find_FKs(List *relationIds)
Definition: heap.c:3181
void DeleteRelationTuple(Oid relid)
Definition: heap.c:1536
static const FormData_pg_attribute * SysAtt[]
Definition: heap.c:231
static void RelationTruncateIndexes(Relation heapRelation)
Definition: heap.c:2953
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:952
Oid binary_upgrade_next_heap_pg_class_relfilenode
Definition: heap.c:80
void DeleteAttributeTuples(Oid relid)
Definition: heap.c:1565
void RemoveStatistics(Oid relid, AttrNumber attnum)
Definition: heap.c:2906
static const FormData_pg_attribute a4
Definition: heap.c:183
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:1010
Oid binary_upgrade_next_toast_pg_class_relfilenode
Definition: heap.c:82
static const FormData_pg_attribute a1
Definition: heap.c:141
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: heap.c:1088
void heap_truncate(List *relids)
Definition: heap.c:3001
void CheckAttributeType(const char *attname, Oid atttypid, Oid attcollation, List *containing_rowtypes, int flags)
Definition: heap.c:544
const FormData_pg_attribute * SystemAttributeDefinition(AttrNumber attno)
Definition: heap.c:239
void CopyStatistics(Oid fromrelid, Oid torelid)
Definition: heap.c:2861
Node * cookDefault(ParseState *pstate, Node *raw_default, Oid atttypid, int32 atttypmod, const char *attname, char attgenerated)
Definition: heap.c:2748
void heap_truncate_check_FKs(List *relations, bool tempTables)
Definition: heap.c:3086
static const FormData_pg_attribute a6
Definition: heap.c:217
Relation heap_create(const char *relname, Oid relnamespace, Oid reltablespace, Oid relid, Oid relfilenode, 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
static bool check_nested_generated_walker(Node *node, void *context)
Definition: heap.c:2688
static void SetRelationNumChecks(Relation rel, int numchecks)
Definition: heap.c:2654
static const FormData_pg_attribute a3
Definition: heap.c:169
static void RelationRemoveInheritance(Oid relid)
Definition: heap.c:1503
static const FormData_pg_attribute a2
Definition: heap.c:155
void StorePartitionBound(Relation rel, Relation parent, PartitionBoundSpec *bound)
Definition: heap.c:3466
void heap_drop_with_catalog(Oid relid)
Definition: heap.c:1759
void InsertPgClassTuple(Relation pg_class_desc, Relation new_rel_desc, Oid new_rel_oid, Datum relacl, Datum reloptions)
Definition: heap.c:879
void CheckAttributeNamesTypes(TupleDesc tupdesc, char relkind, int flags)
Definition: heap.c:452
static void check_nested_generated(ParseState *pstate, Node *node)
Definition: heap.c:2730
Oid binary_upgrade_next_toast_pg_class_oid
Definition: heap.c:81
void heap_truncate_one_rel(Relation rel)
Definition: heap.c:3042
void RemovePartitionKeyByRelId(Oid relid)
Definition: heap.c:3435
Oid binary_upgrade_next_heap_pg_class_oid
Definition: heap.c:79
const FormData_pg_attribute * SystemAttributeByName(const char *attname)
Definition: heap.c:251
static const FormData_pg_attribute a5
Definition: heap.c:197
List * AddRelationNewConstraints(Relation rel, List *newColDefaults, List *newConstraints, bool allow_merge, bool is_local, bool is_internal, const char *queryString)
Definition: heap.c:2254
static Node * cookConstraint(ParseState *pstate, Node *raw_constraint, char *relname)
Definition: heap.c:2823
#define CHKATYPE_ANYRECORD
Definition: heap.h:24
#define CHKATYPE_ANYARRAY
Definition: heap.h:23
#define CHKATYPE_IS_PARTKEY
Definition: heap.h:25
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:680
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:649
#define MaxHeapAttributeNumber
Definition: htup_details.h:47
static Datum fastgetattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
Definition: htup_details.h:745
IndexInfo * BuildDummyIndexInfo(Relation index)
Definition: index.c:2478
void index_build(Relation heapRelation, Relation indexRelation, IndexInfo *indexInfo, bool isreindex, bool parallel)
Definition: index.c:2943
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:158
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:132
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
void CatalogTuplesMultiInsertWithInfo(Relation heapRel, TupleTableSlot **slot, int ntuples, CatalogIndexState indstate)
Definition: indexing.c:261
void CatalogCloseIndexes(CatalogIndexState indstate)
Definition: indexing.c:61
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:221
CatalogIndexState CatalogOpenIndexes(Relation heapRel)
Definition: indexing.c:43
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:350
#define MAX_CATALOG_MULTI_INSERT_BYTES
Definition: indexing.h:33
static struct @151 value
long val
Definition: informix.c:664
int2vector * buildint2vector(const int16 *int2s, int n)
Definition: int.c:114
void CacheInvalidateRelcache(Relation relation)
Definition: inval.c:1363
void CacheInvalidateRelcacheByRelid(Oid relid)
Definition: inval.c:1422
int j
Definition: isn.c:74
int i
Definition: isn.c:73
struct ItemPointerData ItemPointerData
Assert(fmt[strlen(fmt) - 1] !='\n')
void list_sort(List *list, list_sort_comparator cmp)
Definition: list.c:1612
List * list_union(const List *list1, const List *list2)
Definition: list.c:1025
List * lappend(List *list, void *datum)
Definition: list.c:336
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
List * list_copy(const List *oldlist)
Definition: list.c:1532
void list_deduplicate_oid(List *list)
Definition: list.c:1454
int list_oid_cmp(const ListCell *p1, const ListCell *p2)
Definition: list.c:1645
void list_free(List *list)
Definition: list.c:1505
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:701
List * list_append_unique_oid(List *list, Oid datum)
Definition: list.c:1339
List * list_delete_last(List *list)
Definition: list.c:916
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:109
#define NoLock
Definition: lockdefs.h:34
#define AccessExclusiveLock
Definition: lockdefs.h:43
#define AccessShareLock
Definition: lockdefs.h:36
#define RowExclusiveLock
Definition: lockdefs.h:38
Oid get_range_subtype(Oid rangeOid)
Definition: lsyscache.c:3367
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2716
char get_attgenerated(Oid relid, AttrNumber attnum)
Definition: lsyscache.c:913
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3326
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1909
Oid get_range_collation(Oid rangeOid)
Definition: lsyscache.c:3393
bool type_is_collatable(Oid typid)
Definition: lsyscache.c:3038
Oid get_typ_typrelid(Oid typid)
Definition: lsyscache.c:2688
char get_typtype(Oid typid)
Definition: lsyscache.c:2586
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2478
Oid get_relname_relid(const char *relname, Oid relnamespace)
Definition: lsyscache.c:1866
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition: lsyscache.c:825
void pfree(void *pointer)
Definition: mcxt.c:1175
void * palloc(Size size)
Definition: mcxt.c:1068
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:406
#define IsNormalProcessingMode()
Definition: miscadmin.h:408
#define InvalidMultiXactId
Definition: multixact.h:24
void namestrcpy(Name name, const char *str)
Definition: name.c:233
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:2015
#define IsA(nodeptr, _type_)
Definition: nodes.h:624
#define castNode(_type_, nodeptr)
Definition: nodes.h:642
#define InvokeObjectPostCreateHookArg(classId, objectId, subId, is_internal)
Definition: objectaccess.h:173
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
#define ObjectAddressSubSet(addr, class_id, object_id, object_sub_id)
Definition: objectaddress.h:33
oidvector * buildoidvector(const Oid *oids, int n)
Definition: oid.c:167
char * nodeToString(const void *obj)
Definition: outfuncs.c:4785
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
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)
void assign_expr_collations(ParseState *pstate, Node *expr)
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:112
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:43
@ EXPR_KIND_COLUMN_DEFAULT
Definition: parse_node.h:69
@ EXPR_KIND_GENERATED_COLUMN
Definition: parse_node.h:82
@ EXPR_KIND_CHECK_CONSTRAINT
Definition: parse_node.h:67
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:2581
@ CONSTR_CHECK
Definition: parsenodes.h:2584
@ OBJECT_SEQUENCE
Definition: parsenodes.h:2171
@ OBJECT_TABLE
Definition: parsenodes.h:2175
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
PartitionDesc RelationGetPartitionDesc(Relation rel, bool omit_detached)
Definition: partdesc.c:72
Oid get_default_oid_from_partdesc(PartitionDesc partdesc)
Definition: partdesc.c:455
void update_default_partition_oid(Oid parentId, Oid defaultPartId)
Definition: partition.c:338
Oid get_default_partition_oid(Oid parentId)
Definition: partition.c:313
Oid get_partition_parent(Oid relid, bool even_if_detached)
Definition: partition.c:54
Oid StoreAttrDefault(Relation rel, AttrNumber attnum, Node *expr, bool is_internal, bool add_column_mode)
Definition: pg_attrdef.c:46
FormData_pg_attribute
Definition: pg_attribute.h:191
NameData attname
Definition: pg_attribute.h:41
char attstorage
Definition: pg_attribute.h:126
bool attbyval
Definition: pg_attribute.h:112
char attalign
Definition: pg_attribute.h:118
int16 attnum
Definition: pg_attribute.h:83
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
bool attnotnull
Definition: pg_attribute.h:139
NameData relname
Definition: pg_class.h:38
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
#define NAMEDATALEN
char * ChooseConstraintName(const char *name1, const char *name2, const char *label, Oid namespaceid, List *others)
Oid CreateConstraintEntry(const char *constraintName, Oid constraintNamespace, char constraintType, bool isDeferrable, bool isDeferred, bool isValidated, Oid parentConstrId, Oid relId, const int16 *constraintKey, int constraintNKeys, int constraintNTotalKeys, Oid domainId, Oid indexRelId, Oid foreignRelId, const int16 *foreignKey, const Oid *pfEqOp, const Oid *ppEqOp, const Oid *ffEqOp, int foreignNKeys, char foreignUpdateType, char foreignDeleteType, const int16 *fkDeleteSetCols, int numFkDeleteSetCols, char foreignMatchType, const Oid *exclOp, Node *conExpr, const char *conBin, bool conIsLocal, int conInhCount, bool conNoInherit, bool is_internal)
Definition: pg_constraint.c:50
FormData_pg_constraint * Form_pg_constraint
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:44
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:191
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
#define NIL
Definition: pg_list.h:65
#define list_make1_oid(x1)
Definition: pg_list.h:236
#define linitial(l)
Definition: pg_list.h:174
#define lfirst_oid(lc)
Definition: pg_list.h:171
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:164
void recordDependencyOnTablespace(Oid classId, Oid objectId, Oid tablespace)
Definition: pg_shdepend.c:360
FormData_pg_statistic * Form_pg_statistic
Definition: pg_statistic.h:135
void RemoveSubscriptionRel(Oid subid, Oid relid)
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:198
char * makeArrayTypeName(const char *typeName, Oid typeNamespace)
Definition: pg_type.c:816
bool moveArrayTypeName(Oid typeOid, const char *typeName, Oid typeNamespace)
Definition: pg_type.c:856
void pgstat_create_relation(Relation rel)
void pgstat_drop_relation(Relation rel)
#define snprintf
Definition: port.h:225
#define TransactionIdGetDatum(X)
Definition: postgres.h:565
static Datum Float4GetDatum(float4 X)
Definition: postgres.h:725
#define CStringGetDatum(X)
Definition: postgres.h:622
uintptr_t Datum
Definition: postgres.h:411
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define BoolGetDatum(X)
Definition: postgres.h:446
#define Int32GetDatum(X)
Definition: postgres.h:523
#define NameGetDatum(X)
Definition: postgres.h:639
#define Int16GetDatum(X)
Definition: postgres.h:495
#define MultiXactIdGetDatum(X)
Definition: postgres.h:572
#define CharGetDatum(X)
Definition: postgres.h:460
#define PointerGetDatum(X)
Definition: postgres.h:600
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
void CheckTableForSerializableConflictIn(Relation relation)
Definition: predicate.c:4531
OnCommitAction
Definition: primnodes.h:48
@ ONCOMMIT_NOOP
Definition: primnodes.h:49
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:494
@ COERCION_ASSIGNMENT
Definition: primnodes.h:473
void * stringToNode(const char *str)
Definition: read.c:89
#define RelationGetRelid(relation)
Definition: rel.h:489
#define RelationGetDescr(relation)
Definition: rel.h:515
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:495
#define RelationGetRelationName(relation)
Definition: rel.h:523
#define RelationGetNamespace(relation)
Definition: rel.h:530
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4675
Relation RelationBuildLocalRelation(const char *relname, Oid relnamespace, TupleDesc tupDesc, Oid relid, Oid accessmtd, Oid relfilenode, Oid reltablespace, bool shared_relation, bool mapped_relation, char relpersistence, char relkind)
Definition: relcache.c:3457
void RelationForgetRelation(Oid rid)
Definition: relcache.c:2824
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
SMgrRelation RelationCreateStorage(RelFileNode rnode, char relpersistence, bool register_delete)
Definition: storage.c:120
void RelationDropStorage(Relation rel)
Definition: storage.c:205
void RelationTruncate(Relation rel, BlockNumber nblocks)
Definition: storage.c:287
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
ConstrType contype
Definition: parsenodes.h:2610
bool is_no_inherit
Definition: parsenodes.h:2619
char * cooked_expr
Definition: parsenodes.h:2621
bool initially_valid
Definition: parsenodes.h:2658
bool skip_validation
Definition: parsenodes.h:2657
Node * raw_expr
Definition: parsenodes.h:2620
char * conname
Definition: parsenodes.h:2613
Oid conoid
Definition: heap.h:38
char * name
Definition: heap.h:39
AttrNumber attnum
Definition: heap.h:40
bool skip_validation
Definition: heap.h:42
bool is_no_inherit
Definition: heap.h:45
int inhcount
Definition: heap.h:44
bool is_local
Definition: heap.h:43
ConstrType contype
Definition: heap.h:37
Node * expr
Definition: heap.h:41
ItemPointerData t_self
Definition: htup.h:65
Definition: pg_list.h:51
Definition: nodes.h:574
const char * p_sourcetext
Definition: parse_node.h:182
List * p_rtable
Definition: parse_node.h:183
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:110
RelFileNode rd_node
Definition: rel.h:56
Form_pg_class rd_rel
Definition: rel.h:109
uint16 num_check
Definition: tupdesc.h:43
TupleConstr * constr
Definition: tupdesc.h:85
int32 tdtypmod
Definition: tupdesc.h:83
FormData_pg_attribute attrs[FLEXIBLE_ARRAY_MEMBER]
Definition: tupdesc.h:87
Oid tdtypeid
Definition: tupdesc.h:82
bool * tts_isnull
Definition: tuptable.h:128
Datum * tts_values
Definition: tuptable.h:126
Definition: primnodes.h:196
AttrNumber varattno
Definition: primnodes.h:200
int varno
Definition: primnodes.h:198
int location
Definition: primnodes.h:210
Definition: c.h:650
Definition: c.h:661
Definition: regcomp.c:238
#define MinTransactionIdAttributeNumber
Definition: sysattr.h:22
#define MaxCommandIdAttributeNumber
Definition: sysattr.h:25
#define MaxTransactionIdAttributeNumber
Definition: sysattr.h:24
#define TableOidAttributeNumber
Definition: sysattr.h:26
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21
#define MinCommandIdAttributeNumber
Definition: sysattr.h:23
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1221
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1173
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1434
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1184
HeapTuple SearchSysCacheAttName(Oid relid, const char *attname)
Definition: syscache.c:1314
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:179
#define SearchSysCacheCopy2(cacheId, key1, key2)
Definition: syscache.h:181
@ FOREIGNTABLEREL
Definition: syscache.h:65
@ ATTNUM
Definition: syscache.h:41
@ RELOID
Definition: syscache.h:89
@ TYPENAMENSP
Definition: syscache.h:113
@ PARTRELID
Definition: syscache.h:77
#define GetSysCacheOid2(cacheId, oidcol, key1, key2)
Definition: syscache.h:199
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
static void table_relation_set_new_filenode(Relation rel, const RelFileNode *newrnode, char persistence, TransactionId *freezeXid, MultiXactId *minmulti)
Definition: tableam.h:1592
static void table_relation_nontransactional_truncate(Relation rel)
Definition: tableam.h:1609
void CheckTableNotInUse(Relation rel, const char *stmt)
Definition: tablecmds.c:3989
void remove_on_commit_action(Oid relid)
Definition: tablecmds.c:16733
void register_on_commit_action(Oid relid, OnCommitAction action)
Definition: tablecmds.c:16697
#define InvalidTransactionId
Definition: transam.h:31
void FreeTupleDesc(TupleDesc tupdesc)
Definition: tupdesc.c:309
TupleDesc CreateTupleDesc(int natts, Form_pg_attribute *attrs)
Definition: tupdesc.c:90
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
Oid AssignTypeArrayOid(void)
Definition: typecmds.c:2401
#define DEFAULT_TYPDELIM
Definition: typecmds.h:22
List * pull_var_clause(Node *node, int flags)
Definition: var.c:604
bool contain_var_clause(Node *node)
Definition: var.c:400
void CommandCounterIncrement(void)
Definition: xact.c:1074