1 /*-------------------------------------------------------------------------
4 * POSTGRES relation descriptor cache code
6 * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/utils/cache/relcache.c
13 *-------------------------------------------------------------------------
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
24 * The following code contains many undocumented hacks. Please be
33 #include "access/htup_details.h"
34 #include "access/multixact.h"
35 #include "access/reloptions.h"
36 #include "access/sysattr.h"
37 #include "access/transam.h"
38 #include "access/xact.h"
39 #include "catalog/catalog.h"
40 #include "catalog/index.h"
41 #include "catalog/indexing.h"
42 #include "catalog/namespace.h"
43 #include "catalog/pg_amproc.h"
44 #include "catalog/pg_attrdef.h"
45 #include "catalog/pg_authid.h"
46 #include "catalog/pg_auth_members.h"
47 #include "catalog/pg_constraint.h"
48 #include "catalog/pg_database.h"
49 #include "catalog/pg_namespace.h"
50 #include "catalog/pg_opclass.h"
51 #include "catalog/pg_proc.h"
52 #include "catalog/pg_rewrite.h"
53 #include "catalog/pg_tablespace.h"
54 #include "catalog/pg_trigger.h"
55 #include "catalog/pg_type.h"
56 #include "catalog/schemapg.h"
57 #include "catalog/storage.h"
58 #include "commands/trigger.h"
59 #include "common/relpath.h"
60 #include "miscadmin.h"
61 #include "optimizer/clauses.h"
62 #include "optimizer/planmain.h"
63 #include "optimizer/prep.h"
64 #include "optimizer/var.h"
65 #include "rewrite/rewriteDefine.h"
66 #include "storage/lmgr.h"
67 #include "storage/smgr.h"
68 #include "utils/array.h"
69 #include "utils/builtins.h"
70 #include "utils/fmgroids.h"
71 #include "utils/inval.h"
72 #include "utils/lsyscache.h"
73 #include "utils/memutils.h"
74 #include "utils/relmapper.h"
75 #include "utils/resowner_private.h"
76 #include "utils/syscache.h"
77 #include "utils/tqual.h"
81 * name of relcache init file(s), used to speed up backend startup
83 #define RELCACHE_INIT_FILENAME "pg_internal.init"
85 #define RELCACHE_INIT_FILEMAGIC 0x573266 /* version ID value */
88 * hardcoded tuple descriptors, contents generated by genbki.pl
90 static const FormData_pg_attribute Desc_pg_class[Natts_pg_class] = {Schema_pg_class};
91 static const FormData_pg_attribute Desc_pg_attribute[Natts_pg_attribute] = {Schema_pg_attribute};
92 static const FormData_pg_attribute Desc_pg_proc[Natts_pg_proc] = {Schema_pg_proc};
93 static const FormData_pg_attribute Desc_pg_type[Natts_pg_type] = {Schema_pg_type};
94 static const FormData_pg_attribute Desc_pg_database[Natts_pg_database] = {Schema_pg_database};
95 static const FormData_pg_attribute Desc_pg_authid[Natts_pg_authid] = {Schema_pg_authid};
96 static const FormData_pg_attribute Desc_pg_auth_members[Natts_pg_auth_members] = {Schema_pg_auth_members};
97 static const FormData_pg_attribute Desc_pg_index[Natts_pg_index] = {Schema_pg_index};
100 * Hash tables that index the relation cache
102 * We used to index the cache by both name and OID, but now there
103 * is only an index by OID.
105 typedef struct relidcacheent
111 static HTAB *RelationIdCache;
114 * This flag is false until we have prepared the critical relcache entries
115 * that are needed to do indexscans on the tables read by relcache building.
117 bool criticalRelcachesBuilt = false;
120 * This flag is false until we have prepared the critical relcache entries
121 * for shared catalogs (which are the tables needed for login).
123 bool criticalSharedRelcachesBuilt = false;
126 * This counter counts relcache inval events received since backend startup
127 * (but only for rels that are actually in cache). Presently, we use it only
128 * to detect whether data about to be written by write_relcache_init_file()
129 * might already be obsolete.
131 static long relcacheInvalsReceived = 0L;
134 * This list remembers the OIDs of the non-shared relations cached in the
135 * database's local relcache init file. Note that there is no corresponding
136 * list for the shared relcache init file, for reasons explained in the
137 * comments for RelationCacheInitFileRemove.
139 static List *initFileRelationIds = NIL;
142 * eoxact_list[] stores the OIDs of relations that (might) need AtEOXact
143 * cleanup work. This list intentionally has limited size; if it overflows,
144 * we fall back to scanning the whole hashtable. There is no value in a very
145 * large list because (1) at some point, a hash_seq_search scan is faster than
146 * retail lookups, and (2) the value of this is to reduce EOXact work for
147 * short transactions, which can't have dirtied all that many tables anyway.
148 * EOXactListAdd() does not bother to prevent duplicate list entries, so the
149 * cleanup processing must be idempotent.
151 #define MAX_EOXACT_LIST 32
152 static Oid eoxact_list[MAX_EOXACT_LIST];
153 static int eoxact_list_len = 0;
154 static bool eoxact_list_overflowed = false;
156 #define EOXactListAdd(rel) \
158 if (eoxact_list_len < MAX_EOXACT_LIST) \
159 eoxact_list[eoxact_list_len++] = (rel)->rd_id; \
161 eoxact_list_overflowed = true; \
166 * macros to manipulate the lookup hashtables
168 #define RelationCacheInsert(RELATION) \
170 RelIdCacheEnt *idhentry; bool found; \
171 idhentry = (RelIdCacheEnt*)hash_search(RelationIdCache, \
172 (void *) &(RELATION->rd_id), \
173 HASH_ENTER, &found); \
174 /* used to give notice if found -- now just keep quiet */ \
175 idhentry->reldesc = RELATION; \
178 #define RelationIdCacheLookup(ID, RELATION) \
180 RelIdCacheEnt *hentry; \
181 hentry = (RelIdCacheEnt*)hash_search(RelationIdCache, \
185 RELATION = hentry->reldesc; \
190 #define RelationCacheDelete(RELATION) \
192 RelIdCacheEnt *idhentry; \
193 idhentry = (RelIdCacheEnt*)hash_search(RelationIdCache, \
194 (void *) &(RELATION->rd_id), \
195 HASH_REMOVE, NULL); \
196 if (idhentry == NULL) \
197 elog(WARNING, "trying to delete a rd_id reldesc that does not exist"); \
202 * Special cache for opclass-related information
204 * Note: only default support procs get cached, ie, those with
205 * lefttype = righttype = opcintype.
207 typedef struct opclasscacheent
209 Oid opclassoid; /* lookup key: OID of opclass */
210 bool valid; /* set TRUE after successful fill-in */
211 StrategyNumber numSupport; /* max # of support procs (from pg_am) */
212 Oid opcfamily; /* OID of opclass's family */
213 Oid opcintype; /* OID of opclass's declared input type */
214 RegProcedure *supportProcs; /* OIDs of support procedures */
217 static HTAB *OpClassCache = NULL;
220 /* non-export function prototypes */
222 static void RelationDestroyRelation(Relation relation);
223 static void RelationClearRelation(Relation relation, bool rebuild);
225 static void RelationReloadIndexInfo(Relation relation);
226 static void RelationFlushRelation(Relation relation);
227 static void AtEOXact_cleanup(Relation relation, bool isCommit);
228 static void AtEOSubXact_cleanup(Relation relation, bool isCommit,
229 SubTransactionId mySubid, SubTransactionId parentSubid);
230 static bool load_relcache_init_file(bool shared);
231 static void write_relcache_init_file(bool shared);
232 static void write_item(const void *data, Size len, FILE *fp);
234 static void formrdesc(const char *relationName, Oid relationReltype,
235 bool isshared, bool hasoids,
236 int natts, const FormData_pg_attribute *attrs);
238 static HeapTuple ScanPgRelation(Oid targetRelId, bool indexOK);
239 static Relation AllocateRelationDesc(Form_pg_class relp);
240 static void RelationParseRelOptions(Relation relation, HeapTuple tuple);
241 static void RelationBuildTupleDesc(Relation relation);
242 static Relation RelationBuildDesc(Oid targetRelId, bool insertIt);
243 static void RelationInitPhysicalAddr(Relation relation);
244 static void load_critical_index(Oid indexoid, Oid heapoid);
245 static TupleDesc GetPgClassDescriptor(void);
246 static TupleDesc GetPgIndexDescriptor(void);
247 static void AttrDefaultFetch(Relation relation);
248 static void CheckConstraintFetch(Relation relation);
249 static List *insert_ordered_oid(List *list, Oid datum);
250 static void IndexSupportInitialize(oidvector *indclass,
251 RegProcedure *indexSupport,
254 StrategyNumber maxSupportNumber,
255 AttrNumber maxAttributeNumber);
256 static OpClassCacheEnt *LookupOpclassInfo(Oid operatorClassOid,
257 StrategyNumber numSupport);
258 static void RelationCacheInitFileRemoveInDir(const char *tblspcpath);
259 static void unlink_initfile(const char *initfilename);
265 * This is used by RelationBuildDesc to find a pg_class
266 * tuple matching targetRelId. The caller must hold at least
267 * AccessShareLock on the target relid to prevent concurrent-update
268 * scenarios --- else our SnapshotNow scan might fail to find any
269 * version that it thinks is live.
271 * NB: the returned tuple has been copied into palloc'd storage
272 * and must eventually be freed with heap_freetuple.
275 ScanPgRelation(Oid targetRelId, bool indexOK)
277 HeapTuple pg_class_tuple;
278 Relation pg_class_desc;
279 SysScanDesc pg_class_scan;
283 * If something goes wrong during backend startup, we might find ourselves
284 * trying to read pg_class before we've selected a database. That ain't
285 * gonna work, so bail out with a useful error message. If this happens,
286 * it probably means a relcache entry that needs to be nailed isn't.
288 if (!OidIsValid(MyDatabaseId))
289 elog(FATAL, "cannot read pg_class without having selected a database");
295 ObjectIdAttributeNumber,
296 BTEqualStrategyNumber, F_OIDEQ,
297 ObjectIdGetDatum(targetRelId));
300 * Open pg_class and fetch a tuple. Force heap scan if we haven't yet
301 * built the critical relcache entries (this includes initdb and startup
302 * without a pg_internal.init file). The caller can also force a heap
303 * scan by setting indexOK == false.
305 pg_class_desc = heap_open(RelationRelationId, AccessShareLock);
306 pg_class_scan = systable_beginscan(pg_class_desc, ClassOidIndexId,
307 indexOK && criticalRelcachesBuilt,
311 pg_class_tuple = systable_getnext(pg_class_scan);
314 * Must copy tuple before releasing buffer.
316 if (HeapTupleIsValid(pg_class_tuple))
317 pg_class_tuple = heap_copytuple(pg_class_tuple);
320 systable_endscan(pg_class_scan);
321 heap_close(pg_class_desc, AccessShareLock);
323 return pg_class_tuple;
327 * AllocateRelationDesc
329 * This is used to allocate memory for a new relation descriptor
330 * and initialize the rd_rel field from the given pg_class tuple.
333 AllocateRelationDesc(Form_pg_class relp)
336 MemoryContext oldcxt;
337 Form_pg_class relationForm;
339 /* Relcache entries must live in CacheMemoryContext */
340 oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
343 * allocate and zero space for new relation descriptor
345 relation = (Relation) palloc0(sizeof(RelationData));
347 /* make sure relation is marked as having no open file yet */
348 relation->rd_smgr = NULL;
351 * Copy the relation tuple form
353 * We only allocate space for the fixed fields, ie, CLASS_TUPLE_SIZE. The
354 * variable-length fields (relacl, reloptions) are NOT stored in the
355 * relcache --- there'd be little point in it, since we don't copy the
356 * tuple's nulls bitmap and hence wouldn't know if the values are valid.
357 * Bottom line is that relacl *cannot* be retrieved from the relcache. Get
358 * it from the syscache if you need it. The same goes for the original
359 * form of reloptions (however, we do store the parsed form of reloptions
362 relationForm = (Form_pg_class) palloc(CLASS_TUPLE_SIZE);
364 memcpy(relationForm, relp, CLASS_TUPLE_SIZE);
366 /* initialize relation tuple form */
367 relation->rd_rel = relationForm;
369 /* and allocate attribute tuple form storage */
370 relation->rd_att = CreateTemplateTupleDesc(relationForm->relnatts,
371 relationForm->relhasoids);
372 /* which we mark as a reference-counted tupdesc */
373 relation->rd_att->tdrefcount = 1;
375 MemoryContextSwitchTo(oldcxt);
381 * RelationParseRelOptions
382 * Convert pg_class.reloptions into pre-parsed rd_options
384 * tuple is the real pg_class tuple (not rd_rel!) for relation
386 * Note: rd_rel and (if an index) rd_am must be valid already
389 RelationParseRelOptions(Relation relation, HeapTuple tuple)
393 relation->rd_options = NULL;
395 /* Fall out if relkind should not have options */
396 switch (relation->rd_rel->relkind)
398 case RELKIND_RELATION:
399 case RELKIND_TOASTVALUE:
408 * Fetch reloptions from tuple; have to use a hardwired descriptor because
409 * we might not have any other for pg_class yet (consider executing this
410 * code for pg_class itself)
412 options = extractRelOptions(tuple,
413 GetPgClassDescriptor(),
414 relation->rd_rel->relkind == RELKIND_INDEX ?
415 relation->rd_am->amoptions : InvalidOid);
418 * Copy parsed data into CacheMemoryContext. To guard against the
419 * possibility of leaks in the reloptions code, we want to do the actual
420 * parsing in the caller's memory context and copy the results into
421 * CacheMemoryContext after the fact.
425 relation->rd_options = MemoryContextAlloc(CacheMemoryContext,
427 memcpy(relation->rd_options, options, VARSIZE(options));
433 * RelationBuildTupleDesc
435 * Form the relation's tuple descriptor from information in
436 * the pg_attribute, pg_attrdef & pg_constraint system catalogs.
439 RelationBuildTupleDesc(Relation relation)
441 HeapTuple pg_attribute_tuple;
442 Relation pg_attribute_desc;
443 SysScanDesc pg_attribute_scan;
447 AttrDefault *attrdef = NULL;
450 /* copy some fields from pg_class row to rd_att */
451 relation->rd_att->tdtypeid = relation->rd_rel->reltype;
452 relation->rd_att->tdtypmod = -1; /* unnecessary, but... */
453 relation->rd_att->tdhasoid = relation->rd_rel->relhasoids;
455 constr = (TupleConstr *) MemoryContextAlloc(CacheMemoryContext,
456 sizeof(TupleConstr));
457 constr->has_not_null = false;
460 * Form a scan key that selects only user attributes (attnum > 0).
461 * (Eliminating system attribute rows at the index level is lots faster
462 * than fetching them.)
464 ScanKeyInit(&skey[0],
465 Anum_pg_attribute_attrelid,
466 BTEqualStrategyNumber, F_OIDEQ,
467 ObjectIdGetDatum(RelationGetRelid(relation)));
468 ScanKeyInit(&skey[1],
469 Anum_pg_attribute_attnum,
470 BTGreaterStrategyNumber, F_INT2GT,
474 * Open pg_attribute and begin a scan. Force heap scan if we haven't yet
475 * built the critical relcache entries (this includes initdb and startup
476 * without a pg_internal.init file).
478 pg_attribute_desc = heap_open(AttributeRelationId, AccessShareLock);
479 pg_attribute_scan = systable_beginscan(pg_attribute_desc,
480 AttributeRelidNumIndexId,
481 criticalRelcachesBuilt,
486 * add attribute data to relation->rd_att
488 need = relation->rd_rel->relnatts;
490 while (HeapTupleIsValid(pg_attribute_tuple = systable_getnext(pg_attribute_scan)))
492 Form_pg_attribute attp;
494 attp = (Form_pg_attribute) GETSTRUCT(pg_attribute_tuple);
496 if (attp->attnum <= 0 ||
497 attp->attnum > relation->rd_rel->relnatts)
498 elog(ERROR, "invalid attribute number %d for %s",
499 attp->attnum, RelationGetRelationName(relation));
501 memcpy(relation->rd_att->attrs[attp->attnum - 1],
503 ATTRIBUTE_FIXED_PART_SIZE);
505 /* Update constraint/default info */
506 if (attp->attnotnull)
507 constr->has_not_null = true;
512 attrdef = (AttrDefault *)
513 MemoryContextAllocZero(CacheMemoryContext,
514 relation->rd_rel->relnatts *
515 sizeof(AttrDefault));
516 attrdef[ndef].adnum = attp->attnum;
517 attrdef[ndef].adbin = NULL;
526 * end the scan and close the attribute relation
528 systable_endscan(pg_attribute_scan);
529 heap_close(pg_attribute_desc, AccessShareLock);
532 elog(ERROR, "catalog is missing %d attribute(s) for relid %u",
533 need, RelationGetRelid(relation));
536 * The attcacheoff values we read from pg_attribute should all be -1
537 * ("unknown"). Verify this if assert checking is on. They will be
538 * computed when and if needed during tuple access.
540 #ifdef USE_ASSERT_CHECKING
544 for (i = 0; i < relation->rd_rel->relnatts; i++)
545 Assert(relation->rd_att->attrs[i]->attcacheoff == -1);
550 * However, we can easily set the attcacheoff value for the first
551 * attribute: it must be zero. This eliminates the need for special cases
552 * for attnum=1 that used to exist in fastgetattr() and index_getattr().
554 if (relation->rd_rel->relnatts > 0)
555 relation->rd_att->attrs[0]->attcacheoff = 0;
558 * Set up constraint/default info
560 if (constr->has_not_null || ndef > 0 || relation->rd_rel->relchecks)
562 relation->rd_att->constr = constr;
564 if (ndef > 0) /* DEFAULTs */
566 if (ndef < relation->rd_rel->relnatts)
567 constr->defval = (AttrDefault *)
568 repalloc(attrdef, ndef * sizeof(AttrDefault));
570 constr->defval = attrdef;
571 constr->num_defval = ndef;
572 AttrDefaultFetch(relation);
575 constr->num_defval = 0;
577 if (relation->rd_rel->relchecks > 0) /* CHECKs */
579 constr->num_check = relation->rd_rel->relchecks;
580 constr->check = (ConstrCheck *)
581 MemoryContextAllocZero(CacheMemoryContext,
582 constr->num_check * sizeof(ConstrCheck));
583 CheckConstraintFetch(relation);
586 constr->num_check = 0;
591 relation->rd_att->constr = NULL;
596 * RelationBuildRuleLock
598 * Form the relation's rewrite rules from information in
599 * the pg_rewrite system catalog.
601 * Note: The rule parsetrees are potentially very complex node structures.
602 * To allow these trees to be freed when the relcache entry is flushed,
603 * we make a private memory context to hold the RuleLock information for
604 * each relcache entry that has associated rules. The context is used
605 * just for rule info, not for any other subsidiary data of the relcache
606 * entry, because that keeps the update logic in RelationClearRelation()
607 * manageable. The other subsidiary data structures are simple enough
608 * to be easy to free explicitly, anyway.
611 RelationBuildRuleLock(Relation relation)
613 MemoryContext rulescxt;
614 MemoryContext oldcxt;
615 HeapTuple rewrite_tuple;
616 Relation rewrite_desc;
617 TupleDesc rewrite_tupdesc;
618 SysScanDesc rewrite_scan;
626 * Make the private context. Parameters are set on the assumption that
627 * it'll probably not contain much data.
629 rulescxt = AllocSetContextCreate(CacheMemoryContext,
630 RelationGetRelationName(relation),
631 ALLOCSET_SMALL_MINSIZE,
632 ALLOCSET_SMALL_INITSIZE,
633 ALLOCSET_SMALL_MAXSIZE);
634 relation->rd_rulescxt = rulescxt;
637 * allocate an array to hold the rewrite rules (the array is extended if
641 rules = (RewriteRule **)
642 MemoryContextAlloc(rulescxt, sizeof(RewriteRule *) * maxlocks);
649 Anum_pg_rewrite_ev_class,
650 BTEqualStrategyNumber, F_OIDEQ,
651 ObjectIdGetDatum(RelationGetRelid(relation)));
654 * open pg_rewrite and begin a scan
656 * Note: since we scan the rules using RewriteRelRulenameIndexId, we will
657 * be reading the rules in name order, except possibly during
658 * emergency-recovery operations (ie, IgnoreSystemIndexes). This in turn
659 * ensures that rules will be fired in name order.
661 rewrite_desc = heap_open(RewriteRelationId, AccessShareLock);
662 rewrite_tupdesc = RelationGetDescr(rewrite_desc);
663 rewrite_scan = systable_beginscan(rewrite_desc,
664 RewriteRelRulenameIndexId,
668 while (HeapTupleIsValid(rewrite_tuple = systable_getnext(rewrite_scan)))
670 Form_pg_rewrite rewrite_form = (Form_pg_rewrite) GETSTRUCT(rewrite_tuple);
676 rule = (RewriteRule *) MemoryContextAlloc(rulescxt,
677 sizeof(RewriteRule));
679 rule->ruleId = HeapTupleGetOid(rewrite_tuple);
681 rule->event = rewrite_form->ev_type - '0';
682 rule->attrno = rewrite_form->ev_attr;
683 rule->enabled = rewrite_form->ev_enabled;
684 rule->isInstead = rewrite_form->is_instead;
687 * Must use heap_getattr to fetch ev_action and ev_qual. Also, the
688 * rule strings are often large enough to be toasted. To avoid
689 * leaking memory in the caller's context, do the detoasting here so
690 * we can free the detoasted version.
692 rule_datum = heap_getattr(rewrite_tuple,
693 Anum_pg_rewrite_ev_action,
697 rule_str = TextDatumGetCString(rule_datum);
698 oldcxt = MemoryContextSwitchTo(rulescxt);
699 rule->actions = (List *) stringToNode(rule_str);
700 MemoryContextSwitchTo(oldcxt);
703 rule_datum = heap_getattr(rewrite_tuple,
704 Anum_pg_rewrite_ev_qual,
708 rule_str = TextDatumGetCString(rule_datum);
709 oldcxt = MemoryContextSwitchTo(rulescxt);
710 rule->qual = (Node *) stringToNode(rule_str);
711 MemoryContextSwitchTo(oldcxt);
715 * We want the rule's table references to be checked as though by the
716 * table owner, not the user referencing the rule. Therefore, scan
717 * through the rule's actions and set the checkAsUser field on all
718 * rtable entries. We have to look at the qual as well, in case it
721 * The reason for doing this when the rule is loaded, rather than when
722 * it is stored, is that otherwise ALTER TABLE OWNER would have to
723 * grovel through stored rules to update checkAsUser fields. Scanning
724 * the rule tree during load is relatively cheap (compared to
725 * constructing it in the first place), so we do it here.
727 setRuleCheckAsUser((Node *) rule->actions, relation->rd_rel->relowner);
728 setRuleCheckAsUser(rule->qual, relation->rd_rel->relowner);
730 if (numlocks >= maxlocks)
733 rules = (RewriteRule **)
734 repalloc(rules, sizeof(RewriteRule *) * maxlocks);
736 rules[numlocks++] = rule;
740 * end the scan and close the attribute relation
742 systable_endscan(rewrite_scan);
743 heap_close(rewrite_desc, AccessShareLock);
746 * there might not be any rules (if relhasrules is out-of-date)
750 relation->rd_rules = NULL;
751 relation->rd_rulescxt = NULL;
752 MemoryContextDelete(rulescxt);
757 * form a RuleLock and insert into relation
759 rulelock = (RuleLock *) MemoryContextAlloc(rulescxt, sizeof(RuleLock));
760 rulelock->numLocks = numlocks;
761 rulelock->rules = rules;
763 relation->rd_rules = rulelock;
769 * Determine whether two RuleLocks are equivalent
771 * Probably this should be in the rules code someplace...
774 equalRuleLocks(RuleLock *rlock1, RuleLock *rlock2)
779 * As of 7.3 we assume the rule ordering is repeatable, because
780 * RelationBuildRuleLock should read 'em in a consistent order. So just
781 * compare corresponding slots.
787 if (rlock1->numLocks != rlock2->numLocks)
789 for (i = 0; i < rlock1->numLocks; i++)
791 RewriteRule *rule1 = rlock1->rules[i];
792 RewriteRule *rule2 = rlock2->rules[i];
794 if (rule1->ruleId != rule2->ruleId)
796 if (rule1->event != rule2->event)
798 if (rule1->attrno != rule2->attrno)
800 if (rule1->enabled != rule2->enabled)
802 if (rule1->isInstead != rule2->isInstead)
804 if (!equal(rule1->qual, rule2->qual))
806 if (!equal(rule1->actions, rule2->actions))
810 else if (rlock2 != NULL)
819 * Build a relation descriptor. The caller must hold at least
820 * AccessShareLock on the target relid.
822 * The new descriptor is inserted into the hash table if insertIt is true.
824 * Returns NULL if no pg_class row could be found for the given relid
825 * (suggesting we are trying to access a just-deleted relation).
826 * Any other error is reported via elog.
829 RelationBuildDesc(Oid targetRelId, bool insertIt)
833 HeapTuple pg_class_tuple;
837 * find the tuple in pg_class corresponding to the given relation id
839 pg_class_tuple = ScanPgRelation(targetRelId, true);
842 * if no such tuple exists, return NULL
844 if (!HeapTupleIsValid(pg_class_tuple))
848 * get information from the pg_class_tuple
850 relid = HeapTupleGetOid(pg_class_tuple);
851 relp = (Form_pg_class) GETSTRUCT(pg_class_tuple);
852 Assert(relid == targetRelId);
855 * allocate storage for the relation descriptor, and copy pg_class_tuple
856 * to relation->rd_rel.
858 relation = AllocateRelationDesc(relp);
861 * initialize the relation's relation id (relation->rd_id)
863 RelationGetRelid(relation) = relid;
866 * normal relations are not nailed into the cache; nor can a pre-existing
867 * relation be new. It could be temp though. (Actually, it could be new
868 * too, but it's okay to forget that fact if forced to flush the entry.)
870 relation->rd_refcnt = 0;
871 relation->rd_isnailed = false;
872 relation->rd_createSubid = InvalidSubTransactionId;
873 relation->rd_newRelfilenodeSubid = InvalidSubTransactionId;
874 switch (relation->rd_rel->relpersistence)
876 case RELPERSISTENCE_UNLOGGED:
877 case RELPERSISTENCE_PERMANENT:
878 relation->rd_backend = InvalidBackendId;
879 relation->rd_islocaltemp = false;
881 case RELPERSISTENCE_TEMP:
882 if (isTempOrToastNamespace(relation->rd_rel->relnamespace))
884 relation->rd_backend = MyBackendId;
885 relation->rd_islocaltemp = true;
890 * If it's a temp table, but not one of ours, we have to use
891 * the slow, grotty method to figure out the owning backend.
893 * Note: it's possible that rd_backend gets set to MyBackendId
894 * here, in case we are looking at a pg_class entry left over
895 * from a crashed backend that coincidentally had the same
896 * BackendId we're using. We should *not* consider such a
897 * table to be "ours"; this is why we need the separate
898 * rd_islocaltemp flag. The pg_class entry will get flushed
899 * if/when we clean out the corresponding temp table namespace
900 * in preparation for using it.
902 relation->rd_backend =
903 GetTempNamespaceBackendId(relation->rd_rel->relnamespace);
904 Assert(relation->rd_backend != InvalidBackendId);
905 relation->rd_islocaltemp = false;
909 elog(ERROR, "invalid relpersistence: %c",
910 relation->rd_rel->relpersistence);
915 * initialize the tuple descriptor (relation->rd_att).
917 RelationBuildTupleDesc(relation);
920 * Fetch rules and triggers that affect this relation
922 if (relation->rd_rel->relhasrules)
923 RelationBuildRuleLock(relation);
926 relation->rd_rules = NULL;
927 relation->rd_rulescxt = NULL;
930 if (relation->rd_rel->relhastriggers)
931 RelationBuildTriggers(relation);
933 relation->trigdesc = NULL;
936 * if it's an index, initialize index-related information
938 if (OidIsValid(relation->rd_rel->relam))
939 RelationInitIndexAccessInfo(relation);
941 /* extract reloptions if any */
942 RelationParseRelOptions(relation, pg_class_tuple);
945 * initialize the relation lock manager information
947 RelationInitLockInfo(relation); /* see lmgr.c */
950 * initialize physical addressing information for the relation
952 RelationInitPhysicalAddr(relation);
954 /* make sure relation is marked as having no open file yet */
955 relation->rd_smgr = NULL;
958 * now we can free the memory allocated for pg_class_tuple
960 heap_freetuple(pg_class_tuple);
963 * Insert newly created relation into relcache hash table, if requested.
966 RelationCacheInsert(relation);
968 /* It's fully valid */
969 relation->rd_isvalid = true;
975 * Initialize the physical addressing info (RelFileNode) for a relcache entry
977 * Note: at the physical level, relations in the pg_global tablespace must
978 * be treated as shared, even if relisshared isn't set. Hence we do not
979 * look at relisshared here.
982 RelationInitPhysicalAddr(Relation relation)
984 if (relation->rd_rel->reltablespace)
985 relation->rd_node.spcNode = relation->rd_rel->reltablespace;
987 relation->rd_node.spcNode = MyDatabaseTableSpace;
988 if (relation->rd_node.spcNode == GLOBALTABLESPACE_OID)
989 relation->rd_node.dbNode = InvalidOid;
991 relation->rd_node.dbNode = MyDatabaseId;
992 if (relation->rd_rel->relfilenode)
993 relation->rd_node.relNode = relation->rd_rel->relfilenode;
996 /* Consult the relation mapper */
997 relation->rd_node.relNode =
998 RelationMapOidToFilenode(relation->rd_id,
999 relation->rd_rel->relisshared);
1000 if (!OidIsValid(relation->rd_node.relNode))
1001 elog(ERROR, "could not find relation mapping for relation \"%s\", OID %u",
1002 RelationGetRelationName(relation), relation->rd_id);
1007 * Initialize index-access-method support data for an index relation
1010 RelationInitIndexAccessInfo(Relation relation)
1015 Datum indclassDatum;
1016 Datum indoptionDatum;
1019 oidvector *indclass;
1020 int2vector *indoption;
1021 MemoryContext indexcxt;
1022 MemoryContext oldcontext;
1027 * Make a copy of the pg_index entry for the index. Since pg_index
1028 * contains variable-length and possibly-null fields, we have to do this
1029 * honestly rather than just treating it as a Form_pg_index struct.
1031 tuple = SearchSysCache1(INDEXRELID,
1032 ObjectIdGetDatum(RelationGetRelid(relation)));
1033 if (!HeapTupleIsValid(tuple))
1034 elog(ERROR, "cache lookup failed for index %u",
1035 RelationGetRelid(relation));
1036 oldcontext = MemoryContextSwitchTo(CacheMemoryContext);
1037 relation->rd_indextuple = heap_copytuple(tuple);
1038 relation->rd_index = (Form_pg_index) GETSTRUCT(relation->rd_indextuple);
1039 MemoryContextSwitchTo(oldcontext);
1040 ReleaseSysCache(tuple);
1043 * Make a copy of the pg_am entry for the index's access method
1045 tuple = SearchSysCache1(AMOID, ObjectIdGetDatum(relation->rd_rel->relam));
1046 if (!HeapTupleIsValid(tuple))
1047 elog(ERROR, "cache lookup failed for access method %u",
1048 relation->rd_rel->relam);
1049 aform = (Form_pg_am) MemoryContextAlloc(CacheMemoryContext, sizeof *aform);
1050 memcpy(aform, GETSTRUCT(tuple), sizeof *aform);
1051 ReleaseSysCache(tuple);
1052 relation->rd_am = aform;
1054 natts = relation->rd_rel->relnatts;
1055 if (natts != relation->rd_index->indnatts)
1056 elog(ERROR, "relnatts disagrees with indnatts for index %u",
1057 RelationGetRelid(relation));
1058 amsupport = aform->amsupport;
1061 * Make the private context to hold index access info. The reason we need
1062 * a context, and not just a couple of pallocs, is so that we won't leak
1063 * any subsidiary info attached to fmgr lookup records.
1065 * Context parameters are set on the assumption that it'll probably not
1066 * contain much data.
1068 indexcxt = AllocSetContextCreate(CacheMemoryContext,
1069 RelationGetRelationName(relation),
1070 ALLOCSET_SMALL_MINSIZE,
1071 ALLOCSET_SMALL_INITSIZE,
1072 ALLOCSET_SMALL_MAXSIZE);
1073 relation->rd_indexcxt = indexcxt;
1076 * Allocate arrays to hold data
1078 relation->rd_aminfo = (RelationAmInfo *)
1079 MemoryContextAllocZero(indexcxt, sizeof(RelationAmInfo));
1081 relation->rd_opfamily = (Oid *)
1082 MemoryContextAllocZero(indexcxt, natts * sizeof(Oid));
1083 relation->rd_opcintype = (Oid *)
1084 MemoryContextAllocZero(indexcxt, natts * sizeof(Oid));
1088 int nsupport = natts * amsupport;
1090 relation->rd_support = (RegProcedure *)
1091 MemoryContextAllocZero(indexcxt, nsupport * sizeof(RegProcedure));
1092 relation->rd_supportinfo = (FmgrInfo *)
1093 MemoryContextAllocZero(indexcxt, nsupport * sizeof(FmgrInfo));
1097 relation->rd_support = NULL;
1098 relation->rd_supportinfo = NULL;
1101 relation->rd_indcollation = (Oid *)
1102 MemoryContextAllocZero(indexcxt, natts * sizeof(Oid));
1104 relation->rd_indoption = (int16 *)
1105 MemoryContextAllocZero(indexcxt, natts * sizeof(int16));
1108 * indcollation cannot be referenced directly through the C struct,
1109 * because it comes after the variable-width indkey field. Must extract
1110 * the datum the hard way...
1112 indcollDatum = fastgetattr(relation->rd_indextuple,
1113 Anum_pg_index_indcollation,
1114 GetPgIndexDescriptor(),
1117 indcoll = (oidvector *) DatumGetPointer(indcollDatum);
1118 memcpy(relation->rd_indcollation, indcoll->values, natts * sizeof(Oid));
1121 * indclass cannot be referenced directly through the C struct, because it
1122 * comes after the variable-width indkey field. Must extract the datum
1125 indclassDatum = fastgetattr(relation->rd_indextuple,
1126 Anum_pg_index_indclass,
1127 GetPgIndexDescriptor(),
1130 indclass = (oidvector *) DatumGetPointer(indclassDatum);
1133 * Fill the support procedure OID array, as well as the info about
1134 * opfamilies and opclass input types. (aminfo and supportinfo are left
1135 * as zeroes, and are filled on-the-fly when used)
1137 IndexSupportInitialize(indclass, relation->rd_support,
1138 relation->rd_opfamily, relation->rd_opcintype,
1142 * Similarly extract indoption and copy it to the cache entry
1144 indoptionDatum = fastgetattr(relation->rd_indextuple,
1145 Anum_pg_index_indoption,
1146 GetPgIndexDescriptor(),
1149 indoption = (int2vector *) DatumGetPointer(indoptionDatum);
1150 memcpy(relation->rd_indoption, indoption->values, natts * sizeof(int16));
1153 * expressions, predicate, exclusion caches will be filled later
1155 relation->rd_indexprs = NIL;
1156 relation->rd_indpred = NIL;
1157 relation->rd_exclops = NULL;
1158 relation->rd_exclprocs = NULL;
1159 relation->rd_exclstrats = NULL;
1160 relation->rd_amcache = NULL;
1164 * IndexSupportInitialize
1165 * Initializes an index's cached opclass information,
1166 * given the index's pg_index.indclass entry.
1168 * Data is returned into *indexSupport, *opFamily, and *opcInType,
1169 * which are arrays allocated by the caller.
1171 * The caller also passes maxSupportNumber and maxAttributeNumber, since these
1172 * indicate the size of the arrays it has allocated --- but in practice these
1173 * numbers must always match those obtainable from the system catalog entries
1174 * for the index and access method.
1177 IndexSupportInitialize(oidvector *indclass,
1178 RegProcedure *indexSupport,
1181 StrategyNumber maxSupportNumber,
1182 AttrNumber maxAttributeNumber)
1186 for (attIndex = 0; attIndex < maxAttributeNumber; attIndex++)
1188 OpClassCacheEnt *opcentry;
1190 if (!OidIsValid(indclass->values[attIndex]))
1191 elog(ERROR, "bogus pg_index tuple");
1193 /* look up the info for this opclass, using a cache */
1194 opcentry = LookupOpclassInfo(indclass->values[attIndex],
1197 /* copy cached data into relcache entry */
1198 opFamily[attIndex] = opcentry->opcfamily;
1199 opcInType[attIndex] = opcentry->opcintype;
1200 if (maxSupportNumber > 0)
1201 memcpy(&indexSupport[attIndex * maxSupportNumber],
1202 opcentry->supportProcs,
1203 maxSupportNumber * sizeof(RegProcedure));
1210 * This routine maintains a per-opclass cache of the information needed
1211 * by IndexSupportInitialize(). This is more efficient than relying on
1212 * the catalog cache, because we can load all the info about a particular
1213 * opclass in a single indexscan of pg_amproc.
1215 * The information from pg_am about expected range of support function
1216 * numbers is passed in, rather than being looked up, mainly because the
1217 * caller will have it already.
1219 * Note there is no provision for flushing the cache. This is OK at the
1220 * moment because there is no way to ALTER any interesting properties of an
1221 * existing opclass --- all you can do is drop it, which will result in
1222 * a useless but harmless dead entry in the cache. To support altering
1223 * opclass membership (not the same as opfamily membership!), we'd need to
1224 * be able to flush this cache as well as the contents of relcache entries
1227 static OpClassCacheEnt *
1228 LookupOpclassInfo(Oid operatorClassOid,
1229 StrategyNumber numSupport)
1231 OpClassCacheEnt *opcentry;
1235 ScanKeyData skey[3];
1239 if (OpClassCache == NULL)
1241 /* First time through: initialize the opclass cache */
1244 MemSet(&ctl, 0, sizeof(ctl));
1245 ctl.keysize = sizeof(Oid);
1246 ctl.entrysize = sizeof(OpClassCacheEnt);
1247 ctl.hash = oid_hash;
1248 OpClassCache = hash_create("Operator class cache", 64,
1249 &ctl, HASH_ELEM | HASH_FUNCTION);
1251 /* Also make sure CacheMemoryContext exists */
1252 if (!CacheMemoryContext)
1253 CreateCacheMemoryContext();
1256 opcentry = (OpClassCacheEnt *) hash_search(OpClassCache,
1257 (void *) &operatorClassOid,
1258 HASH_ENTER, &found);
1262 /* Need to allocate memory for new entry */
1263 opcentry->valid = false; /* until known OK */
1264 opcentry->numSupport = numSupport;
1267 opcentry->supportProcs = (RegProcedure *)
1268 MemoryContextAllocZero(CacheMemoryContext,
1269 numSupport * sizeof(RegProcedure));
1271 opcentry->supportProcs = NULL;
1275 Assert(numSupport == opcentry->numSupport);
1279 * When testing for cache-flush hazards, we intentionally disable the
1280 * operator class cache and force reloading of the info on each call. This
1281 * is helpful because we want to test the case where a cache flush occurs
1282 * while we are loading the info, and it's very hard to provoke that if
1283 * this happens only once per opclass per backend.
1285 #if defined(CLOBBER_CACHE_ALWAYS)
1286 opcentry->valid = false;
1289 if (opcentry->valid)
1293 * Need to fill in new entry.
1295 * To avoid infinite recursion during startup, force heap scans if we're
1296 * looking up info for the opclasses used by the indexes we would like to
1299 indexOK = criticalRelcachesBuilt ||
1300 (operatorClassOid != OID_BTREE_OPS_OID &&
1301 operatorClassOid != INT2_BTREE_OPS_OID);
1304 * We have to fetch the pg_opclass row to determine its opfamily and
1305 * opcintype, which are needed to look up related operators and functions.
1306 * It'd be convenient to use the syscache here, but that probably doesn't
1307 * work while bootstrapping.
1309 ScanKeyInit(&skey[0],
1310 ObjectIdAttributeNumber,
1311 BTEqualStrategyNumber, F_OIDEQ,
1312 ObjectIdGetDatum(operatorClassOid));
1313 rel = heap_open(OperatorClassRelationId, AccessShareLock);
1314 scan = systable_beginscan(rel, OpclassOidIndexId, indexOK,
1315 SnapshotNow, 1, skey);
1317 if (HeapTupleIsValid(htup = systable_getnext(scan)))
1319 Form_pg_opclass opclassform = (Form_pg_opclass) GETSTRUCT(htup);
1321 opcentry->opcfamily = opclassform->opcfamily;
1322 opcentry->opcintype = opclassform->opcintype;
1325 elog(ERROR, "could not find tuple for opclass %u", operatorClassOid);
1327 systable_endscan(scan);
1328 heap_close(rel, AccessShareLock);
1331 * Scan pg_amproc to obtain support procs for the opclass. We only fetch
1332 * the default ones (those with lefttype = righttype = opcintype).
1336 ScanKeyInit(&skey[0],
1337 Anum_pg_amproc_amprocfamily,
1338 BTEqualStrategyNumber, F_OIDEQ,
1339 ObjectIdGetDatum(opcentry->opcfamily));
1340 ScanKeyInit(&skey[1],
1341 Anum_pg_amproc_amproclefttype,
1342 BTEqualStrategyNumber, F_OIDEQ,
1343 ObjectIdGetDatum(opcentry->opcintype));
1344 ScanKeyInit(&skey[2],
1345 Anum_pg_amproc_amprocrighttype,
1346 BTEqualStrategyNumber, F_OIDEQ,
1347 ObjectIdGetDatum(opcentry->opcintype));
1348 rel = heap_open(AccessMethodProcedureRelationId, AccessShareLock);
1349 scan = systable_beginscan(rel, AccessMethodProcedureIndexId, indexOK,
1350 SnapshotNow, 3, skey);
1352 while (HeapTupleIsValid(htup = systable_getnext(scan)))
1354 Form_pg_amproc amprocform = (Form_pg_amproc) GETSTRUCT(htup);
1356 if (amprocform->amprocnum <= 0 ||
1357 (StrategyNumber) amprocform->amprocnum > numSupport)
1358 elog(ERROR, "invalid amproc number %d for opclass %u",
1359 amprocform->amprocnum, operatorClassOid);
1361 opcentry->supportProcs[amprocform->amprocnum - 1] =
1365 systable_endscan(scan);
1366 heap_close(rel, AccessShareLock);
1369 opcentry->valid = true;
1377 * This is a special cut-down version of RelationBuildDesc(),
1378 * used while initializing the relcache.
1379 * The relation descriptor is built just from the supplied parameters,
1380 * without actually looking at any system table entries. We cheat
1381 * quite a lot since we only need to work for a few basic system
1384 * formrdesc is currently used for: pg_database, pg_authid, pg_auth_members,
1385 * pg_class, pg_attribute, pg_proc, and pg_type
1386 * (see RelationCacheInitializePhase2/3).
1388 * Note that these catalogs can't have constraints (except attnotnull),
1389 * default values, rules, or triggers, since we don't cope with any of that.
1390 * (Well, actually, this only matters for properties that need to be valid
1391 * during bootstrap or before RelationCacheInitializePhase3 runs, and none of
1392 * these properties matter then...)
1394 * NOTE: we assume we are already switched into CacheMemoryContext.
1397 formrdesc(const char *relationName, Oid relationReltype,
1398 bool isshared, bool hasoids,
1399 int natts, const FormData_pg_attribute *attrs)
1406 * allocate new relation desc, clear all fields of reldesc
1408 relation = (Relation) palloc0(sizeof(RelationData));
1410 /* make sure relation is marked as having no open file yet */
1411 relation->rd_smgr = NULL;
1414 * initialize reference count: 1 because it is nailed in cache
1416 relation->rd_refcnt = 1;
1419 * all entries built with this routine are nailed-in-cache; none are for
1420 * new or temp relations.
1422 relation->rd_isnailed = true;
1423 relation->rd_createSubid = InvalidSubTransactionId;
1424 relation->rd_newRelfilenodeSubid = InvalidSubTransactionId;
1425 relation->rd_backend = InvalidBackendId;
1426 relation->rd_islocaltemp = false;
1429 * initialize relation tuple form
1431 * The data we insert here is pretty incomplete/bogus, but it'll serve to
1432 * get us launched. RelationCacheInitializePhase3() will read the real
1433 * data from pg_class and replace what we've done here. Note in
1434 * particular that relowner is left as zero; this cues
1435 * RelationCacheInitializePhase3 that the real data isn't there yet.
1437 relation->rd_rel = (Form_pg_class) palloc0(CLASS_TUPLE_SIZE);
1439 namestrcpy(&relation->rd_rel->relname, relationName);
1440 relation->rd_rel->relnamespace = PG_CATALOG_NAMESPACE;
1441 relation->rd_rel->reltype = relationReltype;
1444 * It's important to distinguish between shared and non-shared relations,
1445 * even at bootstrap time, to make sure we know where they are stored.
1447 relation->rd_rel->relisshared = isshared;
1449 relation->rd_rel->reltablespace = GLOBALTABLESPACE_OID;
1451 /* formrdesc is used only for permanent relations */
1452 relation->rd_rel->relpersistence = RELPERSISTENCE_PERMANENT;
1454 relation->rd_rel->relpages = 0;
1455 relation->rd_rel->reltuples = 0;
1456 relation->rd_rel->relallvisible = 0;
1457 relation->rd_rel->relkind = RELKIND_RELATION;
1458 relation->rd_rel->relhasoids = hasoids;
1459 relation->rd_rel->relnatts = (int16) natts;
1462 * initialize attribute tuple form
1464 * Unlike the case with the relation tuple, this data had better be right
1465 * because it will never be replaced. The data comes from
1466 * src/include/catalog/ headers via genbki.pl.
1468 relation->rd_att = CreateTemplateTupleDesc(natts, hasoids);
1469 relation->rd_att->tdrefcount = 1; /* mark as refcounted */
1471 relation->rd_att->tdtypeid = relationReltype;
1472 relation->rd_att->tdtypmod = -1; /* unnecessary, but... */
1475 * initialize tuple desc info
1477 has_not_null = false;
1478 for (i = 0; i < natts; i++)
1480 memcpy(relation->rd_att->attrs[i],
1482 ATTRIBUTE_FIXED_PART_SIZE);
1483 has_not_null |= attrs[i].attnotnull;
1484 /* make sure attcacheoff is valid */
1485 relation->rd_att->attrs[i]->attcacheoff = -1;
1488 /* initialize first attribute's attcacheoff, cf RelationBuildTupleDesc */
1489 relation->rd_att->attrs[0]->attcacheoff = 0;
1491 /* mark not-null status */
1494 TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr));
1496 constr->has_not_null = true;
1497 relation->rd_att->constr = constr;
1501 * initialize relation id from info in att array (my, this is ugly)
1503 RelationGetRelid(relation) = relation->rd_att->attrs[0]->attrelid;
1506 * All relations made with formrdesc are mapped. This is necessarily so
1507 * because there is no other way to know what filenode they currently
1508 * have. In bootstrap mode, add them to the initial relation mapper data,
1509 * specifying that the initial filenode is the same as the OID.
1511 relation->rd_rel->relfilenode = InvalidOid;
1512 if (IsBootstrapProcessingMode())
1513 RelationMapUpdateMap(RelationGetRelid(relation),
1514 RelationGetRelid(relation),
1518 * initialize the relation lock manager information
1520 RelationInitLockInfo(relation); /* see lmgr.c */
1523 * initialize physical addressing information for the relation
1525 RelationInitPhysicalAddr(relation);
1528 * initialize the rel-has-index flag, using hardwired knowledge
1530 if (IsBootstrapProcessingMode())
1532 /* In bootstrap mode, we have no indexes */
1533 relation->rd_rel->relhasindex = false;
1537 /* Otherwise, all the rels formrdesc is used for have indexes */
1538 relation->rd_rel->relhasindex = true;
1542 * add new reldesc to relcache
1544 RelationCacheInsert(relation);
1546 /* It's fully valid */
1547 relation->rd_isvalid = true;
1551 /* ----------------------------------------------------------------
1552 * Relation Descriptor Lookup Interface
1553 * ----------------------------------------------------------------
1557 * RelationIdGetRelation
1559 * Lookup a reldesc by OID; make one if not already in cache.
1561 * Returns NULL if no pg_class row could be found for the given relid
1562 * (suggesting we are trying to access a just-deleted relation).
1563 * Any other error is reported via elog.
1565 * NB: caller should already have at least AccessShareLock on the
1566 * relation ID, else there are nasty race conditions.
1568 * NB: relation ref count is incremented, or set to 1 if new entry.
1569 * Caller should eventually decrement count. (Usually,
1570 * that happens by calling RelationClose().)
1573 RelationIdGetRelation(Oid relationId)
1578 * first try to find reldesc in the cache
1580 RelationIdCacheLookup(relationId, rd);
1582 if (RelationIsValid(rd))
1584 RelationIncrementReferenceCount(rd);
1585 /* revalidate cache entry if necessary */
1586 if (!rd->rd_isvalid)
1589 * Indexes only have a limited number of possible schema changes,
1590 * and we don't want to use the full-blown procedure because it's
1591 * a headache for indexes that reload itself depends on.
1593 if (rd->rd_rel->relkind == RELKIND_INDEX)
1594 RelationReloadIndexInfo(rd);
1596 RelationClearRelation(rd, true);
1602 * no reldesc in the cache, so have RelationBuildDesc() build one and add
1605 rd = RelationBuildDesc(relationId, true);
1606 if (RelationIsValid(rd))
1607 RelationIncrementReferenceCount(rd);
1611 /* ----------------------------------------------------------------
1612 * cache invalidation support routines
1613 * ----------------------------------------------------------------
1617 * RelationIncrementReferenceCount
1618 * Increments relation reference count.
1620 * Note: bootstrap mode has its own weird ideas about relation refcount
1621 * behavior; we ought to fix it someday, but for now, just disable
1622 * reference count ownership tracking in bootstrap mode.
1625 RelationIncrementReferenceCount(Relation rel)
1627 ResourceOwnerEnlargeRelationRefs(CurrentResourceOwner);
1628 rel->rd_refcnt += 1;
1629 if (!IsBootstrapProcessingMode())
1630 ResourceOwnerRememberRelationRef(CurrentResourceOwner, rel);
1634 * RelationDecrementReferenceCount
1635 * Decrements relation reference count.
1638 RelationDecrementReferenceCount(Relation rel)
1640 Assert(rel->rd_refcnt > 0);
1641 rel->rd_refcnt -= 1;
1642 if (!IsBootstrapProcessingMode())
1643 ResourceOwnerForgetRelationRef(CurrentResourceOwner, rel);
1647 * RelationClose - close an open relation
1649 * Actually, we just decrement the refcount.
1651 * NOTE: if compiled with -DRELCACHE_FORCE_RELEASE then relcache entries
1652 * will be freed as soon as their refcount goes to zero. In combination
1653 * with aset.c's CLOBBER_FREED_MEMORY option, this provides a good test
1654 * to catch references to already-released relcache entries. It slows
1655 * things down quite a bit, however.
1658 RelationClose(Relation relation)
1660 /* Note: no locking manipulations needed */
1661 RelationDecrementReferenceCount(relation);
1663 #ifdef RELCACHE_FORCE_RELEASE
1664 if (RelationHasReferenceCountZero(relation) &&
1665 relation->rd_createSubid == InvalidSubTransactionId &&
1666 relation->rd_newRelfilenodeSubid == InvalidSubTransactionId)
1667 RelationClearRelation(relation, false);
1672 * RelationReloadIndexInfo - reload minimal information for an open index
1674 * This function is used only for indexes. A relcache inval on an index
1675 * can mean that its pg_class or pg_index row changed. There are only
1676 * very limited changes that are allowed to an existing index's schema,
1677 * so we can update the relcache entry without a complete rebuild; which
1678 * is fortunate because we can't rebuild an index entry that is "nailed"
1679 * and/or in active use. We support full replacement of the pg_class row,
1680 * as well as updates of a few simple fields of the pg_index row.
1682 * We can't necessarily reread the catalog rows right away; we might be
1683 * in a failed transaction when we receive the SI notification. If so,
1684 * RelationClearRelation just marks the entry as invalid by setting
1685 * rd_isvalid to false. This routine is called to fix the entry when it
1688 * We assume that at the time we are called, we have at least AccessShareLock
1689 * on the target index. (Note: in the calls from RelationClearRelation,
1690 * this is legitimate because we know the rel has positive refcount.)
1692 * If the target index is an index on pg_class or pg_index, we'd better have
1693 * previously gotten at least AccessShareLock on its underlying catalog,
1694 * else we are at risk of deadlock against someone trying to exclusive-lock
1695 * the heap and index in that order. This is ensured in current usage by
1696 * only applying this to indexes being opened or having positive refcount.
1699 RelationReloadIndexInfo(Relation relation)
1702 HeapTuple pg_class_tuple;
1705 /* Should be called only for invalidated indexes */
1706 Assert(relation->rd_rel->relkind == RELKIND_INDEX &&
1707 !relation->rd_isvalid);
1708 /* Should be closed at smgr level */
1709 Assert(relation->rd_smgr == NULL);
1711 /* Must free any AM cached data upon relcache flush */
1712 if (relation->rd_amcache)
1713 pfree(relation->rd_amcache);
1714 relation->rd_amcache = NULL;
1717 * If it's a shared index, we might be called before backend startup has
1718 * finished selecting a database, in which case we have no way to read
1719 * pg_class yet. However, a shared index can never have any significant
1720 * schema updates, so it's okay to ignore the invalidation signal. Just
1721 * mark it valid and return without doing anything more.
1723 if (relation->rd_rel->relisshared && !criticalRelcachesBuilt)
1725 relation->rd_isvalid = true;
1730 * Read the pg_class row
1732 * Don't try to use an indexscan of pg_class_oid_index to reload the info
1733 * for pg_class_oid_index ...
1735 indexOK = (RelationGetRelid(relation) != ClassOidIndexId);
1736 pg_class_tuple = ScanPgRelation(RelationGetRelid(relation), indexOK);
1737 if (!HeapTupleIsValid(pg_class_tuple))
1738 elog(ERROR, "could not find pg_class tuple for index %u",
1739 RelationGetRelid(relation));
1740 relp = (Form_pg_class) GETSTRUCT(pg_class_tuple);
1741 memcpy(relation->rd_rel, relp, CLASS_TUPLE_SIZE);
1742 /* Reload reloptions in case they changed */
1743 if (relation->rd_options)
1744 pfree(relation->rd_options);
1745 RelationParseRelOptions(relation, pg_class_tuple);
1746 /* done with pg_class tuple */
1747 heap_freetuple(pg_class_tuple);
1748 /* We must recalculate physical address in case it changed */
1749 RelationInitPhysicalAddr(relation);
1752 * For a non-system index, there are fields of the pg_index row that are
1753 * allowed to change, so re-read that row and update the relcache entry.
1754 * Most of the info derived from pg_index (such as support function lookup
1755 * info) cannot change, and indeed the whole point of this routine is to
1756 * update the relcache entry without clobbering that data; so wholesale
1757 * replacement is not appropriate.
1759 if (!IsSystemRelation(relation))
1762 Form_pg_index index;
1764 tuple = SearchSysCache1(INDEXRELID,
1765 ObjectIdGetDatum(RelationGetRelid(relation)));
1766 if (!HeapTupleIsValid(tuple))
1767 elog(ERROR, "cache lookup failed for index %u",
1768 RelationGetRelid(relation));
1769 index = (Form_pg_index) GETSTRUCT(tuple);
1772 * Basically, let's just copy all the bool fields. There are one or
1773 * two of these that can't actually change in the current code, but
1774 * it's not worth it to track exactly which ones they are. None of
1775 * the array fields are allowed to change, though.
1777 relation->rd_index->indisunique = index->indisunique;
1778 relation->rd_index->indisprimary = index->indisprimary;
1779 relation->rd_index->indisexclusion = index->indisexclusion;
1780 relation->rd_index->indimmediate = index->indimmediate;
1781 relation->rd_index->indisclustered = index->indisclustered;
1782 relation->rd_index->indisvalid = index->indisvalid;
1783 relation->rd_index->indcheckxmin = index->indcheckxmin;
1784 relation->rd_index->indisready = index->indisready;
1785 relation->rd_index->indislive = index->indislive;
1787 /* Copy xmin too, as that is needed to make sense of indcheckxmin */
1788 HeapTupleHeaderSetXmin(relation->rd_indextuple->t_data,
1789 HeapTupleHeaderGetXmin(tuple->t_data));
1791 ReleaseSysCache(tuple);
1794 /* Okay, now it's valid again */
1795 relation->rd_isvalid = true;
1799 * RelationDestroyRelation
1801 * Physically delete a relation cache entry and all subsidiary data.
1802 * Caller must already have unhooked the entry from the hash table.
1805 RelationDestroyRelation(Relation relation)
1807 Assert(RelationHasReferenceCountZero(relation));
1810 * Make sure smgr and lower levels close the relation's files, if they
1811 * weren't closed already. (This was probably done by caller, but let's
1812 * just be real sure.)
1814 RelationCloseSmgr(relation);
1817 * Free all the subsidiary data structures of the relcache entry, then the
1820 if (relation->rd_rel)
1821 pfree(relation->rd_rel);
1822 /* can't use DecrTupleDescRefCount here */
1823 Assert(relation->rd_att->tdrefcount > 0);
1824 if (--relation->rd_att->tdrefcount == 0)
1825 FreeTupleDesc(relation->rd_att);
1826 list_free(relation->rd_indexlist);
1827 bms_free(relation->rd_indexattr);
1828 FreeTriggerDesc(relation->trigdesc);
1829 if (relation->rd_options)
1830 pfree(relation->rd_options);
1831 if (relation->rd_indextuple)
1832 pfree(relation->rd_indextuple);
1833 if (relation->rd_am)
1834 pfree(relation->rd_am);
1835 if (relation->rd_indexcxt)
1836 MemoryContextDelete(relation->rd_indexcxt);
1837 if (relation->rd_rulescxt)
1838 MemoryContextDelete(relation->rd_rulescxt);
1843 * RelationClearRelation
1845 * Physically blow away a relation cache entry, or reset it and rebuild
1846 * it from scratch (that is, from catalog entries). The latter path is
1847 * used when we are notified of a change to an open relation (one with
1850 * NB: when rebuilding, we'd better hold some lock on the relation,
1851 * else the catalog data we need to read could be changing under us.
1852 * Also, a rel to be rebuilt had better have refcnt > 0. This is because
1853 * an sinval reset could happen while we're accessing the catalogs, and
1854 * the rel would get blown away underneath us by RelationCacheInvalidate
1855 * if it has zero refcnt.
1857 * The "rebuild" parameter is redundant in current usage because it has
1858 * to match the relation's refcnt status, but we keep it as a crosscheck
1859 * that we're doing what the caller expects.
1862 RelationClearRelation(Relation relation, bool rebuild)
1865 * As per notes above, a rel to be rebuilt MUST have refcnt > 0; while of
1866 * course it would be a bad idea to blow away one with nonzero refcnt.
1869 !RelationHasReferenceCountZero(relation) :
1870 RelationHasReferenceCountZero(relation));
1873 * Make sure smgr and lower levels close the relation's files, if they
1874 * weren't closed already. If the relation is not getting deleted, the
1875 * next smgr access should reopen the files automatically. This ensures
1876 * that the low-level file access state is updated after, say, a vacuum
1879 RelationCloseSmgr(relation);
1882 * Never, never ever blow away a nailed-in system relation, because we'd
1883 * be unable to recover. However, we must redo RelationInitPhysicalAddr
1884 * in case it is a mapped relation whose mapping changed.
1886 * If it's a nailed index, then we need to re-read the pg_class row to see
1887 * if its relfilenode changed. We can't necessarily do that here, because
1888 * we might be in a failed transaction. We assume it's okay to do it if
1889 * there are open references to the relcache entry (cf notes for
1890 * AtEOXact_RelationCache). Otherwise just mark the entry as possibly
1891 * invalid, and it'll be fixed when next opened.
1893 if (relation->rd_isnailed)
1895 RelationInitPhysicalAddr(relation);
1897 if (relation->rd_rel->relkind == RELKIND_INDEX)
1899 relation->rd_isvalid = false; /* needs to be revalidated */
1900 if (relation->rd_refcnt > 1)
1901 RelationReloadIndexInfo(relation);
1907 * Even non-system indexes should not be blown away if they are open and
1908 * have valid index support information. This avoids problems with active
1909 * use of the index support information. As with nailed indexes, we
1910 * re-read the pg_class row to handle possible physical relocation of the
1911 * index, and we check for pg_index updates too.
1913 if (relation->rd_rel->relkind == RELKIND_INDEX &&
1914 relation->rd_refcnt > 0 &&
1915 relation->rd_indexcxt != NULL)
1917 relation->rd_isvalid = false; /* needs to be revalidated */
1918 RelationReloadIndexInfo(relation);
1922 /* Mark it invalid until we've finished rebuild */
1923 relation->rd_isvalid = false;
1926 * If we're really done with the relcache entry, blow it away. But if
1927 * someone is still using it, reconstruct the whole deal without moving
1928 * the physical RelationData record (so that the someone's pointer is
1933 /* Remove it from the hash table */
1934 RelationCacheDelete(relation);
1936 /* And release storage */
1937 RelationDestroyRelation(relation);
1942 * Our strategy for rebuilding an open relcache entry is to build a
1943 * new entry from scratch, swap its contents with the old entry, and
1944 * finally delete the new entry (along with any infrastructure swapped
1945 * over from the old entry). This is to avoid trouble in case an
1946 * error causes us to lose control partway through. The old entry
1947 * will still be marked !rd_isvalid, so we'll try to rebuild it again
1948 * on next access. Meanwhile it's not any less valid than it was
1949 * before, so any code that might expect to continue accessing it
1950 * isn't hurt by the rebuild failure. (Consider for example a
1951 * subtransaction that ALTERs a table and then gets canceled partway
1952 * through the cache entry rebuild. The outer transaction should
1953 * still see the not-modified cache entry as valid.) The worst
1954 * consequence of an error is leaking the necessarily-unreferenced new
1955 * entry, and this shouldn't happen often enough for that to be a big
1958 * When rebuilding an open relcache entry, we must preserve ref count,
1959 * rd_createSubid/rd_newRelfilenodeSubid, and rd_toastoid state. Also
1960 * attempt to preserve the pg_class entry (rd_rel), tupledesc, and
1961 * rewrite-rule substructures in place, because various places assume
1962 * that these structures won't move while they are working with an
1963 * open relcache entry. (Note: the refcount mechanism for tupledescs
1964 * might someday allow us to remove this hack for the tupledesc.)
1966 * Note that this process does not touch CurrentResourceOwner; which
1967 * is good because whatever ref counts the entry may have do not
1968 * necessarily belong to that resource owner.
1971 Oid save_relid = RelationGetRelid(relation);
1975 /* Build temporary entry, but don't link it into hashtable */
1976 newrel = RelationBuildDesc(save_relid, false);
1979 /* Should only get here if relation was deleted */
1980 RelationCacheDelete(relation);
1981 RelationDestroyRelation(relation);
1982 elog(ERROR, "relation %u deleted while still in use", save_relid);
1985 keep_tupdesc = equalTupleDescs(relation->rd_att, newrel->rd_att);
1986 keep_rules = equalRuleLocks(relation->rd_rules, newrel->rd_rules);
1989 * Perform swapping of the relcache entry contents. Within this
1990 * process the old entry is momentarily invalid, so there *must* be no
1991 * possibility of CHECK_FOR_INTERRUPTS within this sequence. Do it in
1992 * all-in-line code for safety.
1994 * Since the vast majority of fields should be swapped, our method is
1995 * to swap the whole structures and then re-swap those few fields we
1996 * didn't want swapped.
1998 #define SWAPFIELD(fldtype, fldname) \
2000 fldtype _tmp = newrel->fldname; \
2001 newrel->fldname = relation->fldname; \
2002 relation->fldname = _tmp; \
2005 /* swap all Relation struct fields */
2007 RelationData tmpstruct;
2009 memcpy(&tmpstruct, newrel, sizeof(RelationData));
2010 memcpy(newrel, relation, sizeof(RelationData));
2011 memcpy(relation, &tmpstruct, sizeof(RelationData));
2014 /* rd_smgr must not be swapped, due to back-links from smgr level */
2015 SWAPFIELD(SMgrRelation, rd_smgr);
2016 /* rd_refcnt must be preserved */
2017 SWAPFIELD(int, rd_refcnt);
2018 /* isnailed shouldn't change */
2019 Assert(newrel->rd_isnailed == relation->rd_isnailed);
2020 /* creation sub-XIDs must be preserved */
2021 SWAPFIELD(SubTransactionId, rd_createSubid);
2022 SWAPFIELD(SubTransactionId, rd_newRelfilenodeSubid);
2023 /* un-swap rd_rel pointers, swap contents instead */
2024 SWAPFIELD(Form_pg_class, rd_rel);
2025 /* ... but actually, we don't have to update newrel->rd_rel */
2026 memcpy(relation->rd_rel, newrel->rd_rel, CLASS_TUPLE_SIZE);
2027 /* preserve old tupledesc and rules if no logical change */
2029 SWAPFIELD(TupleDesc, rd_att);
2032 SWAPFIELD(RuleLock *, rd_rules);
2033 SWAPFIELD(MemoryContext, rd_rulescxt);
2035 /* toast OID override must be preserved */
2036 SWAPFIELD(Oid, rd_toastoid);
2037 /* pgstat_info must be preserved */
2038 SWAPFIELD(struct PgStat_TableStatus *, pgstat_info);
2042 /* And now we can throw away the temporary entry */
2043 RelationDestroyRelation(newrel);
2048 * RelationFlushRelation
2050 * Rebuild the relation if it is open (refcount > 0), else blow it away.
2053 RelationFlushRelation(Relation relation)
2055 if (relation->rd_createSubid != InvalidSubTransactionId ||
2056 relation->rd_newRelfilenodeSubid != InvalidSubTransactionId)
2059 * New relcache entries are always rebuilt, not flushed; else we'd
2060 * forget the "new" status of the relation, which is a useful
2061 * optimization to have. Ditto for the new-relfilenode status.
2063 * The rel could have zero refcnt here, so temporarily increment the
2064 * refcnt to ensure it's safe to rebuild it. We can assume that the
2065 * current transaction has some lock on the rel already.
2067 RelationIncrementReferenceCount(relation);
2068 RelationClearRelation(relation, true);
2069 RelationDecrementReferenceCount(relation);
2074 * Pre-existing rels can be dropped from the relcache if not open.
2076 bool rebuild = !RelationHasReferenceCountZero(relation);
2078 RelationClearRelation(relation, rebuild);
2083 * RelationForgetRelation - unconditionally remove a relcache entry
2085 * External interface for destroying a relcache entry when we
2086 * drop the relation.
2089 RelationForgetRelation(Oid rid)
2093 RelationIdCacheLookup(rid, relation);
2095 if (!PointerIsValid(relation))
2096 return; /* not in cache, nothing to do */
2098 if (!RelationHasReferenceCountZero(relation))
2099 elog(ERROR, "relation %u is still open", rid);
2101 /* Unconditionally destroy the relcache entry */
2102 RelationClearRelation(relation, false);
2106 * RelationCacheInvalidateEntry
2108 * This routine is invoked for SI cache flush messages.
2110 * Any relcache entry matching the relid must be flushed. (Note: caller has
2111 * already determined that the relid belongs to our database or is a shared
2114 * We used to skip local relations, on the grounds that they could
2115 * not be targets of cross-backend SI update messages; but it seems
2116 * safer to process them, so that our *own* SI update messages will
2117 * have the same effects during CommandCounterIncrement for both
2118 * local and nonlocal relations.
2121 RelationCacheInvalidateEntry(Oid relationId)
2125 RelationIdCacheLookup(relationId, relation);
2127 if (PointerIsValid(relation))
2129 relcacheInvalsReceived++;
2130 RelationFlushRelation(relation);
2135 * RelationCacheInvalidate
2136 * Blow away cached relation descriptors that have zero reference counts,
2137 * and rebuild those with positive reference counts. Also reset the smgr
2138 * relation cache and re-read relation mapping data.
2140 * This is currently used only to recover from SI message buffer overflow,
2141 * so we do not touch new-in-transaction relations; they cannot be targets
2142 * of cross-backend SI updates (and our own updates now go through a
2143 * separate linked list that isn't limited by the SI message buffer size).
2144 * Likewise, we need not discard new-relfilenode-in-transaction hints,
2145 * since any invalidation of those would be a local event.
2147 * We do this in two phases: the first pass deletes deletable items, and
2148 * the second one rebuilds the rebuildable items. This is essential for
2149 * safety, because hash_seq_search only copes with concurrent deletion of
2150 * the element it is currently visiting. If a second SI overflow were to
2151 * occur while we are walking the table, resulting in recursive entry to
2152 * this routine, we could crash because the inner invocation blows away
2153 * the entry next to be visited by the outer scan. But this way is OK,
2154 * because (a) during the first pass we won't process any more SI messages,
2155 * so hash_seq_search will complete safely; (b) during the second pass we
2156 * only hold onto pointers to nondeletable entries.
2158 * The two-phase approach also makes it easy to update relfilenodes for
2159 * mapped relations before we do anything else, and to ensure that the
2160 * second pass processes nailed-in-cache items before other nondeletable
2161 * items. This should ensure that system catalogs are up to date before
2162 * we attempt to use them to reload information about other open relations.
2165 RelationCacheInvalidate(void)
2167 HASH_SEQ_STATUS status;
2168 RelIdCacheEnt *idhentry;
2170 List *rebuildFirstList = NIL;
2171 List *rebuildList = NIL;
2175 * Reload relation mapping data before starting to reconstruct cache.
2177 RelationMapInvalidateAll();
2180 hash_seq_init(&status, RelationIdCache);
2182 while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
2184 relation = idhentry->reldesc;
2186 /* Must close all smgr references to avoid leaving dangling ptrs */
2187 RelationCloseSmgr(relation);
2190 * Ignore new relations; no other backend will manipulate them before
2191 * we commit. Likewise, before replacing a relation's relfilenode, we
2192 * shall have acquired AccessExclusiveLock and drained any applicable
2193 * pending invalidations.
2195 if (relation->rd_createSubid != InvalidSubTransactionId ||
2196 relation->rd_newRelfilenodeSubid != InvalidSubTransactionId)
2199 relcacheInvalsReceived++;
2201 if (RelationHasReferenceCountZero(relation))
2203 /* Delete this entry immediately */
2204 Assert(!relation->rd_isnailed);
2205 RelationClearRelation(relation, false);
2210 * If it's a mapped relation, immediately update its rd_node in
2211 * case its relfilenode changed. We must do this during phase 1
2212 * in case the relation is consulted during rebuild of other
2213 * relcache entries in phase 2. It's safe since consulting the
2214 * map doesn't involve any access to relcache entries.
2216 if (RelationIsMapped(relation))
2217 RelationInitPhysicalAddr(relation);
2220 * Add this entry to list of stuff to rebuild in second pass.
2221 * pg_class goes to the front of rebuildFirstList while
2222 * pg_class_oid_index goes to the back of rebuildFirstList, so
2223 * they are done first and second respectively. Other nailed
2224 * relations go to the front of rebuildList, so they'll be done
2225 * next in no particular order; and everything else goes to the
2226 * back of rebuildList.
2228 if (RelationGetRelid(relation) == RelationRelationId)
2229 rebuildFirstList = lcons(relation, rebuildFirstList);
2230 else if (RelationGetRelid(relation) == ClassOidIndexId)
2231 rebuildFirstList = lappend(rebuildFirstList, relation);
2232 else if (relation->rd_isnailed)
2233 rebuildList = lcons(relation, rebuildList);
2235 rebuildList = lappend(rebuildList, relation);
2240 * Now zap any remaining smgr cache entries. This must happen before we
2241 * start to rebuild entries, since that may involve catalog fetches which
2242 * will re-open catalog files.
2246 /* Phase 2: rebuild the items found to need rebuild in phase 1 */
2247 foreach(l, rebuildFirstList)
2249 relation = (Relation) lfirst(l);
2250 RelationClearRelation(relation, true);
2252 list_free(rebuildFirstList);
2253 foreach(l, rebuildList)
2255 relation = (Relation) lfirst(l);
2256 RelationClearRelation(relation, true);
2258 list_free(rebuildList);
2262 * RelationCloseSmgrByOid - close a relcache entry's smgr link
2264 * Needed in some cases where we are changing a relation's physical mapping.
2265 * The link will be automatically reopened on next use.
2268 RelationCloseSmgrByOid(Oid relationId)
2272 RelationIdCacheLookup(relationId, relation);
2274 if (!PointerIsValid(relation))
2275 return; /* not in cache, nothing to do */
2277 RelationCloseSmgr(relation);
2281 * AtEOXact_RelationCache
2283 * Clean up the relcache at main-transaction commit or abort.
2285 * Note: this must be called *before* processing invalidation messages.
2286 * In the case of abort, we don't want to try to rebuild any invalidated
2287 * cache entries (since we can't safely do database accesses). Therefore
2288 * we must reset refcnts before handling pending invalidations.
2290 * As of PostgreSQL 8.1, relcache refcnts should get released by the
2291 * ResourceOwner mechanism. This routine just does a debugging
2292 * cross-check that no pins remain. However, we also need to do special
2293 * cleanup when the current transaction created any relations or made use
2294 * of forced index lists.
2297 AtEOXact_RelationCache(bool isCommit)
2299 HASH_SEQ_STATUS status;
2300 RelIdCacheEnt *idhentry;
2304 * Unless the eoxact_list[] overflowed, we only need to examine the rels
2305 * listed in it. Otherwise fall back on a hash_seq_search scan.
2307 * For simplicity, eoxact_list[] entries are not deleted till end of
2308 * top-level transaction, even though we could remove them at
2309 * subtransaction end in some cases, or remove relations from the list if
2310 * they are cleared for other reasons. Therefore we should expect the
2311 * case that list entries are not found in the hashtable; if not, there's
2312 * nothing to do for them.
2314 if (eoxact_list_overflowed)
2316 hash_seq_init(&status, RelationIdCache);
2317 while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
2319 AtEOXact_cleanup(idhentry->reldesc, isCommit);
2324 for (i = 0; i < eoxact_list_len; i++)
2326 idhentry = (RelIdCacheEnt *) hash_search(RelationIdCache,
2327 (void *) &eoxact_list[i],
2330 if (idhentry != NULL)
2331 AtEOXact_cleanup(idhentry->reldesc, isCommit);
2335 /* Now we're out of the transaction and can clear the list */
2336 eoxact_list_len = 0;
2337 eoxact_list_overflowed = false;
2343 * Clean up a single rel at main-transaction commit or abort
2345 * NB: this processing must be idempotent, because EOXactListAdd() doesn't
2346 * bother to prevent duplicate entries in eoxact_list[].
2349 AtEOXact_cleanup(Relation relation, bool isCommit)
2352 * The relcache entry's ref count should be back to its normal
2353 * not-in-a-transaction state: 0 unless it's nailed in cache.
2355 * In bootstrap mode, this is NOT true, so don't check it --- the
2356 * bootstrap code expects relations to stay open across start/commit
2357 * transaction calls. (That seems bogus, but it's not worth fixing.)
2359 * Note: ideally this check would be applied to every relcache entry,
2360 * not just those that have eoxact work to do. But it's not worth
2361 * forcing a scan of the whole relcache just for this. (Moreover,
2362 * doing so would mean that assert-enabled testing never tests the
2363 * hash_search code path above, which seems a bad idea.)
2365 #ifdef USE_ASSERT_CHECKING
2366 if (!IsBootstrapProcessingMode())
2368 int expected_refcnt;
2370 expected_refcnt = relation->rd_isnailed ? 1 : 0;
2371 Assert(relation->rd_refcnt == expected_refcnt);
2376 * Is it a relation created in the current transaction?
2378 * During commit, reset the flag to zero, since we are now out of the
2379 * creating transaction. During abort, simply delete the relcache
2380 * entry --- it isn't interesting any longer. (NOTE: if we have
2381 * forgotten the new-ness of a new relation due to a forced cache
2382 * flush, the entry will get deleted anyway by shared-cache-inval
2383 * processing of the aborted pg_class insertion.)
2385 if (relation->rd_createSubid != InvalidSubTransactionId)
2388 relation->rd_createSubid = InvalidSubTransactionId;
2391 RelationClearRelation(relation, false);
2397 * Likewise, reset the hint about the relfilenode being new.
2399 relation->rd_newRelfilenodeSubid = InvalidSubTransactionId;
2402 * Flush any temporary index list.
2404 if (relation->rd_indexvalid == 2)
2406 list_free(relation->rd_indexlist);
2407 relation->rd_indexlist = NIL;
2408 relation->rd_oidindex = InvalidOid;
2409 relation->rd_indexvalid = 0;
2414 * AtEOSubXact_RelationCache
2416 * Clean up the relcache at sub-transaction commit or abort.
2418 * Note: this must be called *before* processing invalidation messages.
2421 AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid,
2422 SubTransactionId parentSubid)
2424 HASH_SEQ_STATUS status;
2425 RelIdCacheEnt *idhentry;
2429 * Unless the eoxact_list[] overflowed, we only need to examine the rels
2430 * listed in it. Otherwise fall back on a hash_seq_search scan. Same
2431 * logic as in AtEOXact_RelationCache.
2433 if (eoxact_list_overflowed)
2435 hash_seq_init(&status, RelationIdCache);
2436 while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
2438 AtEOSubXact_cleanup(idhentry->reldesc, isCommit,
2439 mySubid, parentSubid);
2444 for (i = 0; i < eoxact_list_len; i++)
2446 idhentry = (RelIdCacheEnt *) hash_search(RelationIdCache,
2447 (void *) &eoxact_list[i],
2450 if (idhentry != NULL)
2451 AtEOSubXact_cleanup(idhentry->reldesc, isCommit,
2452 mySubid, parentSubid);
2456 /* Don't reset the list; we still need more cleanup later */
2460 * AtEOSubXact_cleanup
2462 * Clean up a single rel at subtransaction commit or abort
2464 * NB: this processing must be idempotent, because EOXactListAdd() doesn't
2465 * bother to prevent duplicate entries in eoxact_list[].
2468 AtEOSubXact_cleanup(Relation relation, bool isCommit,
2469 SubTransactionId mySubid, SubTransactionId parentSubid)
2472 * Is it a relation created in the current subtransaction?
2474 * During subcommit, mark it as belonging to the parent, instead.
2475 * During subabort, simply delete the relcache entry.
2477 if (relation->rd_createSubid == mySubid)
2480 relation->rd_createSubid = parentSubid;
2483 RelationClearRelation(relation, false);
2489 * Likewise, update or drop any new-relfilenode-in-subtransaction
2492 if (relation->rd_newRelfilenodeSubid == mySubid)
2495 relation->rd_newRelfilenodeSubid = parentSubid;
2497 relation->rd_newRelfilenodeSubid = InvalidSubTransactionId;
2501 * Flush any temporary index list.
2503 if (relation->rd_indexvalid == 2)
2505 list_free(relation->rd_indexlist);
2506 relation->rd_indexlist = NIL;
2507 relation->rd_oidindex = InvalidOid;
2508 relation->rd_indexvalid = 0;
2514 * RelationBuildLocalRelation
2515 * Build a relcache entry for an about-to-be-created relation,
2516 * and enter it into the relcache.
2519 RelationBuildLocalRelation(const char *relname,
2525 bool shared_relation,
2526 bool mapped_relation,
2527 char relpersistence,
2531 MemoryContext oldcxt;
2532 int natts = tupDesc->natts;
2537 AssertArg(natts >= 0);
2540 * check for creation of a rel that must be nailed in cache.
2542 * XXX this list had better match the relations specially handled in
2543 * RelationCacheInitializePhase2/3.
2547 case DatabaseRelationId:
2548 case AuthIdRelationId:
2549 case AuthMemRelationId:
2550 case RelationRelationId:
2551 case AttributeRelationId:
2552 case ProcedureRelationId:
2553 case TypeRelationId:
2562 * check that hardwired list of shared rels matches what's in the
2563 * bootstrap .bki file. If you get a failure here during initdb, you
2564 * probably need to fix IsSharedRelation() to match whatever you've done
2565 * to the set of shared relations.
2567 if (shared_relation != IsSharedRelation(relid))
2568 elog(ERROR, "shared_relation flag for \"%s\" does not match IsSharedRelation(%u)",
2571 /* Shared relations had better be mapped, too */
2572 Assert(mapped_relation || !shared_relation);
2575 * switch to the cache context to create the relcache entry.
2577 if (!CacheMemoryContext)
2578 CreateCacheMemoryContext();
2580 oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
2583 * allocate a new relation descriptor and fill in basic state fields.
2585 rel = (Relation) palloc0(sizeof(RelationData));
2587 /* make sure relation is marked as having no open file yet */
2588 rel->rd_smgr = NULL;
2590 /* mark it nailed if appropriate */
2591 rel->rd_isnailed = nailit;
2593 rel->rd_refcnt = nailit ? 1 : 0;
2595 /* it's being created in this transaction */
2596 rel->rd_createSubid = GetCurrentSubTransactionId();
2597 rel->rd_newRelfilenodeSubid = InvalidSubTransactionId;
2600 * create a new tuple descriptor from the one passed in. We do this
2601 * partly to copy it into the cache context, and partly because the new
2602 * relation can't have any defaults or constraints yet; they have to be
2603 * added in later steps, because they require additions to multiple system
2604 * catalogs. We can copy attnotnull constraints here, however.
2606 rel->rd_att = CreateTupleDescCopy(tupDesc);
2607 rel->rd_att->tdrefcount = 1; /* mark as refcounted */
2608 has_not_null = false;
2609 for (i = 0; i < natts; i++)
2611 rel->rd_att->attrs[i]->attnotnull = tupDesc->attrs[i]->attnotnull;
2612 has_not_null |= tupDesc->attrs[i]->attnotnull;
2617 TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr));
2619 constr->has_not_null = true;
2620 rel->rd_att->constr = constr;
2624 * initialize relation tuple form (caller may add/override data later)
2626 rel->rd_rel = (Form_pg_class) palloc0(CLASS_TUPLE_SIZE);
2628 namestrcpy(&rel->rd_rel->relname, relname);
2629 rel->rd_rel->relnamespace = relnamespace;
2631 rel->rd_rel->relkind = relkind;
2632 rel->rd_rel->relhasoids = rel->rd_att->tdhasoid;
2633 rel->rd_rel->relnatts = natts;
2634 rel->rd_rel->reltype = InvalidOid;
2635 /* needed when bootstrapping: */
2636 rel->rd_rel->relowner = BOOTSTRAP_SUPERUSERID;
2638 /* set up persistence and relcache fields dependent on it */
2639 rel->rd_rel->relpersistence = relpersistence;
2640 switch (relpersistence)
2642 case RELPERSISTENCE_UNLOGGED:
2643 case RELPERSISTENCE_PERMANENT:
2644 rel->rd_backend = InvalidBackendId;
2645 rel->rd_islocaltemp = false;
2647 case RELPERSISTENCE_TEMP:
2648 Assert(isTempOrToastNamespace(relnamespace));
2649 rel->rd_backend = MyBackendId;
2650 rel->rd_islocaltemp = true;
2653 elog(ERROR, "invalid relpersistence: %c", relpersistence);
2658 * Insert relation physical and logical identifiers (OIDs) into the right
2659 * places. For a mapped relation, we set relfilenode to zero and rely on
2660 * RelationInitPhysicalAddr to consult the map.
2662 rel->rd_rel->relisshared = shared_relation;
2664 RelationGetRelid(rel) = relid;
2666 for (i = 0; i < natts; i++)
2667 rel->rd_att->attrs[i]->attrelid = relid;
2669 rel->rd_rel->reltablespace = reltablespace;
2671 if (mapped_relation)
2673 rel->rd_rel->relfilenode = InvalidOid;
2674 /* Add it to the active mapping information */
2675 RelationMapUpdateMap(relid, relfilenode, shared_relation, true);
2678 rel->rd_rel->relfilenode = relfilenode;
2680 RelationInitLockInfo(rel); /* see lmgr.c */
2682 RelationInitPhysicalAddr(rel);
2685 * Okay to insert into the relcache hash tables.
2687 RelationCacheInsert(rel);
2690 * Flag relation as needing eoxact cleanup (to clear rd_createSubid).
2691 * We can't do this before storing relid in it.
2696 * done building relcache entry.
2698 MemoryContextSwitchTo(oldcxt);
2700 /* It's fully valid */
2701 rel->rd_isvalid = true;
2704 * Caller expects us to pin the returned entry.
2706 RelationIncrementReferenceCount(rel);
2713 * RelationSetNewRelfilenode
2715 * Assign a new relfilenode (physical file name) to the relation.
2717 * This allows a full rewrite of the relation to be done with transactional
2718 * safety (since the filenode assignment can be rolled back). Note however
2719 * that there is no simple way to access the relation's old data for the
2720 * remainder of the current transaction. This limits the usefulness to cases
2721 * such as TRUNCATE or rebuilding an index from scratch.
2723 * Caller must already hold exclusive lock on the relation.
2725 * The relation is marked with relfrozenxid = freezeXid (InvalidTransactionId
2726 * must be passed for indexes and sequences). This should be a lower bound on
2727 * the XIDs that will be put into the new relation contents.
2730 RelationSetNewRelfilenode(Relation relation, TransactionId freezeXid,
2731 MultiXactId minmulti)
2734 RelFileNodeBackend newrnode;
2737 Form_pg_class classform;
2739 /* Indexes, sequences must have Invalid frozenxid; other rels must not */
2740 Assert((relation->rd_rel->relkind == RELKIND_INDEX ||
2741 relation->rd_rel->relkind == RELKIND_SEQUENCE) ?
2742 freezeXid == InvalidTransactionId :
2743 TransactionIdIsNormal(freezeXid));
2744 Assert(TransactionIdIsNormal(freezeXid) == MultiXactIdIsValid(minmulti));
2746 /* Allocate a new relfilenode */
2747 newrelfilenode = GetNewRelFileNode(relation->rd_rel->reltablespace, NULL,
2748 relation->rd_rel->relpersistence);
2751 * Get a writable copy of the pg_class tuple for the given relation.
2753 pg_class = heap_open(RelationRelationId, RowExclusiveLock);
2755 tuple = SearchSysCacheCopy1(RELOID,
2756 ObjectIdGetDatum(RelationGetRelid(relation)));
2757 if (!HeapTupleIsValid(tuple))
2758 elog(ERROR, "could not find tuple for relation %u",
2759 RelationGetRelid(relation));
2760 classform = (Form_pg_class) GETSTRUCT(tuple);
2763 * Create storage for the main fork of the new relfilenode.
2765 * NOTE: any conflict in relfilenode value will be caught here, if
2766 * GetNewRelFileNode messes up for any reason.
2768 newrnode.node = relation->rd_node;
2769 newrnode.node.relNode = newrelfilenode;
2770 newrnode.backend = relation->rd_backend;
2771 RelationCreateStorage(newrnode.node, relation->rd_rel->relpersistence);
2772 smgrclosenode(newrnode);
2775 * Schedule unlinking of the old storage at transaction commit.
2777 RelationDropStorage(relation);
2780 * Now update the pg_class row. However, if we're dealing with a mapped
2781 * index, pg_class.relfilenode doesn't change; instead we have to send the
2782 * update to the relation mapper.
2784 if (RelationIsMapped(relation))
2785 RelationMapUpdateMap(RelationGetRelid(relation),
2787 relation->rd_rel->relisshared,
2790 classform->relfilenode = newrelfilenode;
2792 /* These changes are safe even for a mapped relation */
2793 if (relation->rd_rel->relkind != RELKIND_SEQUENCE)
2795 classform->relpages = 0; /* it's empty until further notice */
2796 classform->reltuples = 0;
2797 classform->relallvisible = 0;
2799 classform->relfrozenxid = freezeXid;
2800 classform->relminmxid = minmulti;
2802 simple_heap_update(pg_class, &tuple->t_self, tuple);
2803 CatalogUpdateIndexes(pg_class, tuple);
2805 heap_freetuple(tuple);
2807 heap_close(pg_class, RowExclusiveLock);
2810 * Make the pg_class row change visible, as well as the relation map
2811 * change if any. This will cause the relcache entry to get updated, too.
2813 CommandCounterIncrement();
2816 * Mark the rel as having been given a new relfilenode in the current
2817 * (sub) transaction. This is a hint that can be used to optimize later
2818 * operations on the rel in the same transaction.
2820 relation->rd_newRelfilenodeSubid = GetCurrentSubTransactionId();
2822 /* Flag relation as needing eoxact cleanup (to remove the hint) */
2823 EOXactListAdd(relation);
2828 * RelationCacheInitialize
2830 * This initializes the relation descriptor cache. At the time
2831 * that this is invoked, we can't do database access yet (mainly
2832 * because the transaction subsystem is not up); all we are doing
2833 * is making an empty cache hashtable. This must be done before
2834 * starting the initialization transaction, because otherwise
2835 * AtEOXact_RelationCache would crash if that transaction aborts
2836 * before we can get the relcache set up.
2839 #define INITRELCACHESIZE 400
2842 RelationCacheInitialize(void)
2847 * make sure cache memory context exists
2849 if (!CacheMemoryContext)
2850 CreateCacheMemoryContext();
2853 * create hashtable that indexes the relcache
2855 MemSet(&ctl, 0, sizeof(ctl));
2856 ctl.keysize = sizeof(Oid);
2857 ctl.entrysize = sizeof(RelIdCacheEnt);
2858 ctl.hash = oid_hash;
2859 RelationIdCache = hash_create("Relcache by OID", INITRELCACHESIZE,
2860 &ctl, HASH_ELEM | HASH_FUNCTION);
2863 * relation mapper needs to be initialized too
2865 RelationMapInitialize();
2869 * RelationCacheInitializePhase2
2871 * This is called to prepare for access to shared catalogs during startup.
2872 * We must at least set up nailed reldescs for pg_database, pg_authid,
2873 * and pg_auth_members. Ideally we'd like to have reldescs for their
2874 * indexes, too. We attempt to load this information from the shared
2875 * relcache init file. If that's missing or broken, just make phony
2876 * entries for the catalogs themselves. RelationCacheInitializePhase3
2877 * will clean up as needed.
2880 RelationCacheInitializePhase2(void)
2882 MemoryContext oldcxt;
2885 * relation mapper needs initialized too
2887 RelationMapInitializePhase2();
2890 * In bootstrap mode, the shared catalogs aren't there yet anyway, so do
2893 if (IsBootstrapProcessingMode())
2897 * switch to cache memory context
2899 oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
2902 * Try to load the shared relcache cache file. If unsuccessful, bootstrap
2903 * the cache with pre-made descriptors for the critical shared catalogs.
2905 if (!load_relcache_init_file(true))
2907 formrdesc("pg_database", DatabaseRelation_Rowtype_Id, true,
2908 true, Natts_pg_database, Desc_pg_database);
2909 formrdesc("pg_authid", AuthIdRelation_Rowtype_Id, true,
2910 true, Natts_pg_authid, Desc_pg_authid);
2911 formrdesc("pg_auth_members", AuthMemRelation_Rowtype_Id, true,
2912 false, Natts_pg_auth_members, Desc_pg_auth_members);
2914 #define NUM_CRITICAL_SHARED_RELS 3 /* fix if you change list above */
2917 MemoryContextSwitchTo(oldcxt);
2921 * RelationCacheInitializePhase3
2923 * This is called as soon as the catcache and transaction system
2924 * are functional and we have determined MyDatabaseId. At this point
2925 * we can actually read data from the database's system catalogs.
2926 * We first try to read pre-computed relcache entries from the local
2927 * relcache init file. If that's missing or broken, make phony entries
2928 * for the minimum set of nailed-in-cache relations. Then (unless
2929 * bootstrapping) make sure we have entries for the critical system
2930 * indexes. Once we've done all this, we have enough infrastructure to
2931 * open any system catalog or use any catcache. The last step is to
2932 * rewrite the cache files if needed.
2935 RelationCacheInitializePhase3(void)
2937 HASH_SEQ_STATUS status;
2938 RelIdCacheEnt *idhentry;
2939 MemoryContext oldcxt;
2940 bool needNewCacheFile = !criticalSharedRelcachesBuilt;
2943 * relation mapper needs initialized too
2945 RelationMapInitializePhase3();
2948 * switch to cache memory context
2950 oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
2953 * Try to load the local relcache cache file. If unsuccessful, bootstrap
2954 * the cache with pre-made descriptors for the critical "nailed-in" system
2957 if (IsBootstrapProcessingMode() ||
2958 !load_relcache_init_file(false))
2960 needNewCacheFile = true;
2962 formrdesc("pg_class", RelationRelation_Rowtype_Id, false,
2963 true, Natts_pg_class, Desc_pg_class);
2964 formrdesc("pg_attribute", AttributeRelation_Rowtype_Id, false,
2965 false, Natts_pg_attribute, Desc_pg_attribute);
2966 formrdesc("pg_proc", ProcedureRelation_Rowtype_Id, false,
2967 true, Natts_pg_proc, Desc_pg_proc);
2968 formrdesc("pg_type", TypeRelation_Rowtype_Id, false,
2969 true, Natts_pg_type, Desc_pg_type);
2971 #define NUM_CRITICAL_LOCAL_RELS 4 /* fix if you change list above */
2974 MemoryContextSwitchTo(oldcxt);
2976 /* In bootstrap mode, the faked-up formrdesc info is all we'll have */
2977 if (IsBootstrapProcessingMode())
2981 * If we didn't get the critical system indexes loaded into relcache, do
2982 * so now. These are critical because the catcache and/or opclass cache
2983 * depend on them for fetches done during relcache load. Thus, we have an
2984 * infinite-recursion problem. We can break the recursion by doing
2985 * heapscans instead of indexscans at certain key spots. To avoid hobbling
2986 * performance, we only want to do that until we have the critical indexes
2987 * loaded into relcache. Thus, the flag criticalRelcachesBuilt is used to
2988 * decide whether to do heapscan or indexscan at the key spots, and we set
2989 * it true after we've loaded the critical indexes.
2991 * The critical indexes are marked as "nailed in cache", partly to make it
2992 * easy for load_relcache_init_file to count them, but mainly because we
2993 * cannot flush and rebuild them once we've set criticalRelcachesBuilt to
2994 * true. (NOTE: perhaps it would be possible to reload them by
2995 * temporarily setting criticalRelcachesBuilt to false again. For now,
2996 * though, we just nail 'em in.)
2998 * RewriteRelRulenameIndexId and TriggerRelidNameIndexId are not critical
2999 * in the same way as the others, because the critical catalogs don't
3000 * (currently) have any rules or triggers, and so these indexes can be
3001 * rebuilt without inducing recursion. However they are used during
3002 * relcache load when a rel does have rules or triggers, so we choose to
3003 * nail them for performance reasons.
3005 if (!criticalRelcachesBuilt)
3007 load_critical_index(ClassOidIndexId,
3008 RelationRelationId);
3009 load_critical_index(AttributeRelidNumIndexId,
3010 AttributeRelationId);
3011 load_critical_index(IndexRelidIndexId,
3013 load_critical_index(OpclassOidIndexId,
3014 OperatorClassRelationId);
3015 load_critical_index(AccessMethodProcedureIndexId,
3016 AccessMethodProcedureRelationId);
3017 load_critical_index(RewriteRelRulenameIndexId,
3019 load_critical_index(TriggerRelidNameIndexId,
3022 #define NUM_CRITICAL_LOCAL_INDEXES 7 /* fix if you change list above */
3024 criticalRelcachesBuilt = true;
3028 * Process critical shared indexes too.
3030 * DatabaseNameIndexId isn't critical for relcache loading, but rather for
3031 * initial lookup of MyDatabaseId, without which we'll never find any
3032 * non-shared catalogs at all. Autovacuum calls InitPostgres with a
3033 * database OID, so it instead depends on DatabaseOidIndexId. We also
3034 * need to nail up some indexes on pg_authid and pg_auth_members for use
3035 * during client authentication.
3037 if (!criticalSharedRelcachesBuilt)
3039 load_critical_index(DatabaseNameIndexId,
3040 DatabaseRelationId);
3041 load_critical_index(DatabaseOidIndexId,
3042 DatabaseRelationId);
3043 load_critical_index(AuthIdRolnameIndexId,
3045 load_critical_index(AuthIdOidIndexId,
3047 load_critical_index(AuthMemMemRoleIndexId,
3050 #define NUM_CRITICAL_SHARED_INDEXES 5 /* fix if you change list above */
3052 criticalSharedRelcachesBuilt = true;
3056 * Now, scan all the relcache entries and update anything that might be
3057 * wrong in the results from formrdesc or the relcache cache file. If we
3058 * faked up relcache entries using formrdesc, then read the real pg_class
3059 * rows and replace the fake entries with them. Also, if any of the
3060 * relcache entries have rules or triggers, load that info the hard way
3061 * since it isn't recorded in the cache file.
3063 * Whenever we access the catalogs to read data, there is a possibility of
3064 * a shared-inval cache flush causing relcache entries to be removed.
3065 * Since hash_seq_search only guarantees to still work after the *current*
3066 * entry is removed, it's unsafe to continue the hashtable scan afterward.
3067 * We handle this by restarting the scan from scratch after each access.
3068 * This is theoretically O(N^2), but the number of entries that actually
3069 * need to be fixed is small enough that it doesn't matter.
3071 hash_seq_init(&status, RelationIdCache);
3073 while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
3075 Relation relation = idhentry->reldesc;
3076 bool restart = false;
3079 * Make sure *this* entry doesn't get flushed while we work with it.
3081 RelationIncrementReferenceCount(relation);
3084 * If it's a faked-up entry, read the real pg_class tuple.
3086 if (relation->rd_rel->relowner == InvalidOid)
3091 htup = SearchSysCache1(RELOID,
3092 ObjectIdGetDatum(RelationGetRelid(relation)));
3093 if (!HeapTupleIsValid(htup))
3094 elog(FATAL, "cache lookup failed for relation %u",
3095 RelationGetRelid(relation));
3096 relp = (Form_pg_class) GETSTRUCT(htup);
3099 * Copy tuple to relation->rd_rel. (See notes in
3100 * AllocateRelationDesc())
3102 memcpy((char *) relation->rd_rel, (char *) relp, CLASS_TUPLE_SIZE);
3104 /* Update rd_options while we have the tuple */
3105 if (relation->rd_options)
3106 pfree(relation->rd_options);
3107 RelationParseRelOptions(relation, htup);
3110 * Check the values in rd_att were set up correctly. (We cannot
3111 * just copy them over now: formrdesc must have set up the rd_att
3112 * data correctly to start with, because it may already have been
3113 * copied into one or more catcache entries.)
3115 Assert(relation->rd_att->tdtypeid == relp->reltype);
3116 Assert(relation->rd_att->tdtypmod == -1);
3117 Assert(relation->rd_att->tdhasoid == relp->relhasoids);
3119 ReleaseSysCache(htup);
3121 /* relowner had better be OK now, else we'll loop forever */
3122 if (relation->rd_rel->relowner == InvalidOid)
3123 elog(ERROR, "invalid relowner in pg_class entry for \"%s\"",
3124 RelationGetRelationName(relation));
3130 * Fix data that isn't saved in relcache cache file.
3132 * relhasrules or relhastriggers could possibly be wrong or out of
3133 * date. If we don't actually find any rules or triggers, clear the
3134 * local copy of the flag so that we don't get into an infinite loop
3135 * here. We don't make any attempt to fix the pg_class entry, though.
3137 if (relation->rd_rel->relhasrules && relation->rd_rules == NULL)
3139 RelationBuildRuleLock(relation);
3140 if (relation->rd_rules == NULL)
3141 relation->rd_rel->relhasrules = false;
3144 if (relation->rd_rel->relhastriggers && relation->trigdesc == NULL)
3146 RelationBuildTriggers(relation);
3147 if (relation->trigdesc == NULL)
3148 relation->rd_rel->relhastriggers = false;
3152 /* Release hold on the relation */
3153 RelationDecrementReferenceCount(relation);
3155 /* Now, restart the hashtable scan if needed */
3158 hash_seq_term(&status);
3159 hash_seq_init(&status, RelationIdCache);
3164 * Lastly, write out new relcache cache files if needed. We don't bother
3165 * to distinguish cases where only one of the two needs an update.
3167 if (needNewCacheFile)
3170 * Force all the catcaches to finish initializing and thereby open the
3171 * catalogs and indexes they use. This will preload the relcache with
3172 * entries for all the most important system catalogs and indexes, so
3173 * that the init files will be most useful for future backends.
3175 InitCatalogCachePhase2();
3177 /* reset initFileRelationIds list; we'll fill it during write */
3178 initFileRelationIds = NIL;
3180 /* now write the files */
3181 write_relcache_init_file(true);
3182 write_relcache_init_file(false);
3187 * Load one critical system index into the relcache
3189 * indexoid is the OID of the target index, heapoid is the OID of the catalog
3193 load_critical_index(Oid indexoid, Oid heapoid)
3198 * We must lock the underlying catalog before locking the index to avoid
3199 * deadlock, since RelationBuildDesc might well need to read the catalog,
3200 * and if anyone else is exclusive-locking this catalog and index they'll
3201 * be doing it in that order.
3203 LockRelationOid(heapoid, AccessShareLock);
3204 LockRelationOid(indexoid, AccessShareLock);
3205 ird = RelationBuildDesc(indexoid, true);
3207 elog(PANIC, "could not open critical system index %u", indexoid);
3208 ird->rd_isnailed = true;
3210 UnlockRelationOid(indexoid, AccessShareLock);
3211 UnlockRelationOid(heapoid, AccessShareLock);
3215 * GetPgClassDescriptor -- get a predefined tuple descriptor for pg_class
3216 * GetPgIndexDescriptor -- get a predefined tuple descriptor for pg_index
3218 * We need this kluge because we have to be able to access non-fixed-width
3219 * fields of pg_class and pg_index before we have the standard catalog caches
3220 * available. We use predefined data that's set up in just the same way as
3221 * the bootstrapped reldescs used by formrdesc(). The resulting tupdesc is
3222 * not 100% kosher: it does not have the correct rowtype OID in tdtypeid, nor
3223 * does it have a TupleConstr field. But it's good enough for the purpose of
3224 * extracting fields.
3227 BuildHardcodedDescriptor(int natts, const FormData_pg_attribute *attrs,
3231 MemoryContext oldcxt;
3234 oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
3236 result = CreateTemplateTupleDesc(natts, hasoids);
3237 result->tdtypeid = RECORDOID; /* not right, but we don't care */
3238 result->tdtypmod = -1;
3240 for (i = 0; i < natts; i++)
3242 memcpy(result->attrs[i], &attrs[i], ATTRIBUTE_FIXED_PART_SIZE);
3243 /* make sure attcacheoff is valid */
3244 result->attrs[i]->attcacheoff = -1;
3247 /* initialize first attribute's attcacheoff, cf RelationBuildTupleDesc */
3248 result->attrs[0]->attcacheoff = 0;
3250 /* Note: we don't bother to set up a TupleConstr entry */
3252 MemoryContextSwitchTo(oldcxt);
3258 GetPgClassDescriptor(void)
3260 static TupleDesc pgclassdesc = NULL;
3263 if (pgclassdesc == NULL)
3264 pgclassdesc = BuildHardcodedDescriptor(Natts_pg_class,
3272 GetPgIndexDescriptor(void)
3274 static TupleDesc pgindexdesc = NULL;
3277 if (pgindexdesc == NULL)
3278 pgindexdesc = BuildHardcodedDescriptor(Natts_pg_index,
3286 * Load any default attribute value definitions for the relation.
3289 AttrDefaultFetch(Relation relation)
3291 AttrDefault *attrdef = relation->rd_att->constr->defval;
3292 int ndef = relation->rd_att->constr->num_defval;
3303 Anum_pg_attrdef_adrelid,
3304 BTEqualStrategyNumber, F_OIDEQ,
3305 ObjectIdGetDatum(RelationGetRelid(relation)));
3307 adrel = heap_open(AttrDefaultRelationId, AccessShareLock);
3308 adscan = systable_beginscan(adrel, AttrDefaultIndexId, true,
3309 SnapshotNow, 1, &skey);
3312 while (HeapTupleIsValid(htup = systable_getnext(adscan)))
3314 Form_pg_attrdef adform = (Form_pg_attrdef) GETSTRUCT(htup);
3316 for (i = 0; i < ndef; i++)
3318 if (adform->adnum != attrdef[i].adnum)
3320 if (attrdef[i].adbin != NULL)
3321 elog(WARNING, "multiple attrdef records found for attr %s of rel %s",
3322 NameStr(relation->rd_att->attrs[adform->adnum - 1]->attname),
3323 RelationGetRelationName(relation));
3327 val = fastgetattr(htup,
3328 Anum_pg_attrdef_adbin,
3329 adrel->rd_att, &isnull);
3331 elog(WARNING, "null adbin for attr %s of rel %s",
3332 NameStr(relation->rd_att->attrs[adform->adnum - 1]->attname),
3333 RelationGetRelationName(relation));
3335 attrdef[i].adbin = MemoryContextStrdup(CacheMemoryContext,
3336 TextDatumGetCString(val));
3341 elog(WARNING, "unexpected attrdef record found for attr %d of rel %s",
3342 adform->adnum, RelationGetRelationName(relation));
3345 systable_endscan(adscan);
3346 heap_close(adrel, AccessShareLock);
3349 elog(WARNING, "%d attrdef record(s) missing for rel %s",
3350 ndef - found, RelationGetRelationName(relation));
3354 * Load any check constraints for the relation.
3357 CheckConstraintFetch(Relation relation)
3359 ConstrCheck *check = relation->rd_att->constr->check;
3360 int ncheck = relation->rd_att->constr->num_check;
3362 SysScanDesc conscan;
3363 ScanKeyData skey[1];
3369 ScanKeyInit(&skey[0],
3370 Anum_pg_constraint_conrelid,
3371 BTEqualStrategyNumber, F_OIDEQ,
3372 ObjectIdGetDatum(RelationGetRelid(relation)));
3374 conrel = heap_open(ConstraintRelationId, AccessShareLock);
3375 conscan = systable_beginscan(conrel, ConstraintRelidIndexId, true,
3376 SnapshotNow, 1, skey);
3378 while (HeapTupleIsValid(htup = systable_getnext(conscan)))
3380 Form_pg_constraint conform = (Form_pg_constraint) GETSTRUCT(htup);
3382 /* We want check constraints only */
3383 if (conform->contype != CONSTRAINT_CHECK)
3386 if (found >= ncheck)
3387 elog(ERROR, "unexpected constraint record found for rel %s",
3388 RelationGetRelationName(relation));
3390 check[found].ccvalid = conform->convalidated;
3391 check[found].ccnoinherit = conform->connoinherit;
3392 check[found].ccname = MemoryContextStrdup(CacheMemoryContext,
3393 NameStr(conform->conname));
3395 /* Grab and test conbin is actually set */
3396 val = fastgetattr(htup,
3397 Anum_pg_constraint_conbin,
3398 conrel->rd_att, &isnull);
3400 elog(ERROR, "null conbin for rel %s",
3401 RelationGetRelationName(relation));
3403 check[found].ccbin = MemoryContextStrdup(CacheMemoryContext,
3404 TextDatumGetCString(val));
3408 systable_endscan(conscan);
3409 heap_close(conrel, AccessShareLock);
3411 if (found != ncheck)
3412 elog(ERROR, "%d constraint record(s) missing for rel %s",
3413 ncheck - found, RelationGetRelationName(relation));
3417 * RelationGetIndexList -- get a list of OIDs of indexes on this relation
3419 * The index list is created only if someone requests it. We scan pg_index
3420 * to find relevant indexes, and add the list to the relcache entry so that
3421 * we won't have to compute it again. Note that shared cache inval of a
3422 * relcache entry will delete the old list and set rd_indexvalid to 0,
3423 * so that we must recompute the index list on next request. This handles
3424 * creation or deletion of an index.
3426 * Indexes that are marked not IndexIsLive are omitted from the returned list.
3427 * Such indexes are expected to be dropped momentarily, and should not be
3428 * touched at all by any caller of this function.
3430 * The returned list is guaranteed to be sorted in order by OID. This is
3431 * needed by the executor, since for index types that we obtain exclusive
3432 * locks on when updating the index, all backends must lock the indexes in
3433 * the same order or we will get deadlocks (see ExecOpenIndices()). Any
3434 * consistent ordering would do, but ordering by OID is easy.
3436 * Since shared cache inval causes the relcache's copy of the list to go away,
3437 * we return a copy of the list palloc'd in the caller's context. The caller
3438 * may list_free() the returned list after scanning it. This is necessary
3439 * since the caller will typically be doing syscache lookups on the relevant
3440 * indexes, and syscache lookup could cause SI messages to be processed!
3442 * We also update rd_oidindex, which this module treats as effectively part
3443 * of the index list. rd_oidindex is valid when rd_indexvalid isn't zero;
3444 * it is the pg_class OID of a unique index on OID when the relation has one,
3445 * and InvalidOid if there is no such index.
3448 RelationGetIndexList(Relation relation)
3451 SysScanDesc indscan;
3456 MemoryContext oldcxt;
3458 /* Quick exit if we already computed the list. */
3459 if (relation->rd_indexvalid != 0)
3460 return list_copy(relation->rd_indexlist);
3463 * We build the list we intend to return (in the caller's context) while
3464 * doing the scan. After successfully completing the scan, we copy that
3465 * list into the relcache entry. This avoids cache-context memory leakage
3466 * if we get some sort of error partway through.
3469 oidIndex = InvalidOid;
3471 /* Prepare to scan pg_index for entries having indrelid = this rel. */
3473 Anum_pg_index_indrelid,
3474 BTEqualStrategyNumber, F_OIDEQ,
3475 ObjectIdGetDatum(RelationGetRelid(relation)));
3477 indrel = heap_open(IndexRelationId, AccessShareLock);
3478 indscan = systable_beginscan(indrel, IndexIndrelidIndexId, true,
3479 SnapshotNow, 1, &skey);
3481 while (HeapTupleIsValid(htup = systable_getnext(indscan)))
3483 Form_pg_index index = (Form_pg_index) GETSTRUCT(htup);
3484 Datum indclassDatum;
3485 oidvector *indclass;
3489 * Ignore any indexes that are currently being dropped. This will
3490 * prevent them from being searched, inserted into, or considered in
3491 * HOT-safety decisions. It's unsafe to touch such an index at all
3492 * since its catalog entries could disappear at any instant.
3494 if (!IndexIsLive(index))
3497 /* Add index's OID to result list in the proper order */
3498 result = insert_ordered_oid(result, index->indexrelid);
3501 * indclass cannot be referenced directly through the C struct,
3502 * because it comes after the variable-width indkey field. Must
3503 * extract the datum the hard way...
3505 indclassDatum = heap_getattr(htup,
3506 Anum_pg_index_indclass,
3507 GetPgIndexDescriptor(),
3510 indclass = (oidvector *) DatumGetPointer(indclassDatum);
3512 /* Check to see if it is a unique, non-partial btree index on OID */
3513 if (IndexIsValid(index) &&
3514 index->indnatts == 1 &&
3515 index->indisunique && index->indimmediate &&
3516 index->indkey.values[0] == ObjectIdAttributeNumber &&
3517 indclass->values[0] == OID_BTREE_OPS_OID &&
3518 heap_attisnull(htup, Anum_pg_index_indpred))
3519 oidIndex = index->indexrelid;
3522 systable_endscan(indscan);
3523 heap_close(indrel, AccessShareLock);
3525 /* Now save a copy of the completed list in the relcache entry. */
3526 oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
3527 relation->rd_indexlist = list_copy(result);
3528 relation->rd_oidindex = oidIndex;
3529 relation->rd_indexvalid = 1;
3530 MemoryContextSwitchTo(oldcxt);
3536 * insert_ordered_oid
3537 * Insert a new Oid into a sorted list of Oids, preserving ordering
3539 * Building the ordered list this way is O(N^2), but with a pretty small
3540 * constant, so for the number of entries we expect it will probably be
3541 * faster than trying to apply qsort(). Most tables don't have very many
3545 insert_ordered_oid(List *list, Oid datum)
3549 /* Does the datum belong at the front? */
3550 if (list == NIL || datum < linitial_oid(list))
3551 return lcons_oid(datum, list);
3552 /* No, so find the entry it belongs after */
3553 prev = list_head(list);
3556 ListCell *curr = lnext(prev);
3558 if (curr == NULL || datum < lfirst_oid(curr))
3559 break; /* it belongs after 'prev', before 'curr' */
3563 /* Insert datum into list after 'prev' */
3564 lappend_cell_oid(list, prev, datum);
3569 * RelationSetIndexList -- externally force the index list contents
3571 * This is used to temporarily override what we think the set of valid
3572 * indexes is (including the presence or absence of an OID index).
3573 * The forcing will be valid only until transaction commit or abort.
3575 * This should only be applied to nailed relations, because in a non-nailed
3576 * relation the hacked index list could be lost at any time due to SI
3577 * messages. In practice it is only used on pg_class (see REINDEX).
3579 * It is up to the caller to make sure the given list is correctly ordered.
3581 * We deliberately do not change rd_indexattr here: even when operating
3582 * with a temporary partial index list, HOT-update decisions must be made
3583 * correctly with respect to the full index set. It is up to the caller
3584 * to ensure that a correct rd_indexattr set has been cached before first
3585 * calling RelationSetIndexList; else a subsequent inquiry might cause a
3586 * wrong rd_indexattr set to get computed and cached.
3589 RelationSetIndexList(Relation relation, List *indexIds, Oid oidIndex)
3591 MemoryContext oldcxt;
3593 Assert(relation->rd_isnailed);
3594 /* Copy the list into the cache context (could fail for lack of mem) */
3595 oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
3596 indexIds = list_copy(indexIds);
3597 MemoryContextSwitchTo(oldcxt);
3598 /* Okay to replace old list */
3599 list_free(relation->rd_indexlist);
3600 relation->rd_indexlist = indexIds;
3601 relation->rd_oidindex = oidIndex;
3602 relation->rd_indexvalid = 2; /* mark list as forced */
3603 /* Flag relation as needing eoxact cleanup (to reset the list) */
3604 EOXactListAdd(relation);
3608 * RelationGetOidIndex -- get the pg_class OID of the relation's OID index
3610 * Returns InvalidOid if there is no such index.
3613 RelationGetOidIndex(Relation relation)
3618 * If relation doesn't have OIDs at all, caller is probably confused. (We
3619 * could just silently return InvalidOid, but it seems better to throw an
3622 Assert(relation->rd_rel->relhasoids);
3624 if (relation->rd_indexvalid == 0)
3626 /* RelationGetIndexList does the heavy lifting. */
3627 ilist = RelationGetIndexList(relation);
3629 Assert(relation->rd_indexvalid != 0);
3632 return relation->rd_oidindex;
3636 * RelationGetIndexExpressions -- get the index expressions for an index
3638 * We cache the result of transforming pg_index.indexprs into a node tree.
3639 * If the rel is not an index or has no expressional columns, we return NIL.
3640 * Otherwise, the returned tree is copied into the caller's memory context.
3641 * (We don't want to return a pointer to the relcache copy, since it could
3642 * disappear due to relcache invalidation.)
3645 RelationGetIndexExpressions(Relation relation)
3651 MemoryContext oldcxt;
3653 /* Quick exit if we already computed the result. */
3654 if (relation->rd_indexprs)
3655 return (List *) copyObject(relation->rd_indexprs);
3657 /* Quick exit if there is nothing to do. */
3658 if (relation->rd_indextuple == NULL ||
3659 heap_attisnull(relation->rd_indextuple, Anum_pg_index_indexprs))
3663 * We build the tree we intend to return in the caller's context. After
3664 * successfully completing the work, we copy it into the relcache entry.
3665 * This avoids problems if we get some sort of error partway through.
3667 exprsDatum = heap_getattr(relation->rd_indextuple,
3668 Anum_pg_index_indexprs,
3669 GetPgIndexDescriptor(),
3672 exprsString = TextDatumGetCString(exprsDatum);
3673 result = (List *) stringToNode(exprsString);
3677 * Run the expressions through eval_const_expressions. This is not just an
3678 * optimization, but is necessary, because the planner will be comparing
3679 * them to similarly-processed qual clauses, and may fail to detect valid
3680 * matches without this. We don't bother with canonicalize_qual, however.
3682 result = (List *) eval_const_expressions(NULL, (Node *) result);
3684 /* May as well fix opfuncids too */
3685 fix_opfuncids((Node *) result);
3687 /* Now save a copy of the completed tree in the relcache entry. */
3688 oldcxt = MemoryContextSwitchTo(relation->rd_indexcxt);
3689 relation->rd_indexprs = (List *) copyObject(result);
3690 MemoryContextSwitchTo(oldcxt);
3696 * RelationGetIndexPredicate -- get the index predicate for an index
3698 * We cache the result of transforming pg_index.indpred into an implicit-AND
3699 * node tree (suitable for ExecQual).
3700 * If the rel is not an index or has no predicate, we return NIL.
3701 * Otherwise, the returned tree is copied into the caller's memory context.
3702 * (We don't want to return a pointer to the relcache copy, since it could
3703 * disappear due to relcache invalidation.)
3706 RelationGetIndexPredicate(Relation relation)
3712 MemoryContext oldcxt;
3714 /* Quick exit if we already computed the result. */
3715 if (relation->rd_indpred)
3716 return (List *) copyObject(relation->rd_indpred);
3718 /* Quick exit if there is nothing to do. */
3719 if (relation->rd_indextuple == NULL ||
3720 heap_attisnull(relation->rd_indextuple, Anum_pg_index_indpred))
3724 * We build the tree we intend to return in the caller's context. After
3725 * successfully completing the work, we copy it into the relcache entry.
3726 * This avoids problems if we get some sort of error partway through.
3728 predDatum = heap_getattr(relation->rd_indextuple,
3729 Anum_pg_index_indpred,
3730 GetPgIndexDescriptor(),
3733 predString = TextDatumGetCString(predDatum);
3734 result = (List *) stringToNode(predString);
3738 * Run the expression through const-simplification and canonicalization.
3739 * This is not just an optimization, but is necessary, because the planner
3740 * will be comparing it to similarly-processed qual clauses, and may fail
3741 * to detect valid matches without this. This must match the processing
3742 * done to qual clauses in preprocess_expression()! (We can skip the
3743 * stuff involving subqueries, however, since we don't allow any in index
3746 result = (List *) eval_const_expressions(NULL, (Node *) result);
3748 result = (List *) canonicalize_qual((Expr *) result);
3750 /* Also convert to implicit-AND format */
3751 result = make_ands_implicit((Expr *) result);
3753 /* May as well fix opfuncids too */
3754 fix_opfuncids((Node *) result);
3756 /* Now save a copy of the completed tree in the relcache entry. */
3757 oldcxt = MemoryContextSwitchTo(relation->rd_indexcxt);
3758 relation->rd_indpred = (List *) copyObject(result);
3759 MemoryContextSwitchTo(oldcxt);
3765 * RelationGetIndexAttrBitmap -- get a bitmap of index attribute numbers
3767 * The result has a bit set for each attribute used anywhere in the index
3768 * definitions of all the indexes on this relation. (This includes not only
3769 * simple index keys, but attributes used in expressions and partial-index
3772 * If "keyAttrs" is true, only attributes that can be referenced by foreign
3773 * keys are considered.
3775 * Attribute numbers are offset by FirstLowInvalidHeapAttributeNumber so that
3776 * we can include system attributes (e.g., OID) in the bitmap representation.
3778 * Caller had better hold at least RowExclusiveLock on the target relation
3779 * to ensure that it has a stable set of indexes. This also makes it safe
3780 * (deadlock-free) for us to take locks on the relation's indexes.
3782 * The returned result is palloc'd in the caller's memory context and should
3783 * be bms_free'd when not needed anymore.
3786 RelationGetIndexAttrBitmap(Relation relation, bool keyAttrs)
3788 Bitmapset *indexattrs;
3789 Bitmapset *uindexattrs;
3792 MemoryContext oldcxt;
3794 /* Quick exit if we already computed the result. */
3795 if (relation->rd_indexattr != NULL)
3796 return bms_copy(keyAttrs ? relation->rd_keyattr : relation->rd_indexattr);
3798 /* Fast path if definitely no indexes */
3799 if (!RelationGetForm(relation)->relhasindex)
3803 * Get cached list of index OIDs
3805 indexoidlist = RelationGetIndexList(relation);
3807 /* Fall out if no indexes (but relhasindex was set) */
3808 if (indexoidlist == NIL)
3812 * For each index, add referenced attributes to indexattrs.
3814 * Note: we consider all indexes returned by RelationGetIndexList, even if
3815 * they are not indisready or indisvalid. This is important because an
3816 * index for which CREATE INDEX CONCURRENTLY has just started must be
3817 * included in HOT-safety decisions (see README.HOT). If a DROP INDEX
3818 * CONCURRENTLY is far enough along that we should ignore the index, it
3819 * won't be returned at all by RelationGetIndexList.
3823 foreach(l, indexoidlist)
3825 Oid indexOid = lfirst_oid(l);
3827 IndexInfo *indexInfo;
3831 indexDesc = index_open(indexOid, AccessShareLock);
3833 /* Extract index key information from the index's pg_index row */
3834 indexInfo = BuildIndexInfo(indexDesc);
3836 /* Can this index be referenced by a foreign key? */
3837 isKey = indexInfo->ii_Unique &&
3838 indexInfo->ii_Expressions == NIL &&
3839 indexInfo->ii_Predicate == NIL;
3841 /* Collect simple attribute references */
3842 for (i = 0; i < indexInfo->ii_NumIndexAttrs; i++)
3844 int attrnum = indexInfo->ii_KeyAttrNumbers[i];
3848 indexattrs = bms_add_member(indexattrs,
3849 attrnum - FirstLowInvalidHeapAttributeNumber);
3851 uindexattrs = bms_add_member(uindexattrs,
3852 attrnum - FirstLowInvalidHeapAttributeNumber);
3856 /* Collect all attributes used in expressions, too */
3857 pull_varattnos((Node *) indexInfo->ii_Expressions, 1, &indexattrs);
3859 /* Collect all attributes in the index predicate, too */
3860 pull_varattnos((Node *) indexInfo->ii_Predicate, 1, &indexattrs);
3862 index_close(indexDesc, AccessShareLock);
3865 list_free(indexoidlist);
3867 /* Now save a copy of the bitmap in the relcache entry. */
3868 oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
3869 relation->rd_indexattr = bms_copy(indexattrs);
3870 relation->rd_keyattr = bms_copy(uindexattrs);
3871 MemoryContextSwitchTo(oldcxt);
3873 /* We return our original working copy for caller to play with */
3874 return keyAttrs ? uindexattrs : indexattrs;
3878 * RelationGetExclusionInfo -- get info about index's exclusion constraint
3880 * This should be called only for an index that is known to have an
3881 * associated exclusion constraint. It returns arrays (palloc'd in caller's
3882 * context) of the exclusion operator OIDs, their underlying functions'
3883 * OIDs, and their strategy numbers in the index's opclasses. We cache
3884 * all this information since it requires a fair amount of work to get.
3887 RelationGetExclusionInfo(Relation indexRelation,
3890 uint16 **strategies)
3892 int ncols = indexRelation->rd_rel->relnatts;
3897 SysScanDesc conscan;
3898 ScanKeyData skey[1];
3901 MemoryContext oldcxt;
3904 /* Allocate result space in caller context */
3905 *operators = ops = (Oid *) palloc(sizeof(Oid) * ncols);
3906 *procs = funcs = (Oid *) palloc(sizeof(Oid) * ncols);
3907 *strategies = strats = (uint16 *) palloc(sizeof(uint16) * ncols);
3909 /* Quick exit if we have the data cached already */
3910 if (indexRelation->rd_exclstrats != NULL)
3912 memcpy(ops, indexRelation->rd_exclops, sizeof(Oid) * ncols);
3913 memcpy(funcs, indexRelation->rd_exclprocs, sizeof(Oid) * ncols);
3914 memcpy(strats, indexRelation->rd_exclstrats, sizeof(uint16) * ncols);
3919 * Search pg_constraint for the constraint associated with the index. To
3920 * make this not too painfully slow, we use the index on conrelid; that
3921 * will hold the parent relation's OID not the index's own OID.
3923 ScanKeyInit(&skey[0],
3924 Anum_pg_constraint_conrelid,
3925 BTEqualStrategyNumber, F_OIDEQ,
3926 ObjectIdGetDatum(indexRelation->rd_index->indrelid));
3928 conrel = heap_open(ConstraintRelationId, AccessShareLock);
3929 conscan = systable_beginscan(conrel, ConstraintRelidIndexId, true,
3930 SnapshotNow, 1, skey);
3933 while (HeapTupleIsValid(htup = systable_getnext(conscan)))
3935 Form_pg_constraint conform = (Form_pg_constraint) GETSTRUCT(htup);
3941 /* We want the exclusion constraint owning the index */
3942 if (conform->contype != CONSTRAINT_EXCLUSION ||
3943 conform->conindid != RelationGetRelid(indexRelation))
3946 /* There should be only one */
3948 elog(ERROR, "unexpected exclusion constraint record found for rel %s",
3949 RelationGetRelationName(indexRelation));
3952 /* Extract the operator OIDS from conexclop */
3953 val = fastgetattr(htup,
3954 Anum_pg_constraint_conexclop,
3955 conrel->rd_att, &isnull);
3957 elog(ERROR, "null conexclop for rel %s",
3958 RelationGetRelationName(indexRelation));
3960 arr = DatumGetArrayTypeP(val); /* ensure not toasted */
3961 nelem = ARR_DIMS(arr)[0];
3962 if (ARR_NDIM(arr) != 1 ||
3965 ARR_ELEMTYPE(arr) != OIDOID)
3966 elog(ERROR, "conexclop is not a 1-D Oid array");
3968 memcpy(ops, ARR_DATA_PTR(arr), sizeof(Oid) * ncols);
3971 systable_endscan(conscan);
3972 heap_close(conrel, AccessShareLock);
3975 elog(ERROR, "exclusion constraint record missing for rel %s",
3976 RelationGetRelationName(indexRelation));
3978 /* We need the func OIDs and strategy numbers too */
3979 for (i = 0; i < ncols; i++)
3981 funcs[i] = get_opcode(ops[i]);
3982 strats[i] = get_op_opfamily_strategy(ops[i],
3983 indexRelation->rd_opfamily[i]);
3984 /* shouldn't fail, since it was checked at index creation */
3985 if (strats[i] == InvalidStrategy)
3986 elog(ERROR, "could not find strategy for operator %u in family %u",
3987 ops[i], indexRelation->rd_opfamily[i]);
3990 /* Save a copy of the results in the relcache entry. */
3991 oldcxt = MemoryContextSwitchTo(indexRelation->rd_indexcxt);
3992 indexRelation->rd_exclops = (Oid *) palloc(sizeof(Oid) * ncols);
3993 indexRelation->rd_exclprocs = (Oid *) palloc(sizeof(Oid) * ncols);
3994 indexRelation->rd_exclstrats = (uint16 *) palloc(sizeof(uint16) * ncols);
3995 memcpy(indexRelation->rd_exclops, ops, sizeof(Oid) * ncols);
3996 memcpy(indexRelation->rd_exclprocs, funcs, sizeof(Oid) * ncols);
3997 memcpy(indexRelation->rd_exclstrats, strats, sizeof(uint16) * ncols);
3998 MemoryContextSwitchTo(oldcxt);
4003 * Routines to support ereport() reports of relation-related errors
4005 * These could have been put into elog.c, but it seems like a module layering
4006 * violation to have elog.c calling relcache or syscache stuff --- and we
4007 * definitely don't want elog.h including rel.h. So we put them here.
4011 * errtable --- stores schema_name and table_name of a table
4012 * within the current errordata.
4015 errtable(Relation rel)
4017 err_generic_string(PG_DIAG_SCHEMA_NAME,
4018 get_namespace_name(RelationGetNamespace(rel)));
4019 err_generic_string(PG_DIAG_TABLE_NAME, RelationGetRelationName(rel));
4021 return 0; /* return value does not matter */
4025 * errtablecol --- stores schema_name, table_name and column_name
4026 * of a table column within the current errordata.
4028 * The column is specified by attribute number --- for most callers, this is
4029 * easier and less error-prone than getting the column name for themselves.
4032 errtablecol(Relation rel, int attnum)
4034 TupleDesc reldesc = RelationGetDescr(rel);
4035 const char *colname;
4037 /* Use reldesc if it's a user attribute, else consult the catalogs */
4038 if (attnum > 0 && attnum <= reldesc->natts)
4039 colname = NameStr(reldesc->attrs[attnum - 1]->attname);
4041 colname = get_relid_attribute_name(RelationGetRelid(rel), attnum);
4043 return errtablecolname(rel, colname);
4047 * errtablecolname --- stores schema_name, table_name and column_name
4048 * of a table column within the current errordata, where the column name is
4049 * given directly rather than extracted from the relation's catalog data.
4051 * Don't use this directly unless errtablecol() is inconvenient for some
4052 * reason. This might possibly be needed during intermediate states in ALTER
4053 * TABLE, for instance.
4056 errtablecolname(Relation rel, const char *colname)
4059 err_generic_string(PG_DIAG_COLUMN_NAME, colname);
4061 return 0; /* return value does not matter */
4065 * errtableconstraint --- stores schema_name, table_name and constraint_name
4066 * of a table-related constraint within the current errordata.
4069 errtableconstraint(Relation rel, const char *conname)
4072 err_generic_string(PG_DIAG_CONSTRAINT_NAME, conname);
4074 return 0; /* return value does not matter */
4079 * load_relcache_init_file, write_relcache_init_file
4081 * In late 1992, we started regularly having databases with more than
4082 * a thousand classes in them. With this number of classes, it became
4083 * critical to do indexed lookups on the system catalogs.
4085 * Bootstrapping these lookups is very hard. We want to be able to
4086 * use an index on pg_attribute, for example, but in order to do so,
4087 * we must have read pg_attribute for the attributes in the index,
4088 * which implies that we need to use the index.
4090 * In order to get around the problem, we do the following:
4092 * + When the database system is initialized (at initdb time), we
4093 * don't use indexes. We do sequential scans.
4095 * + When the backend is started up in normal mode, we load an image
4096 * of the appropriate relation descriptors, in internal format,
4097 * from an initialization file in the data/base/... directory.
4099 * + If the initialization file isn't there, then we create the
4100 * relation descriptors using sequential scans and write 'em to
4101 * the initialization file for use by subsequent backends.
4103 * As of Postgres 9.0, there is one local initialization file in each
4104 * database, plus one shared initialization file for shared catalogs.
4106 * We could dispense with the initialization files and just build the
4107 * critical reldescs the hard way on every backend startup, but that
4108 * slows down backend startup noticeably.
4110 * We can in fact go further, and save more relcache entries than
4111 * just the ones that are absolutely critical; this allows us to speed
4112 * up backend startup by not having to build such entries the hard way.
4113 * Presently, all the catalog and index entries that are referred to
4114 * by catcaches are stored in the initialization files.
4116 * The same mechanism that detects when catcache and relcache entries
4117 * need to be invalidated (due to catalog updates) also arranges to
4118 * unlink the initialization files when the contents may be out of date.
4119 * The files will then be rebuilt during the next backend startup.
4123 * load_relcache_init_file -- attempt to load cache from the shared
4124 * or local cache init file
4126 * If successful, return TRUE and set criticalRelcachesBuilt or
4127 * criticalSharedRelcachesBuilt to true.
4128 * If not successful, return FALSE.
4130 * NOTE: we assume we are already switched into CacheMemoryContext.
4133 load_relcache_init_file(bool shared)
4136 char initfilename[MAXPGPATH];
4147 snprintf(initfilename, sizeof(initfilename), "global/%s",
4148 RELCACHE_INIT_FILENAME);
4150 snprintf(initfilename, sizeof(initfilename), "%s/%s",
4151 DatabasePath, RELCACHE_INIT_FILENAME);
4153 fp = AllocateFile(initfilename, PG_BINARY_R);
4158 * Read the index relcache entries from the file. Note we will not enter
4159 * any of them into the cache if the read fails partway through; this
4160 * helps to guard against broken init files.
4163 rels = (Relation *) palloc(max_rels * sizeof(Relation));
4165 nailed_rels = nailed_indexes = 0;
4167 /* check for correct magic number (compatible version) */
4168 if (fread(&magic, 1, sizeof(magic), fp) != sizeof(magic))
4170 if (magic != RELCACHE_INIT_FILEMAGIC)
4173 for (relno = 0;; relno++)
4178 Form_pg_class relform;
4181 /* first read the relation descriptor length */
4182 nread = fread(&len, 1, sizeof(len), fp);
4183 if (nread != sizeof(len))
4186 break; /* end of file */
4190 /* safety check for incompatible relcache layout */
4191 if (len != sizeof(RelationData))
4194 /* allocate another relcache header */
4195 if (num_rels >= max_rels)
4198 rels = (Relation *) repalloc(rels, max_rels * sizeof(Relation));
4201 rel = rels[num_rels++] = (Relation) palloc(len);
4203 /* then, read the Relation structure */
4204 if (fread(rel, 1, len, fp) != len)
4207 /* next read the relation tuple form */
4208 if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
4211 relform = (Form_pg_class) palloc(len);
4212 if (fread(relform, 1, len, fp) != len)
4215 rel->rd_rel = relform;
4217 /* initialize attribute tuple forms */
4218 rel->rd_att = CreateTemplateTupleDesc(relform->relnatts,
4219 relform->relhasoids);
4220 rel->rd_att->tdrefcount = 1; /* mark as refcounted */
4222 rel->rd_att->tdtypeid = relform->reltype;
4223 rel->rd_att->tdtypmod = -1; /* unnecessary, but... */
4225 /* next read all the attribute tuple form data entries */
4226 has_not_null = false;
4227 for (i = 0; i < relform->relnatts; i++)
4229 if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
4231 if (len != ATTRIBUTE_FIXED_PART_SIZE)
4233 if (fread(rel->rd_att->attrs[i], 1, len, fp) != len)
4236 has_not_null |= rel->rd_att->attrs[i]->attnotnull;
4239 /* next read the access method specific field */
4240 if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
4244 rel->rd_options = palloc(len);
4245 if (fread(rel->rd_options, 1, len, fp) != len)
4247 if (len != VARSIZE(rel->rd_options))
4248 goto read_failed; /* sanity check */
4252 rel->rd_options = NULL;
4255 /* mark not-null status */
4258 TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr));
4260 constr->has_not_null = true;
4261 rel->rd_att->constr = constr;
4264 /* If it's an index, there's more to do */
4265 if (rel->rd_rel->relkind == RELKIND_INDEX)
4268 MemoryContext indexcxt;
4271 RegProcedure *support;
4276 /* Count nailed indexes to ensure we have 'em all */
4277 if (rel->rd_isnailed)
4280 /* next, read the pg_index tuple */
4281 if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
4284 rel->rd_indextuple = (HeapTuple) palloc(len);
4285 if (fread(rel->rd_indextuple, 1, len, fp) != len)
4288 /* Fix up internal pointers in the tuple -- see heap_copytuple */
4289 rel->rd_indextuple->t_data = (HeapTupleHeader) ((char *) rel->rd_indextuple + HEAPTUPLESIZE);
4290 rel->rd_index = (Form_pg_index) GETSTRUCT(rel->rd_indextuple);
4292 /* next, read the access method tuple form */
4293 if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
4296 am = (Form_pg_am) palloc(len);
4297 if (fread(am, 1, len, fp) != len)
4302 * prepare index info context --- parameters should match
4303 * RelationInitIndexAccessInfo
4305 indexcxt = AllocSetContextCreate(CacheMemoryContext,
4306 RelationGetRelationName(rel),
4307 ALLOCSET_SMALL_MINSIZE,
4308 ALLOCSET_SMALL_INITSIZE,
4309 ALLOCSET_SMALL_MAXSIZE);
4310 rel->rd_indexcxt = indexcxt;
4312 /* next, read the vector of opfamily OIDs */
4313 if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
4316 opfamily = (Oid *) MemoryContextAlloc(indexcxt, len);
4317 if (fread(opfamily, 1, len, fp) != len)
4320 rel->rd_opfamily = opfamily;
4322 /* next, read the vector of opcintype OIDs */
4323 if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
4326 opcintype = (Oid *) MemoryContextAlloc(indexcxt, len);
4327 if (fread(opcintype, 1, len, fp) != len)
4330 rel->rd_opcintype = opcintype;
4332 /* next, read the vector of support procedure OIDs */
4333 if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
4335 support = (RegProcedure *) MemoryContextAlloc(indexcxt, len);
4336 if (fread(support, 1, len, fp) != len)
4339 rel->rd_support = support;
4341 /* next, read the vector of collation OIDs */
4342 if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
4345 indcollation = (Oid *) MemoryContextAlloc(indexcxt, len);
4346 if (fread(indcollation, 1, len, fp) != len)
4349 rel->rd_indcollation = indcollation;
4351 /* finally, read the vector of indoption values */
4352 if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
4355 indoption = (int16 *) MemoryContextAlloc(indexcxt, len);
4356 if (fread(indoption, 1, len, fp) != len)
4359 rel->rd_indoption = indoption;
4361 /* set up zeroed fmgr-info vectors */
4362 rel->rd_aminfo = (RelationAmInfo *)
4363 MemoryContextAllocZero(indexcxt, sizeof(RelationAmInfo));
4364 nsupport = relform->relnatts * am->amsupport;
4365 rel->rd_supportinfo = (FmgrInfo *)
4366 MemoryContextAllocZero(indexcxt, nsupport * sizeof(FmgrInfo));
4370 /* Count nailed rels to ensure we have 'em all */
4371 if (rel->rd_isnailed)
4374 Assert(rel->rd_index == NULL);
4375 Assert(rel->rd_indextuple == NULL);
4376 Assert(rel->rd_am == NULL);
4377 Assert(rel->rd_indexcxt == NULL);
4378 Assert(rel->rd_aminfo == NULL);
4379 Assert(rel->rd_opfamily == NULL);
4380 Assert(rel->rd_opcintype == NULL);
4381 Assert(rel->rd_support == NULL);
4382 Assert(rel->rd_supportinfo == NULL);
4383 Assert(rel->rd_indoption == NULL);
4384 Assert(rel->rd_indcollation == NULL);
4388 * Rules and triggers are not saved (mainly because the internal
4389 * format is complex and subject to change). They must be rebuilt if
4390 * needed by RelationCacheInitializePhase3. This is not expected to
4391 * be a big performance hit since few system catalogs have such. Ditto
4392 * for index expressions, predicates, and exclusion info.
4394 rel->rd_rules = NULL;
4395 rel->rd_rulescxt = NULL;
4396 rel->trigdesc = NULL;
4397 rel->rd_indexprs = NIL;
4398 rel->rd_indpred = NIL;
4399 rel->rd_exclops = NULL;
4400 rel->rd_exclprocs = NULL;
4401 rel->rd_exclstrats = NULL;
4404 * Reset transient-state fields in the relcache entry
4406 rel->rd_smgr = NULL;
4407 if (rel->rd_isnailed)
4411 rel->rd_indexvalid = 0;
4412 rel->rd_indexlist = NIL;
4413 rel->rd_indexattr = NULL;
4414 rel->rd_oidindex = InvalidOid;
4415 rel->rd_createSubid = InvalidSubTransactionId;
4416 rel->rd_newRelfilenodeSubid = InvalidSubTransactionId;
4417 rel->rd_amcache = NULL;
4418 MemSet(&rel->pgstat_info, 0, sizeof(rel->pgstat_info));
4421 * Recompute lock and physical addressing info. This is needed in
4422 * case the pg_internal.init file was copied from some other database
4423 * by CREATE DATABASE.
4425 RelationInitLockInfo(rel);
4426 RelationInitPhysicalAddr(rel);
4430 * We reached the end of the init file without apparent problem. Did we
4431 * get the right number of nailed items? (This is a useful crosscheck in
4432 * case the set of critical rels or indexes changes.)
4436 if (nailed_rels != NUM_CRITICAL_SHARED_RELS ||
4437 nailed_indexes != NUM_CRITICAL_SHARED_INDEXES)
4442 if (nailed_rels != NUM_CRITICAL_LOCAL_RELS ||
4443 nailed_indexes != NUM_CRITICAL_LOCAL_INDEXES)
4448 * OK, all appears well.
4450 * Now insert all the new relcache entries into the cache.
4452 for (relno = 0; relno < num_rels; relno++)
4454 RelationCacheInsert(rels[relno]);
4455 /* also make a list of their OIDs, for RelationIdIsInInitFile */
4457 initFileRelationIds = lcons_oid(RelationGetRelid(rels[relno]),
4458 initFileRelationIds);
4465 criticalSharedRelcachesBuilt = true;
4467 criticalRelcachesBuilt = true;
4471 * init file is broken, so do it the hard way. We don't bother trying to
4472 * free the clutter we just allocated; it's not in the relcache so it
4483 * Write out a new initialization file with the current contents
4484 * of the relcache (either shared rels or local rels, as indicated).
4487 write_relcache_init_file(bool shared)
4490 char tempfilename[MAXPGPATH];
4491 char finalfilename[MAXPGPATH];
4493 HASH_SEQ_STATUS status;
4494 RelIdCacheEnt *idhentry;
4495 MemoryContext oldcxt;
4499 * We must write a temporary file and rename it into place. Otherwise,
4500 * another backend starting at about the same time might crash trying to
4501 * read the partially-complete file.
4505 snprintf(tempfilename, sizeof(tempfilename), "global/%s.%d",
4506 RELCACHE_INIT_FILENAME, MyProcPid);
4507 snprintf(finalfilename, sizeof(finalfilename), "global/%s",
4508 RELCACHE_INIT_FILENAME);
4512 snprintf(tempfilename, sizeof(tempfilename), "%s/%s.%d",
4513 DatabasePath, RELCACHE_INIT_FILENAME, MyProcPid);
4514 snprintf(finalfilename, sizeof(finalfilename), "%s/%s",
4515 DatabasePath, RELCACHE_INIT_FILENAME);
4518 unlink(tempfilename); /* in case it exists w/wrong permissions */
4520 fp = AllocateFile(tempfilename, PG_BINARY_W);
4524 * We used to consider this a fatal error, but we might as well
4525 * continue with backend startup ...
4528 (errcode_for_file_access(),
4529 errmsg("could not create relation-cache initialization file \"%s\": %m",
4531 errdetail("Continuing anyway, but there's something wrong.")));
4536 * Write a magic number to serve as a file version identifier. We can
4537 * change the magic number whenever the relcache layout changes.
4539 magic = RELCACHE_INIT_FILEMAGIC;
4540 if (fwrite(&magic, 1, sizeof(magic), fp) != sizeof(magic))
4541 elog(FATAL, "could not write init file");
4544 * Write all the appropriate reldescs (in no particular order).
4546 hash_seq_init(&status, RelationIdCache);
4548 while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
4550 Relation rel = idhentry->reldesc;
4551 Form_pg_class relform = rel->rd_rel;
4553 /* ignore if not correct group */
4554 if (relform->relisshared != shared)
4557 /* first write the relcache entry proper */
4558 write_item(rel, sizeof(RelationData), fp);
4560 /* next write the relation tuple form */
4561 write_item(relform, CLASS_TUPLE_SIZE, fp);
4563 /* next, do all the attribute tuple form data entries */
4564 for (i = 0; i < relform->relnatts; i++)
4566 write_item(rel->rd_att->attrs[i], ATTRIBUTE_FIXED_PART_SIZE, fp);
4569 /* next, do the access method specific field */
4570 write_item(rel->rd_options,
4571 (rel->rd_options ? VARSIZE(rel->rd_options) : 0),
4574 /* If it's an index, there's more to do */
4575 if (rel->rd_rel->relkind == RELKIND_INDEX)
4577 Form_pg_am am = rel->rd_am;
4579 /* write the pg_index tuple */
4580 /* we assume this was created by heap_copytuple! */
4581 write_item(rel->rd_indextuple,
4582 HEAPTUPLESIZE + rel->rd_indextuple->t_len,
4585 /* next, write the access method tuple form */
4586 write_item(am, sizeof(FormData_pg_am), fp);
4588 /* next, write the vector of opfamily OIDs */
4589 write_item(rel->rd_opfamily,
4590 relform->relnatts * sizeof(Oid),
4593 /* next, write the vector of opcintype OIDs */
4594 write_item(rel->rd_opcintype,
4595 relform->relnatts * sizeof(Oid),
4598 /* next, write the vector of support procedure OIDs */
4599 write_item(rel->rd_support,
4600 relform->relnatts * (am->amsupport * sizeof(RegProcedure)),
4603 /* next, write the vector of collation OIDs */
4604 write_item(rel->rd_indcollation,
4605 relform->relnatts * sizeof(Oid),
4608 /* finally, write the vector of indoption values */
4609 write_item(rel->rd_indoption,
4610 relform->relnatts * sizeof(int16),
4614 /* also make a list of their OIDs, for RelationIdIsInInitFile */
4617 oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
4618 initFileRelationIds = lcons_oid(RelationGetRelid(rel),
4619 initFileRelationIds);
4620 MemoryContextSwitchTo(oldcxt);
4625 elog(FATAL, "could not write init file");
4628 * Now we have to check whether the data we've so painstakingly
4629 * accumulated is already obsolete due to someone else's just-committed
4630 * catalog changes. If so, we just delete the temp file and leave it to
4631 * the next backend to try again. (Our own relcache entries will be
4632 * updated by SI message processing, but we can't be sure whether what we
4633 * wrote out was up-to-date.)
4635 * This mustn't run concurrently with the code that unlinks an init file
4636 * and sends SI messages, so grab a serialization lock for the duration.
4638 LWLockAcquire(RelCacheInitLock, LW_EXCLUSIVE);
4640 /* Make sure we have seen all incoming SI messages */
4641 AcceptInvalidationMessages();
4644 * If we have received any SI relcache invals since backend start, assume
4645 * we may have written out-of-date data.
4647 if (relcacheInvalsReceived == 0L)
4650 * OK, rename the temp file to its final name, deleting any
4651 * previously-existing init file.
4653 * Note: a failure here is possible under Cygwin, if some other
4654 * backend is holding open an unlinked-but-not-yet-gone init file. So
4655 * treat this as a noncritical failure; just remove the useless temp
4658 if (rename(tempfilename, finalfilename) < 0)
4659 unlink(tempfilename);
4663 /* Delete the already-obsolete temp file */
4664 unlink(tempfilename);
4667 LWLockRelease(RelCacheInitLock);
4670 /* write a chunk of data preceded by its length */
4672 write_item(const void *data, Size len, FILE *fp)
4674 if (fwrite(&len, 1, sizeof(len), fp) != sizeof(len))
4675 elog(FATAL, "could not write init file");
4676 if (fwrite(data, 1, len, fp) != len)
4677 elog(FATAL, "could not write init file");
4681 * Detect whether a given relation (identified by OID) is one of the ones
4682 * we store in the local relcache init file.
4684 * Note that we effectively assume that all backends running in a database
4685 * would choose to store the same set of relations in the init file;
4686 * otherwise there are cases where we'd fail to detect the need for an init
4687 * file invalidation. This does not seem likely to be a problem in practice.
4690 RelationIdIsInInitFile(Oid relationId)
4692 return list_member_oid(initFileRelationIds, relationId);
4696 * Invalidate (remove) the init file during commit of a transaction that
4697 * changed one or more of the relation cache entries that are kept in the
4700 * To be safe against concurrent inspection or rewriting of the init file,
4701 * we must take RelCacheInitLock, then remove the old init file, then send
4702 * the SI messages that include relcache inval for such relations, and then
4703 * release RelCacheInitLock. This serializes the whole affair against
4704 * write_relcache_init_file, so that we can be sure that any other process
4705 * that's concurrently trying to create a new init file won't move an
4706 * already-stale version into place after we unlink. Also, because we unlink
4707 * before sending the SI messages, a backend that's currently starting cannot
4708 * read the now-obsolete init file and then miss the SI messages that will
4709 * force it to update its relcache entries. (This works because the backend
4710 * startup sequence gets into the sinval array before trying to load the init
4713 * We take the lock and do the unlink in RelationCacheInitFilePreInvalidate,
4714 * then release the lock in RelationCacheInitFilePostInvalidate. Caller must
4715 * send any pending SI messages between those calls.
4717 * Notice this deals only with the local init file, not the shared init file.
4718 * The reason is that there can never be a "significant" change to the
4719 * relcache entry of a shared relation; the most that could happen is
4720 * updates of noncritical fields such as relpages/reltuples. So, while
4721 * it's worth updating the shared init file from time to time, it can never
4722 * be invalid enough to make it necessary to remove it.
4725 RelationCacheInitFilePreInvalidate(void)
4727 char initfilename[MAXPGPATH];
4729 snprintf(initfilename, sizeof(initfilename), "%s/%s",
4730 DatabasePath, RELCACHE_INIT_FILENAME);
4732 LWLockAcquire(RelCacheInitLock, LW_EXCLUSIVE);
4734 if (unlink(initfilename) < 0)
4737 * The file might not be there if no backend has been started since
4738 * the last removal. But complain about failures other than ENOENT.
4739 * Fortunately, it's not too late to abort the transaction if we can't
4740 * get rid of the would-be-obsolete init file.
4742 if (errno != ENOENT)
4744 (errcode_for_file_access(),
4745 errmsg("could not remove cache file \"%s\": %m",
4751 RelationCacheInitFilePostInvalidate(void)
4753 LWLockRelease(RelCacheInitLock);
4757 * Remove the init files during postmaster startup.
4759 * We used to keep the init files across restarts, but that is unsafe in PITR
4760 * scenarios, and even in simple crash-recovery cases there are windows for
4761 * the init files to become out-of-sync with the database. So now we just
4762 * remove them during startup and expect the first backend launch to rebuild
4763 * them. Of course, this has to happen in each database of the cluster.
4766 RelationCacheInitFileRemove(void)
4768 const char *tblspcdir = "pg_tblspc";
4771 char path[MAXPGPATH];
4774 * We zap the shared cache file too. In theory it can't get out of sync
4775 * enough to be a problem, but in data-corruption cases, who knows ...
4777 snprintf(path, sizeof(path), "global/%s",
4778 RELCACHE_INIT_FILENAME);
4779 unlink_initfile(path);
4781 /* Scan everything in the default tablespace */
4782 RelationCacheInitFileRemoveInDir("base");
4784 /* Scan the tablespace link directory to find non-default tablespaces */
4785 dir = AllocateDir(tblspcdir);
4788 elog(LOG, "could not open tablespace link directory \"%s\": %m",
4793 while ((de = ReadDir(dir, tblspcdir)) != NULL)
4795 if (strspn(de->d_name, "0123456789") == strlen(de->d_name))
4797 /* Scan the tablespace dir for per-database dirs */
4798 snprintf(path, sizeof(path), "%s/%s/%s",
4799 tblspcdir, de->d_name, TABLESPACE_VERSION_DIRECTORY);
4800 RelationCacheInitFileRemoveInDir(path);
4807 /* Process one per-tablespace directory for RelationCacheInitFileRemove */
4809 RelationCacheInitFileRemoveInDir(const char *tblspcpath)
4813 char initfilename[MAXPGPATH];
4815 /* Scan the tablespace directory to find per-database directories */
4816 dir = AllocateDir(tblspcpath);
4819 elog(LOG, "could not open tablespace directory \"%s\": %m",
4824 while ((de = ReadDir(dir, tblspcpath)) != NULL)
4826 if (strspn(de->d_name, "0123456789") == strlen(de->d_name))
4828 /* Try to remove the init file in each database */
4829 snprintf(initfilename, sizeof(initfilename), "%s/%s/%s",
4830 tblspcpath, de->d_name, RELCACHE_INIT_FILENAME);
4831 unlink_initfile(initfilename);
4839 unlink_initfile(const char *initfilename)
4841 if (unlink(initfilename) < 0)
4843 /* It might not be there, but log any error other than ENOENT */
4844 if (errno != ENOENT)
4845 elog(LOG, "could not remove cache file \"%s\": %m", initfilename);