]> granicus.if.org Git - postgresql/blob - src/backend/catalog/heap.c
Move pg_constraint.h function declarations to new file pg_constraint_fn.h.
[postgresql] / src / backend / catalog / heap.c
1 /*-------------------------------------------------------------------------
2  *
3  * heap.c
4  *        code to create and destroy POSTGRES heap relations
5  *
6  * Portions Copyright (c) 1996-2016, 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/htup_details.h"
33 #include "access/multixact.h"
34 #include "access/sysattr.h"
35 #include "access/transam.h"
36 #include "access/xact.h"
37 #include "access/xlog.h"
38 #include "catalog/binary_upgrade.h"
39 #include "catalog/catalog.h"
40 #include "catalog/dependency.h"
41 #include "catalog/heap.h"
42 #include "catalog/index.h"
43 #include "catalog/objectaccess.h"
44 #include "catalog/pg_attrdef.h"
45 #include "catalog/pg_collation.h"
46 #include "catalog/pg_constraint.h"
47 #include "catalog/pg_constraint_fn.h"
48 #include "catalog/pg_foreign_table.h"
49 #include "catalog/pg_inherits.h"
50 #include "catalog/pg_namespace.h"
51 #include "catalog/pg_statistic.h"
52 #include "catalog/pg_tablespace.h"
53 #include "catalog/pg_type.h"
54 #include "catalog/pg_type_fn.h"
55 #include "catalog/storage.h"
56 #include "catalog/storage_xlog.h"
57 #include "commands/tablecmds.h"
58 #include "commands/typecmds.h"
59 #include "miscadmin.h"
60 #include "nodes/nodeFuncs.h"
61 #include "optimizer/var.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 "storage/predicate.h"
67 #include "storage/smgr.h"
68 #include "utils/acl.h"
69 #include "utils/builtins.h"
70 #include "utils/fmgroids.h"
71 #include "utils/inval.h"
72 #include "utils/lsyscache.h"
73 #include "utils/rel.h"
74 #include "utils/ruleutils.h"
75 #include "utils/snapmgr.h"
76 #include "utils/syscache.h"
77 #include "utils/tqual.h"
78
79
80 /* Potentially set by pg_upgrade_support functions */
81 Oid                     binary_upgrade_next_heap_pg_class_oid = InvalidOid;
82 Oid                     binary_upgrade_next_toast_pg_class_oid = InvalidOid;
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                                         Datum relacl,
92                                         Datum reloptions);
93 static ObjectAddress AddNewRelationType(const char *typeName,
94                                    Oid typeNamespace,
95                                    Oid new_rel_oid,
96                                    char new_rel_kind,
97                                    Oid ownerid,
98                                    Oid new_row_type,
99                                    Oid new_array_type);
100 static void RelationRemoveInheritance(Oid relid);
101 static Oid StoreRelCheck(Relation rel, char *ccname, Node *expr,
102                           bool is_validated, bool is_local, int inhcount,
103                           bool is_no_inherit, bool is_internal);
104 static void StoreConstraints(Relation rel, List *cooked_constraints,
105                                  bool is_internal);
106 static bool MergeWithExistingConstraint(Relation rel, char *ccname, Node *expr,
107                                                         bool allow_merge, bool is_local,
108                                                         bool is_no_inherit);
109 static void SetRelationNumChecks(Relation rel, int numchecks);
110 static Node *cookConstraint(ParseState *pstate,
111                            Node *raw_constraint,
112                            char *relname);
113 static List *insert_ordered_unique_oid(List *list, Oid datum);
114
115
116 /* ----------------------------------------------------------------
117  *                              XXX UGLY HARD CODED BADNESS FOLLOWS XXX
118  *
119  *              these should all be moved to someplace in the lib/catalog
120  *              module, if not obliterated first.
121  * ----------------------------------------------------------------
122  */
123
124
125 /*
126  * Note:
127  *              Should the system special case these attributes in the future?
128  *              Advantage:      consume much less space in the ATTRIBUTE relation.
129  *              Disadvantage:  special cases will be all over the place.
130  */
131
132 /*
133  * The initializers below do not include trailing variable length fields,
134  * but that's OK - we're never going to reference anything beyond the
135  * fixed-size portion of the structure anyway.
136  */
137
138 static FormData_pg_attribute a1 = {
139         0, {"ctid"}, TIDOID, 0, sizeof(ItemPointerData),
140         SelfItemPointerAttributeNumber, 0, -1, -1,
141         false, 'p', 's', true, false, false, true, 0
142 };
143
144 static FormData_pg_attribute a2 = {
145         0, {"oid"}, OIDOID, 0, sizeof(Oid),
146         ObjectIdAttributeNumber, 0, -1, -1,
147         true, 'p', 'i', true, false, false, true, 0
148 };
149
150 static FormData_pg_attribute a3 = {
151         0, {"xmin"}, XIDOID, 0, sizeof(TransactionId),
152         MinTransactionIdAttributeNumber, 0, -1, -1,
153         true, 'p', 'i', true, false, false, true, 0
154 };
155
156 static FormData_pg_attribute a4 = {
157         0, {"cmin"}, CIDOID, 0, sizeof(CommandId),
158         MinCommandIdAttributeNumber, 0, -1, -1,
159         true, 'p', 'i', true, false, false, true, 0
160 };
161
162 static FormData_pg_attribute a5 = {
163         0, {"xmax"}, XIDOID, 0, sizeof(TransactionId),
164         MaxTransactionIdAttributeNumber, 0, -1, -1,
165         true, 'p', 'i', true, false, false, true, 0
166 };
167
168 static FormData_pg_attribute a6 = {
169         0, {"cmax"}, CIDOID, 0, sizeof(CommandId),
170         MaxCommandIdAttributeNumber, 0, -1, -1,
171         true, 'p', 'i', true, false, false, true, 0
172 };
173
174 /*
175  * We decided to call this attribute "tableoid" rather than say
176  * "classoid" on the basis that in the future there may be more than one
177  * table of a particular class/type. In any case table is still the word
178  * used in SQL.
179  */
180 static FormData_pg_attribute a7 = {
181         0, {"tableoid"}, OIDOID, 0, sizeof(Oid),
182         TableOidAttributeNumber, 0, -1, -1,
183         true, 'p', 'i', true, false, false, true, 0
184 };
185
186 static const Form_pg_attribute SysAtt[] = {&a1, &a2, &a3, &a4, &a5, &a6, &a7};
187
188 /*
189  * This function returns a Form_pg_attribute pointer for a system attribute.
190  * Note that we elog if the presented attno is invalid, which would only
191  * happen if there's a problem upstream.
192  */
193 Form_pg_attribute
194 SystemAttributeDefinition(AttrNumber attno, bool relhasoids)
195 {
196         if (attno >= 0 || attno < -(int) lengthof(SysAtt))
197                 elog(ERROR, "invalid system attribute number %d", attno);
198         if (attno == ObjectIdAttributeNumber && !relhasoids)
199                 elog(ERROR, "invalid system attribute number %d", attno);
200         return SysAtt[-attno - 1];
201 }
202
203 /*
204  * If the given name is a system attribute name, return a Form_pg_attribute
205  * pointer for a prototype definition.  If not, return NULL.
206  */
207 Form_pg_attribute
208 SystemAttributeByName(const char *attname, bool relhasoids)
209 {
210         int                     j;
211
212         for (j = 0; j < (int) lengthof(SysAtt); j++)
213         {
214                 Form_pg_attribute att = SysAtt[j];
215
216                 if (relhasoids || att->attnum != ObjectIdAttributeNumber)
217                 {
218                         if (strcmp(NameStr(att->attname), attname) == 0)
219                                 return att;
220                 }
221         }
222
223         return NULL;
224 }
225
226
227 /* ----------------------------------------------------------------
228  *                              XXX END OF UGLY HARD CODED BADNESS XXX
229  * ---------------------------------------------------------------- */
230
231
232 /* ----------------------------------------------------------------
233  *              heap_create             - Create an uncataloged heap relation
234  *
235  *              Note API change: the caller must now always provide the OID
236  *              to use for the relation.  The relfilenode may (and, normally,
237  *              should) be left unspecified.
238  *
239  *              rel->rd_rel is initialized by RelationBuildLocalRelation,
240  *              and is mostly zeroes at return.
241  * ----------------------------------------------------------------
242  */
243 Relation
244 heap_create(const char *relname,
245                         Oid relnamespace,
246                         Oid reltablespace,
247                         Oid relid,
248                         Oid relfilenode,
249                         TupleDesc tupDesc,
250                         char relkind,
251                         char relpersistence,
252                         bool shared_relation,
253                         bool mapped_relation,
254                         bool allow_system_table_mods)
255 {
256         bool            create_storage;
257         Relation        rel;
258
259         /* The caller must have provided an OID for the relation. */
260         Assert(OidIsValid(relid));
261
262         /*
263          * Don't allow creating relations in pg_catalog directly, even though it
264          * is allowed to move user defined relations there. Semantics with search
265          * paths including pg_catalog are too confusing for now.
266          *
267          * But allow creating indexes on relations in pg_catalog even if
268          * allow_system_table_mods = off, upper layers already guarantee it's on a
269          * user defined relation, not a system one.
270          */
271         if (!allow_system_table_mods &&
272                 ((IsSystemNamespace(relnamespace) && relkind != RELKIND_INDEX) ||
273                  IsToastNamespace(relnamespace)) &&
274                 IsNormalProcessingMode())
275                 ereport(ERROR,
276                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
277                                  errmsg("permission denied to create \"%s.%s\"",
278                                                 get_namespace_name(relnamespace), relname),
279                 errdetail("System catalog modifications are currently disallowed.")));
280
281         /*
282          * Decide if we need storage or not, and handle a couple other special
283          * cases for particular relkinds.
284          */
285         switch (relkind)
286         {
287                 case RELKIND_VIEW:
288                 case RELKIND_COMPOSITE_TYPE:
289                 case RELKIND_FOREIGN_TABLE:
290                         create_storage = false;
291
292                         /*
293                          * Force reltablespace to zero if the relation has no physical
294                          * storage.  This is mainly just for cleanliness' sake.
295                          */
296                         reltablespace = InvalidOid;
297                         break;
298                 case RELKIND_SEQUENCE:
299                         create_storage = true;
300
301                         /*
302                          * Force reltablespace to zero for sequences, since we don't
303                          * support moving them around into different tablespaces.
304                          */
305                         reltablespace = InvalidOid;
306                         break;
307                 default:
308                         create_storage = true;
309                         break;
310         }
311
312         /*
313          * Unless otherwise requested, the physical ID (relfilenode) is initially
314          * the same as the logical ID (OID).  When the caller did specify a
315          * relfilenode, it already exists; do not attempt to create it.
316          */
317         if (OidIsValid(relfilenode))
318                 create_storage = false;
319         else
320                 relfilenode = relid;
321
322         /*
323          * Never allow a pg_class entry to explicitly specify the database's
324          * default tablespace in reltablespace; force it to zero instead. This
325          * ensures that if the database is cloned with a different default
326          * tablespace, the pg_class entry will still match where CREATE DATABASE
327          * will put the physically copied relation.
328          *
329          * Yes, this is a bit of a hack.
330          */
331         if (reltablespace == MyDatabaseTableSpace)
332                 reltablespace = InvalidOid;
333
334         /*
335          * build the relcache entry.
336          */
337         rel = RelationBuildLocalRelation(relname,
338                                                                          relnamespace,
339                                                                          tupDesc,
340                                                                          relid,
341                                                                          relfilenode,
342                                                                          reltablespace,
343                                                                          shared_relation,
344                                                                          mapped_relation,
345                                                                          relpersistence,
346                                                                          relkind);
347
348         /*
349          * Have the storage manager create the relation's disk file, if needed.
350          *
351          * We only create the main fork here, other forks will be created on
352          * demand.
353          */
354         if (create_storage)
355         {
356                 RelationOpenSmgr(rel);
357                 RelationCreateStorage(rel->rd_node, relpersistence);
358         }
359
360         return rel;
361 }
362
363 /* ----------------------------------------------------------------
364  *              heap_create_with_catalog                - Create a cataloged relation
365  *
366  *              this is done in multiple steps:
367  *
368  *              1) CheckAttributeNamesTypes() is used to make certain the tuple
369  *                 descriptor contains a valid set of attribute names and types
370  *
371  *              2) pg_class is opened and get_relname_relid()
372  *                 performs a scan to ensure that no relation with the
373  *                 same name already exists.
374  *
375  *              3) heap_create() is called to create the new relation on disk.
376  *
377  *              4) TypeCreate() is called to define a new type corresponding
378  *                 to the new relation.
379  *
380  *              5) AddNewRelationTuple() is called to register the
381  *                 relation in pg_class.
382  *
383  *              6) AddNewAttributeTuples() is called to register the
384  *                 new relation's schema in pg_attribute.
385  *
386  *              7) StoreConstraints is called ()                - vadim 08/22/97
387  *
388  *              8) the relations are closed and the new relation's oid
389  *                 is returned.
390  *
391  * ----------------------------------------------------------------
392  */
393
394 /* --------------------------------
395  *              CheckAttributeNamesTypes
396  *
397  *              this is used to make certain the tuple descriptor contains a
398  *              valid set of attribute names and datatypes.  a problem simply
399  *              generates ereport(ERROR) which aborts the current transaction.
400  * --------------------------------
401  */
402 void
403 CheckAttributeNamesTypes(TupleDesc tupdesc, char relkind,
404                                                  bool allow_system_table_mods)
405 {
406         int                     i;
407         int                     j;
408         int                     natts = tupdesc->natts;
409
410         /* Sanity check on column count */
411         if (natts < 0 || natts > MaxHeapAttributeNumber)
412                 ereport(ERROR,
413                                 (errcode(ERRCODE_TOO_MANY_COLUMNS),
414                                  errmsg("tables can have at most %d columns",
415                                                 MaxHeapAttributeNumber)));
416
417         /*
418          * first check for collision with system attribute names
419          *
420          * Skip this for a view or type relation, since those don't have system
421          * attributes.
422          */
423         if (relkind != RELKIND_VIEW && relkind != RELKIND_COMPOSITE_TYPE)
424         {
425                 for (i = 0; i < natts; i++)
426                 {
427                         if (SystemAttributeByName(NameStr(tupdesc->attrs[i]->attname),
428                                                                           tupdesc->tdhasoid) != NULL)
429                                 ereport(ERROR,
430                                                 (errcode(ERRCODE_DUPLICATE_COLUMN),
431                                                  errmsg("column name \"%s\" conflicts with a system column name",
432                                                                 NameStr(tupdesc->attrs[i]->attname))));
433                 }
434         }
435
436         /*
437          * next check for repeated attribute names
438          */
439         for (i = 1; i < natts; i++)
440         {
441                 for (j = 0; j < i; j++)
442                 {
443                         if (strcmp(NameStr(tupdesc->attrs[j]->attname),
444                                            NameStr(tupdesc->attrs[i]->attname)) == 0)
445                                 ereport(ERROR,
446                                                 (errcode(ERRCODE_DUPLICATE_COLUMN),
447                                                  errmsg("column name \"%s\" specified more than once",
448                                                                 NameStr(tupdesc->attrs[j]->attname))));
449                 }
450         }
451
452         /*
453          * next check the attribute types
454          */
455         for (i = 0; i < natts; i++)
456         {
457                 CheckAttributeType(NameStr(tupdesc->attrs[i]->attname),
458                                                    tupdesc->attrs[i]->atttypid,
459                                                    tupdesc->attrs[i]->attcollation,
460                                                    NIL, /* assume we're creating a new rowtype */
461                                                    allow_system_table_mods);
462         }
463 }
464
465 /* --------------------------------
466  *              CheckAttributeType
467  *
468  *              Verify that the proposed datatype of an attribute is legal.
469  *              This is needed mainly because there are types (and pseudo-types)
470  *              in the catalogs that we do not support as elements of real tuples.
471  *              We also check some other properties required of a table column.
472  *
473  * If the attribute is being proposed for addition to an existing table or
474  * composite type, pass a one-element list of the rowtype OID as
475  * containing_rowtypes.  When checking a to-be-created rowtype, it's
476  * sufficient to pass NIL, because there could not be any recursive reference
477  * to a not-yet-existing rowtype.
478  * --------------------------------
479  */
480 void
481 CheckAttributeType(const char *attname,
482                                    Oid atttypid, Oid attcollation,
483                                    List *containing_rowtypes,
484                                    bool allow_system_table_mods)
485 {
486         char            att_typtype = get_typtype(atttypid);
487         Oid                     att_typelem;
488
489         if (atttypid == UNKNOWNOID)
490         {
491                 /*
492                  * Warn user, but don't fail, if column to be created has UNKNOWN type
493                  * (usually as a result of a 'retrieve into' - jolly)
494                  */
495                 ereport(WARNING,
496                                 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
497                                  errmsg("column \"%s\" has type \"unknown\"", attname),
498                                  errdetail("Proceeding with relation creation anyway.")));
499         }
500         else if (att_typtype == TYPTYPE_PSEUDO)
501         {
502                 /*
503                  * Refuse any attempt to create a pseudo-type column, except for a
504                  * special hack for pg_statistic: allow ANYARRAY when modifying system
505                  * catalogs (this allows creating pg_statistic and cloning it during
506                  * VACUUM FULL)
507                  */
508                 if (atttypid != ANYARRAYOID || !allow_system_table_mods)
509                         ereport(ERROR,
510                                         (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
511                                          errmsg("column \"%s\" has pseudo-type %s",
512                                                         attname, format_type_be(atttypid))));
513         }
514         else if (att_typtype == TYPTYPE_DOMAIN)
515         {
516                 /*
517                  * If it's a domain, recurse to check its base type.
518                  */
519                 CheckAttributeType(attname, getBaseType(atttypid), attcollation,
520                                                    containing_rowtypes,
521                                                    allow_system_table_mods);
522         }
523         else if (att_typtype == TYPTYPE_COMPOSITE)
524         {
525                 /*
526                  * For a composite type, recurse into its attributes.
527                  */
528                 Relation        relation;
529                 TupleDesc       tupdesc;
530                 int                     i;
531
532                 /*
533                  * Check for self-containment.  Eventually we might be able to allow
534                  * this (just return without complaint, if so) but it's not clear how
535                  * many other places would require anti-recursion defenses before it
536                  * would be safe to allow tables to contain their own rowtype.
537                  */
538                 if (list_member_oid(containing_rowtypes, atttypid))
539                         ereport(ERROR,
540                                         (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
541                                 errmsg("composite type %s cannot be made a member of itself",
542                                            format_type_be(atttypid))));
543
544                 containing_rowtypes = lcons_oid(atttypid, containing_rowtypes);
545
546                 relation = relation_open(get_typ_typrelid(atttypid), AccessShareLock);
547
548                 tupdesc = RelationGetDescr(relation);
549
550                 for (i = 0; i < tupdesc->natts; i++)
551                 {
552                         Form_pg_attribute attr = tupdesc->attrs[i];
553
554                         if (attr->attisdropped)
555                                 continue;
556                         CheckAttributeType(NameStr(attr->attname),
557                                                            attr->atttypid, attr->attcollation,
558                                                            containing_rowtypes,
559                                                            allow_system_table_mods);
560                 }
561
562                 relation_close(relation, AccessShareLock);
563
564                 containing_rowtypes = list_delete_first(containing_rowtypes);
565         }
566         else if (OidIsValid((att_typelem = get_element_type(atttypid))))
567         {
568                 /*
569                  * Must recurse into array types, too, in case they are composite.
570                  */
571                 CheckAttributeType(attname, att_typelem, attcollation,
572                                                    containing_rowtypes,
573                                                    allow_system_table_mods);
574         }
575
576         /*
577          * This might not be strictly invalid per SQL standard, but it is pretty
578          * useless, and it cannot be dumped, so we must disallow it.
579          */
580         if (!OidIsValid(attcollation) && type_is_collatable(atttypid))
581                 ereport(ERROR,
582                                 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
583                                  errmsg("no collation was derived for column \"%s\" with collatable type %s",
584                                                 attname, format_type_be(atttypid)),
585                 errhint("Use the COLLATE clause to set the collation explicitly.")));
586 }
587
588 /*
589  * InsertPgAttributeTuple
590  *              Construct and insert a new tuple in pg_attribute.
591  *
592  * Caller has already opened and locked pg_attribute.  new_attribute is the
593  * attribute to insert (but we ignore attacl and attoptions, which are always
594  * initialized to NULL).
595  *
596  * indstate is the index state for CatalogIndexInsert.  It can be passed as
597  * NULL, in which case we'll fetch the necessary info.  (Don't do this when
598  * inserting multiple attributes, because it's a tad more expensive.)
599  */
600 void
601 InsertPgAttributeTuple(Relation pg_attribute_rel,
602                                            Form_pg_attribute new_attribute,
603                                            CatalogIndexState indstate)
604 {
605         Datum           values[Natts_pg_attribute];
606         bool            nulls[Natts_pg_attribute];
607         HeapTuple       tup;
608
609         /* This is a tad tedious, but way cleaner than what we used to do... */
610         memset(values, 0, sizeof(values));
611         memset(nulls, false, sizeof(nulls));
612
613         values[Anum_pg_attribute_attrelid - 1] = ObjectIdGetDatum(new_attribute->attrelid);
614         values[Anum_pg_attribute_attname - 1] = NameGetDatum(&new_attribute->attname);
615         values[Anum_pg_attribute_atttypid - 1] = ObjectIdGetDatum(new_attribute->atttypid);
616         values[Anum_pg_attribute_attstattarget - 1] = Int32GetDatum(new_attribute->attstattarget);
617         values[Anum_pg_attribute_attlen - 1] = Int16GetDatum(new_attribute->attlen);
618         values[Anum_pg_attribute_attnum - 1] = Int16GetDatum(new_attribute->attnum);
619         values[Anum_pg_attribute_attndims - 1] = Int32GetDatum(new_attribute->attndims);
620         values[Anum_pg_attribute_attcacheoff - 1] = Int32GetDatum(new_attribute->attcacheoff);
621         values[Anum_pg_attribute_atttypmod - 1] = Int32GetDatum(new_attribute->atttypmod);
622         values[Anum_pg_attribute_attbyval - 1] = BoolGetDatum(new_attribute->attbyval);
623         values[Anum_pg_attribute_attstorage - 1] = CharGetDatum(new_attribute->attstorage);
624         values[Anum_pg_attribute_attalign - 1] = CharGetDatum(new_attribute->attalign);
625         values[Anum_pg_attribute_attnotnull - 1] = BoolGetDatum(new_attribute->attnotnull);
626         values[Anum_pg_attribute_atthasdef - 1] = BoolGetDatum(new_attribute->atthasdef);
627         values[Anum_pg_attribute_attisdropped - 1] = BoolGetDatum(new_attribute->attisdropped);
628         values[Anum_pg_attribute_attislocal - 1] = BoolGetDatum(new_attribute->attislocal);
629         values[Anum_pg_attribute_attinhcount - 1] = Int32GetDatum(new_attribute->attinhcount);
630         values[Anum_pg_attribute_attcollation - 1] = ObjectIdGetDatum(new_attribute->attcollation);
631
632         /* start out with empty permissions and empty options */
633         nulls[Anum_pg_attribute_attacl - 1] = true;
634         nulls[Anum_pg_attribute_attoptions - 1] = true;
635         nulls[Anum_pg_attribute_attfdwoptions - 1] = true;
636
637         tup = heap_form_tuple(RelationGetDescr(pg_attribute_rel), values, nulls);
638
639         /* finally insert the new tuple, update the indexes, and clean up */
640         simple_heap_insert(pg_attribute_rel, tup);
641
642         if (indstate != NULL)
643                 CatalogIndexInsert(indstate, tup);
644         else
645                 CatalogUpdateIndexes(pg_attribute_rel, tup);
646
647         heap_freetuple(tup);
648 }
649
650 /* --------------------------------
651  *              AddNewAttributeTuples
652  *
653  *              this registers the new relation's schema by adding
654  *              tuples to pg_attribute.
655  * --------------------------------
656  */
657 static void
658 AddNewAttributeTuples(Oid new_rel_oid,
659                                           TupleDesc tupdesc,
660                                           char relkind,
661                                           bool oidislocal,
662                                           int oidinhcount)
663 {
664         Form_pg_attribute attr;
665         int                     i;
666         Relation        rel;
667         CatalogIndexState indstate;
668         int                     natts = tupdesc->natts;
669         ObjectAddress myself,
670                                 referenced;
671
672         /*
673          * open pg_attribute and its indexes.
674          */
675         rel = heap_open(AttributeRelationId, RowExclusiveLock);
676
677         indstate = CatalogOpenIndexes(rel);
678
679         /*
680          * First we add the user attributes.  This is also a convenient place to
681          * add dependencies on their datatypes and collations.
682          */
683         for (i = 0; i < natts; i++)
684         {
685                 attr = tupdesc->attrs[i];
686                 /* Fill in the correct relation OID */
687                 attr->attrelid = new_rel_oid;
688                 /* Make sure these are OK, too */
689                 attr->attstattarget = -1;
690                 attr->attcacheoff = -1;
691
692                 InsertPgAttributeTuple(rel, attr, indstate);
693
694                 /* Add dependency info */
695                 myself.classId = RelationRelationId;
696                 myself.objectId = new_rel_oid;
697                 myself.objectSubId = i + 1;
698                 referenced.classId = TypeRelationId;
699                 referenced.objectId = attr->atttypid;
700                 referenced.objectSubId = 0;
701                 recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
702
703                 /* The default collation is pinned, so don't bother recording it */
704                 if (OidIsValid(attr->attcollation) &&
705                         attr->attcollation != DEFAULT_COLLATION_OID)
706                 {
707                         referenced.classId = CollationRelationId;
708                         referenced.objectId = attr->attcollation;
709                         referenced.objectSubId = 0;
710                         recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
711                 }
712         }
713
714         /*
715          * Next we add the system attributes.  Skip OID if rel has no OIDs. Skip
716          * all for a view or type relation.  We don't bother with making datatype
717          * dependencies here, since presumably all these types are pinned.
718          */
719         if (relkind != RELKIND_VIEW && relkind != RELKIND_COMPOSITE_TYPE)
720         {
721                 for (i = 0; i < (int) lengthof(SysAtt); i++)
722                 {
723                         FormData_pg_attribute attStruct;
724
725                         /* skip OID where appropriate */
726                         if (!tupdesc->tdhasoid &&
727                                 SysAtt[i]->attnum == ObjectIdAttributeNumber)
728                                 continue;
729
730                         memcpy(&attStruct, (char *) SysAtt[i], sizeof(FormData_pg_attribute));
731
732                         /* Fill in the correct relation OID in the copied tuple */
733                         attStruct.attrelid = new_rel_oid;
734
735                         /* Fill in correct inheritance info for the OID column */
736                         if (attStruct.attnum == ObjectIdAttributeNumber)
737                         {
738                                 attStruct.attislocal = oidislocal;
739                                 attStruct.attinhcount = oidinhcount;
740                         }
741
742                         InsertPgAttributeTuple(rel, &attStruct, indstate);
743                 }
744         }
745
746         /*
747          * clean up
748          */
749         CatalogCloseIndexes(indstate);
750
751         heap_close(rel, RowExclusiveLock);
752 }
753
754 /* --------------------------------
755  *              InsertPgClassTuple
756  *
757  *              Construct and insert a new tuple in pg_class.
758  *
759  * Caller has already opened and locked pg_class.
760  * Tuple data is taken from new_rel_desc->rd_rel, except for the
761  * variable-width fields which are not present in a cached reldesc.
762  * relacl and reloptions are passed in Datum form (to avoid having
763  * to reference the data types in heap.h).  Pass (Datum) 0 to set them
764  * to NULL.
765  * --------------------------------
766  */
767 void
768 InsertPgClassTuple(Relation pg_class_desc,
769                                    Relation new_rel_desc,
770                                    Oid new_rel_oid,
771                                    Datum relacl,
772                                    Datum reloptions)
773 {
774         Form_pg_class rd_rel = new_rel_desc->rd_rel;
775         Datum           values[Natts_pg_class];
776         bool            nulls[Natts_pg_class];
777         HeapTuple       tup;
778
779         /* This is a tad tedious, but way cleaner than what we used to do... */
780         memset(values, 0, sizeof(values));
781         memset(nulls, false, sizeof(nulls));
782
783         values[Anum_pg_class_relname - 1] = NameGetDatum(&rd_rel->relname);
784         values[Anum_pg_class_relnamespace - 1] = ObjectIdGetDatum(rd_rel->relnamespace);
785         values[Anum_pg_class_reltype - 1] = ObjectIdGetDatum(rd_rel->reltype);
786         values[Anum_pg_class_reloftype - 1] = ObjectIdGetDatum(rd_rel->reloftype);
787         values[Anum_pg_class_relowner - 1] = ObjectIdGetDatum(rd_rel->relowner);
788         values[Anum_pg_class_relam - 1] = ObjectIdGetDatum(rd_rel->relam);
789         values[Anum_pg_class_relfilenode - 1] = ObjectIdGetDatum(rd_rel->relfilenode);
790         values[Anum_pg_class_reltablespace - 1] = ObjectIdGetDatum(rd_rel->reltablespace);
791         values[Anum_pg_class_relpages - 1] = Int32GetDatum(rd_rel->relpages);
792         values[Anum_pg_class_reltuples - 1] = Float4GetDatum(rd_rel->reltuples);
793         values[Anum_pg_class_relallvisible - 1] = Int32GetDatum(rd_rel->relallvisible);
794         values[Anum_pg_class_reltoastrelid - 1] = ObjectIdGetDatum(rd_rel->reltoastrelid);
795         values[Anum_pg_class_relhasindex - 1] = BoolGetDatum(rd_rel->relhasindex);
796         values[Anum_pg_class_relisshared - 1] = BoolGetDatum(rd_rel->relisshared);
797         values[Anum_pg_class_relpersistence - 1] = CharGetDatum(rd_rel->relpersistence);
798         values[Anum_pg_class_relkind - 1] = CharGetDatum(rd_rel->relkind);
799         values[Anum_pg_class_relnatts - 1] = Int16GetDatum(rd_rel->relnatts);
800         values[Anum_pg_class_relchecks - 1] = Int16GetDatum(rd_rel->relchecks);
801         values[Anum_pg_class_relhasoids - 1] = BoolGetDatum(rd_rel->relhasoids);
802         values[Anum_pg_class_relhaspkey - 1] = BoolGetDatum(rd_rel->relhaspkey);
803         values[Anum_pg_class_relhasrules - 1] = BoolGetDatum(rd_rel->relhasrules);
804         values[Anum_pg_class_relhastriggers - 1] = BoolGetDatum(rd_rel->relhastriggers);
805         values[Anum_pg_class_relrowsecurity - 1] = BoolGetDatum(rd_rel->relrowsecurity);
806         values[Anum_pg_class_relforcerowsecurity - 1] = BoolGetDatum(rd_rel->relforcerowsecurity);
807         values[Anum_pg_class_relhassubclass - 1] = BoolGetDatum(rd_rel->relhassubclass);
808         values[Anum_pg_class_relispopulated - 1] = BoolGetDatum(rd_rel->relispopulated);
809         values[Anum_pg_class_relreplident - 1] = CharGetDatum(rd_rel->relreplident);
810         values[Anum_pg_class_relfrozenxid - 1] = TransactionIdGetDatum(rd_rel->relfrozenxid);
811         values[Anum_pg_class_relminmxid - 1] = MultiXactIdGetDatum(rd_rel->relminmxid);
812         if (relacl != (Datum) 0)
813                 values[Anum_pg_class_relacl - 1] = relacl;
814         else
815                 nulls[Anum_pg_class_relacl - 1] = true;
816         if (reloptions != (Datum) 0)
817                 values[Anum_pg_class_reloptions - 1] = reloptions;
818         else
819                 nulls[Anum_pg_class_reloptions - 1] = true;
820
821         tup = heap_form_tuple(RelationGetDescr(pg_class_desc), values, nulls);
822
823         /*
824          * The new tuple must have the oid already chosen for the rel.  Sure would
825          * be embarrassing to do this sort of thing in polite company.
826          */
827         HeapTupleSetOid(tup, new_rel_oid);
828
829         /* finally insert the new tuple, update the indexes, and clean up */
830         simple_heap_insert(pg_class_desc, tup);
831
832         CatalogUpdateIndexes(pg_class_desc, tup);
833
834         heap_freetuple(tup);
835 }
836
837 /* --------------------------------
838  *              AddNewRelationTuple
839  *
840  *              this registers the new relation in the catalogs by
841  *              adding a tuple to pg_class.
842  * --------------------------------
843  */
844 static void
845 AddNewRelationTuple(Relation pg_class_desc,
846                                         Relation new_rel_desc,
847                                         Oid new_rel_oid,
848                                         Oid new_type_oid,
849                                         Oid reloftype,
850                                         Oid relowner,
851                                         char relkind,
852                                         Datum relacl,
853                                         Datum reloptions)
854 {
855         Form_pg_class new_rel_reltup;
856
857         /*
858          * first we update some of the information in our uncataloged relation's
859          * relation descriptor.
860          */
861         new_rel_reltup = new_rel_desc->rd_rel;
862
863         switch (relkind)
864         {
865                 case RELKIND_RELATION:
866                 case RELKIND_MATVIEW:
867                 case RELKIND_INDEX:
868                 case RELKIND_TOASTVALUE:
869                         /* The relation is real, but as yet empty */
870                         new_rel_reltup->relpages = 0;
871                         new_rel_reltup->reltuples = 0;
872                         new_rel_reltup->relallvisible = 0;
873                         break;
874                 case RELKIND_SEQUENCE:
875                         /* Sequences always have a known size */
876                         new_rel_reltup->relpages = 1;
877                         new_rel_reltup->reltuples = 1;
878                         new_rel_reltup->relallvisible = 0;
879                         break;
880                 default:
881                         /* Views, etc, have no disk storage */
882                         new_rel_reltup->relpages = 0;
883                         new_rel_reltup->reltuples = 0;
884                         new_rel_reltup->relallvisible = 0;
885                         break;
886         }
887
888         /* Initialize relfrozenxid and relminmxid */
889         if (relkind == RELKIND_RELATION ||
890                 relkind == RELKIND_MATVIEW ||
891                 relkind == RELKIND_TOASTVALUE)
892         {
893                 /*
894                  * Initialize to the minimum XID that could put tuples in the table.
895                  * We know that no xacts older than RecentXmin are still running, so
896                  * that will do.
897                  */
898                 new_rel_reltup->relfrozenxid = RecentXmin;
899
900                 /*
901                  * Similarly, initialize the minimum Multixact to the first value that
902                  * could possibly be stored in tuples in the table.  Running
903                  * transactions could reuse values from their local cache, so we are
904                  * careful to consider all currently running multis.
905                  *
906                  * XXX this could be refined further, but is it worth the hassle?
907                  */
908                 new_rel_reltup->relminmxid = GetOldestMultiXactId();
909         }
910         else
911         {
912                 /*
913                  * Other relation types will not contain XIDs, so set relfrozenxid to
914                  * InvalidTransactionId.  (Note: a sequence does contain a tuple, but
915                  * we force its xmin to be FrozenTransactionId always; see
916                  * commands/sequence.c.)
917                  */
918                 new_rel_reltup->relfrozenxid = InvalidTransactionId;
919                 new_rel_reltup->relminmxid = InvalidMultiXactId;
920         }
921
922         new_rel_reltup->relowner = relowner;
923         new_rel_reltup->reltype = new_type_oid;
924         new_rel_reltup->reloftype = reloftype;
925
926         new_rel_desc->rd_att->tdtypeid = new_type_oid;
927
928         /* Now build and insert the tuple */
929         InsertPgClassTuple(pg_class_desc, new_rel_desc, new_rel_oid,
930                                            relacl, reloptions);
931 }
932
933
934 /* --------------------------------
935  *              AddNewRelationType -
936  *
937  *              define a composite type corresponding to the new relation
938  * --------------------------------
939  */
940 static ObjectAddress
941 AddNewRelationType(const char *typeName,
942                                    Oid typeNamespace,
943                                    Oid new_rel_oid,
944                                    char new_rel_kind,
945                                    Oid ownerid,
946                                    Oid new_row_type,
947                                    Oid new_array_type)
948 {
949         return
950                 TypeCreate(new_row_type,        /* optional predetermined OID */
951                                    typeName,    /* type name */
952                                    typeNamespace,               /* type namespace */
953                                    new_rel_oid, /* relation oid */
954                                    new_rel_kind,        /* relation kind */
955                                    ownerid,             /* owner's ID */
956                                    -1,                  /* internal size (varlena) */
957                                    TYPTYPE_COMPOSITE,   /* type-type (composite) */
958                                    TYPCATEGORY_COMPOSITE,               /* type-category (ditto) */
959                                    false,               /* composite types are never preferred */
960                                    DEFAULT_TYPDELIM,    /* default array delimiter */
961                                    F_RECORD_IN, /* input procedure */
962                                    F_RECORD_OUT,        /* output procedure */
963                                    F_RECORD_RECV,               /* receive procedure */
964                                    F_RECORD_SEND,               /* send procedure */
965                                    InvalidOid,  /* typmodin procedure - none */
966                                    InvalidOid,  /* typmodout procedure - none */
967                                    InvalidOid,  /* analyze procedure - default */
968                                    InvalidOid,  /* array element type - irrelevant */
969                                    false,               /* this is not an array type */
970                                    new_array_type,              /* array type if any */
971                                    InvalidOid,  /* domain base type - irrelevant */
972                                    NULL,                /* default value - none */
973                                    NULL,                /* default binary representation */
974                                    false,               /* passed by reference */
975                                    'd',                 /* alignment - must be the largest! */
976                                    'x',                 /* fully TOASTable */
977                                    -1,                  /* typmod */
978                                    0,                   /* array dimensions for typBaseType */
979                                    false,               /* Type NOT NULL */
980                                    InvalidOid); /* rowtypes never have a collation */
981 }
982
983 /* --------------------------------
984  *              heap_create_with_catalog
985  *
986  *              creates a new cataloged relation.  see comments above.
987  *
988  * Arguments:
989  *      relname: name to give to new rel
990  *      relnamespace: OID of namespace it goes in
991  *      reltablespace: OID of tablespace it goes in
992  *      relid: OID to assign to new rel, or InvalidOid to select a new OID
993  *      reltypeid: OID to assign to rel's rowtype, or InvalidOid to select one
994  *      reloftypeid: if a typed table, OID of underlying type; else InvalidOid
995  *      ownerid: OID of new rel's owner
996  *      tupdesc: tuple descriptor (source of column definitions)
997  *      cooked_constraints: list of precooked check constraints and defaults
998  *      relkind: relkind for new rel
999  *      relpersistence: rel's persistence status (permanent, temp, or unlogged)
1000  *      shared_relation: TRUE if it's to be a shared relation
1001  *      mapped_relation: TRUE if the relation will use the relfilenode map
1002  *      oidislocal: TRUE if oid column (if any) should be marked attislocal
1003  *      oidinhcount: attinhcount to assign to oid column (if any)
1004  *      oncommit: ON COMMIT marking (only relevant if it's a temp table)
1005  *      reloptions: reloptions in Datum form, or (Datum) 0 if none
1006  *      use_user_acl: TRUE if should look for user-defined default permissions;
1007  *              if FALSE, relacl is always set NULL
1008  *      allow_system_table_mods: TRUE to allow creation in system namespaces
1009  *      is_internal: is this a system-generated catalog?
1010  *
1011  * Output parameters:
1012  *      typaddress: if not null, gets the object address of the new pg_type entry
1013  *
1014  * Returns the OID of the new relation
1015  * --------------------------------
1016  */
1017 Oid
1018 heap_create_with_catalog(const char *relname,
1019                                                  Oid relnamespace,
1020                                                  Oid reltablespace,
1021                                                  Oid relid,
1022                                                  Oid reltypeid,
1023                                                  Oid reloftypeid,
1024                                                  Oid ownerid,
1025                                                  TupleDesc tupdesc,
1026                                                  List *cooked_constraints,
1027                                                  char relkind,
1028                                                  char relpersistence,
1029                                                  bool shared_relation,
1030                                                  bool mapped_relation,
1031                                                  bool oidislocal,
1032                                                  int oidinhcount,
1033                                                  OnCommitAction oncommit,
1034                                                  Datum reloptions,
1035                                                  bool use_user_acl,
1036                                                  bool allow_system_table_mods,
1037                                                  bool is_internal,
1038                                                  ObjectAddress *typaddress)
1039 {
1040         Relation        pg_class_desc;
1041         Relation        new_rel_desc;
1042         Acl                *relacl;
1043         Oid                     existing_relid;
1044         Oid                     old_type_oid;
1045         Oid                     new_type_oid;
1046         ObjectAddress new_type_addr;
1047         Oid                     new_array_oid = InvalidOid;
1048
1049         pg_class_desc = heap_open(RelationRelationId, RowExclusiveLock);
1050
1051         /*
1052          * sanity checks
1053          */
1054         Assert(IsNormalProcessingMode() || IsBootstrapProcessingMode());
1055
1056         CheckAttributeNamesTypes(tupdesc, relkind, allow_system_table_mods);
1057
1058         /*
1059          * This would fail later on anyway, if the relation already exists.  But
1060          * by catching it here we can emit a nicer error message.
1061          */
1062         existing_relid = get_relname_relid(relname, relnamespace);
1063         if (existing_relid != InvalidOid)
1064                 ereport(ERROR,
1065                                 (errcode(ERRCODE_DUPLICATE_TABLE),
1066                                  errmsg("relation \"%s\" already exists", relname)));
1067
1068         /*
1069          * Since we are going to create a rowtype as well, also check for
1070          * collision with an existing type name.  If there is one and it's an
1071          * autogenerated array, we can rename it out of the way; otherwise we can
1072          * at least give a good error message.
1073          */
1074         old_type_oid = GetSysCacheOid2(TYPENAMENSP,
1075                                                                    CStringGetDatum(relname),
1076                                                                    ObjectIdGetDatum(relnamespace));
1077         if (OidIsValid(old_type_oid))
1078         {
1079                 if (!moveArrayTypeName(old_type_oid, relname, relnamespace))
1080                         ereport(ERROR,
1081                                         (errcode(ERRCODE_DUPLICATE_OBJECT),
1082                                          errmsg("type \"%s\" already exists", relname),
1083                            errhint("A relation has an associated type of the same name, "
1084                                            "so you must use a name that doesn't conflict "
1085                                            "with any existing type.")));
1086         }
1087
1088         /*
1089          * Shared relations must be in pg_global (last-ditch check)
1090          */
1091         if (shared_relation && reltablespace != GLOBALTABLESPACE_OID)
1092                 elog(ERROR, "shared relations must be placed in pg_global tablespace");
1093
1094         /*
1095          * Allocate an OID for the relation, unless we were told what to use.
1096          *
1097          * The OID will be the relfilenode as well, so make sure it doesn't
1098          * collide with either pg_class OIDs or existing physical files.
1099          */
1100         if (!OidIsValid(relid))
1101         {
1102                 /* Use binary-upgrade override for pg_class.oid/relfilenode? */
1103                 if (IsBinaryUpgrade &&
1104                         (relkind == RELKIND_RELATION || relkind == RELKIND_SEQUENCE ||
1105                          relkind == RELKIND_VIEW || relkind == RELKIND_MATVIEW ||
1106                          relkind == RELKIND_COMPOSITE_TYPE || relkind == RELKIND_FOREIGN_TABLE))
1107                 {
1108                         if (!OidIsValid(binary_upgrade_next_heap_pg_class_oid))
1109                                 ereport(ERROR,
1110                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1111                                                  errmsg("pg_class heap OID value not set when in binary upgrade mode")));
1112
1113                         relid = binary_upgrade_next_heap_pg_class_oid;
1114                         binary_upgrade_next_heap_pg_class_oid = InvalidOid;
1115                 }
1116                 /* There might be no TOAST table, so we have to test for it. */
1117                 else if (IsBinaryUpgrade &&
1118                                  OidIsValid(binary_upgrade_next_toast_pg_class_oid) &&
1119                                  relkind == RELKIND_TOASTVALUE)
1120                 {
1121                         relid = binary_upgrade_next_toast_pg_class_oid;
1122                         binary_upgrade_next_toast_pg_class_oid = InvalidOid;
1123                 }
1124                 else
1125                         relid = GetNewRelFileNode(reltablespace, pg_class_desc,
1126                                                                           relpersistence);
1127         }
1128
1129         /*
1130          * Determine the relation's initial permissions.
1131          */
1132         if (use_user_acl)
1133         {
1134                 switch (relkind)
1135                 {
1136                         case RELKIND_RELATION:
1137                         case RELKIND_VIEW:
1138                         case RELKIND_MATVIEW:
1139                         case RELKIND_FOREIGN_TABLE:
1140                                 relacl = get_user_default_acl(ACL_OBJECT_RELATION, ownerid,
1141                                                                                           relnamespace);
1142                                 break;
1143                         case RELKIND_SEQUENCE:
1144                                 relacl = get_user_default_acl(ACL_OBJECT_SEQUENCE, ownerid,
1145                                                                                           relnamespace);
1146                                 break;
1147                         default:
1148                                 relacl = NULL;
1149                                 break;
1150                 }
1151         }
1152         else
1153                 relacl = NULL;
1154
1155         /*
1156          * Create the relcache entry (mostly dummy at this point) and the physical
1157          * disk file.  (If we fail further down, it's the smgr's responsibility to
1158          * remove the disk file again.)
1159          */
1160         new_rel_desc = heap_create(relname,
1161                                                            relnamespace,
1162                                                            reltablespace,
1163                                                            relid,
1164                                                            InvalidOid,
1165                                                            tupdesc,
1166                                                            relkind,
1167                                                            relpersistence,
1168                                                            shared_relation,
1169                                                            mapped_relation,
1170                                                            allow_system_table_mods);
1171
1172         Assert(relid == RelationGetRelid(new_rel_desc));
1173
1174         /*
1175          * Decide whether to create an array type over the relation's rowtype. We
1176          * do not create any array types for system catalogs (ie, those made
1177          * during initdb). We do not create them where the use of a relation as
1178          * such is an implementation detail: toast tables, sequences and indexes.
1179          */
1180         if (IsUnderPostmaster && (relkind == RELKIND_RELATION ||
1181                                                           relkind == RELKIND_VIEW ||
1182                                                           relkind == RELKIND_MATVIEW ||
1183                                                           relkind == RELKIND_FOREIGN_TABLE ||
1184                                                           relkind == RELKIND_COMPOSITE_TYPE))
1185                 new_array_oid = AssignTypeArrayOid();
1186
1187         /*
1188          * Since defining a relation also defines a complex type, we add a new
1189          * system type corresponding to the new relation.  The OID of the type can
1190          * be preselected by the caller, but if reltypeid is InvalidOid, we'll
1191          * generate a new OID for it.
1192          *
1193          * NOTE: we could get a unique-index failure here, in case someone else is
1194          * creating the same type name in parallel but hadn't committed yet when
1195          * we checked for a duplicate name above.
1196          */
1197         new_type_addr = AddNewRelationType(relname,
1198                                                                            relnamespace,
1199                                                                            relid,
1200                                                                            relkind,
1201                                                                            ownerid,
1202                                                                            reltypeid,
1203                                                                            new_array_oid);
1204         new_type_oid = new_type_addr.objectId;
1205         if (typaddress)
1206                 *typaddress = new_type_addr;
1207
1208         /*
1209          * Now make the array type if wanted.
1210          */
1211         if (OidIsValid(new_array_oid))
1212         {
1213                 char       *relarrayname;
1214
1215                 relarrayname = makeArrayTypeName(relname, relnamespace);
1216
1217                 TypeCreate(new_array_oid,               /* force the type's OID to this */
1218                                    relarrayname,        /* Array type name */
1219                                    relnamespace,        /* Same namespace as parent */
1220                                    InvalidOid,  /* Not composite, no relationOid */
1221                                    0,                   /* relkind, also N/A here */
1222                                    ownerid,             /* owner's ID */
1223                                    -1,                  /* Internal size (varlena) */
1224                                    TYPTYPE_BASE,        /* Not composite - typelem is */
1225                                    TYPCATEGORY_ARRAY,   /* type-category (array) */
1226                                    false,               /* array types are never preferred */
1227                                    DEFAULT_TYPDELIM,    /* default array delimiter */
1228                                    F_ARRAY_IN,  /* array input proc */
1229                                    F_ARRAY_OUT, /* array output proc */
1230                                    F_ARRAY_RECV,        /* array recv (bin) proc */
1231                                    F_ARRAY_SEND,        /* array send (bin) proc */
1232                                    InvalidOid,  /* typmodin procedure - none */
1233                                    InvalidOid,  /* typmodout procedure - none */
1234                                    F_ARRAY_TYPANALYZE,  /* array analyze procedure */
1235                                    new_type_oid,        /* array element type - the rowtype */
1236                                    true,                /* yes, this is an array type */
1237                                    InvalidOid,  /* this has no array type */
1238                                    InvalidOid,  /* domain base type - irrelevant */
1239                                    NULL,                /* default value - none */
1240                                    NULL,                /* default binary representation */
1241                                    false,               /* passed by reference */
1242                                    'd',                 /* alignment - must be the largest! */
1243                                    'x',                 /* fully TOASTable */
1244                                    -1,                  /* typmod */
1245                                    0,                   /* array dimensions for typBaseType */
1246                                    false,               /* Type NOT NULL */
1247                                    InvalidOid); /* rowtypes never have a collation */
1248
1249                 pfree(relarrayname);
1250         }
1251
1252         /*
1253          * now create an entry in pg_class for the relation.
1254          *
1255          * NOTE: we could get a unique-index failure here, in case someone else is
1256          * creating the same relation name in parallel but hadn't committed yet
1257          * when we checked for a duplicate name above.
1258          */
1259         AddNewRelationTuple(pg_class_desc,
1260                                                 new_rel_desc,
1261                                                 relid,
1262                                                 new_type_oid,
1263                                                 reloftypeid,
1264                                                 ownerid,
1265                                                 relkind,
1266                                                 PointerGetDatum(relacl),
1267                                                 reloptions);
1268
1269         /*
1270          * now add tuples to pg_attribute for the attributes in our new relation.
1271          */
1272         AddNewAttributeTuples(relid, new_rel_desc->rd_att, relkind,
1273                                                   oidislocal, oidinhcount);
1274
1275         /*
1276          * Make a dependency link to force the relation to be deleted if its
1277          * namespace is.  Also make a dependency link to its owner, as well as
1278          * dependencies for any roles mentioned in the default ACL.
1279          *
1280          * For composite types, these dependencies are tracked for the pg_type
1281          * entry, so we needn't record them here.  Likewise, TOAST tables don't
1282          * need a namespace dependency (they live in a pinned namespace) nor an
1283          * owner dependency (they depend indirectly through the parent table), nor
1284          * should they have any ACL entries.  The same applies for extension
1285          * dependencies.
1286          *
1287          * If it's a temp table, we do not make it an extension member; this
1288          * prevents the unintuitive result that deletion of the temp table at
1289          * session end would make the whole extension go away.
1290          *
1291          * Also, skip this in bootstrap mode, since we don't make dependencies
1292          * while bootstrapping.
1293          */
1294         if (relkind != RELKIND_COMPOSITE_TYPE &&
1295                 relkind != RELKIND_TOASTVALUE &&
1296                 !IsBootstrapProcessingMode())
1297         {
1298                 ObjectAddress myself,
1299                                         referenced;
1300
1301                 myself.classId = RelationRelationId;
1302                 myself.objectId = relid;
1303                 myself.objectSubId = 0;
1304                 referenced.classId = NamespaceRelationId;
1305                 referenced.objectId = relnamespace;
1306                 referenced.objectSubId = 0;
1307                 recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
1308
1309                 recordDependencyOnOwner(RelationRelationId, relid, ownerid);
1310
1311                 if (relpersistence != RELPERSISTENCE_TEMP)
1312                         recordDependencyOnCurrentExtension(&myself, false);
1313
1314                 if (reloftypeid)
1315                 {
1316                         referenced.classId = TypeRelationId;
1317                         referenced.objectId = reloftypeid;
1318                         referenced.objectSubId = 0;
1319                         recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
1320                 }
1321
1322                 if (relacl != NULL)
1323                 {
1324                         int                     nnewmembers;
1325                         Oid                *newmembers;
1326
1327                         nnewmembers = aclmembers(relacl, &newmembers);
1328                         updateAclDependencies(RelationRelationId, relid, 0,
1329                                                                   ownerid,
1330                                                                   0, NULL,
1331                                                                   nnewmembers, newmembers);
1332                 }
1333         }
1334
1335         /* Post creation hook for new relation */
1336         InvokeObjectPostCreateHookArg(RelationRelationId, relid, 0, is_internal);
1337
1338         /*
1339          * Store any supplied constraints and defaults.
1340          *
1341          * NB: this may do a CommandCounterIncrement and rebuild the relcache
1342          * entry, so the relation must be valid and self-consistent at this point.
1343          * In particular, there are not yet constraints and defaults anywhere.
1344          */
1345         StoreConstraints(new_rel_desc, cooked_constraints, is_internal);
1346
1347         /*
1348          * If there's a special on-commit action, remember it
1349          */
1350         if (oncommit != ONCOMMIT_NOOP)
1351                 register_on_commit_action(relid, oncommit);
1352
1353         if (relpersistence == RELPERSISTENCE_UNLOGGED)
1354         {
1355                 Assert(relkind == RELKIND_RELATION || relkind == RELKIND_MATVIEW ||
1356                            relkind == RELKIND_TOASTVALUE);
1357                 heap_create_init_fork(new_rel_desc);
1358         }
1359
1360         /*
1361          * ok, the relation has been cataloged, so close our relations and return
1362          * the OID of the newly created relation.
1363          */
1364         heap_close(new_rel_desc, NoLock);       /* do not unlock till end of xact */
1365         heap_close(pg_class_desc, RowExclusiveLock);
1366
1367         return relid;
1368 }
1369
1370 /*
1371  * Set up an init fork for an unlogged table so that it can be correctly
1372  * reinitialized on restart.  Since we're going to do an immediate sync, we
1373  * only need to xlog this if archiving or streaming is enabled.  And the
1374  * immediate sync is required, because otherwise there's no guarantee that
1375  * this will hit the disk before the next checkpoint moves the redo pointer.
1376  */
1377 void
1378 heap_create_init_fork(Relation rel)
1379 {
1380         RelationOpenSmgr(rel);
1381         smgrcreate(rel->rd_smgr, INIT_FORKNUM, false);
1382         if (XLogIsNeeded())
1383                 log_smgrcreate(&rel->rd_smgr->smgr_rnode.node, INIT_FORKNUM);
1384         smgrimmedsync(rel->rd_smgr, INIT_FORKNUM);
1385 }
1386
1387 /*
1388  *              RelationRemoveInheritance
1389  *
1390  * Formerly, this routine checked for child relations and aborted the
1391  * deletion if any were found.  Now we rely on the dependency mechanism
1392  * to check for or delete child relations.  By the time we get here,
1393  * there are no children and we need only remove any pg_inherits rows
1394  * linking this relation to its parent(s).
1395  */
1396 static void
1397 RelationRemoveInheritance(Oid relid)
1398 {
1399         Relation        catalogRelation;
1400         SysScanDesc scan;
1401         ScanKeyData key;
1402         HeapTuple       tuple;
1403
1404         catalogRelation = heap_open(InheritsRelationId, RowExclusiveLock);
1405
1406         ScanKeyInit(&key,
1407                                 Anum_pg_inherits_inhrelid,
1408                                 BTEqualStrategyNumber, F_OIDEQ,
1409                                 ObjectIdGetDatum(relid));
1410
1411         scan = systable_beginscan(catalogRelation, InheritsRelidSeqnoIndexId, true,
1412                                                           NULL, 1, &key);
1413
1414         while (HeapTupleIsValid(tuple = systable_getnext(scan)))
1415                 simple_heap_delete(catalogRelation, &tuple->t_self);
1416
1417         systable_endscan(scan);
1418         heap_close(catalogRelation, RowExclusiveLock);
1419 }
1420
1421 /*
1422  *              DeleteRelationTuple
1423  *
1424  * Remove pg_class row for the given relid.
1425  *
1426  * Note: this is shared by relation deletion and index deletion.  It's
1427  * not intended for use anyplace else.
1428  */
1429 void
1430 DeleteRelationTuple(Oid relid)
1431 {
1432         Relation        pg_class_desc;
1433         HeapTuple       tup;
1434
1435         /* Grab an appropriate lock on the pg_class relation */
1436         pg_class_desc = heap_open(RelationRelationId, RowExclusiveLock);
1437
1438         tup = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
1439         if (!HeapTupleIsValid(tup))
1440                 elog(ERROR, "cache lookup failed for relation %u", relid);
1441
1442         /* delete the relation tuple from pg_class, and finish up */
1443         simple_heap_delete(pg_class_desc, &tup->t_self);
1444
1445         ReleaseSysCache(tup);
1446
1447         heap_close(pg_class_desc, RowExclusiveLock);
1448 }
1449
1450 /*
1451  *              DeleteAttributeTuples
1452  *
1453  * Remove pg_attribute rows for the given relid.
1454  *
1455  * Note: this is shared by relation deletion and index deletion.  It's
1456  * not intended for use anyplace else.
1457  */
1458 void
1459 DeleteAttributeTuples(Oid relid)
1460 {
1461         Relation        attrel;
1462         SysScanDesc scan;
1463         ScanKeyData key[1];
1464         HeapTuple       atttup;
1465
1466         /* Grab an appropriate lock on the pg_attribute relation */
1467         attrel = heap_open(AttributeRelationId, RowExclusiveLock);
1468
1469         /* Use the index to scan only attributes of the target relation */
1470         ScanKeyInit(&key[0],
1471                                 Anum_pg_attribute_attrelid,
1472                                 BTEqualStrategyNumber, F_OIDEQ,
1473                                 ObjectIdGetDatum(relid));
1474
1475         scan = systable_beginscan(attrel, AttributeRelidNumIndexId, true,
1476                                                           NULL, 1, key);
1477
1478         /* Delete all the matching tuples */
1479         while ((atttup = systable_getnext(scan)) != NULL)
1480                 simple_heap_delete(attrel, &atttup->t_self);
1481
1482         /* Clean up after the scan */
1483         systable_endscan(scan);
1484         heap_close(attrel, RowExclusiveLock);
1485 }
1486
1487 /*
1488  *              DeleteSystemAttributeTuples
1489  *
1490  * Remove pg_attribute rows for system columns of the given relid.
1491  *
1492  * Note: this is only used when converting a table to a view.  Views don't
1493  * have system columns, so we should remove them from pg_attribute.
1494  */
1495 void
1496 DeleteSystemAttributeTuples(Oid relid)
1497 {
1498         Relation        attrel;
1499         SysScanDesc scan;
1500         ScanKeyData key[2];
1501         HeapTuple       atttup;
1502
1503         /* Grab an appropriate lock on the pg_attribute relation */
1504         attrel = heap_open(AttributeRelationId, RowExclusiveLock);
1505
1506         /* Use the index to scan only system attributes of the target relation */
1507         ScanKeyInit(&key[0],
1508                                 Anum_pg_attribute_attrelid,
1509                                 BTEqualStrategyNumber, F_OIDEQ,
1510                                 ObjectIdGetDatum(relid));
1511         ScanKeyInit(&key[1],
1512                                 Anum_pg_attribute_attnum,
1513                                 BTLessEqualStrategyNumber, F_INT2LE,
1514                                 Int16GetDatum(0));
1515
1516         scan = systable_beginscan(attrel, AttributeRelidNumIndexId, true,
1517                                                           NULL, 2, key);
1518
1519         /* Delete all the matching tuples */
1520         while ((atttup = systable_getnext(scan)) != NULL)
1521                 simple_heap_delete(attrel, &atttup->t_self);
1522
1523         /* Clean up after the scan */
1524         systable_endscan(scan);
1525         heap_close(attrel, RowExclusiveLock);
1526 }
1527
1528 /*
1529  *              RemoveAttributeById
1530  *
1531  * This is the guts of ALTER TABLE DROP COLUMN: actually mark the attribute
1532  * deleted in pg_attribute.  We also remove pg_statistic entries for it.
1533  * (Everything else needed, such as getting rid of any pg_attrdef entry,
1534  * is handled by dependency.c.)
1535  */
1536 void
1537 RemoveAttributeById(Oid relid, AttrNumber attnum)
1538 {
1539         Relation        rel;
1540         Relation        attr_rel;
1541         HeapTuple       tuple;
1542         Form_pg_attribute attStruct;
1543         char            newattname[NAMEDATALEN];
1544
1545         /*
1546          * Grab an exclusive lock on the target table, which we will NOT release
1547          * until end of transaction.  (In the simple case where we are directly
1548          * dropping this column, AlterTableDropColumn already did this ... but
1549          * when cascading from a drop of some other object, we may not have any
1550          * lock.)
1551          */
1552         rel = relation_open(relid, AccessExclusiveLock);
1553
1554         attr_rel = heap_open(AttributeRelationId, RowExclusiveLock);
1555
1556         tuple = SearchSysCacheCopy2(ATTNUM,
1557                                                                 ObjectIdGetDatum(relid),
1558                                                                 Int16GetDatum(attnum));
1559         if (!HeapTupleIsValid(tuple))           /* shouldn't happen */
1560                 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
1561                          attnum, relid);
1562         attStruct = (Form_pg_attribute) GETSTRUCT(tuple);
1563
1564         if (attnum < 0)
1565         {
1566                 /* System attribute (probably OID) ... just delete the row */
1567
1568                 simple_heap_delete(attr_rel, &tuple->t_self);
1569         }
1570         else
1571         {
1572                 /* Dropping user attributes is lots harder */
1573
1574                 /* Mark the attribute as dropped */
1575                 attStruct->attisdropped = true;
1576
1577                 /*
1578                  * Set the type OID to invalid.  A dropped attribute's type link
1579                  * cannot be relied on (once the attribute is dropped, the type might
1580                  * be too). Fortunately we do not need the type row --- the only
1581                  * really essential information is the type's typlen and typalign,
1582                  * which are preserved in the attribute's attlen and attalign.  We set
1583                  * atttypid to zero here as a means of catching code that incorrectly
1584                  * expects it to be valid.
1585                  */
1586                 attStruct->atttypid = InvalidOid;
1587
1588                 /* Remove any NOT NULL constraint the column may have */
1589                 attStruct->attnotnull = false;
1590
1591                 /* We don't want to keep stats for it anymore */
1592                 attStruct->attstattarget = 0;
1593
1594                 /*
1595                  * Change the column name to something that isn't likely to conflict
1596                  */
1597                 snprintf(newattname, sizeof(newattname),
1598                                  "........pg.dropped.%d........", attnum);
1599                 namestrcpy(&(attStruct->attname), newattname);
1600
1601                 simple_heap_update(attr_rel, &tuple->t_self, tuple);
1602
1603                 /* keep the system catalog indexes current */
1604                 CatalogUpdateIndexes(attr_rel, tuple);
1605         }
1606
1607         /*
1608          * Because updating the pg_attribute row will trigger a relcache flush for
1609          * the target relation, we need not do anything else to notify other
1610          * backends of the change.
1611          */
1612
1613         heap_close(attr_rel, RowExclusiveLock);
1614
1615         if (attnum > 0)
1616                 RemoveStatistics(relid, attnum);
1617
1618         relation_close(rel, NoLock);
1619 }
1620
1621 /*
1622  *              RemoveAttrDefault
1623  *
1624  * If the specified relation/attribute has a default, remove it.
1625  * (If no default, raise error if complain is true, else return quietly.)
1626  */
1627 void
1628 RemoveAttrDefault(Oid relid, AttrNumber attnum,
1629                                   DropBehavior behavior, bool complain, bool internal)
1630 {
1631         Relation        attrdef_rel;
1632         ScanKeyData scankeys[2];
1633         SysScanDesc scan;
1634         HeapTuple       tuple;
1635         bool            found = false;
1636
1637         attrdef_rel = heap_open(AttrDefaultRelationId, RowExclusiveLock);
1638
1639         ScanKeyInit(&scankeys[0],
1640                                 Anum_pg_attrdef_adrelid,
1641                                 BTEqualStrategyNumber, F_OIDEQ,
1642                                 ObjectIdGetDatum(relid));
1643         ScanKeyInit(&scankeys[1],
1644                                 Anum_pg_attrdef_adnum,
1645                                 BTEqualStrategyNumber, F_INT2EQ,
1646                                 Int16GetDatum(attnum));
1647
1648         scan = systable_beginscan(attrdef_rel, AttrDefaultIndexId, true,
1649                                                           NULL, 2, scankeys);
1650
1651         /* There should be at most one matching tuple, but we loop anyway */
1652         while (HeapTupleIsValid(tuple = systable_getnext(scan)))
1653         {
1654                 ObjectAddress object;
1655
1656                 object.classId = AttrDefaultRelationId;
1657                 object.objectId = HeapTupleGetOid(tuple);
1658                 object.objectSubId = 0;
1659
1660                 performDeletion(&object, behavior,
1661                                                 internal ? PERFORM_DELETION_INTERNAL : 0);
1662
1663                 found = true;
1664         }
1665
1666         systable_endscan(scan);
1667         heap_close(attrdef_rel, RowExclusiveLock);
1668
1669         if (complain && !found)
1670                 elog(ERROR, "could not find attrdef tuple for relation %u attnum %d",
1671                          relid, attnum);
1672 }
1673
1674 /*
1675  *              RemoveAttrDefaultById
1676  *
1677  * Remove a pg_attrdef entry specified by OID.  This is the guts of
1678  * attribute-default removal.  Note it should be called via performDeletion,
1679  * not directly.
1680  */
1681 void
1682 RemoveAttrDefaultById(Oid attrdefId)
1683 {
1684         Relation        attrdef_rel;
1685         Relation        attr_rel;
1686         Relation        myrel;
1687         ScanKeyData scankeys[1];
1688         SysScanDesc scan;
1689         HeapTuple       tuple;
1690         Oid                     myrelid;
1691         AttrNumber      myattnum;
1692
1693         /* Grab an appropriate lock on the pg_attrdef relation */
1694         attrdef_rel = heap_open(AttrDefaultRelationId, RowExclusiveLock);
1695
1696         /* Find the pg_attrdef tuple */
1697         ScanKeyInit(&scankeys[0],
1698                                 ObjectIdAttributeNumber,
1699                                 BTEqualStrategyNumber, F_OIDEQ,
1700                                 ObjectIdGetDatum(attrdefId));
1701
1702         scan = systable_beginscan(attrdef_rel, AttrDefaultOidIndexId, true,
1703                                                           NULL, 1, scankeys);
1704
1705         tuple = systable_getnext(scan);
1706         if (!HeapTupleIsValid(tuple))
1707                 elog(ERROR, "could not find tuple for attrdef %u", attrdefId);
1708
1709         myrelid = ((Form_pg_attrdef) GETSTRUCT(tuple))->adrelid;
1710         myattnum = ((Form_pg_attrdef) GETSTRUCT(tuple))->adnum;
1711
1712         /* Get an exclusive lock on the relation owning the attribute */
1713         myrel = relation_open(myrelid, AccessExclusiveLock);
1714
1715         /* Now we can delete the pg_attrdef row */
1716         simple_heap_delete(attrdef_rel, &tuple->t_self);
1717
1718         systable_endscan(scan);
1719         heap_close(attrdef_rel, RowExclusiveLock);
1720
1721         /* Fix the pg_attribute row */
1722         attr_rel = heap_open(AttributeRelationId, RowExclusiveLock);
1723
1724         tuple = SearchSysCacheCopy2(ATTNUM,
1725                                                                 ObjectIdGetDatum(myrelid),
1726                                                                 Int16GetDatum(myattnum));
1727         if (!HeapTupleIsValid(tuple))           /* shouldn't happen */
1728                 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
1729                          myattnum, myrelid);
1730
1731         ((Form_pg_attribute) GETSTRUCT(tuple))->atthasdef = false;
1732
1733         simple_heap_update(attr_rel, &tuple->t_self, tuple);
1734
1735         /* keep the system catalog indexes current */
1736         CatalogUpdateIndexes(attr_rel, tuple);
1737
1738         /*
1739          * Our update of the pg_attribute row will force a relcache rebuild, so
1740          * there's nothing else to do here.
1741          */
1742         heap_close(attr_rel, RowExclusiveLock);
1743
1744         /* Keep lock on attribute's rel until end of xact */
1745         relation_close(myrel, NoLock);
1746 }
1747
1748 /*
1749  * heap_drop_with_catalog       - removes specified relation from catalogs
1750  *
1751  * Note that this routine is not responsible for dropping objects that are
1752  * linked to the pg_class entry via dependencies (for example, indexes and
1753  * constraints).  Those are deleted by the dependency-tracing logic in
1754  * dependency.c before control gets here.  In general, therefore, this routine
1755  * should never be called directly; go through performDeletion() instead.
1756  */
1757 void
1758 heap_drop_with_catalog(Oid relid)
1759 {
1760         Relation        rel;
1761
1762         /*
1763          * Open and lock the relation.
1764          */
1765         rel = relation_open(relid, AccessExclusiveLock);
1766
1767         /*
1768          * There can no longer be anyone *else* touching the relation, but we
1769          * might still have open queries or cursors, or pending trigger events, in
1770          * our own session.
1771          */
1772         CheckTableNotInUse(rel, "DROP TABLE");
1773
1774         /*
1775          * This effectively deletes all rows in the table, and may be done in a
1776          * serializable transaction.  In that case we must record a rw-conflict in
1777          * to this transaction from each transaction holding a predicate lock on
1778          * the table.
1779          */
1780         CheckTableForSerializableConflictIn(rel);
1781
1782         /*
1783          * Delete pg_foreign_table tuple first.
1784          */
1785         if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
1786         {
1787                 Relation        rel;
1788                 HeapTuple       tuple;
1789
1790                 rel = heap_open(ForeignTableRelationId, RowExclusiveLock);
1791
1792                 tuple = SearchSysCache1(FOREIGNTABLEREL, ObjectIdGetDatum(relid));
1793                 if (!HeapTupleIsValid(tuple))
1794                         elog(ERROR, "cache lookup failed for foreign table %u", relid);
1795
1796                 simple_heap_delete(rel, &tuple->t_self);
1797
1798                 ReleaseSysCache(tuple);
1799                 heap_close(rel, RowExclusiveLock);
1800         }
1801
1802         /*
1803          * Schedule unlinking of the relation's physical files at commit.
1804          */
1805         if (rel->rd_rel->relkind != RELKIND_VIEW &&
1806                 rel->rd_rel->relkind != RELKIND_COMPOSITE_TYPE &&
1807                 rel->rd_rel->relkind != RELKIND_FOREIGN_TABLE)
1808         {
1809                 RelationDropStorage(rel);
1810         }
1811
1812         /*
1813          * Close relcache entry, but *keep* AccessExclusiveLock on the relation
1814          * until transaction commit.  This ensures no one else will try to do
1815          * something with the doomed relation.
1816          */
1817         relation_close(rel, NoLock);
1818
1819         /*
1820          * Forget any ON COMMIT action for the rel
1821          */
1822         remove_on_commit_action(relid);
1823
1824         /*
1825          * Flush the relation from the relcache.  We want to do this before
1826          * starting to remove catalog entries, just to be certain that no relcache
1827          * entry rebuild will happen partway through.  (That should not really
1828          * matter, since we don't do CommandCounterIncrement here, but let's be
1829          * safe.)
1830          */
1831         RelationForgetRelation(relid);
1832
1833         /*
1834          * remove inheritance information
1835          */
1836         RelationRemoveInheritance(relid);
1837
1838         /*
1839          * delete statistics
1840          */
1841         RemoveStatistics(relid, 0);
1842
1843         /*
1844          * delete attribute tuples
1845          */
1846         DeleteAttributeTuples(relid);
1847
1848         /*
1849          * delete relation tuple
1850          */
1851         DeleteRelationTuple(relid);
1852 }
1853
1854
1855 /*
1856  * Store a default expression for column attnum of relation rel.
1857  *
1858  * Returns the OID of the new pg_attrdef tuple.
1859  */
1860 Oid
1861 StoreAttrDefault(Relation rel, AttrNumber attnum,
1862                                  Node *expr, bool is_internal)
1863 {
1864         char       *adbin;
1865         char       *adsrc;
1866         Relation        adrel;
1867         HeapTuple       tuple;
1868         Datum           values[4];
1869         static bool nulls[4] = {false, false, false, false};
1870         Relation        attrrel;
1871         HeapTuple       atttup;
1872         Form_pg_attribute attStruct;
1873         Oid                     attrdefOid;
1874         ObjectAddress colobject,
1875                                 defobject;
1876
1877         /*
1878          * Flatten expression to string form for storage.
1879          */
1880         adbin = nodeToString(expr);
1881
1882         /*
1883          * Also deparse it to form the mostly-obsolete adsrc field.
1884          */
1885         adsrc = deparse_expression(expr,
1886                                                         deparse_context_for(RelationGetRelationName(rel),
1887                                                                                                 RelationGetRelid(rel)),
1888                                                            false, false);
1889
1890         /*
1891          * Make the pg_attrdef entry.
1892          */
1893         values[Anum_pg_attrdef_adrelid - 1] = RelationGetRelid(rel);
1894         values[Anum_pg_attrdef_adnum - 1] = attnum;
1895         values[Anum_pg_attrdef_adbin - 1] = CStringGetTextDatum(adbin);
1896         values[Anum_pg_attrdef_adsrc - 1] = CStringGetTextDatum(adsrc);
1897
1898         adrel = heap_open(AttrDefaultRelationId, RowExclusiveLock);
1899
1900         tuple = heap_form_tuple(adrel->rd_att, values, nulls);
1901         attrdefOid = simple_heap_insert(adrel, tuple);
1902
1903         CatalogUpdateIndexes(adrel, tuple);
1904
1905         defobject.classId = AttrDefaultRelationId;
1906         defobject.objectId = attrdefOid;
1907         defobject.objectSubId = 0;
1908
1909         heap_close(adrel, RowExclusiveLock);
1910
1911         /* now can free some of the stuff allocated above */
1912         pfree(DatumGetPointer(values[Anum_pg_attrdef_adbin - 1]));
1913         pfree(DatumGetPointer(values[Anum_pg_attrdef_adsrc - 1]));
1914         heap_freetuple(tuple);
1915         pfree(adbin);
1916         pfree(adsrc);
1917
1918         /*
1919          * Update the pg_attribute entry for the column to show that a default
1920          * exists.
1921          */
1922         attrrel = heap_open(AttributeRelationId, RowExclusiveLock);
1923         atttup = SearchSysCacheCopy2(ATTNUM,
1924                                                                  ObjectIdGetDatum(RelationGetRelid(rel)),
1925                                                                  Int16GetDatum(attnum));
1926         if (!HeapTupleIsValid(atttup))
1927                 elog(ERROR, "cache lookup failed for attribute %d of relation %u",
1928                          attnum, RelationGetRelid(rel));
1929         attStruct = (Form_pg_attribute) GETSTRUCT(atttup);
1930         if (!attStruct->atthasdef)
1931         {
1932                 attStruct->atthasdef = true;
1933                 simple_heap_update(attrrel, &atttup->t_self, atttup);
1934                 /* keep catalog indexes current */
1935                 CatalogUpdateIndexes(attrrel, atttup);
1936         }
1937         heap_close(attrrel, RowExclusiveLock);
1938         heap_freetuple(atttup);
1939
1940         /*
1941          * Make a dependency so that the pg_attrdef entry goes away if the column
1942          * (or whole table) is deleted.
1943          */
1944         colobject.classId = RelationRelationId;
1945         colobject.objectId = RelationGetRelid(rel);
1946         colobject.objectSubId = attnum;
1947
1948         recordDependencyOn(&defobject, &colobject, DEPENDENCY_AUTO);
1949
1950         /*
1951          * Record dependencies on objects used in the expression, too.
1952          */
1953         recordDependencyOnExpr(&defobject, expr, NIL, DEPENDENCY_NORMAL);
1954
1955         /*
1956          * Post creation hook for attribute defaults.
1957          *
1958          * XXX. ALTER TABLE ALTER COLUMN SET/DROP DEFAULT is implemented with a
1959          * couple of deletion/creation of the attribute's default entry, so the
1960          * callee should check existence of an older version of this entry if it
1961          * needs to distinguish.
1962          */
1963         InvokeObjectPostCreateHookArg(AttrDefaultRelationId,
1964                                                                   RelationGetRelid(rel), attnum, is_internal);
1965
1966         return attrdefOid;
1967 }
1968
1969 /*
1970  * Store a check-constraint expression for the given relation.
1971  *
1972  * Caller is responsible for updating the count of constraints
1973  * in the pg_class entry for the relation.
1974  *
1975  * The OID of the new constraint is returned.
1976  */
1977 static Oid
1978 StoreRelCheck(Relation rel, char *ccname, Node *expr,
1979                           bool is_validated, bool is_local, int inhcount,
1980                           bool is_no_inherit, bool is_internal)
1981 {
1982         char       *ccbin;
1983         char       *ccsrc;
1984         List       *varList;
1985         int                     keycount;
1986         int16      *attNos;
1987         Oid                     constrOid;
1988
1989         /*
1990          * Flatten expression to string form for storage.
1991          */
1992         ccbin = nodeToString(expr);
1993
1994         /*
1995          * Also deparse it to form the mostly-obsolete consrc field.
1996          */
1997         ccsrc = deparse_expression(expr,
1998                                                         deparse_context_for(RelationGetRelationName(rel),
1999                                                                                                 RelationGetRelid(rel)),
2000                                                            false, false);
2001
2002         /*
2003          * Find columns of rel that are used in expr
2004          *
2005          * NB: pull_var_clause is okay here only because we don't allow subselects
2006          * in check constraints; it would fail to examine the contents of
2007          * subselects.
2008          */
2009         varList = pull_var_clause(expr,
2010                                                           PVC_REJECT_AGGREGATES,
2011                                                           PVC_REJECT_PLACEHOLDERS);
2012         keycount = list_length(varList);
2013
2014         if (keycount > 0)
2015         {
2016                 ListCell   *vl;
2017                 int                     i = 0;
2018
2019                 attNos = (int16 *) palloc(keycount * sizeof(int16));
2020                 foreach(vl, varList)
2021                 {
2022                         Var                *var = (Var *) lfirst(vl);
2023                         int                     j;
2024
2025                         for (j = 0; j < i; j++)
2026                                 if (attNos[j] == var->varattno)
2027                                         break;
2028                         if (j == i)
2029                                 attNos[i++] = var->varattno;
2030                 }
2031                 keycount = i;
2032         }
2033         else
2034                 attNos = NULL;
2035
2036         /*
2037          * Create the Check Constraint
2038          */
2039         constrOid =
2040                 CreateConstraintEntry(ccname,   /* Constraint Name */
2041                                                           RelationGetNamespace(rel),            /* namespace */
2042                                                           CONSTRAINT_CHECK, /* Constraint Type */
2043                                                           false,        /* Is Deferrable */
2044                                                           false,        /* Is Deferred */
2045                                                           is_validated,
2046                                                           RelationGetRelid(rel),        /* relation */
2047                                                           attNos,       /* attrs in the constraint */
2048                                                           keycount, /* # attrs in the constraint */
2049                                                           InvalidOid,           /* not a domain constraint */
2050                                                           InvalidOid,           /* no associated index */
2051                                                           InvalidOid,           /* Foreign key fields */
2052                                                           NULL,
2053                                                           NULL,
2054                                                           NULL,
2055                                                           NULL,
2056                                                           0,
2057                                                           ' ',
2058                                                           ' ',
2059                                                           ' ',
2060                                                           NULL,         /* not an exclusion constraint */
2061                                                           expr,         /* Tree form of check constraint */
2062                                                           ccbin,        /* Binary form of check constraint */
2063                                                           ccsrc,        /* Source form of check constraint */
2064                                                           is_local, /* conislocal */
2065                                                           inhcount, /* coninhcount */
2066                                                           is_no_inherit,        /* connoinherit */
2067                                                           is_internal);         /* internally constructed? */
2068
2069         pfree(ccbin);
2070         pfree(ccsrc);
2071
2072         return constrOid;
2073 }
2074
2075 /*
2076  * Store defaults and constraints (passed as a list of CookedConstraint).
2077  *
2078  * Each CookedConstraint struct is modified to store the new catalog tuple OID.
2079  *
2080  * NOTE: only pre-cooked expressions will be passed this way, which is to
2081  * say expressions inherited from an existing relation.  Newly parsed
2082  * expressions can be added later, by direct calls to StoreAttrDefault
2083  * and StoreRelCheck (see AddRelationNewConstraints()).
2084  */
2085 static void
2086 StoreConstraints(Relation rel, List *cooked_constraints, bool is_internal)
2087 {
2088         int                     numchecks = 0;
2089         ListCell   *lc;
2090
2091         if (cooked_constraints == NIL)
2092                 return;                                 /* nothing to do */
2093
2094         /*
2095          * Deparsing of constraint expressions will fail unless the just-created
2096          * pg_attribute tuples for this relation are made visible.  So, bump the
2097          * command counter.  CAUTION: this will cause a relcache entry rebuild.
2098          */
2099         CommandCounterIncrement();
2100
2101         foreach(lc, cooked_constraints)
2102         {
2103                 CookedConstraint *con = (CookedConstraint *) lfirst(lc);
2104
2105                 switch (con->contype)
2106                 {
2107                         case CONSTR_DEFAULT:
2108                                 con->conoid = StoreAttrDefault(rel, con->attnum, con->expr,
2109                                                                                            is_internal);
2110                                 break;
2111                         case CONSTR_CHECK:
2112                                 con->conoid =
2113                                         StoreRelCheck(rel, con->name, con->expr,
2114                                                                   !con->skip_validation, con->is_local,
2115                                                                   con->inhcount, con->is_no_inherit,
2116                                                                   is_internal);
2117                                 numchecks++;
2118                                 break;
2119                         default:
2120                                 elog(ERROR, "unrecognized constraint type: %d",
2121                                          (int) con->contype);
2122                 }
2123         }
2124
2125         if (numchecks > 0)
2126                 SetRelationNumChecks(rel, numchecks);
2127 }
2128
2129 /*
2130  * AddRelationNewConstraints
2131  *
2132  * Add new column default expressions and/or constraint check expressions
2133  * to an existing relation.  This is defined to do both for efficiency in
2134  * DefineRelation, but of course you can do just one or the other by passing
2135  * empty lists.
2136  *
2137  * rel: relation to be modified
2138  * newColDefaults: list of RawColumnDefault structures
2139  * newConstraints: list of Constraint nodes
2140  * allow_merge: TRUE if check constraints may be merged with existing ones
2141  * is_local: TRUE if definition is local, FALSE if it's inherited
2142  * is_internal: TRUE if result of some internal process, not a user request
2143  *
2144  * All entries in newColDefaults will be processed.  Entries in newConstraints
2145  * will be processed only if they are CONSTR_CHECK type.
2146  *
2147  * Returns a list of CookedConstraint nodes that shows the cooked form of
2148  * the default and constraint expressions added to the relation.
2149  *
2150  * NB: caller should have opened rel with AccessExclusiveLock, and should
2151  * hold that lock till end of transaction.  Also, we assume the caller has
2152  * done a CommandCounterIncrement if necessary to make the relation's catalog
2153  * tuples visible.
2154  */
2155 List *
2156 AddRelationNewConstraints(Relation rel,
2157                                                   List *newColDefaults,
2158                                                   List *newConstraints,
2159                                                   bool allow_merge,
2160                                                   bool is_local,
2161                                                   bool is_internal)
2162 {
2163         List       *cookedConstraints = NIL;
2164         TupleDesc       tupleDesc;
2165         TupleConstr *oldconstr;
2166         int                     numoldchecks;
2167         ParseState *pstate;
2168         RangeTblEntry *rte;
2169         int                     numchecks;
2170         List       *checknames;
2171         ListCell   *cell;
2172         Node       *expr;
2173         CookedConstraint *cooked;
2174
2175         /*
2176          * Get info about existing constraints.
2177          */
2178         tupleDesc = RelationGetDescr(rel);
2179         oldconstr = tupleDesc->constr;
2180         if (oldconstr)
2181                 numoldchecks = oldconstr->num_check;
2182         else
2183                 numoldchecks = 0;
2184
2185         /*
2186          * Create a dummy ParseState and insert the target relation as its sole
2187          * rangetable entry.  We need a ParseState for transformExpr.
2188          */
2189         pstate = make_parsestate(NULL);
2190         rte = addRangeTableEntryForRelation(pstate,
2191                                                                                 rel,
2192                                                                                 NULL,
2193                                                                                 false,
2194                                                                                 true);
2195         addRTEtoQuery(pstate, rte, true, true, true);
2196
2197         /*
2198          * Process column default expressions.
2199          */
2200         foreach(cell, newColDefaults)
2201         {
2202                 RawColumnDefault *colDef = (RawColumnDefault *) lfirst(cell);
2203                 Form_pg_attribute atp = rel->rd_att->attrs[colDef->attnum - 1];
2204                 Oid                     defOid;
2205
2206                 expr = cookDefault(pstate, colDef->raw_default,
2207                                                    atp->atttypid, atp->atttypmod,
2208                                                    NameStr(atp->attname));
2209
2210                 /*
2211                  * If the expression is just a NULL constant, we do not bother to make
2212                  * an explicit pg_attrdef entry, since the default behavior is
2213                  * equivalent.
2214                  *
2215                  * Note a nonobvious property of this test: if the column is of a
2216                  * domain type, what we'll get is not a bare null Const but a
2217                  * CoerceToDomain expr, so we will not discard the default.  This is
2218                  * critical because the column default needs to be retained to
2219                  * override any default that the domain might have.
2220                  */
2221                 if (expr == NULL ||
2222                         (IsA(expr, Const) &&((Const *) expr)->constisnull))
2223                         continue;
2224
2225                 defOid = StoreAttrDefault(rel, colDef->attnum, expr, is_internal);
2226
2227                 cooked = (CookedConstraint *) palloc(sizeof(CookedConstraint));
2228                 cooked->contype = CONSTR_DEFAULT;
2229                 cooked->conoid = defOid;
2230                 cooked->name = NULL;
2231                 cooked->attnum = colDef->attnum;
2232                 cooked->expr = expr;
2233                 cooked->skip_validation = false;
2234                 cooked->is_local = is_local;
2235                 cooked->inhcount = is_local ? 0 : 1;
2236                 cooked->is_no_inherit = false;
2237                 cookedConstraints = lappend(cookedConstraints, cooked);
2238         }
2239
2240         /*
2241          * Process constraint expressions.
2242          */
2243         numchecks = numoldchecks;
2244         checknames = NIL;
2245         foreach(cell, newConstraints)
2246         {
2247                 Constraint *cdef = (Constraint *) lfirst(cell);
2248                 char       *ccname;
2249                 Oid                     constrOid;
2250
2251                 if (cdef->contype != CONSTR_CHECK)
2252                         continue;
2253
2254                 if (cdef->raw_expr != NULL)
2255                 {
2256                         Assert(cdef->cooked_expr == NULL);
2257
2258                         /*
2259                          * Transform raw parsetree to executable expression, and verify
2260                          * it's valid as a CHECK constraint.
2261                          */
2262                         expr = cookConstraint(pstate, cdef->raw_expr,
2263                                                                   RelationGetRelationName(rel));
2264                 }
2265                 else
2266                 {
2267                         Assert(cdef->cooked_expr != NULL);
2268
2269                         /*
2270                          * Here, we assume the parser will only pass us valid CHECK
2271                          * expressions, so we do no particular checking.
2272                          */
2273                         expr = stringToNode(cdef->cooked_expr);
2274                 }
2275
2276                 /*
2277                  * Check name uniqueness, or generate a name if none was given.
2278                  */
2279                 if (cdef->conname != NULL)
2280                 {
2281                         ListCell   *cell2;
2282
2283                         ccname = cdef->conname;
2284                         /* Check against other new constraints */
2285                         /* Needed because we don't do CommandCounterIncrement in loop */
2286                         foreach(cell2, checknames)
2287                         {
2288                                 if (strcmp((char *) lfirst(cell2), ccname) == 0)
2289                                         ereport(ERROR,
2290                                                         (errcode(ERRCODE_DUPLICATE_OBJECT),
2291                                                          errmsg("check constraint \"%s\" already exists",
2292                                                                         ccname)));
2293                         }
2294
2295                         /* save name for future checks */
2296                         checknames = lappend(checknames, ccname);
2297
2298                         /*
2299                          * Check against pre-existing constraints.  If we are allowed to
2300                          * merge with an existing constraint, there's no more to do here.
2301                          * (We omit the duplicate constraint from the result, which is
2302                          * what ATAddCheckConstraint wants.)
2303                          */
2304                         if (MergeWithExistingConstraint(rel, ccname, expr,
2305                                                                                         allow_merge, is_local,
2306                                                                                         cdef->is_no_inherit))
2307                                 continue;
2308                 }
2309                 else
2310                 {
2311                         /*
2312                          * When generating a name, we want to create "tab_col_check" for a
2313                          * column constraint and "tab_check" for a table constraint.  We
2314                          * no longer have any info about the syntactic positioning of the
2315                          * constraint phrase, so we approximate this by seeing whether the
2316                          * expression references more than one column.  (If the user
2317                          * played by the rules, the result is the same...)
2318                          *
2319                          * Note: pull_var_clause() doesn't descend into sublinks, but we
2320                          * eliminated those above; and anyway this only needs to be an
2321                          * approximate answer.
2322                          */
2323                         List       *vars;
2324                         char       *colname;
2325
2326                         vars = pull_var_clause(expr,
2327                                                                    PVC_REJECT_AGGREGATES,
2328                                                                    PVC_REJECT_PLACEHOLDERS);
2329
2330                         /* eliminate duplicates */
2331                         vars = list_union(NIL, vars);
2332
2333                         if (list_length(vars) == 1)
2334                                 colname = get_attname(RelationGetRelid(rel),
2335                                                                           ((Var *) linitial(vars))->varattno);
2336                         else
2337                                 colname = NULL;
2338
2339                         ccname = ChooseConstraintName(RelationGetRelationName(rel),
2340                                                                                   colname,
2341                                                                                   "check",
2342                                                                                   RelationGetNamespace(rel),
2343                                                                                   checknames);
2344
2345                         /* save name for future checks */
2346                         checknames = lappend(checknames, ccname);
2347                 }
2348
2349                 /*
2350                  * OK, store it.
2351                  */
2352                 constrOid =
2353                         StoreRelCheck(rel, ccname, expr, cdef->initially_valid, is_local,
2354                                                   is_local ? 0 : 1, cdef->is_no_inherit, is_internal);
2355
2356                 numchecks++;
2357
2358                 cooked = (CookedConstraint *) palloc(sizeof(CookedConstraint));
2359                 cooked->contype = CONSTR_CHECK;
2360                 cooked->conoid = constrOid;
2361                 cooked->name = ccname;
2362                 cooked->attnum = 0;
2363                 cooked->expr = expr;
2364                 cooked->skip_validation = cdef->skip_validation;
2365                 cooked->is_local = is_local;
2366                 cooked->inhcount = is_local ? 0 : 1;
2367                 cooked->is_no_inherit = cdef->is_no_inherit;
2368                 cookedConstraints = lappend(cookedConstraints, cooked);
2369         }
2370
2371         /*
2372          * Update the count of constraints in the relation's pg_class tuple. We do
2373          * this even if there was no change, in order to ensure that an SI update
2374          * message is sent out for the pg_class tuple, which will force other
2375          * backends to rebuild their relcache entries for the rel. (This is
2376          * critical if we added defaults but not constraints.)
2377          */
2378         SetRelationNumChecks(rel, numchecks);
2379
2380         return cookedConstraints;
2381 }
2382
2383 /*
2384  * Check for a pre-existing check constraint that conflicts with a proposed
2385  * new one, and either adjust its conislocal/coninhcount settings or throw
2386  * error as needed.
2387  *
2388  * Returns TRUE if merged (constraint is a duplicate), or FALSE if it's
2389  * got a so-far-unique name, or throws error if conflict.
2390  *
2391  * XXX See MergeConstraintsIntoExisting too if you change this code.
2392  */
2393 static bool
2394 MergeWithExistingConstraint(Relation rel, char *ccname, Node *expr,
2395                                                         bool allow_merge, bool is_local,
2396                                                         bool is_no_inherit)
2397 {
2398         bool            found;
2399         Relation        conDesc;
2400         SysScanDesc conscan;
2401         ScanKeyData skey[2];
2402         HeapTuple       tup;
2403
2404         /* Search for a pg_constraint entry with same name and relation */
2405         conDesc = heap_open(ConstraintRelationId, RowExclusiveLock);
2406
2407         found = false;
2408
2409         ScanKeyInit(&skey[0],
2410                                 Anum_pg_constraint_conname,
2411                                 BTEqualStrategyNumber, F_NAMEEQ,
2412                                 CStringGetDatum(ccname));
2413
2414         ScanKeyInit(&skey[1],
2415                                 Anum_pg_constraint_connamespace,
2416                                 BTEqualStrategyNumber, F_OIDEQ,
2417                                 ObjectIdGetDatum(RelationGetNamespace(rel)));
2418
2419         conscan = systable_beginscan(conDesc, ConstraintNameNspIndexId, true,
2420                                                                  NULL, 2, skey);
2421
2422         while (HeapTupleIsValid(tup = systable_getnext(conscan)))
2423         {
2424                 Form_pg_constraint con = (Form_pg_constraint) GETSTRUCT(tup);
2425
2426                 if (con->conrelid == RelationGetRelid(rel))
2427                 {
2428                         /* Found it.  Conflicts if not identical check constraint */
2429                         if (con->contype == CONSTRAINT_CHECK)
2430                         {
2431                                 Datum           val;
2432                                 bool            isnull;
2433
2434                                 val = fastgetattr(tup,
2435                                                                   Anum_pg_constraint_conbin,
2436                                                                   conDesc->rd_att, &isnull);
2437                                 if (isnull)
2438                                         elog(ERROR, "null conbin for rel %s",
2439                                                  RelationGetRelationName(rel));
2440                                 if (equal(expr, stringToNode(TextDatumGetCString(val))))
2441                                         found = true;
2442                         }
2443                         if (!found || !allow_merge)
2444                                 ereport(ERROR,
2445                                                 (errcode(ERRCODE_DUPLICATE_OBJECT),
2446                                 errmsg("constraint \"%s\" for relation \"%s\" already exists",
2447                                            ccname, RelationGetRelationName(rel))));
2448
2449                         tup = heap_copytuple(tup);
2450                         con = (Form_pg_constraint) GETSTRUCT(tup);
2451
2452                         /* If the constraint is "no inherit" then cannot merge */
2453                         if (con->connoinherit)
2454                                 ereport(ERROR,
2455                                                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2456                                                  errmsg("constraint \"%s\" conflicts with non-inherited constraint on relation \"%s\"",
2457                                                                 ccname, RelationGetRelationName(rel))));
2458
2459                         if (is_local)
2460                                 con->conislocal = true;
2461                         else
2462                                 con->coninhcount++;
2463                         if (is_no_inherit)
2464                         {
2465                                 Assert(is_local);
2466                                 con->connoinherit = true;
2467                         }
2468                         /* OK to update the tuple */
2469                         ereport(NOTICE,
2470                            (errmsg("merging constraint \"%s\" with inherited definition",
2471                                            ccname)));
2472                         simple_heap_update(conDesc, &tup->t_self, tup);
2473                         CatalogUpdateIndexes(conDesc, tup);
2474                         break;
2475                 }
2476         }
2477
2478         systable_endscan(conscan);
2479         heap_close(conDesc, RowExclusiveLock);
2480
2481         return found;
2482 }
2483
2484 /*
2485  * Update the count of constraints in the relation's pg_class tuple.
2486  *
2487  * Caller had better hold exclusive lock on the relation.
2488  *
2489  * An important side effect is that a SI update message will be sent out for
2490  * the pg_class tuple, which will force other backends to rebuild their
2491  * relcache entries for the rel.  Also, this backend will rebuild its
2492  * own relcache entry at the next CommandCounterIncrement.
2493  */
2494 static void
2495 SetRelationNumChecks(Relation rel, int numchecks)
2496 {
2497         Relation        relrel;
2498         HeapTuple       reltup;
2499         Form_pg_class relStruct;
2500
2501         relrel = heap_open(RelationRelationId, RowExclusiveLock);
2502         reltup = SearchSysCacheCopy1(RELOID,
2503                                                                  ObjectIdGetDatum(RelationGetRelid(rel)));
2504         if (!HeapTupleIsValid(reltup))
2505                 elog(ERROR, "cache lookup failed for relation %u",
2506                          RelationGetRelid(rel));
2507         relStruct = (Form_pg_class) GETSTRUCT(reltup);
2508
2509         if (relStruct->relchecks != numchecks)
2510         {
2511                 relStruct->relchecks = numchecks;
2512
2513                 simple_heap_update(relrel, &reltup->t_self, reltup);
2514
2515                 /* keep catalog indexes current */
2516                 CatalogUpdateIndexes(relrel, reltup);
2517         }
2518         else
2519         {
2520                 /* Skip the disk update, but force relcache inval anyway */
2521                 CacheInvalidateRelcache(rel);
2522         }
2523
2524         heap_freetuple(reltup);
2525         heap_close(relrel, RowExclusiveLock);
2526 }
2527
2528 /*
2529  * Take a raw default and convert it to a cooked format ready for
2530  * storage.
2531  *
2532  * Parse state should be set up to recognize any vars that might appear
2533  * in the expression.  (Even though we plan to reject vars, it's more
2534  * user-friendly to give the correct error message than "unknown var".)
2535  *
2536  * If atttypid is not InvalidOid, coerce the expression to the specified
2537  * type (and typmod atttypmod).   attname is only needed in this case:
2538  * it is used in the error message, if any.
2539  */
2540 Node *
2541 cookDefault(ParseState *pstate,
2542                         Node *raw_default,
2543                         Oid atttypid,
2544                         int32 atttypmod,
2545                         char *attname)
2546 {
2547         Node       *expr;
2548
2549         Assert(raw_default != NULL);
2550
2551         /*
2552          * Transform raw parsetree to executable expression.
2553          */
2554         expr = transformExpr(pstate, raw_default, EXPR_KIND_COLUMN_DEFAULT);
2555
2556         /*
2557          * Make sure default expr does not refer to any vars (we need this check
2558          * since the pstate includes the target table).
2559          */
2560         if (contain_var_clause(expr))
2561                 ereport(ERROR,
2562                                 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2563                           errmsg("cannot use column references in default expression")));
2564
2565         /*
2566          * transformExpr() should have already rejected subqueries, aggregates,
2567          * and window functions, based on the EXPR_KIND_ for a default expression.
2568          *
2569          * It can't return a set either.
2570          */
2571         if (expression_returns_set(expr))
2572                 ereport(ERROR,
2573                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
2574                                  errmsg("default expression must not return a set")));
2575
2576         /*
2577          * Coerce the expression to the correct type and typmod, if given. This
2578          * should match the parser's processing of non-defaulted expressions ---
2579          * see transformAssignedExpr().
2580          */
2581         if (OidIsValid(atttypid))
2582         {
2583                 Oid                     type_id = exprType(expr);
2584
2585                 expr = coerce_to_target_type(pstate, expr, type_id,
2586                                                                          atttypid, atttypmod,
2587                                                                          COERCION_ASSIGNMENT,
2588                                                                          COERCE_IMPLICIT_CAST,
2589                                                                          -1);
2590                 if (expr == NULL)
2591                         ereport(ERROR,
2592                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
2593                                          errmsg("column \"%s\" is of type %s"
2594                                                         " but default expression is of type %s",
2595                                                         attname,
2596                                                         format_type_be(atttypid),
2597                                                         format_type_be(type_id)),
2598                            errhint("You will need to rewrite or cast the expression.")));
2599         }
2600
2601         /*
2602          * Finally, take care of collations in the finished expression.
2603          */
2604         assign_expr_collations(pstate, expr);
2605
2606         return expr;
2607 }
2608
2609 /*
2610  * Take a raw CHECK constraint expression and convert it to a cooked format
2611  * ready for storage.
2612  *
2613  * Parse state must be set up to recognize any vars that might appear
2614  * in the expression.
2615  */
2616 static Node *
2617 cookConstraint(ParseState *pstate,
2618                            Node *raw_constraint,
2619                            char *relname)
2620 {
2621         Node       *expr;
2622
2623         /*
2624          * Transform raw parsetree to executable expression.
2625          */
2626         expr = transformExpr(pstate, raw_constraint, EXPR_KIND_CHECK_CONSTRAINT);
2627
2628         /*
2629          * Make sure it yields a boolean result.
2630          */
2631         expr = coerce_to_boolean(pstate, expr, "CHECK");
2632
2633         /*
2634          * Take care of collations.
2635          */
2636         assign_expr_collations(pstate, expr);
2637
2638         /*
2639          * Make sure no outside relations are referred to (this is probably dead
2640          * code now that add_missing_from is history).
2641          */
2642         if (list_length(pstate->p_rtable) != 1)
2643                 ereport(ERROR,
2644                                 (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2645                         errmsg("only table \"%s\" can be referenced in check constraint",
2646                                    relname)));
2647
2648         return expr;
2649 }
2650
2651
2652 /*
2653  * RemoveStatistics --- remove entries in pg_statistic for a rel or column
2654  *
2655  * If attnum is zero, remove all entries for rel; else remove only the one(s)
2656  * for that column.
2657  */
2658 void
2659 RemoveStatistics(Oid relid, AttrNumber attnum)
2660 {
2661         Relation        pgstatistic;
2662         SysScanDesc scan;
2663         ScanKeyData key[2];
2664         int                     nkeys;
2665         HeapTuple       tuple;
2666
2667         pgstatistic = heap_open(StatisticRelationId, RowExclusiveLock);
2668
2669         ScanKeyInit(&key[0],
2670                                 Anum_pg_statistic_starelid,
2671                                 BTEqualStrategyNumber, F_OIDEQ,
2672                                 ObjectIdGetDatum(relid));
2673
2674         if (attnum == 0)
2675                 nkeys = 1;
2676         else
2677         {
2678                 ScanKeyInit(&key[1],
2679                                         Anum_pg_statistic_staattnum,
2680                                         BTEqualStrategyNumber, F_INT2EQ,
2681                                         Int16GetDatum(attnum));
2682                 nkeys = 2;
2683         }
2684
2685         scan = systable_beginscan(pgstatistic, StatisticRelidAttnumInhIndexId, true,
2686                                                           NULL, nkeys, key);
2687
2688         /* we must loop even when attnum != 0, in case of inherited stats */
2689         while (HeapTupleIsValid(tuple = systable_getnext(scan)))
2690                 simple_heap_delete(pgstatistic, &tuple->t_self);
2691
2692         systable_endscan(scan);
2693
2694         heap_close(pgstatistic, RowExclusiveLock);
2695 }
2696
2697
2698 /*
2699  * RelationTruncateIndexes - truncate all indexes associated
2700  * with the heap relation to zero tuples.
2701  *
2702  * The routine will truncate and then reconstruct the indexes on
2703  * the specified relation.  Caller must hold exclusive lock on rel.
2704  */
2705 static void
2706 RelationTruncateIndexes(Relation heapRelation)
2707 {
2708         ListCell   *indlist;
2709
2710         /* Ask the relcache to produce a list of the indexes of the rel */
2711         foreach(indlist, RelationGetIndexList(heapRelation))
2712         {
2713                 Oid                     indexId = lfirst_oid(indlist);
2714                 Relation        currentIndex;
2715                 IndexInfo  *indexInfo;
2716
2717                 /* Open the index relation; use exclusive lock, just to be sure */
2718                 currentIndex = index_open(indexId, AccessExclusiveLock);
2719
2720                 /* Fetch info needed for index_build */
2721                 indexInfo = BuildIndexInfo(currentIndex);
2722
2723                 /*
2724                  * Now truncate the actual file (and discard buffers).
2725                  */
2726                 RelationTruncate(currentIndex, 0);
2727
2728                 /* Initialize the index and rebuild */
2729                 /* Note: we do not need to re-establish pkey setting */
2730                 index_build(heapRelation, currentIndex, indexInfo, false, true);
2731
2732                 /* We're done with this index */
2733                 index_close(currentIndex, NoLock);
2734         }
2735 }
2736
2737 /*
2738  *       heap_truncate
2739  *
2740  *       This routine deletes all data within all the specified relations.
2741  *
2742  * This is not transaction-safe!  There is another, transaction-safe
2743  * implementation in commands/tablecmds.c.  We now use this only for
2744  * ON COMMIT truncation of temporary tables, where it doesn't matter.
2745  */
2746 void
2747 heap_truncate(List *relids)
2748 {
2749         List       *relations = NIL;
2750         ListCell   *cell;
2751
2752         /* Open relations for processing, and grab exclusive access on each */
2753         foreach(cell, relids)
2754         {
2755                 Oid                     rid = lfirst_oid(cell);
2756                 Relation        rel;
2757
2758                 rel = heap_open(rid, AccessExclusiveLock);
2759                 relations = lappend(relations, rel);
2760         }
2761
2762         /* Don't allow truncate on tables that are referenced by foreign keys */
2763         heap_truncate_check_FKs(relations, true);
2764
2765         /* OK to do it */
2766         foreach(cell, relations)
2767         {
2768                 Relation        rel = lfirst(cell);
2769
2770                 /* Truncate the relation */
2771                 heap_truncate_one_rel(rel);
2772
2773                 /* Close the relation, but keep exclusive lock on it until commit */
2774                 heap_close(rel, NoLock);
2775         }
2776 }
2777
2778 /*
2779  *       heap_truncate_one_rel
2780  *
2781  *       This routine deletes all data within the specified relation.
2782  *
2783  * This is not transaction-safe, because the truncation is done immediately
2784  * and cannot be rolled back later.  Caller is responsible for having
2785  * checked permissions etc, and must have obtained AccessExclusiveLock.
2786  */
2787 void
2788 heap_truncate_one_rel(Relation rel)
2789 {
2790         Oid                     toastrelid;
2791
2792         /* Truncate the actual file (and discard buffers) */
2793         RelationTruncate(rel, 0);
2794
2795         /* If the relation has indexes, truncate the indexes too */
2796         RelationTruncateIndexes(rel);
2797
2798         /* If there is a toast table, truncate that too */
2799         toastrelid = rel->rd_rel->reltoastrelid;
2800         if (OidIsValid(toastrelid))
2801         {
2802                 Relation        toastrel = heap_open(toastrelid, AccessExclusiveLock);
2803
2804                 RelationTruncate(toastrel, 0);
2805                 RelationTruncateIndexes(toastrel);
2806                 /* keep the lock... */
2807                 heap_close(toastrel, NoLock);
2808         }
2809 }
2810
2811 /*
2812  * heap_truncate_check_FKs
2813  *              Check for foreign keys referencing a list of relations that
2814  *              are to be truncated, and raise error if there are any
2815  *
2816  * We disallow such FKs (except self-referential ones) since the whole point
2817  * of TRUNCATE is to not scan the individual rows to be thrown away.
2818  *
2819  * This is split out so it can be shared by both implementations of truncate.
2820  * Caller should already hold a suitable lock on the relations.
2821  *
2822  * tempTables is only used to select an appropriate error message.
2823  */
2824 void
2825 heap_truncate_check_FKs(List *relations, bool tempTables)
2826 {
2827         List       *oids = NIL;
2828         List       *dependents;
2829         ListCell   *cell;
2830
2831         /*
2832          * Build a list of OIDs of the interesting relations.
2833          *
2834          * If a relation has no triggers, then it can neither have FKs nor be
2835          * referenced by a FK from another table, so we can ignore it.
2836          */
2837         foreach(cell, relations)
2838         {
2839                 Relation        rel = lfirst(cell);
2840
2841                 if (rel->rd_rel->relhastriggers)
2842                         oids = lappend_oid(oids, RelationGetRelid(rel));
2843         }
2844
2845         /*
2846          * Fast path: if no relation has triggers, none has FKs either.
2847          */
2848         if (oids == NIL)
2849                 return;
2850
2851         /*
2852          * Otherwise, must scan pg_constraint.  We make one pass with all the
2853          * relations considered; if this finds nothing, then all is well.
2854          */
2855         dependents = heap_truncate_find_FKs(oids);
2856         if (dependents == NIL)
2857                 return;
2858
2859         /*
2860          * Otherwise we repeat the scan once per relation to identify a particular
2861          * pair of relations to complain about.  This is pretty slow, but
2862          * performance shouldn't matter much in a failure path.  The reason for
2863          * doing things this way is to ensure that the message produced is not
2864          * dependent on chance row locations within pg_constraint.
2865          */
2866         foreach(cell, oids)
2867         {
2868                 Oid                     relid = lfirst_oid(cell);
2869                 ListCell   *cell2;
2870
2871                 dependents = heap_truncate_find_FKs(list_make1_oid(relid));
2872
2873                 foreach(cell2, dependents)
2874                 {
2875                         Oid                     relid2 = lfirst_oid(cell2);
2876
2877                         if (!list_member_oid(oids, relid2))
2878                         {
2879                                 char       *relname = get_rel_name(relid);
2880                                 char       *relname2 = get_rel_name(relid2);
2881
2882                                 if (tempTables)
2883                                         ereport(ERROR,
2884                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2885                                                          errmsg("unsupported ON COMMIT and foreign key combination"),
2886                                                          errdetail("Table \"%s\" references \"%s\", but they do not have the same ON COMMIT setting.",
2887                                                                            relname2, relname)));
2888                                 else
2889                                         ereport(ERROR,
2890                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2891                                                          errmsg("cannot truncate a table referenced in a foreign key constraint"),
2892                                                          errdetail("Table \"%s\" references \"%s\".",
2893                                                                            relname2, relname),
2894                                                    errhint("Truncate table \"%s\" at the same time, "
2895                                                                    "or use TRUNCATE ... CASCADE.",
2896                                                                    relname2)));
2897                         }
2898                 }
2899         }
2900 }
2901
2902 /*
2903  * heap_truncate_find_FKs
2904  *              Find relations having foreign keys referencing any of the given rels
2905  *
2906  * Input and result are both lists of relation OIDs.  The result contains
2907  * no duplicates, does *not* include any rels that were already in the input
2908  * list, and is sorted in OID order.  (The last property is enforced mainly
2909  * to guarantee consistent behavior in the regression tests; we don't want
2910  * behavior to change depending on chance locations of rows in pg_constraint.)
2911  *
2912  * Note: caller should already have appropriate lock on all rels mentioned
2913  * in relationIds.  Since adding or dropping an FK requires exclusive lock
2914  * on both rels, this ensures that the answer will be stable.
2915  */
2916 List *
2917 heap_truncate_find_FKs(List *relationIds)
2918 {
2919         List       *result = NIL;
2920         Relation        fkeyRel;
2921         SysScanDesc fkeyScan;
2922         HeapTuple       tuple;
2923
2924         /*
2925          * Must scan pg_constraint.  Right now, it is a seqscan because there is
2926          * no available index on confrelid.
2927          */
2928         fkeyRel = heap_open(ConstraintRelationId, AccessShareLock);
2929
2930         fkeyScan = systable_beginscan(fkeyRel, InvalidOid, false,
2931                                                                   NULL, 0, NULL);
2932
2933         while (HeapTupleIsValid(tuple = systable_getnext(fkeyScan)))
2934         {
2935                 Form_pg_constraint con = (Form_pg_constraint) GETSTRUCT(tuple);
2936
2937                 /* Not a foreign key */
2938                 if (con->contype != CONSTRAINT_FOREIGN)
2939                         continue;
2940
2941                 /* Not referencing one of our list of tables */
2942                 if (!list_member_oid(relationIds, con->confrelid))
2943                         continue;
2944
2945                 /* Add referencer unless already in input or result list */
2946                 if (!list_member_oid(relationIds, con->conrelid))
2947                         result = insert_ordered_unique_oid(result, con->conrelid);
2948         }
2949
2950         systable_endscan(fkeyScan);
2951         heap_close(fkeyRel, AccessShareLock);
2952
2953         return result;
2954 }
2955
2956 /*
2957  * insert_ordered_unique_oid
2958  *              Insert a new Oid into a sorted list of Oids, preserving ordering,
2959  *              and eliminating duplicates
2960  *
2961  * Building the ordered list this way is O(N^2), but with a pretty small
2962  * constant, so for the number of entries we expect it will probably be
2963  * faster than trying to apply qsort().  It seems unlikely someone would be
2964  * trying to truncate a table with thousands of dependent tables ...
2965  */
2966 static List *
2967 insert_ordered_unique_oid(List *list, Oid datum)
2968 {
2969         ListCell   *prev;
2970
2971         /* Does the datum belong at the front? */
2972         if (list == NIL || datum < linitial_oid(list))
2973                 return lcons_oid(datum, list);
2974         /* Does it match the first entry? */
2975         if (datum == linitial_oid(list))
2976                 return list;                    /* duplicate, so don't insert */
2977         /* No, so find the entry it belongs after */
2978         prev = list_head(list);
2979         for (;;)
2980         {
2981                 ListCell   *curr = lnext(prev);
2982
2983                 if (curr == NULL || datum < lfirst_oid(curr))
2984                         break;                          /* it belongs after 'prev', before 'curr' */
2985
2986                 if (datum == lfirst_oid(curr))
2987                         return list;            /* duplicate, so don't insert */
2988
2989                 prev = curr;
2990         }
2991         /* Insert datum into list after 'prev' */
2992         lappend_cell_oid(list, prev, datum);
2993         return list;
2994 }