PostgreSQL Source Code  git master
relcache.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * relcache.c
4  * POSTGRES relation descriptor cache code
5  *
6  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/utils/cache/relcache.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 /*
16  * INTERFACE ROUTINES
17  * RelationCacheInitialize - initialize relcache (to empty)
18  * RelationCacheInitializePhase2 - initialize shared-catalog entries
19  * RelationCacheInitializePhase3 - finish initializing relcache
20  * RelationIdGetRelation - get a reldesc by relation id
21  * RelationClose - close an open relation
22  *
23  * NOTES
24  * The following code contains many undocumented hacks. Please be
25  * careful....
26  */
27 #include "postgres.h"
28 
29 #include <sys/file.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 
33 #include "access/htup_details.h"
34 #include "access/multixact.h"
35 #include "access/nbtree.h"
36 #include "access/parallel.h"
37 #include "access/reloptions.h"
38 #include "access/sysattr.h"
39 #include "access/table.h"
40 #include "access/tableam.h"
41 #include "access/tupdesc_details.h"
42 #include "access/xact.h"
43 #include "access/xlog.h"
44 #include "catalog/catalog.h"
45 #include "catalog/index.h"
46 #include "catalog/indexing.h"
47 #include "catalog/namespace.h"
48 #include "catalog/partition.h"
49 #include "catalog/pg_am.h"
50 #include "catalog/pg_amproc.h"
51 #include "catalog/pg_attrdef.h"
53 #include "catalog/pg_authid.h"
54 #include "catalog/pg_constraint.h"
55 #include "catalog/pg_database.h"
56 #include "catalog/pg_namespace.h"
57 #include "catalog/pg_opclass.h"
58 #include "catalog/pg_proc.h"
59 #include "catalog/pg_publication.h"
60 #include "catalog/pg_rewrite.h"
61 #include "catalog/pg_shseclabel.h"
64 #include "catalog/pg_tablespace.h"
65 #include "catalog/pg_trigger.h"
66 #include "catalog/pg_type.h"
67 #include "catalog/schemapg.h"
68 #include "catalog/storage.h"
69 #include "commands/policy.h"
70 #include "commands/trigger.h"
71 #include "miscadmin.h"
72 #include "nodes/makefuncs.h"
73 #include "nodes/nodeFuncs.h"
74 #include "optimizer/optimizer.h"
75 #include "rewrite/rewriteDefine.h"
76 #include "rewrite/rowsecurity.h"
77 #include "storage/lmgr.h"
78 #include "storage/smgr.h"
79 #include "utils/array.h"
80 #include "utils/builtins.h"
81 #include "utils/datum.h"
82 #include "utils/fmgroids.h"
83 #include "utils/inval.h"
84 #include "utils/lsyscache.h"
85 #include "utils/memutils.h"
86 #include "utils/relmapper.h"
87 #include "utils/resowner_private.h"
88 #include "utils/snapmgr.h"
89 #include "utils/syscache.h"
90 
91 #define RELCACHE_INIT_FILEMAGIC 0x573266 /* version ID value */
92 
93 /*
94  * Whether to bother checking if relation cache memory needs to be freed
95  * eagerly. See also RelationBuildDesc() and pg_config_manual.h.
96  */
97 #if defined(RECOVER_RELATION_BUILD_MEMORY) && (RECOVER_RELATION_BUILD_MEMORY != 0)
98 #define MAYBE_RECOVER_RELATION_BUILD_MEMORY 1
99 #else
100 #define RECOVER_RELATION_BUILD_MEMORY 0
101 #ifdef CLOBBER_CACHE_ENABLED
102 #define MAYBE_RECOVER_RELATION_BUILD_MEMORY 1
103 #endif
104 #endif
105 
106 /*
107  * hardcoded tuple descriptors, contents generated by genbki.pl
108  */
109 static const FormData_pg_attribute Desc_pg_class[Natts_pg_class] = {Schema_pg_class};
110 static const FormData_pg_attribute Desc_pg_attribute[Natts_pg_attribute] = {Schema_pg_attribute};
111 static const FormData_pg_attribute Desc_pg_proc[Natts_pg_proc] = {Schema_pg_proc};
112 static const FormData_pg_attribute Desc_pg_type[Natts_pg_type] = {Schema_pg_type};
113 static const FormData_pg_attribute Desc_pg_database[Natts_pg_database] = {Schema_pg_database};
114 static const FormData_pg_attribute Desc_pg_authid[Natts_pg_authid] = {Schema_pg_authid};
115 static const FormData_pg_attribute Desc_pg_auth_members[Natts_pg_auth_members] = {Schema_pg_auth_members};
116 static const FormData_pg_attribute Desc_pg_index[Natts_pg_index] = {Schema_pg_index};
117 static const FormData_pg_attribute Desc_pg_shseclabel[Natts_pg_shseclabel] = {Schema_pg_shseclabel};
118 static const FormData_pg_attribute Desc_pg_subscription[Natts_pg_subscription] = {Schema_pg_subscription};
119 
120 /*
121  * Hash tables that index the relation cache
122  *
123  * We used to index the cache by both name and OID, but now there
124  * is only an index by OID.
125  */
126 typedef struct relidcacheent
127 {
130 } RelIdCacheEnt;
131 
133 
134 /*
135  * This flag is false until we have prepared the critical relcache entries
136  * that are needed to do indexscans on the tables read by relcache building.
137  */
139 
140 /*
141  * This flag is false until we have prepared the critical relcache entries
142  * for shared catalogs (which are the tables needed for login).
143  */
145 
146 /*
147  * This counter counts relcache inval events received since backend startup
148  * (but only for rels that are actually in cache). Presently, we use it only
149  * to detect whether data about to be written by write_relcache_init_file()
150  * might already be obsolete.
151  */
152 static long relcacheInvalsReceived = 0L;
153 
154 /*
155  * eoxact_list[] stores the OIDs of relations that (might) need AtEOXact
156  * cleanup work. This list intentionally has limited size; if it overflows,
157  * we fall back to scanning the whole hashtable. There is no value in a very
158  * large list because (1) at some point, a hash_seq_search scan is faster than
159  * retail lookups, and (2) the value of this is to reduce EOXact work for
160  * short transactions, which can't have dirtied all that many tables anyway.
161  * EOXactListAdd() does not bother to prevent duplicate list entries, so the
162  * cleanup processing must be idempotent.
163  */
164 #define MAX_EOXACT_LIST 32
166 static int eoxact_list_len = 0;
167 static bool eoxact_list_overflowed = false;
168 
169 #define EOXactListAdd(rel) \
170  do { \
171  if (eoxact_list_len < MAX_EOXACT_LIST) \
172  eoxact_list[eoxact_list_len++] = (rel)->rd_id; \
173  else \
174  eoxact_list_overflowed = true; \
175  } while (0)
176 
177 /*
178  * EOXactTupleDescArray stores TupleDescs that (might) need AtEOXact
179  * cleanup work. The array expands as needed; there is no hashtable because
180  * we don't need to access individual items except at EOXact.
181  */
183 static int NextEOXactTupleDescNum = 0;
184 static int EOXactTupleDescArrayLen = 0;
185 
186 /*
187  * macros to manipulate the lookup hashtable
188  */
189 #define RelationCacheInsert(RELATION, replace_allowed) \
190 do { \
191  RelIdCacheEnt *hentry; bool found; \
192  hentry = (RelIdCacheEnt *) hash_search(RelationIdCache, \
193  (void *) &((RELATION)->rd_id), \
194  HASH_ENTER, &found); \
195  if (found) \
196  { \
197  /* see comments in RelationBuildDesc and RelationBuildLocalRelation */ \
198  Relation _old_rel = hentry->reldesc; \
199  Assert(replace_allowed); \
200  hentry->reldesc = (RELATION); \
201  if (RelationHasReferenceCountZero(_old_rel)) \
202  RelationDestroyRelation(_old_rel, false); \
203  else if (!IsBootstrapProcessingMode()) \
204  elog(WARNING, "leaking still-referenced relcache entry for \"%s\"", \
205  RelationGetRelationName(_old_rel)); \
206  } \
207  else \
208  hentry->reldesc = (RELATION); \
209 } while(0)
210 
211 #define RelationIdCacheLookup(ID, RELATION) \
212 do { \
213  RelIdCacheEnt *hentry; \
214  hentry = (RelIdCacheEnt *) hash_search(RelationIdCache, \
215  (void *) &(ID), \
216  HASH_FIND, NULL); \
217  if (hentry) \
218  RELATION = hentry->reldesc; \
219  else \
220  RELATION = NULL; \
221 } while(0)
222 
223 #define RelationCacheDelete(RELATION) \
224 do { \
225  RelIdCacheEnt *hentry; \
226  hentry = (RelIdCacheEnt *) hash_search(RelationIdCache, \
227  (void *) &((RELATION)->rd_id), \
228  HASH_REMOVE, NULL); \
229  if (hentry == NULL) \
230  elog(WARNING, "failed to delete relcache entry for OID %u", \
231  (RELATION)->rd_id); \
232 } while(0)
233 
234 
235 /*
236  * Special cache for opclass-related information
237  *
238  * Note: only default support procs get cached, ie, those with
239  * lefttype = righttype = opcintype.
240  */
241 typedef struct opclasscacheent
242 {
243  Oid opclassoid; /* lookup key: OID of opclass */
244  bool valid; /* set true after successful fill-in */
245  StrategyNumber numSupport; /* max # of support procs (from pg_am) */
246  Oid opcfamily; /* OID of opclass's family */
247  Oid opcintype; /* OID of opclass's declared input type */
248  RegProcedure *supportProcs; /* OIDs of support procedures */
250 
251 static HTAB *OpClassCache = NULL;
252 
253 
254 /* non-export function prototypes */
255 
256 static void RelationDestroyRelation(Relation relation, bool remember_tupdesc);
257 static void RelationClearRelation(Relation relation, bool rebuild);
258 
259 static void RelationReloadIndexInfo(Relation relation);
260 static void RelationReloadNailed(Relation relation);
261 static void RelationFlushRelation(Relation relation);
263 #ifdef USE_ASSERT_CHECKING
264 static void AssertPendingSyncConsistency(Relation relation);
265 #endif
266 static void AtEOXact_cleanup(Relation relation, bool isCommit);
267 static void AtEOSubXact_cleanup(Relation relation, bool isCommit,
268  SubTransactionId mySubid, SubTransactionId parentSubid);
269 static bool load_relcache_init_file(bool shared);
270 static void write_relcache_init_file(bool shared);
271 static void write_item(const void *data, Size len, FILE *fp);
272 
273 static void formrdesc(const char *relationName, Oid relationReltype,
274  bool isshared, int natts, const FormData_pg_attribute *attrs);
275 
276 static HeapTuple ScanPgRelation(Oid targetRelId, bool indexOK, bool force_non_historic);
278 static void RelationParseRelOptions(Relation relation, HeapTuple tuple);
279 static void RelationBuildTupleDesc(Relation relation);
280 static Relation RelationBuildDesc(Oid targetRelId, bool insertIt);
281 static void RelationInitPhysicalAddr(Relation relation);
282 static void load_critical_index(Oid indexoid, Oid heapoid);
283 static TupleDesc GetPgClassDescriptor(void);
284 static TupleDesc GetPgIndexDescriptor(void);
285 static void AttrDefaultFetch(Relation relation, int ndef);
286 static int AttrDefaultCmp(const void *a, const void *b);
287 static void CheckConstraintFetch(Relation relation);
288 static int CheckConstraintCmp(const void *a, const void *b);
289 static void InitIndexAmRoutine(Relation relation);
290 static void IndexSupportInitialize(oidvector *indclass,
291  RegProcedure *indexSupport,
292  Oid *opFamily,
293  Oid *opcInType,
294  StrategyNumber maxSupportNumber,
295  AttrNumber maxAttributeNumber);
296 static OpClassCacheEnt *LookupOpclassInfo(Oid operatorClassOid,
297  StrategyNumber numSupport);
298 static void RelationCacheInitFileRemoveInDir(const char *tblspcpath);
299 static void unlink_initfile(const char *initfilename, int elevel);
300 
301 
302 /*
303  * ScanPgRelation
304  *
305  * This is used by RelationBuildDesc to find a pg_class
306  * tuple matching targetRelId. The caller must hold at least
307  * AccessShareLock on the target relid to prevent concurrent-update
308  * scenarios; it isn't guaranteed that all scans used to build the
309  * relcache entry will use the same snapshot. If, for example,
310  * an attribute were to be added after scanning pg_class and before
311  * scanning pg_attribute, relnatts wouldn't match.
312  *
313  * NB: the returned tuple has been copied into palloc'd storage
314  * and must eventually be freed with heap_freetuple.
315  */
316 static HeapTuple
317 ScanPgRelation(Oid targetRelId, bool indexOK, bool force_non_historic)
318 {
319  HeapTuple pg_class_tuple;
320  Relation pg_class_desc;
321  SysScanDesc pg_class_scan;
322  ScanKeyData key[1];
323  Snapshot snapshot = NULL;
324 
325  /*
326  * If something goes wrong during backend startup, we might find ourselves
327  * trying to read pg_class before we've selected a database. That ain't
328  * gonna work, so bail out with a useful error message. If this happens,
329  * it probably means a relcache entry that needs to be nailed isn't.
330  */
331  if (!OidIsValid(MyDatabaseId))
332  elog(FATAL, "cannot read pg_class without having selected a database");
333 
334  /*
335  * form a scan key
336  */
337  ScanKeyInit(&key[0],
338  Anum_pg_class_oid,
339  BTEqualStrategyNumber, F_OIDEQ,
340  ObjectIdGetDatum(targetRelId));
341 
342  /*
343  * Open pg_class and fetch a tuple. Force heap scan if we haven't yet
344  * built the critical relcache entries (this includes initdb and startup
345  * without a pg_internal.init file). The caller can also force a heap
346  * scan by setting indexOK == false.
347  */
348  pg_class_desc = table_open(RelationRelationId, AccessShareLock);
349 
350  /*
351  * The caller might need a tuple that's newer than the one the historic
352  * snapshot; currently the only case requiring to do so is looking up the
353  * relfilenode of non mapped system relations during decoding. That
354  * snapshot can't change in the midst of a relcache build, so there's no
355  * need to register the snapshot.
356  */
357  if (force_non_historic)
358  snapshot = GetNonHistoricCatalogSnapshot(RelationRelationId);
359 
360  pg_class_scan = systable_beginscan(pg_class_desc, ClassOidIndexId,
361  indexOK && criticalRelcachesBuilt,
362  snapshot,
363  1, key);
364 
365  pg_class_tuple = systable_getnext(pg_class_scan);
366 
367  /*
368  * Must copy tuple before releasing buffer.
369  */
370  if (HeapTupleIsValid(pg_class_tuple))
371  pg_class_tuple = heap_copytuple(pg_class_tuple);
372 
373  /* all done */
374  systable_endscan(pg_class_scan);
375  table_close(pg_class_desc, AccessShareLock);
376 
377  return pg_class_tuple;
378 }
379 
380 /*
381  * AllocateRelationDesc
382  *
383  * This is used to allocate memory for a new relation descriptor
384  * and initialize the rd_rel field from the given pg_class tuple.
385  */
386 static Relation
388 {
389  Relation relation;
390  MemoryContext oldcxt;
391  Form_pg_class relationForm;
392 
393  /* Relcache entries must live in CacheMemoryContext */
395 
396  /*
397  * allocate and zero space for new relation descriptor
398  */
399  relation = (Relation) palloc0(sizeof(RelationData));
400 
401  /* make sure relation is marked as having no open file yet */
402  relation->rd_smgr = NULL;
403 
404  /*
405  * Copy the relation tuple form
406  *
407  * We only allocate space for the fixed fields, ie, CLASS_TUPLE_SIZE. The
408  * variable-length fields (relacl, reloptions) are NOT stored in the
409  * relcache --- there'd be little point in it, since we don't copy the
410  * tuple's nulls bitmap and hence wouldn't know if the values are valid.
411  * Bottom line is that relacl *cannot* be retrieved from the relcache. Get
412  * it from the syscache if you need it. The same goes for the original
413  * form of reloptions (however, we do store the parsed form of reloptions
414  * in rd_options).
415  */
416  relationForm = (Form_pg_class) palloc(CLASS_TUPLE_SIZE);
417 
418  memcpy(relationForm, relp, CLASS_TUPLE_SIZE);
419 
420  /* initialize relation tuple form */
421  relation->rd_rel = relationForm;
422 
423  /* and allocate attribute tuple form storage */
424  relation->rd_att = CreateTemplateTupleDesc(relationForm->relnatts);
425  /* which we mark as a reference-counted tupdesc */
426  relation->rd_att->tdrefcount = 1;
427 
428  MemoryContextSwitchTo(oldcxt);
429 
430  return relation;
431 }
432 
433 /*
434  * RelationParseRelOptions
435  * Convert pg_class.reloptions into pre-parsed rd_options
436  *
437  * tuple is the real pg_class tuple (not rd_rel!) for relation
438  *
439  * Note: rd_rel and (if an index) rd_indam must be valid already
440  */
441 static void
443 {
444  bytea *options;
445  amoptions_function amoptsfn;
446 
447  relation->rd_options = NULL;
448 
449  /*
450  * Look up any AM-specific parse function; fall out if relkind should not
451  * have options.
452  */
453  switch (relation->rd_rel->relkind)
454  {
455  case RELKIND_RELATION:
456  case RELKIND_TOASTVALUE:
457  case RELKIND_VIEW:
458  case RELKIND_MATVIEW:
459  case RELKIND_PARTITIONED_TABLE:
460  amoptsfn = NULL;
461  break;
462  case RELKIND_INDEX:
463  case RELKIND_PARTITIONED_INDEX:
464  amoptsfn = relation->rd_indam->amoptions;
465  break;
466  default:
467  return;
468  }
469 
470  /*
471  * Fetch reloptions from tuple; have to use a hardwired descriptor because
472  * we might not have any other for pg_class yet (consider executing this
473  * code for pg_class itself)
474  */
475  options = extractRelOptions(tuple, GetPgClassDescriptor(), amoptsfn);
476 
477  /*
478  * Copy parsed data into CacheMemoryContext. To guard against the
479  * possibility of leaks in the reloptions code, we want to do the actual
480  * parsing in the caller's memory context and copy the results into
481  * CacheMemoryContext after the fact.
482  */
483  if (options)
484  {
486  VARSIZE(options));
487  memcpy(relation->rd_options, options, VARSIZE(options));
488  pfree(options);
489  }
490 }
491 
492 /*
493  * RelationBuildTupleDesc
494  *
495  * Form the relation's tuple descriptor from information in
496  * the pg_attribute, pg_attrdef & pg_constraint system catalogs.
497  */
498 static void
500 {
501  HeapTuple pg_attribute_tuple;
502  Relation pg_attribute_desc;
503  SysScanDesc pg_attribute_scan;
504  ScanKeyData skey[2];
505  int need;
506  TupleConstr *constr;
507  AttrMissing *attrmiss = NULL;
508  int ndef = 0;
509 
510  /* fill rd_att's type ID fields (compare heap.c's AddNewRelationTuple) */
511  relation->rd_att->tdtypeid =
512  relation->rd_rel->reltype ? relation->rd_rel->reltype : RECORDOID;
513  relation->rd_att->tdtypmod = -1; /* just to be sure */
514 
516  sizeof(TupleConstr));
517  constr->has_not_null = false;
518  constr->has_generated_stored = false;
519 
520  /*
521  * Form a scan key that selects only user attributes (attnum > 0).
522  * (Eliminating system attribute rows at the index level is lots faster
523  * than fetching them.)
524  */
525  ScanKeyInit(&skey[0],
526  Anum_pg_attribute_attrelid,
527  BTEqualStrategyNumber, F_OIDEQ,
529  ScanKeyInit(&skey[1],
530  Anum_pg_attribute_attnum,
531  BTGreaterStrategyNumber, F_INT2GT,
532  Int16GetDatum(0));
533 
534  /*
535  * Open pg_attribute and begin a scan. Force heap scan if we haven't yet
536  * built the critical relcache entries (this includes initdb and startup
537  * without a pg_internal.init file).
538  */
539  pg_attribute_desc = table_open(AttributeRelationId, AccessShareLock);
540  pg_attribute_scan = systable_beginscan(pg_attribute_desc,
543  NULL,
544  2, skey);
545 
546  /*
547  * add attribute data to relation->rd_att
548  */
549  need = RelationGetNumberOfAttributes(relation);
550 
551  while (HeapTupleIsValid(pg_attribute_tuple = systable_getnext(pg_attribute_scan)))
552  {
553  Form_pg_attribute attp;
554  int attnum;
555 
556  attp = (Form_pg_attribute) GETSTRUCT(pg_attribute_tuple);
557 
558  attnum = attp->attnum;
559  if (attnum <= 0 || attnum > RelationGetNumberOfAttributes(relation))
560  elog(ERROR, "invalid attribute number %d for relation \"%s\"",
561  attp->attnum, RelationGetRelationName(relation));
562 
563  memcpy(TupleDescAttr(relation->rd_att, attnum - 1),
564  attp,
566 
567  /* Update constraint/default info */
568  if (attp->attnotnull)
569  constr->has_not_null = true;
570  if (attp->attgenerated == ATTRIBUTE_GENERATED_STORED)
571  constr->has_generated_stored = true;
572  if (attp->atthasdef)
573  ndef++;
574 
575  /* If the column has a "missing" value, put it in the attrmiss array */
576  if (attp->atthasmissing)
577  {
578  Datum missingval;
579  bool missingNull;
580 
581  /* Do we have a missing value? */
582  missingval = heap_getattr(pg_attribute_tuple,
583  Anum_pg_attribute_attmissingval,
584  pg_attribute_desc->rd_att,
585  &missingNull);
586  if (!missingNull)
587  {
588  /* Yes, fetch from the array */
589  MemoryContext oldcxt;
590  bool is_null;
591  int one = 1;
592  Datum missval;
593 
594  if (attrmiss == NULL)
595  attrmiss = (AttrMissing *)
597  relation->rd_rel->relnatts *
598  sizeof(AttrMissing));
599 
600  missval = array_get_element(missingval,
601  1,
602  &one,
603  -1,
604  attp->attlen,
605  attp->attbyval,
606  attp->attalign,
607  &is_null);
608  Assert(!is_null);
609  if (attp->attbyval)
610  {
611  /* for copy by val just copy the datum direct */
612  attrmiss[attnum - 1].am_value = missval;
613  }
614  else
615  {
616  /* otherwise copy in the correct context */
618  attrmiss[attnum - 1].am_value = datumCopy(missval,
619  attp->attbyval,
620  attp->attlen);
621  MemoryContextSwitchTo(oldcxt);
622  }
623  attrmiss[attnum - 1].am_present = true;
624  }
625  }
626  need--;
627  if (need == 0)
628  break;
629  }
630 
631  /*
632  * end the scan and close the attribute relation
633  */
634  systable_endscan(pg_attribute_scan);
635  table_close(pg_attribute_desc, AccessShareLock);
636 
637  if (need != 0)
638  elog(ERROR, "pg_attribute catalog is missing %d attribute(s) for relation OID %u",
639  need, RelationGetRelid(relation));
640 
641  /*
642  * The attcacheoff values we read from pg_attribute should all be -1
643  * ("unknown"). Verify this if assert checking is on. They will be
644  * computed when and if needed during tuple access.
645  */
646 #ifdef USE_ASSERT_CHECKING
647  {
648  int i;
649 
650  for (i = 0; i < RelationGetNumberOfAttributes(relation); i++)
651  Assert(TupleDescAttr(relation->rd_att, i)->attcacheoff == -1);
652  }
653 #endif
654 
655  /*
656  * However, we can easily set the attcacheoff value for the first
657  * attribute: it must be zero. This eliminates the need for special cases
658  * for attnum=1 that used to exist in fastgetattr() and index_getattr().
659  */
660  if (RelationGetNumberOfAttributes(relation) > 0)
661  TupleDescAttr(relation->rd_att, 0)->attcacheoff = 0;
662 
663  /*
664  * Set up constraint/default info
665  */
666  if (constr->has_not_null ||
667  constr->has_generated_stored ||
668  ndef > 0 ||
669  attrmiss ||
670  relation->rd_rel->relchecks > 0)
671  {
672  relation->rd_att->constr = constr;
673 
674  if (ndef > 0) /* DEFAULTs */
675  AttrDefaultFetch(relation, ndef);
676  else
677  constr->num_defval = 0;
678 
679  constr->missing = attrmiss;
680 
681  if (relation->rd_rel->relchecks > 0) /* CHECKs */
682  CheckConstraintFetch(relation);
683  else
684  constr->num_check = 0;
685  }
686  else
687  {
688  pfree(constr);
689  relation->rd_att->constr = NULL;
690  }
691 }
692 
693 /*
694  * RelationBuildRuleLock
695  *
696  * Form the relation's rewrite rules from information in
697  * the pg_rewrite system catalog.
698  *
699  * Note: The rule parsetrees are potentially very complex node structures.
700  * To allow these trees to be freed when the relcache entry is flushed,
701  * we make a private memory context to hold the RuleLock information for
702  * each relcache entry that has associated rules. The context is used
703  * just for rule info, not for any other subsidiary data of the relcache
704  * entry, because that keeps the update logic in RelationClearRelation()
705  * manageable. The other subsidiary data structures are simple enough
706  * to be easy to free explicitly, anyway.
707  */
708 static void
710 {
711  MemoryContext rulescxt;
712  MemoryContext oldcxt;
713  HeapTuple rewrite_tuple;
714  Relation rewrite_desc;
715  TupleDesc rewrite_tupdesc;
716  SysScanDesc rewrite_scan;
718  RuleLock *rulelock;
719  int numlocks;
720  RewriteRule **rules;
721  int maxlocks;
722 
723  /*
724  * Make the private context. Assume it'll not contain much data.
725  */
727  "relation rules",
729  relation->rd_rulescxt = rulescxt;
731  RelationGetRelationName(relation));
732 
733  /*
734  * allocate an array to hold the rewrite rules (the array is extended if
735  * necessary)
736  */
737  maxlocks = 4;
738  rules = (RewriteRule **)
739  MemoryContextAlloc(rulescxt, sizeof(RewriteRule *) * maxlocks);
740  numlocks = 0;
741 
742  /*
743  * form a scan key
744  */
745  ScanKeyInit(&key,
746  Anum_pg_rewrite_ev_class,
747  BTEqualStrategyNumber, F_OIDEQ,
749 
750  /*
751  * open pg_rewrite and begin a scan
752  *
753  * Note: since we scan the rules using RewriteRelRulenameIndexId, we will
754  * be reading the rules in name order, except possibly during
755  * emergency-recovery operations (ie, IgnoreSystemIndexes). This in turn
756  * ensures that rules will be fired in name order.
757  */
758  rewrite_desc = table_open(RewriteRelationId, AccessShareLock);
759  rewrite_tupdesc = RelationGetDescr(rewrite_desc);
760  rewrite_scan = systable_beginscan(rewrite_desc,
762  true, NULL,
763  1, &key);
764 
765  while (HeapTupleIsValid(rewrite_tuple = systable_getnext(rewrite_scan)))
766  {
767  Form_pg_rewrite rewrite_form = (Form_pg_rewrite) GETSTRUCT(rewrite_tuple);
768  bool isnull;
769  Datum rule_datum;
770  char *rule_str;
771  RewriteRule *rule;
772 
773  rule = (RewriteRule *) MemoryContextAlloc(rulescxt,
774  sizeof(RewriteRule));
775 
776  rule->ruleId = rewrite_form->oid;
777 
778  rule->event = rewrite_form->ev_type - '0';
779  rule->enabled = rewrite_form->ev_enabled;
780  rule->isInstead = rewrite_form->is_instead;
781 
782  /*
783  * Must use heap_getattr to fetch ev_action and ev_qual. Also, the
784  * rule strings are often large enough to be toasted. To avoid
785  * leaking memory in the caller's context, do the detoasting here so
786  * we can free the detoasted version.
787  */
788  rule_datum = heap_getattr(rewrite_tuple,
789  Anum_pg_rewrite_ev_action,
790  rewrite_tupdesc,
791  &isnull);
792  Assert(!isnull);
793  rule_str = TextDatumGetCString(rule_datum);
794  oldcxt = MemoryContextSwitchTo(rulescxt);
795  rule->actions = (List *) stringToNode(rule_str);
796  MemoryContextSwitchTo(oldcxt);
797  pfree(rule_str);
798 
799  rule_datum = heap_getattr(rewrite_tuple,
800  Anum_pg_rewrite_ev_qual,
801  rewrite_tupdesc,
802  &isnull);
803  Assert(!isnull);
804  rule_str = TextDatumGetCString(rule_datum);
805  oldcxt = MemoryContextSwitchTo(rulescxt);
806  rule->qual = (Node *) stringToNode(rule_str);
807  MemoryContextSwitchTo(oldcxt);
808  pfree(rule_str);
809 
810  /*
811  * We want the rule's table references to be checked as though by the
812  * table owner, not the user referencing the rule. Therefore, scan
813  * through the rule's actions and set the checkAsUser field on all
814  * rtable entries. We have to look at the qual as well, in case it
815  * contains sublinks.
816  *
817  * The reason for doing this when the rule is loaded, rather than when
818  * it is stored, is that otherwise ALTER TABLE OWNER would have to
819  * grovel through stored rules to update checkAsUser fields. Scanning
820  * the rule tree during load is relatively cheap (compared to
821  * constructing it in the first place), so we do it here.
822  */
823  setRuleCheckAsUser((Node *) rule->actions, relation->rd_rel->relowner);
824  setRuleCheckAsUser(rule->qual, relation->rd_rel->relowner);
825 
826  if (numlocks >= maxlocks)
827  {
828  maxlocks *= 2;
829  rules = (RewriteRule **)
830  repalloc(rules, sizeof(RewriteRule *) * maxlocks);
831  }
832  rules[numlocks++] = rule;
833  }
834 
835  /*
836  * end the scan and close the attribute relation
837  */
838  systable_endscan(rewrite_scan);
839  table_close(rewrite_desc, AccessShareLock);
840 
841  /*
842  * there might not be any rules (if relhasrules is out-of-date)
843  */
844  if (numlocks == 0)
845  {
846  relation->rd_rules = NULL;
847  relation->rd_rulescxt = NULL;
848  MemoryContextDelete(rulescxt);
849  return;
850  }
851 
852  /*
853  * form a RuleLock and insert into relation
854  */
855  rulelock = (RuleLock *) MemoryContextAlloc(rulescxt, sizeof(RuleLock));
856  rulelock->numLocks = numlocks;
857  rulelock->rules = rules;
858 
859  relation->rd_rules = rulelock;
860 }
861 
862 /*
863  * equalRuleLocks
864  *
865  * Determine whether two RuleLocks are equivalent
866  *
867  * Probably this should be in the rules code someplace...
868  */
869 static bool
871 {
872  int i;
873 
874  /*
875  * As of 7.3 we assume the rule ordering is repeatable, because
876  * RelationBuildRuleLock should read 'em in a consistent order. So just
877  * compare corresponding slots.
878  */
879  if (rlock1 != NULL)
880  {
881  if (rlock2 == NULL)
882  return false;
883  if (rlock1->numLocks != rlock2->numLocks)
884  return false;
885  for (i = 0; i < rlock1->numLocks; i++)
886  {
887  RewriteRule *rule1 = rlock1->rules[i];
888  RewriteRule *rule2 = rlock2->rules[i];
889 
890  if (rule1->ruleId != rule2->ruleId)
891  return false;
892  if (rule1->event != rule2->event)
893  return false;
894  if (rule1->enabled != rule2->enabled)
895  return false;
896  if (rule1->isInstead != rule2->isInstead)
897  return false;
898  if (!equal(rule1->qual, rule2->qual))
899  return false;
900  if (!equal(rule1->actions, rule2->actions))
901  return false;
902  }
903  }
904  else if (rlock2 != NULL)
905  return false;
906  return true;
907 }
908 
909 /*
910  * equalPolicy
911  *
912  * Determine whether two policies are equivalent
913  */
914 static bool
916 {
917  int i;
918  Oid *r1,
919  *r2;
920 
921  if (policy1 != NULL)
922  {
923  if (policy2 == NULL)
924  return false;
925 
926  if (policy1->polcmd != policy2->polcmd)
927  return false;
928  if (policy1->hassublinks != policy2->hassublinks)
929  return false;
930  if (strcmp(policy1->policy_name, policy2->policy_name) != 0)
931  return false;
932  if (ARR_DIMS(policy1->roles)[0] != ARR_DIMS(policy2->roles)[0])
933  return false;
934 
935  r1 = (Oid *) ARR_DATA_PTR(policy1->roles);
936  r2 = (Oid *) ARR_DATA_PTR(policy2->roles);
937 
938  for (i = 0; i < ARR_DIMS(policy1->roles)[0]; i++)
939  {
940  if (r1[i] != r2[i])
941  return false;
942  }
943 
944  if (!equal(policy1->qual, policy2->qual))
945  return false;
946  if (!equal(policy1->with_check_qual, policy2->with_check_qual))
947  return false;
948  }
949  else if (policy2 != NULL)
950  return false;
951 
952  return true;
953 }
954 
955 /*
956  * equalRSDesc
957  *
958  * Determine whether two RowSecurityDesc's are equivalent
959  */
960 static bool
962 {
963  ListCell *lc,
964  *rc;
965 
966  if (rsdesc1 == NULL && rsdesc2 == NULL)
967  return true;
968 
969  if ((rsdesc1 != NULL && rsdesc2 == NULL) ||
970  (rsdesc1 == NULL && rsdesc2 != NULL))
971  return false;
972 
973  if (list_length(rsdesc1->policies) != list_length(rsdesc2->policies))
974  return false;
975 
976  /* RelationBuildRowSecurity should build policies in order */
977  forboth(lc, rsdesc1->policies, rc, rsdesc2->policies)
978  {
981 
982  if (!equalPolicy(l, r))
983  return false;
984  }
985 
986  return true;
987 }
988 
989 /*
990  * RelationBuildDesc
991  *
992  * Build a relation descriptor. The caller must hold at least
993  * AccessShareLock on the target relid.
994  *
995  * The new descriptor is inserted into the hash table if insertIt is true.
996  *
997  * Returns NULL if no pg_class row could be found for the given relid
998  * (suggesting we are trying to access a just-deleted relation).
999  * Any other error is reported via elog.
1000  */
1001 static Relation
1002 RelationBuildDesc(Oid targetRelId, bool insertIt)
1003 {
1004  Relation relation;
1005  Oid relid;
1006  HeapTuple pg_class_tuple;
1007  Form_pg_class relp;
1008 
1009  /*
1010  * This function and its subroutines can allocate a good deal of transient
1011  * data in CurrentMemoryContext. Traditionally we've just leaked that
1012  * data, reasoning that the caller's context is at worst of transaction
1013  * scope, and relcache loads shouldn't happen so often that it's essential
1014  * to recover transient data before end of statement/transaction. However
1015  * that's definitely not true in clobber-cache test builds, and perhaps
1016  * it's not true in other cases.
1017  *
1018  * When cache clobbering is enabled or when forced to by
1019  * RECOVER_RELATION_BUILD_MEMORY=1, arrange to allocate the junk in a
1020  * temporary context that we'll free before returning. Make it a child
1021  * of caller's context so that it will get cleaned up appropriately if
1022  * we error out partway through.
1023  */
1024 #ifdef MAYBE_RECOVER_RELATION_BUILD_MEMORY
1025  MemoryContext tmpcxt = NULL;
1026  MemoryContext oldcxt = NULL;
1027 
1029  {
1031  "RelationBuildDesc workspace",
1033  oldcxt = MemoryContextSwitchTo(tmpcxt);
1034  }
1035 #endif
1036 
1037  /*
1038  * find the tuple in pg_class corresponding to the given relation id
1039  */
1040  pg_class_tuple = ScanPgRelation(targetRelId, true, false);
1041 
1042  /*
1043  * if no such tuple exists, return NULL
1044  */
1045  if (!HeapTupleIsValid(pg_class_tuple))
1046  {
1047 #ifdef MAYBE_RECOVER_RELATION_BUILD_MEMORY
1048  if (tmpcxt)
1049  {
1050  /* Return to caller's context, and blow away the temporary context */
1051  MemoryContextSwitchTo(oldcxt);
1052  MemoryContextDelete(tmpcxt);
1053  }
1054 #endif
1055  return NULL;
1056  }
1057 
1058  /*
1059  * get information from the pg_class_tuple
1060  */
1061  relp = (Form_pg_class) GETSTRUCT(pg_class_tuple);
1062  relid = relp->oid;
1063  Assert(relid == targetRelId);
1064 
1065  /*
1066  * allocate storage for the relation descriptor, and copy pg_class_tuple
1067  * to relation->rd_rel.
1068  */
1069  relation = AllocateRelationDesc(relp);
1070 
1071  /*
1072  * initialize the relation's relation id (relation->rd_id)
1073  */
1074  RelationGetRelid(relation) = relid;
1075 
1076  /*
1077  * Normal relations are not nailed into the cache. Since we don't flush
1078  * new relations, it won't be new. It could be temp though.
1079  */
1080  relation->rd_refcnt = 0;
1081  relation->rd_isnailed = false;
1086  switch (relation->rd_rel->relpersistence)
1087  {
1088  case RELPERSISTENCE_UNLOGGED:
1089  case RELPERSISTENCE_PERMANENT:
1090  relation->rd_backend = InvalidBackendId;
1091  relation->rd_islocaltemp = false;
1092  break;
1093  case RELPERSISTENCE_TEMP:
1094  if (isTempOrTempToastNamespace(relation->rd_rel->relnamespace))
1095  {
1096  relation->rd_backend = BackendIdForTempRelations();
1097  relation->rd_islocaltemp = true;
1098  }
1099  else
1100  {
1101  /*
1102  * If it's a temp table, but not one of ours, we have to use
1103  * the slow, grotty method to figure out the owning backend.
1104  *
1105  * Note: it's possible that rd_backend gets set to MyBackendId
1106  * here, in case we are looking at a pg_class entry left over
1107  * from a crashed backend that coincidentally had the same
1108  * BackendId we're using. We should *not* consider such a
1109  * table to be "ours"; this is why we need the separate
1110  * rd_islocaltemp flag. The pg_class entry will get flushed
1111  * if/when we clean out the corresponding temp table namespace
1112  * in preparation for using it.
1113  */
1114  relation->rd_backend =
1115  GetTempNamespaceBackendId(relation->rd_rel->relnamespace);
1116  Assert(relation->rd_backend != InvalidBackendId);
1117  relation->rd_islocaltemp = false;
1118  }
1119  break;
1120  default:
1121  elog(ERROR, "invalid relpersistence: %c",
1122  relation->rd_rel->relpersistence);
1123  break;
1124  }
1125 
1126  /*
1127  * initialize the tuple descriptor (relation->rd_att).
1128  */
1129  RelationBuildTupleDesc(relation);
1130 
1131  /*
1132  * Fetch rules and triggers that affect this relation
1133  */
1134  if (relation->rd_rel->relhasrules)
1135  RelationBuildRuleLock(relation);
1136  else
1137  {
1138  relation->rd_rules = NULL;
1139  relation->rd_rulescxt = NULL;
1140  }
1141 
1142  if (relation->rd_rel->relhastriggers)
1143  RelationBuildTriggers(relation);
1144  else
1145  relation->trigdesc = NULL;
1146 
1147  if (relation->rd_rel->relrowsecurity)
1148  RelationBuildRowSecurity(relation);
1149  else
1150  relation->rd_rsdesc = NULL;
1151 
1152  /* foreign key data is not loaded till asked for */
1153  relation->rd_fkeylist = NIL;
1154  relation->rd_fkeyvalid = false;
1155 
1156  /* partitioning data is not loaded till asked for */
1157  relation->rd_partkey = NULL;
1158  relation->rd_partkeycxt = NULL;
1159  relation->rd_partdesc = NULL;
1160  relation->rd_pdcxt = NULL;
1161  relation->rd_partcheck = NIL;
1162  relation->rd_partcheckvalid = false;
1163  relation->rd_partcheckcxt = NULL;
1164 
1165  /*
1166  * initialize access method information
1167  */
1168  switch (relation->rd_rel->relkind)
1169  {
1170  case RELKIND_INDEX:
1171  case RELKIND_PARTITIONED_INDEX:
1172  Assert(relation->rd_rel->relam != InvalidOid);
1173  RelationInitIndexAccessInfo(relation);
1174  break;
1175  case RELKIND_RELATION:
1176  case RELKIND_TOASTVALUE:
1177  case RELKIND_MATVIEW:
1178  Assert(relation->rd_rel->relam != InvalidOid);
1180  break;
1181  case RELKIND_SEQUENCE:
1182  Assert(relation->rd_rel->relam == InvalidOid);
1184  break;
1185  case RELKIND_VIEW:
1186  case RELKIND_COMPOSITE_TYPE:
1187  case RELKIND_FOREIGN_TABLE:
1188  case RELKIND_PARTITIONED_TABLE:
1189  Assert(relation->rd_rel->relam == InvalidOid);
1190  break;
1191  }
1192 
1193  /* extract reloptions if any */
1194  RelationParseRelOptions(relation, pg_class_tuple);
1195 
1196  /*
1197  * initialize the relation lock manager information
1198  */
1199  RelationInitLockInfo(relation); /* see lmgr.c */
1200 
1201  /*
1202  * initialize physical addressing information for the relation
1203  */
1204  RelationInitPhysicalAddr(relation);
1205 
1206  /* make sure relation is marked as having no open file yet */
1207  relation->rd_smgr = NULL;
1208 
1209  /*
1210  * now we can free the memory allocated for pg_class_tuple
1211  */
1212  heap_freetuple(pg_class_tuple);
1213 
1214  /*
1215  * Insert newly created relation into relcache hash table, if requested.
1216  *
1217  * There is one scenario in which we might find a hashtable entry already
1218  * present, even though our caller failed to find it: if the relation is a
1219  * system catalog or index that's used during relcache load, we might have
1220  * recursively created the same relcache entry during the preceding steps.
1221  * So allow RelationCacheInsert to delete any already-present relcache
1222  * entry for the same OID. The already-present entry should have refcount
1223  * zero (else somebody forgot to close it); in the event that it doesn't,
1224  * we'll elog a WARNING and leak the already-present entry.
1225  */
1226  if (insertIt)
1227  RelationCacheInsert(relation, true);
1228 
1229  /* It's fully valid */
1230  relation->rd_isvalid = true;
1231 
1232 #ifdef MAYBE_RECOVER_RELATION_BUILD_MEMORY
1233  if (tmpcxt)
1234  {
1235  /* Return to caller's context, and blow away the temporary context */
1236  MemoryContextSwitchTo(oldcxt);
1237  MemoryContextDelete(tmpcxt);
1238  }
1239 #endif
1240 
1241  return relation;
1242 }
1243 
1244 /*
1245  * Initialize the physical addressing info (RelFileNode) for a relcache entry
1246  *
1247  * Note: at the physical level, relations in the pg_global tablespace must
1248  * be treated as shared, even if relisshared isn't set. Hence we do not
1249  * look at relisshared here.
1250  */
1251 static void
1253 {
1254  Oid oldnode = relation->rd_node.relNode;
1255 
1256  /* these relations kinds never have storage */
1257  if (!RELKIND_HAS_STORAGE(relation->rd_rel->relkind))
1258  return;
1259 
1260  if (relation->rd_rel->reltablespace)
1261  relation->rd_node.spcNode = relation->rd_rel->reltablespace;
1262  else
1263  relation->rd_node.spcNode = MyDatabaseTableSpace;
1264  if (relation->rd_node.spcNode == GLOBALTABLESPACE_OID)
1265  relation->rd_node.dbNode = InvalidOid;
1266  else
1267  relation->rd_node.dbNode = MyDatabaseId;
1268 
1269  if (relation->rd_rel->relfilenode)
1270  {
1271  /*
1272  * Even if we are using a decoding snapshot that doesn't represent the
1273  * current state of the catalog we need to make sure the filenode
1274  * points to the current file since the older file will be gone (or
1275  * truncated). The new file will still contain older rows so lookups
1276  * in them will work correctly. This wouldn't work correctly if
1277  * rewrites were allowed to change the schema in an incompatible way,
1278  * but those are prevented both on catalog tables and on user tables
1279  * declared as additional catalog tables.
1280  */
1283  && IsTransactionState())
1284  {
1285  HeapTuple phys_tuple;
1286  Form_pg_class physrel;
1287 
1288  phys_tuple = ScanPgRelation(RelationGetRelid(relation),
1289  RelationGetRelid(relation) != ClassOidIndexId,
1290  true);
1291  if (!HeapTupleIsValid(phys_tuple))
1292  elog(ERROR, "could not find pg_class entry for %u",
1293  RelationGetRelid(relation));
1294  physrel = (Form_pg_class) GETSTRUCT(phys_tuple);
1295 
1296  relation->rd_rel->reltablespace = physrel->reltablespace;
1297  relation->rd_rel->relfilenode = physrel->relfilenode;
1298  heap_freetuple(phys_tuple);
1299  }
1300 
1301  relation->rd_node.relNode = relation->rd_rel->relfilenode;
1302  }
1303  else
1304  {
1305  /* Consult the relation mapper */
1306  relation->rd_node.relNode =
1307  RelationMapOidToFilenode(relation->rd_id,
1308  relation->rd_rel->relisshared);
1309  if (!OidIsValid(relation->rd_node.relNode))
1310  elog(ERROR, "could not find relation mapping for relation \"%s\", OID %u",
1311  RelationGetRelationName(relation), relation->rd_id);
1312  }
1313 
1314  /*
1315  * For RelationNeedsWAL() to answer correctly on parallel workers, restore
1316  * rd_firstRelfilenodeSubid. No subtransactions start or end while in
1317  * parallel mode, so the specific SubTransactionId does not matter.
1318  */
1319  if (IsParallelWorker() && oldnode != relation->rd_node.relNode)
1320  {
1321  if (RelFileNodeSkippingWAL(relation->rd_node))
1323  else
1325  }
1326 }
1327 
1328 /*
1329  * Fill in the IndexAmRoutine for an index relation.
1330  *
1331  * relation's rd_amhandler and rd_indexcxt must be valid already.
1332  */
1333 static void
1335 {
1336  IndexAmRoutine *cached,
1337  *tmp;
1338 
1339  /*
1340  * Call the amhandler in current, short-lived memory context, just in case
1341  * it leaks anything (it probably won't, but let's be paranoid).
1342  */
1343  tmp = GetIndexAmRoutine(relation->rd_amhandler);
1344 
1345  /* OK, now transfer the data into relation's rd_indexcxt. */
1346  cached = (IndexAmRoutine *) MemoryContextAlloc(relation->rd_indexcxt,
1347  sizeof(IndexAmRoutine));
1348  memcpy(cached, tmp, sizeof(IndexAmRoutine));
1349  relation->rd_indam = cached;
1350 
1351  pfree(tmp);
1352 }
1353 
1354 /*
1355  * Initialize index-access-method support data for an index relation
1356  */
1357 void
1359 {
1360  HeapTuple tuple;
1361  Form_pg_am aform;
1362  Datum indcollDatum;
1363  Datum indclassDatum;
1364  Datum indoptionDatum;
1365  bool isnull;
1366  oidvector *indcoll;
1367  oidvector *indclass;
1368  int2vector *indoption;
1369  MemoryContext indexcxt;
1370  MemoryContext oldcontext;
1371  int indnatts;
1372  int indnkeyatts;
1373  uint16 amsupport;
1374 
1375  /*
1376  * Make a copy of the pg_index entry for the index. Since pg_index
1377  * contains variable-length and possibly-null fields, we have to do this
1378  * honestly rather than just treating it as a Form_pg_index struct.
1379  */
1380  tuple = SearchSysCache1(INDEXRELID,
1381  ObjectIdGetDatum(RelationGetRelid(relation)));
1382  if (!HeapTupleIsValid(tuple))
1383  elog(ERROR, "cache lookup failed for index %u",
1384  RelationGetRelid(relation));
1386  relation->rd_indextuple = heap_copytuple(tuple);
1387  relation->rd_index = (Form_pg_index) GETSTRUCT(relation->rd_indextuple);
1388  MemoryContextSwitchTo(oldcontext);
1389  ReleaseSysCache(tuple);
1390 
1391  /*
1392  * Look up the index's access method, save the OID of its handler function
1393  */
1394  tuple = SearchSysCache1(AMOID, ObjectIdGetDatum(relation->rd_rel->relam));
1395  if (!HeapTupleIsValid(tuple))
1396  elog(ERROR, "cache lookup failed for access method %u",
1397  relation->rd_rel->relam);
1398  aform = (Form_pg_am) GETSTRUCT(tuple);
1399  relation->rd_amhandler = aform->amhandler;
1400  ReleaseSysCache(tuple);
1401 
1402  indnatts = RelationGetNumberOfAttributes(relation);
1403  if (indnatts != IndexRelationGetNumberOfAttributes(relation))
1404  elog(ERROR, "relnatts disagrees with indnatts for index %u",
1405  RelationGetRelid(relation));
1406  indnkeyatts = IndexRelationGetNumberOfKeyAttributes(relation);
1407 
1408  /*
1409  * Make the private context to hold index access info. The reason we need
1410  * a context, and not just a couple of pallocs, is so that we won't leak
1411  * any subsidiary info attached to fmgr lookup records.
1412  */
1414  "index info",
1416  relation->rd_indexcxt = indexcxt;
1418  RelationGetRelationName(relation));
1419 
1420  /*
1421  * Now we can fetch the index AM's API struct
1422  */
1423  InitIndexAmRoutine(relation);
1424 
1425  /*
1426  * Allocate arrays to hold data. Opclasses are not used for included
1427  * columns, so allocate them for indnkeyatts only.
1428  */
1429  relation->rd_opfamily = (Oid *)
1430  MemoryContextAllocZero(indexcxt, indnkeyatts * sizeof(Oid));
1431  relation->rd_opcintype = (Oid *)
1432  MemoryContextAllocZero(indexcxt, indnkeyatts * sizeof(Oid));
1433 
1434  amsupport = relation->rd_indam->amsupport;
1435  if (amsupport > 0)
1436  {
1437  int nsupport = indnatts * amsupport;
1438 
1439  relation->rd_support = (RegProcedure *)
1440  MemoryContextAllocZero(indexcxt, nsupport * sizeof(RegProcedure));
1441  relation->rd_supportinfo = (FmgrInfo *)
1442  MemoryContextAllocZero(indexcxt, nsupport * sizeof(FmgrInfo));
1443  }
1444  else
1445  {
1446  relation->rd_support = NULL;
1447  relation->rd_supportinfo = NULL;
1448  }
1449 
1450  relation->rd_indcollation = (Oid *)
1451  MemoryContextAllocZero(indexcxt, indnkeyatts * sizeof(Oid));
1452 
1453  relation->rd_indoption = (int16 *)
1454  MemoryContextAllocZero(indexcxt, indnkeyatts * sizeof(int16));
1455 
1456  /*
1457  * indcollation cannot be referenced directly through the C struct,
1458  * because it comes after the variable-width indkey field. Must extract
1459  * the datum the hard way...
1460  */
1461  indcollDatum = fastgetattr(relation->rd_indextuple,
1462  Anum_pg_index_indcollation,
1464  &isnull);
1465  Assert(!isnull);
1466  indcoll = (oidvector *) DatumGetPointer(indcollDatum);
1467  memcpy(relation->rd_indcollation, indcoll->values, indnkeyatts * sizeof(Oid));
1468 
1469  /*
1470  * indclass cannot be referenced directly through the C struct, because it
1471  * comes after the variable-width indkey field. Must extract the datum
1472  * the hard way...
1473  */
1474  indclassDatum = fastgetattr(relation->rd_indextuple,
1475  Anum_pg_index_indclass,
1477  &isnull);
1478  Assert(!isnull);
1479  indclass = (oidvector *) DatumGetPointer(indclassDatum);
1480 
1481  /*
1482  * Fill the support procedure OID array, as well as the info about
1483  * opfamilies and opclass input types. (aminfo and supportinfo are left
1484  * as zeroes, and are filled on-the-fly when used)
1485  */
1486  IndexSupportInitialize(indclass, relation->rd_support,
1487  relation->rd_opfamily, relation->rd_opcintype,
1488  amsupport, indnkeyatts);
1489 
1490  /*
1491  * Similarly extract indoption and copy it to the cache entry
1492  */
1493  indoptionDatum = fastgetattr(relation->rd_indextuple,
1494  Anum_pg_index_indoption,
1496  &isnull);
1497  Assert(!isnull);
1498  indoption = (int2vector *) DatumGetPointer(indoptionDatum);
1499  memcpy(relation->rd_indoption, indoption->values, indnkeyatts * sizeof(int16));
1500 
1501  (void) RelationGetIndexAttOptions(relation, false);
1502 
1503  /*
1504  * expressions, predicate, exclusion caches will be filled later
1505  */
1506  relation->rd_indexprs = NIL;
1507  relation->rd_indpred = NIL;
1508  relation->rd_exclops = NULL;
1509  relation->rd_exclprocs = NULL;
1510  relation->rd_exclstrats = NULL;
1511  relation->rd_amcache = NULL;
1512 }
1513 
1514 /*
1515  * IndexSupportInitialize
1516  * Initializes an index's cached opclass information,
1517  * given the index's pg_index.indclass entry.
1518  *
1519  * Data is returned into *indexSupport, *opFamily, and *opcInType,
1520  * which are arrays allocated by the caller.
1521  *
1522  * The caller also passes maxSupportNumber and maxAttributeNumber, since these
1523  * indicate the size of the arrays it has allocated --- but in practice these
1524  * numbers must always match those obtainable from the system catalog entries
1525  * for the index and access method.
1526  */
1527 static void
1529  RegProcedure *indexSupport,
1530  Oid *opFamily,
1531  Oid *opcInType,
1532  StrategyNumber maxSupportNumber,
1533  AttrNumber maxAttributeNumber)
1534 {
1535  int attIndex;
1536 
1537  for (attIndex = 0; attIndex < maxAttributeNumber; attIndex++)
1538  {
1539  OpClassCacheEnt *opcentry;
1540 
1541  if (!OidIsValid(indclass->values[attIndex]))
1542  elog(ERROR, "bogus pg_index tuple");
1543 
1544  /* look up the info for this opclass, using a cache */
1545  opcentry = LookupOpclassInfo(indclass->values[attIndex],
1546  maxSupportNumber);
1547 
1548  /* copy cached data into relcache entry */
1549  opFamily[attIndex] = opcentry->opcfamily;
1550  opcInType[attIndex] = opcentry->opcintype;
1551  if (maxSupportNumber > 0)
1552  memcpy(&indexSupport[attIndex * maxSupportNumber],
1553  opcentry->supportProcs,
1554  maxSupportNumber * sizeof(RegProcedure));
1555  }
1556 }
1557 
1558 /*
1559  * LookupOpclassInfo
1560  *
1561  * This routine maintains a per-opclass cache of the information needed
1562  * by IndexSupportInitialize(). This is more efficient than relying on
1563  * the catalog cache, because we can load all the info about a particular
1564  * opclass in a single indexscan of pg_amproc.
1565  *
1566  * The information from pg_am about expected range of support function
1567  * numbers is passed in, rather than being looked up, mainly because the
1568  * caller will have it already.
1569  *
1570  * Note there is no provision for flushing the cache. This is OK at the
1571  * moment because there is no way to ALTER any interesting properties of an
1572  * existing opclass --- all you can do is drop it, which will result in
1573  * a useless but harmless dead entry in the cache. To support altering
1574  * opclass membership (not the same as opfamily membership!), we'd need to
1575  * be able to flush this cache as well as the contents of relcache entries
1576  * for indexes.
1577  */
1578 static OpClassCacheEnt *
1579 LookupOpclassInfo(Oid operatorClassOid,
1580  StrategyNumber numSupport)
1581 {
1582  OpClassCacheEnt *opcentry;
1583  bool found;
1584  Relation rel;
1585  SysScanDesc scan;
1586  ScanKeyData skey[3];
1587  HeapTuple htup;
1588  bool indexOK;
1589 
1590  if (OpClassCache == NULL)
1591  {
1592  /* First time through: initialize the opclass cache */
1593  HASHCTL ctl;
1594 
1595  ctl.keysize = sizeof(Oid);
1596  ctl.entrysize = sizeof(OpClassCacheEnt);
1597  OpClassCache = hash_create("Operator class cache", 64,
1598  &ctl, HASH_ELEM | HASH_BLOBS);
1599 
1600  /* Also make sure CacheMemoryContext exists */
1601  if (!CacheMemoryContext)
1603  }
1604 
1605  opcentry = (OpClassCacheEnt *) hash_search(OpClassCache,
1606  (void *) &operatorClassOid,
1607  HASH_ENTER, &found);
1608 
1609  if (!found)
1610  {
1611  /* Need to allocate memory for new entry */
1612  opcentry->valid = false; /* until known OK */
1613  opcentry->numSupport = numSupport;
1614 
1615  if (numSupport > 0)
1616  opcentry->supportProcs = (RegProcedure *)
1618  (numSupport + 1) * sizeof(RegProcedure));
1619  else
1620  opcentry->supportProcs = NULL;
1621  }
1622  else
1623  {
1624  Assert(numSupport == opcentry->numSupport);
1625  }
1626 
1627  /*
1628  * When testing for cache-flush hazards, we intentionally disable the
1629  * operator class cache and force reloading of the info on each call. This
1630  * is helpful because we want to test the case where a cache flush occurs
1631  * while we are loading the info, and it's very hard to provoke that if
1632  * this happens only once per opclass per backend.
1633  */
1634 #ifdef CLOBBER_CACHE_ENABLED
1636  opcentry->valid = false;
1637 #endif
1638 
1639  if (opcentry->valid)
1640  return opcentry;
1641 
1642  /*
1643  * Need to fill in new entry.
1644  *
1645  * To avoid infinite recursion during startup, force heap scans if we're
1646  * looking up info for the opclasses used by the indexes we would like to
1647  * reference here.
1648  */
1649  indexOK = criticalRelcachesBuilt ||
1650  (operatorClassOid != OID_BTREE_OPS_OID &&
1651  operatorClassOid != INT2_BTREE_OPS_OID);
1652 
1653  /*
1654  * We have to fetch the pg_opclass row to determine its opfamily and
1655  * opcintype, which are needed to look up related operators and functions.
1656  * It'd be convenient to use the syscache here, but that probably doesn't
1657  * work while bootstrapping.
1658  */
1659  ScanKeyInit(&skey[0],
1660  Anum_pg_opclass_oid,
1661  BTEqualStrategyNumber, F_OIDEQ,
1662  ObjectIdGetDatum(operatorClassOid));
1663  rel = table_open(OperatorClassRelationId, AccessShareLock);
1664  scan = systable_beginscan(rel, OpclassOidIndexId, indexOK,
1665  NULL, 1, skey);
1666 
1667  if (HeapTupleIsValid(htup = systable_getnext(scan)))
1668  {
1669  Form_pg_opclass opclassform = (Form_pg_opclass) GETSTRUCT(htup);
1670 
1671  opcentry->opcfamily = opclassform->opcfamily;
1672  opcentry->opcintype = opclassform->opcintype;
1673  }
1674  else
1675  elog(ERROR, "could not find tuple for opclass %u", operatorClassOid);
1676 
1677  systable_endscan(scan);
1679 
1680  /*
1681  * Scan pg_amproc to obtain support procs for the opclass. We only fetch
1682  * the default ones (those with lefttype = righttype = opcintype).
1683  */
1684  if (numSupport > 0)
1685  {
1686  ScanKeyInit(&skey[0],
1687  Anum_pg_amproc_amprocfamily,
1688  BTEqualStrategyNumber, F_OIDEQ,
1689  ObjectIdGetDatum(opcentry->opcfamily));
1690  ScanKeyInit(&skey[1],
1691  Anum_pg_amproc_amproclefttype,
1692  BTEqualStrategyNumber, F_OIDEQ,
1693  ObjectIdGetDatum(opcentry->opcintype));
1694  ScanKeyInit(&skey[2],
1695  Anum_pg_amproc_amprocrighttype,
1696  BTEqualStrategyNumber, F_OIDEQ,
1697  ObjectIdGetDatum(opcentry->opcintype));
1698  rel = table_open(AccessMethodProcedureRelationId, AccessShareLock);
1699  scan = systable_beginscan(rel, AccessMethodProcedureIndexId, indexOK,
1700  NULL, 3, skey);
1701 
1702  while (HeapTupleIsValid(htup = systable_getnext(scan)))
1703  {
1704  Form_pg_amproc amprocform = (Form_pg_amproc) GETSTRUCT(htup);
1705 
1706  if (amprocform->amprocnum <= 0 ||
1707  (StrategyNumber) amprocform->amprocnum > numSupport)
1708  elog(ERROR, "invalid amproc number %d for opclass %u",
1709  amprocform->amprocnum, operatorClassOid);
1710 
1711  opcentry->supportProcs[amprocform->amprocnum - 1] =
1712  amprocform->amproc;
1713  }
1714 
1715  systable_endscan(scan);
1717  }
1718 
1719  opcentry->valid = true;
1720  return opcentry;
1721 }
1722 
1723 /*
1724  * Fill in the TableAmRoutine for a relation
1725  *
1726  * relation's rd_amhandler must be valid already.
1727  */
1728 static void
1730 {
1731  relation->rd_tableam = GetTableAmRoutine(relation->rd_amhandler);
1732 }
1733 
1734 /*
1735  * Initialize table access method support for a table like relation
1736  */
1737 void
1739 {
1740  HeapTuple tuple;
1741  Form_pg_am aform;
1742 
1743  if (relation->rd_rel->relkind == RELKIND_SEQUENCE)
1744  {
1745  /*
1746  * Sequences are currently accessed like heap tables, but it doesn't
1747  * seem prudent to show that in the catalog. So just overwrite it
1748  * here.
1749  */
1750  relation->rd_amhandler = F_HEAP_TABLEAM_HANDLER;
1751  }
1752  else if (IsCatalogRelation(relation))
1753  {
1754  /*
1755  * Avoid doing a syscache lookup for catalog tables.
1756  */
1757  Assert(relation->rd_rel->relam == HEAP_TABLE_AM_OID);
1758  relation->rd_amhandler = F_HEAP_TABLEAM_HANDLER;
1759  }
1760  else
1761  {
1762  /*
1763  * Look up the table access method, save the OID of its handler
1764  * function.
1765  */
1766  Assert(relation->rd_rel->relam != InvalidOid);
1767  tuple = SearchSysCache1(AMOID,
1768  ObjectIdGetDatum(relation->rd_rel->relam));
1769  if (!HeapTupleIsValid(tuple))
1770  elog(ERROR, "cache lookup failed for access method %u",
1771  relation->rd_rel->relam);
1772  aform = (Form_pg_am) GETSTRUCT(tuple);
1773  relation->rd_amhandler = aform->amhandler;
1774  ReleaseSysCache(tuple);
1775  }
1776 
1777  /*
1778  * Now we can fetch the table AM's API struct
1779  */
1780  InitTableAmRoutine(relation);
1781 }
1782 
1783 /*
1784  * formrdesc
1785  *
1786  * This is a special cut-down version of RelationBuildDesc(),
1787  * used while initializing the relcache.
1788  * The relation descriptor is built just from the supplied parameters,
1789  * without actually looking at any system table entries. We cheat
1790  * quite a lot since we only need to work for a few basic system
1791  * catalogs.
1792  *
1793  * The catalogs this is used for can't have constraints (except attnotnull),
1794  * default values, rules, or triggers, since we don't cope with any of that.
1795  * (Well, actually, this only matters for properties that need to be valid
1796  * during bootstrap or before RelationCacheInitializePhase3 runs, and none of
1797  * these properties matter then...)
1798  *
1799  * NOTE: we assume we are already switched into CacheMemoryContext.
1800  */
1801 static void
1802 formrdesc(const char *relationName, Oid relationReltype,
1803  bool isshared,
1804  int natts, const FormData_pg_attribute *attrs)
1805 {
1806  Relation relation;
1807  int i;
1808  bool has_not_null;
1809 
1810  /*
1811  * allocate new relation desc, clear all fields of reldesc
1812  */
1813  relation = (Relation) palloc0(sizeof(RelationData));
1814 
1815  /* make sure relation is marked as having no open file yet */
1816  relation->rd_smgr = NULL;
1817 
1818  /*
1819  * initialize reference count: 1 because it is nailed in cache
1820  */
1821  relation->rd_refcnt = 1;
1822 
1823  /*
1824  * all entries built with this routine are nailed-in-cache; none are for
1825  * new or temp relations.
1826  */
1827  relation->rd_isnailed = true;
1832  relation->rd_backend = InvalidBackendId;
1833  relation->rd_islocaltemp = false;
1834 
1835  /*
1836  * initialize relation tuple form
1837  *
1838  * The data we insert here is pretty incomplete/bogus, but it'll serve to
1839  * get us launched. RelationCacheInitializePhase3() will read the real
1840  * data from pg_class and replace what we've done here. Note in
1841  * particular that relowner is left as zero; this cues
1842  * RelationCacheInitializePhase3 that the real data isn't there yet.
1843  */
1845 
1846  namestrcpy(&relation->rd_rel->relname, relationName);
1847  relation->rd_rel->relnamespace = PG_CATALOG_NAMESPACE;
1848  relation->rd_rel->reltype = relationReltype;
1849 
1850  /*
1851  * It's important to distinguish between shared and non-shared relations,
1852  * even at bootstrap time, to make sure we know where they are stored.
1853  */
1854  relation->rd_rel->relisshared = isshared;
1855  if (isshared)
1856  relation->rd_rel->reltablespace = GLOBALTABLESPACE_OID;
1857 
1858  /* formrdesc is used only for permanent relations */
1859  relation->rd_rel->relpersistence = RELPERSISTENCE_PERMANENT;
1860 
1861  /* ... and they're always populated, too */
1862  relation->rd_rel->relispopulated = true;
1863 
1864  relation->rd_rel->relreplident = REPLICA_IDENTITY_NOTHING;
1865  relation->rd_rel->relpages = 0;
1866  relation->rd_rel->reltuples = -1;
1867  relation->rd_rel->relallvisible = 0;
1868  relation->rd_rel->relkind = RELKIND_RELATION;
1869  relation->rd_rel->relnatts = (int16) natts;
1870  relation->rd_rel->relam = HEAP_TABLE_AM_OID;
1871 
1872  /*
1873  * initialize attribute tuple form
1874  *
1875  * Unlike the case with the relation tuple, this data had better be right
1876  * because it will never be replaced. The data comes from
1877  * src/include/catalog/ headers via genbki.pl.
1878  */
1879  relation->rd_att = CreateTemplateTupleDesc(natts);
1880  relation->rd_att->tdrefcount = 1; /* mark as refcounted */
1881 
1882  relation->rd_att->tdtypeid = relationReltype;
1883  relation->rd_att->tdtypmod = -1; /* just to be sure */
1884 
1885  /*
1886  * initialize tuple desc info
1887  */
1888  has_not_null = false;
1889  for (i = 0; i < natts; i++)
1890  {
1891  memcpy(TupleDescAttr(relation->rd_att, i),
1892  &attrs[i],
1894  has_not_null |= attrs[i].attnotnull;
1895  /* make sure attcacheoff is valid */
1896  TupleDescAttr(relation->rd_att, i)->attcacheoff = -1;
1897  }
1898 
1899  /* initialize first attribute's attcacheoff, cf RelationBuildTupleDesc */
1900  TupleDescAttr(relation->rd_att, 0)->attcacheoff = 0;
1901 
1902  /* mark not-null status */
1903  if (has_not_null)
1904  {
1905  TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr));
1906 
1907  constr->has_not_null = true;
1908  relation->rd_att->constr = constr;
1909  }
1910 
1911  /*
1912  * initialize relation id from info in att array (my, this is ugly)
1913  */
1914  RelationGetRelid(relation) = TupleDescAttr(relation->rd_att, 0)->attrelid;
1915 
1916  /*
1917  * All relations made with formrdesc are mapped. This is necessarily so
1918  * because there is no other way to know what filenode they currently
1919  * have. In bootstrap mode, add them to the initial relation mapper data,
1920  * specifying that the initial filenode is the same as the OID.
1921  */
1922  relation->rd_rel->relfilenode = InvalidOid;
1925  RelationGetRelid(relation),
1926  isshared, true);
1927 
1928  /*
1929  * initialize the relation lock manager information
1930  */
1931  RelationInitLockInfo(relation); /* see lmgr.c */
1932 
1933  /*
1934  * initialize physical addressing information for the relation
1935  */
1936  RelationInitPhysicalAddr(relation);
1937 
1938  /*
1939  * initialize the table am handler
1940  */
1941  relation->rd_rel->relam = HEAP_TABLE_AM_OID;
1942  relation->rd_tableam = GetHeapamTableAmRoutine();
1943 
1944  /*
1945  * initialize the rel-has-index flag, using hardwired knowledge
1946  */
1948  {
1949  /* In bootstrap mode, we have no indexes */
1950  relation->rd_rel->relhasindex = false;
1951  }
1952  else
1953  {
1954  /* Otherwise, all the rels formrdesc is used for have indexes */
1955  relation->rd_rel->relhasindex = true;
1956  }
1957 
1958  /*
1959  * add new reldesc to relcache
1960  */
1961  RelationCacheInsert(relation, false);
1962 
1963  /* It's fully valid */
1964  relation->rd_isvalid = true;
1965 }
1966 
1967 
1968 /* ----------------------------------------------------------------
1969  * Relation Descriptor Lookup Interface
1970  * ----------------------------------------------------------------
1971  */
1972 
1973 /*
1974  * RelationIdGetRelation
1975  *
1976  * Lookup a reldesc by OID; make one if not already in cache.
1977  *
1978  * Returns NULL if no pg_class row could be found for the given relid
1979  * (suggesting we are trying to access a just-deleted relation).
1980  * Any other error is reported via elog.
1981  *
1982  * NB: caller should already have at least AccessShareLock on the
1983  * relation ID, else there are nasty race conditions.
1984  *
1985  * NB: relation ref count is incremented, or set to 1 if new entry.
1986  * Caller should eventually decrement count. (Usually,
1987  * that happens by calling RelationClose().)
1988  */
1989 Relation
1991 {
1992  Relation rd;
1993 
1994  /* Make sure we're in an xact, even if this ends up being a cache hit */
1996 
1997  /*
1998  * first try to find reldesc in the cache
1999  */
2000  RelationIdCacheLookup(relationId, rd);
2001 
2002  if (RelationIsValid(rd))
2003  {
2004  /* return NULL for dropped relations */
2006  {
2007  Assert(!rd->rd_isvalid);
2008  return NULL;
2009  }
2010 
2012  /* revalidate cache entry if necessary */
2013  if (!rd->rd_isvalid)
2014  {
2015  /*
2016  * Indexes only have a limited number of possible schema changes,
2017  * and we don't want to use the full-blown procedure because it's
2018  * a headache for indexes that reload itself depends on.
2019  */
2020  if (rd->rd_rel->relkind == RELKIND_INDEX ||
2021  rd->rd_rel->relkind == RELKIND_PARTITIONED_INDEX)
2023  else
2024  RelationClearRelation(rd, true);
2025 
2026  /*
2027  * Normally entries need to be valid here, but before the relcache
2028  * has been initialized, not enough infrastructure exists to
2029  * perform pg_class lookups. The structure of such entries doesn't
2030  * change, but we still want to update the rd_rel entry. So
2031  * rd_isvalid = false is left in place for a later lookup.
2032  */
2033  Assert(rd->rd_isvalid ||
2035  }
2036  return rd;
2037  }
2038 
2039  /*
2040  * no reldesc in the cache, so have RelationBuildDesc() build one and add
2041  * it.
2042  */
2043  rd = RelationBuildDesc(relationId, true);
2044  if (RelationIsValid(rd))
2046  return rd;
2047 }
2048 
2049 /* ----------------------------------------------------------------
2050  * cache invalidation support routines
2051  * ----------------------------------------------------------------
2052  */
2053 
2054 /*
2055  * RelationIncrementReferenceCount
2056  * Increments relation reference count.
2057  *
2058  * Note: bootstrap mode has its own weird ideas about relation refcount
2059  * behavior; we ought to fix it someday, but for now, just disable
2060  * reference count ownership tracking in bootstrap mode.
2061  */
2062 void
2064 {
2066  rel->rd_refcnt += 1;
2069 }
2070 
2071 /*
2072  * RelationDecrementReferenceCount
2073  * Decrements relation reference count.
2074  */
2075 void
2077 {
2078  Assert(rel->rd_refcnt > 0);
2079  rel->rd_refcnt -= 1;
2082 }
2083 
2084 /*
2085  * RelationClose - close an open relation
2086  *
2087  * Actually, we just decrement the refcount.
2088  *
2089  * NOTE: if compiled with -DRELCACHE_FORCE_RELEASE then relcache entries
2090  * will be freed as soon as their refcount goes to zero. In combination
2091  * with aset.c's CLOBBER_FREED_MEMORY option, this provides a good test
2092  * to catch references to already-released relcache entries. It slows
2093  * things down quite a bit, however.
2094  */
2095 void
2097 {
2098  /* Note: no locking manipulations needed */
2100 
2101  /*
2102  * If the relation is no longer open in this session, we can clean up any
2103  * stale partition descriptors it has. This is unlikely, so check to see
2104  * if there are child contexts before expending a call to mcxt.c.
2105  */
2106  if (RelationHasReferenceCountZero(relation) &&
2107  relation->rd_pdcxt != NULL &&
2108  relation->rd_pdcxt->firstchild != NULL)
2110 
2111 #ifdef RELCACHE_FORCE_RELEASE
2112  if (RelationHasReferenceCountZero(relation) &&
2113  relation->rd_createSubid == InvalidSubTransactionId &&
2115  RelationClearRelation(relation, false);
2116 #endif
2117 }
2118 
2119 /*
2120  * RelationReloadIndexInfo - reload minimal information for an open index
2121  *
2122  * This function is used only for indexes. A relcache inval on an index
2123  * can mean that its pg_class or pg_index row changed. There are only
2124  * very limited changes that are allowed to an existing index's schema,
2125  * so we can update the relcache entry without a complete rebuild; which
2126  * is fortunate because we can't rebuild an index entry that is "nailed"
2127  * and/or in active use. We support full replacement of the pg_class row,
2128  * as well as updates of a few simple fields of the pg_index row.
2129  *
2130  * We can't necessarily reread the catalog rows right away; we might be
2131  * in a failed transaction when we receive the SI notification. If so,
2132  * RelationClearRelation just marks the entry as invalid by setting
2133  * rd_isvalid to false. This routine is called to fix the entry when it
2134  * is next needed.
2135  *
2136  * We assume that at the time we are called, we have at least AccessShareLock
2137  * on the target index. (Note: in the calls from RelationClearRelation,
2138  * this is legitimate because we know the rel has positive refcount.)
2139  *
2140  * If the target index is an index on pg_class or pg_index, we'd better have
2141  * previously gotten at least AccessShareLock on its underlying catalog,
2142  * else we are at risk of deadlock against someone trying to exclusive-lock
2143  * the heap and index in that order. This is ensured in current usage by
2144  * only applying this to indexes being opened or having positive refcount.
2145  */
2146 static void
2148 {
2149  bool indexOK;
2150  HeapTuple pg_class_tuple;
2151  Form_pg_class relp;
2152 
2153  /* Should be called only for invalidated, live indexes */
2154  Assert((relation->rd_rel->relkind == RELKIND_INDEX ||
2155  relation->rd_rel->relkind == RELKIND_PARTITIONED_INDEX) &&
2156  !relation->rd_isvalid &&
2158 
2159  /* Ensure it's closed at smgr level */
2160  RelationCloseSmgr(relation);
2161 
2162  /* Must free any AM cached data upon relcache flush */
2163  if (relation->rd_amcache)
2164  pfree(relation->rd_amcache);
2165  relation->rd_amcache = NULL;
2166 
2167  /*
2168  * If it's a shared index, we might be called before backend startup has
2169  * finished selecting a database, in which case we have no way to read
2170  * pg_class yet. However, a shared index can never have any significant
2171  * schema updates, so it's okay to ignore the invalidation signal. Just
2172  * mark it valid and return without doing anything more.
2173  */
2174  if (relation->rd_rel->relisshared && !criticalRelcachesBuilt)
2175  {
2176  relation->rd_isvalid = true;
2177  return;
2178  }
2179 
2180  /*
2181  * Read the pg_class row
2182  *
2183  * Don't try to use an indexscan of pg_class_oid_index to reload the info
2184  * for pg_class_oid_index ...
2185  */
2186  indexOK = (RelationGetRelid(relation) != ClassOidIndexId);
2187  pg_class_tuple = ScanPgRelation(RelationGetRelid(relation), indexOK, false);
2188  if (!HeapTupleIsValid(pg_class_tuple))
2189  elog(ERROR, "could not find pg_class tuple for index %u",
2190  RelationGetRelid(relation));
2191  relp = (Form_pg_class) GETSTRUCT(pg_class_tuple);
2192  memcpy(relation->rd_rel, relp, CLASS_TUPLE_SIZE);
2193  /* Reload reloptions in case they changed */
2194  if (relation->rd_options)
2195  pfree(relation->rd_options);
2196  RelationParseRelOptions(relation, pg_class_tuple);
2197  /* done with pg_class tuple */
2198  heap_freetuple(pg_class_tuple);
2199  /* We must recalculate physical address in case it changed */
2200  RelationInitPhysicalAddr(relation);
2201 
2202  /*
2203  * For a non-system index, there are fields of the pg_index row that are
2204  * allowed to change, so re-read that row and update the relcache entry.
2205  * Most of the info derived from pg_index (such as support function lookup
2206  * info) cannot change, and indeed the whole point of this routine is to
2207  * update the relcache entry without clobbering that data; so wholesale
2208  * replacement is not appropriate.
2209  */
2210  if (!IsSystemRelation(relation))
2211  {
2212  HeapTuple tuple;
2214 
2215  tuple = SearchSysCache1(INDEXRELID,
2216  ObjectIdGetDatum(RelationGetRelid(relation)));
2217  if (!HeapTupleIsValid(tuple))
2218  elog(ERROR, "cache lookup failed for index %u",
2219  RelationGetRelid(relation));
2220  index = (Form_pg_index) GETSTRUCT(tuple);
2221 
2222  /*
2223  * Basically, let's just copy all the bool fields. There are one or
2224  * two of these that can't actually change in the current code, but
2225  * it's not worth it to track exactly which ones they are. None of
2226  * the array fields are allowed to change, though.
2227  */
2228  relation->rd_index->indisunique = index->indisunique;
2229  relation->rd_index->indisprimary = index->indisprimary;
2230  relation->rd_index->indisexclusion = index->indisexclusion;
2231  relation->rd_index->indimmediate = index->indimmediate;
2232  relation->rd_index->indisclustered = index->indisclustered;
2233  relation->rd_index->indisvalid = index->indisvalid;
2234  relation->rd_index->indcheckxmin = index->indcheckxmin;
2235  relation->rd_index->indisready = index->indisready;
2236  relation->rd_index->indislive = index->indislive;
2237 
2238  /* Copy xmin too, as that is needed to make sense of indcheckxmin */
2240  HeapTupleHeaderGetXmin(tuple->t_data));
2241 
2242  ReleaseSysCache(tuple);
2243  }
2244 
2245  /* Okay, now it's valid again */
2246  relation->rd_isvalid = true;
2247 }
2248 
2249 /*
2250  * RelationReloadNailed - reload minimal information for nailed relations.
2251  *
2252  * The structure of a nailed relation can never change (which is good, because
2253  * we rely on knowing their structure to be able to read catalog content). But
2254  * some parts, e.g. pg_class.relfrozenxid, are still important to have
2255  * accurate content for. Therefore those need to be reloaded after the arrival
2256  * of invalidations.
2257  */
2258 static void
2260 {
2261  Assert(relation->rd_isnailed);
2262 
2263  /*
2264  * Redo RelationInitPhysicalAddr in case it is a mapped relation whose
2265  * mapping changed.
2266  */
2267  RelationInitPhysicalAddr(relation);
2268 
2269  /* flag as needing to be revalidated */
2270  relation->rd_isvalid = false;
2271 
2272  /*
2273  * Can only reread catalog contents if in a transaction. If the relation
2274  * is currently open (not counting the nailed refcount), do so
2275  * immediately. Otherwise we've already marked the entry as possibly
2276  * invalid, and it'll be fixed when next opened.
2277  */
2278  if (!IsTransactionState() || relation->rd_refcnt <= 1)
2279  return;
2280 
2281  if (relation->rd_rel->relkind == RELKIND_INDEX)
2282  {
2283  /*
2284  * If it's a nailed-but-not-mapped index, then we need to re-read the
2285  * pg_class row to see if its relfilenode changed.
2286  */
2287  RelationReloadIndexInfo(relation);
2288  }
2289  else
2290  {
2291  /*
2292  * Reload a non-index entry. We can't easily do so if relcaches
2293  * aren't yet built, but that's fine because at that stage the
2294  * attributes that need to be current (like relfrozenxid) aren't yet
2295  * accessed. To ensure the entry will later be revalidated, we leave
2296  * it in invalid state, but allow use (cf. RelationIdGetRelation()).
2297  */
2299  {
2300  HeapTuple pg_class_tuple;
2301  Form_pg_class relp;
2302 
2303  /*
2304  * NB: Mark the entry as valid before starting to scan, to avoid
2305  * self-recursion when re-building pg_class.
2306  */
2307  relation->rd_isvalid = true;
2308 
2309  pg_class_tuple = ScanPgRelation(RelationGetRelid(relation),
2310  true, false);
2311  relp = (Form_pg_class) GETSTRUCT(pg_class_tuple);
2312  memcpy(relation->rd_rel, relp, CLASS_TUPLE_SIZE);
2313  heap_freetuple(pg_class_tuple);
2314 
2315  /*
2316  * Again mark as valid, to protect against concurrently arriving
2317  * invalidations.
2318  */
2319  relation->rd_isvalid = true;
2320  }
2321  }
2322 }
2323 
2324 /*
2325  * RelationDestroyRelation
2326  *
2327  * Physically delete a relation cache entry and all subsidiary data.
2328  * Caller must already have unhooked the entry from the hash table.
2329  */
2330 static void
2331 RelationDestroyRelation(Relation relation, bool remember_tupdesc)
2332 {
2334 
2335  /*
2336  * Make sure smgr and lower levels close the relation's files, if they
2337  * weren't closed already. (This was probably done by caller, but let's
2338  * just be real sure.)
2339  */
2340  RelationCloseSmgr(relation);
2341 
2342  /*
2343  * Free all the subsidiary data structures of the relcache entry, then the
2344  * entry itself.
2345  */
2346  if (relation->rd_rel)
2347  pfree(relation->rd_rel);
2348  /* can't use DecrTupleDescRefCount here */
2349  Assert(relation->rd_att->tdrefcount > 0);
2350  if (--relation->rd_att->tdrefcount == 0)
2351  {
2352  /*
2353  * If we Rebuilt a relcache entry during a transaction then its
2354  * possible we did that because the TupDesc changed as the result of
2355  * an ALTER TABLE that ran at less than AccessExclusiveLock. It's
2356  * possible someone copied that TupDesc, in which case the copy would
2357  * point to free'd memory. So if we rebuild an entry we keep the
2358  * TupDesc around until end of transaction, to be safe.
2359  */
2360  if (remember_tupdesc)
2362  else
2363  FreeTupleDesc(relation->rd_att);
2364  }
2365  FreeTriggerDesc(relation->trigdesc);
2366  list_free_deep(relation->rd_fkeylist);
2367  list_free(relation->rd_indexlist);
2368  list_free(relation->rd_statlist);
2369  bms_free(relation->rd_indexattr);
2370  bms_free(relation->rd_keyattr);
2371  bms_free(relation->rd_pkattr);
2372  bms_free(relation->rd_idattr);
2373  if (relation->rd_pubactions)
2374  pfree(relation->rd_pubactions);
2375  if (relation->rd_options)
2376  pfree(relation->rd_options);
2377  if (relation->rd_indextuple)
2378  pfree(relation->rd_indextuple);
2379  if (relation->rd_amcache)
2380  pfree(relation->rd_amcache);
2381  if (relation->rd_fdwroutine)
2382  pfree(relation->rd_fdwroutine);
2383  if (relation->rd_indexcxt)
2384  MemoryContextDelete(relation->rd_indexcxt);
2385  if (relation->rd_rulescxt)
2386  MemoryContextDelete(relation->rd_rulescxt);
2387  if (relation->rd_rsdesc)
2388  MemoryContextDelete(relation->rd_rsdesc->rscxt);
2389  if (relation->rd_partkeycxt)
2391  if (relation->rd_pdcxt)
2392  MemoryContextDelete(relation->rd_pdcxt);
2393  if (relation->rd_partcheckcxt)
2395  pfree(relation);
2396 }
2397 
2398 /*
2399  * RelationClearRelation
2400  *
2401  * Physically blow away a relation cache entry, or reset it and rebuild
2402  * it from scratch (that is, from catalog entries). The latter path is
2403  * used when we are notified of a change to an open relation (one with
2404  * refcount > 0).
2405  *
2406  * NB: when rebuilding, we'd better hold some lock on the relation,
2407  * else the catalog data we need to read could be changing under us.
2408  * Also, a rel to be rebuilt had better have refcnt > 0. This is because
2409  * a sinval reset could happen while we're accessing the catalogs, and
2410  * the rel would get blown away underneath us by RelationCacheInvalidate
2411  * if it has zero refcnt.
2412  *
2413  * The "rebuild" parameter is redundant in current usage because it has
2414  * to match the relation's refcnt status, but we keep it as a crosscheck
2415  * that we're doing what the caller expects.
2416  */
2417 static void
2418 RelationClearRelation(Relation relation, bool rebuild)
2419 {
2420  /*
2421  * As per notes above, a rel to be rebuilt MUST have refcnt > 0; while of
2422  * course it would be an equally bad idea to blow away one with nonzero
2423  * refcnt, since that would leave someone somewhere with a dangling
2424  * pointer. All callers are expected to have verified that this holds.
2425  */
2426  Assert(rebuild ?
2427  !RelationHasReferenceCountZero(relation) :
2428  RelationHasReferenceCountZero(relation));
2429 
2430  /*
2431  * Make sure smgr and lower levels close the relation's files, if they
2432  * weren't closed already. If the relation is not getting deleted, the
2433  * next smgr access should reopen the files automatically. This ensures
2434  * that the low-level file access state is updated after, say, a vacuum
2435  * truncation.
2436  */
2437  RelationCloseSmgr(relation);
2438 
2439  /* Free AM cached data, if any */
2440  if (relation->rd_amcache)
2441  pfree(relation->rd_amcache);
2442  relation->rd_amcache = NULL;
2443 
2444  /*
2445  * Treat nailed-in system relations separately, they always need to be
2446  * accessible, so we can't blow them away.
2447  */
2448  if (relation->rd_isnailed)
2449  {
2450  RelationReloadNailed(relation);
2451  return;
2452  }
2453 
2454  /* Mark it invalid until we've finished rebuild */
2455  relation->rd_isvalid = false;
2456 
2457  /* See RelationForgetRelation(). */
2458  if (relation->rd_droppedSubid != InvalidSubTransactionId)
2459  return;
2460 
2461  /*
2462  * Even non-system indexes should not be blown away if they are open and
2463  * have valid index support information. This avoids problems with active
2464  * use of the index support information. As with nailed indexes, we
2465  * re-read the pg_class row to handle possible physical relocation of the
2466  * index, and we check for pg_index updates too.
2467  */
2468  if ((relation->rd_rel->relkind == RELKIND_INDEX ||
2469  relation->rd_rel->relkind == RELKIND_PARTITIONED_INDEX) &&
2470  relation->rd_refcnt > 0 &&
2471  relation->rd_indexcxt != NULL)
2472  {
2473  if (IsTransactionState())
2474  RelationReloadIndexInfo(relation);
2475  return;
2476  }
2477 
2478  /*
2479  * If we're really done with the relcache entry, blow it away. But if
2480  * someone is still using it, reconstruct the whole deal without moving
2481  * the physical RelationData record (so that the someone's pointer is
2482  * still valid).
2483  */
2484  if (!rebuild)
2485  {
2486  /* Remove it from the hash table */
2487  RelationCacheDelete(relation);
2488 
2489  /* And release storage */
2490  RelationDestroyRelation(relation, false);
2491  }
2492  else if (!IsTransactionState())
2493  {
2494  /*
2495  * If we're not inside a valid transaction, we can't do any catalog
2496  * access so it's not possible to rebuild yet. Just exit, leaving
2497  * rd_isvalid = false so that the rebuild will occur when the entry is
2498  * next opened.
2499  *
2500  * Note: it's possible that we come here during subtransaction abort,
2501  * and the reason for wanting to rebuild is that the rel is open in
2502  * the outer transaction. In that case it might seem unsafe to not
2503  * rebuild immediately, since whatever code has the rel already open
2504  * will keep on using the relcache entry as-is. However, in such a
2505  * case the outer transaction should be holding a lock that's
2506  * sufficient to prevent any significant change in the rel's schema,
2507  * so the existing entry contents should be good enough for its
2508  * purposes; at worst we might be behind on statistics updates or the
2509  * like. (See also CheckTableNotInUse() and its callers.) These same
2510  * remarks also apply to the cases above where we exit without having
2511  * done RelationReloadIndexInfo() yet.
2512  */
2513  return;
2514  }
2515  else
2516  {
2517  /*
2518  * Our strategy for rebuilding an open relcache entry is to build a
2519  * new entry from scratch, swap its contents with the old entry, and
2520  * finally delete the new entry (along with any infrastructure swapped
2521  * over from the old entry). This is to avoid trouble in case an
2522  * error causes us to lose control partway through. The old entry
2523  * will still be marked !rd_isvalid, so we'll try to rebuild it again
2524  * on next access. Meanwhile it's not any less valid than it was
2525  * before, so any code that might expect to continue accessing it
2526  * isn't hurt by the rebuild failure. (Consider for example a
2527  * subtransaction that ALTERs a table and then gets canceled partway
2528  * through the cache entry rebuild. The outer transaction should
2529  * still see the not-modified cache entry as valid.) The worst
2530  * consequence of an error is leaking the necessarily-unreferenced new
2531  * entry, and this shouldn't happen often enough for that to be a big
2532  * problem.
2533  *
2534  * When rebuilding an open relcache entry, we must preserve ref count,
2535  * rd_*Subid, and rd_toastoid state. Also attempt to preserve the
2536  * pg_class entry (rd_rel), tupledesc, rewrite-rule, partition key,
2537  * and partition descriptor substructures in place, because various
2538  * places assume that these structures won't move while they are
2539  * working with an open relcache entry. (Note: the refcount
2540  * mechanism for tupledescs might someday allow us to remove this hack
2541  * for the tupledesc.)
2542  *
2543  * Note that this process does not touch CurrentResourceOwner; which
2544  * is good because whatever ref counts the entry may have do not
2545  * necessarily belong to that resource owner.
2546  */
2547  Relation newrel;
2548  Oid save_relid = RelationGetRelid(relation);
2549  bool keep_tupdesc;
2550  bool keep_rules;
2551  bool keep_policies;
2552  bool keep_partkey;
2553 
2554  /* Build temporary entry, but don't link it into hashtable */
2555  newrel = RelationBuildDesc(save_relid, false);
2556  if (newrel == NULL)
2557  {
2558  /*
2559  * We can validly get here, if we're using a historic snapshot in
2560  * which a relation, accessed from outside logical decoding, is
2561  * still invisible. In that case it's fine to just mark the
2562  * relation as invalid and return - it'll fully get reloaded by
2563  * the cache reset at the end of logical decoding (or at the next
2564  * access). During normal processing we don't want to ignore this
2565  * case as it shouldn't happen there, as explained below.
2566  */
2567  if (HistoricSnapshotActive())
2568  return;
2569 
2570  /*
2571  * This shouldn't happen as dropping a relation is intended to be
2572  * impossible if still referenced (cf. CheckTableNotInUse()). But
2573  * if we get here anyway, we can't just delete the relcache entry,
2574  * as it possibly could get accessed later (as e.g. the error
2575  * might get trapped and handled via a subtransaction rollback).
2576  */
2577  elog(ERROR, "relation %u deleted while still in use", save_relid);
2578  }
2579 
2580  keep_tupdesc = equalTupleDescs(relation->rd_att, newrel->rd_att);
2581  keep_rules = equalRuleLocks(relation->rd_rules, newrel->rd_rules);
2582  keep_policies = equalRSDesc(relation->rd_rsdesc, newrel->rd_rsdesc);
2583  /* partkey is immutable once set up, so we can always keep it */
2584  keep_partkey = (relation->rd_partkey != NULL);
2585 
2586  /*
2587  * Perform swapping of the relcache entry contents. Within this
2588  * process the old entry is momentarily invalid, so there *must* be no
2589  * possibility of CHECK_FOR_INTERRUPTS within this sequence. Do it in
2590  * all-in-line code for safety.
2591  *
2592  * Since the vast majority of fields should be swapped, our method is
2593  * to swap the whole structures and then re-swap those few fields we
2594  * didn't want swapped.
2595  */
2596 #define SWAPFIELD(fldtype, fldname) \
2597  do { \
2598  fldtype _tmp = newrel->fldname; \
2599  newrel->fldname = relation->fldname; \
2600  relation->fldname = _tmp; \
2601  } while (0)
2602 
2603  /* swap all Relation struct fields */
2604  {
2605  RelationData tmpstruct;
2606 
2607  memcpy(&tmpstruct, newrel, sizeof(RelationData));
2608  memcpy(newrel, relation, sizeof(RelationData));
2609  memcpy(relation, &tmpstruct, sizeof(RelationData));
2610  }
2611 
2612  /* rd_smgr must not be swapped, due to back-links from smgr level */
2613  SWAPFIELD(SMgrRelation, rd_smgr);
2614  /* rd_refcnt must be preserved */
2615  SWAPFIELD(int, rd_refcnt);
2616  /* isnailed shouldn't change */
2617  Assert(newrel->rd_isnailed == relation->rd_isnailed);
2618  /* creation sub-XIDs must be preserved */
2619  SWAPFIELD(SubTransactionId, rd_createSubid);
2620  SWAPFIELD(SubTransactionId, rd_newRelfilenodeSubid);
2621  SWAPFIELD(SubTransactionId, rd_firstRelfilenodeSubid);
2622  SWAPFIELD(SubTransactionId, rd_droppedSubid);
2623  /* un-swap rd_rel pointers, swap contents instead */
2624  SWAPFIELD(Form_pg_class, rd_rel);
2625  /* ... but actually, we don't have to update newrel->rd_rel */
2626  memcpy(relation->rd_rel, newrel->rd_rel, CLASS_TUPLE_SIZE);
2627  /* preserve old tupledesc, rules, policies if no logical change */
2628  if (keep_tupdesc)
2629  SWAPFIELD(TupleDesc, rd_att);
2630  if (keep_rules)
2631  {
2632  SWAPFIELD(RuleLock *, rd_rules);
2633  SWAPFIELD(MemoryContext, rd_rulescxt);
2634  }
2635  if (keep_policies)
2636  SWAPFIELD(RowSecurityDesc *, rd_rsdesc);
2637  /* toast OID override must be preserved */
2638  SWAPFIELD(Oid, rd_toastoid);
2639  /* pgstat_info must be preserved */
2640  SWAPFIELD(struct PgStat_TableStatus *, pgstat_info);
2641  /* preserve old partition key if we have one */
2642  if (keep_partkey)
2643  {
2644  SWAPFIELD(PartitionKey, rd_partkey);
2645  SWAPFIELD(MemoryContext, rd_partkeycxt);
2646  }
2647  if (newrel->rd_pdcxt != NULL)
2648  {
2649  /*
2650  * We are rebuilding a partitioned relation with a non-zero
2651  * reference count, so we must keep the old partition descriptor
2652  * around, in case there's a PartitionDirectory with a pointer to
2653  * it. This means we can't free the old rd_pdcxt yet. (This is
2654  * necessary because RelationGetPartitionDesc hands out direct
2655  * pointers to the relcache's data structure, unlike our usual
2656  * practice which is to hand out copies. We'd have the same
2657  * problem with rd_partkey, except that we always preserve that
2658  * once created.)
2659  *
2660  * To ensure that it's not leaked completely, re-attach it to the
2661  * new reldesc, or make it a child of the new reldesc's rd_pdcxt
2662  * in the unlikely event that there is one already. (Compare hack
2663  * in RelationBuildPartitionDesc.) RelationClose will clean up
2664  * any such contexts once the reference count reaches zero.
2665  *
2666  * In the case where the reference count is zero, this code is not
2667  * reached, which should be OK because in that case there should
2668  * be no PartitionDirectory with a pointer to the old entry.
2669  *
2670  * Note that newrel and relation have already been swapped, so the
2671  * "old" partition descriptor is actually the one hanging off of
2672  * newrel.
2673  */
2674  relation->rd_partdesc = NULL; /* ensure rd_partdesc is invalid */
2675  if (relation->rd_pdcxt != NULL) /* probably never happens */
2676  MemoryContextSetParent(newrel->rd_pdcxt, relation->rd_pdcxt);
2677  else
2678  relation->rd_pdcxt = newrel->rd_pdcxt;
2679  /* drop newrel's pointers so we don't destroy it below */
2680  newrel->rd_partdesc = NULL;
2681  newrel->rd_pdcxt = NULL;
2682  }
2683 
2684 #undef SWAPFIELD
2685 
2686  /* And now we can throw away the temporary entry */
2687  RelationDestroyRelation(newrel, !keep_tupdesc);
2688  }
2689 }
2690 
2691 /*
2692  * RelationFlushRelation
2693  *
2694  * Rebuild the relation if it is open (refcount > 0), else blow it away.
2695  * This is used when we receive a cache invalidation event for the rel.
2696  */
2697 static void
2699 {
2700  if (relation->rd_createSubid != InvalidSubTransactionId ||
2702  {
2703  /*
2704  * New relcache entries are always rebuilt, not flushed; else we'd
2705  * forget the "new" status of the relation. Ditto for the
2706  * new-relfilenode status.
2707  *
2708  * The rel could have zero refcnt here, so temporarily increment the
2709  * refcnt to ensure it's safe to rebuild it. We can assume that the
2710  * current transaction has some lock on the rel already.
2711  */
2713  RelationClearRelation(relation, true);
2715  }
2716  else
2717  {
2718  /*
2719  * Pre-existing rels can be dropped from the relcache if not open.
2720  */
2721  bool rebuild = !RelationHasReferenceCountZero(relation);
2722 
2723  RelationClearRelation(relation, rebuild);
2724  }
2725 }
2726 
2727 /*
2728  * RelationForgetRelation - caller reports that it dropped the relation
2729  */
2730 void
2732 {
2733  Relation relation;
2734 
2735  RelationIdCacheLookup(rid, relation);
2736 
2737  if (!PointerIsValid(relation))
2738  return; /* not in cache, nothing to do */
2739 
2740  if (!RelationHasReferenceCountZero(relation))
2741  elog(ERROR, "relation %u is still open", rid);
2742 
2744  if (relation->rd_createSubid != InvalidSubTransactionId ||
2746  {
2747  /*
2748  * In the event of subtransaction rollback, we must not forget
2749  * rd_*Subid. Mark the entry "dropped" so RelationClearRelation()
2750  * invalidates it in lieu of destroying it. (If we're in a top
2751  * transaction, we could opt to destroy the entry.)
2752  */
2754  }
2755 
2756  RelationClearRelation(relation, false);
2757 }
2758 
2759 /*
2760  * RelationCacheInvalidateEntry
2761  *
2762  * This routine is invoked for SI cache flush messages.
2763  *
2764  * Any relcache entry matching the relid must be flushed. (Note: caller has
2765  * already determined that the relid belongs to our database or is a shared
2766  * relation.)
2767  *
2768  * We used to skip local relations, on the grounds that they could
2769  * not be targets of cross-backend SI update messages; but it seems
2770  * safer to process them, so that our *own* SI update messages will
2771  * have the same effects during CommandCounterIncrement for both
2772  * local and nonlocal relations.
2773  */
2774 void
2776 {
2777  Relation relation;
2778 
2779  RelationIdCacheLookup(relationId, relation);
2780 
2781  if (PointerIsValid(relation))
2782  {
2784  RelationFlushRelation(relation);
2785  }
2786 }
2787 
2788 /*
2789  * RelationCacheInvalidate
2790  * Blow away cached relation descriptors that have zero reference counts,
2791  * and rebuild those with positive reference counts. Also reset the smgr
2792  * relation cache and re-read relation mapping data.
2793  *
2794  * This is currently used only to recover from SI message buffer overflow,
2795  * so we do not touch relations having new-in-transaction relfilenodes; they
2796  * cannot be targets of cross-backend SI updates (and our own updates now go
2797  * through a separate linked list that isn't limited by the SI message
2798  * buffer size).
2799  *
2800  * We do this in two phases: the first pass deletes deletable items, and
2801  * the second one rebuilds the rebuildable items. This is essential for
2802  * safety, because hash_seq_search only copes with concurrent deletion of
2803  * the element it is currently visiting. If a second SI overflow were to
2804  * occur while we are walking the table, resulting in recursive entry to
2805  * this routine, we could crash because the inner invocation blows away
2806  * the entry next to be visited by the outer scan. But this way is OK,
2807  * because (a) during the first pass we won't process any more SI messages,
2808  * so hash_seq_search will complete safely; (b) during the second pass we
2809  * only hold onto pointers to nondeletable entries.
2810  *
2811  * The two-phase approach also makes it easy to update relfilenodes for
2812  * mapped relations before we do anything else, and to ensure that the
2813  * second pass processes nailed-in-cache items before other nondeletable
2814  * items. This should ensure that system catalogs are up to date before
2815  * we attempt to use them to reload information about other open relations.
2816  */
2817 void
2819 {
2821  RelIdCacheEnt *idhentry;
2822  Relation relation;
2823  List *rebuildFirstList = NIL;
2824  List *rebuildList = NIL;
2825  ListCell *l;
2826 
2827  /*
2828  * Reload relation mapping data before starting to reconstruct cache.
2829  */
2831 
2832  /* Phase 1 */
2833  hash_seq_init(&status, RelationIdCache);
2834 
2835  while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
2836  {
2837  relation = idhentry->reldesc;
2838 
2839  /* Must close all smgr references to avoid leaving dangling ptrs */
2840  RelationCloseSmgr(relation);
2841 
2842  /*
2843  * Ignore new relations; no other backend will manipulate them before
2844  * we commit. Likewise, before replacing a relation's relfilenode, we
2845  * shall have acquired AccessExclusiveLock and drained any applicable
2846  * pending invalidations.
2847  */
2848  if (relation->rd_createSubid != InvalidSubTransactionId ||
2850  continue;
2851 
2853 
2854  if (RelationHasReferenceCountZero(relation))
2855  {
2856  /* Delete this entry immediately */
2857  Assert(!relation->rd_isnailed);
2858  RelationClearRelation(relation, false);
2859  }
2860  else
2861  {
2862  /*
2863  * If it's a mapped relation, immediately update its rd_node in
2864  * case its relfilenode changed. We must do this during phase 1
2865  * in case the relation is consulted during rebuild of other
2866  * relcache entries in phase 2. It's safe since consulting the
2867  * map doesn't involve any access to relcache entries.
2868  */
2869  if (RelationIsMapped(relation))
2870  RelationInitPhysicalAddr(relation);
2871 
2872  /*
2873  * Add this entry to list of stuff to rebuild in second pass.
2874  * pg_class goes to the front of rebuildFirstList while
2875  * pg_class_oid_index goes to the back of rebuildFirstList, so
2876  * they are done first and second respectively. Other nailed
2877  * relations go to the front of rebuildList, so they'll be done
2878  * next in no particular order; and everything else goes to the
2879  * back of rebuildList.
2880  */
2881  if (RelationGetRelid(relation) == RelationRelationId)
2882  rebuildFirstList = lcons(relation, rebuildFirstList);
2883  else if (RelationGetRelid(relation) == ClassOidIndexId)
2884  rebuildFirstList = lappend(rebuildFirstList, relation);
2885  else if (relation->rd_isnailed)
2886  rebuildList = lcons(relation, rebuildList);
2887  else
2888  rebuildList = lappend(rebuildList, relation);
2889  }
2890  }
2891 
2892  /*
2893  * Now zap any remaining smgr cache entries. This must happen before we
2894  * start to rebuild entries, since that may involve catalog fetches which
2895  * will re-open catalog files.
2896  */
2897  smgrcloseall();
2898 
2899  /* Phase 2: rebuild the items found to need rebuild in phase 1 */
2900  foreach(l, rebuildFirstList)
2901  {
2902  relation = (Relation) lfirst(l);
2903  RelationClearRelation(relation, true);
2904  }
2905  list_free(rebuildFirstList);
2906  foreach(l, rebuildList)
2907  {
2908  relation = (Relation) lfirst(l);
2909  RelationClearRelation(relation, true);
2910  }
2911  list_free(rebuildList);
2912 }
2913 
2914 /*
2915  * RelationCloseSmgrByOid - close a relcache entry's smgr link
2916  *
2917  * Needed in some cases where we are changing a relation's physical mapping.
2918  * The link will be automatically reopened on next use.
2919  */
2920 void
2922 {
2923  Relation relation;
2924 
2925  RelationIdCacheLookup(relationId, relation);
2926 
2927  if (!PointerIsValid(relation))
2928  return; /* not in cache, nothing to do */
2929 
2930  RelationCloseSmgr(relation);
2931 }
2932 
2933 static void
2935 {
2936  if (EOXactTupleDescArray == NULL)
2937  {
2938  MemoryContext oldcxt;
2939 
2941 
2942  EOXactTupleDescArray = (TupleDesc *) palloc(16 * sizeof(TupleDesc));
2945  MemoryContextSwitchTo(oldcxt);
2946  }
2948  {
2949  int32 newlen = EOXactTupleDescArrayLen * 2;
2950 
2952 
2953  EOXactTupleDescArray = (TupleDesc *) repalloc(EOXactTupleDescArray,
2954  newlen * sizeof(TupleDesc));
2955  EOXactTupleDescArrayLen = newlen;
2956  }
2957 
2958  EOXactTupleDescArray[NextEOXactTupleDescNum++] = td;
2959 }
2960 
2961 #ifdef USE_ASSERT_CHECKING
2962 static void
2963 AssertPendingSyncConsistency(Relation relation)
2964 {
2965  bool relcache_verdict =
2966  RelationIsPermanent(relation) &&
2967  ((relation->rd_createSubid != InvalidSubTransactionId &&
2968  RELKIND_HAS_STORAGE(relation->rd_rel->relkind)) ||
2970 
2971  Assert(relcache_verdict == RelFileNodeSkippingWAL(relation->rd_node));
2972 
2973  if (relation->rd_droppedSubid != InvalidSubTransactionId)
2974  Assert(!relation->rd_isvalid &&
2975  (relation->rd_createSubid != InvalidSubTransactionId ||
2977 }
2978 
2979 /*
2980  * AssertPendingSyncs_RelationCache
2981  *
2982  * Assert that relcache.c and storage.c agree on whether to skip WAL.
2983  */
2984 void
2986 {
2988  LOCALLOCK *locallock;
2989  Relation *rels;
2990  int maxrels;
2991  int nrels;
2992  RelIdCacheEnt *idhentry;
2993  int i;
2994 
2995  /*
2996  * Open every relation that this transaction has locked. If, for some
2997  * relation, storage.c is skipping WAL and relcache.c is not skipping WAL,
2998  * a CommandCounterIncrement() typically yields a local invalidation
2999  * message that destroys the relcache entry. By recreating such entries
3000  * here, we detect the problem.
3001  */
3003  maxrels = 1;
3004  rels = palloc(maxrels * sizeof(*rels));
3005  nrels = 0;
3006  hash_seq_init(&status, GetLockMethodLocalHash());
3007  while ((locallock = (LOCALLOCK *) hash_seq_search(&status)) != NULL)
3008  {
3009  Oid relid;
3010  Relation r;
3011 
3012  if (locallock->nLocks <= 0)
3013  continue;
3014  if ((LockTagType) locallock->tag.lock.locktag_type !=
3016  continue;
3017  relid = ObjectIdGetDatum(locallock->tag.lock.locktag_field2);
3018  r = RelationIdGetRelation(relid);
3019  if (!RelationIsValid(r))
3020  continue;
3021  if (nrels >= maxrels)
3022  {
3023  maxrels *= 2;
3024  rels = repalloc(rels, maxrels * sizeof(*rels));
3025  }
3026  rels[nrels++] = r;
3027  }
3028 
3029  hash_seq_init(&status, RelationIdCache);
3030  while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
3031  AssertPendingSyncConsistency(idhentry->reldesc);
3032 
3033  for (i = 0; i < nrels; i++)
3034  RelationClose(rels[i]);
3036 }
3037 #endif
3038 
3039 /*
3040  * AtEOXact_RelationCache
3041  *
3042  * Clean up the relcache at main-transaction commit or abort.
3043  *
3044  * Note: this must be called *before* processing invalidation messages.
3045  * In the case of abort, we don't want to try to rebuild any invalidated
3046  * cache entries (since we can't safely do database accesses). Therefore
3047  * we must reset refcnts before handling pending invalidations.
3048  *
3049  * As of PostgreSQL 8.1, relcache refcnts should get released by the
3050  * ResourceOwner mechanism. This routine just does a debugging
3051  * cross-check that no pins remain. However, we also need to do special
3052  * cleanup when the current transaction created any relations or made use
3053  * of forced index lists.
3054  */
3055 void
3057 {
3059  RelIdCacheEnt *idhentry;
3060  int i;
3061 
3062  /*
3063  * Unless the eoxact_list[] overflowed, we only need to examine the rels
3064  * listed in it. Otherwise fall back on a hash_seq_search scan.
3065  *
3066  * For simplicity, eoxact_list[] entries are not deleted till end of
3067  * top-level transaction, even though we could remove them at
3068  * subtransaction end in some cases, or remove relations from the list if
3069  * they are cleared for other reasons. Therefore we should expect the
3070  * case that list entries are not found in the hashtable; if not, there's
3071  * nothing to do for them.
3072  */
3074  {
3075  hash_seq_init(&status, RelationIdCache);
3076  while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
3077  {
3078  AtEOXact_cleanup(idhentry->reldesc, isCommit);
3079  }
3080  }
3081  else
3082  {
3083  for (i = 0; i < eoxact_list_len; i++)
3084  {
3085  idhentry = (RelIdCacheEnt *) hash_search(RelationIdCache,
3086  (void *) &eoxact_list[i],
3087  HASH_FIND,
3088  NULL);
3089  if (idhentry != NULL)
3090  AtEOXact_cleanup(idhentry->reldesc, isCommit);
3091  }
3092  }
3093 
3094  if (EOXactTupleDescArrayLen > 0)
3095  {
3096  Assert(EOXactTupleDescArray != NULL);
3097  for (i = 0; i < NextEOXactTupleDescNum; i++)
3098  FreeTupleDesc(EOXactTupleDescArray[i]);
3099  pfree(EOXactTupleDescArray);
3100  EOXactTupleDescArray = NULL;
3101  }
3102 
3103  /* Now we're out of the transaction and can clear the lists */
3104  eoxact_list_len = 0;
3105  eoxact_list_overflowed = false;
3108 }
3109 
3110 /*
3111  * AtEOXact_cleanup
3112  *
3113  * Clean up a single rel at main-transaction commit or abort
3114  *
3115  * NB: this processing must be idempotent, because EOXactListAdd() doesn't
3116  * bother to prevent duplicate entries in eoxact_list[].
3117  */
3118 static void
3119 AtEOXact_cleanup(Relation relation, bool isCommit)
3120 {
3121  bool clear_relcache = false;
3122 
3123  /*
3124  * The relcache entry's ref count should be back to its normal
3125  * not-in-a-transaction state: 0 unless it's nailed in cache.
3126  *
3127  * In bootstrap mode, this is NOT true, so don't check it --- the
3128  * bootstrap code expects relations to stay open across start/commit
3129  * transaction calls. (That seems bogus, but it's not worth fixing.)
3130  *
3131  * Note: ideally this check would be applied to every relcache entry, not
3132  * just those that have eoxact work to do. But it's not worth forcing a
3133  * scan of the whole relcache just for this. (Moreover, doing so would
3134  * mean that assert-enabled testing never tests the hash_search code path
3135  * above, which seems a bad idea.)
3136  */
3137 #ifdef USE_ASSERT_CHECKING
3139  {
3140  int expected_refcnt;
3141 
3142  expected_refcnt = relation->rd_isnailed ? 1 : 0;
3143  Assert(relation->rd_refcnt == expected_refcnt);
3144  }
3145 #endif
3146 
3147  /*
3148  * Is the relation live after this transaction ends?
3149  *
3150  * During commit, clear the relcache entry if it is preserved after
3151  * relation drop, in order not to orphan the entry. During rollback,
3152  * clear the relcache entry if the relation is created in the current
3153  * transaction since it isn't interesting any longer once we are out of
3154  * the transaction.
3155  */
3156  clear_relcache =
3157  (isCommit ?
3160 
3161  /*
3162  * Since we are now out of the transaction, reset the subids to zero. That
3163  * also lets RelationClearRelation() drop the relcache entry.
3164  */
3169 
3170  if (clear_relcache)
3171  {
3172  if (RelationHasReferenceCountZero(relation))
3173  {
3174  RelationClearRelation(relation, false);
3175  return;
3176  }
3177  else
3178  {
3179  /*
3180  * Hmm, somewhere there's a (leaked?) reference to the relation.
3181  * We daren't remove the entry for fear of dereferencing a
3182  * dangling pointer later. Bleat, and mark it as not belonging to
3183  * the current transaction. Hopefully it'll get cleaned up
3184  * eventually. This must be just a WARNING to avoid
3185  * error-during-error-recovery loops.
3186  */
3187  elog(WARNING, "cannot remove relcache entry for \"%s\" because it has nonzero refcount",
3188  RelationGetRelationName(relation));
3189  }
3190  }
3191 }
3192 
3193 /*
3194  * AtEOSubXact_RelationCache
3195  *
3196  * Clean up the relcache at sub-transaction commit or abort.
3197  *
3198  * Note: this must be called *before* processing invalidation messages.
3199  */
3200 void
3202  SubTransactionId parentSubid)
3203 {
3205  RelIdCacheEnt *idhentry;
3206  int i;
3207 
3208  /*
3209  * Unless the eoxact_list[] overflowed, we only need to examine the rels
3210  * listed in it. Otherwise fall back on a hash_seq_search scan. Same
3211  * logic as in AtEOXact_RelationCache.
3212  */
3214  {
3215  hash_seq_init(&status, RelationIdCache);
3216  while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
3217  {
3218  AtEOSubXact_cleanup(idhentry->reldesc, isCommit,
3219  mySubid, parentSubid);
3220  }
3221  }
3222  else
3223  {
3224  for (i = 0; i < eoxact_list_len; i++)
3225  {
3226  idhentry = (RelIdCacheEnt *) hash_search(RelationIdCache,
3227  (void *) &eoxact_list[i],
3228  HASH_FIND,
3229  NULL);
3230  if (idhentry != NULL)
3231  AtEOSubXact_cleanup(idhentry->reldesc, isCommit,
3232  mySubid, parentSubid);
3233  }
3234  }
3235 
3236  /* Don't reset the list; we still need more cleanup later */
3237 }
3238 
3239 /*
3240  * AtEOSubXact_cleanup
3241  *
3242  * Clean up a single rel at subtransaction commit or abort
3243  *
3244  * NB: this processing must be idempotent, because EOXactListAdd() doesn't
3245  * bother to prevent duplicate entries in eoxact_list[].
3246  */
3247 static void
3248 AtEOSubXact_cleanup(Relation relation, bool isCommit,
3249  SubTransactionId mySubid, SubTransactionId parentSubid)
3250 {
3251  /*
3252  * Is it a relation created in the current subtransaction?
3253  *
3254  * During subcommit, mark it as belonging to the parent, instead, as long
3255  * as it has not been dropped. Otherwise simply delete the relcache entry.
3256  * --- it isn't interesting any longer.
3257  */
3258  if (relation->rd_createSubid == mySubid)
3259  {
3260  /*
3261  * Valid rd_droppedSubid means the corresponding relation is dropped
3262  * but the relcache entry is preserved for at-commit pending sync. We
3263  * need to drop it explicitly here not to make the entry orphan.
3264  */
3265  Assert(relation->rd_droppedSubid == mySubid ||
3267  if (isCommit && relation->rd_droppedSubid == InvalidSubTransactionId)
3268  relation->rd_createSubid = parentSubid;
3269  else if (RelationHasReferenceCountZero(relation))
3270  {
3271  /* allow the entry to be removed */
3276  RelationClearRelation(relation, false);
3277  return;
3278  }
3279  else
3280  {
3281  /*
3282  * Hmm, somewhere there's a (leaked?) reference to the relation.
3283  * We daren't remove the entry for fear of dereferencing a
3284  * dangling pointer later. Bleat, and transfer it to the parent
3285  * subtransaction so we can try again later. This must be just a
3286  * WARNING to avoid error-during-error-recovery loops.
3287  */
3288  relation->rd_createSubid = parentSubid;
3289  elog(WARNING, "cannot remove relcache entry for \"%s\" because it has nonzero refcount",
3290  RelationGetRelationName(relation));
3291  }
3292  }
3293 
3294  /*
3295  * Likewise, update or drop any new-relfilenode-in-subtransaction record
3296  * or drop record.
3297  */
3298  if (relation->rd_newRelfilenodeSubid == mySubid)
3299  {
3300  if (isCommit)
3301  relation->rd_newRelfilenodeSubid = parentSubid;
3302  else
3304  }
3305 
3306  if (relation->rd_firstRelfilenodeSubid == mySubid)
3307  {
3308  if (isCommit)
3309  relation->rd_firstRelfilenodeSubid = parentSubid;
3310  else
3312  }
3313 
3314  if (relation->rd_droppedSubid == mySubid)
3315  {
3316  if (isCommit)
3317  relation->rd_droppedSubid = parentSubid;
3318  else
3320  }
3321 }
3322 
3323 
3324 /*
3325  * RelationBuildLocalRelation
3326  * Build a relcache entry for an about-to-be-created relation,
3327  * and enter it into the relcache.
3328  */
3329 Relation
3331  Oid relnamespace,
3332  TupleDesc tupDesc,
3333  Oid relid,
3334  Oid accessmtd,
3335  Oid relfilenode,
3336  Oid reltablespace,
3337  bool shared_relation,
3338  bool mapped_relation,
3339  char relpersistence,
3340  char relkind)
3341 {
3342  Relation rel;
3343  MemoryContext oldcxt;
3344  int natts = tupDesc->natts;
3345  int i;
3346  bool has_not_null;
3347  bool nailit;
3348 
3349  AssertArg(natts >= 0);
3350 
3351  /*
3352  * check for creation of a rel that must be nailed in cache.
3353  *
3354  * XXX this list had better match the relations specially handled in
3355  * RelationCacheInitializePhase2/3.
3356  */
3357  switch (relid)
3358  {
3359  case DatabaseRelationId:
3360  case AuthIdRelationId:
3361  case AuthMemRelationId:
3362  case RelationRelationId:
3363  case AttributeRelationId:
3364  case ProcedureRelationId:
3365  case TypeRelationId:
3366  nailit = true;
3367  break;
3368  default:
3369  nailit = false;
3370  break;
3371  }
3372 
3373  /*
3374  * check that hardwired list of shared rels matches what's in the
3375  * bootstrap .bki file. If you get a failure here during initdb, you
3376  * probably need to fix IsSharedRelation() to match whatever you've done
3377  * to the set of shared relations.
3378  */
3379  if (shared_relation != IsSharedRelation(relid))
3380  elog(ERROR, "shared_relation flag for \"%s\" does not match IsSharedRelation(%u)",
3381  relname, relid);
3382 
3383  /* Shared relations had better be mapped, too */
3384  Assert(mapped_relation || !shared_relation);
3385 
3386  /*
3387  * switch to the cache context to create the relcache entry.
3388  */
3389  if (!CacheMemoryContext)
3391 
3393 
3394  /*
3395  * allocate a new relation descriptor and fill in basic state fields.
3396  */
3397  rel = (Relation) palloc0(sizeof(RelationData));
3398 
3399  /* make sure relation is marked as having no open file yet */
3400  rel->rd_smgr = NULL;
3401 
3402  /* mark it nailed if appropriate */
3403  rel->rd_isnailed = nailit;
3404 
3405  rel->rd_refcnt = nailit ? 1 : 0;
3406 
3407  /* it's being created in this transaction */
3412 
3413  /*
3414  * create a new tuple descriptor from the one passed in. We do this
3415  * partly to copy it into the cache context, and partly because the new
3416  * relation can't have any defaults or constraints yet; they have to be
3417  * added in later steps, because they require additions to multiple system
3418  * catalogs. We can copy attnotnull constraints here, however.
3419  */
3420  rel->rd_att = CreateTupleDescCopy(tupDesc);
3421  rel->rd_att->tdrefcount = 1; /* mark as refcounted */
3422  has_not_null = false;
3423  for (i = 0; i < natts; i++)
3424  {
3425  Form_pg_attribute satt = TupleDescAttr(tupDesc, i);
3426  Form_pg_attribute datt = TupleDescAttr(rel->rd_att, i);
3427 
3428  datt->attidentity = satt->attidentity;
3429  datt->attgenerated = satt->attgenerated;
3430  datt->attnotnull = satt->attnotnull;
3431  has_not_null |= satt->attnotnull;
3432  }
3433 
3434  if (has_not_null)
3435  {
3436  TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr));
3437 
3438  constr->has_not_null = true;
3439  rel->rd_att->constr = constr;
3440  }
3441 
3442  /*
3443  * initialize relation tuple form (caller may add/override data later)
3444  */
3446 
3447  namestrcpy(&rel->rd_rel->relname, relname);
3448  rel->rd_rel->relnamespace = relnamespace;
3449 
3450  rel->rd_rel->relkind = relkind;
3451  rel->rd_rel->relnatts = natts;
3452  rel->rd_rel->reltype = InvalidOid;
3453  /* needed when bootstrapping: */
3454  rel->rd_rel->relowner = BOOTSTRAP_SUPERUSERID;
3455 
3456  /* set up persistence and relcache fields dependent on it */
3457  rel->rd_rel->relpersistence = relpersistence;
3458  switch (relpersistence)
3459  {
3460  case RELPERSISTENCE_UNLOGGED:
3461  case RELPERSISTENCE_PERMANENT:
3463  rel->rd_islocaltemp = false;
3464  break;
3465  case RELPERSISTENCE_TEMP:
3466  Assert(isTempOrTempToastNamespace(relnamespace));
3468  rel->rd_islocaltemp = true;
3469  break;
3470  default:
3471  elog(ERROR, "invalid relpersistence: %c", relpersistence);
3472  break;
3473  }
3474 
3475  /* if it's a materialized view, it's not populated initially */
3476  if (relkind == RELKIND_MATVIEW)
3477  rel->rd_rel->relispopulated = false;
3478  else
3479  rel->rd_rel->relispopulated = true;
3480 
3481  /* set replica identity -- system catalogs and non-tables don't have one */
3482  if (!IsCatalogNamespace(relnamespace) &&
3483  (relkind == RELKIND_RELATION ||
3484  relkind == RELKIND_MATVIEW ||
3485  relkind == RELKIND_PARTITIONED_TABLE))
3486  rel->rd_rel->relreplident = REPLICA_IDENTITY_DEFAULT;
3487  else
3488  rel->rd_rel->relreplident = REPLICA_IDENTITY_NOTHING;
3489 
3490  /*
3491  * Insert relation physical and logical identifiers (OIDs) into the right
3492  * places. For a mapped relation, we set relfilenode to zero and rely on
3493  * RelationInitPhysicalAddr to consult the map.
3494  */
3495  rel->rd_rel->relisshared = shared_relation;
3496 
3497  RelationGetRelid(rel) = relid;
3498 
3499  for (i = 0; i < natts; i++)
3500  TupleDescAttr(rel->rd_att, i)->attrelid = relid;
3501 
3502  rel->rd_rel->reltablespace = reltablespace;
3503 
3504  if (mapped_relation)
3505  {
3506  rel->rd_rel->relfilenode = InvalidOid;
3507  /* Add it to the active mapping information */
3508  RelationMapUpdateMap(relid, relfilenode, shared_relation, true);
3509  }
3510  else
3511  rel->rd_rel->relfilenode = relfilenode;
3512 
3513  RelationInitLockInfo(rel); /* see lmgr.c */
3514 
3516 
3517  rel->rd_rel->relam = accessmtd;
3518 
3519  /*
3520  * RelationInitTableAccessMethod will do syscache lookups, so we mustn't
3521  * run it in CacheMemoryContext. Fortunately, the remaining steps don't
3522  * require a long-lived current context.
3523  */
3524  MemoryContextSwitchTo(oldcxt);
3525 
3526  if (relkind == RELKIND_RELATION ||
3527  relkind == RELKIND_SEQUENCE ||
3528  relkind == RELKIND_TOASTVALUE ||
3529  relkind == RELKIND_MATVIEW)
3531 
3532  /*
3533  * Okay to insert into the relcache hash table.
3534  *
3535  * Ordinarily, there should certainly not be an existing hash entry for
3536  * the same OID; but during bootstrap, when we create a "real" relcache
3537  * entry for one of the bootstrap relations, we'll be overwriting the
3538  * phony one created with formrdesc. So allow that to happen for nailed
3539  * rels.
3540  */
3541  RelationCacheInsert(rel, nailit);
3542 
3543  /*
3544  * Flag relation as needing eoxact cleanup (to clear rd_createSubid). We
3545  * can't do this before storing relid in it.
3546  */
3547  EOXactListAdd(rel);
3548 
3549  /* It's fully valid */
3550  rel->rd_isvalid = true;
3551 
3552  /*
3553  * Caller expects us to pin the returned entry.
3554  */
3556 
3557  return rel;
3558 }
3559 
3560 
3561 /*
3562  * RelationSetNewRelfilenode
3563  *
3564  * Assign a new relfilenode (physical file name), and possibly a new
3565  * persistence setting, to the relation.
3566  *
3567  * This allows a full rewrite of the relation to be done with transactional
3568  * safety (since the filenode assignment can be rolled back). Note however
3569  * that there is no simple way to access the relation's old data for the
3570  * remainder of the current transaction. This limits the usefulness to cases
3571  * such as TRUNCATE or rebuilding an index from scratch.
3572  *
3573  * Caller must already hold exclusive lock on the relation.
3574  */
3575 void
3576 RelationSetNewRelfilenode(Relation relation, char persistence)
3577 {
3578  Oid newrelfilenode;
3579  Relation pg_class;
3580  HeapTuple tuple;
3581  Form_pg_class classform;
3582  MultiXactId minmulti = InvalidMultiXactId;
3583  TransactionId freezeXid = InvalidTransactionId;
3584  RelFileNode newrnode;
3585 
3586  /* Allocate a new relfilenode */
3587  newrelfilenode = GetNewRelFileNode(relation->rd_rel->reltablespace, NULL,
3588  persistence);
3589 
3590  /*
3591  * Get a writable copy of the pg_class tuple for the given relation.
3592  */
3593  pg_class = table_open(RelationRelationId, RowExclusiveLock);
3594 
3595  tuple = SearchSysCacheCopy1(RELOID,
3596  ObjectIdGetDatum(RelationGetRelid(relation)));
3597  if (!HeapTupleIsValid(tuple))
3598  elog(ERROR, "could not find tuple for relation %u",
3599  RelationGetRelid(relation));
3600  classform = (Form_pg_class) GETSTRUCT(tuple);
3601 
3602  /*
3603  * Schedule unlinking of the old storage at transaction commit.
3604  */
3605  RelationDropStorage(relation);
3606 
3607  /*
3608  * Create storage for the main fork of the new relfilenode. If it's a
3609  * table-like object, call into the table AM to do so, which'll also
3610  * create the table's init fork if needed.
3611  *
3612  * NOTE: If relevant for the AM, any conflict in relfilenode value will be
3613  * caught here, if GetNewRelFileNode messes up for any reason.
3614  */
3615  newrnode = relation->rd_node;
3616  newrnode.relNode = newrelfilenode;
3617 
3618  switch (relation->rd_rel->relkind)
3619  {
3620  case RELKIND_INDEX:
3621  case RELKIND_SEQUENCE:
3622  {
3623  /* handle these directly, at least for now */
3624  SMgrRelation srel;
3625 
3626  srel = RelationCreateStorage(newrnode, persistence);
3627  smgrclose(srel);
3628  }
3629  break;
3630 
3631  case RELKIND_RELATION:
3632  case RELKIND_TOASTVALUE:
3633  case RELKIND_MATVIEW:
3634  table_relation_set_new_filenode(relation, &newrnode,
3635  persistence,
3636  &freezeXid, &minmulti);
3637  break;
3638 
3639  default:
3640  /* we shouldn't be called for anything else */
3641  elog(ERROR, "relation \"%s\" does not have storage",
3642  RelationGetRelationName(relation));
3643  break;
3644  }
3645 
3646  /*
3647  * If we're dealing with a mapped index, pg_class.relfilenode doesn't
3648  * change; instead we have to send the update to the relation mapper.
3649  *
3650  * For mapped indexes, we don't actually change the pg_class entry at all;
3651  * this is essential when reindexing pg_class itself. That leaves us with
3652  * possibly-inaccurate values of relpages etc, but those will be fixed up
3653  * later.
3654  */
3655  if (RelationIsMapped(relation))
3656  {
3657  /* This case is only supported for indexes */
3658  Assert(relation->rd_rel->relkind == RELKIND_INDEX);
3659 
3660  /* Since we're not updating pg_class, these had better not change */
3661  Assert(classform->relfrozenxid == freezeXid);
3662  Assert(classform->relminmxid == minmulti);
3663  Assert(classform->relpersistence == persistence);
3664 
3665  /*
3666  * In some code paths it's possible that the tuple update we'd
3667  * otherwise do here is the only thing that would assign an XID for
3668  * the current transaction. However, we must have an XID to delete
3669  * files, so make sure one is assigned.
3670  */
3671  (void) GetCurrentTransactionId();
3672 
3673  /* Do the deed */
3675  newrelfilenode,
3676  relation->rd_rel->relisshared,
3677  false);
3678 
3679  /* Since we're not updating pg_class, must trigger inval manually */
3680  CacheInvalidateRelcache(relation);
3681  }
3682  else
3683  {
3684  /* Normal case, update the pg_class entry */
3685  classform->relfilenode = newrelfilenode;
3686 
3687  /* relpages etc. never change for sequences */
3688  if (relation->rd_rel->relkind != RELKIND_SEQUENCE)
3689  {
3690  classform->relpages = 0; /* it's empty until further notice */
3691  classform->reltuples = -1;
3692  classform->relallvisible = 0;
3693  }
3694  classform->relfrozenxid = freezeXid;
3695  classform->relminmxid = minmulti;
3696  classform->relpersistence = persistence;
3697 
3698  CatalogTupleUpdate(pg_class, &tuple->t_self, tuple);
3699  }
3700 
3701  heap_freetuple(tuple);
3702 
3703  table_close(pg_class, RowExclusiveLock);
3704 
3705  /*
3706  * Make the pg_class row change or relation map change visible. This will
3707  * cause the relcache entry to get updated, too.
3708  */
3710 
3711  RelationAssumeNewRelfilenode(relation);
3712 }
3713 
3714 /*
3715  * RelationAssumeNewRelfilenode
3716  *
3717  * Code that modifies pg_class.reltablespace or pg_class.relfilenode must call
3718  * this. The call shall precede any code that might insert WAL records whose
3719  * replay would modify bytes in the new RelFileNode, and the call shall follow
3720  * any WAL modifying bytes in the prior RelFileNode. See struct RelationData.
3721  * Ideally, call this as near as possible to the CommandCounterIncrement()
3722  * that makes the pg_class change visible (before it or after it); that
3723  * minimizes the chance of future development adding a forbidden WAL insertion
3724  * between RelationAssumeNewRelfilenode() and CommandCounterIncrement().
3725  */
3726 void
3728 {
3731  relation->rd_firstRelfilenodeSubid = relation->rd_newRelfilenodeSubid;
3732 
3733  /* Flag relation as needing eoxact cleanup (to clear these fields) */
3734  EOXactListAdd(relation);
3735 }
3736 
3737 
3738 /*
3739  * RelationCacheInitialize
3740  *
3741  * This initializes the relation descriptor cache. At the time
3742  * that this is invoked, we can't do database access yet (mainly
3743  * because the transaction subsystem is not up); all we are doing
3744  * is making an empty cache hashtable. This must be done before
3745  * starting the initialization transaction, because otherwise
3746  * AtEOXact_RelationCache would crash if that transaction aborts
3747  * before we can get the relcache set up.
3748  */
3749 
3750 #define INITRELCACHESIZE 400
3751 
3752 void
3754 {
3755  HASHCTL ctl;
3756 
3757  /*
3758  * make sure cache memory context exists
3759  */
3760  if (!CacheMemoryContext)
3762 
3763  /*
3764  * create hashtable that indexes the relcache
3765  */
3766  ctl.keysize = sizeof(Oid);
3767  ctl.entrysize = sizeof(RelIdCacheEnt);
3768  RelationIdCache = hash_create("Relcache by OID", INITRELCACHESIZE,
3769  &ctl, HASH_ELEM | HASH_BLOBS);
3770 
3771  /*
3772  * relation mapper needs to be initialized too
3773  */
3775 }
3776 
3777 /*
3778  * RelationCacheInitializePhase2
3779  *
3780  * This is called to prepare for access to shared catalogs during startup.
3781  * We must at least set up nailed reldescs for pg_database, pg_authid,
3782  * pg_auth_members, and pg_shseclabel. Ideally we'd like to have reldescs
3783  * for their indexes, too. We attempt to load this information from the
3784  * shared relcache init file. If that's missing or broken, just make
3785  * phony entries for the catalogs themselves.
3786  * RelationCacheInitializePhase3 will clean up as needed.
3787  */
3788 void
3790 {
3791  MemoryContext oldcxt;
3792 
3793  /*
3794  * relation mapper needs initialized too
3795  */
3797 
3798  /*
3799  * In bootstrap mode, the shared catalogs aren't there yet anyway, so do
3800  * nothing.
3801  */
3803  return;
3804 
3805  /*
3806  * switch to cache memory context
3807  */
3809 
3810  /*
3811  * Try to load the shared relcache cache file. If unsuccessful, bootstrap
3812  * the cache with pre-made descriptors for the critical shared catalogs.
3813  */
3814  if (!load_relcache_init_file(true))
3815  {
3816  formrdesc("pg_database", DatabaseRelation_Rowtype_Id, true,
3817  Natts_pg_database, Desc_pg_database);
3818  formrdesc("pg_authid", AuthIdRelation_Rowtype_Id, true,
3819  Natts_pg_authid, Desc_pg_authid);
3820  formrdesc("pg_auth_members", AuthMemRelation_Rowtype_Id, true,
3821  Natts_pg_auth_members, Desc_pg_auth_members);
3822  formrdesc("pg_shseclabel", SharedSecLabelRelation_Rowtype_Id, true,
3823  Natts_pg_shseclabel, Desc_pg_shseclabel);
3824  formrdesc("pg_subscription", SubscriptionRelation_Rowtype_Id, true,
3825  Natts_pg_subscription, Desc_pg_subscription);
3826 
3827 #define NUM_CRITICAL_SHARED_RELS 5 /* fix if you change list above */
3828  }
3829 
3830  MemoryContextSwitchTo(oldcxt);
3831 }
3832 
3833 /*
3834  * RelationCacheInitializePhase3
3835  *
3836  * This is called as soon as the catcache and transaction system
3837  * are functional and we have determined MyDatabaseId. At this point
3838  * we can actually read data from the database's system catalogs.
3839  * We first try to read pre-computed relcache entries from the local
3840  * relcache init file. If that's missing or broken, make phony entries
3841  * for the minimum set of nailed-in-cache relations. Then (unless
3842  * bootstrapping) make sure we have entries for the critical system
3843  * indexes. Once we've done all this, we have enough infrastructure to
3844  * open any system catalog or use any catcache. The last step is to
3845  * rewrite the cache files if needed.
3846  */
3847 void
3849 {
3851  RelIdCacheEnt *idhentry;
3852  MemoryContext oldcxt;
3853  bool needNewCacheFile = !criticalSharedRelcachesBuilt;
3854 
3855  /*
3856  * relation mapper needs initialized too
3857  */
3859 
3860  /*
3861  * switch to cache memory context
3862  */
3864 
3865  /*
3866  * Try to load the local relcache cache file. If unsuccessful, bootstrap
3867  * the cache with pre-made descriptors for the critical "nailed-in" system
3868  * catalogs.
3869  */
3870  if (IsBootstrapProcessingMode() ||
3871  !load_relcache_init_file(false))
3872  {
3873  needNewCacheFile = true;
3874 
3875  formrdesc("pg_class", RelationRelation_Rowtype_Id, false,
3876  Natts_pg_class, Desc_pg_class);
3877  formrdesc("pg_attribute", AttributeRelation_Rowtype_Id, false,
3878  Natts_pg_attribute, Desc_pg_attribute);
3879  formrdesc("pg_proc", ProcedureRelation_Rowtype_Id, false,
3880  Natts_pg_proc, Desc_pg_proc);
3881  formrdesc("pg_type", TypeRelation_Rowtype_Id, false,
3882  Natts_pg_type, Desc_pg_type);
3883 
3884 #define NUM_CRITICAL_LOCAL_RELS 4 /* fix if you change list above */
3885  }
3886 
3887  MemoryContextSwitchTo(oldcxt);
3888 
3889  /* In bootstrap mode, the faked-up formrdesc info is all we'll have */
3891  return;
3892 
3893  /*
3894  * If we didn't get the critical system indexes loaded into relcache, do
3895  * so now. These are critical because the catcache and/or opclass cache
3896  * depend on them for fetches done during relcache load. Thus, we have an
3897  * infinite-recursion problem. We can break the recursion by doing
3898  * heapscans instead of indexscans at certain key spots. To avoid hobbling
3899  * performance, we only want to do that until we have the critical indexes
3900  * loaded into relcache. Thus, the flag criticalRelcachesBuilt is used to
3901  * decide whether to do heapscan or indexscan at the key spots, and we set
3902  * it true after we've loaded the critical indexes.
3903  *
3904  * The critical indexes are marked as "nailed in cache", partly to make it
3905  * easy for load_relcache_init_file to count them, but mainly because we
3906  * cannot flush and rebuild them once we've set criticalRelcachesBuilt to
3907  * true. (NOTE: perhaps it would be possible to reload them by
3908  * temporarily setting criticalRelcachesBuilt to false again. For now,
3909  * though, we just nail 'em in.)
3910  *
3911  * RewriteRelRulenameIndexId and TriggerRelidNameIndexId are not critical
3912  * in the same way as the others, because the critical catalogs don't
3913  * (currently) have any rules or triggers, and so these indexes can be
3914  * rebuilt without inducing recursion. However they are used during
3915  * relcache load when a rel does have rules or triggers, so we choose to
3916  * nail them for performance reasons.
3917  */
3919  {
3921  RelationRelationId);
3923  AttributeRelationId);
3925  IndexRelationId);
3927  OperatorClassRelationId);
3929  AccessMethodProcedureRelationId);
3931  RewriteRelationId);
3933  TriggerRelationId);
3934 
3935 #define NUM_CRITICAL_LOCAL_INDEXES 7 /* fix if you change list above */
3936 
3937  criticalRelcachesBuilt = true;
3938  }
3939 
3940  /*
3941  * Process critical shared indexes too.
3942  *
3943  * DatabaseNameIndexId isn't critical for relcache loading, but rather for
3944  * initial lookup of MyDatabaseId, without which we'll never find any
3945  * non-shared catalogs at all. Autovacuum calls InitPostgres with a
3946  * database OID, so it instead depends on DatabaseOidIndexId. We also
3947  * need to nail up some indexes on pg_authid and pg_auth_members for use
3948  * during client authentication. SharedSecLabelObjectIndexId isn't
3949  * critical for the core system, but authentication hooks might be
3950  * interested in it.
3951  */
3953  {
3955  DatabaseRelationId);
3957  DatabaseRelationId);
3959  AuthIdRelationId);
3961  AuthIdRelationId);
3963  AuthMemRelationId);
3965  SharedSecLabelRelationId);
3966 
3967 #define NUM_CRITICAL_SHARED_INDEXES 6 /* fix if you change list above */
3968 
3970  }
3971 
3972  /*
3973  * Now, scan all the relcache entries and update anything that might be
3974  * wrong in the results from formrdesc or the relcache cache file. If we
3975  * faked up relcache entries using formrdesc, then read the real pg_class
3976  * rows and replace the fake entries with them. Also, if any of the
3977  * relcache entries have rules, triggers, or security policies, load that
3978  * info the hard way since it isn't recorded in the cache file.
3979  *
3980  * Whenever we access the catalogs to read data, there is a possibility of
3981  * a shared-inval cache flush causing relcache entries to be removed.
3982  * Since hash_seq_search only guarantees to still work after the *current*
3983  * entry is removed, it's unsafe to continue the hashtable scan afterward.
3984  * We handle this by restarting the scan from scratch after each access.
3985  * This is theoretically O(N^2), but the number of entries that actually
3986  * need to be fixed is small enough that it doesn't matter.
3987  */
3988  hash_seq_init(&status, RelationIdCache);
3989 
3990  while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
3991  {
3992  Relation relation = idhentry->reldesc;
3993  bool restart = false;
3994 
3995  /*
3996  * Make sure *this* entry doesn't get flushed while we work with it.
3997  */
3999 
4000  /*
4001  * If it's a faked-up entry, read the real pg_class tuple.
4002  */
4003  if (relation->rd_rel->relowner == InvalidOid)
4004  {
4005  HeapTuple htup;
4006  Form_pg_class relp;
4007 
4008  htup = SearchSysCache1(RELOID,
4009  ObjectIdGetDatum(RelationGetRelid(relation)));
4010  if (!HeapTupleIsValid(htup))
4011  elog(FATAL, "cache lookup failed for relation %u",
4012  RelationGetRelid(relation));
4013  relp = (Form_pg_class) GETSTRUCT(htup);
4014 
4015  /*
4016  * Copy tuple to relation->rd_rel. (See notes in
4017  * AllocateRelationDesc())
4018  */
4019  memcpy((char *) relation->rd_rel, (char *) relp, CLASS_TUPLE_SIZE);
4020 
4021  /* Update rd_options while we have the tuple */
4022  if (relation->rd_options)
4023  pfree(relation->rd_options);
4024  RelationParseRelOptions(relation, htup);
4025 
4026  /*
4027  * Check the values in rd_att were set up correctly. (We cannot
4028  * just copy them over now: formrdesc must have set up the rd_att
4029  * data correctly to start with, because it may already have been
4030  * copied into one or more catcache entries.)
4031  */
4032  Assert(relation->rd_att->tdtypeid == relp->reltype);
4033  Assert(relation->rd_att->tdtypmod == -1);
4034 
4035  ReleaseSysCache(htup);
4036 
4037  /* relowner had better be OK now, else we'll loop forever */
4038  if (relation->rd_rel->relowner == InvalidOid)
4039  elog(ERROR, "invalid relowner in pg_class entry for \"%s\"",
4040  RelationGetRelationName(relation));
4041 
4042  restart = true;
4043  }
4044 
4045  /*
4046  * Fix data that isn't saved in relcache cache file.
4047  *
4048  * relhasrules or relhastriggers could possibly be wrong or out of
4049  * date. If we don't actually find any rules or triggers, clear the
4050  * local copy of the flag so that we don't get into an infinite loop
4051  * here. We don't make any attempt to fix the pg_class entry, though.
4052  */
4053  if (relation->rd_rel->relhasrules && relation->rd_rules == NULL)
4054  {
4055  RelationBuildRuleLock(relation);
4056  if (relation->rd_rules == NULL)
4057  relation->rd_rel->relhasrules = false;
4058  restart = true;
4059  }
4060  if (relation->rd_rel->relhastriggers && relation->trigdesc == NULL)
4061  {
4062  RelationBuildTriggers(relation);
4063  if (relation->trigdesc == NULL)
4064  relation->rd_rel->relhastriggers = false;
4065  restart = true;
4066  }
4067 
4068  /*
4069  * Re-load the row security policies if the relation has them, since
4070  * they are not preserved in the cache. Note that we can never NOT
4071  * have a policy while relrowsecurity is true,
4072  * RelationBuildRowSecurity will create a single default-deny policy
4073  * if there is no policy defined in pg_policy.
4074  */
4075  if (relation->rd_rel->relrowsecurity && relation->rd_rsdesc == NULL)
4076  {
4077  RelationBuildRowSecurity(relation);
4078 
4079  Assert(relation->rd_rsdesc != NULL);
4080  restart = true;
4081  }
4082 
4083  /* Reload tableam data if needed */
4084  if (relation->rd_tableam == NULL &&
4085  (relation->rd_rel->relkind == RELKIND_RELATION ||
4086  relation->rd_rel->relkind == RELKIND_SEQUENCE ||
4087  relation->rd_rel->relkind == RELKIND_TOASTVALUE ||
4088  relation->rd_rel->relkind == RELKIND_MATVIEW))
4089  {
4091  Assert(relation->rd_tableam != NULL);
4092 
4093  restart = true;
4094  }
4095 
4096  /* Release hold on the relation */
4098 
4099  /* Now, restart the hashtable scan if needed */
4100  if (restart)
4101  {
4102  hash_seq_term(&status);
4103  hash_seq_init(&status, RelationIdCache);
4104  }
4105  }
4106 
4107  /*
4108  * Lastly, write out new relcache cache files if needed. We don't bother
4109  * to distinguish cases where only one of the two needs an update.
4110  */
4111  if (needNewCacheFile)
4112  {
4113  /*
4114  * Force all the catcaches to finish initializing and thereby open the
4115  * catalogs and indexes they use. This will preload the relcache with
4116  * entries for all the most important system catalogs and indexes, so
4117  * that the init files will be most useful for future backends.
4118  */
4120 
4121  /* now write the files */
4123  write_relcache_init_file(false);
4124  }
4125 }
4126 
4127 /*
4128  * Load one critical system index into the relcache
4129  *
4130  * indexoid is the OID of the target index, heapoid is the OID of the catalog
4131  * it belongs to.
4132  */
4133 static void
4134 load_critical_index(Oid indexoid, Oid heapoid)
4135 {
4136  Relation ird;
4137 
4138  /*
4139  * We must lock the underlying catalog before locking the index to avoid
4140  * deadlock, since RelationBuildDesc might well need to read the catalog,
4141  * and if anyone else is exclusive-locking this catalog and index they'll
4142  * be doing it in that order.
4143  */
4144  LockRelationOid(heapoid, AccessShareLock);
4145  LockRelationOid(indexoid, AccessShareLock);
4146  ird = RelationBuildDesc(indexoid, true);
4147  if (ird == NULL)
4148  elog(PANIC, "could not open critical system index %u", indexoid);
4149  ird->rd_isnailed = true;
4150  ird->rd_refcnt = 1;
4153 
4154  (void) RelationGetIndexAttOptions(ird, false);
4155 }
4156 
4157 /*
4158  * GetPgClassDescriptor -- get a predefined tuple descriptor for pg_class
4159  * GetPgIndexDescriptor -- get a predefined tuple descriptor for pg_index
4160  *
4161  * We need this kluge because we have to be able to access non-fixed-width
4162  * fields of pg_class and pg_index before we have the standard catalog caches
4163  * available. We use predefined data that's set up in just the same way as
4164  * the bootstrapped reldescs used by formrdesc(). The resulting tupdesc is
4165  * not 100% kosher: it does not have the correct rowtype OID in tdtypeid, nor
4166  * does it have a TupleConstr field. But it's good enough for the purpose of
4167  * extracting fields.
4168  */
4169 static TupleDesc
4171 {
4172  TupleDesc result;
4173  MemoryContext oldcxt;
4174  int i;
4175 
4177 
4178  result = CreateTemplateTupleDesc(natts);
4179  result->tdtypeid = RECORDOID; /* not right, but we don't care */
4180  result->tdtypmod = -1;
4181 
4182  for (i = 0; i < natts; i++)
4183  {
4184  memcpy(TupleDescAttr(result, i), &attrs[i], ATTRIBUTE_FIXED_PART_SIZE);
4185  /* make sure attcacheoff is valid */
4186  TupleDescAttr(result, i)->attcacheoff = -1;
4187  }
4188 
4189  /* initialize first attribute's attcacheoff, cf RelationBuildTupleDesc */
4190  TupleDescAttr(result, 0)->attcacheoff = 0;
4191 
4192  /* Note: we don't bother to set up a TupleConstr entry */
4193 
4194  MemoryContextSwitchTo(oldcxt);
4195 
4196  return result;
4197 }
4198 
4199 static TupleDesc
4201 {
4202  static TupleDesc pgclassdesc = NULL;
4203 
4204  /* Already done? */
4205  if (pgclassdesc == NULL)
4206  pgclassdesc = BuildHardcodedDescriptor(Natts_pg_class,
4207  Desc_pg_class);
4208 
4209  return pgclassdesc;
4210 }
4211 
4212 static TupleDesc
4214 {
4215  static TupleDesc pgindexdesc = NULL;
4216 
4217  /* Already done? */
4218  if (pgindexdesc == NULL)
4219  pgindexdesc = BuildHardcodedDescriptor(Natts_pg_index,
4220  Desc_pg_index);
4221 
4222  return pgindexdesc;
4223 }
4224 
4225 /*
4226  * Load any default attribute value definitions for the relation.
4227  *
4228  * ndef is the number of attributes that were marked atthasdef.
4229  *
4230  * Note: we don't make it a hard error to be missing some pg_attrdef records.
4231  * We can limp along as long as nothing needs to use the default value. Code
4232  * that fails to find an expected AttrDefault record should throw an error.
4233  */
4234 static void
4235 AttrDefaultFetch(Relation relation, int ndef)
4236 {
4237  AttrDefault *attrdef;
4238  Relation adrel;
4239  SysScanDesc adscan;
4240  ScanKeyData skey;
4241  HeapTuple htup;
4242  int found = 0;
4243 
4244  /* Allocate array with room for as many entries as expected */
4245  attrdef = (AttrDefault *)
4247  ndef * sizeof(AttrDefault));
4248 
4249  /* Search pg_attrdef for relevant entries */
4250  ScanKeyInit(&skey,
4251  Anum_pg_attrdef_adrelid,
4252  BTEqualStrategyNumber, F_OIDEQ,
4253  ObjectIdGetDatum(RelationGetRelid(relation)));
4254 
4255  adrel = table_open(AttrDefaultRelationId, AccessShareLock);
4256  adscan = systable_beginscan(adrel, AttrDefaultIndexId, true,
4257  NULL, 1, &skey);
4258 
4259  while (HeapTupleIsValid(htup = systable_getnext(adscan)))
4260  {
4261  Form_pg_attrdef adform = (Form_pg_attrdef) GETSTRUCT(htup);
4262  Datum val;
4263  bool isnull;
4264 
4265  /* protect limited size of array */
4266  if (found >= ndef)
4267  {
4268  elog(WARNING, "unexpected pg_attrdef record found for attribute %d of relation \"%s\"",
4269  adform->adnum, RelationGetRelationName(relation));
4270  break;
4271  }
4272 
4273  val = fastgetattr(htup,
4274  Anum_pg_attrdef_adbin,
4275  adrel->rd_att, &isnull);
4276  if (isnull)
4277  elog(WARNING, "null adbin for attribute %d of relation \"%s\"",
4278  adform->adnum, RelationGetRelationName(relation));
4279  else
4280  {
4281  /* detoast and convert to cstring in caller's context */
4282  char *s = TextDatumGetCString(val);
4283 
4284  attrdef[found].adnum = adform->adnum;
4285  attrdef[found].adbin = MemoryContextStrdup(CacheMemoryContext, s);
4286  pfree(s);
4287  found++;
4288  }
4289  }
4290 
4291  systable_endscan(adscan);
4292  table_close(adrel, AccessShareLock);
4293 
4294  if (found != ndef)
4295  elog(WARNING, "%d pg_attrdef record(s) missing for relation \"%s\"",
4296  ndef - found, RelationGetRelationName(relation));
4297 
4298  /*
4299  * Sort the AttrDefault entries by adnum, for the convenience of
4300  * equalTupleDescs(). (Usually, they already will be in order, but this
4301  * might not be so if systable_getnext isn't using an index.)
4302  */
4303  if (found > 1)
4304  qsort(attrdef, found, sizeof(AttrDefault), AttrDefaultCmp);
4305 
4306  /* Install array only after it's fully valid */
4307  relation->rd_att->constr->defval = attrdef;
4308  relation->rd_att->constr->num_defval = found;
4309 }
4310 
4311 /*
4312  * qsort comparator to sort AttrDefault entries by adnum
4313  */
4314 static int
4315 AttrDefaultCmp(const void *a, const void *b)
4316 {
4317  const AttrDefault *ada = (const AttrDefault *) a;
4318  const AttrDefault *adb = (const AttrDefault *) b;
4319 
4320  return ada->adnum - adb->adnum;
4321 }
4322 
4323 /*
4324  * Load any check constraints for the relation.
4325  *
4326  * As with defaults, if we don't find the expected number of them, just warn
4327  * here. The executor should throw an error if an INSERT/UPDATE is attempted.
4328  */
4329 static void
4331 {
4332  ConstrCheck *check;
4333  int ncheck = relation->rd_rel->relchecks;
4334  Relation conrel;
4335  SysScanDesc conscan;
4336  ScanKeyData skey[1];
4337  HeapTuple htup;
4338  int found = 0;
4339 
4340  /* Allocate array with room for as many entries as expected */
4341  check = (ConstrCheck *)
4343  ncheck * sizeof(ConstrCheck));
4344 
4345  /* Search pg_constraint for relevant entries */
4346  ScanKeyInit(&skey[0],
4347  Anum_pg_constraint_conrelid,
4348  BTEqualStrategyNumber, F_OIDEQ,
4349  ObjectIdGetDatum(RelationGetRelid(relation)));
4350 
4351  conrel = table_open(ConstraintRelationId, AccessShareLock);
4352  conscan = systable_beginscan(conrel, ConstraintRelidTypidNameIndexId, true,
4353  NULL, 1, skey);
4354 
4355  while (HeapTupleIsValid(htup = systable_getnext(conscan)))
4356  {
4358  Datum val;
4359  bool isnull;
4360 
4361  /* We want check constraints only */
4362  if (conform->contype != CONSTRAINT_CHECK)
4363  continue;
4364 
4365  /* protect limited size of array */
4366  if (found >= ncheck)
4367  {
4368  elog(WARNING, "unexpected pg_constraint record found for relation \"%s\"",
4369  RelationGetRelationName(relation));
4370  break;
4371  }
4372 
4373  check[found].ccvalid = conform->convalidated;
4374  check[found].ccnoinherit = conform->connoinherit;
4376  NameStr(conform->conname));
4377 
4378  /* Grab and test conbin is actually set */
4379  val = fastgetattr(htup,
4380  Anum_pg_constraint_conbin,
4381  conrel->rd_att, &isnull);
4382  if (isnull)
4383  elog(WARNING, "null conbin for relation \"%s\"",
4384  RelationGetRelationName(relation));
4385  else
4386  {
4387  /* detoast and convert to cstring in caller's context */
4388  char *s = TextDatumGetCString(val);
4389 
4390  check[found].ccbin = MemoryContextStrdup(CacheMemoryContext, s);
4391  pfree(s);
4392  found++;
4393  }
4394  }
4395 
4396  systable_endscan(conscan);
4397  table_close(conrel, AccessShareLock);
4398 
4399  if (found != ncheck)
4400  elog(WARNING, "%d pg_constraint record(s) missing for relation \"%s\"",
4401  ncheck - found, RelationGetRelationName(relation));
4402 
4403  /*
4404  * Sort the records by name. This ensures that CHECKs are applied in a
4405  * deterministic order, and it also makes equalTupleDescs() faster.
4406  */
4407  if (found > 1)
4408  qsort(check, found, sizeof(ConstrCheck), CheckConstraintCmp);
4409 
4410  /* Install array only after it's fully valid */
4411  relation->rd_att->constr->check = check;
4412  relation->rd_att->constr->num_check = found;
4413 }
4414 
4415 /*
4416  * qsort comparator to sort ConstrCheck entries by name
4417  */
4418 static int
4419 CheckConstraintCmp(const void *a, const void *b)
4420 {
4421  const ConstrCheck *ca = (const ConstrCheck *) a;
4422  const ConstrCheck *cb = (const ConstrCheck *) b;
4423 
4424  return strcmp(ca->ccname, cb->ccname);
4425 }
4426 
4427 /*
4428  * RelationGetFKeyList -- get a list of foreign key info for the relation
4429  *
4430  * Returns a list of ForeignKeyCacheInfo structs, one per FK constraining
4431  * the given relation. This data is a direct copy of relevant fields from
4432  * pg_constraint. The list items are in no particular order.
4433  *
4434  * CAUTION: the returned list is part of the relcache's data, and could
4435  * vanish in a relcache entry reset. Callers must inspect or copy it
4436  * before doing anything that might trigger a cache flush, such as
4437  * system catalog accesses. copyObject() can be used if desired.
4438  * (We define it this way because current callers want to filter and
4439  * modify the list entries anyway, so copying would be a waste of time.)
4440  */
4441 List *
4443 {
4444  List *result;
4445  Relation conrel;
4446  SysScanDesc conscan;
4447  ScanKeyData skey;
4448  HeapTuple htup;
4449  List *oldlist;
4450  MemoryContext oldcxt;
4451 
4452  /* Quick exit if we already computed the list. */
4453  if (relation->rd_fkeyvalid)
4454  return relation->rd_fkeylist;
4455 
4456  /* Fast path: non-partitioned tables without triggers can't have FKs */
4457  if (!relation->rd_rel->relhastriggers &&
4458  relation->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
4459  return NIL;
4460 
4461  /*
4462  * We build the list we intend to return (in the caller's context) while
4463  * doing the scan. After successfully completing the scan, we copy that
4464  * list into the relcache entry. This avoids cache-context memory leakage
4465  * if we get some sort of error partway through.
4466  */
4467  result = NIL;
4468 
4469  /* Prepare to scan pg_constraint for entries having conrelid = this rel. */
4470  ScanKeyInit(&skey,
4471  Anum_pg_constraint_conrelid,
4472  BTEqualStrategyNumber, F_OIDEQ,
4473  ObjectIdGetDatum(RelationGetRelid(relation)));
4474 
4475  conrel = table_open(ConstraintRelationId, AccessShareLock);
4476  conscan = systable_beginscan(conrel, ConstraintRelidTypidNameIndexId, true,
4477  NULL, 1, &skey);
4478 
4479  while (HeapTupleIsValid(htup = systable_getnext(conscan)))
4480  {
4481  Form_pg_constraint constraint = (Form_pg_constraint) GETSTRUCT(htup);
4482  ForeignKeyCacheInfo *info;
4483 
4484  /* consider only foreign keys */
4485  if (constraint->contype != CONSTRAINT_FOREIGN)
4486  continue;
4487 
4488  info = makeNode(ForeignKeyCacheInfo);
4489  info->conoid = constraint->oid;
4490  info->conrelid = constraint->conrelid;
4491  info->confrelid = constraint->confrelid;
4492 
4493  DeconstructFkConstraintRow(htup, &info->nkeys,
4494  info->conkey,
4495  info->confkey,
4496  info->conpfeqop,
4497  NULL, NULL);
4498 
4499  /* Add FK's node to the result list */
4500  result = lappend(result, info);
4501  }
4502 
4503  systable_endscan(conscan);
4504  table_close(conrel, AccessShareLock);
4505 
4506  /* Now save a copy of the completed list in the relcache entry. */
4508  oldlist = relation->rd_fkeylist;
4509  relation->rd_fkeylist = copyObject(result);
4510  relation->rd_fkeyvalid = true;
4511  MemoryContextSwitchTo(oldcxt);
4512 
4513  /* Don't leak the old list, if there is one */
4514  list_free_deep(oldlist);
4515 
4516  return result;
4517 }
4518 
4519 /*
4520  * RelationGetIndexList -- get a list of OIDs of indexes on this relation
4521  *
4522  * The index list is created only if someone requests it. We scan pg_index
4523  * to find relevant indexes, and add the list to the relcache entry so that
4524  * we won't have to compute it again. Note that shared cache inval of a
4525  * relcache entry will delete the old list and set rd_indexvalid to false,
4526  * so that we must recompute the index list on next request. This handles
4527  * creation or deletion of an index.
4528  *
4529  * Indexes that are marked not indislive are omitted from the returned list.
4530  * Such indexes are expected to be dropped momentarily, and should not be
4531  * touched at all by any caller of this function.
4532  *
4533  * The returned list is guaranteed to be sorted in order by OID. This is
4534  * needed by the executor, since for index types that we obtain exclusive
4535  * locks on when updating the index, all backends must lock the indexes in
4536  * the same order or we will get deadlocks (see ExecOpenIndices()). Any
4537  * consistent ordering would do, but ordering by OID is easy.
4538  *
4539  * Since shared cache inval causes the relcache's copy of the list to go away,
4540  * we return a copy of the list palloc'd in the caller's context. The caller
4541  * may list_free() the returned list after scanning it. This is necessary
4542  * since the caller will typically be doing syscache lookups on the relevant
4543  * indexes, and syscache lookup could cause SI messages to be processed!
4544  *
4545  * In exactly the same way, we update rd_pkindex, which is the OID of the
4546  * relation's primary key index if any, else InvalidOid; and rd_replidindex,
4547  * which is the pg_class OID of an index to be used as the relation's
4548  * replication identity index, or InvalidOid if there is no such index.
4549  */
4550 List *
4552 {
4553  Relation indrel;
4554  SysScanDesc indscan;
4555  ScanKeyData skey;
4556  HeapTuple htup;
4557  List *result;
4558  List *oldlist;
4559  char replident = relation->rd_rel->relreplident;
4560  Oid pkeyIndex = InvalidOid;
4561  Oid candidateIndex = InvalidOid;
4562  MemoryContext oldcxt;
4563 
4564  /* Quick exit if we already computed the list. */
4565  if (relation->rd_indexvalid)
4566  return list_copy(relation->rd_indexlist);
4567 
4568  /*
4569  * We build the list we intend to return (in the caller's context) while
4570  * doing the scan. After successfully completing the scan, we copy that
4571  * list into the relcache entry. This avoids cache-context memory leakage
4572  * if we get some sort of error partway through.
4573  */
4574  result = NIL;
4575 
4576  /* Prepare to scan pg_index for entries having indrelid = this rel. */
4577  ScanKeyInit(&skey,
4578  Anum_pg_index_indrelid,
4579  BTEqualStrategyNumber, F_OIDEQ,
4580  ObjectIdGetDatum(RelationGetRelid(relation)));
4581 
4582  indrel = table_open(IndexRelationId, AccessShareLock);
4583  indscan = systable_beginscan(indrel, IndexIndrelidIndexId, true,
4584  NULL, 1, &skey);
4585 
4586  while (HeapTupleIsValid(htup = systable_getnext(indscan)))
4587  {
4589 
4590  /*
4591  * Ignore any indexes that are currently being dropped. This will
4592  * prevent them from being searched, inserted into, or considered in
4593  * HOT-safety decisions. It's unsafe to touch such an index at all
4594  * since its catalog entries could disappear at any instant.
4595  */
4596  if (!index->indislive)
4597  continue;
4598 
4599  /* add index's OID to result list */
4600  result = lappend_oid(result, index->indexrelid);
4601 
4602  /*
4603  * Invalid, non-unique, non-immediate or predicate indexes aren't
4604  * interesting for either oid indexes or replication identity indexes,
4605  * so don't check them.
4606  */
4607  if (!index->indisvalid || !index->indisunique ||
4608  !index->indimmediate ||
4609  !heap_attisnull(htup, Anum_pg_index_indpred, NULL))
4610  continue;
4611 
4612  /* remember primary key index if any */
4613  if (index->indisprimary)
4614  pkeyIndex = index->indexrelid;
4615 
4616  /* remember explicitly chosen replica index */
4617  if (index->indisreplident)
4618  candidateIndex = index->indexrelid;
4619  }
4620 
4621  systable_endscan(indscan);
4622 
4623  table_close(indrel, AccessShareLock);
4624 
4625  /* Sort the result list into OID order, per API spec. */
4626  list_sort(result, list_oid_cmp);
4627 
4628  /* Now save a copy of the completed list in the relcache entry. */
4630  oldlist = relation->rd_indexlist;
4631  relation->rd_indexlist = list_copy(result);
4632  relation->rd_pkindex = pkeyIndex;
4633  if (replident == REPLICA_IDENTITY_DEFAULT && OidIsValid(pkeyIndex))
4634  relation->rd_replidindex = pkeyIndex;
4635  else if (replident == REPLICA_IDENTITY_INDEX && OidIsValid(candidateIndex))
4636  relation->rd_replidindex = candidateIndex;
4637  else
4638  relation->rd_replidindex = InvalidOid;
4639  relation->rd_indexvalid = true;
4640  MemoryContextSwitchTo(oldcxt);
4641 
4642  /* Don't leak the old list, if there is one */
4643  list_free(oldlist);
4644 
4645  return result;
4646 }
4647 
4648 /*
4649  * RelationGetStatExtList
4650  * get a list of OIDs of statistics objects on this relation
4651  *
4652  * The statistics list is created only if someone requests it, in a way
4653  * similar to RelationGetIndexList(). We scan pg_statistic_ext to find
4654  * relevant statistics, and add the list to the relcache entry so that we
4655  * won't have to compute it again. Note that shared cache inval of a
4656  * relcache entry will delete the old list and set rd_statvalid to 0,
4657  * so that we must recompute the statistics list on next request. This
4658  * handles creation or deletion of a statistics object.
4659  *
4660  * The returned list is guaranteed to be sorted in order by OID, although
4661  * this is not currently needed.
4662  *
4663  * Since shared cache inval causes the relcache's copy of the list to go away,
4664  * we return a copy of the list palloc'd in the caller's context. The caller
4665  * may list_free() the returned list after scanning it. This is necessary
4666  * since the caller will typically be doing syscache lookups on the relevant
4667  * statistics, and syscache lookup could cause SI messages to be processed!
4668  */
4669 List *
4671 {
4672  Relation indrel;
4673  SysScanDesc indscan;
4674  ScanKeyData skey;
4675  HeapTuple htup;
4676  List *result;
4677  List *oldlist;
4678  MemoryContext oldcxt;
4679 
4680  /* Quick exit if we already computed the list. */
4681  if (relation->rd_statvalid != 0)
4682  return list_copy(relation->rd_statlist);
4683 
4684  /*
4685  * We build the list we intend to return (in the caller's context) while
4686  * doing the scan. After successfully completing the scan, we copy that
4687  * list into the relcache entry. This avoids cache-context memory leakage
4688  * if we get some sort of error partway through.
4689  */
4690  result = NIL;
4691 
4692  /*
4693  * Prepare to scan pg_statistic_ext for entries having stxrelid = this
4694  * rel.
4695  */
4696  ScanKeyInit(&skey,
4697  Anum_pg_statistic_ext_stxrelid,
4698  BTEqualStrategyNumber, F_OIDEQ,
4699  ObjectIdGetDatum(RelationGetRelid(relation)));
4700 
4701  indrel = table_open(StatisticExtRelationId, AccessShareLock);
4702  indscan = systable_beginscan(indrel, StatisticExtRelidIndexId, true,
4703  NULL, 1, &skey);
4704 
4705  while (HeapTupleIsValid(htup = systable_getnext(indscan)))
4706  {
4707  Oid oid = ((Form_pg_statistic_ext) GETSTRUCT(htup))->oid;
4708 
4709  result = lappend_oid(result, oid);
4710  }
4711 
4712  systable_endscan(indscan);
4713 
4714  table_close(indrel, AccessShareLock);
4715 
4716  /* Sort the result list into OID order, per API spec. */
4717  list_sort(result, list_oid_cmp);
4718 
4719  /* Now save a copy of the completed list in the relcache entry. */
4721  oldlist = relation->rd_statlist;
4722  relation->rd_statlist = list_copy(result);
4723 
4724  relation->rd_statvalid = true;
4725  MemoryContextSwitchTo(oldcxt);
4726 
4727  /* Don't leak the old list, if there is one */
4728  list_free(oldlist);
4729 
4730  return result;
4731 }
4732 
4733 /*
4734  * RelationGetPrimaryKeyIndex -- get OID of the relation's primary key index
4735  *
4736  * Returns InvalidOid if there is no such index.
4737  */
4738 Oid
4740 {
4741  List *ilist;
4742 
4743  if (!relation->rd_indexvalid)
4744  {
4745  /* RelationGetIndexList does the heavy lifting. */
4746  ilist = RelationGetIndexList(relation);
4747  list_free(ilist);
4748  Assert(relation->rd_indexvalid);
4749  }
4750 
4751  return relation->rd_pkindex;
4752 }
4753 
4754 /*
4755  * RelationGetReplicaIndex -- get OID of the relation's replica identity index
4756  *
4757  * Returns InvalidOid if there is no such index.
4758  */
4759 Oid
4761 {
4762  List *ilist;
4763 
4764  if (!relation->rd_indexvalid)
4765  {
4766  /* RelationGetIndexList does the heavy lifting. */
4767  ilist = RelationGetIndexList(relation);
4768  list_free(ilist);
4769  Assert(relation->rd_indexvalid);
4770  }
4771 
4772  return relation->rd_replidindex;
4773 }
4774 
4775 /*
4776  * RelationGetIndexExpressions -- get the index expressions for an index
4777  *
4778  * We cache the result of transforming pg_index.indexprs into a node tree.
4779  * If the rel is not an index or has no expressional columns, we return NIL.
4780  * Otherwise, the returned tree is copied into the caller's memory context.
4781  * (We don't want to return a pointer to the relcache copy, since it could
4782  * disappear due to relcache invalidation.)
4783  */
4784 List *
4786 {
4787  List *result;
4788  Datum exprsDatum;
4789  bool isnull;
4790  char *exprsString;
4791  MemoryContext oldcxt;
4792 
4793  /* Quick exit if we already computed the result. */
4794  if (relation->rd_indexprs)
4795  return copyObject(relation->rd_indexprs);
4796 
4797  /* Quick exit if there is nothing to do. */
4798  if (relation->rd_indextuple == NULL ||
4799  heap_attisnull(relation->rd_indextuple, Anum_pg_index_indexprs, NULL))
4800  return NIL;
4801 
4802  /*
4803  * We build the tree we intend to return in the caller's context. After
4804  * successfully completing the work, we copy it into the relcache entry.
4805  * This avoids problems if we get some sort of error partway through.
4806  */
4807  exprsDatum = heap_getattr(relation->rd_indextuple,
4808  Anum_pg_index_indexprs,
4810  &isnull);
4811  Assert(!isnull);
4812  exprsString = TextDatumGetCString(exprsDatum);
4813  result = (List *) stringToNode(exprsString);
4814  pfree(exprsString);
4815 
4816  /*
4817  * Run the expressions through eval_const_expressions. This is not just an
4818  * optimization, but is necessary, because the planner will be comparing
4819  * them to similarly-processed qual clauses, and may fail to detect valid
4820  * matches without this. We must not use canonicalize_qual, however,
4821  * since these aren't qual expressions.
4822  */
4823  result = (List *) eval_const_expressions(NULL, (Node *) result);
4824 
4825  /* May as well fix opfuncids too */
4826  fix_opfuncids((Node *) result);
4827 
4828  /* Now save a copy of the completed tree in the relcache entry. */
4829  oldcxt = MemoryContextSwitchTo(relation->rd_indexcxt);
4830  relation->rd_indexprs = copyObject(result);
4831  MemoryContextSwitchTo(oldcxt);
4832 
4833  return result;
4834 }
4835 
4836 /*
4837  * RelationGetDummyIndexExpressions -- get dummy expressions for an index
4838  *
4839  * Return a list of dummy expressions (just Const nodes) with the same
4840  * types/typmods/collations as the index's real expressions. This is
4841  * useful in situations where we don't want to run any user-defined code.
4842  */
4843 List *
4845 {
4846  List *result;
4847  Datum exprsDatum;
4848  bool isnull;
4849  char *exprsString;
4850  List *rawExprs;
4851  ListCell *lc;
4852 
4853  /* Quick exit if there is nothing to do. */
4854  if (relation->rd_indextuple == NULL ||
4855  heap_attisnull(relation->rd_indextuple, Anum_pg_index_indexprs, NULL))
4856  return NIL;
4857 
4858  /* Extract raw node tree(s) from index tuple. */
4859  exprsDatum = heap_getattr(relation->rd_indextuple,
4860  Anum_pg_index_indexprs,
4862  &isnull);
4863  Assert(!isnull);
4864  exprsString = TextDatumGetCString(exprsDatum);
4865  rawExprs = (List *) stringToNode(exprsString);
4866  pfree(exprsString);
4867 
4868  /* Construct null Consts; the typlen and typbyval are arbitrary. */
4869  result = NIL;
4870  foreach(lc, rawExprs)
4871  {
4872  Node *rawExpr = (Node *) lfirst(lc);
4873 
4874  result = lappend(result,
4875  makeConst(exprType(rawExpr),
4876  exprTypmod(rawExpr),
4877  exprCollation(rawExpr),
4878  1,
4879  (Datum) 0,
4880  true,
4881  true));
4882  }
4883 
4884  return result;
4885 }
4886 
4887 /*
4888  * RelationGetIndexPredicate -- get the index predicate for an index
4889  *
4890  * We cache the result of transforming pg_index.indpred into an implicit-AND
4891  * node tree (suitable for use in planning).
4892  * If the rel is not an index or has no predicate, we return NIL.
4893  * Otherwise, the returned tree is copied into the caller's memory context.
4894  * (We don't want to return a pointer to the relcache copy, since it could
4895  * disappear due to relcache invalidation.)
4896  */
4897 List *
4899 {
4900  List *result;
4901  Datum predDatum;
4902  bool isnull;
4903  char *predString;
4904  MemoryContext oldcxt;
4905 
4906  /* Quick exit if we already computed the result. */
4907  if (relation->rd_indpred)
4908  return copyObject(relation->rd_indpred);
4909 
4910  /* Quick exit if there is nothing to do. */
4911  if (relation->rd_indextuple == NULL ||
4912  heap_attisnull(relation->rd_indextuple, Anum_pg_index_indpred, NULL))
4913  return NIL;
4914 
4915  /*
4916  * We build the tree we intend to return in the caller's context. After
4917  * successfully completing the work, we copy it into the relcache entry.
4918  * This avoids problems if we get some sort of error partway through.
4919  */
4920  predDatum = heap_getattr(relation->rd_indextuple,
4921  Anum_pg_index_indpred,
4923  &isnull);
4924  Assert(!isnull);
4925  predString = TextDatumGetCString(predDatum);
4926  result = (List *) stringToNode(predString);
4927  pfree(predString);
4928 
4929  /*
4930  * Run the expression through const-simplification and canonicalization.
4931  * This is not just an optimization, but is necessary, because the planner
4932  * will be comparing it to similarly-processed qual clauses, and may fail
4933  * to detect valid matches without this. This must match the processing
4934  * done to qual clauses in preprocess_expression()! (We can skip the
4935  * stuff involving subqueries, however, since we don't allow any in index
4936  * predicates.)
4937  */
4938  result = (List *) eval_const_expressions(NULL, (Node *) result);
4939 
4940  result = (List *) canonicalize_qual((Expr *) result, false);
4941 
4942  /* Also convert to implicit-AND format */
4943  result = make_ands_implicit((Expr *) result);
4944 
4945  /* May as well fix opfuncids too */
4946  fix_opfuncids((Node *) result);
4947 
4948  /* Now save a copy of the completed tree in the relcache entry. */
4949  oldcxt = MemoryContextSwitchTo(relation->rd_indexcxt);
4950  relation->rd_indpred = copyObject(result);
4951  MemoryContextSwitchTo(oldcxt);
4952 
4953  return result;
4954 }
4955 
4956 /*
4957  * RelationGetIndexAttrBitmap -- get a bitmap of index attribute numbers
4958  *
4959  * The result has a bit set for each attribute used anywhere in the index
4960  * definitions of all the indexes on this relation. (This includes not only
4961  * simple index keys, but attributes used in expressions and partial-index
4962  * predicates.)
4963  *
4964  * Depending on attrKind, a bitmap covering the attnums for all index columns,
4965  * for all potential foreign key columns, or for all columns in the configured
4966  * replica identity index is returned.
4967  *
4968  * Attribute numbers are offset by FirstLowInvalidHeapAttributeNumber so that
4969  * we can include system attributes (e.g., OID) in the bitmap representation.
4970  *
4971  * Caller had better hold at least RowExclusiveLock on the target relation
4972  * to ensure it is safe (deadlock-free) for us to take locks on the relation's
4973  * indexes. Note that since the introduction of CREATE INDEX CONCURRENTLY,
4974  * that lock level doesn't guarantee a stable set of indexes, so we have to
4975  * be prepared to retry here in case of a change in the set of indexes.
4976  *
4977  * The returned result is palloc'd in the caller's memory context and should
4978  * be bms_free'd when not needed anymore.
4979  */
4980 Bitmapset *
4982 {
4983  Bitmapset *indexattrs; /* indexed columns */
4984  Bitmapset *uindexattrs; /* columns in unique indexes */
4985  Bitmapset *pkindexattrs; /* columns in the primary index */
4986  Bitmapset *idindexattrs; /* columns in the replica identity */
4987  List *indexoidlist;
4988  List *newindexoidlist;
4989  Oid relpkindex;
4990  Oid relreplindex;
4991  ListCell *l;
4992  MemoryContext oldcxt;
4993 
4994  /* Quick exit if we already computed the result. */
4995  if (relation->rd_indexattr != NULL)
4996  {
4997  switch (attrKind)
4998  {
4999  case INDEX_ATTR_BITMAP_ALL:
5000  return bms_copy(relation->rd_indexattr);
5001  case INDEX_ATTR_BITMAP_KEY:
5002  return bms_copy(relation->rd_keyattr);
5004  return bms_copy(relation->rd_pkattr);
5006  return bms_copy(relation->rd_idattr);
5007  default:
5008  elog(ERROR, "unknown attrKind %u", attrKind);
5009  }
5010  }
5011 
5012  /* Fast path if definitely no indexes */
5013  if (!RelationGetForm(relation)->relhasindex)
5014  return NULL;
5015 
5016  /*
5017  * Get cached list of index OIDs. If we have to start over, we do so here.
5018  */
5019 restart:
5020  indexoidlist = RelationGetIndexList(relation);
5021 
5022  /* Fall out if no indexes (but relhasindex was set) */
5023  if (indexoidlist == NIL)
5024  return NULL;
5025 
5026  /*
5027  * Copy the rd_pkindex and rd_replidindex values computed by
5028  * RelationGetIndexList before proceeding. This is needed because a
5029  * relcache flush could occur inside index_open below, resetting the
5030  * fields managed by RelationGetIndexList. We need to do the work with
5031  * stable values of these fields.
5032  */
5033  relpkindex = relation->rd_pkindex;
5034  relreplindex = relation->rd_replidindex;
5035 
5036  /*
5037  * For each index, add referenced attributes to indexattrs.
5038  *
5039  * Note: we consider all indexes returned by RelationGetIndexList, even if
5040  * they are not indisready or indisvalid. This is important because an
5041  * index for which CREATE INDEX CONCURRENTLY has just started must be
5042  * included in HOT-safety decisions (see README.HOT). If a DROP INDEX
5043  * CONCURRENTLY is far enough along that we should ignore the index, it
5044  * won't be returned at all by RelationGetIndexList.
5045  */
5046  indexattrs = NULL;
5047  uindexattrs = NULL;
5048  pkindexattrs = NULL;
5049  idindexattrs = NULL;
5050  foreach(l, indexoidlist)
5051  {
5052  Oid indexOid = lfirst_oid(l);
5053  Relation indexDesc;
5054  Datum datum;
5055  bool isnull;
5056  Node *indexExpressions;
5057  Node *indexPredicate;
5058  int i;
5059  bool isKey; /* candidate key */
5060  bool isPK; /* primary key */
5061  bool isIDKey; /* replica identity index */
5062 
5063  indexDesc = index_open(indexOid, AccessShareLock);
5064 
5065  /*
5066  * Extract index expressions and index predicate. Note: Don't use
5067  * RelationGetIndexExpressions()/RelationGetIndexPredicate(), because
5068  * those might run constant expressions evaluation, which needs a
5069  * snapshot, which we might not have here. (Also, it's probably more
5070  * sound to collect the bitmaps before any transformations that might
5071  * eliminate columns, but the practical impact of this is limited.)
5072  */
5073 
5074  datum = heap_getattr(indexDesc->rd_indextuple, Anum_pg_index_indexprs,
5075  GetPgIndexDescriptor(), &isnull);
5076  if (!isnull)
5077  indexExpressions = stringToNode(TextDatumGetCString(datum));
5078  else
5079  indexExpressions = NULL;
5080 
5081  datum = heap_getattr(indexDesc->rd_indextuple, Anum_pg_index_indpred,
5082  GetPgIndexDescriptor(), &isnull);
5083  if (!isnull)
5084  indexPredicate = stringToNode(TextDatumGetCString(datum));
5085  else
5086  indexPredicate = NULL;
5087 
5088  /* Can this index be referenced by a foreign key? */
5089  isKey = indexDesc->rd_index->indisunique &&
5090  indexExpressions == NULL &&
5091  indexPredicate == NULL;
5092 
5093  /* Is this a primary key? */
5094  isPK = (indexOid == relpkindex);
5095 
5096  /* Is this index the configured (or default) replica identity? */
5097  isIDKey = (indexOid == relreplindex);
5098 
5099  /* Collect simple attribute references */
5100  for (i = 0; i < indexDesc->rd_index->indnatts; i++)
5101  {
5102  int attrnum = indexDesc->rd_index->indkey.values[i];
5103 
5104  /*
5105  * Since we have covering indexes with non-key columns, we must
5106  * handle them accurately here. non-key columns must be added into
5107  * indexattrs, since they are in index, and HOT-update shouldn't
5108  * miss them. Obviously, non-key columns couldn't be referenced by
5109  * foreign key or identity key. Hence we do not include them into
5110  * uindexattrs, pkindexattrs and idindexattrs bitmaps.
5111  */
5112  if (attrnum != 0)
5113  {
5114  indexattrs = bms_add_member(indexattrs,
5116 
5117  if (isKey && i < indexDesc->rd_index->indnkeyatts)
5118  uindexattrs = bms_add_member(uindexattrs,
5120 
5121  if (isPK && i < indexDesc->rd_index->indnkeyatts)
5122  pkindexattrs = bms_add_member(pkindexattrs,
5124 
5125  if (isIDKey && i < indexDesc->rd_index->indnkeyatts)
5126  idindexattrs = bms_add_member(idindexattrs,
5128  }
5129  }
5130 
5131  /* Collect all attributes used in expressions, too */
5132  pull_varattnos(indexExpressions, 1, &indexattrs);
5133 
5134  /* Collect all attributes in the index predicate, too */
5135  pull_varattnos(indexPredicate, 1, &indexattrs);
5136 
5137  index_close(indexDesc, AccessShareLock);
5138  }
5139 
5140  /*
5141  * During one of the index_opens in the above loop, we might have received
5142  * a relcache flush event on this relcache entry, which might have been
5143  * signaling a change in the rel's index list. If so, we'd better start
5144  * over to ensure we deliver up-to-date attribute bitmaps.
5145  */
5146  newindexoidlist = RelationGetIndexList(relation);
5147  if (equal(indexoidlist, newindexoidlist) &&
5148  relpkindex == relation->rd_pkindex &&
5149  relreplindex == relation->rd_replidindex)
5150  {
5151  /* Still the same index set, so proceed */
5152  list_free(newindexoidlist);
5153  list_free(indexoidlist);
5154  }
5155  else
5156  {
5157  /* Gotta do it over ... might as well not leak memory */
5158  list_free(newindexoidlist);
5159  list_free(indexoidlist);
5160  bms_free(uindexattrs);
5161  bms_free(pkindexattrs);
5162  bms_free(idindexattrs);
5163  bms_free(indexattrs);
5164 
5165  goto restart;
5166  }
5167 
5168  /* Don't leak the old values of these bitmaps, if any */
5169  bms_free(relation->rd_indexattr);
5170  relation->rd_indexattr = NULL;
5171  bms_free(relation->rd_keyattr);
5172  relation->rd_keyattr = NULL;
5173  bms_free(relation->rd_pkattr);
5174  relation->rd_pkattr = NULL;
5175  bms_free(relation->rd_idattr);
5176  relation->rd_idattr = NULL;
5177 
5178  /*
5179  * Now save copies of the bitmaps in the relcache entry. We intentionally
5180  * set rd_indexattr last, because that's the one that signals validity of
5181  * the values; if we run out of memory before making that copy, we won't
5182  * leave the relcache entry looking like the other ones are valid but
5183  * empty.
5184  */
5186  relation->rd_keyattr = bms_copy(uindexattrs);
5187  relation->rd_pkattr = bms_copy(pkindexattrs);
5188  relation->rd_idattr = bms_copy(idindexattrs);
5189  relation->rd_indexattr = bms_copy(indexattrs);
5190  MemoryContextSwitchTo(oldcxt);
5191 
5192  /* We return our original working copy for caller to play with */
5193  switch (attrKind)
5194  {
5195  case INDEX_ATTR_BITMAP_ALL:
5196  return indexattrs;
5197  case INDEX_ATTR_BITMAP_KEY:
5198  return uindexattrs;
5200  return pkindexattrs;
5202  return idindexattrs;
5203  default:
5204  elog(ERROR, "unknown attrKind %u", attrKind);
5205  return NULL;
5206  }
5207 }
5208 
5209 /*
5210  * RelationGetExclusionInfo -- get info about index's exclusion constraint
5211  *
5212  * This should be called only for an index that is known to have an
5213  * associated exclusion constraint. It returns arrays (palloc'd in caller's
5214  * context) of the exclusion operator OIDs, their underlying functions'
5215  * OIDs, and their strategy numbers in the index's opclasses. We cache
5216  * all this information since it requires a fair amount of work to get.
5217  */
5218 void
5220  Oid **operators,
5221  Oid **procs,
5222  uint16 **strategies)
5223 {
5224  int indnkeyatts;
5225  Oid *ops;
5226  Oid *funcs;
5227  uint16 *strats;
5228  Relation conrel;
5229  SysScanDesc conscan;
5230  ScanKeyData skey[1];
5231  HeapTuple htup;
5232  bool found;
5233  MemoryContext oldcxt;
5234  int i;
5235 
5236  indnkeyatts = IndexRelationGetNumberOfKeyAttributes(indexRelation);
5237 
5238  /* Allocate result space in caller context */
5239  *operators = ops = (Oid *) palloc(sizeof(Oid) * indnkeyatts);
5240  *procs = funcs = (Oid *) palloc(sizeof(Oid) * indnkeyatts);
5241  *strategies = strats = (uint16 *) palloc(sizeof(uint16) * indnkeyatts);
5242 
5243  /* Quick exit if we have the data cached already */
5244  if (indexRelation->rd_exclstrats != NULL)
5245  {
5246  memcpy(ops, indexRelation->rd_exclops, sizeof(Oid) * indnkeyatts);
5247  memcpy(funcs, indexRelation->rd_exclprocs, sizeof(Oid) * indnkeyatts);
5248  memcpy(strats, indexRelation->rd_exclstrats, sizeof(uint16) * indnkeyatts);
5249  return;
5250  }
5251 
5252  /*
5253  * Search pg_constraint for the constraint associated with the index. To
5254  * make this not too painfully slow, we use the index on conrelid; that
5255  * will hold the parent relation's OID not the index's own OID.
5256  *
5257  * Note: if we wanted to rely on the constraint name matching the index's
5258  * name, we could just do a direct lookup using pg_constraint's unique
5259  * index. For the moment it doesn't seem worth requiring that.
5260  */
5261  ScanKeyInit(&skey[0],
5262  Anum_pg_constraint_conrelid,
5263  BTEqualStrategyNumber, F_OIDEQ,
5264  ObjectIdGetDatum(indexRelation->rd_index->indrelid));
5265 
5266  conrel = table_open(ConstraintRelationId, AccessShareLock);
5267  conscan = systable_beginscan(conrel, ConstraintRelidTypidNameIndexId, true,
5268  NULL, 1, skey);
5269  found = false;
5270 
5271  while (HeapTupleIsValid(htup = systable_getnext(conscan)))
5272  {
5274  Datum val;
5275  bool isnull;
5276  ArrayType *arr;
5277  int nelem;
5278 
5279  /* We want the exclusion constraint owning the index */
5280  if (conform->contype != CONSTRAINT_EXCLUSION ||
5281  conform->conindid != RelationGetRelid(indexRelation))
5282  continue;
5283 
5284  /* There should be only one */
5285  if (found)
5286  elog(ERROR, "unexpected exclusion constraint record found for rel %s",
5287  RelationGetRelationName(indexRelation));
5288  found = true;
5289 
5290  /* Extract the operator OIDS from conexclop */
5291  val = fastgetattr(htup,
5292  Anum_pg_constraint_conexclop,
5293  conrel->rd_att, &isnull);
5294  if (isnull)
5295  elog(ERROR, "null conexclop for rel %s",
5296  RelationGetRelationName(indexRelation));
5297 
5298  arr = DatumGetArrayTypeP(val); /* ensure not toasted */
5299  nelem = ARR_DIMS(arr)[0];
5300  if (ARR_NDIM(arr) != 1 ||
5301  nelem != indnkeyatts ||
5302  ARR_HASNULL(arr) ||
5303  ARR_ELEMTYPE(arr) != OIDOID)
5304  elog(ERROR, "conexclop is not a 1-D Oid array");
5305 
5306  memcpy(ops, ARR_DATA_PTR(arr), sizeof(Oid) * indnkeyatts);
5307  }
5308 
5309  systable_endscan(conscan);
5310  table_close(conrel, AccessShareLock);
5311 
5312  if (!found)
5313  elog(ERROR, "exclusion constraint record missing for rel %s",
5314  RelationGetRelationName(indexRelation));
5315 
5316  /* We need the func OIDs and strategy numbers too */
5317  for (i = 0; i < indnkeyatts; i++)
5318  {
5319  funcs[i] = get_opcode(ops[i]);
5320  strats[i] = get_op_opfamily_strategy(ops[i],
5321  indexRelation->rd_opfamily[i]);
5322  /* shouldn't fail, since it was checked at index creation */
5323  if (strats[i] == InvalidStrategy)
5324  elog(ERROR, "could not find strategy for operator %u in family %u",
5325  ops[i], indexRelation->rd_opfamily[i]);
5326  }
5327 
5328  /* Save a copy of the results in the relcache entry. */
5329  oldcxt = MemoryContextSwitchTo(indexRelation->rd_indexcxt);
5330  indexRelation->rd_exclops = (Oid *) palloc(sizeof(Oid) * indnkeyatts);
5331  indexRelation->rd_exclprocs = (Oid *) palloc(sizeof(Oid) * indnkeyatts);
5332  indexRelation->rd_exclstrats = (uint16 *) palloc(sizeof(uint16) * indnkeyatts);
5333  memcpy(indexRelation->rd_exclops, ops, sizeof(Oid) * indnkeyatts);
5334  memcpy(indexRelation->rd_exclprocs, funcs, sizeof(Oid) * indnkeyatts);
5335  memcpy(indexRelation->rd_exclstrats, strats, sizeof(uint16) * indnkeyatts);
5336  MemoryContextSwitchTo(oldcxt);
5337 }
5338 
5339 /*
5340  * Get publication actions for the given relation.
5341  */
5342 struct PublicationActions *
5344 {
5345  List *puboids;
5346  ListCell *lc;
5347  MemoryContext oldcxt;
5348  PublicationActions *pubactions = palloc0(sizeof(PublicationActions));
5349 
5350  /*
5351  * If not publishable, it publishes no actions. (pgoutput_change() will
5352  * ignore it.)
5353  */
5354  if (!is_publishable_relation(relation))
5355  return pubactions;
5356 
5357  if (relation->rd_pubactions)
5358  return memcpy(pubactions, relation->rd_pubactions,
5359  sizeof(PublicationActions));
5360 
5361  /* Fetch the publication membership info. */
5362  puboids = GetRelationPublications(RelationGetRelid(relation));
5363  if (relation->rd_rel->relispartition)
5364  {
5365  /* Add publications that the ancestors are in too. */
5366  List *ancestors = get_partition_ancestors(RelationGetRelid(relation));
5367  ListCell *lc;
5368 
5369  foreach(lc, ancestors)
5370  {
5371  Oid ancestor = lfirst_oid(lc);
5372 
5373  puboids = list_concat_unique_oid(puboids,
5374  GetRelationPublications(ancestor));
5375  }
5376  }
5377  puboids = list_concat_unique_oid(puboids, GetAllTablesPublications());
5378 
5379  foreach(lc, puboids)
5380  {
5381  Oid pubid = lfirst_oid(lc);
5382  HeapTuple tup;
5383  Form_pg_publication pubform;
5384 
5386 
5387  if (!HeapTupleIsValid(tup))
5388  elog(ERROR, "cache lookup failed for publication %u", pubid);
5389 
5390  pubform = (Form_pg_publication) GETSTRUCT(tup);
5391 
5392  pubactions->pubinsert |= pubform->pubinsert;
5393  pubactions->pubupdate |= pubform->pubupdate;
5394  pubactions->pubdelete |= pubform->pubdelete;
5395  pubactions->pubtruncate |= pubform->pubtruncate;
5396 
5397  ReleaseSysCache(tup);
5398 
5399  /*
5400  * If we know everything is replicated, there is no point to check for
5401  * other publications.
5402  */
5403  if (pubactions->pubinsert && pubactions->pubupdate &&
5404  pubactions->pubdelete && pubactions->pubtruncate)
5405  break;
5406  }
5407 
5408  if (relation->rd_pubactions)
5409  {
5410  pfree(relation->rd_pubactions);
5411  relation->rd_pubactions = NULL;
5412  }
5413 
5414  /* Now save copy of the actions in the relcache entry. */
5416  relation->rd_pubactions = palloc(sizeof(PublicationActions));
5417  memcpy(relation->rd_pubactions, pubactions, sizeof(PublicationActions));
5418  MemoryContextSwitchTo(oldcxt);
5419 
5420  return pubactions;
5421 }
5422 
5423 /*
5424  * RelationGetIndexRawAttOptions -- get AM/opclass-specific options for the index
5425  */
5426 Datum *
5428 {
5429  Oid indexrelid = RelationGetRelid(indexrel);
5430  int16 natts = RelationGetNumberOfAttributes(indexrel);
5431  Datum *options = NULL;
5432  int16 attnum;
5433 
5434  for (attnum = 1; attnum <= natts; attnum++)
5435  {
5436  if (indexrel->rd_indam->amoptsprocnum == 0)
5437  continue;
5438 
5439  if (!OidIsValid(index_getprocid(indexrel, attnum,
5440  indexrel->rd_indam->amoptsprocnum)))
5441  continue;
5442 
5443  if (!options)
5444  options = palloc0(sizeof(Datum) * natts);
5445 
5446  options[attnum - 1] = get_attoptions(indexrelid, attnum);
5447  }
5448 
5449  return options;
5450 }
5451 
5452 static bytea **
5453 CopyIndexAttOptions(bytea **srcopts, int natts)
5454 {
5455  bytea **opts = palloc(sizeof(*opts) * natts);
5456 
5457  for (int i = 0; i < natts; i++)
5458  {
5459  bytea *opt = srcopts[i];
5460 
5461  opts[i] = !opt ? NULL : (bytea *)
5462  DatumGetPointer(datumCopy(PointerGetDatum(opt), false, -1));
5463  }
5464 
5465  return opts;
5466 }
5467 
5468 /*
5469  * RelationGetIndexAttOptions
5470  * get AM/opclass-specific options for an index parsed into a binary form
5471  */
5472 bytea **
5474 {
5475  MemoryContext oldcxt;
5476  bytea **opts = relation->rd_opcoptions;
5477  Oid relid = RelationGetRelid(relation);
5478  int natts = RelationGetNumberOfAttributes(relation); /* XXX
5479  * IndexRelationGetNumberOfKeyAttributes */
5480  int i;
5481 
5482  /* Try to copy cached options. */
5483  if (opts)
5484  return copy ? CopyIndexAttOptions(opts, natts) : opts;
5485 
5486  /* Get and parse opclass options. */
5487  opts = palloc0(sizeof(*opts) * natts);
5488 
5489  for (i = 0; i < natts; i++)
5490  {
5492  {
5493  Datum attoptions = get_attoptions(relid, i + 1);
5494 
5495  opts[i] = index_opclass_options(relation, i + 1, attoptions, false);
5496 
5497  if (attoptions != (Datum) 0)
5498  pfree(DatumGetPointer(attoptions));
5499  }
5500  }
5501 
5502  /* Copy parsed options to the cache. */
5503  oldcxt = MemoryContextSwitchTo(relation->rd_indexcxt);
5504  relation->rd_opcoptions = CopyIndexAttOptions(opts, natts);
5505  MemoryContextSwitchTo(oldcxt);
5506 
5507  if (copy)
5508  return opts;
5509 
5510  for (i = 0; i < natts; i++)
5511  {
5512  if (opts[i])
5513  pfree(opts[i]);
5514  }
5515 
5516  pfree(opts);
5517 
5518  return relation->rd_opcoptions;
5519 }
5520 
5521 /*
5522  * Routines to support ereport() reports of relation-related errors
5523  *
5524  * These could have been put into elog.c, but it seems like a module layering
5525  * violation to have elog.c calling relcache or syscache stuff --- and we
5526  * definitely don't want elog.h including rel.h. So we put them here.
5527  */
5528 
5529 /*
5530  * errtable --- stores schema_name and table_name of a table
5531  * within the current errordata.
5532  */
5533 int
5535 {
5539 
5540  return 0; /* return value does not matter */
5541 }
5542 
5543 /*
5544  * errtablecol --- stores schema_name, table_name and column_name
5545  * of a table column within the current errordata.
5546  *
5547  * The column is specified by attribute number --- for most callers, this is
5548  * easier and less error-prone than getting the column name for themselves.
5549  */
5550 int
5552 {
5554  const char *colname;
5555 
5556  /* Use reldesc if it's a user attribute, else consult the catalogs */
5557  if (attnum > 0 && attnum <= reldesc->natts)
5558  colname = NameStr(TupleDescAttr(reldesc, attnum - 1)->attname);
5559  else
5560  colname = get_attname(RelationGetRelid(rel), attnum, false);
5561 
5562  return errtablecolname(rel, colname);
5563 }
5564 
5565 /*
5566  * errtablecolname --- stores schema_name, table_name and column_name
5567  * of a table column within the current errordata, where the column name is
5568  * given directly rather than extracted from the relation's catalog data.
5569  *
5570  * Don't use this directly unless errtablecol() is inconvenient for some
5571  * reason. This might possibly be needed during intermediate states in ALTER
5572  * TABLE, for instance.
5573  */
5574 int
5575 errtablecolname(Relation rel, const char *colname)
5576 {
5577  errtable(rel);
5579 
5580  return 0; /* return value does not matter */
5581 }
5582 
5583 /*
5584  * errtableconstraint --- stores schema_name, table_name and constraint_name
5585  * of a table-related constraint within the current errordata.
5586  */
5587 int
5588 errtableconstraint(Relation rel, const char *conname)
5589 {
5590  errtable(rel);
5592 
5593  return 0; /* return value does not matter */
5594 }
5595 
5596 
5597 /*
5598  * load_relcache_init_file, write_relcache_init_file
5599  *
5600  * In late 1992, we started regularly having databases with more than
5601  * a thousand classes in them. With this number of classes, it became
5602  * critical to do indexed lookups on the system catalogs.
5603  *
5604  * Bootstrapping these lookups is very hard. We want to be able to
5605  * use an index on pg_attribute, for example, but in order to do so,
5606  * we must have read pg_attribute for the attributes in the index,
5607  * which implies that we need to use the index.
5608  *
5609  * In order to get around the problem, we do the following:
5610  *
5611  * + When the database system is initialized (at initdb time), we
5612  * don't use indexes. We do sequential scans.
5613  *
5614  * + When the backend is started up in normal mode, we load an image
5615  * of the appropriate relation descriptors, in internal format,
5616  * from an initialization file in the data/base/... directory.
5617  *
5618  * + If the initialization file isn't there, then we create the
5619  * relation descriptors using sequential scans and write 'em to
5620  * the initialization file for use by subsequent backends.
5621  *
5622  * As of Postgres 9.0, there is one local initialization file in each
5623  * database, plus one shared initialization file for shared catalogs.
5624  *
5625  * We could dispense with the initialization files and just build the
5626  * critical reldescs the hard way on every backend startup, but that
5627  * slows down backend startup noticeably.
5628  *
5629  * We can in fact go further, and save more relcache entries than
5630  * just the ones that are absolutely critical; this allows us to speed
5631  * up backend startup by not having to build such entries the hard way.
5632  * Presently, all the catalog and index entries that are referred to
5633  * by catcaches are stored in the initialization files.
5634  *
5635  * The same mechanism that detects when catcache and relcache entries
5636  * need to be invalidated (due to catalog updates) also arranges to
5637  * unlink the initialization files when the contents may be out of date.
5638  * The files will then be rebuilt during the next backend startup.
5639  */
5640 
5641 /*
5642  * load_relcache_init_file -- attempt to load cache from the shared
5643  * or local cache init file
5644  *
5645  * If successful, return true and set criticalRelcachesBuilt or
5646  * criticalSharedRelcachesBuilt to true.
5647  * If not successful, return false.
5648  *
5649  * NOTE: we assume we are already switched into CacheMemoryContext.
5650  */
5651 static bool
5653 {
5654  FILE *fp;
5655  char initfilename[MAXPGPATH];
5656  Relation *rels;
5657  int relno,
5658  num_rels,
5659  max_rels,
5660  nailed_rels,
5661  nailed_indexes,
5662  magic;
5663  int i;
5664 
5665  if (shared)
5666  snprintf(initfilename, sizeof(initfilename), "global/%s",
5668  else
5669  snprintf(initfilename, sizeof(initfilename), "%s/%s",
5671 
5672  fp = AllocateFile(initfilename, PG_BINARY_R);
5673  if (fp == NULL)
5674  return false;
5675 
5676  /*
5677  * Read the index relcache entries from the file. Note we will not enter
5678  * any of them into the cache if the read fails partway through; this
5679  * helps to guard against broken init files.
5680  */
5681  max_rels = 100;
5682  rels = (Relation *) palloc(max_rels * sizeof(Relation));
5683  num_rels = 0;
5684  nailed_rels = nailed_indexes = 0;
5685 
5686  /* check for correct magic number (compatible version) */
5687  if (fread(&magic, 1, sizeof(magic), fp) != sizeof(magic))
5688  goto read_failed;
5689  if (magic != RELCACHE_INIT_FILEMAGIC)
5690  goto read_failed;
5691 
5692  for (relno = 0;; relno++)
5693  {
5694  Size len;
5695  size_t nread;
5696  Relation rel;
5697  Form_pg_class relform;
5698  bool has_not_null;
5699 
5700  /* first read the relation descriptor length */
5701  nread = fread(&len, 1, sizeof(len), fp);
5702  if (nread != sizeof(len))
5703  {
5704  if (nread == 0)
5705  break; /* end of file */
5706  goto read_failed;
5707  }
5708 
5709  /* safety check for incompatible relcache layout */
5710  if (len != sizeof(RelationData))
5711  goto read_failed;
5712 
5713  /* allocate another relcache header */
5714  if (num_rels >= max_rels)
5715  {
5716  max_rels *= 2;
5717  rels = (Relation *) repalloc(rels, max_rels * sizeof(Relation));
5718  }
5719 
5720  rel = rels[num_rels++] = (Relation) palloc(len);
5721 
5722  /* then, read the Relation structure */
5723  if (fread(rel, 1, len, fp) != len)
5724  goto read_failed;
5725 
5726  /* next read the relation tuple form */
5727  if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
5728  goto read_failed;
5729 
5730  relform = (Form_pg_class) palloc(len);
5731  if (fread(relform, 1, len, fp) != len)
5732  goto read_failed;
5733 
5734  rel->rd_rel = relform;
5735 
5736  /* initialize attribute tuple forms */
5737  rel->rd_att = CreateTemplateTupleDesc(relform->relnatts);
5738  rel->rd_att->tdrefcount = 1; /* mark as refcounted */
5739 
5740  rel->rd_att->tdtypeid = relform->reltype ? relform->reltype : RECORDOID;
5741  rel->rd_att->tdtypmod = -1; /* just to be sure */
5742 
5743  /* next read all the attribute tuple form data entries */
5744  has_not_null = false;
5745  for (i = 0; i < relform->relnatts; i++)
5746  {
5747  Form_pg_attribute attr = TupleDescAttr(rel->rd_att, i);
5748 
5749  if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
5750  goto read_failed;
5751  if (len != ATTRIBUTE_FIXED_PART_SIZE)
5752  goto read_failed;
5753  if (fread(attr, 1, len, fp) != len)
5754  goto read_failed;
5755 
5756  has_not_null |= attr->attnotnull;
5757  }
5758 
5759  /* next read the access method specific field */
5760  if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
5761  goto read_failed;
5762  if (len > 0)
5763  {
5764  rel->rd_options = palloc(len);
5765  if (fread(rel->rd_options, 1, len, fp) != len)
5766  goto read_failed;
5767  if (len != VARSIZE(rel->rd_options))
5768  goto read_failed; /* sanity check */
5769  }
5770  else
5771  {
5772  rel->rd_options = NULL;
5773  }
5774 
5775  /* mark not-null status */
5776  if (has_not_null)
5777  {
5778  TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr));
5779 
5780  constr->has_not_null = true;
5781  rel->rd_att->constr = constr;
5782  }
5783 
5784  /*
5785  * If it's an index, there's more to do. Note we explicitly ignore
5786  * partitioned indexes here.
5787  */
5788  if (rel->rd_rel->relkind == RELKIND_INDEX)
5789  {
5790  MemoryContext indexcxt;
5791  Oid *opfamily;
5792  Oid *opcintype;
5793  RegProcedure *support;
5794  int nsupport;
5795  int16 *indoption;
5796  Oid *indcollation;
5797 
5798  /* Count nailed indexes to ensure we have 'em all */
5799  if (rel->rd_isnailed)
5800  nailed_indexes++;
5801 
5802  /* next, read the pg_index tuple */
5803  if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
5804  goto read_failed;
5805 
5806  rel->rd_indextuple = (HeapTuple) palloc(len);
5807  if (fread(rel->rd_indextuple, 1, len, fp) != len)
5808  goto read_failed;
5809 
5810  /* Fix up internal pointers in the tuple -- see heap_copytuple */
5811  rel->rd_indextuple->t_data = (HeapTupleHeader) ((char *) rel->rd_indextuple + HEAPTUPLESIZE);
5813 
5814  /*
5815  * prepare index info context --- parameters should match
5816  * RelationInitIndexAccessInfo
5817  */
5819  "index info",
5821  rel->rd_indexcxt = indexcxt;
5824 
5825  /*
5826  * Now we can fetch the index AM's API struct. (We can't store
5827  * that in the init file, since it contains function pointers that
5828  * might vary across server executions. Fortunately, it should be
5829  * safe to call the amhandler even while bootstrapping indexes.)
5830  */
5831  InitIndexAmRoutine(rel);
5832 
5833  /* next, read the vector of opfamily OIDs */
5834  if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
5835  goto read_failed;
5836 
5837  opfamily = (Oid *) MemoryContextAlloc(indexcxt, len);
5838  if (fread(opfamily, 1, len, fp) != len)
5839  goto read_failed;
5840 
5841  rel->rd_opfamily = opfamily;
5842 
5843  /* next, read the vector of opcintype OIDs */
5844  if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
5845  goto read_failed;
5846 
5847  opcintype = (Oid *) MemoryContextAlloc(indexcxt, len);
5848  if (fread(opcintype, 1, len, fp) != len)
5849  goto read_failed;
5850 
5851  rel->rd_opcintype = opcintype;
5852 
5853  /* next, read the vector of support procedure OIDs */
5854  if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
5855  goto read_failed;
5856  support = (RegProcedure *) MemoryContextAlloc(indexcxt, len);
5857  if (fread(support, 1, len, fp) != len)
5858  goto read_failed;
5859 
5860  rel->rd_support = support;
5861 
5862  /* next, read the vector of collation OIDs */
5863  if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
5864  goto read_failed;
5865 
5866  indcollation = (Oid *) MemoryContextAlloc(indexcxt, len);
5867  if (fread(indcollation, 1, len, fp) != len)
5868  goto read_failed;
5869 
5870  rel->rd_indcollation = indcollation;
5871 
5872  /* finally, read the vector of indoption values */
5873  if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
5874  goto read_failed;
5875 
5876  indoption = (int16 *) MemoryContextAlloc(indexcxt, len);
5877  if (fread(indoption, 1, len, fp) != len)
5878  goto read_failed;
5879 
5880  rel->rd_indoption = indoption;
5881 
5882  /* finally, read the vector of opcoptions values */
5883  rel->rd_opcoptions = (bytea **)
5884  MemoryContextAllocZero(indexcxt, sizeof(*rel->rd_opcoptions) * relform->relnatts);
5885 
5886  for (i = 0; i < relform->relnatts; i++)
5887  {
5888  if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
5889  goto read_failed;
5890 
5891  if (len > 0)
5892  {
5893  rel->rd_opcoptions[i] = (bytea *) MemoryContextAlloc(indexcxt, len);
5894  if (fread(rel->rd_opcoptions[i], 1, len, fp) != len)
5895  goto read_failed;
5896  }
5897  }
5898 
5899  /* set up zeroed fmgr-info vector */
5900  nsupport = relform->relnatts * rel->rd_indam->amsupport;
5901  rel->rd_supportinfo = (FmgrInfo *)
5902  MemoryContextAllocZero(indexcxt, nsupport * sizeof(FmgrInfo));
5903  }
5904  else
5905  {
5906  /* Count nailed rels to ensure we have 'em all */
5907  if (rel->rd_isnailed)
5908  nailed_rels++;
5909 
5910  /* Load table AM data */
5911  if (rel->rd_rel->relkind == RELKIND_RELATION ||
5912  rel->rd_rel->relkind == RELKIND_SEQUENCE ||
5913  rel->rd_rel->relkind == RELKIND_TOASTVALUE ||
5914  rel->rd_rel->relkind == RELKIND_MATVIEW)
5916 
5917  Assert(rel->rd_index == NULL);
5918  Assert(rel->rd_indextuple == NULL);
5919  Assert(rel->rd_indexcxt == NULL);
5920  Assert(rel->rd_indam == NULL);
5921  Assert(rel->rd_opfamily == NULL);
5922  Assert(rel->rd_opcintype == NULL);
5923  Assert(rel->rd_support == NULL);
5924  Assert(rel->rd_supportinfo == NULL);
5925  Assert(rel->rd_indoption == NULL);
5926  Assert(rel->rd_indcollation == NULL);
5927  Assert(rel->rd_opcoptions == NULL);
5928  }
5929 
5930  /*
5931  * Rules and triggers are not saved (mainly because the internal
5932  * format is complex and subject to change). They must be rebuilt if
5933  * needed by RelationCacheInitializePhase3. This is not expected to
5934  * be a big performance hit since few system catalogs have such. Ditto
5935  * for RLS policy data, partition info, index expressions, predicates,
5936  * exclusion info, and FDW info.
5937  */
5938  rel->rd_rules = NULL;
5939  rel->rd_rulescxt = NULL;
5940  rel->trigdesc = NULL;
5941  rel->rd_rsdesc = NULL;
5942  rel->rd_partkey = NULL;
5943  rel->rd_partkeycxt = NULL;
5944  rel->rd_partdesc = NULL;
5945  rel->rd_pdcxt = NULL;
5946  rel->rd_partcheck =