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