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