1 /*-------------------------------------------------------------------------
4 * POSTGRES relation descriptor cache code
6 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/utils/cache/relcache.c,v 1.248 2006/09/05 21:08:36 tgl Exp $
13 *-------------------------------------------------------------------------
17 * RelationCacheInitialize - initialize relcache (to empty)
18 * RelationCacheInitializePhase2 - finish initializing relcache
19 * RelationIdGetRelation - get a reldesc by relation id
20 * RelationClose - close an open relation
23 * The following code contains many undocumented hacks. Please be
32 #include "access/genam.h"
33 #include "access/heapam.h"
34 #include "access/reloptions.h"
35 #include "access/xact.h"
36 #include "catalog/catalog.h"
37 #include "catalog/indexing.h"
38 #include "catalog/namespace.h"
39 #include "catalog/pg_amop.h"
40 #include "catalog/pg_amproc.h"
41 #include "catalog/pg_attrdef.h"
42 #include "catalog/pg_authid.h"
43 #include "catalog/pg_constraint.h"
44 #include "catalog/pg_namespace.h"
45 #include "catalog/pg_opclass.h"
46 #include "catalog/pg_proc.h"
47 #include "catalog/pg_rewrite.h"
48 #include "catalog/pg_type.h"
49 #include "commands/trigger.h"
50 #include "miscadmin.h"
51 #include "optimizer/clauses.h"
52 #include "optimizer/planmain.h"
53 #include "optimizer/prep.h"
54 #include "rewrite/rewriteDefine.h"
55 #include "storage/fd.h"
56 #include "storage/smgr.h"
57 #include "utils/builtins.h"
58 #include "utils/fmgroids.h"
59 #include "utils/inval.h"
60 #include "utils/memutils.h"
61 #include "utils/relcache.h"
62 #include "utils/resowner.h"
63 #include "utils/syscache.h"
64 #include "utils/typcache.h"
68 * name of relcache init file, used to speed up backend startup
70 #define RELCACHE_INIT_FILENAME "pg_internal.init"
72 #define RELCACHE_INIT_FILEMAGIC 0x573263 /* version ID value */
75 * hardcoded tuple descriptors. see include/catalog/pg_attribute.h
77 static FormData_pg_attribute Desc_pg_class[Natts_pg_class] = {Schema_pg_class};
78 static FormData_pg_attribute Desc_pg_attribute[Natts_pg_attribute] = {Schema_pg_attribute};
79 static FormData_pg_attribute Desc_pg_proc[Natts_pg_proc] = {Schema_pg_proc};
80 static FormData_pg_attribute Desc_pg_type[Natts_pg_type] = {Schema_pg_type};
81 static FormData_pg_attribute Desc_pg_index[Natts_pg_index] = {Schema_pg_index};
84 * Hash tables that index the relation cache
86 * We used to index the cache by both name and OID, but now there
87 * is only an index by OID.
89 typedef struct relidcacheent
95 static HTAB *RelationIdCache;
98 * This flag is false until we have prepared the critical relcache entries
99 * that are needed to do indexscans on the tables read by relcache building.
101 bool criticalRelcachesBuilt = false;
104 * This counter counts relcache inval events received since backend startup
105 * (but only for rels that are actually in cache). Presently, we use it only
106 * to detect whether data about to be written by write_relcache_init_file()
107 * might already be obsolete.
109 static long relcacheInvalsReceived = 0L;
112 * This list remembers the OIDs of the relations cached in the relcache
115 static List *initFileRelationIds = NIL;
118 * This flag lets us optimize away work in AtEO(Sub)Xact_RelationCache().
120 static bool need_eoxact_work = false;
124 * macros to manipulate the lookup hashtables
126 #define RelationCacheInsert(RELATION) \
128 RelIdCacheEnt *idhentry; bool found; \
129 idhentry = (RelIdCacheEnt*)hash_search(RelationIdCache, \
130 (void *) &(RELATION->rd_id), \
133 /* used to give notice if found -- now just keep quiet */ \
134 idhentry->reldesc = RELATION; \
137 #define RelationIdCacheLookup(ID, RELATION) \
139 RelIdCacheEnt *hentry; \
140 hentry = (RelIdCacheEnt*)hash_search(RelationIdCache, \
141 (void *) &(ID), HASH_FIND,NULL); \
143 RELATION = hentry->reldesc; \
148 #define RelationCacheDelete(RELATION) \
150 RelIdCacheEnt *idhentry; \
151 idhentry = (RelIdCacheEnt*)hash_search(RelationIdCache, \
152 (void *) &(RELATION->rd_id), \
153 HASH_REMOVE, NULL); \
154 if (idhentry == NULL) \
155 elog(WARNING, "trying to delete a rd_id reldesc that does not exist"); \
160 * Special cache for opclass-related information
162 * Note: only default-subtype operators and support procs get cached
164 typedef struct opclasscacheent
166 Oid opclassoid; /* lookup key: OID of opclass */
167 bool valid; /* set TRUE after successful fill-in */
168 StrategyNumber numStrats; /* max # of strategies (from pg_am) */
169 StrategyNumber numSupport; /* max # of support procs (from pg_am) */
170 Oid *operatorOids; /* strategy operators' OIDs */
171 RegProcedure *supportProcs; /* support procs */
174 static HTAB *OpClassCache = NULL;
177 /* non-export function prototypes */
179 static void RelationClearRelation(Relation relation, bool rebuild);
181 static void RelationReloadClassinfo(Relation relation);
182 static void RelationFlushRelation(Relation relation);
183 static bool load_relcache_init_file(void);
184 static void write_relcache_init_file(void);
185 static void write_item(const void *data, Size len, FILE *fp);
187 static void formrdesc(const char *relationName, Oid relationReltype,
188 bool hasoids, int natts, FormData_pg_attribute *att);
190 static HeapTuple ScanPgRelation(Oid targetRelId, bool indexOK);
191 static Relation AllocateRelationDesc(Relation relation, Form_pg_class relp);
192 static void RelationParseRelOptions(Relation relation, HeapTuple tuple);
193 static void RelationBuildTupleDesc(Relation relation);
194 static Relation RelationBuildDesc(Oid targetRelId, Relation oldrelation);
195 static void RelationInitPhysicalAddr(Relation relation);
196 static TupleDesc GetPgClassDescriptor(void);
197 static TupleDesc GetPgIndexDescriptor(void);
198 static void AttrDefaultFetch(Relation relation);
199 static void CheckConstraintFetch(Relation relation);
200 static List *insert_ordered_oid(List *list, Oid datum);
201 static void IndexSupportInitialize(oidvector *indclass,
203 RegProcedure *indexSupport,
204 StrategyNumber maxStrategyNumber,
205 StrategyNumber maxSupportNumber,
206 AttrNumber maxAttributeNumber);
207 static OpClassCacheEnt *LookupOpclassInfo(Oid operatorClassOid,
208 StrategyNumber numStrats,
209 StrategyNumber numSupport);
215 * this is used by RelationBuildDesc to find a pg_class
216 * tuple matching targetRelId.
218 * NB: the returned tuple has been copied into palloc'd storage
219 * and must eventually be freed with heap_freetuple.
222 ScanPgRelation(Oid targetRelId, bool indexOK)
224 HeapTuple pg_class_tuple;
225 Relation pg_class_desc;
226 SysScanDesc pg_class_scan;
233 ObjectIdAttributeNumber,
234 BTEqualStrategyNumber, F_OIDEQ,
235 ObjectIdGetDatum(targetRelId));
238 * Open pg_class and fetch a tuple. Force heap scan if we haven't yet
239 * built the critical relcache entries (this includes initdb and startup
240 * without a pg_internal.init file). The caller can also force a heap
241 * scan by setting indexOK == false.
243 pg_class_desc = heap_open(RelationRelationId, AccessShareLock);
244 pg_class_scan = systable_beginscan(pg_class_desc, ClassOidIndexId,
245 indexOK && criticalRelcachesBuilt,
249 pg_class_tuple = systable_getnext(pg_class_scan);
252 * Must copy tuple before releasing buffer.
254 if (HeapTupleIsValid(pg_class_tuple))
255 pg_class_tuple = heap_copytuple(pg_class_tuple);
258 systable_endscan(pg_class_scan);
259 heap_close(pg_class_desc, AccessShareLock);
261 return pg_class_tuple;
265 * AllocateRelationDesc
267 * This is used to allocate memory for a new relation descriptor
268 * and initialize the rd_rel field.
270 * If 'relation' is NULL, allocate a new RelationData object.
271 * If not, reuse the given object (that path is taken only when
272 * we have to rebuild a relcache entry during RelationClearRelation).
275 AllocateRelationDesc(Relation relation, Form_pg_class relp)
277 MemoryContext oldcxt;
278 Form_pg_class relationForm;
280 /* Relcache entries must live in CacheMemoryContext */
281 oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
284 * allocate space for new relation descriptor, if needed
286 if (relation == NULL)
287 relation = (Relation) palloc(sizeof(RelationData));
290 * clear all fields of reldesc
292 MemSet(relation, 0, sizeof(RelationData));
293 relation->rd_targblock = InvalidBlockNumber;
295 /* make sure relation is marked as having no open file yet */
296 relation->rd_smgr = NULL;
299 * Copy the relation tuple form
301 * We only allocate space for the fixed fields, ie, CLASS_TUPLE_SIZE.
302 * The variable-length fields (relacl, reloptions) are NOT stored in the
303 * relcache --- there'd be little point in it, since we don't copy the
304 * tuple's nulls bitmap and hence wouldn't know if the values are valid.
305 * Bottom line is that relacl *cannot* be retrieved from the relcache.
306 * Get it from the syscache if you need it. The same goes for the
307 * original form of reloptions (however, we do store the parsed form
308 * of reloptions in rd_options).
310 relationForm = (Form_pg_class) palloc(CLASS_TUPLE_SIZE);
312 memcpy(relationForm, relp, CLASS_TUPLE_SIZE);
314 /* initialize relation tuple form */
315 relation->rd_rel = relationForm;
317 /* and allocate attribute tuple form storage */
318 relation->rd_att = CreateTemplateTupleDesc(relationForm->relnatts,
319 relationForm->relhasoids);
320 /* which we mark as a reference-counted tupdesc */
321 relation->rd_att->tdrefcount = 1;
323 MemoryContextSwitchTo(oldcxt);
329 * RelationParseRelOptions
330 * Convert pg_class.reloptions into pre-parsed rd_options
332 * tuple is the real pg_class tuple (not rd_rel!) for relation
334 * Note: rd_rel and (if an index) rd_am must be valid already
337 RelationParseRelOptions(Relation relation, HeapTuple tuple)
343 relation->rd_options = NULL;
345 /* Fall out if relkind should not have options */
346 switch (relation->rd_rel->relkind)
348 case RELKIND_RELATION:
349 case RELKIND_TOASTVALUE:
350 case RELKIND_UNCATALOGED:
358 * Fetch reloptions from tuple; have to use a hardwired descriptor
359 * because we might not have any other for pg_class yet (consider
360 * executing this code for pg_class itself)
362 datum = fastgetattr(tuple,
363 Anum_pg_class_reloptions,
364 GetPgClassDescriptor(),
369 /* Parse into appropriate format; don't error out here */
370 switch (relation->rd_rel->relkind)
372 case RELKIND_RELATION:
373 case RELKIND_TOASTVALUE:
374 case RELKIND_UNCATALOGED:
375 options = heap_reloptions(relation->rd_rel->relkind, datum,
379 options = index_reloptions(relation->rd_am->amoptions, datum,
383 Assert(false); /* can't get here */
384 options = NULL; /* keep compiler quiet */
388 /* Copy parsed data into CacheMemoryContext */
391 relation->rd_options = MemoryContextAlloc(CacheMemoryContext,
393 memcpy(relation->rd_options, options, VARSIZE(options));
398 * RelationBuildTupleDesc
400 * Form the relation's tuple descriptor from information in
401 * the pg_attribute, pg_attrdef & pg_constraint system catalogs.
404 RelationBuildTupleDesc(Relation relation)
406 HeapTuple pg_attribute_tuple;
407 Relation pg_attribute_desc;
408 SysScanDesc pg_attribute_scan;
412 AttrDefault *attrdef = NULL;
415 /* copy some fields from pg_class row to rd_att */
416 relation->rd_att->tdtypeid = relation->rd_rel->reltype;
417 relation->rd_att->tdtypmod = -1; /* unnecessary, but... */
418 relation->rd_att->tdhasoid = relation->rd_rel->relhasoids;
420 constr = (TupleConstr *) MemoryContextAlloc(CacheMemoryContext,
421 sizeof(TupleConstr));
422 constr->has_not_null = false;
425 * Form a scan key that selects only user attributes (attnum > 0).
426 * (Eliminating system attribute rows at the index level is lots faster
427 * than fetching them.)
429 ScanKeyInit(&skey[0],
430 Anum_pg_attribute_attrelid,
431 BTEqualStrategyNumber, F_OIDEQ,
432 ObjectIdGetDatum(RelationGetRelid(relation)));
433 ScanKeyInit(&skey[1],
434 Anum_pg_attribute_attnum,
435 BTGreaterStrategyNumber, F_INT2GT,
439 * Open pg_attribute and begin a scan. Force heap scan if we haven't yet
440 * built the critical relcache entries (this includes initdb and startup
441 * without a pg_internal.init file).
443 pg_attribute_desc = heap_open(AttributeRelationId, AccessShareLock);
444 pg_attribute_scan = systable_beginscan(pg_attribute_desc,
445 AttributeRelidNumIndexId,
446 criticalRelcachesBuilt,
451 * add attribute data to relation->rd_att
453 need = relation->rd_rel->relnatts;
455 while (HeapTupleIsValid(pg_attribute_tuple = systable_getnext(pg_attribute_scan)))
457 Form_pg_attribute attp;
459 attp = (Form_pg_attribute) GETSTRUCT(pg_attribute_tuple);
461 if (attp->attnum <= 0 ||
462 attp->attnum > relation->rd_rel->relnatts)
463 elog(ERROR, "invalid attribute number %d for %s",
464 attp->attnum, RelationGetRelationName(relation));
466 memcpy(relation->rd_att->attrs[attp->attnum - 1],
468 ATTRIBUTE_TUPLE_SIZE);
470 /* Update constraint/default info */
471 if (attp->attnotnull)
472 constr->has_not_null = true;
477 attrdef = (AttrDefault *)
478 MemoryContextAllocZero(CacheMemoryContext,
479 relation->rd_rel->relnatts *
480 sizeof(AttrDefault));
481 attrdef[ndef].adnum = attp->attnum;
482 attrdef[ndef].adbin = NULL;
491 * end the scan and close the attribute relation
493 systable_endscan(pg_attribute_scan);
494 heap_close(pg_attribute_desc, AccessShareLock);
497 elog(ERROR, "catalog is missing %d attribute(s) for relid %u",
498 need, RelationGetRelid(relation));
501 * The attcacheoff values we read from pg_attribute should all be -1
502 * ("unknown"). Verify this if assert checking is on. They will be
503 * computed when and if needed during tuple access.
505 #ifdef USE_ASSERT_CHECKING
509 for (i = 0; i < relation->rd_rel->relnatts; i++)
510 Assert(relation->rd_att->attrs[i]->attcacheoff == -1);
515 * However, we can easily set the attcacheoff value for the first
516 * attribute: it must be zero. This eliminates the need for special cases
517 * for attnum=1 that used to exist in fastgetattr() and index_getattr().
519 if (relation->rd_rel->relnatts > 0)
520 relation->rd_att->attrs[0]->attcacheoff = 0;
523 * Set up constraint/default info
525 if (constr->has_not_null || ndef > 0 || relation->rd_rel->relchecks)
527 relation->rd_att->constr = constr;
529 if (ndef > 0) /* DEFAULTs */
531 if (ndef < relation->rd_rel->relnatts)
532 constr->defval = (AttrDefault *)
533 repalloc(attrdef, ndef * sizeof(AttrDefault));
535 constr->defval = attrdef;
536 constr->num_defval = ndef;
537 AttrDefaultFetch(relation);
540 constr->num_defval = 0;
542 if (relation->rd_rel->relchecks > 0) /* CHECKs */
544 constr->num_check = relation->rd_rel->relchecks;
545 constr->check = (ConstrCheck *)
546 MemoryContextAllocZero(CacheMemoryContext,
547 constr->num_check * sizeof(ConstrCheck));
548 CheckConstraintFetch(relation);
551 constr->num_check = 0;
556 relation->rd_att->constr = NULL;
561 * RelationBuildRuleLock
563 * Form the relation's rewrite rules from information in
564 * the pg_rewrite system catalog.
566 * Note: The rule parsetrees are potentially very complex node structures.
567 * To allow these trees to be freed when the relcache entry is flushed,
568 * we make a private memory context to hold the RuleLock information for
569 * each relcache entry that has associated rules. The context is used
570 * just for rule info, not for any other subsidiary data of the relcache
571 * entry, because that keeps the update logic in RelationClearRelation()
572 * manageable. The other subsidiary data structures are simple enough
573 * to be easy to free explicitly, anyway.
576 RelationBuildRuleLock(Relation relation)
578 MemoryContext rulescxt;
579 MemoryContext oldcxt;
580 HeapTuple rewrite_tuple;
581 Relation rewrite_desc;
582 TupleDesc rewrite_tupdesc;
583 SysScanDesc rewrite_scan;
591 * Make the private context. Parameters are set on the assumption that
592 * it'll probably not contain much data.
594 rulescxt = AllocSetContextCreate(CacheMemoryContext,
595 RelationGetRelationName(relation),
596 ALLOCSET_SMALL_MINSIZE,
597 ALLOCSET_SMALL_INITSIZE,
598 ALLOCSET_SMALL_MAXSIZE);
599 relation->rd_rulescxt = rulescxt;
602 * allocate an array to hold the rewrite rules (the array is extended if
606 rules = (RewriteRule **)
607 MemoryContextAlloc(rulescxt, sizeof(RewriteRule *) * maxlocks);
614 Anum_pg_rewrite_ev_class,
615 BTEqualStrategyNumber, F_OIDEQ,
616 ObjectIdGetDatum(RelationGetRelid(relation)));
619 * open pg_rewrite and begin a scan
621 * Note: since we scan the rules using RewriteRelRulenameIndexId, we will
622 * be reading the rules in name order, except possibly during
623 * emergency-recovery operations (ie, IgnoreSystemIndexes). This in
624 * turn ensures that rules will be fired in name order.
626 rewrite_desc = heap_open(RewriteRelationId, AccessShareLock);
627 rewrite_tupdesc = RelationGetDescr(rewrite_desc);
628 rewrite_scan = systable_beginscan(rewrite_desc,
629 RewriteRelRulenameIndexId,
633 while (HeapTupleIsValid(rewrite_tuple = systable_getnext(rewrite_scan)))
635 Form_pg_rewrite rewrite_form = (Form_pg_rewrite) GETSTRUCT(rewrite_tuple);
642 rule = (RewriteRule *) MemoryContextAlloc(rulescxt,
643 sizeof(RewriteRule));
645 rule->ruleId = HeapTupleGetOid(rewrite_tuple);
647 rule->event = rewrite_form->ev_type - '0';
648 rule->attrno = rewrite_form->ev_attr;
649 rule->isInstead = rewrite_form->is_instead;
652 * Must use heap_getattr to fetch ev_action and ev_qual. Also,
653 * the rule strings are often large enough to be toasted. To avoid
654 * leaking memory in the caller's context, do the detoasting here
655 * so we can free the detoasted version.
657 rule_datum = heap_getattr(rewrite_tuple,
658 Anum_pg_rewrite_ev_action,
662 rule_text = DatumGetTextP(rule_datum);
663 rule_str = DatumGetCString(DirectFunctionCall1(textout,
664 PointerGetDatum(rule_text)));
665 oldcxt = MemoryContextSwitchTo(rulescxt);
666 rule->actions = (List *) stringToNode(rule_str);
667 MemoryContextSwitchTo(oldcxt);
669 if ((Pointer) rule_text != DatumGetPointer(rule_datum))
672 rule_datum = heap_getattr(rewrite_tuple,
673 Anum_pg_rewrite_ev_qual,
677 rule_text = DatumGetTextP(rule_datum);
678 rule_str = DatumGetCString(DirectFunctionCall1(textout,
679 PointerGetDatum(rule_text)));
680 oldcxt = MemoryContextSwitchTo(rulescxt);
681 rule->qual = (Node *) stringToNode(rule_str);
682 MemoryContextSwitchTo(oldcxt);
684 if ((Pointer) rule_text != DatumGetPointer(rule_datum))
688 * We want the rule's table references to be checked as though by the
689 * table owner, not the user referencing the rule. Therefore, scan
690 * through the rule's actions and set the checkAsUser field on all
691 * rtable entries. We have to look at the qual as well, in case it
694 * The reason for doing this when the rule is loaded, rather than
695 * when it is stored, is that otherwise ALTER TABLE OWNER would have
696 * to grovel through stored rules to update checkAsUser fields.
697 * Scanning the rule tree during load is relatively cheap (compared
698 * to constructing it in the first place), so we do it here.
700 setRuleCheckAsUser((Node *) rule->actions, relation->rd_rel->relowner);
701 setRuleCheckAsUser(rule->qual, relation->rd_rel->relowner);
703 if (numlocks >= maxlocks)
706 rules = (RewriteRule **)
707 repalloc(rules, sizeof(RewriteRule *) * maxlocks);
709 rules[numlocks++] = rule;
713 * end the scan and close the attribute relation
715 systable_endscan(rewrite_scan);
716 heap_close(rewrite_desc, AccessShareLock);
719 * form a RuleLock and insert into relation
721 rulelock = (RuleLock *) MemoryContextAlloc(rulescxt, sizeof(RuleLock));
722 rulelock->numLocks = numlocks;
723 rulelock->rules = rules;
725 relation->rd_rules = rulelock;
731 * Determine whether two RuleLocks are equivalent
733 * Probably this should be in the rules code someplace...
736 equalRuleLocks(RuleLock *rlock1, RuleLock *rlock2)
741 * As of 7.3 we assume the rule ordering is repeatable, because
742 * RelationBuildRuleLock should read 'em in a consistent order. So just
743 * compare corresponding slots.
749 if (rlock1->numLocks != rlock2->numLocks)
751 for (i = 0; i < rlock1->numLocks; i++)
753 RewriteRule *rule1 = rlock1->rules[i];
754 RewriteRule *rule2 = rlock2->rules[i];
756 if (rule1->ruleId != rule2->ruleId)
758 if (rule1->event != rule2->event)
760 if (rule1->attrno != rule2->attrno)
762 if (rule1->isInstead != rule2->isInstead)
764 if (!equal(rule1->qual, rule2->qual))
766 if (!equal(rule1->actions, rule2->actions))
770 else if (rlock2 != NULL)
776 /* ----------------------------------
779 * Build a relation descriptor --- either a new one, or by
780 * recycling the given old relation object. The latter case
781 * supports rebuilding a relcache entry without invalidating
784 * Returns NULL if no pg_class row could be found for the given relid
785 * (suggesting we are trying to access a just-deleted relation).
786 * Any other error is reported via elog.
787 * --------------------------------
790 RelationBuildDesc(Oid targetRelId, Relation oldrelation)
794 HeapTuple pg_class_tuple;
796 MemoryContext oldcxt;
799 * find the tuple in pg_class corresponding to the given relation id
801 pg_class_tuple = ScanPgRelation(targetRelId, true);
804 * if no such tuple exists, return NULL
806 if (!HeapTupleIsValid(pg_class_tuple))
810 * get information from the pg_class_tuple
812 relid = HeapTupleGetOid(pg_class_tuple);
813 relp = (Form_pg_class) GETSTRUCT(pg_class_tuple);
816 * allocate storage for the relation descriptor, and copy pg_class_tuple
817 * to relation->rd_rel.
819 relation = AllocateRelationDesc(oldrelation, relp);
822 * initialize the relation's relation id (relation->rd_id)
824 RelationGetRelid(relation) = relid;
827 * normal relations are not nailed into the cache; nor can a pre-existing
828 * relation be new. It could be temp though. (Actually, it could be new
829 * too, but it's okay to forget that fact if forced to flush the entry.)
831 relation->rd_refcnt = 0;
832 relation->rd_isnailed = false;
833 relation->rd_createSubid = InvalidSubTransactionId;
834 relation->rd_istemp = isTempNamespace(relation->rd_rel->relnamespace);
837 * initialize the tuple descriptor (relation->rd_att).
839 RelationBuildTupleDesc(relation);
842 * Fetch rules and triggers that affect this relation
844 if (relation->rd_rel->relhasrules)
845 RelationBuildRuleLock(relation);
848 relation->rd_rules = NULL;
849 relation->rd_rulescxt = NULL;
852 if (relation->rd_rel->reltriggers > 0)
853 RelationBuildTriggers(relation);
855 relation->trigdesc = NULL;
858 * if it's an index, initialize index-related information
860 if (OidIsValid(relation->rd_rel->relam))
861 RelationInitIndexAccessInfo(relation);
863 /* extract reloptions if any */
864 RelationParseRelOptions(relation, pg_class_tuple);
867 * initialize the relation lock manager information
869 RelationInitLockInfo(relation); /* see lmgr.c */
872 * initialize physical addressing information for the relation
874 RelationInitPhysicalAddr(relation);
876 /* make sure relation is marked as having no open file yet */
877 relation->rd_smgr = NULL;
880 * now we can free the memory allocated for pg_class_tuple
882 heap_freetuple(pg_class_tuple);
885 * Insert newly created relation into relcache hash tables.
887 oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
888 RelationCacheInsert(relation);
889 MemoryContextSwitchTo(oldcxt);
891 /* It's fully valid */
892 relation->rd_isvalid = true;
898 * Initialize the physical addressing info (RelFileNode) for a relcache entry
901 RelationInitPhysicalAddr(Relation relation)
903 if (relation->rd_rel->reltablespace)
904 relation->rd_node.spcNode = relation->rd_rel->reltablespace;
906 relation->rd_node.spcNode = MyDatabaseTableSpace;
907 if (relation->rd_rel->relisshared)
908 relation->rd_node.dbNode = InvalidOid;
910 relation->rd_node.dbNode = MyDatabaseId;
911 relation->rd_node.relNode = relation->rd_rel->relfilenode;
915 * Initialize index-access-method support data for an index relation
918 RelationInitIndexAccessInfo(Relation relation)
924 MemoryContext indexcxt;
925 MemoryContext oldcontext;
927 RegProcedure *support;
928 FmgrInfo *supportinfo;
934 * Make a copy of the pg_index entry for the index. Since pg_index
935 * contains variable-length and possibly-null fields, we have to do this
936 * honestly rather than just treating it as a Form_pg_index struct.
938 tuple = SearchSysCache(INDEXRELID,
939 ObjectIdGetDatum(RelationGetRelid(relation)),
941 if (!HeapTupleIsValid(tuple))
942 elog(ERROR, "cache lookup failed for index %u",
943 RelationGetRelid(relation));
944 oldcontext = MemoryContextSwitchTo(CacheMemoryContext);
945 relation->rd_indextuple = heap_copytuple(tuple);
946 relation->rd_index = (Form_pg_index) GETSTRUCT(relation->rd_indextuple);
947 MemoryContextSwitchTo(oldcontext);
948 ReleaseSysCache(tuple);
951 * indclass cannot be referenced directly through the C struct, because it
952 * is after the variable-width indkey field. Therefore we extract the
953 * datum the hard way and provide a direct link in the relcache.
955 indclassDatum = fastgetattr(relation->rd_indextuple,
956 Anum_pg_index_indclass,
957 GetPgIndexDescriptor(),
960 relation->rd_indclass = (oidvector *) DatumGetPointer(indclassDatum);
963 * Make a copy of the pg_am entry for the index's access method
965 tuple = SearchSysCache(AMOID,
966 ObjectIdGetDatum(relation->rd_rel->relam),
968 if (!HeapTupleIsValid(tuple))
969 elog(ERROR, "cache lookup failed for access method %u",
970 relation->rd_rel->relam);
971 aform = (Form_pg_am) MemoryContextAlloc(CacheMemoryContext, sizeof *aform);
972 memcpy(aform, GETSTRUCT(tuple), sizeof *aform);
973 ReleaseSysCache(tuple);
974 relation->rd_am = aform;
976 natts = relation->rd_rel->relnatts;
977 if (natts != relation->rd_index->indnatts)
978 elog(ERROR, "relnatts disagrees with indnatts for index %u",
979 RelationGetRelid(relation));
980 amstrategies = aform->amstrategies;
981 amsupport = aform->amsupport;
984 * Make the private context to hold index access info. The reason we need
985 * a context, and not just a couple of pallocs, is so that we won't leak
986 * any subsidiary info attached to fmgr lookup records.
988 * Context parameters are set on the assumption that it'll probably not
991 indexcxt = AllocSetContextCreate(CacheMemoryContext,
992 RelationGetRelationName(relation),
993 ALLOCSET_SMALL_MINSIZE,
994 ALLOCSET_SMALL_INITSIZE,
995 ALLOCSET_SMALL_MAXSIZE);
996 relation->rd_indexcxt = indexcxt;
999 * Allocate arrays to hold data
1001 relation->rd_aminfo = (RelationAmInfo *)
1002 MemoryContextAllocZero(indexcxt, sizeof(RelationAmInfo));
1004 if (amstrategies > 0)
1006 MemoryContextAllocZero(indexcxt,
1007 natts * amstrategies * sizeof(Oid));
1013 int nsupport = natts * amsupport;
1015 support = (RegProcedure *)
1016 MemoryContextAllocZero(indexcxt, nsupport * sizeof(RegProcedure));
1017 supportinfo = (FmgrInfo *)
1018 MemoryContextAllocZero(indexcxt, nsupport * sizeof(FmgrInfo));
1026 relation->rd_operator = operator;
1027 relation->rd_support = support;
1028 relation->rd_supportinfo = supportinfo;
1031 * Fill the operator and support procedure OID arrays. (aminfo and
1032 * supportinfo are left as zeroes, and are filled on-the-fly when used)
1034 IndexSupportInitialize(relation->rd_indclass,
1036 amstrategies, amsupport, natts);
1039 * expressions and predicate cache will be filled later
1041 relation->rd_indexprs = NIL;
1042 relation->rd_indpred = NIL;
1043 relation->rd_amcache = NULL;
1047 * IndexSupportInitialize
1048 * Initializes an index's cached opclass information,
1049 * given the index's pg_index.indclass entry.
1051 * Data is returned into *indexOperator and *indexSupport, which are arrays
1052 * allocated by the caller.
1054 * The caller also passes maxStrategyNumber, maxSupportNumber, and
1055 * maxAttributeNumber, since these indicate the size of the arrays
1056 * it has allocated --- but in practice these numbers must always match
1057 * those obtainable from the system catalog entries for the index and
1061 IndexSupportInitialize(oidvector *indclass,
1063 RegProcedure *indexSupport,
1064 StrategyNumber maxStrategyNumber,
1065 StrategyNumber maxSupportNumber,
1066 AttrNumber maxAttributeNumber)
1070 for (attIndex = 0; attIndex < maxAttributeNumber; attIndex++)
1072 OpClassCacheEnt *opcentry;
1074 if (!OidIsValid(indclass->values[attIndex]))
1075 elog(ERROR, "bogus pg_index tuple");
1077 /* look up the info for this opclass, using a cache */
1078 opcentry = LookupOpclassInfo(indclass->values[attIndex],
1082 /* copy cached data into relcache entry */
1083 if (maxStrategyNumber > 0)
1084 memcpy(&indexOperator[attIndex * maxStrategyNumber],
1085 opcentry->operatorOids,
1086 maxStrategyNumber * sizeof(Oid));
1087 if (maxSupportNumber > 0)
1088 memcpy(&indexSupport[attIndex * maxSupportNumber],
1089 opcentry->supportProcs,
1090 maxSupportNumber * sizeof(RegProcedure));
1097 * This routine maintains a per-opclass cache of the information needed
1098 * by IndexSupportInitialize(). This is more efficient than relying on
1099 * the catalog cache, because we can load all the info about a particular
1100 * opclass in a single indexscan of pg_amproc or pg_amop.
1102 * The information from pg_am about expected range of strategy and support
1103 * numbers is passed in, rather than being looked up, mainly because the
1104 * caller will have it already.
1106 * XXX There isn't any provision for flushing the cache. However, there
1107 * isn't any provision for flushing relcache entries when opclass info
1108 * changes, either :-(
1110 static OpClassCacheEnt *
1111 LookupOpclassInfo(Oid operatorClassOid,
1112 StrategyNumber numStrats,
1113 StrategyNumber numSupport)
1115 OpClassCacheEnt *opcentry;
1119 ScanKeyData skey[2];
1123 if (OpClassCache == NULL)
1125 /* First time through: initialize the opclass cache */
1128 if (!CacheMemoryContext)
1129 CreateCacheMemoryContext();
1131 MemSet(&ctl, 0, sizeof(ctl));
1132 ctl.keysize = sizeof(Oid);
1133 ctl.entrysize = sizeof(OpClassCacheEnt);
1134 ctl.hash = oid_hash;
1135 OpClassCache = hash_create("Operator class cache", 64,
1136 &ctl, HASH_ELEM | HASH_FUNCTION);
1139 opcentry = (OpClassCacheEnt *) hash_search(OpClassCache,
1140 (void *) &operatorClassOid,
1141 HASH_ENTER, &found);
1143 if (found && opcentry->valid)
1145 /* Already made an entry for it */
1146 Assert(numStrats == opcentry->numStrats);
1147 Assert(numSupport == opcentry->numSupport);
1151 /* Need to fill in new entry */
1152 opcentry->valid = false; /* until known OK */
1153 opcentry->numStrats = numStrats;
1154 opcentry->numSupport = numSupport;
1157 opcentry->operatorOids = (Oid *)
1158 MemoryContextAllocZero(CacheMemoryContext,
1159 numStrats * sizeof(Oid));
1161 opcentry->operatorOids = NULL;
1164 opcentry->supportProcs = (RegProcedure *)
1165 MemoryContextAllocZero(CacheMemoryContext,
1166 numSupport * sizeof(RegProcedure));
1168 opcentry->supportProcs = NULL;
1171 * To avoid infinite recursion during startup, force heap scans if we're
1172 * looking up info for the opclasses used by the indexes we would like to
1175 indexOK = criticalRelcachesBuilt ||
1176 (operatorClassOid != OID_BTREE_OPS_OID &&
1177 operatorClassOid != INT2_BTREE_OPS_OID);
1180 * Scan pg_amop to obtain operators for the opclass. We only fetch the
1181 * default ones (those with subtype zero).
1185 ScanKeyInit(&skey[0],
1186 Anum_pg_amop_amopclaid,
1187 BTEqualStrategyNumber, F_OIDEQ,
1188 ObjectIdGetDatum(operatorClassOid));
1189 ScanKeyInit(&skey[1],
1190 Anum_pg_amop_amopsubtype,
1191 BTEqualStrategyNumber, F_OIDEQ,
1192 ObjectIdGetDatum(InvalidOid));
1193 rel = heap_open(AccessMethodOperatorRelationId, AccessShareLock);
1194 scan = systable_beginscan(rel, AccessMethodStrategyIndexId, indexOK,
1195 SnapshotNow, 2, skey);
1197 while (HeapTupleIsValid(htup = systable_getnext(scan)))
1199 Form_pg_amop amopform = (Form_pg_amop) GETSTRUCT(htup);
1201 if (amopform->amopstrategy <= 0 ||
1202 (StrategyNumber) amopform->amopstrategy > numStrats)
1203 elog(ERROR, "invalid amopstrategy number %d for opclass %u",
1204 amopform->amopstrategy, operatorClassOid);
1205 opcentry->operatorOids[amopform->amopstrategy - 1] =
1209 systable_endscan(scan);
1210 heap_close(rel, AccessShareLock);
1214 * Scan pg_amproc to obtain support procs for the opclass. We only fetch
1215 * the default ones (those with subtype zero).
1219 ScanKeyInit(&skey[0],
1220 Anum_pg_amproc_amopclaid,
1221 BTEqualStrategyNumber, F_OIDEQ,
1222 ObjectIdGetDatum(operatorClassOid));
1223 ScanKeyInit(&skey[1],
1224 Anum_pg_amproc_amprocsubtype,
1225 BTEqualStrategyNumber, F_OIDEQ,
1226 ObjectIdGetDatum(InvalidOid));
1227 rel = heap_open(AccessMethodProcedureRelationId, AccessShareLock);
1228 scan = systable_beginscan(rel, AccessMethodProcedureIndexId, indexOK,
1229 SnapshotNow, 2, skey);
1231 while (HeapTupleIsValid(htup = systable_getnext(scan)))
1233 Form_pg_amproc amprocform = (Form_pg_amproc) GETSTRUCT(htup);
1235 if (amprocform->amprocnum <= 0 ||
1236 (StrategyNumber) amprocform->amprocnum > numSupport)
1237 elog(ERROR, "invalid amproc number %d for opclass %u",
1238 amprocform->amprocnum, operatorClassOid);
1240 opcentry->supportProcs[amprocform->amprocnum - 1] =
1244 systable_endscan(scan);
1245 heap_close(rel, AccessShareLock);
1248 opcentry->valid = true;
1256 * This is a special cut-down version of RelationBuildDesc()
1257 * used by RelationCacheInitializePhase2() in initializing the relcache.
1258 * The relation descriptor is built just from the supplied parameters,
1259 * without actually looking at any system table entries. We cheat
1260 * quite a lot since we only need to work for a few basic system
1263 * formrdesc is currently used for: pg_class, pg_attribute, pg_proc,
1264 * and pg_type (see RelationCacheInitializePhase2).
1266 * Note that these catalogs can't have constraints (except attnotnull),
1267 * default values, rules, or triggers, since we don't cope with any of that.
1269 * NOTE: we assume we are already switched into CacheMemoryContext.
1272 formrdesc(const char *relationName, Oid relationReltype,
1273 bool hasoids, int natts, FormData_pg_attribute *att)
1280 * allocate new relation desc, clear all fields of reldesc
1282 relation = (Relation) palloc0(sizeof(RelationData));
1283 relation->rd_targblock = InvalidBlockNumber;
1285 /* make sure relation is marked as having no open file yet */
1286 relation->rd_smgr = NULL;
1289 * initialize reference count: 1 because it is nailed in cache
1291 relation->rd_refcnt = 1;
1294 * all entries built with this routine are nailed-in-cache; none are for
1295 * new or temp relations.
1297 relation->rd_isnailed = true;
1298 relation->rd_createSubid = InvalidSubTransactionId;
1299 relation->rd_istemp = false;
1302 * initialize relation tuple form
1304 * The data we insert here is pretty incomplete/bogus, but it'll serve to
1305 * get us launched. RelationCacheInitializePhase2() will read the real
1306 * data from pg_class and replace what we've done here.
1308 relation->rd_rel = (Form_pg_class) palloc0(CLASS_TUPLE_SIZE);
1310 namestrcpy(&relation->rd_rel->relname, relationName);
1311 relation->rd_rel->relnamespace = PG_CATALOG_NAMESPACE;
1312 relation->rd_rel->reltype = relationReltype;
1315 * It's important to distinguish between shared and non-shared relations,
1316 * even at bootstrap time, to make sure we know where they are stored. At
1317 * present, all relations that formrdesc is used for are not shared.
1319 relation->rd_rel->relisshared = false;
1321 relation->rd_rel->relpages = 1;
1322 relation->rd_rel->reltuples = 1;
1323 relation->rd_rel->relkind = RELKIND_RELATION;
1324 relation->rd_rel->relhasoids = hasoids;
1325 relation->rd_rel->relnatts = (int16) natts;
1328 * initialize attribute tuple form
1330 * Unlike the case with the relation tuple, this data had better be right
1331 * because it will never be replaced. The input values must be correctly
1332 * defined by macros in src/include/catalog/ headers.
1334 relation->rd_att = CreateTemplateTupleDesc(natts, hasoids);
1335 relation->rd_att->tdrefcount = 1; /* mark as refcounted */
1337 relation->rd_att->tdtypeid = relationReltype;
1338 relation->rd_att->tdtypmod = -1; /* unnecessary, but... */
1341 * initialize tuple desc info
1343 has_not_null = false;
1344 for (i = 0; i < natts; i++)
1346 memcpy(relation->rd_att->attrs[i],
1348 ATTRIBUTE_TUPLE_SIZE);
1349 has_not_null |= att[i].attnotnull;
1350 /* make sure attcacheoff is valid */
1351 relation->rd_att->attrs[i]->attcacheoff = -1;
1354 /* initialize first attribute's attcacheoff, cf RelationBuildTupleDesc */
1355 relation->rd_att->attrs[0]->attcacheoff = 0;
1357 /* mark not-null status */
1360 TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr));
1362 constr->has_not_null = true;
1363 relation->rd_att->constr = constr;
1367 * initialize relation id from info in att array (my, this is ugly)
1369 RelationGetRelid(relation) = relation->rd_att->attrs[0]->attrelid;
1370 relation->rd_rel->relfilenode = RelationGetRelid(relation);
1373 * initialize the relation lock manager information
1375 RelationInitLockInfo(relation); /* see lmgr.c */
1378 * initialize physical addressing information for the relation
1380 RelationInitPhysicalAddr(relation);
1383 * initialize the rel-has-index flag, using hardwired knowledge
1385 if (IsBootstrapProcessingMode())
1387 /* In bootstrap mode, we have no indexes */
1388 relation->rd_rel->relhasindex = false;
1392 /* Otherwise, all the rels formrdesc is used for have indexes */
1393 relation->rd_rel->relhasindex = true;
1397 * add new reldesc to relcache
1399 RelationCacheInsert(relation);
1401 /* It's fully valid */
1402 relation->rd_isvalid = true;
1406 /* ----------------------------------------------------------------
1407 * Relation Descriptor Lookup Interface
1408 * ----------------------------------------------------------------
1412 * RelationIdGetRelation
1414 * Lookup a reldesc by OID; make one if not already in cache.
1416 * Returns NULL if no pg_class row could be found for the given relid
1417 * (suggesting we are trying to access a just-deleted relation).
1418 * Any other error is reported via elog.
1420 * NB: caller should already have at least AccessShareLock on the
1421 * relation ID, else there are nasty race conditions.
1423 * NB: relation ref count is incremented, or set to 1 if new entry.
1424 * Caller should eventually decrement count. (Usually,
1425 * that happens by calling RelationClose().)
1428 RelationIdGetRelation(Oid relationId)
1433 * first try to find reldesc in the cache
1435 RelationIdCacheLookup(relationId, rd);
1437 if (RelationIsValid(rd))
1439 RelationIncrementReferenceCount(rd);
1440 /* revalidate nailed index if necessary */
1441 if (!rd->rd_isvalid)
1442 RelationReloadClassinfo(rd);
1447 * no reldesc in the cache, so have RelationBuildDesc() build one and add
1450 rd = RelationBuildDesc(relationId, NULL);
1451 if (RelationIsValid(rd))
1452 RelationIncrementReferenceCount(rd);
1456 /* ----------------------------------------------------------------
1457 * cache invalidation support routines
1458 * ----------------------------------------------------------------
1462 * RelationIncrementReferenceCount
1463 * Increments relation reference count.
1465 * Note: bootstrap mode has its own weird ideas about relation refcount
1466 * behavior; we ought to fix it someday, but for now, just disable
1467 * reference count ownership tracking in bootstrap mode.
1470 RelationIncrementReferenceCount(Relation rel)
1472 ResourceOwnerEnlargeRelationRefs(CurrentResourceOwner);
1473 rel->rd_refcnt += 1;
1474 if (!IsBootstrapProcessingMode())
1475 ResourceOwnerRememberRelationRef(CurrentResourceOwner, rel);
1479 * RelationDecrementReferenceCount
1480 * Decrements relation reference count.
1483 RelationDecrementReferenceCount(Relation rel)
1485 Assert(rel->rd_refcnt > 0);
1486 rel->rd_refcnt -= 1;
1487 if (!IsBootstrapProcessingMode())
1488 ResourceOwnerForgetRelationRef(CurrentResourceOwner, rel);
1492 * RelationClose - close an open relation
1494 * Actually, we just decrement the refcount.
1496 * NOTE: if compiled with -DRELCACHE_FORCE_RELEASE then relcache entries
1497 * will be freed as soon as their refcount goes to zero. In combination
1498 * with aset.c's CLOBBER_FREED_MEMORY option, this provides a good test
1499 * to catch references to already-released relcache entries. It slows
1500 * things down quite a bit, however.
1503 RelationClose(Relation relation)
1505 /* Note: no locking manipulations needed */
1506 RelationDecrementReferenceCount(relation);
1508 #ifdef RELCACHE_FORCE_RELEASE
1509 if (RelationHasReferenceCountZero(relation) &&
1510 relation->rd_createSubid == InvalidSubTransactionId)
1511 RelationClearRelation(relation, false);
1516 * RelationReloadClassinfo - reload the pg_class row (only)
1518 * This function is used only for indexes. We currently allow only the
1519 * pg_class row of an existing index to change (to support changes of
1520 * owner, tablespace, or relfilenode), not its pg_index row or other
1521 * subsidiary index schema information. Therefore it's sufficient to do
1522 * this when we get an SI invalidation. Furthermore, there are cases
1523 * where it's necessary not to throw away the index information, especially
1524 * for "nailed" indexes which we are unable to rebuild on-the-fly.
1526 * We can't necessarily reread the pg_class row right away; we might be
1527 * in a failed transaction when we receive the SI notification. If so,
1528 * RelationClearRelation just marks the entry as invalid by setting
1529 * rd_isvalid to false. This routine is called to fix the entry when it
1533 RelationReloadClassinfo(Relation relation)
1536 HeapTuple pg_class_tuple;
1539 /* Should be called only for invalidated indexes */
1540 Assert(relation->rd_rel->relkind == RELKIND_INDEX &&
1541 !relation->rd_isvalid);
1542 /* Should be closed at smgr level */
1543 Assert(relation->rd_smgr == NULL);
1546 * Read the pg_class row
1548 * Don't try to use an indexscan of pg_class_oid_index to reload the info
1549 * for pg_class_oid_index ...
1551 indexOK = (RelationGetRelid(relation) != ClassOidIndexId);
1552 pg_class_tuple = ScanPgRelation(RelationGetRelid(relation), indexOK);
1553 if (!HeapTupleIsValid(pg_class_tuple))
1554 elog(ERROR, "could not find pg_class tuple for index %u",
1555 RelationGetRelid(relation));
1556 relp = (Form_pg_class) GETSTRUCT(pg_class_tuple);
1557 memcpy(relation->rd_rel, relp, CLASS_TUPLE_SIZE);
1558 /* Reload reloptions in case they changed */
1559 if (relation->rd_options)
1560 pfree(relation->rd_options);
1561 RelationParseRelOptions(relation, pg_class_tuple);
1562 /* done with pg_class tuple */
1563 heap_freetuple(pg_class_tuple);
1564 /* We must recalculate physical address in case it changed */
1565 RelationInitPhysicalAddr(relation);
1566 /* Make sure targblock is reset in case rel was truncated */
1567 relation->rd_targblock = InvalidBlockNumber;
1568 /* Must free any AM cached data, too */
1569 if (relation->rd_amcache)
1570 pfree(relation->rd_amcache);
1571 relation->rd_amcache = NULL;
1572 /* Okay, now it's valid again */
1573 relation->rd_isvalid = true;
1577 * RelationClearRelation
1579 * Physically blow away a relation cache entry, or reset it and rebuild
1580 * it from scratch (that is, from catalog entries). The latter path is
1581 * usually used when we are notified of a change to an open relation
1582 * (one with refcount > 0). However, this routine just does whichever
1583 * it's told to do; callers must determine which they want.
1586 RelationClearRelation(Relation relation, bool rebuild)
1588 Oid old_reltype = relation->rd_rel->reltype;
1589 MemoryContext oldcxt;
1592 * Make sure smgr and lower levels close the relation's files, if they
1593 * weren't closed already. If the relation is not getting deleted, the
1594 * next smgr access should reopen the files automatically. This ensures
1595 * that the low-level file access state is updated after, say, a vacuum
1598 RelationCloseSmgr(relation);
1601 * Never, never ever blow away a nailed-in system relation, because we'd
1602 * be unable to recover. However, we must reset rd_targblock, in case we
1603 * got called because of a relation cache flush that was triggered by
1606 * If it's a nailed index, then we need to re-read the pg_class row to see
1607 * if its relfilenode changed. We can't necessarily do that here, because
1608 * we might be in a failed transaction. We assume it's okay to do it if
1609 * there are open references to the relcache entry (cf notes for
1610 * AtEOXact_RelationCache). Otherwise just mark the entry as possibly
1611 * invalid, and it'll be fixed when next opened.
1613 if (relation->rd_isnailed)
1615 relation->rd_targblock = InvalidBlockNumber;
1616 if (relation->rd_rel->relkind == RELKIND_INDEX)
1618 relation->rd_isvalid = false; /* needs to be revalidated */
1619 if (relation->rd_refcnt > 1)
1620 RelationReloadClassinfo(relation);
1626 * Even non-system indexes should not be blown away if they are open and
1627 * have valid index support information. This avoids problems with active
1628 * use of the index support information. As with nailed indexes, we
1629 * re-read the pg_class row to handle possible physical relocation of
1632 if (relation->rd_rel->relkind == RELKIND_INDEX &&
1633 relation->rd_refcnt > 0 &&
1634 relation->rd_indexcxt != NULL)
1636 relation->rd_isvalid = false; /* needs to be revalidated */
1637 RelationReloadClassinfo(relation);
1642 * Remove relation from hash tables
1644 * Note: we might be reinserting it momentarily, but we must not have it
1645 * visible in the hash tables until it's valid again, so don't try to
1646 * optimize this away...
1648 oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
1649 RelationCacheDelete(relation);
1650 MemoryContextSwitchTo(oldcxt);
1652 /* Clear out catcache's entries for this relation */
1653 CatalogCacheFlushRelation(RelationGetRelid(relation));
1656 * Free all the subsidiary data structures of the relcache entry. We
1657 * cannot free rd_att if we are trying to rebuild the entry, however,
1658 * because pointers to it may be cached in various places. The rule
1659 * manager might also have pointers into the rewrite rules. So to begin
1660 * with, we can only get rid of these fields:
1662 FreeTriggerDesc(relation->trigdesc);
1663 if (relation->rd_indextuple)
1664 pfree(relation->rd_indextuple);
1665 if (relation->rd_am)
1666 pfree(relation->rd_am);
1667 if (relation->rd_rel)
1668 pfree(relation->rd_rel);
1669 if (relation->rd_options)
1670 pfree(relation->rd_options);
1671 list_free(relation->rd_indexlist);
1672 if (relation->rd_indexcxt)
1673 MemoryContextDelete(relation->rd_indexcxt);
1676 * If we're really done with the relcache entry, blow it away. But if
1677 * someone is still using it, reconstruct the whole deal without moving
1678 * the physical RelationData record (so that the someone's pointer is
1683 /* ok to zap remaining substructure */
1684 flush_rowtype_cache(old_reltype);
1685 /* can't use DecrTupleDescRefCount here */
1686 Assert(relation->rd_att->tdrefcount > 0);
1687 if (--relation->rd_att->tdrefcount == 0)
1688 FreeTupleDesc(relation->rd_att);
1689 if (relation->rd_rulescxt)
1690 MemoryContextDelete(relation->rd_rulescxt);
1696 * When rebuilding an open relcache entry, must preserve ref count and
1697 * rd_createSubid state. Also attempt to preserve the tupledesc and
1698 * rewrite-rule substructures in place. (Note: the refcount mechanism
1699 * for tupledescs may eventually ensure that we don't really need to
1700 * preserve the tupledesc in-place, but for now there are still a lot
1701 * of places that assume an open rel's tupledesc won't move.)
1703 * Note that this process does not touch CurrentResourceOwner; which
1704 * is good because whatever ref counts the entry may have do not
1705 * necessarily belong to that resource owner.
1707 Oid save_relid = RelationGetRelid(relation);
1708 int old_refcnt = relation->rd_refcnt;
1709 SubTransactionId old_createSubid = relation->rd_createSubid;
1710 TupleDesc old_att = relation->rd_att;
1711 RuleLock *old_rules = relation->rd_rules;
1712 MemoryContext old_rulescxt = relation->rd_rulescxt;
1714 if (RelationBuildDesc(save_relid, relation) != relation)
1716 /* Should only get here if relation was deleted */
1717 flush_rowtype_cache(old_reltype);
1718 Assert(old_att->tdrefcount > 0);
1719 if (--old_att->tdrefcount == 0)
1720 FreeTupleDesc(old_att);
1722 MemoryContextDelete(old_rulescxt);
1724 elog(ERROR, "relation %u deleted while still in use", save_relid);
1726 relation->rd_refcnt = old_refcnt;
1727 relation->rd_createSubid = old_createSubid;
1728 if (equalTupleDescs(old_att, relation->rd_att))
1730 /* needn't flush typcache here */
1731 Assert(relation->rd_att->tdrefcount == 1);
1732 if (--relation->rd_att->tdrefcount == 0)
1733 FreeTupleDesc(relation->rd_att);
1734 relation->rd_att = old_att;
1738 flush_rowtype_cache(old_reltype);
1739 Assert(old_att->tdrefcount > 0);
1740 if (--old_att->tdrefcount == 0)
1741 FreeTupleDesc(old_att);
1743 if (equalRuleLocks(old_rules, relation->rd_rules))
1745 if (relation->rd_rulescxt)
1746 MemoryContextDelete(relation->rd_rulescxt);
1747 relation->rd_rules = old_rules;
1748 relation->rd_rulescxt = old_rulescxt;
1753 MemoryContextDelete(old_rulescxt);
1759 * RelationFlushRelation
1761 * Rebuild the relation if it is open (refcount > 0), else blow it away.
1764 RelationFlushRelation(Relation relation)
1768 if (relation->rd_createSubid != InvalidSubTransactionId)
1771 * New relcache entries are always rebuilt, not flushed; else we'd
1772 * forget the "new" status of the relation, which is a useful
1773 * optimization to have.
1780 * Pre-existing rels can be dropped from the relcache if not open.
1782 rebuild = !RelationHasReferenceCountZero(relation);
1785 RelationClearRelation(relation, rebuild);
1789 * RelationForgetRelation - unconditionally remove a relcache entry
1791 * External interface for destroying a relcache entry when we
1792 * drop the relation.
1795 RelationForgetRelation(Oid rid)
1799 RelationIdCacheLookup(rid, relation);
1801 if (!PointerIsValid(relation))
1802 return; /* not in cache, nothing to do */
1804 if (!RelationHasReferenceCountZero(relation))
1805 elog(ERROR, "relation %u is still open", rid);
1807 /* Unconditionally destroy the relcache entry */
1808 RelationClearRelation(relation, false);
1812 * RelationCacheInvalidateEntry
1814 * This routine is invoked for SI cache flush messages.
1816 * Any relcache entry matching the relid must be flushed. (Note: caller has
1817 * already determined that the relid belongs to our database or is a shared
1820 * We used to skip local relations, on the grounds that they could
1821 * not be targets of cross-backend SI update messages; but it seems
1822 * safer to process them, so that our *own* SI update messages will
1823 * have the same effects during CommandCounterIncrement for both
1824 * local and nonlocal relations.
1827 RelationCacheInvalidateEntry(Oid relationId)
1831 RelationIdCacheLookup(relationId, relation);
1833 if (PointerIsValid(relation))
1835 relcacheInvalsReceived++;
1836 RelationFlushRelation(relation);
1841 * RelationCacheInvalidate
1842 * Blow away cached relation descriptors that have zero reference counts,
1843 * and rebuild those with positive reference counts. Also reset the smgr
1846 * This is currently used only to recover from SI message buffer overflow,
1847 * so we do not touch new-in-transaction relations; they cannot be targets
1848 * of cross-backend SI updates (and our own updates now go through a
1849 * separate linked list that isn't limited by the SI message buffer size).
1851 * We do this in two phases: the first pass deletes deletable items, and
1852 * the second one rebuilds the rebuildable items. This is essential for
1853 * safety, because hash_seq_search only copes with concurrent deletion of
1854 * the element it is currently visiting. If a second SI overflow were to
1855 * occur while we are walking the table, resulting in recursive entry to
1856 * this routine, we could crash because the inner invocation blows away
1857 * the entry next to be visited by the outer scan. But this way is OK,
1858 * because (a) during the first pass we won't process any more SI messages,
1859 * so hash_seq_search will complete safely; (b) during the second pass we
1860 * only hold onto pointers to nondeletable entries.
1862 * The two-phase approach also makes it easy to ensure that we process
1863 * nailed-in-cache indexes before other nondeletable items, and that we
1864 * process pg_class_oid_index first of all. In scenarios where a nailed
1865 * index has been given a new relfilenode, we have to detect that update
1866 * before the nailed index is used in reloading any other relcache entry.
1869 RelationCacheInvalidate(void)
1871 HASH_SEQ_STATUS status;
1872 RelIdCacheEnt *idhentry;
1874 List *rebuildFirstList = NIL;
1875 List *rebuildList = NIL;
1879 hash_seq_init(&status, RelationIdCache);
1881 while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
1883 relation = idhentry->reldesc;
1885 /* Must close all smgr references to avoid leaving dangling ptrs */
1886 RelationCloseSmgr(relation);
1888 /* Ignore new relations, since they are never SI targets */
1889 if (relation->rd_createSubid != InvalidSubTransactionId)
1892 relcacheInvalsReceived++;
1894 if (RelationHasReferenceCountZero(relation))
1896 /* Delete this entry immediately */
1897 Assert(!relation->rd_isnailed);
1898 RelationClearRelation(relation, false);
1903 * Add this entry to list of stuff to rebuild in second pass.
1904 * pg_class_oid_index goes on the front of rebuildFirstList, other
1905 * nailed indexes on the back, and everything else into
1906 * rebuildList (in no particular order).
1908 if (relation->rd_isnailed &&
1909 relation->rd_rel->relkind == RELKIND_INDEX)
1911 if (RelationGetRelid(relation) == ClassOidIndexId)
1912 rebuildFirstList = lcons(relation, rebuildFirstList);
1914 rebuildFirstList = lappend(rebuildFirstList, relation);
1917 rebuildList = lcons(relation, rebuildList);
1922 * Now zap any remaining smgr cache entries. This must happen before we
1923 * start to rebuild entries, since that may involve catalog fetches which
1924 * will re-open catalog files.
1928 /* Phase 2: rebuild the items found to need rebuild in phase 1 */
1929 foreach(l, rebuildFirstList)
1931 relation = (Relation) lfirst(l);
1932 RelationClearRelation(relation, true);
1934 list_free(rebuildFirstList);
1935 foreach(l, rebuildList)
1937 relation = (Relation) lfirst(l);
1938 RelationClearRelation(relation, true);
1940 list_free(rebuildList);
1944 * AtEOXact_RelationCache
1946 * Clean up the relcache at main-transaction commit or abort.
1948 * Note: this must be called *before* processing invalidation messages.
1949 * In the case of abort, we don't want to try to rebuild any invalidated
1950 * cache entries (since we can't safely do database accesses). Therefore
1951 * we must reset refcnts before handling pending invalidations.
1953 * As of PostgreSQL 8.1, relcache refcnts should get released by the
1954 * ResourceOwner mechanism. This routine just does a debugging
1955 * cross-check that no pins remain. However, we also need to do special
1956 * cleanup when the current transaction created any relations or made use
1957 * of forced index lists.
1960 AtEOXact_RelationCache(bool isCommit)
1962 HASH_SEQ_STATUS status;
1963 RelIdCacheEnt *idhentry;
1966 * To speed up transaction exit, we want to avoid scanning the relcache
1967 * unless there is actually something for this routine to do. Other than
1968 * the debug-only Assert checks, most transactions don't create any work
1969 * for us to do here, so we keep a static flag that gets set if there is
1970 * anything to do. (Currently, this means either a relation is created in
1971 * the current xact, or an index list is forced.) For simplicity, the
1972 * flag remains set till end of top-level transaction, even though we
1973 * could clear it at subtransaction end in some cases.
1975 if (!need_eoxact_work
1976 #ifdef USE_ASSERT_CHECKING
1982 hash_seq_init(&status, RelationIdCache);
1984 while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
1986 Relation relation = idhentry->reldesc;
1989 * The relcache entry's ref count should be back to its normal
1990 * not-in-a-transaction state: 0 unless it's nailed in cache.
1992 * In bootstrap mode, this is NOT true, so don't check it --- the
1993 * bootstrap code expects relations to stay open across start/commit
1994 * transaction calls. (That seems bogus, but it's not worth fixing.)
1996 #ifdef USE_ASSERT_CHECKING
1997 if (!IsBootstrapProcessingMode())
1999 int expected_refcnt;
2001 expected_refcnt = relation->rd_isnailed ? 1 : 0;
2002 Assert(relation->rd_refcnt == expected_refcnt);
2007 * Is it a relation created in the current transaction?
2009 * During commit, reset the flag to zero, since we are now out of the
2010 * creating transaction. During abort, simply delete the relcache
2011 * entry --- it isn't interesting any longer. (NOTE: if we have
2012 * forgotten the new-ness of a new relation due to a forced cache
2013 * flush, the entry will get deleted anyway by shared-cache-inval
2014 * processing of the aborted pg_class insertion.)
2016 if (relation->rd_createSubid != InvalidSubTransactionId)
2019 relation->rd_createSubid = InvalidSubTransactionId;
2022 RelationClearRelation(relation, false);
2028 * Flush any temporary index list.
2030 if (relation->rd_indexvalid == 2)
2032 list_free(relation->rd_indexlist);
2033 relation->rd_indexlist = NIL;
2034 relation->rd_oidindex = InvalidOid;
2035 relation->rd_indexvalid = 0;
2039 /* Once done with the transaction, we can reset need_eoxact_work */
2040 need_eoxact_work = false;
2044 * AtEOSubXact_RelationCache
2046 * Clean up the relcache at sub-transaction commit or abort.
2048 * Note: this must be called *before* processing invalidation messages.
2051 AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid,
2052 SubTransactionId parentSubid)
2054 HASH_SEQ_STATUS status;
2055 RelIdCacheEnt *idhentry;
2058 * Skip the relcache scan if nothing to do --- see notes for
2059 * AtEOXact_RelationCache.
2061 if (!need_eoxact_work)
2064 hash_seq_init(&status, RelationIdCache);
2066 while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
2068 Relation relation = idhentry->reldesc;
2071 * Is it a relation created in the current subtransaction?
2073 * During subcommit, mark it as belonging to the parent, instead.
2074 * During subabort, simply delete the relcache entry.
2076 if (relation->rd_createSubid == mySubid)
2079 relation->rd_createSubid = parentSubid;
2082 Assert(RelationHasReferenceCountZero(relation));
2083 RelationClearRelation(relation, false);
2089 * Flush any temporary index list.
2091 if (relation->rd_indexvalid == 2)
2093 list_free(relation->rd_indexlist);
2094 relation->rd_indexlist = NIL;
2095 relation->rd_oidindex = InvalidOid;
2096 relation->rd_indexvalid = 0;
2102 * RelationBuildLocalRelation
2103 * Build a relcache entry for an about-to-be-created relation,
2104 * and enter it into the relcache.
2107 RelationBuildLocalRelation(const char *relname,
2112 bool shared_relation)
2115 MemoryContext oldcxt;
2116 int natts = tupDesc->natts;
2121 AssertArg(natts >= 0);
2124 * check for creation of a rel that must be nailed in cache.
2126 * XXX this list had better match RelationCacheInitializePhase2's list.
2130 case RelationRelationId:
2131 case AttributeRelationId:
2132 case ProcedureRelationId:
2133 case TypeRelationId:
2142 * check that hardwired list of shared rels matches what's in the
2143 * bootstrap .bki file. If you get a failure here during initdb,
2144 * you probably need to fix IsSharedRelation() to match whatever
2145 * you've done to the set of shared relations.
2147 if (shared_relation != IsSharedRelation(relid))
2148 elog(ERROR, "shared_relation flag for \"%s\" does not match IsSharedRelation(%u)",
2152 * switch to the cache context to create the relcache entry.
2154 if (!CacheMemoryContext)
2155 CreateCacheMemoryContext();
2157 oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
2160 * allocate a new relation descriptor and fill in basic state fields.
2162 rel = (Relation) palloc0(sizeof(RelationData));
2164 rel->rd_targblock = InvalidBlockNumber;
2166 /* make sure relation is marked as having no open file yet */
2167 rel->rd_smgr = NULL;
2169 /* mark it nailed if appropriate */
2170 rel->rd_isnailed = nailit;
2172 rel->rd_refcnt = nailit ? 1 : 0;
2174 /* it's being created in this transaction */
2175 rel->rd_createSubid = GetCurrentSubTransactionId();
2177 /* must flag that we have rels created in this transaction */
2178 need_eoxact_work = true;
2180 /* is it a temporary relation? */
2181 rel->rd_istemp = isTempNamespace(relnamespace);
2184 * create a new tuple descriptor from the one passed in. We do this
2185 * partly to copy it into the cache context, and partly because the new
2186 * relation can't have any defaults or constraints yet; they have to be
2187 * added in later steps, because they require additions to multiple system
2188 * catalogs. We can copy attnotnull constraints here, however.
2190 rel->rd_att = CreateTupleDescCopy(tupDesc);
2191 rel->rd_att->tdrefcount = 1; /* mark as refcounted */
2192 has_not_null = false;
2193 for (i = 0; i < natts; i++)
2195 rel->rd_att->attrs[i]->attnotnull = tupDesc->attrs[i]->attnotnull;
2196 has_not_null |= tupDesc->attrs[i]->attnotnull;
2201 TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr));
2203 constr->has_not_null = true;
2204 rel->rd_att->constr = constr;
2208 * initialize relation tuple form (caller may add/override data later)
2210 rel->rd_rel = (Form_pg_class) palloc0(CLASS_TUPLE_SIZE);
2212 namestrcpy(&rel->rd_rel->relname, relname);
2213 rel->rd_rel->relnamespace = relnamespace;
2215 rel->rd_rel->relkind = RELKIND_UNCATALOGED;
2216 rel->rd_rel->relhasoids = rel->rd_att->tdhasoid;
2217 rel->rd_rel->relnatts = natts;
2218 rel->rd_rel->reltype = InvalidOid;
2219 /* needed when bootstrapping: */
2220 rel->rd_rel->relowner = BOOTSTRAP_SUPERUSERID;
2223 * Insert relation physical and logical identifiers (OIDs) into the right
2224 * places. Note that the physical ID (relfilenode) is initially the same
2225 * as the logical ID (OID).
2227 rel->rd_rel->relisshared = shared_relation;
2229 RelationGetRelid(rel) = relid;
2231 for (i = 0; i < natts; i++)
2232 rel->rd_att->attrs[i]->attrelid = relid;
2234 rel->rd_rel->relfilenode = relid;
2235 rel->rd_rel->reltablespace = reltablespace;
2237 RelationInitLockInfo(rel); /* see lmgr.c */
2239 RelationInitPhysicalAddr(rel);
2242 * Okay to insert into the relcache hash tables.
2244 RelationCacheInsert(rel);
2247 * done building relcache entry.
2249 MemoryContextSwitchTo(oldcxt);
2251 /* It's fully valid */
2252 rel->rd_isvalid = true;
2255 * Caller expects us to pin the returned entry.
2257 RelationIncrementReferenceCount(rel);
2263 * RelationCacheInitialize
2265 * This initializes the relation descriptor cache. At the time
2266 * that this is invoked, we can't do database access yet (mainly
2267 * because the transaction subsystem is not up); all we are doing
2268 * is making an empty cache hashtable. This must be done before
2269 * starting the initialization transaction, because otherwise
2270 * AtEOXact_RelationCache would crash if that transaction aborts
2271 * before we can get the relcache set up.
2274 #define INITRELCACHESIZE 400
2277 RelationCacheInitialize(void)
2279 MemoryContext oldcxt;
2283 * switch to cache memory context
2285 if (!CacheMemoryContext)
2286 CreateCacheMemoryContext();
2288 oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
2291 * create hashtable that indexes the relcache
2293 MemSet(&ctl, 0, sizeof(ctl));
2294 ctl.keysize = sizeof(Oid);
2295 ctl.entrysize = sizeof(RelIdCacheEnt);
2296 ctl.hash = oid_hash;
2297 RelationIdCache = hash_create("Relcache by OID", INITRELCACHESIZE,
2298 &ctl, HASH_ELEM | HASH_FUNCTION);
2300 MemoryContextSwitchTo(oldcxt);
2304 * RelationCacheInitializePhase2
2306 * This is called as soon as the catcache and transaction system
2307 * are functional. At this point we can actually read data from
2308 * the system catalogs. We first try to read pre-computed relcache
2309 * entries from the pg_internal.init file. If that's missing or
2310 * broken, make phony entries for the minimum set of nailed-in-cache
2311 * relations. Then (unless bootstrapping) make sure we have entries
2312 * for the critical system indexes. Once we've done all this, we
2313 * have enough infrastructure to open any system catalog or use any
2314 * catcache. The last step is to rewrite pg_internal.init if needed.
2317 RelationCacheInitializePhase2(void)
2319 HASH_SEQ_STATUS status;
2320 RelIdCacheEnt *idhentry;
2321 MemoryContext oldcxt;
2322 bool needNewCacheFile = false;
2325 * switch to cache memory context
2327 oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
2330 * Try to load the relcache cache file. If unsuccessful, bootstrap the
2331 * cache with pre-made descriptors for the critical "nailed-in" system
2334 if (IsBootstrapProcessingMode() ||
2335 !load_relcache_init_file())
2337 needNewCacheFile = true;
2339 formrdesc("pg_class", PG_CLASS_RELTYPE_OID,
2340 true, Natts_pg_class, Desc_pg_class);
2341 formrdesc("pg_attribute", PG_ATTRIBUTE_RELTYPE_OID,
2342 false, Natts_pg_attribute, Desc_pg_attribute);
2343 formrdesc("pg_proc", PG_PROC_RELTYPE_OID,
2344 true, Natts_pg_proc, Desc_pg_proc);
2345 formrdesc("pg_type", PG_TYPE_RELTYPE_OID,
2346 true, Natts_pg_type, Desc_pg_type);
2348 #define NUM_CRITICAL_RELS 4 /* fix if you change list above */
2351 MemoryContextSwitchTo(oldcxt);
2353 /* In bootstrap mode, the faked-up formrdesc info is all we'll have */
2354 if (IsBootstrapProcessingMode())
2358 * If we didn't get the critical system indexes loaded into relcache, do
2359 * so now. These are critical because the catcache depends on them for
2360 * catcache fetches that are done during relcache load. Thus, we have an
2361 * infinite-recursion problem. We can break the recursion by doing
2362 * heapscans instead of indexscans at certain key spots. To avoid hobbling
2363 * performance, we only want to do that until we have the critical indexes
2364 * loaded into relcache. Thus, the flag criticalRelcachesBuilt is used to
2365 * decide whether to do heapscan or indexscan at the key spots, and we set
2366 * it true after we've loaded the critical indexes.
2368 * The critical indexes are marked as "nailed in cache", partly to make it
2369 * easy for load_relcache_init_file to count them, but mainly because we
2370 * cannot flush and rebuild them once we've set criticalRelcachesBuilt to
2371 * true. (NOTE: perhaps it would be possible to reload them by
2372 * temporarily setting criticalRelcachesBuilt to false again. For now,
2373 * though, we just nail 'em in.)
2375 * RewriteRelRulenameIndexId and TriggerRelidNameIndexId are not critical
2376 * in the same way as the others, because the critical catalogs don't
2377 * (currently) have any rules or triggers, and so these indexes can be
2378 * rebuilt without inducing recursion. However they are used during
2379 * relcache load when a rel does have rules or triggers, so we choose to
2380 * nail them for performance reasons.
2382 if (!criticalRelcachesBuilt)
2386 #define LOAD_CRIT_INDEX(indexoid) \
2388 ird = RelationBuildDesc((indexoid), NULL); \
2389 ird->rd_isnailed = true; \
2390 ird->rd_refcnt = 1; \
2393 LOAD_CRIT_INDEX(ClassOidIndexId);
2394 LOAD_CRIT_INDEX(AttributeRelidNumIndexId);
2395 LOAD_CRIT_INDEX(IndexRelidIndexId);
2396 LOAD_CRIT_INDEX(AccessMethodStrategyIndexId);
2397 LOAD_CRIT_INDEX(AccessMethodProcedureIndexId);
2398 LOAD_CRIT_INDEX(OperatorOidIndexId);
2399 LOAD_CRIT_INDEX(RewriteRelRulenameIndexId);
2400 LOAD_CRIT_INDEX(TriggerRelidNameIndexId);
2402 #define NUM_CRITICAL_INDEXES 8 /* fix if you change list above */
2404 criticalRelcachesBuilt = true;
2408 * Now, scan all the relcache entries and update anything that might be
2409 * wrong in the results from formrdesc or the relcache cache file. If we
2410 * faked up relcache entries using formrdesc, then read the real pg_class
2411 * rows and replace the fake entries with them. Also, if any of the
2412 * relcache entries have rules or triggers, load that info the hard way
2413 * since it isn't recorded in the cache file.
2415 hash_seq_init(&status, RelationIdCache);
2417 while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
2419 Relation relation = idhentry->reldesc;
2422 * If it's a faked-up entry, read the real pg_class tuple.
2424 if (needNewCacheFile && relation->rd_isnailed)
2429 htup = SearchSysCache(RELOID,
2430 ObjectIdGetDatum(RelationGetRelid(relation)),
2432 if (!HeapTupleIsValid(htup))
2433 elog(FATAL, "cache lookup failed for relation %u",
2434 RelationGetRelid(relation));
2435 relp = (Form_pg_class) GETSTRUCT(htup);
2438 * Copy tuple to relation->rd_rel. (See notes in
2439 * AllocateRelationDesc())
2441 Assert(relation->rd_rel != NULL);
2442 memcpy((char *) relation->rd_rel, (char *) relp, CLASS_TUPLE_SIZE);
2444 /* Update rd_options while we have the tuple */
2445 if (relation->rd_options)
2446 pfree(relation->rd_options);
2447 RelationParseRelOptions(relation, htup);
2450 * Also update the derived fields in rd_att.
2452 relation->rd_att->tdtypeid = relp->reltype;
2453 relation->rd_att->tdtypmod = -1; /* unnecessary, but... */
2454 relation->rd_att->tdhasoid = relp->relhasoids;
2456 ReleaseSysCache(htup);
2460 * Fix data that isn't saved in relcache cache file.
2462 if (relation->rd_rel->relhasrules && relation->rd_rules == NULL)
2463 RelationBuildRuleLock(relation);
2464 if (relation->rd_rel->reltriggers > 0 && relation->trigdesc == NULL)
2465 RelationBuildTriggers(relation);
2469 * Lastly, write out a new relcache cache file if one is needed.
2471 if (needNewCacheFile)
2474 * Force all the catcaches to finish initializing and thereby open the
2475 * catalogs and indexes they use. This will preload the relcache with
2476 * entries for all the most important system catalogs and indexes, so
2477 * that the init file will be most useful for future backends.
2479 InitCatalogCachePhase2();
2481 /* now write the file */
2482 write_relcache_init_file();
2487 * GetPgClassDescriptor -- get a predefined tuple descriptor for pg_class
2488 * GetPgIndexDescriptor -- get a predefined tuple descriptor for pg_index
2490 * We need this kluge because we have to be able to access non-fixed-width
2491 * fields of pg_class and pg_index before we have the standard catalog caches
2492 * available. We use predefined data that's set up in just the same way as
2493 * the bootstrapped reldescs used by formrdesc(). The resulting tupdesc is
2494 * not 100% kosher: it does not have the correct rowtype OID in tdtypeid, nor
2495 * does it have a TupleConstr field. But it's good enough for the purpose of
2496 * extracting fields.
2499 BuildHardcodedDescriptor(int natts, Form_pg_attribute attrs, bool hasoids)
2502 MemoryContext oldcxt;
2505 oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
2507 result = CreateTemplateTupleDesc(natts, hasoids);
2508 result->tdtypeid = RECORDOID; /* not right, but we don't care */
2509 result->tdtypmod = -1;
2511 for (i = 0; i < natts; i++)
2513 memcpy(result->attrs[i], &attrs[i], ATTRIBUTE_TUPLE_SIZE);
2514 /* make sure attcacheoff is valid */
2515 result->attrs[i]->attcacheoff = -1;
2518 /* initialize first attribute's attcacheoff, cf RelationBuildTupleDesc */
2519 result->attrs[0]->attcacheoff = 0;
2521 /* Note: we don't bother to set up a TupleConstr entry */
2523 MemoryContextSwitchTo(oldcxt);
2529 GetPgClassDescriptor(void)
2531 static TupleDesc pgclassdesc = NULL;
2534 if (pgclassdesc == NULL)
2535 pgclassdesc = BuildHardcodedDescriptor(Natts_pg_class,
2543 GetPgIndexDescriptor(void)
2545 static TupleDesc pgindexdesc = NULL;
2548 if (pgindexdesc == NULL)
2549 pgindexdesc = BuildHardcodedDescriptor(Natts_pg_index,
2557 AttrDefaultFetch(Relation relation)
2559 AttrDefault *attrdef = relation->rd_att->constr->defval;
2560 int ndef = relation->rd_att->constr->num_defval;
2571 Anum_pg_attrdef_adrelid,
2572 BTEqualStrategyNumber, F_OIDEQ,
2573 ObjectIdGetDatum(RelationGetRelid(relation)));
2575 adrel = heap_open(AttrDefaultRelationId, AccessShareLock);
2576 adscan = systable_beginscan(adrel, AttrDefaultIndexId, true,
2577 SnapshotNow, 1, &skey);
2580 while (HeapTupleIsValid(htup = systable_getnext(adscan)))
2582 Form_pg_attrdef adform = (Form_pg_attrdef) GETSTRUCT(htup);
2584 for (i = 0; i < ndef; i++)
2586 if (adform->adnum != attrdef[i].adnum)
2588 if (attrdef[i].adbin != NULL)
2589 elog(WARNING, "multiple attrdef records found for attr %s of rel %s",
2590 NameStr(relation->rd_att->attrs[adform->adnum - 1]->attname),
2591 RelationGetRelationName(relation));
2595 val = fastgetattr(htup,
2596 Anum_pg_attrdef_adbin,
2597 adrel->rd_att, &isnull);
2599 elog(WARNING, "null adbin for attr %s of rel %s",
2600 NameStr(relation->rd_att->attrs[adform->adnum - 1]->attname),
2601 RelationGetRelationName(relation));
2603 attrdef[i].adbin = MemoryContextStrdup(CacheMemoryContext,
2604 DatumGetCString(DirectFunctionCall1(textout,
2610 elog(WARNING, "unexpected attrdef record found for attr %d of rel %s",
2611 adform->adnum, RelationGetRelationName(relation));
2614 systable_endscan(adscan);
2615 heap_close(adrel, AccessShareLock);
2618 elog(WARNING, "%d attrdef record(s) missing for rel %s",
2619 ndef - found, RelationGetRelationName(relation));
2623 CheckConstraintFetch(Relation relation)
2625 ConstrCheck *check = relation->rd_att->constr->check;
2626 int ncheck = relation->rd_att->constr->num_check;
2628 SysScanDesc conscan;
2629 ScanKeyData skey[1];
2635 ScanKeyInit(&skey[0],
2636 Anum_pg_constraint_conrelid,
2637 BTEqualStrategyNumber, F_OIDEQ,
2638 ObjectIdGetDatum(RelationGetRelid(relation)));
2640 conrel = heap_open(ConstraintRelationId, AccessShareLock);
2641 conscan = systable_beginscan(conrel, ConstraintRelidIndexId, true,
2642 SnapshotNow, 1, skey);
2644 while (HeapTupleIsValid(htup = systable_getnext(conscan)))
2646 Form_pg_constraint conform = (Form_pg_constraint) GETSTRUCT(htup);
2648 /* We want check constraints only */
2649 if (conform->contype != CONSTRAINT_CHECK)
2652 if (found >= ncheck)
2653 elog(ERROR, "unexpected constraint record found for rel %s",
2654 RelationGetRelationName(relation));
2656 check[found].ccname = MemoryContextStrdup(CacheMemoryContext,
2657 NameStr(conform->conname));
2659 /* Grab and test conbin is actually set */
2660 val = fastgetattr(htup,
2661 Anum_pg_constraint_conbin,
2662 conrel->rd_att, &isnull);
2664 elog(ERROR, "null conbin for rel %s",
2665 RelationGetRelationName(relation));
2667 check[found].ccbin = MemoryContextStrdup(CacheMemoryContext,
2668 DatumGetCString(DirectFunctionCall1(textout,
2673 systable_endscan(conscan);
2674 heap_close(conrel, AccessShareLock);
2676 if (found != ncheck)
2677 elog(ERROR, "%d constraint record(s) missing for rel %s",
2678 ncheck - found, RelationGetRelationName(relation));
2682 * RelationGetIndexList -- get a list of OIDs of indexes on this relation
2684 * The index list is created only if someone requests it. We scan pg_index
2685 * to find relevant indexes, and add the list to the relcache entry so that
2686 * we won't have to compute it again. Note that shared cache inval of a
2687 * relcache entry will delete the old list and set rd_indexvalid to 0,
2688 * so that we must recompute the index list on next request. This handles
2689 * creation or deletion of an index.
2691 * The returned list is guaranteed to be sorted in order by OID. This is
2692 * needed by the executor, since for index types that we obtain exclusive
2693 * locks on when updating the index, all backends must lock the indexes in
2694 * the same order or we will get deadlocks (see ExecOpenIndices()). Any
2695 * consistent ordering would do, but ordering by OID is easy.
2697 * Since shared cache inval causes the relcache's copy of the list to go away,
2698 * we return a copy of the list palloc'd in the caller's context. The caller
2699 * may list_free() the returned list after scanning it. This is necessary
2700 * since the caller will typically be doing syscache lookups on the relevant
2701 * indexes, and syscache lookup could cause SI messages to be processed!
2703 * We also update rd_oidindex, which this module treats as effectively part
2704 * of the index list. rd_oidindex is valid when rd_indexvalid isn't zero;
2705 * it is the pg_class OID of a unique index on OID when the relation has one,
2706 * and InvalidOid if there is no such index.
2709 RelationGetIndexList(Relation relation)
2712 SysScanDesc indscan;
2717 MemoryContext oldcxt;
2719 /* Quick exit if we already computed the list. */
2720 if (relation->rd_indexvalid != 0)
2721 return list_copy(relation->rd_indexlist);
2724 * We build the list we intend to return (in the caller's context) while
2725 * doing the scan. After successfully completing the scan, we copy that
2726 * list into the relcache entry. This avoids cache-context memory leakage
2727 * if we get some sort of error partway through.
2730 oidIndex = InvalidOid;
2732 /* Prepare to scan pg_index for entries having indrelid = this rel. */
2734 Anum_pg_index_indrelid,
2735 BTEqualStrategyNumber, F_OIDEQ,
2736 ObjectIdGetDatum(RelationGetRelid(relation)));
2738 indrel = heap_open(IndexRelationId, AccessShareLock);
2739 indscan = systable_beginscan(indrel, IndexIndrelidIndexId, true,
2740 SnapshotNow, 1, &skey);
2742 while (HeapTupleIsValid(htup = systable_getnext(indscan)))
2744 Form_pg_index index = (Form_pg_index) GETSTRUCT(htup);
2746 /* Add index's OID to result list in the proper order */
2747 result = insert_ordered_oid(result, index->indexrelid);
2749 /* Check to see if it is a unique, non-partial btree index on OID */
2750 if (index->indnatts == 1 &&
2751 index->indisunique &&
2752 index->indkey.values[0] == ObjectIdAttributeNumber &&
2753 index->indclass.values[0] == OID_BTREE_OPS_OID &&
2754 heap_attisnull(htup, Anum_pg_index_indpred))
2755 oidIndex = index->indexrelid;
2758 systable_endscan(indscan);
2759 heap_close(indrel, AccessShareLock);
2761 /* Now save a copy of the completed list in the relcache entry. */
2762 oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
2763 relation->rd_indexlist = list_copy(result);
2764 relation->rd_oidindex = oidIndex;
2765 relation->rd_indexvalid = 1;
2766 MemoryContextSwitchTo(oldcxt);
2772 * insert_ordered_oid
2773 * Insert a new Oid into a sorted list of Oids, preserving ordering
2775 * Building the ordered list this way is O(N^2), but with a pretty small
2776 * constant, so for the number of entries we expect it will probably be
2777 * faster than trying to apply qsort(). Most tables don't have very many
2781 insert_ordered_oid(List *list, Oid datum)
2785 /* Does the datum belong at the front? */
2786 if (list == NIL || datum < linitial_oid(list))
2787 return lcons_oid(datum, list);
2788 /* No, so find the entry it belongs after */
2789 prev = list_head(list);
2792 ListCell *curr = lnext(prev);
2794 if (curr == NULL || datum < lfirst_oid(curr))
2795 break; /* it belongs after 'prev', before 'curr' */
2799 /* Insert datum into list after 'prev' */
2800 lappend_cell_oid(list, prev, datum);
2805 * RelationSetIndexList -- externally force the index list contents
2807 * This is used to temporarily override what we think the set of valid
2808 * indexes is (including the presence or absence of an OID index).
2809 * The forcing will be valid only until transaction commit or abort.
2811 * This should only be applied to nailed relations, because in a non-nailed
2812 * relation the hacked index list could be lost at any time due to SI
2813 * messages. In practice it is only used on pg_class (see REINDEX).
2815 * It is up to the caller to make sure the given list is correctly ordered.
2818 RelationSetIndexList(Relation relation, List *indexIds, Oid oidIndex)
2820 MemoryContext oldcxt;
2822 Assert(relation->rd_isnailed);
2823 /* Copy the list into the cache context (could fail for lack of mem) */
2824 oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
2825 indexIds = list_copy(indexIds);
2826 MemoryContextSwitchTo(oldcxt);
2827 /* Okay to replace old list */
2828 list_free(relation->rd_indexlist);
2829 relation->rd_indexlist = indexIds;
2830 relation->rd_oidindex = oidIndex;
2831 relation->rd_indexvalid = 2; /* mark list as forced */
2832 /* must flag that we have a forced index list */
2833 need_eoxact_work = true;
2837 * RelationGetOidIndex -- get the pg_class OID of the relation's OID index
2839 * Returns InvalidOid if there is no such index.
2842 RelationGetOidIndex(Relation relation)
2847 * If relation doesn't have OIDs at all, caller is probably confused. (We
2848 * could just silently return InvalidOid, but it seems better to throw an
2851 Assert(relation->rd_rel->relhasoids);
2853 if (relation->rd_indexvalid == 0)
2855 /* RelationGetIndexList does the heavy lifting. */
2856 ilist = RelationGetIndexList(relation);
2858 Assert(relation->rd_indexvalid != 0);
2861 return relation->rd_oidindex;
2865 * RelationGetIndexExpressions -- get the index expressions for an index
2867 * We cache the result of transforming pg_index.indexprs into a node tree.
2868 * If the rel is not an index or has no expressional columns, we return NIL.
2869 * Otherwise, the returned tree is copied into the caller's memory context.
2870 * (We don't want to return a pointer to the relcache copy, since it could
2871 * disappear due to relcache invalidation.)
2874 RelationGetIndexExpressions(Relation relation)
2880 MemoryContext oldcxt;
2882 /* Quick exit if we already computed the result. */
2883 if (relation->rd_indexprs)
2884 return (List *) copyObject(relation->rd_indexprs);
2886 /* Quick exit if there is nothing to do. */
2887 if (relation->rd_indextuple == NULL ||
2888 heap_attisnull(relation->rd_indextuple, Anum_pg_index_indexprs))
2892 * We build the tree we intend to return in the caller's context. After
2893 * successfully completing the work, we copy it into the relcache entry.
2894 * This avoids problems if we get some sort of error partway through.
2896 exprsDatum = heap_getattr(relation->rd_indextuple,
2897 Anum_pg_index_indexprs,
2898 GetPgIndexDescriptor(),
2901 exprsString = DatumGetCString(DirectFunctionCall1(textout, exprsDatum));
2902 result = (List *) stringToNode(exprsString);
2906 * Run the expressions through eval_const_expressions. This is not just an
2907 * optimization, but is necessary, because the planner will be comparing
2908 * them to similarly-processed qual clauses, and may fail to detect valid
2909 * matches without this. We don't bother with canonicalize_qual, however.
2911 result = (List *) eval_const_expressions((Node *) result);
2914 * Also mark any coercion format fields as "don't care", so that the
2915 * planner can match to both explicit and implicit coercions.
2917 set_coercionform_dontcare((Node *) result);
2919 /* May as well fix opfuncids too */
2920 fix_opfuncids((Node *) result);
2922 /* Now save a copy of the completed tree in the relcache entry. */
2923 oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
2924 relation->rd_indexprs = (List *) copyObject(result);
2925 MemoryContextSwitchTo(oldcxt);
2931 * RelationGetIndexPredicate -- get the index predicate for an index
2933 * We cache the result of transforming pg_index.indpred into an implicit-AND
2934 * node tree (suitable for ExecQual).
2935 * If the rel is not an index or has no predicate, we return NIL.
2936 * Otherwise, the returned tree is copied into the caller's memory context.
2937 * (We don't want to return a pointer to the relcache copy, since it could
2938 * disappear due to relcache invalidation.)
2941 RelationGetIndexPredicate(Relation relation)
2947 MemoryContext oldcxt;
2949 /* Quick exit if we already computed the result. */
2950 if (relation->rd_indpred)
2951 return (List *) copyObject(relation->rd_indpred);
2953 /* Quick exit if there is nothing to do. */
2954 if (relation->rd_indextuple == NULL ||
2955 heap_attisnull(relation->rd_indextuple, Anum_pg_index_indpred))
2959 * We build the tree we intend to return in the caller's context. After
2960 * successfully completing the work, we copy it into the relcache entry.
2961 * This avoids problems if we get some sort of error partway through.
2963 predDatum = heap_getattr(relation->rd_indextuple,
2964 Anum_pg_index_indpred,
2965 GetPgIndexDescriptor(),
2968 predString = DatumGetCString(DirectFunctionCall1(textout, predDatum));
2969 result = (List *) stringToNode(predString);
2973 * Run the expression through const-simplification and canonicalization.
2974 * This is not just an optimization, but is necessary, because the planner
2975 * will be comparing it to similarly-processed qual clauses, and may fail
2976 * to detect valid matches without this. This must match the processing
2977 * done to qual clauses in preprocess_expression()! (We can skip the
2978 * stuff involving subqueries, however, since we don't allow any in index
2981 result = (List *) eval_const_expressions((Node *) result);
2983 result = (List *) canonicalize_qual((Expr *) result);
2986 * Also mark any coercion format fields as "don't care", so that the
2987 * planner can match to both explicit and implicit coercions.
2989 set_coercionform_dontcare((Node *) result);
2991 /* Also convert to implicit-AND format */
2992 result = make_ands_implicit((Expr *) result);
2994 /* May as well fix opfuncids too */
2995 fix_opfuncids((Node *) result);
2997 /* Now save a copy of the completed tree in the relcache entry. */
2998 oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
2999 relation->rd_indpred = (List *) copyObject(result);
3000 MemoryContextSwitchTo(oldcxt);
3007 * load_relcache_init_file, write_relcache_init_file
3009 * In late 1992, we started regularly having databases with more than
3010 * a thousand classes in them. With this number of classes, it became
3011 * critical to do indexed lookups on the system catalogs.
3013 * Bootstrapping these lookups is very hard. We want to be able to
3014 * use an index on pg_attribute, for example, but in order to do so,
3015 * we must have read pg_attribute for the attributes in the index,
3016 * which implies that we need to use the index.
3018 * In order to get around the problem, we do the following:
3020 * + When the database system is initialized (at initdb time), we
3021 * don't use indexes. We do sequential scans.
3023 * + When the backend is started up in normal mode, we load an image
3024 * of the appropriate relation descriptors, in internal format,
3025 * from an initialization file in the data/base/... directory.
3027 * + If the initialization file isn't there, then we create the
3028 * relation descriptors using sequential scans and write 'em to
3029 * the initialization file for use by subsequent backends.
3031 * We could dispense with the initialization file and just build the
3032 * critical reldescs the hard way on every backend startup, but that
3033 * slows down backend startup noticeably.
3035 * We can in fact go further, and save more relcache entries than
3036 * just the ones that are absolutely critical; this allows us to speed
3037 * up backend startup by not having to build such entries the hard way.
3038 * Presently, all the catalog and index entries that are referred to
3039 * by catcaches are stored in the initialization file.
3041 * The same mechanism that detects when catcache and relcache entries
3042 * need to be invalidated (due to catalog updates) also arranges to
3043 * unlink the initialization file when its contents may be out of date.
3044 * The file will then be rebuilt during the next backend startup.
3048 * load_relcache_init_file -- attempt to load cache from the init file
3050 * If successful, return TRUE and set criticalRelcachesBuilt to true.
3051 * If not successful, return FALSE.
3053 * NOTE: we assume we are already switched into CacheMemoryContext.
3056 load_relcache_init_file(void)
3059 char initfilename[MAXPGPATH];
3069 snprintf(initfilename, sizeof(initfilename), "%s/%s",
3070 DatabasePath, RELCACHE_INIT_FILENAME);
3072 fp = AllocateFile(initfilename, PG_BINARY_R);
3077 * Read the index relcache entries from the file. Note we will not enter
3078 * any of them into the cache if the read fails partway through; this
3079 * helps to guard against broken init files.
3082 rels = (Relation *) palloc(max_rels * sizeof(Relation));
3084 nailed_rels = nailed_indexes = 0;
3085 initFileRelationIds = NIL;
3087 /* check for correct magic number (compatible version) */
3088 if (fread(&magic, 1, sizeof(magic), fp) != sizeof(magic))
3090 if (magic != RELCACHE_INIT_FILEMAGIC)
3093 for (relno = 0;; relno++)
3098 Form_pg_class relform;
3100 Datum indclassDatum;
3103 /* first read the relation descriptor length */
3104 if ((nread = fread(&len, 1, sizeof(len), fp)) != sizeof(len))
3107 break; /* end of file */
3111 /* safety check for incompatible relcache layout */
3112 if (len != sizeof(RelationData))
3115 /* allocate another relcache header */
3116 if (num_rels >= max_rels)
3119 rels = (Relation *) repalloc(rels, max_rels * sizeof(Relation));
3122 rel = rels[num_rels++] = (Relation) palloc(len);
3124 /* then, read the Relation structure */
3125 if ((nread = fread(rel, 1, len, fp)) != len)
3128 /* next read the relation tuple form */
3129 if ((nread = fread(&len, 1, sizeof(len), fp)) != sizeof(len))
3132 relform = (Form_pg_class) palloc(len);
3133 if ((nread = fread(relform, 1, len, fp)) != len)
3136 rel->rd_rel = relform;
3138 /* initialize attribute tuple forms */
3139 rel->rd_att = CreateTemplateTupleDesc(relform->relnatts,
3140 relform->relhasoids);
3141 rel->rd_att->tdrefcount = 1; /* mark as refcounted */
3143 rel->rd_att->tdtypeid = relform->reltype;
3144 rel->rd_att->tdtypmod = -1; /* unnecessary, but... */
3146 /* next read all the attribute tuple form data entries */
3147 has_not_null = false;
3148 for (i = 0; i < relform->relnatts; i++)
3150 if ((nread = fread(&len, 1, sizeof(len), fp)) != sizeof(len))
3152 if (len != ATTRIBUTE_TUPLE_SIZE)
3154 if ((nread = fread(rel->rd_att->attrs[i], 1, len, fp)) != len)
3157 has_not_null |= rel->rd_att->attrs[i]->attnotnull;
3160 /* next read the access method specific field */
3161 if ((nread = fread(&len, 1, sizeof(len), fp)) != sizeof(len))
3165 rel->rd_options = palloc(len);
3166 if ((nread = fread(rel->rd_options, 1, len, fp)) != len)
3168 if (len != VARATT_SIZE(rel->rd_options))
3169 goto read_failed; /* sanity check */
3173 rel->rd_options = NULL;
3176 /* mark not-null status */
3179 TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr));
3181 constr->has_not_null = true;
3182 rel->rd_att->constr = constr;
3185 /* If it's an index, there's more to do */
3186 if (rel->rd_rel->relkind == RELKIND_INDEX)
3189 MemoryContext indexcxt;
3191 RegProcedure *support;
3194 /* Count nailed indexes to ensure we have 'em all */
3195 if (rel->rd_isnailed)
3198 /* next, read the pg_index tuple */
3199 if ((nread = fread(&len, 1, sizeof(len), fp)) != sizeof(len))
3202 rel->rd_indextuple = (HeapTuple) palloc(len);
3203 if ((nread = fread(rel->rd_indextuple, 1, len, fp)) != len)
3206 /* Fix up internal pointers in the tuple -- see heap_copytuple */
3207 rel->rd_indextuple->t_data = (HeapTupleHeader) ((char *) rel->rd_indextuple + HEAPTUPLESIZE);
3208 rel->rd_index = (Form_pg_index) GETSTRUCT(rel->rd_indextuple);
3210 /* fix up indclass pointer too */
3211 indclassDatum = fastgetattr(rel->rd_indextuple,
3212 Anum_pg_index_indclass,
3213 GetPgIndexDescriptor(),
3216 rel->rd_indclass = (oidvector *) DatumGetPointer(indclassDatum);
3218 /* next, read the access method tuple form */
3219 if ((nread = fread(&len, 1, sizeof(len), fp)) != sizeof(len))
3222 am = (Form_pg_am) palloc(len);
3223 if ((nread = fread(am, 1, len, fp)) != len)
3228 * prepare index info context --- parameters should match
3229 * RelationInitIndexAccessInfo
3231 indexcxt = AllocSetContextCreate(CacheMemoryContext,
3232 RelationGetRelationName(rel),
3233 ALLOCSET_SMALL_MINSIZE,
3234 ALLOCSET_SMALL_INITSIZE,
3235 ALLOCSET_SMALL_MAXSIZE);
3236 rel->rd_indexcxt = indexcxt;
3238 /* next, read the vector of operator OIDs */
3239 if ((nread = fread(&len, 1, sizeof(len), fp)) != sizeof(len))
3242 operator = (Oid *) MemoryContextAlloc(indexcxt, len);
3243 if ((nread = fread(operator, 1, len, fp)) != len)
3246 rel->rd_operator = operator;
3248 /* finally, read the vector of support procedures */
3249 if ((nread = fread(&len, 1, sizeof(len), fp)) != sizeof(len))
3251 support = (RegProcedure *) MemoryContextAlloc(indexcxt, len);
3252 if ((nread = fread(support, 1, len, fp)) != len)
3255 rel->rd_support = support;
3257 /* set up zeroed fmgr-info vectors */
3258 rel->rd_aminfo = (RelationAmInfo *)
3259 MemoryContextAllocZero(indexcxt, sizeof(RelationAmInfo));
3260 nsupport = relform->relnatts * am->amsupport;
3261 rel->rd_supportinfo = (FmgrInfo *)
3262 MemoryContextAllocZero(indexcxt, nsupport * sizeof(FmgrInfo));
3266 /* Count nailed rels to ensure we have 'em all */
3267 if (rel->rd_isnailed)
3270 Assert(rel->rd_index == NULL);
3271 Assert(rel->rd_indextuple == NULL);
3272 Assert(rel->rd_indclass == NULL);
3273 Assert(rel->rd_am == NULL);
3274 Assert(rel->rd_indexcxt == NULL);
3275 Assert(rel->rd_aminfo == NULL);
3276 Assert(rel->rd_operator == NULL);
3277 Assert(rel->rd_support == NULL);
3278 Assert(rel->rd_supportinfo == NULL);
3282 * Rules and triggers are not saved (mainly because the internal
3283 * format is complex and subject to change). They must be rebuilt if
3284 * needed by RelationCacheInitializePhase2. This is not expected to
3285 * be a big performance hit since few system catalogs have such. Ditto
3286 * for index expressions and predicates.
3288 rel->rd_rules = NULL;
3289 rel->rd_rulescxt = NULL;
3290 rel->trigdesc = NULL;
3291 rel->rd_indexprs = NIL;
3292 rel->rd_indpred = NIL;
3295 * Reset transient-state fields in the relcache entry
3297 rel->rd_smgr = NULL;
3298 rel->rd_targblock = InvalidBlockNumber;
3299 if (rel->rd_isnailed)
3303 rel->rd_indexvalid = 0;
3304 rel->rd_indexlist = NIL;
3305 rel->rd_oidindex = InvalidOid;
3306 rel->rd_createSubid = InvalidSubTransactionId;
3307 rel->rd_amcache = NULL;
3308 MemSet(&rel->pgstat_info, 0, sizeof(rel->pgstat_info));
3311 * Recompute lock and physical addressing info. This is needed in
3312 * case the pg_internal.init file was copied from some other database
3313 * by CREATE DATABASE.
3315 RelationInitLockInfo(rel);
3316 RelationInitPhysicalAddr(rel);
3320 * We reached the end of the init file without apparent problem. Did we
3321 * get the right number of nailed items? (This is a useful crosscheck in
3322 * case the set of critical rels or indexes changes.)
3324 if (nailed_rels != NUM_CRITICAL_RELS ||
3325 nailed_indexes != NUM_CRITICAL_INDEXES)
3329 * OK, all appears well.
3331 * Now insert all the new relcache entries into the cache.
3333 for (relno = 0; relno < num_rels; relno++)
3335 RelationCacheInsert(rels[relno]);
3336 /* also make a list of their OIDs, for RelationIdIsInInitFile */
3337 initFileRelationIds = lcons_oid(RelationGetRelid(rels[relno]),
3338 initFileRelationIds);
3344 criticalRelcachesBuilt = true;
3348 * init file is broken, so do it the hard way. We don't bother trying to
3349 * free the clutter we just allocated; it's not in the relcache so it
3360 * Write out a new initialization file with the current contents
3364 write_relcache_init_file(void)
3367 char tempfilename[MAXPGPATH];
3368 char finalfilename[MAXPGPATH];
3370 HASH_SEQ_STATUS status;
3371 RelIdCacheEnt *idhentry;
3372 MemoryContext oldcxt;
3376 * We must write a temporary file and rename it into place. Otherwise,
3377 * another backend starting at about the same time might crash trying to
3378 * read the partially-complete file.
3380 snprintf(tempfilename, sizeof(tempfilename), "%s/%s.%d",
3381 DatabasePath, RELCACHE_INIT_FILENAME, MyProcPid);
3382 snprintf(finalfilename, sizeof(finalfilename), "%s/%s",
3383 DatabasePath, RELCACHE_INIT_FILENAME);
3385 unlink(tempfilename); /* in case it exists w/wrong permissions */
3387 fp = AllocateFile(tempfilename, PG_BINARY_W);
3391 * We used to consider this a fatal error, but we might as well
3392 * continue with backend startup ...
3395 (errcode_for_file_access(),
3396 errmsg("could not create relation-cache initialization file \"%s\": %m",
3398 errdetail("Continuing anyway, but there's something wrong.")));
3403 * Write a magic number to serve as a file version identifier. We can
3404 * change the magic number whenever the relcache layout changes.
3406 magic = RELCACHE_INIT_FILEMAGIC;
3407 if (fwrite(&magic, 1, sizeof(magic), fp) != sizeof(magic))
3408 elog(FATAL, "could not write init file");
3411 * Write all the reldescs (in no particular order).
3413 hash_seq_init(&status, RelationIdCache);
3415 initFileRelationIds = NIL;
3417 while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
3419 Relation rel = idhentry->reldesc;
3420 Form_pg_class relform = rel->rd_rel;
3422 /* first write the relcache entry proper */
3423 write_item(rel, sizeof(RelationData), fp);
3425 /* next write the relation tuple form */
3426 write_item(relform, CLASS_TUPLE_SIZE, fp);
3428 /* next, do all the attribute tuple form data entries */
3429 for (i = 0; i < relform->relnatts; i++)
3431 write_item(rel->rd_att->attrs[i], ATTRIBUTE_TUPLE_SIZE, fp);
3434 /* next, do the access method specific field */
3435 write_item(rel->rd_options,
3436 (rel->rd_options ? VARATT_SIZE(rel->rd_options) : 0),
3439 /* If it's an index, there's more to do */
3440 if (rel->rd_rel->relkind == RELKIND_INDEX)
3442 Form_pg_am am = rel->rd_am;
3444 /* write the pg_index tuple */
3445 /* we assume this was created by heap_copytuple! */
3446 write_item(rel->rd_indextuple,
3447 HEAPTUPLESIZE + rel->rd_indextuple->t_len,
3450 /* next, write the access method tuple form */
3451 write_item(am, sizeof(FormData_pg_am), fp);
3453 /* next, write the vector of operator OIDs */
3454 write_item(rel->rd_operator,
3455 relform->relnatts * (am->amstrategies * sizeof(Oid)),
3458 /* finally, write the vector of support procedures */
3459 write_item(rel->rd_support,
3460 relform->relnatts * (am->amsupport * sizeof(RegProcedure)),
3464 /* also make a list of their OIDs, for RelationIdIsInInitFile */
3465 oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
3466 initFileRelationIds = lcons_oid(RelationGetRelid(rel),
3467 initFileRelationIds);
3468 MemoryContextSwitchTo(oldcxt);
3472 elog(FATAL, "could not write init file");
3475 * Now we have to check whether the data we've so painstakingly
3476 * accumulated is already obsolete due to someone else's just-committed
3477 * catalog changes. If so, we just delete the temp file and leave it to
3478 * the next backend to try again. (Our own relcache entries will be
3479 * updated by SI message processing, but we can't be sure whether what we
3480 * wrote out was up-to-date.)
3482 * This mustn't run concurrently with RelationCacheInitFileInvalidate, so
3483 * grab a serialization lock for the duration.
3485 LWLockAcquire(RelCacheInitLock, LW_EXCLUSIVE);
3487 /* Make sure we have seen all incoming SI messages */
3488 AcceptInvalidationMessages();
3491 * If we have received any SI relcache invals since backend start, assume
3492 * we may have written out-of-date data.
3494 if (relcacheInvalsReceived == 0L)
3497 * OK, rename the temp file to its final name, deleting any
3498 * previously-existing init file.
3500 * Note: a failure here is possible under Cygwin, if some other
3501 * backend is holding open an unlinked-but-not-yet-gone init file. So
3502 * treat this as a noncritical failure; just remove the useless temp
3505 if (rename(tempfilename, finalfilename) < 0)
3506 unlink(tempfilename);
3510 /* Delete the already-obsolete temp file */
3511 unlink(tempfilename);
3514 LWLockRelease(RelCacheInitLock);
3517 /* write a chunk of data preceded by its length */
3519 write_item(const void *data, Size len, FILE *fp)
3521 if (fwrite(&len, 1, sizeof(len), fp) != sizeof(len))
3522 elog(FATAL, "could not write init file");
3523 if (fwrite(data, 1, len, fp) != len)
3524 elog(FATAL, "could not write init file");
3528 * Detect whether a given relation (identified by OID) is one of the ones
3529 * we store in the init file.
3531 * Note that we effectively assume that all backends running in a database
3532 * would choose to store the same set of relations in the init file;
3533 * otherwise there are cases where we'd fail to detect the need for an init
3534 * file invalidation. This does not seem likely to be a problem in practice.
3537 RelationIdIsInInitFile(Oid relationId)
3539 return list_member_oid(initFileRelationIds, relationId);
3543 * Invalidate (remove) the init file during commit of a transaction that
3544 * changed one or more of the relation cache entries that are kept in the
3547 * We actually need to remove the init file twice: once just before sending
3548 * the SI messages that include relcache inval for such relations, and once
3549 * just after sending them. The unlink before ensures that a backend that's
3550 * currently starting cannot read the now-obsolete init file and then miss
3551 * the SI messages that will force it to update its relcache entries. (This
3552 * works because the backend startup sequence gets into the PGPROC array before
3553 * trying to load the init file.) The unlink after is to synchronize with a
3554 * backend that may currently be trying to write an init file based on data
3555 * that we've just rendered invalid. Such a backend will see the SI messages,
3556 * but we can't leave the init file sitting around to fool later backends.
3558 * Ignore any failure to unlink the file, since it might not be there if
3559 * no backend has been started since the last removal.
3562 RelationCacheInitFileInvalidate(bool beforeSend)
3564 char initfilename[MAXPGPATH];
3566 snprintf(initfilename, sizeof(initfilename), "%s/%s",
3567 DatabasePath, RELCACHE_INIT_FILENAME);
3571 /* no interlock needed here */
3572 unlink(initfilename);
3577 * We need to interlock this against write_relcache_init_file, to
3578 * guard against possibility that someone renames a new-but-
3579 * already-obsolete init file into place just after we unlink. With
3580 * the interlock, it's certain that write_relcache_init_file will
3581 * notice our SI inval message before renaming into place, or else
3582 * that we will execute second and successfully unlink the file.
3584 LWLockAcquire(RelCacheInitLock, LW_EXCLUSIVE);
3585 unlink(initfilename);
3586 LWLockRelease(RelCacheInitLock);