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