Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * relcache.c
4 : : * POSTGRES relation descriptor cache code
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/utils/cache/relcache.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : : /*
16 : : * INTERFACE ROUTINES
17 : : * RelationCacheInitialize - initialize relcache (to empty)
18 : : * RelationCacheInitializePhase2 - initialize shared-catalog entries
19 : : * RelationCacheInitializePhase3 - finish initializing relcache
20 : : * RelationIdGetRelation - get a reldesc by relation id
21 : : * RelationClose - close an open relation
22 : : *
23 : : * NOTES
24 : : * The following code contains many undocumented hacks. Please be
25 : : * careful....
26 : : */
27 : : #include "postgres.h"
28 : :
29 : : #include <sys/file.h>
30 : : #include <fcntl.h>
31 : : #include <unistd.h>
32 : :
33 : : #include "access/htup_details.h"
34 : : #include "access/multixact.h"
35 : : #include "access/parallel.h"
36 : : #include "access/reloptions.h"
37 : : #include "access/sysattr.h"
38 : : #include "access/table.h"
39 : : #include "access/tableam.h"
40 : : #include "access/tupdesc_details.h"
41 : : #include "access/xact.h"
42 : : #include "catalog/binary_upgrade.h"
43 : : #include "catalog/catalog.h"
44 : : #include "catalog/indexing.h"
45 : : #include "catalog/namespace.h"
46 : : #include "catalog/partition.h"
47 : : #include "catalog/pg_am.h"
48 : : #include "catalog/pg_amproc.h"
49 : : #include "catalog/pg_attrdef.h"
50 : : #include "catalog/pg_auth_members.h"
51 : : #include "catalog/pg_authid.h"
52 : : #include "catalog/pg_constraint.h"
53 : : #include "catalog/pg_database.h"
54 : : #include "catalog/pg_namespace.h"
55 : : #include "catalog/pg_opclass.h"
56 : : #include "catalog/pg_proc.h"
57 : : #include "catalog/pg_publication.h"
58 : : #include "catalog/pg_rewrite.h"
59 : : #include "catalog/pg_shseclabel.h"
60 : : #include "catalog/pg_statistic_ext.h"
61 : : #include "catalog/pg_subscription.h"
62 : : #include "catalog/pg_tablespace.h"
63 : : #include "catalog/pg_trigger.h"
64 : : #include "catalog/pg_type.h"
65 : : #include "catalog/schemapg.h"
66 : : #include "catalog/storage.h"
67 : : #include "commands/policy.h"
68 : : #include "commands/publicationcmds.h"
69 : : #include "commands/trigger.h"
70 : : #include "common/int.h"
71 : : #include "miscadmin.h"
72 : : #include "nodes/makefuncs.h"
73 : : #include "nodes/nodeFuncs.h"
74 : : #include "optimizer/optimizer.h"
75 : : #include "pgstat.h"
76 : : #include "rewrite/rewriteDefine.h"
77 : : #include "rewrite/rowsecurity.h"
78 : : #include "storage/lmgr.h"
79 : : #include "storage/smgr.h"
80 : : #include "utils/array.h"
81 : : #include "utils/builtins.h"
82 : : #include "utils/catcache.h"
83 : : #include "utils/datum.h"
84 : : #include "utils/fmgroids.h"
85 : : #include "utils/inval.h"
86 : : #include "utils/lsyscache.h"
87 : : #include "utils/memutils.h"
88 : : #include "utils/relmapper.h"
89 : : #include "utils/resowner.h"
90 : : #include "utils/snapmgr.h"
91 : : #include "utils/syscache.h"
92 : :
93 : : #define RELCACHE_INIT_FILEMAGIC 0x573266 /* version ID value */
94 : :
95 : : /*
96 : : * Whether to bother checking if relation cache memory needs to be freed
97 : : * eagerly. See also RelationBuildDesc() and pg_config_manual.h.
98 : : */
99 : : #if defined(RECOVER_RELATION_BUILD_MEMORY) && (RECOVER_RELATION_BUILD_MEMORY != 0)
100 : : #define MAYBE_RECOVER_RELATION_BUILD_MEMORY 1
101 : : #else
102 : : #define RECOVER_RELATION_BUILD_MEMORY 0
103 : : #ifdef DISCARD_CACHES_ENABLED
104 : : #define MAYBE_RECOVER_RELATION_BUILD_MEMORY 1
105 : : #endif
106 : : #endif
107 : :
108 : : /*
109 : : * hardcoded tuple descriptors, contents generated by genbki.pl
110 : : */
111 : : static const FormData_pg_attribute Desc_pg_class[Natts_pg_class] = {Schema_pg_class};
112 : : static const FormData_pg_attribute Desc_pg_attribute[Natts_pg_attribute] = {Schema_pg_attribute};
113 : : static const FormData_pg_attribute Desc_pg_proc[Natts_pg_proc] = {Schema_pg_proc};
114 : : static const FormData_pg_attribute Desc_pg_type[Natts_pg_type] = {Schema_pg_type};
115 : : static const FormData_pg_attribute Desc_pg_database[Natts_pg_database] = {Schema_pg_database};
116 : : static const FormData_pg_attribute Desc_pg_authid[Natts_pg_authid] = {Schema_pg_authid};
117 : : static const FormData_pg_attribute Desc_pg_auth_members[Natts_pg_auth_members] = {Schema_pg_auth_members};
118 : : static const FormData_pg_attribute Desc_pg_index[Natts_pg_index] = {Schema_pg_index};
119 : : static const FormData_pg_attribute Desc_pg_shseclabel[Natts_pg_shseclabel] = {Schema_pg_shseclabel};
120 : : static const FormData_pg_attribute Desc_pg_subscription[Natts_pg_subscription] = {Schema_pg_subscription};
121 : :
122 : : /*
123 : : * Hash tables that index the relation cache
124 : : *
125 : : * We used to index the cache by both name and OID, but now there
126 : : * is only an index by OID.
127 : : */
128 : : typedef struct relidcacheent
129 : : {
130 : : Oid reloid;
131 : : Relation reldesc;
132 : : } RelIdCacheEnt;
133 : :
134 : : static HTAB *RelationIdCache;
135 : :
136 : : /*
137 : : * This flag is false until we have prepared the critical relcache entries
138 : : * that are needed to do indexscans on the tables read by relcache building.
139 : : */
140 : : bool criticalRelcachesBuilt = false;
141 : :
142 : : /*
143 : : * This flag is false until we have prepared the critical relcache entries
144 : : * for shared catalogs (which are the tables needed for login).
145 : : */
146 : : bool criticalSharedRelcachesBuilt = false;
147 : :
148 : : /*
149 : : * This counter counts relcache inval events received since backend startup
150 : : * (but only for rels that are actually in cache). Presently, we use it only
151 : : * to detect whether data about to be written by write_relcache_init_file()
152 : : * might already be obsolete.
153 : : */
154 : : static long relcacheInvalsReceived = 0L;
155 : :
156 : : /*
157 : : * in_progress_list is a stack of ongoing RelationBuildDesc() calls. CREATE
158 : : * INDEX CONCURRENTLY makes catalog changes under ShareUpdateExclusiveLock.
159 : : * It critically relies on each backend absorbing those changes no later than
160 : : * next transaction start. Hence, RelationBuildDesc() loops until it finishes
161 : : * without accepting a relevant invalidation. (Most invalidation consumers
162 : : * don't do this.)
163 : : */
164 : : typedef struct inprogressent
165 : : {
166 : : Oid reloid; /* OID of relation being built */
167 : : bool invalidated; /* whether an invalidation arrived for it */
168 : : } InProgressEnt;
169 : :
170 : : static InProgressEnt *in_progress_list;
171 : : static int in_progress_list_len;
172 : : static int in_progress_list_maxlen;
173 : :
174 : : /*
175 : : * eoxact_list[] stores the OIDs of relations that (might) need AtEOXact
176 : : * cleanup work. This list intentionally has limited size; if it overflows,
177 : : * we fall back to scanning the whole hashtable. There is no value in a very
178 : : * large list because (1) at some point, a hash_seq_search scan is faster than
179 : : * retail lookups, and (2) the value of this is to reduce EOXact work for
180 : : * short transactions, which can't have dirtied all that many tables anyway.
181 : : * EOXactListAdd() does not bother to prevent duplicate list entries, so the
182 : : * cleanup processing must be idempotent.
183 : : */
184 : : #define MAX_EOXACT_LIST 32
185 : : static Oid eoxact_list[MAX_EOXACT_LIST];
186 : : static int eoxact_list_len = 0;
187 : : static bool eoxact_list_overflowed = false;
188 : :
189 : : #define EOXactListAdd(rel) \
190 : : do { \
191 : : if (eoxact_list_len < MAX_EOXACT_LIST) \
192 : : eoxact_list[eoxact_list_len++] = (rel)->rd_id; \
193 : : else \
194 : : eoxact_list_overflowed = true; \
195 : : } while (0)
196 : :
197 : : /*
198 : : * EOXactTupleDescArray stores TupleDescs that (might) need AtEOXact
199 : : * cleanup work. The array expands as needed; there is no hashtable because
200 : : * we don't need to access individual items except at EOXact.
201 : : */
202 : : static TupleDesc *EOXactTupleDescArray;
203 : : static int NextEOXactTupleDescNum = 0;
204 : : static int EOXactTupleDescArrayLen = 0;
205 : :
206 : : /*
207 : : * macros to manipulate the lookup hashtable
208 : : */
209 : : #define RelationCacheInsert(RELATION, replace_allowed) \
210 : : do { \
211 : : RelIdCacheEnt *hentry; bool found; \
212 : : hentry = (RelIdCacheEnt *) hash_search(RelationIdCache, \
213 : : &((RELATION)->rd_id), \
214 : : HASH_ENTER, &found); \
215 : : if (found) \
216 : : { \
217 : : /* see comments in RelationBuildDesc and RelationBuildLocalRelation */ \
218 : : Relation _old_rel = hentry->reldesc; \
219 : : Assert(replace_allowed); \
220 : : hentry->reldesc = (RELATION); \
221 : : if (RelationHasReferenceCountZero(_old_rel)) \
222 : : RelationDestroyRelation(_old_rel, false); \
223 : : else if (!IsBootstrapProcessingMode()) \
224 : : elog(WARNING, "leaking still-referenced relcache entry for \"%s\"", \
225 : : RelationGetRelationName(_old_rel)); \
226 : : } \
227 : : else \
228 : : hentry->reldesc = (RELATION); \
229 : : } while(0)
230 : :
231 : : #define RelationIdCacheLookup(ID, RELATION) \
232 : : do { \
233 : : RelIdCacheEnt *hentry; \
234 : : hentry = (RelIdCacheEnt *) hash_search(RelationIdCache, \
235 : : &(ID), \
236 : : HASH_FIND, NULL); \
237 : : if (hentry) \
238 : : RELATION = hentry->reldesc; \
239 : : else \
240 : : RELATION = NULL; \
241 : : } while(0)
242 : :
243 : : #define RelationCacheDelete(RELATION) \
244 : : do { \
245 : : RelIdCacheEnt *hentry; \
246 : : hentry = (RelIdCacheEnt *) hash_search(RelationIdCache, \
247 : : &((RELATION)->rd_id), \
248 : : HASH_REMOVE, NULL); \
249 : : if (hentry == NULL) \
250 : : elog(WARNING, "failed to delete relcache entry for OID %u", \
251 : : (RELATION)->rd_id); \
252 : : } while(0)
253 : :
254 : :
255 : : /*
256 : : * Special cache for opclass-related information
257 : : *
258 : : * Note: only default support procs get cached, ie, those with
259 : : * lefttype = righttype = opcintype.
260 : : */
261 : : typedef struct opclasscacheent
262 : : {
263 : : Oid opclassoid; /* lookup key: OID of opclass */
264 : : bool valid; /* set true after successful fill-in */
265 : : StrategyNumber numSupport; /* max # of support procs (from pg_am) */
266 : : Oid opcfamily; /* OID of opclass's family */
267 : : Oid opcintype; /* OID of opclass's declared input type */
268 : : RegProcedure *supportProcs; /* OIDs of support procedures */
269 : : } OpClassCacheEnt;
270 : :
271 : : static HTAB *OpClassCache = NULL;
272 : :
273 : :
274 : : /* non-export function prototypes */
275 : :
276 : : static void RelationCloseCleanup(Relation relation);
277 : : static void RelationDestroyRelation(Relation relation, bool remember_tupdesc);
278 : : static void RelationInvalidateRelation(Relation relation);
279 : : static void RelationClearRelation(Relation relation);
280 : : static void RelationRebuildRelation(Relation relation);
281 : :
282 : : static void RelationReloadIndexInfo(Relation relation);
283 : : static void RelationReloadNailed(Relation relation);
284 : : static void RelationFlushRelation(Relation relation);
285 : : static void RememberToFreeTupleDescAtEOX(TupleDesc td);
286 : : #ifdef USE_ASSERT_CHECKING
287 : : static void AssertPendingSyncConsistency(Relation relation);
288 : : #endif
289 : : static void AtEOXact_cleanup(Relation relation, bool isCommit);
290 : : static void AtEOSubXact_cleanup(Relation relation, bool isCommit,
291 : : SubTransactionId mySubid, SubTransactionId parentSubid);
292 : : static bool load_relcache_init_file(bool shared);
293 : : static void write_relcache_init_file(bool shared);
294 : : static void write_item(const void *data, Size len, FILE *fp);
295 : :
296 : : static void formrdesc(const char *relationName, Oid relationReltype,
297 : : bool isshared, int natts, const FormData_pg_attribute *attrs);
298 : :
299 : : static HeapTuple ScanPgRelation(Oid targetRelId, bool indexOK, bool force_non_historic);
300 : : static Relation AllocateRelationDesc(Form_pg_class relp);
301 : : static void RelationParseRelOptions(Relation relation, HeapTuple tuple);
302 : : static void RelationBuildTupleDesc(Relation relation);
303 : : static Relation RelationBuildDesc(Oid targetRelId, bool insertIt);
304 : : static void RelationInitPhysicalAddr(Relation relation);
305 : : static void load_critical_index(Oid indexoid, Oid heapoid);
306 : : static TupleDesc GetPgClassDescriptor(void);
307 : : static TupleDesc GetPgIndexDescriptor(void);
308 : : static void AttrDefaultFetch(Relation relation, int ndef);
309 : : static int AttrDefaultCmp(const void *a, const void *b);
310 : : static void CheckNNConstraintFetch(Relation relation);
311 : : static int CheckConstraintCmp(const void *a, const void *b);
312 : : static void InitIndexAmRoutine(Relation relation);
313 : : static void IndexSupportInitialize(oidvector *indclass,
314 : : RegProcedure *indexSupport,
315 : : Oid *opFamily,
316 : : Oid *opcInType,
317 : : StrategyNumber maxSupportNumber,
318 : : AttrNumber maxAttributeNumber);
319 : : static OpClassCacheEnt *LookupOpclassInfo(Oid operatorClassOid,
320 : : StrategyNumber numSupport);
321 : : static void RelationCacheInitFileRemoveInDir(const char *tblspcpath);
322 : : static void unlink_initfile(const char *initfilename, int elevel);
323 : :
324 : :
325 : : /*
326 : : * ScanPgRelation
327 : : *
328 : : * This is used by RelationBuildDesc to find a pg_class
329 : : * tuple matching targetRelId. The caller must hold at least
330 : : * AccessShareLock on the target relid to prevent concurrent-update
331 : : * scenarios; it isn't guaranteed that all scans used to build the
332 : : * relcache entry will use the same snapshot. If, for example,
333 : : * an attribute were to be added after scanning pg_class and before
334 : : * scanning pg_attribute, relnatts wouldn't match.
335 : : *
336 : : * NB: the returned tuple has been copied into palloc'd storage
337 : : * and must eventually be freed with heap_freetuple.
338 : : */
339 : : static HeapTuple
4395 rhaas@postgresql.org 340 :CBC 810867 : ScanPgRelation(Oid targetRelId, bool indexOK, bool force_non_historic)
341 : : {
342 : : HeapTuple pg_class_tuple;
343 : : Relation pg_class_desc;
344 : : SysScanDesc pg_class_scan;
345 : : ScanKeyData key[1];
2178 andres@anarazel.de 346 : 810867 : Snapshot snapshot = NULL;
347 : :
348 : : /*
349 : : * If something goes wrong during backend startup, we might find ourselves
350 : : * trying to read pg_class before we've selected a database. That ain't
351 : : * gonna work, so bail out with a useful error message. If this happens,
352 : : * it probably means a relcache entry that needs to be nailed isn't.
353 : : */
6059 tgl@sss.pgh.pa.us 354 [ - + ]: 810867 : if (!OidIsValid(MyDatabaseId))
6059 tgl@sss.pgh.pa.us 355 [ # # ]:UBC 0 : elog(FATAL, "cannot read pg_class without having selected a database");
356 : :
357 : : /*
358 : : * form a scan key
359 : : */
7640 tgl@sss.pgh.pa.us 360 :CBC 810867 : ScanKeyInit(&key[0],
361 : : Anum_pg_class_oid,
362 : : BTEqualStrategyNumber, F_OIDEQ,
363 : : ObjectIdGetDatum(targetRelId));
364 : :
365 : : /*
366 : : * Open pg_class and fetch a tuple. Force heap scan if we haven't yet
367 : : * built the critical relcache entries (this includes initdb and startup
368 : : * without a pg_internal.init file). The caller can also force a heap
369 : : * scan by setting indexOK == false.
370 : : */
2610 andres@anarazel.de 371 : 810867 : pg_class_desc = table_open(RelationRelationId, AccessShareLock);
372 : :
373 : : /*
374 : : * The caller might need a tuple that's newer than what's visible to the
375 : : * historic snapshot; currently the only case requiring to do so is
376 : : * looking up the relfilenumber of non mapped system relations during
377 : : * decoding.
378 : : */
4395 rhaas@postgresql.org 379 [ + + ]: 810867 : if (force_non_historic)
369 heikki.linnakangas@i 380 : 1752 : snapshot = RegisterSnapshot(GetNonHistoricCatalogSnapshot(RelationRelationId));
381 : :
7640 tgl@sss.pgh.pa.us 382 : 810867 : pg_class_scan = systable_beginscan(pg_class_desc, ClassOidIndexId,
8208 383 [ + + + + ]: 810867 : indexOK && criticalRelcachesBuilt,
384 : : snapshot,
385 : : 1, key);
386 : :
8790 387 : 810864 : pg_class_tuple = systable_getnext(pg_class_scan);
388 : :
389 : : /*
390 : : * Must copy tuple before releasing buffer.
391 : : */
392 [ + + ]: 810861 : if (HeapTupleIsValid(pg_class_tuple))
393 : 810654 : pg_class_tuple = heap_copytuple(pg_class_tuple);
394 : :
395 : : /* all done */
396 : 810861 : systable_endscan(pg_class_scan);
397 : :
369 heikki.linnakangas@i 398 [ + + ]: 810861 : if (snapshot)
399 : 1752 : UnregisterSnapshot(snapshot);
400 : :
2610 andres@anarazel.de 401 : 810861 : table_close(pg_class_desc, AccessShareLock);
402 : :
8790 tgl@sss.pgh.pa.us 403 : 810861 : return pg_class_tuple;
404 : : }
405 : :
406 : : /*
407 : : * AllocateRelationDesc
408 : : *
409 : : * This is used to allocate memory for a new relation descriptor
410 : : * and initialize the rd_rel field from the given pg_class tuple.
411 : : */
412 : : static Relation
5906 413 : 734363 : AllocateRelationDesc(Form_pg_class relp)
414 : : {
415 : : Relation relation;
416 : : MemoryContext oldcxt;
417 : : Form_pg_class relationForm;
418 : :
419 : : /* Relcache entries must live in CacheMemoryContext */
9389 420 : 734363 : oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
421 : :
422 : : /*
423 : : * allocate and zero space for new relation descriptor
424 : : */
95 michael@paquier.xyz 425 :GNC 734363 : relation = palloc0_object(RelationData);
426 : :
427 : : /* make sure relation is marked as having no open file yet */
8069 tgl@sss.pgh.pa.us 428 :CBC 734363 : relation->rd_smgr = NULL;
429 : :
430 : : /*
431 : : * Copy the relation tuple form
432 : : *
433 : : * We only allocate space for the fixed fields, ie, CLASS_TUPLE_SIZE. The
434 : : * variable-length fields (relacl, reloptions) are NOT stored in the
435 : : * relcache --- there'd be little point in it, since we don't copy the
436 : : * tuple's nulls bitmap and hence wouldn't know if the values are valid.
437 : : * Bottom line is that relacl *cannot* be retrieved from the relcache. Get
438 : : * it from the syscache if you need it. The same goes for the original
439 : : * form of reloptions (however, we do store the parsed form of reloptions
440 : : * in rd_options).
441 : : */
9389 442 : 734363 : relationForm = (Form_pg_class) palloc(CLASS_TUPLE_SIZE);
443 : :
7613 neilc@samurai.com 444 : 734363 : memcpy(relationForm, relp, CLASS_TUPLE_SIZE);
445 : :
446 : : /* initialize relation tuple form */
10057 bruce@momjian.us 447 : 734363 : relation->rd_rel = relationForm;
448 : :
449 : : /* and allocate attribute tuple form storage */
2672 andres@anarazel.de 450 : 734363 : relation->rd_att = CreateTemplateTupleDesc(relationForm->relnatts);
451 : : /* which we mark as a reference-counted tupdesc */
7212 tgl@sss.pgh.pa.us 452 : 734363 : relation->rd_att->tdrefcount = 1;
453 : :
9389 454 : 734363 : MemoryContextSwitchTo(oldcxt);
455 : :
10416 bruce@momjian.us 456 : 734363 : return relation;
457 : : }
458 : :
459 : : /*
460 : : * RelationParseRelOptions
461 : : * Convert pg_class.reloptions into pre-parsed rd_options
462 : : *
463 : : * tuple is the real pg_class tuple (not rd_rel!) for relation
464 : : *
465 : : * Note: rd_rel and (if an index) rd_indam must be valid already
466 : : */
467 : : static void
7195 tgl@sss.pgh.pa.us 468 : 806978 : RelationParseRelOptions(Relation relation, HeapTuple tuple)
469 : : {
470 : : bytea *options;
471 : : amoptions_function amoptsfn;
472 : :
473 : 806978 : relation->rd_options = NULL;
474 : :
475 : : /*
476 : : * Look up any AM-specific parse function; fall out if relkind should not
477 : : * have options.
478 : : */
7196 bruce@momjian.us 479 [ + + + ]: 806978 : switch (relation->rd_rel->relkind)
480 : : {
7195 tgl@sss.pgh.pa.us 481 : 450013 : case RELKIND_RELATION:
482 : : case RELKIND_TOASTVALUE:
483 : : case RELKIND_VIEW:
484 : : case RELKIND_MATVIEW:
485 : : case RELKIND_PARTITIONED_TABLE:
2977 alvherre@alvh.no-ip. 486 : 450013 : amoptsfn = NULL;
487 : 450013 : break;
488 : 347601 : case RELKIND_INDEX:
489 : : case RELKIND_PARTITIONED_INDEX:
2610 andres@anarazel.de 490 : 347601 : amoptsfn = relation->rd_indam->amoptions;
7195 tgl@sss.pgh.pa.us 491 : 347601 : break;
492 : 9364 : default:
493 : 9364 : return;
494 : : }
495 : :
496 : : /*
497 : : * Fetch reloptions from tuple; have to use a hardwired descriptor because
498 : : * we might not have any other for pg_class yet (consider executing this
499 : : * code for pg_class itself)
500 : : */
703 akorotkov@postgresql 501 : 797614 : options = extractRelOptions(tuple, GetPgClassDescriptor(), amoptsfn);
502 : :
503 : : /*
504 : : * Copy parsed data into CacheMemoryContext. To guard against the
505 : : * possibility of leaks in the reloptions code, we want to do the actual
506 : : * parsing in the caller's memory context and copy the results into
507 : : * CacheMemoryContext after the fact.
508 : : */
7195 tgl@sss.pgh.pa.us 509 [ + + ]: 797614 : if (options)
510 : : {
511 : 9445 : relation->rd_options = MemoryContextAlloc(CacheMemoryContext,
7195 tgl@sss.pgh.pa.us 512 :ECB (8272) : VARSIZE(options));
7195 tgl@sss.pgh.pa.us 513 :CBC 9445 : memcpy(relation->rd_options, options, VARSIZE(options));
5906 514 : 9445 : pfree(options);
515 : : }
516 : : }
517 : :
518 : : /*
519 : : * RelationBuildTupleDesc
520 : : *
521 : : * Form the relation's tuple descriptor from information in
522 : : * the pg_attribute, pg_attrdef & pg_constraint system catalogs.
523 : : */
524 : : static void
7640 525 : 734363 : RelationBuildTupleDesc(Relation relation)
526 : : {
527 : : HeapTuple pg_attribute_tuple;
528 : : Relation pg_attribute_desc;
529 : : SysScanDesc pg_attribute_scan;
530 : : ScanKeyData skey[2];
531 : : int need;
532 : : TupleConstr *constr;
2909 andrew@dunslane.net 533 : 734363 : AttrMissing *attrmiss = NULL;
9468 bruce@momjian.us 534 : 734363 : int ndef = 0;
535 : :
536 : : /* fill rd_att's type ID fields (compare heap.c's AddNewRelationTuple) */
2077 tgl@sss.pgh.pa.us 537 : 734363 : relation->rd_att->tdtypeid =
538 [ + + ]: 734363 : relation->rd_rel->reltype ? relation->rd_rel->reltype : RECORDOID;
539 : 734363 : relation->rd_att->tdtypmod = -1; /* just to be sure */
540 : :
1804 541 : 734363 : constr = (TupleConstr *) MemoryContextAllocZero(CacheMemoryContext,
542 : : sizeof(TupleConstr));
543 : :
544 : : /*
545 : : * Form a scan key that selects only user attributes (attnum > 0).
546 : : * (Eliminating system attribute rows at the index level is lots faster
547 : : * than fetching them.)
548 : : */
8159 549 : 734363 : ScanKeyInit(&skey[0],
550 : : Anum_pg_attribute_attrelid,
551 : : BTEqualStrategyNumber, F_OIDEQ,
552 : : ObjectIdGetDatum(RelationGetRelid(relation)));
553 : 734363 : ScanKeyInit(&skey[1],
554 : : Anum_pg_attribute_attnum,
555 : : BTGreaterStrategyNumber, F_INT2GT,
556 : : Int16GetDatum(0));
557 : :
558 : : /*
559 : : * Open pg_attribute and begin a scan. Force heap scan if we haven't yet
560 : : * built the critical relcache entries (this includes initdb and startup
561 : : * without a pg_internal.init file).
562 : : */
2610 andres@anarazel.de 563 : 734363 : pg_attribute_desc = table_open(AttributeRelationId, AccessShareLock);
8790 tgl@sss.pgh.pa.us 564 : 734363 : pg_attribute_scan = systable_beginscan(pg_attribute_desc,
565 : : AttributeRelidNumIndexId,
566 : : criticalRelcachesBuilt,
567 : : NULL,
568 : : 2, skey);
569 : :
570 : : /*
571 : : * add attribute data to relation->rd_att
572 : : */
2899 teodor@sigaev.ru 573 : 734363 : need = RelationGetNumberOfAttributes(relation);
574 : :
8790 tgl@sss.pgh.pa.us 575 [ + + ]: 2625143 : while (HeapTupleIsValid(pg_attribute_tuple = systable_getnext(pg_attribute_scan)))
576 : : {
577 : : Form_pg_attribute attp;
578 : : int attnum;
579 : :
10057 bruce@momjian.us 580 : 2619497 : attp = (Form_pg_attribute) GETSTRUCT(pg_attribute_tuple);
581 : :
2909 andrew@dunslane.net 582 : 2619497 : attnum = attp->attnum;
2899 teodor@sigaev.ru 583 [ + - - + ]: 2619497 : if (attnum <= 0 || attnum > RelationGetNumberOfAttributes(relation))
1804 tgl@sss.pgh.pa.us 584 [ # # ]:UBC 0 : elog(ERROR, "invalid attribute number %d for relation \"%s\"",
585 : : attp->attnum, RelationGetRelationName(relation));
586 : :
2909 andrew@dunslane.net 587 :CBC 2619497 : memcpy(TupleDescAttr(relation->rd_att, attnum - 1),
588 : : attp,
589 : : ATTRIBUTE_FIXED_PART_SIZE);
590 : :
450 drowley@postgresql.o 591 : 2619497 : populate_compact_attribute(relation->rd_att, attnum - 1);
592 : :
593 : : /* Update constraint/default info */
8755 tgl@sss.pgh.pa.us 594 [ + + ]: 2619497 : if (attp->attnotnull)
8790 595 : 1129006 : constr->has_not_null = true;
2542 peter@eisentraut.org 596 [ + + ]: 2619497 : if (attp->attgenerated == ATTRIBUTE_GENERATED_STORED)
597 : 6373 : constr->has_generated_stored = true;
401 598 [ + + ]: 2619497 : if (attp->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
599 : 3632 : constr->has_generated_virtual = true;
8790 tgl@sss.pgh.pa.us 600 [ + + ]: 2619497 : if (attp->atthasdef)
601 : 27584 : ndef++;
602 : :
603 : : /* If the column has a "missing" value, put it in the attrmiss array */
2909 andrew@dunslane.net 604 [ + + ]: 2619497 : if (attp->atthasmissing)
605 : : {
606 : : Datum missingval;
607 : : bool missingNull;
608 : :
609 : : /* Do we have a missing value? */
610 : 4090 : missingval = heap_getattr(pg_attribute_tuple,
611 : : Anum_pg_attribute_attmissingval,
612 : : pg_attribute_desc->rd_att,
613 : : &missingNull);
614 [ + - ]: 4090 : if (!missingNull)
615 : : {
616 : : /* Yes, fetch from the array */
617 : : MemoryContext oldcxt;
618 : : bool is_null;
619 : 4090 : int one = 1;
620 : : Datum missval;
621 : :
622 [ + + ]: 4090 : if (attrmiss == NULL)
623 : : attrmiss = (AttrMissing *)
624 : 1984 : MemoryContextAllocZero(CacheMemoryContext,
625 : 1984 : relation->rd_rel->relnatts *
626 : : sizeof(AttrMissing));
627 : :
628 : 4090 : missval = array_get_element(missingval,
629 : : 1,
630 : : &one,
631 : : -1,
632 : 4090 : attp->attlen,
633 : 4090 : attp->attbyval,
634 : 4090 : attp->attalign,
635 : : &is_null);
636 [ - + ]: 4090 : Assert(!is_null);
637 [ + + ]: 4090 : if (attp->attbyval)
638 : : {
639 : : /* for copy by val just copy the datum direct */
2818 akapila@postgresql.o 640 : 2616 : attrmiss[attnum - 1].am_value = missval;
641 : : }
642 : : else
643 : : {
644 : : /* otherwise copy in the correct context */
2909 andrew@dunslane.net 645 : 1474 : oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
2818 akapila@postgresql.o 646 : 2948 : attrmiss[attnum - 1].am_value = datumCopy(missval,
647 : 1474 : attp->attbyval,
648 : 1474 : attp->attlen);
2909 andrew@dunslane.net 649 : 1474 : MemoryContextSwitchTo(oldcxt);
650 : : }
2818 akapila@postgresql.o 651 : 4090 : attrmiss[attnum - 1].am_present = true;
652 : : }
653 : : }
8790 tgl@sss.pgh.pa.us 654 : 2619497 : need--;
655 [ + + ]: 2619497 : if (need == 0)
656 : 728717 : break;
657 : : }
658 : :
659 : : /*
660 : : * end the scan and close the attribute relation
661 : : */
662 : 734360 : systable_endscan(pg_attribute_scan);
2610 andres@anarazel.de 663 : 734360 : table_close(pg_attribute_desc, AccessShareLock);
664 : :
8790 tgl@sss.pgh.pa.us 665 [ - + ]: 734360 : if (need != 0)
1804 tgl@sss.pgh.pa.us 666 [ # # ]:UBC 0 : elog(ERROR, "pg_attribute catalog is missing %d attribute(s) for relation OID %u",
667 : : need, RelationGetRelid(relation));
668 : :
669 : : /*
670 : : * We can easily set the attcacheoff value for the first attribute: it
671 : : * must be zero. This eliminates the need for special cases for attnum=1
672 : : * that used to exist in fastgetattr() and index_getattr().
673 : : */
2899 teodor@sigaev.ru 674 [ + + ]:CBC 734360 : if (RelationGetNumberOfAttributes(relation) > 0)
450 drowley@postgresql.o 675 : 728717 : TupleDescCompactAttr(relation->rd_att, 0)->attcacheoff = 0;
676 : :
677 : : /*
678 : : * Set up constraint/default info
679 : : */
2229 peter@eisentraut.org 680 [ + + ]: 734360 : if (constr->has_not_null ||
681 [ + + ]: 495679 : constr->has_generated_stored ||
401 682 [ + + + + ]: 492985 : constr->has_generated_virtual ||
2229 683 [ + + ]: 489626 : ndef > 0 ||
684 : 489602 : attrmiss ||
1804 tgl@sss.pgh.pa.us 685 [ + + ]: 489602 : relation->rd_rel->relchecks > 0)
10433 vadim4o@yahoo.com 686 : 247900 : {
342 alvherre@alvh.no-ip. 687 : 247900 : bool is_catalog = IsCatalogRelation(relation);
688 : :
8790 tgl@sss.pgh.pa.us 689 : 247900 : relation->rd_att->constr = constr;
690 : :
691 [ + + ]: 247900 : if (ndef > 0) /* DEFAULTs */
1804 692 : 19444 : AttrDefaultFetch(relation, ndef);
693 : : else
8790 694 : 228456 : constr->num_defval = 0;
695 : :
2909 andrew@dunslane.net 696 : 247900 : constr->missing = attrmiss;
697 : :
698 : : /* CHECK and NOT NULLs */
342 alvherre@alvh.no-ip. 699 [ + + ]: 247900 : if (relation->rd_rel->relchecks > 0 ||
700 [ + + + + ]: 240765 : (!is_catalog && constr->has_not_null))
701 : 91073 : CheckNNConstraintFetch(relation);
702 : :
703 : : /*
704 : : * Any not-null constraint that wasn't marked invalid by
705 : : * CheckNNConstraintFetch must necessarily be valid; make it so in the
706 : : * CompactAttribute array.
707 : : */
708 [ + + ]: 247900 : if (!is_catalog)
709 : : {
710 [ + + ]: 338089 : for (int i = 0; i < relation->rd_rel->relnatts; i++)
711 : : {
712 : : CompactAttribute *attr;
713 : :
714 : 241323 : attr = TupleDescCompactAttr(relation->rd_att, i);
715 : :
716 [ + + ]: 241323 : if (attr->attnullability == ATTNULLABLE_UNKNOWN)
717 : 127671 : attr->attnullability = ATTNULLABLE_VALID;
718 : : else
719 [ + + - + ]: 113652 : Assert(attr->attnullability == ATTNULLABLE_INVALID ||
720 : : attr->attnullability == ATTNULLABLE_UNRESTRICTED);
721 : : }
722 : : }
723 : :
724 [ + + ]: 247900 : if (relation->rd_rel->relchecks == 0)
8790 tgl@sss.pgh.pa.us 725 : 240765 : constr->num_check = 0;
726 : : }
727 : : else
728 : : {
729 : 486460 : pfree(constr);
730 : 486460 : relation->rd_att->constr = NULL;
731 : : }
10841 scrappy@hub.org 732 : 734360 : }
733 : :
734 : : /*
735 : : * RelationBuildRuleLock
736 : : *
737 : : * Form the relation's rewrite rules from information in
738 : : * the pg_rewrite system catalog.
739 : : *
740 : : * Note: The rule parsetrees are potentially very complex node structures.
741 : : * To allow these trees to be freed when the relcache entry is flushed,
742 : : * we make a private memory context to hold the RuleLock information for
743 : : * each relcache entry that has associated rules. The context is used
744 : : * just for rule info, not for any other subsidiary data of the relcache
745 : : * entry, because that keeps the update logic in RelationRebuildRelation()
746 : : * manageable. The other subsidiary data structures are simple enough
747 : : * to be easy to free explicitly, anyway.
748 : : *
749 : : * Note: The relation's reloptions must have been extracted first.
750 : : */
751 : : static void
752 : 20874 : RelationBuildRuleLock(Relation relation)
753 : : {
754 : : MemoryContext rulescxt;
755 : : MemoryContext oldcxt;
756 : : HeapTuple rewrite_tuple;
757 : : Relation rewrite_desc;
758 : : TupleDesc rewrite_tupdesc;
759 : : SysScanDesc rewrite_scan;
760 : : ScanKeyData key;
761 : : RuleLock *rulelock;
762 : : int numlocks;
763 : : RewriteRule **rules;
764 : : int maxlocks;
765 : :
766 : : /*
767 : : * Make the private context. Assume it'll not contain much data.
768 : : */
2910 tgl@sss.pgh.pa.us 769 : 20874 : rulescxt = AllocSetContextCreate(CacheMemoryContext,
770 : : "relation rules",
771 : : ALLOCSET_SMALL_SIZES);
9389 772 : 20874 : relation->rd_rulescxt = rulescxt;
2900 peter_e@gmx.net 773 : 20874 : MemoryContextCopyAndSetIdentifier(rulescxt,
774 : : RelationGetRelationName(relation));
775 : :
776 : : /*
777 : : * allocate an array to hold the rewrite rules (the array is extended if
778 : : * necessary)
779 : : */
10416 bruce@momjian.us 780 : 20874 : maxlocks = 4;
781 : : rules = (RewriteRule **)
9389 tgl@sss.pgh.pa.us 782 : 20874 : MemoryContextAlloc(rulescxt, sizeof(RewriteRule *) * maxlocks);
10416 bruce@momjian.us 783 : 20874 : numlocks = 0;
784 : :
785 : : /*
786 : : * form a scan key
787 : : */
8159 tgl@sss.pgh.pa.us 788 : 20874 : ScanKeyInit(&key,
789 : : Anum_pg_rewrite_ev_class,
790 : : BTEqualStrategyNumber, F_OIDEQ,
791 : : ObjectIdGetDatum(RelationGetRelid(relation)));
792 : :
793 : : /*
794 : : * open pg_rewrite and begin a scan
795 : : *
796 : : * Note: since we scan the rules using RewriteRelRulenameIndexId, we will
797 : : * be reading the rules in name order, except possibly during
798 : : * emergency-recovery operations (ie, IgnoreSystemIndexes). This in turn
799 : : * ensures that rules will be fired in name order.
800 : : */
2610 andres@anarazel.de 801 : 20874 : rewrite_desc = table_open(RewriteRelationId, AccessShareLock);
8731 tgl@sss.pgh.pa.us 802 : 20874 : rewrite_tupdesc = RelationGetDescr(rewrite_desc);
8593 bruce@momjian.us 803 : 20874 : rewrite_scan = systable_beginscan(rewrite_desc,
804 : : RewriteRelRulenameIndexId,
805 : : true, NULL,
806 : : 1, &key);
807 : :
8731 tgl@sss.pgh.pa.us 808 [ + + ]: 41626 : while (HeapTupleIsValid(rewrite_tuple = systable_getnext(rewrite_scan)))
809 : : {
810 : 20752 : Form_pg_rewrite rewrite_form = (Form_pg_rewrite) GETSTRUCT(rewrite_tuple);
811 : : bool isnull;
812 : : Datum rule_datum;
813 : : char *rule_str;
814 : : RewriteRule *rule;
815 : : Oid check_as_user;
816 : :
9389 817 : 20752 : rule = (RewriteRule *) MemoryContextAlloc(rulescxt,
818 : : sizeof(RewriteRule));
819 : :
2672 andres@anarazel.de 820 : 20752 : rule->ruleId = rewrite_form->oid;
821 : :
8732 tgl@sss.pgh.pa.us 822 : 20752 : rule->event = rewrite_form->ev_type - '0';
6936 JanWieck@Yahoo.com 823 : 20752 : rule->enabled = rewrite_form->ev_enabled;
8732 tgl@sss.pgh.pa.us 824 : 20752 : rule->isInstead = rewrite_form->is_instead;
825 : :
826 : : /*
827 : : * Must use heap_getattr to fetch ev_action and ev_qual. Also, the
828 : : * rule strings are often large enough to be toasted. To avoid
829 : : * leaking memory in the caller's context, do the detoasting here so
830 : : * we can free the detoasted version.
831 : : */
7371 832 : 20752 : rule_datum = heap_getattr(rewrite_tuple,
833 : : Anum_pg_rewrite_ev_action,
834 : : rewrite_tupdesc,
835 : : &isnull);
9124 bruce@momjian.us 836 [ - + ]: 20752 : Assert(!isnull);
6564 tgl@sss.pgh.pa.us 837 : 20752 : rule_str = TextDatumGetCString(rule_datum);
9199 838 : 20752 : oldcxt = MemoryContextSwitchTo(rulescxt);
7371 839 : 20752 : rule->actions = (List *) stringToNode(rule_str);
9389 840 : 20752 : MemoryContextSwitchTo(oldcxt);
7371 841 : 20752 : pfree(rule_str);
842 : :
843 : 20752 : rule_datum = heap_getattr(rewrite_tuple,
844 : : Anum_pg_rewrite_ev_qual,
845 : : rewrite_tupdesc,
846 : : &isnull);
9124 bruce@momjian.us 847 [ - + ]: 20752 : Assert(!isnull);
6564 tgl@sss.pgh.pa.us 848 : 20752 : rule_str = TextDatumGetCString(rule_datum);
9199 849 : 20752 : oldcxt = MemoryContextSwitchTo(rulescxt);
7371 850 : 20752 : rule->qual = (Node *) stringToNode(rule_str);
9389 851 : 20752 : MemoryContextSwitchTo(oldcxt);
7371 852 : 20752 : pfree(rule_str);
853 : :
854 : : /*
855 : : * If this is a SELECT rule defining a view, and the view has
856 : : * "security_invoker" set, we must perform all permissions checks on
857 : : * relations referred to by the rule as the invoking user.
858 : : *
859 : : * In all other cases (including non-SELECT rules on security invoker
860 : : * views), perform the permissions checks as the relation owner.
861 : : */
1454 dean.a.rasheed@gmail 862 [ + + ]: 20752 : if (rule->event == CMD_SELECT &&
863 [ + + + + ]: 35854 : relation->rd_rel->relkind == RELKIND_VIEW &&
864 [ - + + + : 16952 : RelationHasSecurityInvoker(relation))
+ + ]
865 : 84 : check_as_user = InvalidOid;
866 : : else
867 : 20668 : check_as_user = relation->rd_rel->relowner;
868 : :
869 : : /*
870 : : * Scan through the rule's actions and set the checkAsUser field on
871 : : * all RTEPermissionInfos. We have to look at the qual as well, in
872 : : * case it contains sublinks.
873 : : *
874 : : * The reason for doing this when the rule is loaded, rather than when
875 : : * it is stored, is that otherwise ALTER TABLE OWNER would have to
876 : : * grovel through stored rules to update checkAsUser fields. Scanning
877 : : * the rule tree during load is relatively cheap (compared to
878 : : * constructing it in the first place), so we do it here.
879 : : */
880 : 20752 : setRuleCheckAsUser((Node *) rule->actions, check_as_user);
881 : 20752 : setRuleCheckAsUser(rule->qual, check_as_user);
882 : :
9540 tgl@sss.pgh.pa.us 883 [ + + ]: 20752 : if (numlocks >= maxlocks)
884 : : {
10416 bruce@momjian.us 885 : 17 : maxlocks *= 2;
886 : : rules = (RewriteRule **)
9389 tgl@sss.pgh.pa.us 887 : 17 : repalloc(rules, sizeof(RewriteRule *) * maxlocks);
888 : : }
9540 889 : 20752 : rules[numlocks++] = rule;
890 : : }
891 : :
892 : : /*
893 : : * end the scan and close the attribute relation
894 : : */
8731 895 : 20874 : systable_endscan(rewrite_scan);
2610 andres@anarazel.de 896 : 20874 : table_close(rewrite_desc, AccessShareLock);
897 : :
898 : : /*
899 : : * there might not be any rules (if relhasrules is out-of-date)
900 : : */
6334 tgl@sss.pgh.pa.us 901 [ + + ]: 20874 : if (numlocks == 0)
902 : : {
903 : 1493 : relation->rd_rules = NULL;
904 : 1493 : relation->rd_rulescxt = NULL;
905 : 1493 : MemoryContextDelete(rulescxt);
906 : 1493 : return;
907 : : }
908 : :
909 : : /*
910 : : * form a RuleLock and insert into relation
911 : : */
9389 912 : 19381 : rulelock = (RuleLock *) MemoryContextAlloc(rulescxt, sizeof(RuleLock));
10416 bruce@momjian.us 913 : 19381 : rulelock->numLocks = numlocks;
914 : 19381 : rulelock->rules = rules;
915 : :
916 : 19381 : relation->rd_rules = rulelock;
917 : : }
918 : :
919 : : /*
920 : : * equalRuleLocks
921 : : *
922 : : * Determine whether two RuleLocks are equivalent
923 : : *
924 : : * Probably this should be in the rules code someplace...
925 : : */
926 : : static bool
9540 tgl@sss.pgh.pa.us 927 : 209193 : equalRuleLocks(RuleLock *rlock1, RuleLock *rlock2)
928 : : {
929 : : int i;
930 : :
931 : : /*
932 : : * As of 7.3 we assume the rule ordering is repeatable, because
933 : : * RelationBuildRuleLock should read 'em in a consistent order. So just
934 : : * compare corresponding slots.
935 : : */
936 [ + + ]: 209193 : if (rlock1 != NULL)
937 : : {
938 [ + + ]: 1381 : if (rlock2 == NULL)
939 : 34 : return false;
940 [ + + ]: 1347 : if (rlock1->numLocks != rlock2->numLocks)
941 : 3 : return false;
942 [ + + ]: 2562 : for (i = 0; i < rlock1->numLocks; i++)
943 : : {
944 : 1365 : RewriteRule *rule1 = rlock1->rules[i];
8731 945 : 1365 : RewriteRule *rule2 = rlock2->rules[i];
946 : :
947 [ - + ]: 1365 : if (rule1->ruleId != rule2->ruleId)
9540 tgl@sss.pgh.pa.us 948 :UBC 0 : return false;
9540 tgl@sss.pgh.pa.us 949 [ - + ]:CBC 1365 : if (rule1->event != rule2->event)
9540 tgl@sss.pgh.pa.us 950 :UBC 0 : return false;
6284 tgl@sss.pgh.pa.us 951 [ + + ]:CBC 1365 : if (rule1->enabled != rule2->enabled)
952 : 23 : return false;
9540 953 [ - + ]: 1342 : if (rule1->isInstead != rule2->isInstead)
9540 tgl@sss.pgh.pa.us 954 :UBC 0 : return false;
9468 bruce@momjian.us 955 [ - + ]:CBC 1342 : if (!equal(rule1->qual, rule2->qual))
9540 tgl@sss.pgh.pa.us 956 :UBC 0 : return false;
9468 bruce@momjian.us 957 [ + + ]:CBC 1342 : if (!equal(rule1->actions, rule2->actions))
9540 tgl@sss.pgh.pa.us 958 : 124 : return false;
959 : : }
960 : : }
961 [ + + ]: 207812 : else if (rlock2 != NULL)
962 : 8882 : return false;
963 : 200127 : return true;
964 : : }
965 : :
966 : : /*
967 : : * equalPolicy
968 : : *
969 : : * Determine whether two policies are equivalent
970 : : */
971 : : static bool
4190 sfrost@snowman.net 972 : 189 : equalPolicy(RowSecurityPolicy *policy1, RowSecurityPolicy *policy2)
973 : : {
974 : : int i;
975 : : Oid *r1,
976 : : *r2;
977 : :
978 [ + - ]: 189 : if (policy1 != NULL)
979 : : {
980 [ - + ]: 189 : if (policy2 == NULL)
4190 sfrost@snowman.net 981 :UBC 0 : return false;
982 : :
4068 tgl@sss.pgh.pa.us 983 [ - + ]:CBC 189 : if (policy1->polcmd != policy2->polcmd)
4190 sfrost@snowman.net 984 :UBC 0 : return false;
4188 sfrost@snowman.net 985 [ - + ]:CBC 189 : if (policy1->hassublinks != policy2->hassublinks)
4190 sfrost@snowman.net 986 :UBC 0 : return false;
3949 bruce@momjian.us 987 [ - + ]:CBC 189 : if (strcmp(policy1->policy_name, policy2->policy_name) != 0)
4190 sfrost@snowman.net 988 :UBC 0 : return false;
4190 sfrost@snowman.net 989 [ - + ]:CBC 189 : if (ARR_DIMS(policy1->roles)[0] != ARR_DIMS(policy2->roles)[0])
4190 sfrost@snowman.net 990 :UBC 0 : return false;
991 : :
4190 sfrost@snowman.net 992 [ - + ]:CBC 189 : r1 = (Oid *) ARR_DATA_PTR(policy1->roles);
993 [ - + ]: 189 : r2 = (Oid *) ARR_DATA_PTR(policy2->roles);
994 : :
995 [ + + ]: 378 : for (i = 0; i < ARR_DIMS(policy1->roles)[0]; i++)
996 : : {
997 [ - + ]: 189 : if (r1[i] != r2[i])
4190 sfrost@snowman.net 998 :UBC 0 : return false;
999 : : }
1000 : :
3985 sfrost@snowman.net 1001 [ - + ]:CBC 189 : if (!equal(policy1->qual, policy2->qual))
4190 sfrost@snowman.net 1002 :UBC 0 : return false;
4190 sfrost@snowman.net 1003 [ - + ]:CBC 189 : if (!equal(policy1->with_check_qual, policy2->with_check_qual))
4190 sfrost@snowman.net 1004 :UBC 0 : return false;
1005 : : }
1006 [ # # ]: 0 : else if (policy2 != NULL)
1007 : 0 : return false;
1008 : :
4190 sfrost@snowman.net 1009 :CBC 189 : return true;
1010 : : }
1011 : :
1012 : : /*
1013 : : * equalRSDesc
1014 : : *
1015 : : * Determine whether two RowSecurityDesc's are equivalent
1016 : : */
1017 : : static bool
1018 : 209193 : equalRSDesc(RowSecurityDesc *rsdesc1, RowSecurityDesc *rsdesc2)
1019 : : {
1020 : : ListCell *lc,
1021 : : *rc;
1022 : :
1023 [ + + + + ]: 209193 : if (rsdesc1 == NULL && rsdesc2 == NULL)
1024 : 208907 : return true;
1025 : :
1026 [ + + + + : 286 : if ((rsdesc1 != NULL && rsdesc2 == NULL) ||
+ + ]
1027 [ + - ]: 169 : (rsdesc1 == NULL && rsdesc2 != NULL))
1028 : 174 : return false;
1029 : :
1030 [ + + ]: 112 : if (list_length(rsdesc1->policies) != list_length(rsdesc2->policies))
1031 : 3 : return false;
1032 : :
1033 : : /* RelationBuildRowSecurity should build policies in order */
1034 [ + + + + : 298 : forboth(lc, rsdesc1->policies, rc, rsdesc2->policies)
+ + + + +
+ + - +
+ ]
1035 : : {
3949 bruce@momjian.us 1036 : 189 : RowSecurityPolicy *l = (RowSecurityPolicy *) lfirst(lc);
1037 : 189 : RowSecurityPolicy *r = (RowSecurityPolicy *) lfirst(rc);
1038 : :
1039 [ - + ]: 189 : if (!equalPolicy(l, r))
4190 sfrost@snowman.net 1040 :UBC 0 : return false;
1041 : : }
1042 : :
4188 sfrost@snowman.net 1043 :CBC 109 : return true;
1044 : : }
1045 : :
1046 : : /*
1047 : : * RelationBuildDesc
1048 : : *
1049 : : * Build a relation descriptor. The caller must hold at least
1050 : : * AccessShareLock on the target relid.
1051 : : *
1052 : : * The new descriptor is inserted into the hash table if insertIt is true.
1053 : : *
1054 : : * Returns NULL if no pg_class row could be found for the given relid
1055 : : * (suggesting we are trying to access a just-deleted relation).
1056 : : * Any other error is reported via elog.
1057 : : */
1058 : : static Relation
5906 tgl@sss.pgh.pa.us 1059 : 734558 : RelationBuildDesc(Oid targetRelId, bool insertIt)
1060 : : {
1061 : : int in_progress_offset;
1062 : : Relation relation;
1063 : : Oid relid;
1064 : : HeapTuple pg_class_tuple;
1065 : : Form_pg_class relp;
1066 : :
1067 : : /*
1068 : : * This function and its subroutines can allocate a good deal of transient
1069 : : * data in CurrentMemoryContext. Traditionally we've just leaked that
1070 : : * data, reasoning that the caller's context is at worst of transaction
1071 : : * scope, and relcache loads shouldn't happen so often that it's essential
1072 : : * to recover transient data before end of statement/transaction. However
1073 : : * that's definitely not true when debug_discard_caches is active, and
1074 : : * perhaps it's not true in other cases.
1075 : : *
1076 : : * When debug_discard_caches is active or when forced to by
1077 : : * RECOVER_RELATION_BUILD_MEMORY=1, arrange to allocate the junk in a
1078 : : * temporary context that we'll free before returning. Make it a child of
1079 : : * caller's context so that it will get cleaned up appropriately if we
1080 : : * error out partway through.
1081 : : */
1082 : : #ifdef MAYBE_RECOVER_RELATION_BUILD_MEMORY
1894 peter@eisentraut.org 1083 : 734558 : MemoryContext tmpcxt = NULL;
1084 : 734558 : MemoryContext oldcxt = NULL;
1085 : :
1706 tgl@sss.pgh.pa.us 1086 [ - + ]: 734558 : if (RECOVER_RELATION_BUILD_MEMORY || debug_discard_caches > 0)
1087 : : {
1894 peter@eisentraut.org 1088 :UBC 0 : tmpcxt = AllocSetContextCreate(CurrentMemoryContext,
1089 : : "RelationBuildDesc workspace",
1090 : : ALLOCSET_DEFAULT_SIZES);
1091 : 0 : oldcxt = MemoryContextSwitchTo(tmpcxt);
1092 : : }
1093 : : #endif
1094 : :
1095 : : /* Register to catch invalidation messages */
1604 noah@leadboat.com 1096 [ + + ]:CBC 734558 : if (in_progress_list_len >= in_progress_list_maxlen)
1097 : : {
1098 : : int allocsize;
1099 : :
1100 : 15 : allocsize = in_progress_list_maxlen * 2;
1101 : 15 : in_progress_list = repalloc(in_progress_list,
1102 : : allocsize * sizeof(*in_progress_list));
1103 : 15 : in_progress_list_maxlen = allocsize;
1104 : : }
1105 : 734558 : in_progress_offset = in_progress_list_len++;
1106 : 734558 : in_progress_list[in_progress_offset].reloid = targetRelId;
1107 : 734570 : retry:
1108 : 734570 : in_progress_list[in_progress_offset].invalidated = false;
1109 : :
1110 : : /*
1111 : : * find the tuple in pg_class corresponding to the given relation id
1112 : : */
4395 rhaas@postgresql.org 1113 : 734570 : pg_class_tuple = ScanPgRelation(targetRelId, true, false);
1114 : :
1115 : : /*
1116 : : * if no such tuple exists, return NULL
1117 : : */
10416 bruce@momjian.us 1118 [ + + ]: 734570 : if (!HeapTupleIsValid(pg_class_tuple))
1119 : : {
1120 : : #ifdef MAYBE_RECOVER_RELATION_BUILD_MEMORY
1894 peter@eisentraut.org 1121 [ - + ]: 207 : if (tmpcxt)
1122 : : {
1123 : : /* Return to caller's context, and blow away the temporary context */
1894 peter@eisentraut.org 1124 :UBC 0 : MemoryContextSwitchTo(oldcxt);
1125 : 0 : MemoryContextDelete(tmpcxt);
1126 : : }
1127 : : #endif
1604 noah@leadboat.com 1128 [ - + ]:CBC 207 : Assert(in_progress_offset + 1 == in_progress_list_len);
1129 : 207 : in_progress_list_len--;
10416 bruce@momjian.us 1130 : 207 : return NULL;
1131 : : }
1132 : :
1133 : : /*
1134 : : * get information from the pg_class_tuple
1135 : : */
1136 : 734363 : relp = (Form_pg_class) GETSTRUCT(pg_class_tuple);
2672 andres@anarazel.de 1137 : 734363 : relid = relp->oid;
5880 tgl@sss.pgh.pa.us 1138 [ - + ]: 734363 : Assert(relid == targetRelId);
1139 : :
1140 : : /*
1141 : : * allocate storage for the relation descriptor, and copy pg_class_tuple
1142 : : * to relation->rd_rel.
1143 : : */
5906 1144 : 734363 : relation = AllocateRelationDesc(relp);
1145 : :
1146 : : /*
1147 : : * initialize the relation's relation id (relation->rd_id)
1148 : : */
10070 bruce@momjian.us 1149 : 734363 : RelationGetRelid(relation) = relid;
1150 : :
1151 : : /*
1152 : : * Normal relations are not nailed into the cache. Since we don't flush
1153 : : * new relations, it won't be new. It could be temp though.
1154 : : */
7911 tgl@sss.pgh.pa.us 1155 : 734363 : relation->rd_refcnt = 0;
7869 1156 : 734363 : relation->rd_isnailed = false;
7850 1157 : 734363 : relation->rd_createSubid = InvalidSubTransactionId;
1348 rhaas@postgresql.org 1158 : 734363 : relation->rd_newRelfilelocatorSubid = InvalidSubTransactionId;
1159 : 734363 : relation->rd_firstRelfilelocatorSubid = InvalidSubTransactionId;
2171 noah@leadboat.com 1160 : 734363 : relation->rd_droppedSubid = InvalidSubTransactionId;
5571 rhaas@postgresql.org 1161 [ + + - ]: 734363 : switch (relation->rd_rel->relpersistence)
1162 : : {
5555 1163 : 718927 : case RELPERSISTENCE_UNLOGGED:
1164 : : case RELPERSISTENCE_PERMANENT:
742 heikki.linnakangas@i 1165 : 718927 : relation->rd_backend = INVALID_PROC_NUMBER;
4836 tgl@sss.pgh.pa.us 1166 : 718927 : relation->rd_islocaltemp = false;
5571 rhaas@postgresql.org 1167 : 718927 : break;
1168 : 15436 : case RELPERSISTENCE_TEMP:
4220 bruce@momjian.us 1169 [ + + ]: 15436 : if (isTempOrTempToastNamespace(relation->rd_rel->relnamespace))
1170 : : {
742 heikki.linnakangas@i 1171 [ + - ]: 15418 : relation->rd_backend = ProcNumberForTempRelations();
4836 tgl@sss.pgh.pa.us 1172 : 15418 : relation->rd_islocaltemp = true;
1173 : : }
1174 : : else
1175 : : {
1176 : : /*
1177 : : * If it's a temp table, but not one of ours, we have to use
1178 : : * the slow, grotty method to figure out the owning backend.
1179 : : *
1180 : : * Note: it's possible that rd_backend gets set to
1181 : : * MyProcNumber here, in case we are looking at a pg_class
1182 : : * entry left over from a crashed backend that coincidentally
1183 : : * had the same ProcNumber we're using. We should *not*
1184 : : * consider such a table to be "ours"; this is why we need the
1185 : : * separate rd_islocaltemp flag. The pg_class entry will get
1186 : : * flushed if/when we clean out the corresponding temp table
1187 : : * namespace in preparation for using it.
1188 : : */
5571 rhaas@postgresql.org 1189 : 18 : relation->rd_backend =
742 heikki.linnakangas@i 1190 : 18 : GetTempNamespaceProcNumber(relation->rd_rel->relnamespace);
1191 [ - + ]: 18 : Assert(relation->rd_backend != INVALID_PROC_NUMBER);
4836 tgl@sss.pgh.pa.us 1192 : 18 : relation->rd_islocaltemp = false;
1193 : : }
5571 rhaas@postgresql.org 1194 : 15436 : break;
5571 rhaas@postgresql.org 1195 :UBC 0 : default:
1196 [ # # ]: 0 : elog(ERROR, "invalid relpersistence: %c",
1197 : : relation->rd_rel->relpersistence);
1198 : : break;
1199 : : }
1200 : :
1201 : : /*
1202 : : * initialize the tuple descriptor (relation->rd_att).
1203 : : */
7640 tgl@sss.pgh.pa.us 1204 :CBC 734363 : RelationBuildTupleDesc(relation);
1205 : :
1206 : : /* foreign key data is not loaded till asked for */
3557 1207 : 734360 : relation->rd_fkeylist = NIL;
1208 : 734360 : relation->rd_fkeyvalid = false;
1209 : :
1210 : : /* partitioning data is not loaded till asked for */
2272 1211 : 734360 : relation->rd_partkey = NULL;
1212 : 734360 : relation->rd_partkeycxt = NULL;
1213 : 734360 : relation->rd_partdesc = NULL;
1782 alvherre@alvh.no-ip. 1214 : 734360 : relation->rd_partdesc_nodetached = NULL;
1215 : 734360 : relation->rd_partdesc_nodetached_xmin = InvalidTransactionId;
2272 tgl@sss.pgh.pa.us 1216 : 734360 : relation->rd_pdcxt = NULL;
1782 alvherre@alvh.no-ip. 1217 : 734360 : relation->rd_pddcxt = NULL;
2528 tgl@sss.pgh.pa.us 1218 : 734360 : relation->rd_partcheck = NIL;
1219 : 734360 : relation->rd_partcheckvalid = false;
1220 : 734360 : relation->rd_partcheckcxt = NULL;
1221 : :
1222 : : /*
1223 : : * initialize access method information
1224 : : */
1563 peter@eisentraut.org 1225 [ + + ]: 734360 : if (relation->rd_rel->relkind == RELKIND_INDEX ||
1226 [ + + ]: 450239 : relation->rd_rel->relkind == RELKIND_PARTITIONED_INDEX)
1227 : 287651 : RelationInitIndexAccessInfo(relation);
1228 [ + + + + : 446709 : else if (RELKIND_HAS_TABLE_AM(relation->rd_rel->relkind) ||
+ + ]
1229 [ + + ]: 69571 : relation->rd_rel->relkind == RELKIND_SEQUENCE)
1230 : 380280 : RelationInitTableAccessMethod(relation);
720 alvherre@alvh.no-ip. 1231 [ + + ]: 66429 : else if (relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1232 : : {
1233 : : /*
1234 : : * Do nothing: access methods are a setting that partitions can
1235 : : * inherit.
1236 : : */
1237 : : }
1238 : : else
1563 peter@eisentraut.org 1239 [ - + ]: 32923 : Assert(relation->rd_rel->relam == InvalidOid);
1240 : :
1241 : : /* extract reloptions if any */
7195 tgl@sss.pgh.pa.us 1242 : 734357 : RelationParseRelOptions(relation, pg_class_tuple);
1243 : :
1244 : : /*
1245 : : * Fetch rules and triggers that affect this relation.
1246 : : *
1247 : : * Note that RelationBuildRuleLock() relies on this being done after
1248 : : * extracting the relation's reloptions.
1249 : : */
1454 dean.a.rasheed@gmail 1250 [ + + ]: 734357 : if (relation->rd_rel->relhasrules)
1251 : 20874 : RelationBuildRuleLock(relation);
1252 : : else
1253 : : {
1254 : 713483 : relation->rd_rules = NULL;
1255 : 713483 : relation->rd_rulescxt = NULL;
1256 : : }
1257 : :
1258 [ + + ]: 734357 : if (relation->rd_rel->relhastriggers)
1259 : 32823 : RelationBuildTriggers(relation);
1260 : : else
1261 : 701534 : relation->trigdesc = NULL;
1262 : :
1263 [ + + ]: 734357 : if (relation->rd_rel->relrowsecurity)
1264 : 1225 : RelationBuildRowSecurity(relation);
1265 : : else
1266 : 733132 : relation->rd_rsdesc = NULL;
1267 : :
1268 : : /*
1269 : : * initialize the relation lock manager information
1270 : : */
3189 tgl@sss.pgh.pa.us 1271 : 734357 : RelationInitLockInfo(relation); /* see lmgr.c */
1272 : :
1273 : : /*
1274 : : * initialize physical addressing information for the relation
1275 : : */
7940 1276 : 734357 : RelationInitPhysicalAddr(relation);
1277 : :
1278 : : /* make sure relation is marked as having no open file yet */
8069 1279 : 734357 : relation->rd_smgr = NULL;
1280 : :
1281 : : /*
1282 : : * now we can free the memory allocated for pg_class_tuple
1283 : : */
7196 bruce@momjian.us 1284 : 734357 : heap_freetuple(pg_class_tuple);
1285 : :
1286 : : /*
1287 : : * If an invalidation arrived mid-build, start over. Between here and the
1288 : : * end of this function, don't add code that does or reasonably could read
1289 : : * system catalogs. That range must be free from invalidation processing
1290 : : * for the !insertIt case. For the insertIt case, RelationCacheInsert()
1291 : : * will enroll this relation in ordinary relcache invalidation processing,
1292 : : */
1604 noah@leadboat.com 1293 [ + + ]: 734357 : if (in_progress_list[in_progress_offset].invalidated)
1294 : : {
1295 : 12 : RelationDestroyRelation(relation, false);
1296 : 12 : goto retry;
1297 : : }
1298 [ - + ]: 734345 : Assert(in_progress_offset + 1 == in_progress_list_len);
1299 : 734345 : in_progress_list_len--;
1300 : :
1301 : : /*
1302 : : * Insert newly created relation into relcache hash table, if requested.
1303 : : *
1304 : : * There is one scenario in which we might find a hashtable entry already
1305 : : * present, even though our caller failed to find it: if the relation is a
1306 : : * system catalog or index that's used during relcache load, we might have
1307 : : * recursively created the same relcache entry during the preceding steps.
1308 : : * So allow RelationCacheInsert to delete any already-present relcache
1309 : : * entry for the same OID. The already-present entry should have refcount
1310 : : * zero (else somebody forgot to close it); in the event that it doesn't,
1311 : : * we'll elog a WARNING and leak the already-present entry.
1312 : : */
5906 tgl@sss.pgh.pa.us 1313 [ + + ]: 734345 : if (insertIt)
4319 1314 [ - + - - : 525152 : RelationCacheInsert(relation, true);
- - - - ]
1315 : :
1316 : : /* It's fully valid */
7869 1317 : 734345 : relation->rd_isvalid = true;
1318 : :
1319 : : #ifdef MAYBE_RECOVER_RELATION_BUILD_MEMORY
1894 peter@eisentraut.org 1320 [ - + ]: 734345 : if (tmpcxt)
1321 : : {
1322 : : /* Return to caller's context, and blow away the temporary context */
1894 peter@eisentraut.org 1323 :UBC 0 : MemoryContextSwitchTo(oldcxt);
1324 : 0 : MemoryContextDelete(tmpcxt);
1325 : : }
1326 : : #endif
1327 : :
10416 bruce@momjian.us 1328 :CBC 734345 : return relation;
1329 : : }
1330 : :
1331 : : /*
1332 : : * Initialize the physical addressing info (RelFileLocator) for a relcache entry
1333 : : *
1334 : : * Note: at the physical level, relations in the pg_global tablespace must
1335 : : * be treated as shared, even if relisshared isn't set. Hence we do not
1336 : : * look at relisshared here.
1337 : : */
1338 : : static void
7940 tgl@sss.pgh.pa.us 1339 : 2914782 : RelationInitPhysicalAddr(Relation relation)
1340 : : {
1348 rhaas@postgresql.org 1341 : 2914782 : RelFileNumber oldnumber = relation->rd_locator.relNumber;
1342 : :
1343 : : /* these relations kinds never have storage */
2627 alvherre@alvh.no-ip. 1344 [ + + + + : 2914782 : if (!RELKIND_HAS_STORAGE(relation->rd_rel->relkind))
+ + + + +
+ ]
1345 : 87029 : return;
1346 : :
7940 tgl@sss.pgh.pa.us 1347 [ + + ]: 2827753 : if (relation->rd_rel->reltablespace)
1348 rhaas@postgresql.org 1348 : 449155 : relation->rd_locator.spcOid = relation->rd_rel->reltablespace;
1349 : : else
1350 : 2378598 : relation->rd_locator.spcOid = MyDatabaseTableSpace;
1351 [ + + ]: 2827753 : if (relation->rd_locator.spcOid == GLOBALTABLESPACE_OID)
1352 : 447172 : relation->rd_locator.dbOid = InvalidOid;
1353 : : else
1354 : 2380581 : relation->rd_locator.dbOid = MyDatabaseId;
1355 : :
5880 tgl@sss.pgh.pa.us 1356 [ + + ]: 2827753 : if (relation->rd_rel->relfilenode)
1357 : : {
1358 : : /*
1359 : : * Even if we are using a decoding snapshot that doesn't represent the
1360 : : * current state of the catalog we need to make sure the filenode
1361 : : * points to the current file since the older file will be gone (or
1362 : : * truncated). The new file will still contain older rows so lookups
1363 : : * in them will work correctly. This wouldn't work correctly if
1364 : : * rewrites were allowed to change the schema in an incompatible way,
1365 : : * but those are prevented both on catalog tables and on user tables
1366 : : * declared as additional catalog tables.
1367 : : */
4395 rhaas@postgresql.org 1368 [ + + ]: 2091661 : if (HistoricSnapshotActive()
1369 [ - + - - : 2634 : && RelationIsAccessibleInLogicalDecoding(relation)
+ - - + -
- - - + +
+ + - + -
- + + ]
1370 [ + - ]: 1752 : && IsTransactionState())
1371 : : {
1372 : : HeapTuple phys_tuple;
1373 : : Form_pg_class physrel;
1374 : :
1375 : 1752 : phys_tuple = ScanPgRelation(RelationGetRelid(relation),
3189 tgl@sss.pgh.pa.us 1376 : 1752 : RelationGetRelid(relation) != ClassOidIndexId,
1377 : : true);
4395 rhaas@postgresql.org 1378 [ - + ]: 1752 : if (!HeapTupleIsValid(phys_tuple))
4395 rhaas@postgresql.org 1379 [ # # ]:UBC 0 : elog(ERROR, "could not find pg_class entry for %u",
1380 : : RelationGetRelid(relation));
4395 rhaas@postgresql.org 1381 :CBC 1752 : physrel = (Form_pg_class) GETSTRUCT(phys_tuple);
1382 : :
1383 : 1752 : relation->rd_rel->reltablespace = physrel->reltablespace;
1384 : 1752 : relation->rd_rel->relfilenode = physrel->relfilenode;
1385 : 1752 : heap_freetuple(phys_tuple);
1386 : : }
1387 : :
1348 1388 : 2091661 : relation->rd_locator.relNumber = relation->rd_rel->relfilenode;
1389 : : }
1390 : : else
1391 : : {
1392 : : /* Consult the relation mapper */
1393 : 736092 : relation->rd_locator.relNumber =
1394 : 736092 : RelationMapOidToFilenumber(relation->rd_id,
1395 : 736092 : relation->rd_rel->relisshared);
1396 [ - + ]: 736092 : if (!RelFileNumberIsValid(relation->rd_locator.relNumber))
5880 tgl@sss.pgh.pa.us 1397 [ # # ]:UBC 0 : elog(ERROR, "could not find relation mapping for relation \"%s\", OID %u",
1398 : : RelationGetRelationName(relation), relation->rd_id);
1399 : : }
1400 : :
1401 : : /*
1402 : : * For RelationNeedsWAL() to answer correctly on parallel workers, restore
1403 : : * rd_firstRelfilelocatorSubid. No subtransactions start or end while in
1404 : : * parallel mode, so the specific SubTransactionId does not matter.
1405 : : */
1348 rhaas@postgresql.org 1406 [ + + + + ]:CBC 2827753 : if (IsParallelWorker() && oldnumber != relation->rd_locator.relNumber)
1407 : : {
1408 [ + + ]: 33592 : if (RelFileLocatorSkippingWAL(relation->rd_locator))
1409 : 3 : relation->rd_firstRelfilelocatorSubid = TopSubTransactionId;
1410 : : else
1411 : 33589 : relation->rd_firstRelfilelocatorSubid = InvalidSubTransactionId;
1412 : : }
1413 : : }
1414 : :
1415 : : /*
1416 : : * Fill in the IndexAmRoutine for an index relation.
1417 : : *
1418 : : * relation's rd_amhandler and rd_indexcxt must be valid already.
1419 : : */
1420 : : static void
3710 tgl@sss.pgh.pa.us 1421 : 1494704 : InitIndexAmRoutine(Relation relation)
1422 : : {
1423 : : MemoryContext oldctx;
1424 : :
1425 : : /*
1426 : : * We formerly specified that the amhandler should return a palloc'd
1427 : : * struct. That's now deprecated in favor of returning a pointer to a
1428 : : * static struct, but to avoid completely breaking old external AMs, run
1429 : : * the amhandler in the relation's rd_indexcxt.
1430 : : */
75 tgl@sss.pgh.pa.us 1431 :GNC 1494704 : oldctx = MemoryContextSwitchTo(relation->rd_indexcxt);
1432 : 1494704 : relation->rd_indam = GetIndexAmRoutine(relation->rd_amhandler);
1433 : 1494704 : MemoryContextSwitchTo(oldctx);
3710 tgl@sss.pgh.pa.us 1434 :CBC 1494704 : }
1435 : :
1436 : : /*
1437 : : * Initialize index-access-method support data for an index relation
1438 : : */
1439 : : void
8926 1440 : 295760 : RelationInitIndexAccessInfo(Relation relation)
1441 : : {
1442 : : HeapTuple tuple;
1443 : : Form_pg_am aform;
1444 : : Datum indcollDatum;
1445 : : Datum indclassDatum;
1446 : : Datum indoptionDatum;
1447 : : bool isnull;
1448 : : oidvector *indcoll;
1449 : : oidvector *indclass;
1450 : : int2vector *indoption;
1451 : : MemoryContext indexcxt;
1452 : : MemoryContext oldcontext;
1453 : : int indnatts;
1454 : : int indnkeyatts;
1455 : : uint16 amsupport;
1456 : :
1457 : : /*
1458 : : * Make a copy of the pg_index entry for the index. Since pg_index
1459 : : * contains variable-length and possibly-null fields, we have to do this
1460 : : * honestly rather than just treating it as a Form_pg_index struct.
1461 : : */
5873 rhaas@postgresql.org 1462 : 295760 : tuple = SearchSysCache1(INDEXRELID,
1463 : : ObjectIdGetDatum(RelationGetRelid(relation)));
8790 tgl@sss.pgh.pa.us 1464 [ - + ]: 295760 : if (!HeapTupleIsValid(tuple))
8269 tgl@sss.pgh.pa.us 1465 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for index %u",
1466 : : RelationGetRelid(relation));
8327 tgl@sss.pgh.pa.us 1467 :CBC 295760 : oldcontext = MemoryContextSwitchTo(CacheMemoryContext);
1468 : 295760 : relation->rd_indextuple = heap_copytuple(tuple);
1469 : 295760 : relation->rd_index = (Form_pg_index) GETSTRUCT(relation->rd_indextuple);
1470 : 295760 : MemoryContextSwitchTo(oldcontext);
8790 1471 : 295760 : ReleaseSysCache(tuple);
1472 : :
1473 : : /*
1474 : : * Look up the index's access method, save the OID of its handler function
1475 : : */
1563 peter@eisentraut.org 1476 [ - + ]: 295760 : Assert(relation->rd_rel->relam != InvalidOid);
5873 rhaas@postgresql.org 1477 : 295760 : tuple = SearchSysCache1(AMOID, ObjectIdGetDatum(relation->rd_rel->relam));
8790 tgl@sss.pgh.pa.us 1478 [ - + ]: 295760 : if (!HeapTupleIsValid(tuple))
8269 tgl@sss.pgh.pa.us 1479 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for access method %u",
1480 : : relation->rd_rel->relam);
3710 tgl@sss.pgh.pa.us 1481 :CBC 295760 : aform = (Form_pg_am) GETSTRUCT(tuple);
1482 : 295760 : relation->rd_amhandler = aform->amhandler;
8790 1483 : 295760 : ReleaseSysCache(tuple);
1484 : :
2899 teodor@sigaev.ru 1485 : 295760 : indnatts = RelationGetNumberOfAttributes(relation);
1486 [ - + ]: 295760 : if (indnatts != IndexRelationGetNumberOfAttributes(relation))
8269 tgl@sss.pgh.pa.us 1487 [ # # ]:UBC 0 : elog(ERROR, "relnatts disagrees with indnatts for index %u",
1488 : : RelationGetRelid(relation));
2899 teodor@sigaev.ru 1489 :CBC 295760 : indnkeyatts = IndexRelationGetNumberOfKeyAttributes(relation);
1490 : :
1491 : : /*
1492 : : * Make the private context to hold index access info. The reason we need
1493 : : * a context, and not just a couple of pallocs, is so that we won't leak
1494 : : * any subsidiary info attached to fmgr lookup records.
1495 : : */
2910 tgl@sss.pgh.pa.us 1496 : 295760 : indexcxt = AllocSetContextCreate(CacheMemoryContext,
1497 : : "index info",
1498 : : ALLOCSET_SMALL_SIZES);
8926 1499 : 295760 : relation->rd_indexcxt = indexcxt;
2900 peter_e@gmx.net 1500 : 295760 : MemoryContextCopyAndSetIdentifier(indexcxt,
1501 : : RelationGetRelationName(relation));
1502 : :
1503 : : /*
1504 : : * Now we can fetch the index AM's API struct
1505 : : */
3710 tgl@sss.pgh.pa.us 1506 : 295760 : InitIndexAmRoutine(relation);
1507 : :
1508 : : /*
1509 : : * Allocate arrays to hold data. Opclasses are not used for included
1510 : : * columns, so allocate them for indnkeyatts only.
1511 : : */
7022 1512 : 295760 : relation->rd_opfamily = (Oid *)
2899 teodor@sigaev.ru 1513 : 295760 : MemoryContextAllocZero(indexcxt, indnkeyatts * sizeof(Oid));
7022 tgl@sss.pgh.pa.us 1514 : 295760 : relation->rd_opcintype = (Oid *)
2899 teodor@sigaev.ru 1515 : 295760 : MemoryContextAllocZero(indexcxt, indnkeyatts * sizeof(Oid));
1516 : :
2610 andres@anarazel.de 1517 : 295760 : amsupport = relation->rd_indam->amsupport;
9053 tgl@sss.pgh.pa.us 1518 [ + - ]: 295760 : if (amsupport > 0)
1519 : : {
2176 akorotkov@postgresql 1520 : 295760 : int nsupport = indnatts * amsupport;
1521 : :
7022 tgl@sss.pgh.pa.us 1522 : 295760 : relation->rd_support = (RegProcedure *)
8162 1523 : 295760 : MemoryContextAllocZero(indexcxt, nsupport * sizeof(RegProcedure));
7022 1524 : 295760 : relation->rd_supportinfo = (FmgrInfo *)
8162 1525 : 295760 : MemoryContextAllocZero(indexcxt, nsupport * sizeof(FmgrInfo));
1526 : : }
1527 : : else
1528 : : {
7022 tgl@sss.pgh.pa.us 1529 :UBC 0 : relation->rd_support = NULL;
1530 : 0 : relation->rd_supportinfo = NULL;
1531 : : }
1532 : :
5514 peter_e@gmx.net 1533 :CBC 295760 : relation->rd_indcollation = (Oid *)
2894 teodor@sigaev.ru 1534 : 295760 : MemoryContextAllocZero(indexcxt, indnkeyatts * sizeof(Oid));
1535 : :
7005 tgl@sss.pgh.pa.us 1536 : 295760 : relation->rd_indoption = (int16 *)
2894 teodor@sigaev.ru 1537 : 295760 : MemoryContextAllocZero(indexcxt, indnkeyatts * sizeof(int16));
1538 : :
1539 : : /*
1540 : : * indcollation cannot be referenced directly through the C struct,
1541 : : * because it comes after the variable-width indkey field. Must extract
1542 : : * the datum the hard way...
1543 : : */
5514 peter_e@gmx.net 1544 : 295760 : indcollDatum = fastgetattr(relation->rd_indextuple,
1545 : : Anum_pg_index_indcollation,
1546 : : GetPgIndexDescriptor(),
1547 : : &isnull);
1548 [ - + ]: 295760 : Assert(!isnull);
1549 : 295760 : indcoll = (oidvector *) DatumGetPointer(indcollDatum);
2894 teodor@sigaev.ru 1550 : 295760 : memcpy(relation->rd_indcollation, indcoll->values, indnkeyatts * sizeof(Oid));
1551 : :
1552 : : /*
1553 : : * indclass cannot be referenced directly through the C struct, because it
1554 : : * comes after the variable-width indkey field. Must extract the datum
1555 : : * the hard way...
1556 : : */
7022 tgl@sss.pgh.pa.us 1557 : 295760 : indclassDatum = fastgetattr(relation->rd_indextuple,
1558 : : Anum_pg_index_indclass,
1559 : : GetPgIndexDescriptor(),
1560 : : &isnull);
1561 [ - + ]: 295760 : Assert(!isnull);
1562 : 295760 : indclass = (oidvector *) DatumGetPointer(indclassDatum);
1563 : :
1564 : : /*
1565 : : * Fill the support procedure OID array, as well as the info about
1566 : : * opfamilies and opclass input types. (aminfo and supportinfo are left
1567 : : * as zeroes, and are filled on-the-fly when used)
1568 : : */
5585 1569 : 295760 : IndexSupportInitialize(indclass, relation->rd_support,
1570 : : relation->rd_opfamily, relation->rd_opcintype,
1571 : : amsupport, indnkeyatts);
1572 : :
1573 : : /*
1574 : : * Similarly extract indoption and copy it to the cache entry
1575 : : */
7005 1576 : 295760 : indoptionDatum = fastgetattr(relation->rd_indextuple,
1577 : : Anum_pg_index_indoption,
1578 : : GetPgIndexDescriptor(),
1579 : : &isnull);
1580 [ - + ]: 295760 : Assert(!isnull);
1581 : 295760 : indoption = (int2vector *) DatumGetPointer(indoptionDatum);
2894 teodor@sigaev.ru 1582 : 295760 : memcpy(relation->rd_indoption, indoption->values, indnkeyatts * sizeof(int16));
1583 : :
2176 akorotkov@postgresql 1584 : 295760 : (void) RelationGetIndexAttOptions(relation, false);
1585 : :
1586 : : /*
1587 : : * expressions, predicate, exclusion caches will be filled later
1588 : : */
8327 tgl@sss.pgh.pa.us 1589 : 295757 : relation->rd_indexprs = NIL;
1590 : 295757 : relation->rd_indpred = NIL;
5942 1591 : 295757 : relation->rd_exclops = NULL;
1592 : 295757 : relation->rd_exclprocs = NULL;
1593 : 295757 : relation->rd_exclstrats = NULL;
7264 1594 : 295757 : relation->rd_amcache = NULL;
10841 scrappy@hub.org 1595 : 295757 : }
1596 : :
1597 : : /*
1598 : : * IndexSupportInitialize
1599 : : * Initializes an index's cached opclass information,
1600 : : * given the index's pg_index.indclass entry.
1601 : : *
1602 : : * Data is returned into *indexSupport, *opFamily, and *opcInType,
1603 : : * which are arrays allocated by the caller.
1604 : : *
1605 : : * The caller also passes maxSupportNumber and maxAttributeNumber, since these
1606 : : * indicate the size of the arrays it has allocated --- but in practice these
1607 : : * numbers must always match those obtainable from the system catalog entries
1608 : : * for the index and access method.
1609 : : */
1610 : : static void
7656 tgl@sss.pgh.pa.us 1611 : 295760 : IndexSupportInitialize(oidvector *indclass,
1612 : : RegProcedure *indexSupport,
1613 : : Oid *opFamily,
1614 : : Oid *opcInType,
1615 : : StrategyNumber maxSupportNumber,
1616 : : AttrNumber maxAttributeNumber)
1617 : : {
1618 : : int attIndex;
1619 : :
8790 1620 [ + + ]: 796204 : for (attIndex = 0; attIndex < maxAttributeNumber; attIndex++)
1621 : : {
1622 : : OpClassCacheEnt *opcentry;
1623 : :
7656 1624 [ - + ]: 500444 : if (!OidIsValid(indclass->values[attIndex]))
8269 tgl@sss.pgh.pa.us 1625 [ # # ]:UBC 0 : elog(ERROR, "bogus pg_index tuple");
1626 : :
1627 : : /* look up the info for this opclass, using a cache */
7656 tgl@sss.pgh.pa.us 1628 :CBC 500444 : opcentry = LookupOpclassInfo(indclass->values[attIndex],
1629 : : maxSupportNumber);
1630 : :
1631 : : /* copy cached data into relcache entry */
7022 1632 : 500444 : opFamily[attIndex] = opcentry->opcfamily;
1633 : 500444 : opcInType[attIndex] = opcentry->opcintype;
8790 1634 [ + - ]: 500444 : if (maxSupportNumber > 0)
2176 akorotkov@postgresql 1635 : 500444 : memcpy(&indexSupport[attIndex * maxSupportNumber],
8162 tgl@sss.pgh.pa.us 1636 : 500444 : opcentry->supportProcs,
1637 : : maxSupportNumber * sizeof(RegProcedure));
1638 : : }
8790 1639 : 295760 : }
1640 : :
1641 : : /*
1642 : : * LookupOpclassInfo
1643 : : *
1644 : : * This routine maintains a per-opclass cache of the information needed
1645 : : * by IndexSupportInitialize(). This is more efficient than relying on
1646 : : * the catalog cache, because we can load all the info about a particular
1647 : : * opclass in a single indexscan of pg_amproc.
1648 : : *
1649 : : * The information from pg_am about expected range of support function
1650 : : * numbers is passed in, rather than being looked up, mainly because the
1651 : : * caller will have it already.
1652 : : *
1653 : : * Note there is no provision for flushing the cache. This is OK at the
1654 : : * moment because there is no way to ALTER any interesting properties of an
1655 : : * existing opclass --- all you can do is drop it, which will result in
1656 : : * a useless but harmless dead entry in the cache. To support altering
1657 : : * opclass membership (not the same as opfamily membership!), we'd need to
1658 : : * be able to flush this cache as well as the contents of relcache entries
1659 : : * for indexes.
1660 : : */
1661 : : static OpClassCacheEnt *
1662 : 500444 : LookupOpclassInfo(Oid operatorClassOid,
1663 : : StrategyNumber numSupport)
1664 : : {
1665 : : OpClassCacheEnt *opcentry;
1666 : : bool found;
1667 : : Relation rel;
1668 : : SysScanDesc scan;
1669 : : ScanKeyData skey[3];
1670 : : HeapTuple htup;
1671 : : bool indexOK;
1672 : :
1673 [ + + ]: 500444 : if (OpClassCache == NULL)
1674 : : {
1675 : : /* First time through: initialize the opclass cache */
1676 : : HASHCTL ctl;
1677 : :
1678 : : /* Also make sure CacheMemoryContext exists */
1714 1679 [ - + ]: 15450 : if (!CacheMemoryContext)
1714 tgl@sss.pgh.pa.us 1680 :UBC 0 : CreateCacheMemoryContext();
1681 : :
8790 tgl@sss.pgh.pa.us 1682 :CBC 15450 : ctl.keysize = sizeof(Oid);
1683 : 15450 : ctl.entrysize = sizeof(OpClassCacheEnt);
1684 : 15450 : OpClassCache = hash_create("Operator class cache", 64,
1685 : : &ctl, HASH_ELEM | HASH_BLOBS);
1686 : : }
1687 : :
1688 : 500444 : opcentry = (OpClassCacheEnt *) hash_search(OpClassCache,
1689 : : &operatorClassOid,
1690 : : HASH_ENTER, &found);
1691 : :
6682 1692 [ + + ]: 500444 : if (!found)
1693 : : {
1694 : : /* Initialize new entry */
1695 : 45929 : opcentry->valid = false; /* until known OK */
1696 : 45929 : opcentry->numSupport = numSupport;
1714 1697 : 45929 : opcentry->supportProcs = NULL; /* filled below */
1698 : : }
1699 : : else
1700 : : {
8790 1701 [ - + ]: 454515 : Assert(numSupport == opcentry->numSupport);
1702 : : }
1703 : :
1704 : : /*
1705 : : * When aggressively testing cache-flush hazards, we disable the operator
1706 : : * class cache and force reloading of the info on each call. This models
1707 : : * no real-world behavior, since the cache entries are never invalidated
1708 : : * otherwise. However it can be helpful for detecting bugs in the cache
1709 : : * loading logic itself, such as reliance on a non-nailed index. Given
1710 : : * the limited use-case and the fact that this adds a great deal of
1711 : : * expense, we enable it only for high values of debug_discard_caches.
1712 : : */
1713 : : #ifdef DISCARD_CACHES_ENABLED
1706 1714 [ - + ]: 500444 : if (debug_discard_caches > 2)
1894 peter@eisentraut.org 1715 :UBC 0 : opcentry->valid = false;
1716 : : #endif
1717 : :
6682 tgl@sss.pgh.pa.us 1718 [ + + ]:CBC 500444 : if (opcentry->valid)
1719 : 454515 : return opcentry;
1720 : :
1721 : : /*
1722 : : * Need to fill in new entry. First allocate space, unless we already did
1723 : : * so in some previous attempt.
1724 : : */
1714 1725 [ + - + - ]: 45929 : if (opcentry->supportProcs == NULL && numSupport > 0)
1726 : 45929 : opcentry->supportProcs = (RegProcedure *)
1727 : 45929 : MemoryContextAllocZero(CacheMemoryContext,
1728 : : numSupport * sizeof(RegProcedure));
1729 : :
1730 : : /*
1731 : : * To avoid infinite recursion during startup, force heap scans if we're
1732 : : * looking up info for the opclasses used by the indexes we would like to
1733 : : * reference here.
1734 : : */
8790 1735 [ + + ]: 51196 : indexOK = criticalRelcachesBuilt ||
1736 [ + + ]: 5267 : (operatorClassOid != OID_BTREE_OPS_OID &&
1737 [ + + ]: 3613 : operatorClassOid != INT2_BTREE_OPS_OID);
1738 : :
1739 : : /*
1740 : : * We have to fetch the pg_opclass row to determine its opfamily and
1741 : : * opcintype, which are needed to look up related operators and functions.
1742 : : * It'd be convenient to use the syscache here, but that probably doesn't
1743 : : * work while bootstrapping.
1744 : : */
7022 1745 : 45929 : ScanKeyInit(&skey[0],
1746 : : Anum_pg_opclass_oid,
1747 : : BTEqualStrategyNumber, F_OIDEQ,
1748 : : ObjectIdGetDatum(operatorClassOid));
2610 andres@anarazel.de 1749 : 45929 : rel = table_open(OperatorClassRelationId, AccessShareLock);
7022 tgl@sss.pgh.pa.us 1750 : 45929 : scan = systable_beginscan(rel, OpclassOidIndexId, indexOK,
1751 : : NULL, 1, skey);
1752 : :
1753 [ + - ]: 45929 : if (HeapTupleIsValid(htup = systable_getnext(scan)))
1754 : : {
1755 : 45929 : Form_pg_opclass opclassform = (Form_pg_opclass) GETSTRUCT(htup);
1756 : :
1757 : 45929 : opcentry->opcfamily = opclassform->opcfamily;
1758 : 45929 : opcentry->opcintype = opclassform->opcintype;
1759 : : }
1760 : : else
7022 tgl@sss.pgh.pa.us 1761 [ # # ]:UBC 0 : elog(ERROR, "could not find tuple for opclass %u", operatorClassOid);
1762 : :
7022 tgl@sss.pgh.pa.us 1763 :CBC 45929 : systable_endscan(scan);
2610 andres@anarazel.de 1764 : 45929 : table_close(rel, AccessShareLock);
1765 : :
1766 : : /*
1767 : : * Scan pg_amproc to obtain support procs for the opclass. We only fetch
1768 : : * the default ones (those with lefttype = righttype = opcintype).
1769 : : */
8790 tgl@sss.pgh.pa.us 1770 [ + - ]: 45929 : if (numSupport > 0)
1771 : : {
8159 1772 : 45929 : ScanKeyInit(&skey[0],
1773 : : Anum_pg_amproc_amprocfamily,
1774 : : BTEqualStrategyNumber, F_OIDEQ,
1775 : : ObjectIdGetDatum(opcentry->opcfamily));
1776 : 45929 : ScanKeyInit(&skey[1],
1777 : : Anum_pg_amproc_amproclefttype,
1778 : : BTEqualStrategyNumber, F_OIDEQ,
1779 : : ObjectIdGetDatum(opcentry->opcintype));
7022 1780 : 45929 : ScanKeyInit(&skey[2],
1781 : : Anum_pg_amproc_amprocrighttype,
1782 : : BTEqualStrategyNumber, F_OIDEQ,
1783 : : ObjectIdGetDatum(opcentry->opcintype));
2610 andres@anarazel.de 1784 : 45929 : rel = table_open(AccessMethodProcedureRelationId, AccessShareLock);
7640 tgl@sss.pgh.pa.us 1785 : 45929 : scan = systable_beginscan(rel, AccessMethodProcedureIndexId, indexOK,
1786 : : NULL, 3, skey);
1787 : :
8159 1788 [ + + ]: 220919 : while (HeapTupleIsValid(htup = systable_getnext(scan)))
1789 : : {
8790 1790 : 174990 : Form_pg_amproc amprocform = (Form_pg_amproc) GETSTRUCT(htup);
1791 : :
2176 akorotkov@postgresql 1792 [ + - ]: 174990 : if (amprocform->amprocnum <= 0 ||
8790 tgl@sss.pgh.pa.us 1793 [ - + ]: 174990 : (StrategyNumber) amprocform->amprocnum > numSupport)
8269 tgl@sss.pgh.pa.us 1794 [ # # ]:UBC 0 : elog(ERROR, "invalid amproc number %d for opclass %u",
1795 : : amprocform->amprocnum, operatorClassOid);
1796 : :
2176 akorotkov@postgresql 1797 :CBC 174990 : opcentry->supportProcs[amprocform->amprocnum - 1] =
1798 : 174990 : amprocform->amproc;
1799 : : }
1800 : :
8159 tgl@sss.pgh.pa.us 1801 : 45929 : systable_endscan(scan);
2610 andres@anarazel.de 1802 : 45929 : table_close(rel, AccessShareLock);
1803 : : }
1804 : :
8790 tgl@sss.pgh.pa.us 1805 : 45929 : opcentry->valid = true;
1806 : 45929 : return opcentry;
1807 : : }
1808 : :
1809 : : /*
1810 : : * Fill in the TableAmRoutine for a relation
1811 : : *
1812 : : * relation's rd_amhandler must be valid already.
1813 : : */
1814 : : static void
2566 andres@anarazel.de 1815 : 1123630 : InitTableAmRoutine(Relation relation)
1816 : : {
1817 : 1123630 : relation->rd_tableam = GetTableAmRoutine(relation->rd_amhandler);
1818 : 1123630 : }
1819 : :
1820 : : /*
1821 : : * Initialize table access method support for a table like relation
1822 : : */
1823 : : void
1824 : 1123630 : RelationInitTableAccessMethod(Relation relation)
1825 : : {
1826 : : HeapTuple tuple;
1827 : : Form_pg_am aform;
1828 : :
1829 [ + + ]: 1123630 : if (relation->rd_rel->relkind == RELKIND_SEQUENCE)
1830 : : {
1831 : : /*
1832 : : * Sequences are currently accessed like heap tables, but it doesn't
1833 : : * seem prudent to show that in the catalog. So just overwrite it
1834 : : * here.
1835 : : */
1563 peter@eisentraut.org 1836 [ - + ]: 4097 : Assert(relation->rd_rel->relam == InvalidOid);
1964 tgl@sss.pgh.pa.us 1837 : 4097 : relation->rd_amhandler = F_HEAP_TABLEAM_HANDLER;
1838 : : }
2566 andres@anarazel.de 1839 [ + + ]: 1119533 : else if (IsCatalogRelation(relation))
1840 : : {
1841 : : /*
1842 : : * Avoid doing a syscache lookup for catalog tables.
1843 : : */
1844 [ - + ]: 881246 : Assert(relation->rd_rel->relam == HEAP_TABLE_AM_OID);
1964 tgl@sss.pgh.pa.us 1845 : 881246 : relation->rd_amhandler = F_HEAP_TABLEAM_HANDLER;
1846 : : }
1847 : : else
1848 : : {
1849 : : /*
1850 : : * Look up the table access method, save the OID of its handler
1851 : : * function.
1852 : : */
2566 andres@anarazel.de 1853 [ - + ]: 238287 : Assert(relation->rd_rel->relam != InvalidOid);
1854 : 238287 : tuple = SearchSysCache1(AMOID,
1855 : 238287 : ObjectIdGetDatum(relation->rd_rel->relam));
1856 [ - + ]: 238287 : if (!HeapTupleIsValid(tuple))
2566 andres@anarazel.de 1857 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for access method %u",
1858 : : relation->rd_rel->relam);
2566 andres@anarazel.de 1859 :CBC 238287 : aform = (Form_pg_am) GETSTRUCT(tuple);
1860 : 238287 : relation->rd_amhandler = aform->amhandler;
1861 : 238287 : ReleaseSysCache(tuple);
1862 : : }
1863 : :
1864 : : /*
1865 : : * Now we can fetch the table AM's API struct
1866 : : */
1867 : 1123630 : InitTableAmRoutine(relation);
1868 : 1123630 : }
1869 : :
1870 : : /*
1871 : : * formrdesc
1872 : : *
1873 : : * This is a special cut-down version of RelationBuildDesc(),
1874 : : * used while initializing the relcache.
1875 : : * The relation descriptor is built just from the supplied parameters,
1876 : : * without actually looking at any system table entries. We cheat
1877 : : * quite a lot since we only need to work for a few basic system
1878 : : * catalogs.
1879 : : *
1880 : : * The catalogs this is used for can't have constraints (except attnotnull),
1881 : : * default values, rules, or triggers, since we don't cope with any of that.
1882 : : * (Well, actually, this only matters for properties that need to be valid
1883 : : * during bootstrap or before RelationCacheInitializePhase3 runs, and none of
1884 : : * these properties matter then...)
1885 : : *
1886 : : * NOTE: we assume we are already switched into CacheMemoryContext.
1887 : : */
1888 : : static void
6014 tgl@sss.pgh.pa.us 1889 : 18104 : formrdesc(const char *relationName, Oid relationReltype,
1890 : : bool isshared,
1891 : : int natts, const FormData_pg_attribute *attrs)
1892 : : {
1893 : : Relation relation;
1894 : : int i;
1895 : : bool has_not_null;
1896 : :
1897 : : /*
1898 : : * allocate new relation desc, clear all fields of reldesc
1899 : : */
95 michael@paquier.xyz 1900 :GNC 18104 : relation = palloc0_object(RelationData);
1901 : :
1902 : : /* make sure relation is marked as having no open file yet */
8069 tgl@sss.pgh.pa.us 1903 :CBC 18104 : relation->rd_smgr = NULL;
1904 : :
1905 : : /*
1906 : : * initialize reference count: 1 because it is nailed in cache
1907 : : */
7911 1908 : 18104 : relation->rd_refcnt = 1;
1909 : :
1910 : : /*
1911 : : * all entries built with this routine are nailed-in-cache; none are for
1912 : : * new or temp relations.
1913 : : */
7869 1914 : 18104 : relation->rd_isnailed = true;
7850 1915 : 18104 : relation->rd_createSubid = InvalidSubTransactionId;
1348 rhaas@postgresql.org 1916 : 18104 : relation->rd_newRelfilelocatorSubid = InvalidSubTransactionId;
1917 : 18104 : relation->rd_firstRelfilelocatorSubid = InvalidSubTransactionId;
2171 noah@leadboat.com 1918 : 18104 : relation->rd_droppedSubid = InvalidSubTransactionId;
742 heikki.linnakangas@i 1919 : 18104 : relation->rd_backend = INVALID_PROC_NUMBER;
4836 tgl@sss.pgh.pa.us 1920 : 18104 : relation->rd_islocaltemp = false;
1921 : :
1922 : : /*
1923 : : * initialize relation tuple form
1924 : : *
1925 : : * The data we insert here is pretty incomplete/bogus, but it'll serve to
1926 : : * get us launched. RelationCacheInitializePhase3() will read the real
1927 : : * data from pg_class and replace what we've done here. Note in
1928 : : * particular that relowner is left as zero; this cues
1929 : : * RelationCacheInitializePhase3 that the real data isn't there yet.
1930 : : */
8523 bruce@momjian.us 1931 : 18104 : relation->rd_rel = (Form_pg_class) palloc0(CLASS_TUPLE_SIZE);
1932 : :
8755 tgl@sss.pgh.pa.us 1933 : 18104 : namestrcpy(&relation->rd_rel->relname, relationName);
1934 : 18104 : relation->rd_rel->relnamespace = PG_CATALOG_NAMESPACE;
6014 1935 : 18104 : relation->rd_rel->reltype = relationReltype;
1936 : :
1937 : : /*
1938 : : * It's important to distinguish between shared and non-shared relations,
1939 : : * even at bootstrap time, to make sure we know where they are stored.
1940 : : */
6059 1941 : 18104 : relation->rd_rel->relisshared = isshared;
1942 [ + + ]: 18104 : if (isshared)
1943 : 11480 : relation->rd_rel->reltablespace = GLOBALTABLESPACE_OID;
1944 : :
1945 : : /* formrdesc is used only for permanent relations */
5571 rhaas@postgresql.org 1946 : 18104 : relation->rd_rel->relpersistence = RELPERSISTENCE_PERMANENT;
1947 : :
1948 : : /* ... and they're always populated, too */
4696 tgl@sss.pgh.pa.us 1949 : 18104 : relation->rd_rel->relispopulated = true;
1950 : :
4510 rhaas@postgresql.org 1951 : 18104 : relation->rd_rel->relreplident = REPLICA_IDENTITY_NOTHING;
5311 tgl@sss.pgh.pa.us 1952 : 18104 : relation->rd_rel->relpages = 0;
2023 1953 : 18104 : relation->rd_rel->reltuples = -1;
5266 1954 : 18104 : relation->rd_rel->relallvisible = 0;
377 melanieplageman@gmai 1955 : 18104 : relation->rd_rel->relallfrozen = 0;
10416 bruce@momjian.us 1956 : 18104 : relation->rd_rel->relkind = RELKIND_RELATION;
9389 tgl@sss.pgh.pa.us 1957 : 18104 : relation->rd_rel->relnatts = (int16) natts;
1958 : :
1959 : : /*
1960 : : * initialize attribute tuple form
1961 : : *
1962 : : * Unlike the case with the relation tuple, this data had better be right
1963 : : * because it will never be replaced. The data comes from
1964 : : * src/include/catalog/ headers via genbki.pl.
1965 : : */
2672 andres@anarazel.de 1966 : 18104 : relation->rd_att = CreateTemplateTupleDesc(natts);
7212 tgl@sss.pgh.pa.us 1967 : 18104 : relation->rd_att->tdrefcount = 1; /* mark as refcounted */
1968 : :
6014 1969 : 18104 : relation->rd_att->tdtypeid = relationReltype;
2077 1970 : 18104 : relation->rd_att->tdtypmod = -1; /* just to be sure */
1971 : :
1972 : : /*
1973 : : * initialize tuple desc info
1974 : : */
8521 1975 : 18104 : has_not_null = false;
10416 bruce@momjian.us 1976 [ + + ]: 365424 : for (i = 0; i < natts; i++)
1977 : : {
3129 andres@anarazel.de 1978 : 694640 : memcpy(TupleDescAttr(relation->rd_att, i),
6059 tgl@sss.pgh.pa.us 1979 : 347320 : &attrs[i],
1980 : : ATTRIBUTE_FIXED_PART_SIZE);
1981 : 347320 : has_not_null |= attrs[i].attnotnull;
1982 : :
450 drowley@postgresql.o 1983 : 347320 : populate_compact_attribute(relation->rd_att, i);
1984 : : }
1985 : :
1986 : : /* initialize first attribute's attcacheoff, cf RelationBuildTupleDesc */
1987 : 18104 : TupleDescCompactAttr(relation->rd_att, 0)->attcacheoff = 0;
1988 : :
1989 : : /* mark not-null status */
8521 tgl@sss.pgh.pa.us 1990 [ + - ]: 18104 : if (has_not_null)
1991 : : {
95 michael@paquier.xyz 1992 :GNC 18104 : TupleConstr *constr = palloc0_object(TupleConstr);
1993 : :
8521 tgl@sss.pgh.pa.us 1994 :CBC 18104 : constr->has_not_null = true;
1995 : 18104 : relation->rd_att->constr = constr;
1996 : : }
1997 : :
1998 : : /*
1999 : : * initialize relation id from info in att array (my, this is ugly)
2000 : : */
3129 andres@anarazel.de 2001 : 18104 : RelationGetRelid(relation) = TupleDescAttr(relation->rd_att, 0)->attrelid;
2002 : :
2003 : : /*
2004 : : * All relations made with formrdesc are mapped. This is necessarily so
2005 : : * because there is no other way to know what filenumber they currently
2006 : : * have. In bootstrap mode, add them to the initial relation mapper data,
2007 : : * specifying that the initial filenumber is the same as the OID.
2008 : : */
1348 rhaas@postgresql.org 2009 : 18104 : relation->rd_rel->relfilenode = InvalidRelFileNumber;
5880 tgl@sss.pgh.pa.us 2010 [ + + ]: 18104 : if (IsBootstrapProcessingMode())
2011 : 204 : RelationMapUpdateMap(RelationGetRelid(relation),
2012 : : RelationGetRelid(relation),
2013 : : isshared, true);
2014 : :
2015 : : /*
2016 : : * initialize the relation lock manager information
2017 : : */
3189 2018 : 18104 : RelationInitLockInfo(relation); /* see lmgr.c */
2019 : :
2020 : : /*
2021 : : * initialize physical addressing information for the relation
2022 : : */
7940 2023 : 18104 : RelationInitPhysicalAddr(relation);
2024 : :
2025 : : /*
2026 : : * initialize the table am handler
2027 : : */
2566 andres@anarazel.de 2028 : 18104 : relation->rd_rel->relam = HEAP_TABLE_AM_OID;
2029 : 18104 : relation->rd_tableam = GetHeapamTableAmRoutine();
2030 : :
2031 : : /*
2032 : : * initialize the rel-has-index flag, using hardwired knowledge
2033 : : */
7763 tgl@sss.pgh.pa.us 2034 [ + + ]: 18104 : if (IsBootstrapProcessingMode())
2035 : : {
2036 : : /* In bootstrap mode, we have no indexes */
2037 : 204 : relation->rd_rel->relhasindex = false;
2038 : : }
2039 : : else
2040 : : {
2041 : : /* Otherwise, all the rels formrdesc is used for have indexes */
8723 2042 : 17900 : relation->rd_rel->relhasindex = true;
2043 : : }
2044 : :
2045 : : /*
2046 : : * add new reldesc to relcache
2047 : : */
4319 2048 [ - + ]: 18104 : RelationCacheInsert(relation, false);
2049 : :
2050 : : /* It's fully valid */
7869 2051 : 18104 : relation->rd_isvalid = true;
10841 scrappy@hub.org 2052 : 18104 : }
2053 : :
2054 : : #ifdef USE_ASSERT_CHECKING
2055 : : /*
2056 : : * AssertCouldGetRelation
2057 : : *
2058 : : * Check safety of calling RelationIdGetRelation().
2059 : : *
2060 : : * In code that reads catalogs in the event of a cache miss, call this
2061 : : * before checking the cache.
2062 : : */
2063 : : void
332 noah@leadboat.com 2064 : 117215254 : AssertCouldGetRelation(void)
2065 : : {
2066 [ - + ]: 117215254 : Assert(IsTransactionState());
2067 : 117215254 : AssertBufferLocksPermitCatalogRead();
2068 : 117215254 : }
2069 : : #endif
2070 : :
2071 : :
2072 : : /* ----------------------------------------------------------------
2073 : : * Relation Descriptor Lookup Interface
2074 : : * ----------------------------------------------------------------
2075 : : */
2076 : :
2077 : : /*
2078 : : * RelationIdGetRelation
2079 : : *
2080 : : * Lookup a reldesc by OID; make one if not already in cache.
2081 : : *
2082 : : * Returns NULL if no pg_class row could be found for the given relid
2083 : : * (suggesting we are trying to access a just-deleted relation).
2084 : : * Any other error is reported via elog.
2085 : : *
2086 : : * NB: caller should already have at least AccessShareLock on the
2087 : : * relation ID, else there are nasty race conditions.
2088 : : *
2089 : : * NB: relation ref count is incremented, or set to 1 if new entry.
2090 : : * Caller should eventually decrement count. (Usually,
2091 : : * that happens by calling RelationClose().)
2092 : : */
2093 : : Relation
7167 tgl@sss.pgh.pa.us 2094 : 22520361 : RelationIdGetRelation(Oid relationId)
2095 : : {
2096 : : Relation rd;
2097 : :
332 noah@leadboat.com 2098 : 22520361 : AssertCouldGetRelation();
2099 : :
2100 : : /*
2101 : : * first try to find reldesc in the cache
2102 : : */
10416 bruce@momjian.us 2103 [ + + ]: 22520361 : RelationIdCacheLookup(relationId, rd);
2104 : :
2105 [ + + ]: 22520361 : if (RelationIsValid(rd))
2106 : : {
2107 : : /* return NULL for dropped relations */
2171 noah@leadboat.com 2108 [ + + ]: 22013731 : if (rd->rd_droppedSubid != InvalidSubTransactionId)
2109 : : {
2110 [ - + ]: 164 : Assert(!rd->rd_isvalid);
2111 : 164 : return NULL;
2112 : : }
2113 : :
10416 bruce@momjian.us 2114 : 22013567 : RelationIncrementReferenceCount(rd);
2115 : : /* revalidate cache entry if necessary */
7869 tgl@sss.pgh.pa.us 2116 [ + + ]: 22013567 : if (!rd->rd_isvalid)
2117 : : {
500 heikki.linnakangas@i 2118 : 87061 : RelationRebuildRelation(rd);
2119 : :
2120 : : /*
2121 : : * Normally entries need to be valid here, but before the relcache
2122 : : * has been initialized, not enough infrastructure exists to
2123 : : * perform pg_class lookups. The structure of such entries doesn't
2124 : : * change, but we still want to update the rd_rel entry. So
2125 : : * rd_isvalid = false is left in place for a later lookup.
2126 : : */
2833 andres@anarazel.de 2127 [ + + + - : 87055 : Assert(rd->rd_isvalid ||
- + ]
2128 : : (rd->rd_isnailed && !criticalRelcachesBuilt));
2129 : : }
10416 bruce@momjian.us 2130 : 22013561 : return rd;
2131 : : }
2132 : :
2133 : : /*
2134 : : * no reldesc in the cache, so have RelationBuildDesc() build one and add
2135 : : * it.
2136 : : */
5906 tgl@sss.pgh.pa.us 2137 : 506630 : rd = RelationBuildDesc(relationId, true);
7911 2138 [ + + ]: 506630 : if (RelationIsValid(rd))
2139 : 506423 : RelationIncrementReferenceCount(rd);
10841 scrappy@hub.org 2140 : 506630 : return rd;
2141 : : }
2142 : :
2143 : : /* ----------------------------------------------------------------
2144 : : * cache invalidation support routines
2145 : : * ----------------------------------------------------------------
2146 : : */
2147 : :
2148 : : /* ResourceOwner callbacks to track relcache references */
2149 : : static void ResOwnerReleaseRelation(Datum res);
2150 : : static char *ResOwnerPrintRelCache(Datum res);
2151 : :
2152 : : static const ResourceOwnerDesc relref_resowner_desc =
2153 : : {
2154 : : .name = "relcache reference",
2155 : : .release_phase = RESOURCE_RELEASE_BEFORE_LOCKS,
2156 : : .release_priority = RELEASE_PRIO_RELCACHE_REFS,
2157 : : .ReleaseResource = ResOwnerReleaseRelation,
2158 : : .DebugPrint = ResOwnerPrintRelCache
2159 : : };
2160 : :
2161 : : /* Convenience wrappers over ResourceOwnerRemember/Forget */
2162 : : static inline void
858 heikki.linnakangas@i 2163 : 32951748 : ResourceOwnerRememberRelationRef(ResourceOwner owner, Relation rel)
2164 : : {
2165 : 32951748 : ResourceOwnerRemember(owner, PointerGetDatum(rel), &relref_resowner_desc);
2166 : 32951748 : }
2167 : : static inline void
2168 : 32928322 : ResourceOwnerForgetRelationRef(ResourceOwner owner, Relation rel)
2169 : : {
2170 : 32928322 : ResourceOwnerForget(owner, PointerGetDatum(rel), &relref_resowner_desc);
2171 : 32928322 : }
2172 : :
2173 : : /*
2174 : : * RelationIncrementReferenceCount
2175 : : * Increments relation reference count.
2176 : : *
2177 : : * Note: bootstrap mode has its own weird ideas about relation refcount
2178 : : * behavior; we ought to fix it someday, but for now, just disable
2179 : : * reference count ownership tracking in bootstrap mode.
2180 : : */
2181 : : void
7911 tgl@sss.pgh.pa.us 2182 : 33247058 : RelationIncrementReferenceCount(Relation rel)
2183 : : {
858 heikki.linnakangas@i 2184 : 33247058 : ResourceOwnerEnlarge(CurrentResourceOwner);
7911 tgl@sss.pgh.pa.us 2185 : 33247058 : rel->rd_refcnt += 1;
2186 [ + + ]: 33247058 : if (!IsBootstrapProcessingMode())
2187 : 32951748 : ResourceOwnerRememberRelationRef(CurrentResourceOwner, rel);
2188 : 33247058 : }
2189 : :
2190 : : /*
2191 : : * RelationDecrementReferenceCount
2192 : : * Decrements relation reference count.
2193 : : */
2194 : : void
2195 : 33223632 : RelationDecrementReferenceCount(Relation rel)
2196 : : {
2197 [ - + ]: 33223632 : Assert(rel->rd_refcnt > 0);
2198 : 33223632 : rel->rd_refcnt -= 1;
2199 [ + + ]: 33223632 : if (!IsBootstrapProcessingMode())
2200 : 32928322 : ResourceOwnerForgetRelationRef(CurrentResourceOwner, rel);
2201 : 33223632 : }
2202 : :
2203 : : /*
2204 : : * RelationClose - close an open relation
2205 : : *
2206 : : * Actually, we just decrement the refcount.
2207 : : *
2208 : : * NOTE: if compiled with -DRELCACHE_FORCE_RELEASE then relcache entries
2209 : : * will be freed as soon as their refcount goes to zero. In combination
2210 : : * with aset.c's CLOBBER_FREED_MEMORY option, this provides a good test
2211 : : * to catch references to already-released relcache entries. It slows
2212 : : * things down quite a bit, however.
2213 : : */
2214 : : void
10841 scrappy@hub.org 2215 : 22572044 : RelationClose(Relation relation)
2216 : : {
2217 : : /* Note: no locking manipulations needed */
10416 bruce@momjian.us 2218 : 22572044 : RelationDecrementReferenceCount(relation);
2219 : :
858 heikki.linnakangas@i 2220 : 22572044 : RelationCloseCleanup(relation);
2221 : 22572044 : }
2222 : :
2223 : : static void
2224 : 22595470 : RelationCloseCleanup(Relation relation)
2225 : : {
2226 : : /*
2227 : : * If the relation is no longer open in this session, we can clean up any
2228 : : * stale partition descriptors it has. This is unlikely, so check to see
2229 : : * if there are child contexts before expending a call to mcxt.c.
2230 : : */
1782 alvherre@alvh.no-ip. 2231 [ + + ]: 22595470 : if (RelationHasReferenceCountZero(relation))
2232 : : {
2233 [ + + ]: 13371800 : if (relation->rd_pdcxt != NULL &&
2234 [ + + ]: 56916 : relation->rd_pdcxt->firstchild != NULL)
2235 : 2414 : MemoryContextDeleteChildren(relation->rd_pdcxt);
2236 : :
2237 [ + + ]: 13371800 : if (relation->rd_pddcxt != NULL &&
2238 [ - + ]: 54 : relation->rd_pddcxt->firstchild != NULL)
1782 alvherre@alvh.no-ip. 2239 :UBC 0 : MemoryContextDeleteChildren(relation->rd_pddcxt);
2240 : : }
2241 : :
2242 : : #ifdef RELCACHE_FORCE_RELEASE
2243 : : if (RelationHasReferenceCountZero(relation) &&
2244 : : relation->rd_createSubid == InvalidSubTransactionId &&
2245 : : relation->rd_firstRelfilelocatorSubid == InvalidSubTransactionId)
2246 : : RelationClearRelation(relation);
2247 : : #endif
10841 scrappy@hub.org 2248 :CBC 22595470 : }
2249 : :
2250 : : /*
2251 : : * RelationReloadIndexInfo - reload minimal information for an open index
2252 : : *
2253 : : * This function is used only for indexes. A relcache inval on an index
2254 : : * can mean that its pg_class or pg_index row changed. There are only
2255 : : * very limited changes that are allowed to an existing index's schema,
2256 : : * so we can update the relcache entry without a complete rebuild; which
2257 : : * is fortunate because we can't rebuild an index entry that is "nailed"
2258 : : * and/or in active use. We support full replacement of the pg_class row,
2259 : : * as well as updates of a few simple fields of the pg_index row.
2260 : : *
2261 : : * We assume that at the time we are called, we have at least AccessShareLock
2262 : : * on the target index.
2263 : : *
2264 : : * If the target index is an index on pg_class or pg_index, we'd better have
2265 : : * previously gotten at least AccessShareLock on its underlying catalog,
2266 : : * else we are at risk of deadlock against someone trying to exclusive-lock
2267 : : * the heap and index in that order. This is ensured in current usage by
2268 : : * only applying this to indexes being opened or having positive refcount.
2269 : : */
2270 : : static void
6892 tgl@sss.pgh.pa.us 2271 : 59956 : RelationReloadIndexInfo(Relation relation)
2272 : : {
2273 : : bool indexOK;
2274 : : HeapTuple pg_class_tuple;
2275 : : Form_pg_class relp;
2276 : :
2277 : : /* Should be called only for invalidated, live indexes */
2977 alvherre@alvh.no-ip. 2278 [ + + + - : 59956 : Assert((relation->rd_rel->relkind == RELKIND_INDEX ||
+ - - + ]
2279 : : relation->rd_rel->relkind == RELKIND_PARTITIONED_INDEX) &&
2280 : : !relation->rd_isvalid &&
2281 : : relation->rd_droppedSubid == InvalidSubTransactionId);
2282 : :
2283 : : /*
2284 : : * If it's a shared index, we might be called before backend startup has
2285 : : * finished selecting a database, in which case we have no way to read
2286 : : * pg_class yet. However, a shared index can never have any significant
2287 : : * schema updates, so it's okay to mostly ignore the invalidation signal.
2288 : : * Its physical relfilenumber might've changed, but that's all. Update
2289 : : * the physical relfilenumber, mark it valid and return without doing
2290 : : * anything more.
2291 : : */
6059 tgl@sss.pgh.pa.us 2292 [ + + - + ]: 59956 : if (relation->rd_rel->relisshared && !criticalRelcachesBuilt)
2293 : : {
500 heikki.linnakangas@i 2294 :UBC 0 : RelationInitPhysicalAddr(relation);
6059 tgl@sss.pgh.pa.us 2295 : 0 : relation->rd_isvalid = true;
2296 : 0 : return;
2297 : : }
2298 : :
2299 : : /*
2300 : : * Read the pg_class row
2301 : : *
2302 : : * Don't try to use an indexscan of pg_class_oid_index to reload the info
2303 : : * for pg_class_oid_index ...
2304 : : */
7640 tgl@sss.pgh.pa.us 2305 :CBC 59956 : indexOK = (RelationGetRelid(relation) != ClassOidIndexId);
4395 rhaas@postgresql.org 2306 : 59956 : pg_class_tuple = ScanPgRelation(RelationGetRelid(relation), indexOK, false);
9228 inoue@tpf.co.jp 2307 [ - + ]: 59953 : if (!HeapTupleIsValid(pg_class_tuple))
7360 tgl@sss.pgh.pa.us 2308 [ # # ]:UBC 0 : elog(ERROR, "could not find pg_class tuple for index %u",
2309 : : RelationGetRelid(relation));
9228 inoue@tpf.co.jp 2310 :CBC 59953 : relp = (Form_pg_class) GETSTRUCT(pg_class_tuple);
7360 tgl@sss.pgh.pa.us 2311 : 59953 : memcpy(relation->rd_rel, relp, CLASS_TUPLE_SIZE);
2312 : : /* Reload reloptions in case they changed */
7196 bruce@momjian.us 2313 [ + + ]: 59953 : if (relation->rd_options)
2314 : 570 : pfree(relation->rd_options);
7195 tgl@sss.pgh.pa.us 2315 : 59953 : RelationParseRelOptions(relation, pg_class_tuple);
2316 : : /* done with pg_class tuple */
9228 inoue@tpf.co.jp 2317 : 59953 : heap_freetuple(pg_class_tuple);
2318 : : /* We must recalculate physical address in case it changed */
7360 tgl@sss.pgh.pa.us 2319 : 59953 : RelationInitPhysicalAddr(relation);
2320 : :
2321 : : /*
2322 : : * For a non-system index, there are fields of the pg_index row that are
2323 : : * allowed to change, so re-read that row and update the relcache entry.
2324 : : * Most of the info derived from pg_index (such as support function lookup
2325 : : * info) cannot change, and indeed the whole point of this routine is to
2326 : : * update the relcache entry without clobbering that data; so wholesale
2327 : : * replacement is not appropriate.
2328 : : */
6892 2329 [ + + ]: 59953 : if (!IsSystemRelation(relation))
2330 : : {
2331 : : HeapTuple tuple;
2332 : : Form_pg_index index;
2333 : :
5873 rhaas@postgresql.org 2334 : 22571 : tuple = SearchSysCache1(INDEXRELID,
2335 : : ObjectIdGetDatum(RelationGetRelid(relation)));
6892 tgl@sss.pgh.pa.us 2336 [ - + ]: 22571 : if (!HeapTupleIsValid(tuple))
6695 bruce@momjian.us 2337 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for index %u",
2338 : : RelationGetRelid(relation));
6892 tgl@sss.pgh.pa.us 2339 :CBC 22571 : index = (Form_pg_index) GETSTRUCT(tuple);
2340 : :
2341 : : /*
2342 : : * Basically, let's just copy all the bool fields. There are one or
2343 : : * two of these that can't actually change in the current code, but
2344 : : * it's not worth it to track exactly which ones they are. None of
2345 : : * the array fields are allowed to change, though.
2346 : : */
4855 2347 : 22571 : relation->rd_index->indisunique = index->indisunique;
1501 peter@eisentraut.org 2348 : 22571 : relation->rd_index->indnullsnotdistinct = index->indnullsnotdistinct;
4855 tgl@sss.pgh.pa.us 2349 : 22571 : relation->rd_index->indisprimary = index->indisprimary;
2350 : 22571 : relation->rd_index->indisexclusion = index->indisexclusion;
2351 : 22571 : relation->rd_index->indimmediate = index->indimmediate;
2352 : 22571 : relation->rd_index->indisclustered = index->indisclustered;
6892 2353 : 22571 : relation->rd_index->indisvalid = index->indisvalid;
6751 2354 : 22571 : relation->rd_index->indcheckxmin = index->indcheckxmin;
2355 : 22571 : relation->rd_index->indisready = index->indisready;
4855 2356 : 22571 : relation->rd_index->indislive = index->indislive;
975 michael@paquier.xyz 2357 : 22571 : relation->rd_index->indisreplident = index->indisreplident;
2358 : :
2359 : : /* Copy xmin too, as that is needed to make sense of indcheckxmin */
6751 tgl@sss.pgh.pa.us 2360 : 22571 : HeapTupleHeaderSetXmin(relation->rd_indextuple->t_data,
2361 : 22571 : HeapTupleHeaderGetXmin(tuple->t_data));
2362 : :
6892 2363 : 22571 : ReleaseSysCache(tuple);
2364 : : }
2365 : :
2366 : : /* Okay, now it's valid again */
7869 2367 : 59953 : relation->rd_isvalid = true;
2368 : : }
2369 : :
2370 : : /*
2371 : : * RelationReloadNailed - reload minimal information for nailed relations.
2372 : : *
2373 : : * The structure of a nailed relation can never change (which is good, because
2374 : : * we rely on knowing their structure to be able to read catalog content). But
2375 : : * some parts, e.g. pg_class.relfrozenxid, are still important to have
2376 : : * accurate content for. Therefore those need to be reloaded after the arrival
2377 : : * of invalidations.
2378 : : */
2379 : : static void
2833 andres@anarazel.de 2380 : 77803 : RelationReloadNailed(Relation relation)
2381 : : {
2382 : : /* Should be called only for invalidated, nailed relations */
500 heikki.linnakangas@i 2383 [ - + ]: 77803 : Assert(!relation->rd_isvalid);
2833 andres@anarazel.de 2384 [ - + ]: 77803 : Assert(relation->rd_isnailed);
2385 : : /* nailed indexes are handled by RelationReloadIndexInfo() */
500 heikki.linnakangas@i 2386 [ - + ]: 77803 : Assert(relation->rd_rel->relkind == RELKIND_RELATION);
332 noah@leadboat.com 2387 : 77803 : AssertCouldGetRelation();
2388 : :
2389 : : /*
2390 : : * Redo RelationInitPhysicalAddr in case it is a mapped relation whose
2391 : : * mapping changed.
2392 : : */
2833 andres@anarazel.de 2393 : 77803 : RelationInitPhysicalAddr(relation);
2394 : :
2395 : : /*
2396 : : * Reload a non-index entry. We can't easily do so if relcaches aren't
2397 : : * yet built, but that's fine because at that stage the attributes that
2398 : : * need to be current (like relfrozenxid) aren't yet accessed. To ensure
2399 : : * the entry will later be revalidated, we leave it in invalid state, but
2400 : : * allow use (cf. RelationIdGetRelation()).
2401 : : */
500 heikki.linnakangas@i 2402 [ + + ]: 77803 : if (criticalRelcachesBuilt)
2403 : : {
2404 : : HeapTuple pg_class_tuple;
2405 : : Form_pg_class relp;
2406 : :
2407 : : /*
2408 : : * NB: Mark the entry as valid before starting to scan, to avoid
2409 : : * self-recursion when re-building pg_class.
2410 : : */
2411 : 14589 : relation->rd_isvalid = true;
2412 : :
2413 : 14589 : pg_class_tuple = ScanPgRelation(RelationGetRelid(relation),
2414 : : true, false);
2415 : 14586 : relp = (Form_pg_class) GETSTRUCT(pg_class_tuple);
2416 : 14586 : memcpy(relation->rd_rel, relp, CLASS_TUPLE_SIZE);
2417 : 14586 : heap_freetuple(pg_class_tuple);
2418 : :
2419 : : /*
2420 : : * Again mark as valid, to protect against concurrently arriving
2421 : : * invalidations.
2422 : : */
2423 : 14586 : relation->rd_isvalid = true;
2424 : : }
2833 andres@anarazel.de 2425 : 77800 : }
2426 : :
2427 : : /*
2428 : : * RelationDestroyRelation
2429 : : *
2430 : : * Physically delete a relation cache entry and all subsidiary data.
2431 : : * Caller must already have unhooked the entry from the hash table.
2432 : : */
2433 : : static void
4361 simon@2ndQuadrant.co 2434 : 622941 : RelationDestroyRelation(Relation relation, bool remember_tupdesc)
2435 : : {
5906 tgl@sss.pgh.pa.us 2436 [ - + ]: 622941 : Assert(RelationHasReferenceCountZero(relation));
2437 : :
2438 : : /*
2439 : : * Make sure smgr and lower levels close the relation's files, if they
2440 : : * weren't closed already. (This was probably done by caller, but let's
2441 : : * just be real sure.)
2442 : : */
2443 : 622941 : RelationCloseSmgr(relation);
2444 : :
2445 : : /* break mutual link with stats entry */
1439 andres@anarazel.de 2446 : 622941 : pgstat_unlink_relation(relation);
2447 : :
2448 : : /*
2449 : : * Free all the subsidiary data structures of the relcache entry, then the
2450 : : * entry itself.
2451 : : */
5906 tgl@sss.pgh.pa.us 2452 [ + - ]: 622941 : if (relation->rd_rel)
2453 : 622941 : pfree(relation->rd_rel);
2454 : : /* can't use DecrTupleDescRefCount here */
2455 [ - + ]: 622941 : Assert(relation->rd_att->tdrefcount > 0);
2456 [ + + ]: 622941 : if (--relation->rd_att->tdrefcount == 0)
2457 : : {
2458 : : /*
2459 : : * If we Rebuilt a relcache entry during a transaction then its
2460 : : * possible we did that because the TupDesc changed as the result of
2461 : : * an ALTER TABLE that ran at less than AccessExclusiveLock. It's
2462 : : * possible someone copied that TupDesc, in which case the copy would
2463 : : * point to free'd memory. So if we rebuild an entry we keep the
2464 : : * TupDesc around until end of transaction, to be safe.
2465 : : */
4361 simon@2ndQuadrant.co 2466 [ + + ]: 621240 : if (remember_tupdesc)
2467 : 12021 : RememberToFreeTupleDescAtEOX(relation->rd_att);
2468 : : else
2469 : 609219 : FreeTupleDesc(relation->rd_att);
2470 : : }
3557 tgl@sss.pgh.pa.us 2471 : 622941 : FreeTriggerDesc(relation->trigdesc);
2472 : 622941 : list_free_deep(relation->rd_fkeylist);
5906 2473 : 622941 : list_free(relation->rd_indexlist);
1823 2474 : 622941 : list_free(relation->rd_statlist);
4323 2475 : 622941 : bms_free(relation->rd_keyattr);
3342 peter_e@gmx.net 2476 : 622941 : bms_free(relation->rd_pkattr);
4323 tgl@sss.pgh.pa.us 2477 : 622941 : bms_free(relation->rd_idattr);
1091 tomas.vondra@postgre 2478 : 622941 : bms_free(relation->rd_hotblockingattr);
2479 : 622941 : bms_free(relation->rd_summarizedattr);
1482 akapila@postgresql.o 2480 [ + + ]: 622941 : if (relation->rd_pubdesc)
2481 : 3844 : pfree(relation->rd_pubdesc);
5906 tgl@sss.pgh.pa.us 2482 [ + + ]: 622941 : if (relation->rd_options)
2483 : 6293 : pfree(relation->rd_options);
2484 [ + + ]: 622941 : if (relation->rd_indextuple)
2485 : 181639 : pfree(relation->rd_indextuple);
703 akorotkov@postgresql 2486 [ - + ]: 622941 : if (relation->rd_amcache)
703 akorotkov@postgresql 2487 :UBC 0 : pfree(relation->rd_amcache);
2420 heikki.linnakangas@i 2488 [ + + ]:CBC 622941 : if (relation->rd_fdwroutine)
2489 : 150 : pfree(relation->rd_fdwroutine);
5906 tgl@sss.pgh.pa.us 2490 [ + + ]: 622941 : if (relation->rd_indexcxt)
2491 : 181639 : MemoryContextDelete(relation->rd_indexcxt);
2492 [ + + ]: 622941 : if (relation->rd_rulescxt)
2493 : 13259 : MemoryContextDelete(relation->rd_rulescxt);
4139 sfrost@snowman.net 2494 [ + + ]: 622941 : if (relation->rd_rsdesc)
2495 : 1138 : MemoryContextDelete(relation->rd_rsdesc->rscxt);
3385 rhaas@postgresql.org 2496 [ + + ]: 622941 : if (relation->rd_partkeycxt)
2497 : 9208 : MemoryContextDelete(relation->rd_partkeycxt);
2498 [ + + ]: 622941 : if (relation->rd_pdcxt)
2499 : 8932 : MemoryContextDelete(relation->rd_pdcxt);
1782 alvherre@alvh.no-ip. 2500 [ + + ]: 622941 : if (relation->rd_pddcxt)
2501 : 30 : MemoryContextDelete(relation->rd_pddcxt);
2528 tgl@sss.pgh.pa.us 2502 [ + + ]: 622941 : if (relation->rd_partcheckcxt)
2503 : 1604 : MemoryContextDelete(relation->rd_partcheckcxt);
5906 2504 : 622941 : pfree(relation);
2505 : 622941 : }
2506 : :
2507 : : /*
2508 : : * RelationInvalidateRelation - mark a relation cache entry as invalid
2509 : : *
2510 : : * An entry that's marked as invalid will be reloaded on next access.
2511 : : */
2512 : : static void
647 heikki.linnakangas@i 2513 : 826648 : RelationInvalidateRelation(Relation relation)
2514 : : {
2515 : : /*
2516 : : * Make sure smgr and lower levels close the relation's files, if they
2517 : : * weren't closed already. If the relation is not getting deleted, the
2518 : : * next smgr access should reopen the files automatically. This ensures
2519 : : * that the low-level file access state is updated after, say, a vacuum
2520 : : * truncation.
2521 : : */
2522 : 826648 : RelationCloseSmgr(relation);
2523 : :
2524 : : /* Free AM cached data, if any */
2525 [ + + ]: 826648 : if (relation->rd_amcache)
2526 : 37503 : pfree(relation->rd_amcache);
2527 : 826648 : relation->rd_amcache = NULL;
2528 : :
2529 : 826648 : relation->rd_isvalid = false;
2530 : 826648 : }
2531 : :
2532 : : /*
2533 : : * RelationClearRelation - physically blow away a relation cache entry
2534 : : *
2535 : : * The caller must ensure that the entry is no longer needed, i.e. its
2536 : : * reference count is zero. Also, the rel or its storage must not be created
2537 : : * in the current transaction (rd_createSubid and rd_firstRelfilelocatorSubid
2538 : : * must not be set).
2539 : : */
2540 : : static void
500 2541 : 413736 : RelationClearRelation(Relation relation)
2542 : : {
2543 [ - + ]: 413736 : Assert(RelationHasReferenceCountZero(relation));
2544 [ - + ]: 413736 : Assert(!relation->rd_isnailed);
2545 : :
2546 : : /*
2547 : : * Relations created in the same transaction must never be removed, see
2548 : : * RelationFlushRelation.
2549 : : */
2550 [ - + ]: 413736 : Assert(relation->rd_createSubid == InvalidSubTransactionId);
2551 [ - + ]: 413736 : Assert(relation->rd_firstRelfilelocatorSubid == InvalidSubTransactionId);
2552 [ - + ]: 413736 : Assert(relation->rd_droppedSubid == InvalidSubTransactionId);
2553 : :
2554 : : /* first mark it as invalid */
2555 : 413736 : RelationInvalidateRelation(relation);
2556 : :
2557 : : /* Remove it from the hash table */
2558 [ - + - - ]: 413736 : RelationCacheDelete(relation);
2559 : :
2560 : : /* And release storage */
2561 : 413736 : RelationDestroyRelation(relation, false);
2562 : 413736 : }
2563 : :
2564 : : /*
2565 : : * RelationRebuildRelation - rebuild a relation cache entry in place
2566 : : *
2567 : : * Reset and rebuild a relation cache entry from scratch (that is, from
2568 : : * catalog entries). This is used when we are notified of a change to an open
2569 : : * relation (one with refcount > 0). The entry is reconstructed without
2570 : : * moving the physical RelationData record, so that the refcount holder's
2571 : : * pointer is still valid.
2572 : : *
2573 : : * NB: when rebuilding, we'd better hold some lock on the relation, else the
2574 : : * catalog data we need to read could be changing under us. Also, a rel to be
2575 : : * rebuilt had better have refcnt > 0. This is because a sinval reset could
2576 : : * happen while we're accessing the catalogs, and the rel would get blown away
2577 : : * underneath us by RelationCacheInvalidate if it has zero refcnt.
2578 : : */
2579 : : static void
2580 : 346955 : RelationRebuildRelation(Relation relation)
2581 : : {
2582 [ - + ]: 346955 : Assert(!RelationHasReferenceCountZero(relation));
332 noah@leadboat.com 2583 : 346955 : AssertCouldGetRelation();
2584 : : /* there is no reason to ever rebuild a dropped relation */
500 heikki.linnakangas@i 2585 [ - + ]: 346955 : Assert(relation->rd_droppedSubid == InvalidSubTransactionId);
2586 : :
2587 : : /* Close and mark it as invalid until we've finished the rebuild */
2588 : 346955 : RelationInvalidateRelation(relation);
2589 : :
2590 : : /*
2591 : : * Indexes only have a limited number of possible schema changes, and we
2592 : : * don't want to use the full-blown procedure because it's a headache for
2593 : : * indexes that reload itself depends on.
2594 : : *
2595 : : * As an exception, use the full procedure if the index access info hasn't
2596 : : * been initialized yet. Index creation relies on that: it first builds
2597 : : * the relcache entry with RelationBuildLocalRelation(), creates the
2598 : : * pg_index tuple only after that, and then relies on
2599 : : * CommandCounterIncrement to load the pg_index contents.
2600 : : */
2977 alvherre@alvh.no-ip. 2601 [ + + ]: 346955 : if ((relation->rd_rel->relkind == RELKIND_INDEX ||
2602 [ + + ]: 273294 : relation->rd_rel->relkind == RELKIND_PARTITIONED_INDEX) &&
7360 tgl@sss.pgh.pa.us 2603 [ + + ]: 76782 : relation->rd_indexcxt != NULL)
2604 : : {
500 heikki.linnakangas@i 2605 : 59956 : RelationReloadIndexInfo(relation);
7360 tgl@sss.pgh.pa.us 2606 : 59953 : return;
2607 : : }
2608 : : /* Nailed relations are handled separately. */
500 heikki.linnakangas@i 2609 [ + + ]: 286999 : else if (relation->rd_isnailed)
2610 : : {
2611 : 77803 : RelationReloadNailed(relation);
4420 tgl@sss.pgh.pa.us 2612 : 77800 : return;
2613 : : }
2614 : : else
2615 : : {
2616 : : /*
2617 : : * Our strategy for rebuilding an open relcache entry is to build a
2618 : : * new entry from scratch, swap its contents with the old entry, and
2619 : : * finally delete the new entry (along with any infrastructure swapped
2620 : : * over from the old entry). This is to avoid trouble in case an
2621 : : * error causes us to lose control partway through. The old entry
2622 : : * will still be marked !rd_isvalid, so we'll try to rebuild it again
2623 : : * on next access. Meanwhile it's not any less valid than it was
2624 : : * before, so any code that might expect to continue accessing it
2625 : : * isn't hurt by the rebuild failure. (Consider for example a
2626 : : * subtransaction that ALTERs a table and then gets canceled partway
2627 : : * through the cache entry rebuild. The outer transaction should
2628 : : * still see the not-modified cache entry as valid.) The worst
2629 : : * consequence of an error is leaking the necessarily-unreferenced new
2630 : : * entry, and this shouldn't happen often enough for that to be a big
2631 : : * problem.
2632 : : *
2633 : : * When rebuilding an open relcache entry, we must preserve ref count,
2634 : : * rd_*Subid, and rd_toastoid state. Also attempt to preserve the
2635 : : * pg_class entry (rd_rel), tupledesc, rewrite-rule, partition key,
2636 : : * and partition descriptor substructures in place, because various
2637 : : * places assume that these structures won't move while they are
2638 : : * working with an open relcache entry. (Note: the refcount
2639 : : * mechanism for tupledescs might someday allow us to remove this hack
2640 : : * for the tupledesc.)
2641 : : *
2642 : : * Note that this process does not touch CurrentResourceOwner; which
2643 : : * is good because whatever ref counts the entry may have do not
2644 : : * necessarily belong to that resource owner.
2645 : : */
2646 : : Relation newrel;
7640 2647 : 209196 : Oid save_relid = RelationGetRelid(relation);
2648 : : bool keep_tupdesc;
2649 : : bool keep_rules;
2650 : : bool keep_policies;
2651 : : bool keep_partkey;
2652 : :
2653 : : /* Build temporary entry, but don't link it into hashtable */
5906 2654 : 209196 : newrel = RelationBuildDesc(save_relid, false);
2655 : :
2656 : : /*
2657 : : * Between here and the end of the swap, don't add code that does or
2658 : : * reasonably could read system catalogs. That range must be free
2659 : : * from invalidation processing. See RelationBuildDesc() manipulation
2660 : : * of in_progress_list.
2661 : : */
2662 : :
2663 [ - + ]: 209193 : if (newrel == NULL)
2664 : : {
2665 : : /*
2666 : : * We can validly get here, if we're using a historic snapshot in
2667 : : * which a relation, accessed from outside logical decoding, is
2668 : : * still invisible. In that case it's fine to just mark the
2669 : : * relation as invalid and return - it'll fully get reloaded by
2670 : : * the cache reset at the end of logical decoding (or at the next
2671 : : * access). During normal processing we don't want to ignore this
2672 : : * case as it shouldn't happen there, as explained below.
2673 : : */
4085 andres@anarazel.de 2674 [ # # ]:UBC 0 : if (HistoricSnapshotActive())
2675 : 0 : return;
2676 : :
2677 : : /*
2678 : : * This shouldn't happen as dropping a relation is intended to be
2679 : : * impossible if still referenced (cf. CheckTableNotInUse()). But
2680 : : * if we get here anyway, we can't just delete the relcache entry,
2681 : : * as it possibly could get accessed later (as e.g. the error
2682 : : * might get trapped and handled via a subtransaction rollback).
2683 : : */
7640 tgl@sss.pgh.pa.us 2684 [ # # ]: 0 : elog(ERROR, "relation %u deleted while still in use", save_relid);
2685 : : }
2686 : :
2687 : : /*
2688 : : * If we were to, again, have cases of the relkind of a relcache entry
2689 : : * changing, we would need to ensure that pgstats does not get
2690 : : * confused.
2691 : : */
1199 andres@anarazel.de 2692 [ - + ]:CBC 209193 : Assert(relation->rd_rel->relkind == newrel->rd_rel->relkind);
2693 : :
5906 tgl@sss.pgh.pa.us 2694 : 209193 : keep_tupdesc = equalTupleDescs(relation->rd_att, newrel->rd_att);
2695 : 209193 : keep_rules = equalRuleLocks(relation->rd_rules, newrel->rd_rules);
4139 sfrost@snowman.net 2696 : 209193 : keep_policies = equalRSDesc(relation->rd_rsdesc, newrel->rd_rsdesc);
2697 : : /* partkey is immutable once set up, so we can always keep it */
3385 rhaas@postgresql.org 2698 : 209193 : keep_partkey = (relation->rd_partkey != NULL);
2699 : :
2700 : : /*
2701 : : * Perform swapping of the relcache entry contents. Within this
2702 : : * process the old entry is momentarily invalid, so there *must* be no
2703 : : * possibility of CHECK_FOR_INTERRUPTS within this sequence. Do it in
2704 : : * all-in-line code for safety.
2705 : : *
2706 : : * Since the vast majority of fields should be swapped, our method is
2707 : : * to swap the whole structures and then re-swap those few fields we
2708 : : * didn't want swapped.
2709 : : */
2710 : : #define SWAPFIELD(fldtype, fldname) \
2711 : : do { \
2712 : : fldtype _tmp = newrel->fldname; \
2713 : : newrel->fldname = relation->fldname; \
2714 : : relation->fldname = _tmp; \
2715 : : } while (0)
2716 : :
2717 : : /* swap all Relation struct fields */
2718 : : {
2719 : : RelationData tmpstruct;
2720 : :
5906 tgl@sss.pgh.pa.us 2721 : 209193 : memcpy(&tmpstruct, newrel, sizeof(RelationData));
2722 : 209193 : memcpy(newrel, relation, sizeof(RelationData));
2723 : 209193 : memcpy(relation, &tmpstruct, sizeof(RelationData));
2724 : : }
2725 : :
2726 : : /* rd_smgr must not be swapped, due to back-links from smgr level */
2727 : 209193 : SWAPFIELD(SMgrRelation, rd_smgr);
2728 : : /* rd_refcnt must be preserved */
2729 : 209193 : SWAPFIELD(int, rd_refcnt);
2730 : : /* isnailed shouldn't change */
2731 [ - + ]: 209193 : Assert(newrel->rd_isnailed == relation->rd_isnailed);
2732 : : /* creation sub-XIDs must be preserved */
2733 : 209193 : SWAPFIELD(SubTransactionId, rd_createSubid);
1348 rhaas@postgresql.org 2734 : 209193 : SWAPFIELD(SubTransactionId, rd_newRelfilelocatorSubid);
2735 : 209193 : SWAPFIELD(SubTransactionId, rd_firstRelfilelocatorSubid);
2171 noah@leadboat.com 2736 : 209193 : SWAPFIELD(SubTransactionId, rd_droppedSubid);
2737 : : /* un-swap rd_rel pointers, swap contents instead */
5906 tgl@sss.pgh.pa.us 2738 : 209193 : SWAPFIELD(Form_pg_class, rd_rel);
2739 : : /* ... but actually, we don't have to update newrel->rd_rel */
2740 : 209193 : memcpy(relation->rd_rel, newrel->rd_rel, CLASS_TUPLE_SIZE);
2741 : : /* preserve old tupledesc, rules, policies if no logical change */
2742 [ + + ]: 209193 : if (keep_tupdesc)
2743 : 197025 : SWAPFIELD(TupleDesc, rd_att);
2744 [ + + ]: 209193 : if (keep_rules)
2745 : : {
2746 : 200127 : SWAPFIELD(RuleLock *, rd_rules);
2747 : 200127 : SWAPFIELD(MemoryContext, rd_rulescxt);
2748 : : }
4190 sfrost@snowman.net 2749 [ + + ]: 209193 : if (keep_policies)
4139 2750 : 209016 : SWAPFIELD(RowSecurityDesc *, rd_rsdesc);
2751 : : /* toast OID override must be preserved */
5883 tgl@sss.pgh.pa.us 2752 : 209193 : SWAPFIELD(Oid, rd_toastoid);
2753 : : /* pgstat_info / enabled must be preserved */
5906 2754 : 209193 : SWAPFIELD(struct PgStat_TableStatus *, pgstat_info);
1439 andres@anarazel.de 2755 : 209193 : SWAPFIELD(bool, pgstat_enabled);
2756 : : /* preserve old partition key if we have one */
3385 rhaas@postgresql.org 2757 [ + + ]: 209193 : if (keep_partkey)
2758 : : {
2759 : 8739 : SWAPFIELD(PartitionKey, rd_partkey);
2760 : 8739 : SWAPFIELD(MemoryContext, rd_partkeycxt);
2761 : : }
1782 alvherre@alvh.no-ip. 2762 [ + + - + ]: 209193 : if (newrel->rd_pdcxt != NULL || newrel->rd_pddcxt != NULL)
2763 : : {
2764 : : /*
2765 : : * We are rebuilding a partitioned relation with a non-zero
2766 : : * reference count, so we must keep the old partition descriptor
2767 : : * around, in case there's a PartitionDirectory with a pointer to
2768 : : * it. This means we can't free the old rd_pdcxt yet. (This is
2769 : : * necessary because RelationGetPartitionDesc hands out direct
2770 : : * pointers to the relcache's data structure, unlike our usual
2771 : : * practice which is to hand out copies. We'd have the same
2772 : : * problem with rd_partkey, except that we always preserve that
2773 : : * once created.)
2774 : : *
2775 : : * To ensure that it's not leaked completely, re-attach it to the
2776 : : * new reldesc, or make it a child of the new reldesc's rd_pdcxt
2777 : : * in the unlikely event that there is one already. (Compare hack
2778 : : * in RelationBuildPartitionDesc.) RelationClose will clean up
2779 : : * any such contexts once the reference count reaches zero.
2780 : : *
2781 : : * In the case where the reference count is zero, this code is not
2782 : : * reached, which should be OK because in that case there should
2783 : : * be no PartitionDirectory with a pointer to the old entry.
2784 : : *
2785 : : * Note that newrel and relation have already been swapped, so the
2786 : : * "old" partition descriptor is actually the one hanging off of
2787 : : * newrel.
2788 : : */
2272 tgl@sss.pgh.pa.us 2789 : 6871 : relation->rd_partdesc = NULL; /* ensure rd_partdesc is invalid */
1782 alvherre@alvh.no-ip. 2790 : 6871 : relation->rd_partdesc_nodetached = NULL;
2791 : 6871 : relation->rd_partdesc_nodetached_xmin = InvalidTransactionId;
2272 tgl@sss.pgh.pa.us 2792 [ - + ]: 6871 : if (relation->rd_pdcxt != NULL) /* probably never happens */
2272 tgl@sss.pgh.pa.us 2793 :UBC 0 : MemoryContextSetParent(newrel->rd_pdcxt, relation->rd_pdcxt);
2794 : : else
2272 tgl@sss.pgh.pa.us 2795 :CBC 6871 : relation->rd_pdcxt = newrel->rd_pdcxt;
1782 alvherre@alvh.no-ip. 2796 [ - + ]: 6871 : if (relation->rd_pddcxt != NULL)
1782 alvherre@alvh.no-ip. 2797 :UBC 0 : MemoryContextSetParent(newrel->rd_pddcxt, relation->rd_pddcxt);
2798 : : else
1782 alvherre@alvh.no-ip. 2799 :CBC 6871 : relation->rd_pddcxt = newrel->rd_pddcxt;
2800 : : /* drop newrel's pointers so we don't destroy it below */
2565 rhaas@postgresql.org 2801 : 6871 : newrel->rd_partdesc = NULL;
1782 alvherre@alvh.no-ip. 2802 : 6871 : newrel->rd_partdesc_nodetached = NULL;
2803 : 6871 : newrel->rd_partdesc_nodetached_xmin = InvalidTransactionId;
2565 rhaas@postgresql.org 2804 : 6871 : newrel->rd_pdcxt = NULL;
1782 alvherre@alvh.no-ip. 2805 : 6871 : newrel->rd_pddcxt = NULL;
2806 : : }
2807 : :
2808 : : #undef SWAPFIELD
2809 : :
2810 : : /* And now we can throw away the temporary entry */
4361 simon@2ndQuadrant.co 2811 : 209193 : RelationDestroyRelation(newrel, !keep_tupdesc);
2812 : : }
2813 : : }
2814 : :
2815 : : /*
2816 : : * RelationFlushRelation
2817 : : *
2818 : : * Rebuild the relation if it is open (refcount > 0), else blow it away.
2819 : : * This is used when we receive a cache invalidation event for the rel.
2820 : : */
2821 : : static void
9203 tgl@sss.pgh.pa.us 2822 : 423272 : RelationFlushRelation(Relation relation)
2823 : : {
6989 bruce@momjian.us 2824 [ + + ]: 423272 : if (relation->rd_createSubid != InvalidSubTransactionId ||
1348 rhaas@postgresql.org 2825 [ + + ]: 243411 : relation->rd_firstRelfilelocatorSubid != InvalidSubTransactionId)
2826 : : {
2827 : : /*
2828 : : * New relcache entries are always rebuilt, not flushed; else we'd
2829 : : * forget the "new" status of the relation. Ditto for the
2830 : : * new-relfilenumber status.
2831 : : */
647 heikki.linnakangas@i 2832 [ + + + + ]: 378101 : if (IsTransactionState() && relation->rd_droppedSubid == InvalidSubTransactionId)
2833 : : {
2834 : : /*
2835 : : * The rel could have zero refcnt here, so temporarily increment
2836 : : * the refcnt to ensure it's safe to rebuild it. We can assume
2837 : : * that the current transaction has some lock on the rel already.
2838 : : */
2839 : 188044 : RelationIncrementReferenceCount(relation);
500 2840 : 188044 : RelationRebuildRelation(relation);
647 2841 : 188041 : RelationDecrementReferenceCount(relation);
2842 : : }
2843 : : else
2844 : 1008 : RelationInvalidateRelation(relation);
2845 : : }
2846 : : else
2847 : : {
2848 : : /*
2849 : : * Pre-existing rels can be dropped from the relcache if not open.
2850 : : *
2851 : : * If the entry is in use, rebuild it if possible. If we're not
2852 : : * inside a valid transaction, we can't do any catalog access so it's
2853 : : * not possible to rebuild yet. Just mark it as invalid in that case,
2854 : : * so that the rebuild will occur when the entry is next opened.
2855 : : *
2856 : : * Note: it's possible that we come here during subtransaction abort,
2857 : : * and the reason for wanting to rebuild is that the rel is open in
2858 : : * the outer transaction. In that case it might seem unsafe to not
2859 : : * rebuild immediately, since whatever code has the rel already open
2860 : : * will keep on using the relcache entry as-is. However, in such a
2861 : : * case the outer transaction should be holding a lock that's
2862 : : * sufficient to prevent any significant change in the rel's schema,
2863 : : * so the existing entry contents should be good enough for its
2864 : : * purposes; at worst we might be behind on statistics updates or the
2865 : : * like. (See also CheckTableNotInUse() and its callers.)
2866 : : */
500 2867 [ + + ]: 234220 : if (RelationHasReferenceCountZero(relation))
2868 : 154220 : RelationClearRelation(relation);
2869 [ + + ]: 80000 : else if (!IsTransactionState())
2870 : 6260 : RelationInvalidateRelation(relation);
2871 [ + + + + ]: 73740 : else if (relation->rd_isnailed && relation->rd_refcnt == 1)
2872 : : {
2873 : : /*
2874 : : * A nailed relation with refcnt == 1 is unused. We cannot clear
2875 : : * it, but there's also no need no need to rebuild it immediately.
2876 : : */
2877 : 2010 : RelationInvalidateRelation(relation);
2878 : : }
2879 : : else
2880 : 71730 : RelationRebuildRelation(relation);
2881 : : }
9660 tgl@sss.pgh.pa.us 2882 : 423269 : }
2883 : :
2884 : : /*
2885 : : * RelationForgetRelation - caller reports that it dropped the relation
2886 : : */
2887 : : void
10416 bruce@momjian.us 2888 : 38797 : RelationForgetRelation(Oid rid)
2889 : : {
2890 : : Relation relation;
2891 : :
2892 [ + - ]: 38797 : RelationIdCacheLookup(rid, relation);
2893 : :
172 peter@eisentraut.org 2894 [ - + ]:GNC 38797 : if (!relation)
8698 tgl@sss.pgh.pa.us 2895 :UBC 0 : return; /* not in cache, nothing to do */
2896 : :
8698 tgl@sss.pgh.pa.us 2897 [ - + ]:CBC 38797 : if (!RelationHasReferenceCountZero(relation))
8269 tgl@sss.pgh.pa.us 2898 [ # # ]:UBC 0 : elog(ERROR, "relation %u is still open", rid);
2899 : :
2171 noah@leadboat.com 2900 [ - + ]:CBC 38797 : Assert(relation->rd_droppedSubid == InvalidSubTransactionId);
2901 [ + + ]: 38797 : if (relation->rd_createSubid != InvalidSubTransactionId ||
1348 rhaas@postgresql.org 2902 [ + + ]: 38001 : relation->rd_firstRelfilelocatorSubid != InvalidSubTransactionId)
2903 : : {
2904 : : /*
2905 : : * In the event of subtransaction rollback, we must not forget
2906 : : * rd_*Subid. Mark the entry "dropped" and invalidate it, instead of
2907 : : * destroying it right away. (If we're in a top transaction, we could
2908 : : * opt to destroy the entry.)
2909 : : */
2171 noah@leadboat.com 2910 : 821 : relation->rd_droppedSubid = GetCurrentSubTransactionId();
500 heikki.linnakangas@i 2911 : 821 : RelationInvalidateRelation(relation);
2912 : : }
2913 : : else
2914 : 37976 : RelationClearRelation(relation);
2915 : : }
2916 : :
2917 : : /*
2918 : : * RelationCacheInvalidateEntry
2919 : : *
2920 : : * This routine is invoked for SI cache flush messages.
2921 : : *
2922 : : * Any relcache entry matching the relid must be flushed. (Note: caller has
2923 : : * already determined that the relid belongs to our database or is a shared
2924 : : * relation.)
2925 : : *
2926 : : * We used to skip local relations, on the grounds that they could
2927 : : * not be targets of cross-backend SI update messages; but it seems
2928 : : * safer to process them, so that our *own* SI update messages will
2929 : : * have the same effects during CommandCounterIncrement for both
2930 : : * local and nonlocal relations.
2931 : : */
2932 : : void
7734 tgl@sss.pgh.pa.us 2933 : 1677490 : RelationCacheInvalidateEntry(Oid relationId)
2934 : : {
2935 : : Relation relation;
2936 : :
10416 bruce@momjian.us 2937 [ + + ]: 1677490 : RelationIdCacheLookup(relationId, relation);
2938 : :
172 peter@eisentraut.org 2939 [ + + ]:GNC 1677490 : if (relation)
2940 : : {
8790 tgl@sss.pgh.pa.us 2941 :CBC 423272 : relcacheInvalsReceived++;
9203 2942 : 423272 : RelationFlushRelation(relation);
2943 : : }
2944 : : else
2945 : : {
2946 : : int i;
2947 : :
1604 noah@leadboat.com 2948 [ + + ]: 1294465 : for (i = 0; i < in_progress_list_len; i++)
2949 [ + + ]: 40247 : if (in_progress_list[i].reloid == relationId)
2950 : 9 : in_progress_list[i].invalidated = true;
2951 : : }
10841 scrappy@hub.org 2952 : 1677487 : }
2953 : :
2954 : : /*
2955 : : * RelationCacheInvalidate
2956 : : * Blow away cached relation descriptors that have zero reference counts,
2957 : : * and rebuild those with positive reference counts. Also reset the smgr
2958 : : * relation cache and re-read relation mapping data.
2959 : : *
2960 : : * Apart from debug_discard_caches, this is currently used only to recover
2961 : : * from SI message buffer overflow, so we do not touch relations having
2962 : : * new-in-transaction relfilenumbers; they cannot be targets of cross-backend
2963 : : * SI updates (and our own updates now go through a separate linked list
2964 : : * that isn't limited by the SI message buffer size).
2965 : : *
2966 : : * We do this in two phases: the first pass deletes deletable items, and
2967 : : * the second one rebuilds the rebuildable items. This is essential for
2968 : : * safety, because hash_seq_search only copes with concurrent deletion of
2969 : : * the element it is currently visiting. If a second SI overflow were to
2970 : : * occur while we are walking the table, resulting in recursive entry to
2971 : : * this routine, we could crash because the inner invocation blows away
2972 : : * the entry next to be visited by the outer scan. But this way is OK,
2973 : : * because (a) during the first pass we won't process any more SI messages,
2974 : : * so hash_seq_search will complete safely; (b) during the second pass we
2975 : : * only hold onto pointers to nondeletable entries.
2976 : : *
2977 : : * The two-phase approach also makes it easy to update relfilenumbers for
2978 : : * mapped relations before we do anything else, and to ensure that the
2979 : : * second pass processes nailed-in-cache items before other nondeletable
2980 : : * items. This should ensure that system catalogs are up to date before
2981 : : * we attempt to use them to reload information about other open relations.
2982 : : *
2983 : : * After those two phases of work having immediate effects, we normally
2984 : : * signal any RelationBuildDesc() on the stack to start over. However, we
2985 : : * don't do this if called as part of debug_discard_caches. Otherwise,
2986 : : * RelationBuildDesc() would become an infinite loop.
2987 : : */
2988 : : void
1604 noah@leadboat.com 2989 : 2687 : RelationCacheInvalidate(bool debug_discard)
2990 : : {
2991 : : HASH_SEQ_STATUS status;
2992 : : RelIdCacheEnt *idhentry;
2993 : : Relation relation;
8208 tgl@sss.pgh.pa.us 2994 : 2687 : List *rebuildFirstList = NIL;
9124 bruce@momjian.us 2995 : 2687 : List *rebuildList = NIL;
2996 : : ListCell *l;
2997 : : int i;
2998 : :
2999 : : /*
3000 : : * Reload relation mapping data before starting to reconstruct cache.
3001 : : */
5325 tgl@sss.pgh.pa.us 3002 : 2687 : RelationMapInvalidateAll();
3003 : :
3004 : : /* Phase 1 */
8755 3005 : 2687 : hash_seq_init(&status, RelationIdCache);
3006 : :
3007 [ + + ]: 277310 : while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
3008 : : {
3009 : 274623 : relation = idhentry->reldesc;
3010 : :
3011 : : /*
3012 : : * Ignore new relations; no other backend will manipulate them before
3013 : : * we commit. Likewise, before replacing a relation's relfilelocator,
3014 : : * we shall have acquired AccessExclusiveLock and drained any
3015 : : * applicable pending invalidations.
3016 : : */
4829 simon@2ndQuadrant.co 3017 [ + + ]: 274623 : if (relation->rd_createSubid != InvalidSubTransactionId ||
1348 rhaas@postgresql.org 3018 [ + + ]: 274582 : relation->rd_firstRelfilelocatorSubid != InvalidSubTransactionId)
8927 tgl@sss.pgh.pa.us 3019 : 49 : continue;
3020 : :
8790 3021 : 274574 : relcacheInvalsReceived++;
3022 : :
8617 3023 [ + + ]: 274574 : if (RelationHasReferenceCountZero(relation))
3024 : : {
3025 : : /* Delete this entry immediately */
500 heikki.linnakangas@i 3026 : 218596 : RelationClearRelation(relation);
3027 : : }
3028 : : else
3029 : : {
3030 : : /*
3031 : : * If it's a mapped relation, immediately update its rd_locator in
3032 : : * case its relfilenumber changed. We must do this during phase 1
3033 : : * in case the relation is consulted during rebuild of other
3034 : : * relcache entries in phase 2. It's safe since consulting the
3035 : : * map doesn't involve any access to relcache entries.
3036 : : */
5325 tgl@sss.pgh.pa.us 3037 [ + + - + : 55978 : if (RelationIsMapped(relation))
- - - - -
- + + ]
3038 : : {
632 heikki.linnakangas@i 3039 : 43387 : RelationCloseSmgr(relation);
5325 tgl@sss.pgh.pa.us 3040 : 43387 : RelationInitPhysicalAddr(relation);
3041 : : }
3042 : :
3043 : : /*
3044 : : * Add this entry to list of stuff to rebuild in second pass.
3045 : : * pg_class goes to the front of rebuildFirstList while
3046 : : * pg_class_oid_index goes to the back of rebuildFirstList, so
3047 : : * they are done first and second respectively. Other nailed
3048 : : * relations go to the front of rebuildList, so they'll be done
3049 : : * next in no particular order; and everything else goes to the
3050 : : * back of rebuildList.
3051 : : */
3052 [ + + ]: 55978 : if (RelationGetRelid(relation) == RelationRelationId)
3053 : 2496 : rebuildFirstList = lcons(relation, rebuildFirstList);
3054 [ + + ]: 53482 : else if (RelationGetRelid(relation) == ClassOidIndexId)
3055 : 2496 : rebuildFirstList = lappend(rebuildFirstList, relation);
3056 [ + + ]: 50986 : else if (relation->rd_isnailed)
8208 3057 : 50875 : rebuildList = lcons(relation, rebuildList);
3058 : : else
5325 3059 : 111 : rebuildList = lappend(rebuildList, relation);
3060 : : }
3061 : : }
3062 : :
3063 : : /*
3064 : : * We cannot destroy the SMgrRelations as there might still be references
3065 : : * to them, but close the underlying file descriptors.
3066 : : */
734 heikki.linnakangas@i 3067 : 2687 : smgrreleaseall();
3068 : :
3069 : : /*
3070 : : * Phase 2: rebuild (or invalidate) the items found to need rebuild in
3071 : : * phase 1
3072 : : */
7360 tgl@sss.pgh.pa.us 3073 [ + + + + : 7679 : foreach(l, rebuildFirstList)
+ + ]
3074 : : {
3075 : 4992 : relation = (Relation) lfirst(l);
500 heikki.linnakangas@i 3076 [ + + + - : 4992 : if (!IsTransactionState() || (relation->rd_isnailed && relation->rd_refcnt == 1))
+ + ]
3077 : 4991 : RelationInvalidateRelation(relation);
3078 : : else
3079 : 1 : RelationRebuildRelation(relation);
3080 : : }
7360 tgl@sss.pgh.pa.us 3081 : 2687 : list_free(rebuildFirstList);
8927 3082 [ + - + + : 53673 : foreach(l, rebuildList)
+ + ]
3083 : : {
3084 : 50986 : relation = (Relation) lfirst(l);
500 heikki.linnakangas@i 3085 [ + + + + : 50986 : if (!IsTransactionState() || (relation->rd_isnailed && relation->rd_refcnt == 1))
+ + ]
3086 : 50867 : RelationInvalidateRelation(relation);
3087 : : else
3088 : 119 : RelationRebuildRelation(relation);
3089 : : }
7959 neilc@samurai.com 3090 : 2687 : list_free(rebuildList);
3091 : :
1604 noah@leadboat.com 3092 [ + - ]: 2687 : if (!debug_discard)
3093 : : /* Any RelationBuildDesc() on the stack must start over. */
3094 [ + + ]: 2691 : for (i = 0; i < in_progress_list_len; i++)
3095 : 4 : in_progress_list[i].invalidated = true;
10841 scrappy@hub.org 3096 : 2687 : }
3097 : :
3098 : : static void
4361 simon@2ndQuadrant.co 3099 : 12021 : RememberToFreeTupleDescAtEOX(TupleDesc td)
3100 : : {
3101 [ + + ]: 12021 : if (EOXactTupleDescArray == NULL)
3102 : : {
3103 : : MemoryContext oldcxt;
3104 : :
3105 : 6485 : oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
3106 : :
3107 : 6485 : EOXactTupleDescArray = (TupleDesc *) palloc(16 * sizeof(TupleDesc));
3108 : 6485 : EOXactTupleDescArrayLen = 16;
3109 : 6485 : NextEOXactTupleDescNum = 0;
3110 : 6485 : MemoryContextSwitchTo(oldcxt);
3111 : : }
3112 [ + + ]: 5536 : else if (NextEOXactTupleDescNum >= EOXactTupleDescArrayLen)
3113 : : {
4331 bruce@momjian.us 3114 : 31 : int32 newlen = EOXactTupleDescArrayLen * 2;
3115 : :
4361 simon@2ndQuadrant.co 3116 [ - + ]: 31 : Assert(EOXactTupleDescArrayLen > 0);
3117 : :
3118 : 31 : EOXactTupleDescArray = (TupleDesc *) repalloc(EOXactTupleDescArray,
3119 : : newlen * sizeof(TupleDesc));
3120 : 31 : EOXactTupleDescArrayLen = newlen;
3121 : : }
3122 : :
3123 : 12021 : EOXactTupleDescArray[NextEOXactTupleDescNum++] = td;
3124 : 12021 : }
3125 : :
3126 : : #ifdef USE_ASSERT_CHECKING
3127 : : static void
2171 noah@leadboat.com 3128 : 266670 : AssertPendingSyncConsistency(Relation relation)
3129 : : {
3130 : 266670 : bool relcache_verdict =
1031 tgl@sss.pgh.pa.us 3131 [ + + ]: 533309 : RelationIsPermanent(relation) &&
3132 [ + + ]: 266639 : ((relation->rd_createSubid != InvalidSubTransactionId &&
3133 [ + + + + : 3415 : RELKIND_HAS_STORAGE(relation->rd_rel->relkind)) ||
+ + + + +
+ ]
3134 [ + + ]: 263412 : relation->rd_firstRelfilelocatorSubid != InvalidSubTransactionId);
3135 : :
1348 rhaas@postgresql.org 3136 [ - + ]: 266670 : Assert(relcache_verdict == RelFileLocatorSkippingWAL(relation->rd_locator));
3137 : :
2171 noah@leadboat.com 3138 [ + + ]: 266670 : if (relation->rd_droppedSubid != InvalidSubTransactionId)
3139 [ + - - + : 100 : Assert(!relation->rd_isvalid &&
- - ]
3140 : : (relation->rd_createSubid != InvalidSubTransactionId ||
3141 : : relation->rd_firstRelfilelocatorSubid != InvalidSubTransactionId));
3142 : 266670 : }
3143 : :
3144 : : /*
3145 : : * AssertPendingSyncs_RelationCache
3146 : : *
3147 : : * Assert that relcache.c and storage.c agree on whether to skip WAL.
3148 : : */
3149 : : void
3150 : 1975 : AssertPendingSyncs_RelationCache(void)
3151 : : {
3152 : : HASH_SEQ_STATUS status;
3153 : : LOCALLOCK *locallock;
3154 : : Relation *rels;
3155 : : int maxrels;
3156 : : int nrels;
3157 : : RelIdCacheEnt *idhentry;
3158 : : int i;
3159 : :
3160 : : /*
3161 : : * Open every relation that this transaction has locked. If, for some
3162 : : * relation, storage.c is skipping WAL and relcache.c is not skipping WAL,
3163 : : * a CommandCounterIncrement() typically yields a local invalidation
3164 : : * message that destroys the relcache entry. By recreating such entries
3165 : : * here, we detect the problem.
3166 : : */
3167 : 1975 : PushActiveSnapshot(GetTransactionSnapshot());
3168 : 1975 : maxrels = 1;
3169 : 1975 : rels = palloc(maxrels * sizeof(*rels));
3170 : 1975 : nrels = 0;
3171 : 1975 : hash_seq_init(&status, GetLockMethodLocalHash());
3172 [ + + ]: 16139 : while ((locallock = (LOCALLOCK *) hash_seq_search(&status)) != NULL)
3173 : : {
3174 : : Oid relid;
3175 : : Relation r;
3176 : :
3177 [ - + ]: 14164 : if (locallock->nLocks <= 0)
2171 noah@leadboat.com 3178 :UBC 0 : continue;
2171 noah@leadboat.com 3179 [ + + ]:CBC 14164 : if ((LockTagType) locallock->tag.lock.locktag_type !=
3180 : : LOCKTAG_RELATION)
3181 : 3631 : continue;
219 peter@eisentraut.org 3182 :GNC 10533 : relid = locallock->tag.lock.locktag_field2;
2171 noah@leadboat.com 3183 :CBC 10533 : r = RelationIdGetRelation(relid);
3184 [ + + ]: 10533 : if (!RelationIsValid(r))
3185 : 364 : continue;
3186 [ + + ]: 10169 : if (nrels >= maxrels)
3187 : : {
3188 : 3359 : maxrels *= 2;
3189 : 3359 : rels = repalloc(rels, maxrels * sizeof(*rels));
3190 : : }
3191 : 10169 : rels[nrels++] = r;
3192 : : }
3193 : :
3194 : 1975 : hash_seq_init(&status, RelationIdCache);
3195 [ + + ]: 268645 : while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
3196 : 266670 : AssertPendingSyncConsistency(idhentry->reldesc);
3197 : :
3198 [ + + ]: 12144 : for (i = 0; i < nrels; i++)
3199 : 10169 : RelationClose(rels[i]);
3200 : 1975 : PopActiveSnapshot();
3201 : 1975 : }
3202 : : #endif
3203 : :
3204 : : /*
3205 : : * AtEOXact_RelationCache
3206 : : *
3207 : : * Clean up the relcache at main-transaction commit or abort.
3208 : : *
3209 : : * Note: this must be called *before* processing invalidation messages.
3210 : : * In the case of abort, we don't want to try to rebuild any invalidated
3211 : : * cache entries (since we can't safely do database accesses). Therefore
3212 : : * we must reset refcnts before handling pending invalidations.
3213 : : *
3214 : : * As of PostgreSQL 8.1, relcache refcnts should get released by the
3215 : : * ResourceOwner mechanism. This routine just does a debugging
3216 : : * cross-check that no pins remain. However, we also need to do special
3217 : : * cleanup when the current transaction created any relations or made use
3218 : : * of forced index lists.
3219 : : */
3220 : : void
7927 tgl@sss.pgh.pa.us 3221 : 339521 : AtEOXact_RelationCache(bool isCommit)
3222 : : {
3223 : : HASH_SEQ_STATUS status;
3224 : : RelIdCacheEnt *idhentry;
3225 : : int i;
3226 : :
3227 : : /*
3228 : : * Forget in_progress_list. This is relevant when we're aborting due to
3229 : : * an error during RelationBuildDesc().
3230 : : */
1604 noah@leadboat.com 3231 [ + + - + ]: 339521 : Assert(in_progress_list_len == 0 || !isCommit);
3232 : 339521 : in_progress_list_len = 0;
3233 : :
3234 : : /*
3235 : : * Unless the eoxact_list[] overflowed, we only need to examine the rels
3236 : : * listed in it. Otherwise fall back on a hash_seq_search scan.
3237 : : *
3238 : : * For simplicity, eoxact_list[] entries are not deleted till end of
3239 : : * top-level transaction, even though we could remove them at
3240 : : * subtransaction end in some cases, or remove relations from the list if
3241 : : * they are cleared for other reasons. Therefore we should expect the
3242 : : * case that list entries are not found in the hashtable; if not, there's
3243 : : * nothing to do for them.
3244 : : */
4802 tgl@sss.pgh.pa.us 3245 [ + + ]: 339521 : if (eoxact_list_overflowed)
3246 : : {
3247 : 82 : hash_seq_init(&status, RelationIdCache);
3248 [ + + ]: 22196 : while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
3249 : : {
3250 : 22114 : AtEOXact_cleanup(idhentry->reldesc, isCommit);
3251 : : }
3252 : : }
3253 : : else
3254 : : {
3255 [ + + ]: 402203 : for (i = 0; i < eoxact_list_len; i++)
3256 : : {
3257 : 62764 : idhentry = (RelIdCacheEnt *) hash_search(RelationIdCache,
1133 peter@eisentraut.org 3258 : 62764 : &eoxact_list[i],
3259 : : HASH_FIND,
3260 : : NULL);
4802 tgl@sss.pgh.pa.us 3261 [ + + ]: 62764 : if (idhentry != NULL)
3262 : 61537 : AtEOXact_cleanup(idhentry->reldesc, isCommit);
3263 : : }
3264 : : }
3265 : :
4361 simon@2ndQuadrant.co 3266 [ + + ]: 339521 : if (EOXactTupleDescArrayLen > 0)
3267 : : {
3268 [ - + ]: 6485 : Assert(EOXactTupleDescArray != NULL);
3269 [ + + ]: 18506 : for (i = 0; i < NextEOXactTupleDescNum; i++)
3270 : 12021 : FreeTupleDesc(EOXactTupleDescArray[i]);
3271 : 6485 : pfree(EOXactTupleDescArray);
3272 : 6485 : EOXactTupleDescArray = NULL;
3273 : : }
3274 : :
3275 : : /* Now we're out of the transaction and can clear the lists */
4802 tgl@sss.pgh.pa.us 3276 : 339521 : eoxact_list_len = 0;
3277 : 339521 : eoxact_list_overflowed = false;
4361 simon@2ndQuadrant.co 3278 : 339521 : NextEOXactTupleDescNum = 0;
3279 : 339521 : EOXactTupleDescArrayLen = 0;
4802 tgl@sss.pgh.pa.us 3280 : 339521 : }
3281 : :
3282 : : /*
3283 : : * AtEOXact_cleanup
3284 : : *
3285 : : * Clean up a single rel at main-transaction commit or abort
3286 : : *
3287 : : * NB: this processing must be idempotent, because EOXactListAdd() doesn't
3288 : : * bother to prevent duplicate entries in eoxact_list[].
3289 : : */
3290 : : static void
3291 : 83651 : AtEOXact_cleanup(Relation relation, bool isCommit)
3292 : : {
2171 noah@leadboat.com 3293 : 83651 : bool clear_relcache = false;
3294 : :
3295 : : /*
3296 : : * The relcache entry's ref count should be back to its normal
3297 : : * not-in-a-transaction state: 0 unless it's nailed in cache.
3298 : : *
3299 : : * In bootstrap mode, this is NOT true, so don't check it --- the
3300 : : * bootstrap code expects relations to stay open across start/commit
3301 : : * transaction calls. (That seems bogus, but it's not worth fixing.)
3302 : : *
3303 : : * Note: ideally this check would be applied to every relcache entry, not
3304 : : * just those that have eoxact work to do. But it's not worth forcing a
3305 : : * scan of the whole relcache just for this. (Moreover, doing so would
3306 : : * mean that assert-enabled testing never tests the hash_search code path
3307 : : * above, which seems a bad idea.)
3308 : : */
3309 : : #ifdef USE_ASSERT_CHECKING
4673 bruce@momjian.us 3310 [ + + ]: 83651 : if (!IsBootstrapProcessingMode())
3311 : : {
3312 : : int expected_refcnt;
3313 : :
3314 : 70493 : expected_refcnt = relation->rd_isnailed ? 1 : 0;
3315 [ - + ]: 70493 : Assert(relation->rd_refcnt == expected_refcnt);
3316 : : }
3317 : : #endif
3318 : :
3319 : : /*
3320 : : * Is the relation live after this transaction ends?
3321 : : *
3322 : : * During commit, clear the relcache entry if it is preserved after
3323 : : * relation drop, in order not to orphan the entry. During rollback,
3324 : : * clear the relcache entry if the relation is created in the current
3325 : : * transaction since it isn't interesting any longer once we are out of
3326 : : * the transaction.
3327 : : */
2171 noah@leadboat.com 3328 : 83651 : clear_relcache =
3329 : : (isCommit ?
3330 [ + + ]: 83651 : relation->rd_droppedSubid != InvalidSubTransactionId :
3331 : 2490 : relation->rd_createSubid != InvalidSubTransactionId);
3332 : :
3333 : : /*
3334 : : * Since we are now out of the transaction, reset the subids to zero. That
3335 : : * also lets RelationClearRelation() drop the relcache entry.
3336 : : */
3337 : 83651 : relation->rd_createSubid = InvalidSubTransactionId;
1348 rhaas@postgresql.org 3338 : 83651 : relation->rd_newRelfilelocatorSubid = InvalidSubTransactionId;
3339 : 83651 : relation->rd_firstRelfilelocatorSubid = InvalidSubTransactionId;
2171 noah@leadboat.com 3340 : 83651 : relation->rd_droppedSubid = InvalidSubTransactionId;
3341 : :
3342 [ + + ]: 83651 : if (clear_relcache)
3343 : : {
3344 [ + - ]: 2882 : if (RelationHasReferenceCountZero(relation))
3345 : : {
500 heikki.linnakangas@i 3346 : 2882 : RelationClearRelation(relation);
4673 bruce@momjian.us 3347 : 2882 : return;
3348 : : }
3349 : : else
3350 : : {
3351 : : /*
3352 : : * Hmm, somewhere there's a (leaked?) reference to the relation.
3353 : : * We daren't remove the entry for fear of dereferencing a
3354 : : * dangling pointer later. Bleat, and mark it as not belonging to
3355 : : * the current transaction. Hopefully it'll get cleaned up
3356 : : * eventually. This must be just a WARNING to avoid
3357 : : * error-during-error-recovery loops.
3358 : : */
3845 tgl@sss.pgh.pa.us 3359 [ # # ]:UBC 0 : elog(WARNING, "cannot remove relcache entry for \"%s\" because it has nonzero refcount",
3360 : : RelationGetRelationName(relation));
3361 : : }
3362 : : }
3363 : : }
3364 : :
3365 : : /*
3366 : : * AtEOSubXact_RelationCache
3367 : : *
3368 : : * Clean up the relcache at sub-transaction commit or abort.
3369 : : *
3370 : : * Note: this must be called *before* processing invalidation messages.
3371 : : */
3372 : : void
7850 tgl@sss.pgh.pa.us 3373 :CBC 11693 : AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid,
3374 : : SubTransactionId parentSubid)
3375 : : {
3376 : : HASH_SEQ_STATUS status;
3377 : : RelIdCacheEnt *idhentry;
3378 : : int i;
3379 : :
3380 : : /*
3381 : : * Forget in_progress_list. This is relevant when we're aborting due to
3382 : : * an error during RelationBuildDesc(). We don't commit subtransactions
3383 : : * during RelationBuildDesc().
3384 : : */
1604 noah@leadboat.com 3385 [ - + - - ]: 11693 : Assert(in_progress_list_len == 0 || !isCommit);
3386 : 11693 : in_progress_list_len = 0;
3387 : :
3388 : : /*
3389 : : * Unless the eoxact_list[] overflowed, we only need to examine the rels
3390 : : * listed in it. Otherwise fall back on a hash_seq_search scan. Same
3391 : : * logic as in AtEOXact_RelationCache.
3392 : : */
4802 tgl@sss.pgh.pa.us 3393 [ - + ]: 11693 : if (eoxact_list_overflowed)
3394 : : {
4802 tgl@sss.pgh.pa.us 3395 :UBC 0 : hash_seq_init(&status, RelationIdCache);
3396 [ # # ]: 0 : while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
3397 : : {
3398 : 0 : AtEOSubXact_cleanup(idhentry->reldesc, isCommit,
3399 : : mySubid, parentSubid);
3400 : : }
3401 : : }
3402 : : else
3403 : : {
4802 tgl@sss.pgh.pa.us 3404 [ + + ]:CBC 16473 : for (i = 0; i < eoxact_list_len; i++)
3405 : : {
3406 : 4780 : idhentry = (RelIdCacheEnt *) hash_search(RelationIdCache,
1133 peter@eisentraut.org 3407 : 4780 : &eoxact_list[i],
3408 : : HASH_FIND,
3409 : : NULL);
4802 tgl@sss.pgh.pa.us 3410 [ + + ]: 4780 : if (idhentry != NULL)
3411 : 4284 : AtEOSubXact_cleanup(idhentry->reldesc, isCommit,
3412 : : mySubid, parentSubid);
3413 : : }
3414 : : }
3415 : :
3416 : : /* Don't reset the list; we still need more cleanup later */
3417 : 11693 : }
3418 : :
3419 : : /*
3420 : : * AtEOSubXact_cleanup
3421 : : *
3422 : : * Clean up a single rel at subtransaction commit or abort
3423 : : *
3424 : : * NB: this processing must be idempotent, because EOXactListAdd() doesn't
3425 : : * bother to prevent duplicate entries in eoxact_list[].
3426 : : */
3427 : : static void
3428 : 4284 : AtEOSubXact_cleanup(Relation relation, bool isCommit,
3429 : : SubTransactionId mySubid, SubTransactionId parentSubid)
3430 : : {
3431 : : /*
3432 : : * Is it a relation created in the current subtransaction?
3433 : : *
3434 : : * During subcommit, mark it as belonging to the parent, instead, as long
3435 : : * as it has not been dropped. Otherwise simply delete the relcache entry.
3436 : : * --- it isn't interesting any longer.
3437 : : */
4673 bruce@momjian.us 3438 [ + + ]: 4284 : if (relation->rd_createSubid == mySubid)
3439 : : {
3440 : : /*
3441 : : * Valid rd_droppedSubid means the corresponding relation is dropped
3442 : : * but the relcache entry is preserved for at-commit pending sync. We
3443 : : * need to drop it explicitly here not to make the entry orphan.
3444 : : */
2171 noah@leadboat.com 3445 [ + + - + ]: 99 : Assert(relation->rd_droppedSubid == mySubid ||
3446 : : relation->rd_droppedSubid == InvalidSubTransactionId);
3447 [ + + + - ]: 99 : if (isCommit && relation->rd_droppedSubid == InvalidSubTransactionId)
4673 bruce@momjian.us 3448 : 37 : relation->rd_createSubid = parentSubid;
3845 tgl@sss.pgh.pa.us 3449 [ + - ]: 62 : else if (RelationHasReferenceCountZero(relation))
3450 : : {
3451 : : /* allow the entry to be removed */
2171 noah@leadboat.com 3452 : 62 : relation->rd_createSubid = InvalidSubTransactionId;
1348 rhaas@postgresql.org 3453 : 62 : relation->rd_newRelfilelocatorSubid = InvalidSubTransactionId;
3454 : 62 : relation->rd_firstRelfilelocatorSubid = InvalidSubTransactionId;
2171 noah@leadboat.com 3455 : 62 : relation->rd_droppedSubid = InvalidSubTransactionId;
500 heikki.linnakangas@i 3456 : 62 : RelationClearRelation(relation);
4673 bruce@momjian.us 3457 : 62 : return;
3458 : : }
3459 : : else
3460 : : {
3461 : : /*
3462 : : * Hmm, somewhere there's a (leaked?) reference to the relation.
3463 : : * We daren't remove the entry for fear of dereferencing a
3464 : : * dangling pointer later. Bleat, and transfer it to the parent
3465 : : * subtransaction so we can try again later. This must be just a
3466 : : * WARNING to avoid error-during-error-recovery loops.
3467 : : */
3845 tgl@sss.pgh.pa.us 3468 :UBC 0 : relation->rd_createSubid = parentSubid;
3469 [ # # ]: 0 : elog(WARNING, "cannot remove relcache entry for \"%s\" because it has nonzero refcount",
3470 : : RelationGetRelationName(relation));
3471 : : }
3472 : : }
3473 : :
3474 : : /*
3475 : : * Likewise, update or drop any new-relfilenumber-in-subtransaction record
3476 : : * or drop record.
3477 : : */
1348 rhaas@postgresql.org 3478 [ + + ]:CBC 4222 : if (relation->rd_newRelfilelocatorSubid == mySubid)
3479 : : {
4673 bruce@momjian.us 3480 [ + + ]: 73 : if (isCommit)
1348 rhaas@postgresql.org 3481 : 39 : relation->rd_newRelfilelocatorSubid = parentSubid;
3482 : : else
3483 : 34 : relation->rd_newRelfilelocatorSubid = InvalidSubTransactionId;
3484 : : }
3485 : :
3486 [ + + ]: 4222 : if (relation->rd_firstRelfilelocatorSubid == mySubid)
3487 : : {
2171 noah@leadboat.com 3488 [ + + ]: 55 : if (isCommit)
1348 rhaas@postgresql.org 3489 : 27 : relation->rd_firstRelfilelocatorSubid = parentSubid;
3490 : : else
3491 : 28 : relation->rd_firstRelfilelocatorSubid = InvalidSubTransactionId;
3492 : : }
3493 : :
2171 noah@leadboat.com 3494 [ + + ]: 4222 : if (relation->rd_droppedSubid == mySubid)
3495 : : {
3496 [ + + ]: 16 : if (isCommit)
3497 : 1 : relation->rd_droppedSubid = parentSubid;
3498 : : else
3499 : 15 : relation->rd_droppedSubid = InvalidSubTransactionId;
3500 : : }
3501 : : }
3502 : :
3503 : :
3504 : : /*
3505 : : * RelationBuildLocalRelation
3506 : : * Build a relcache entry for an about-to-be-created relation,
3507 : : * and enter it into the relcache.
3508 : : */
3509 : : Relation
9025 tgl@sss.pgh.pa.us 3510 : 71502 : RelationBuildLocalRelation(const char *relname,
3511 : : Oid relnamespace,
3512 : : TupleDesc tupDesc,
3513 : : Oid relid,
3514 : : Oid accessmtd,
3515 : : RelFileNumber relfilenumber,
3516 : : Oid reltablespace,
3517 : : bool shared_relation,
3518 : : bool mapped_relation,
3519 : : char relpersistence,
3520 : : char relkind)
3521 : : {
3522 : : Relation rel;
3523 : : MemoryContext oldcxt;
3524 : 71502 : int natts = tupDesc->natts;
3525 : : int i;
3526 : : bool has_not_null;
3527 : : bool nailit;
3528 : :
1234 peter@eisentraut.org 3529 [ - + ]: 71502 : Assert(natts >= 0);
3530 : :
3531 : : /*
3532 : : * check for creation of a rel that must be nailed in cache.
3533 : : *
3534 : : * XXX this list had better match the relations specially handled in
3535 : : * RelationCacheInitializePhase2/3.
3536 : : */
7640 tgl@sss.pgh.pa.us 3537 [ + + ]: 71502 : switch (relid)
3538 : : {
6059 3539 : 357 : case DatabaseRelationId:
3540 : : case AuthIdRelationId:
3541 : : case AuthMemRelationId:
3542 : : case RelationRelationId:
3543 : : case AttributeRelationId:
3544 : : case ProcedureRelationId:
3545 : : case TypeRelationId:
7640 3546 : 357 : nailit = true;
3547 : 357 : break;
3548 : 71145 : default:
3549 : 71145 : nailit = false;
3550 : 71145 : break;
3551 : : }
3552 : :
3553 : : /*
3554 : : * check that hardwired list of shared rels matches what's in the
3555 : : * bootstrap .bki file. If you get a failure here during initdb, you
3556 : : * probably need to fix IsSharedRelation() to match whatever you've done
3557 : : * to the set of shared relations.
3558 : : */
7167 3559 [ - + ]: 71502 : if (shared_relation != IsSharedRelation(relid))
7167 tgl@sss.pgh.pa.us 3560 [ # # ]:UBC 0 : elog(ERROR, "shared_relation flag for \"%s\" does not match IsSharedRelation(%u)",
3561 : : relname, relid);
3562 : :
3563 : : /* Shared relations had better be mapped, too */
5880 tgl@sss.pgh.pa.us 3564 [ + + - + ]:CBC 71502 : Assert(mapped_relation || !shared_relation);
3565 : :
3566 : : /*
3567 : : * switch to the cache context to create the relcache entry.
3568 : : */
9025 3569 [ - + ]: 71502 : if (!CacheMemoryContext)
9025 tgl@sss.pgh.pa.us 3570 :UBC 0 : CreateCacheMemoryContext();
3571 : :
9389 tgl@sss.pgh.pa.us 3572 :CBC 71502 : oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
3573 : :
3574 : : /*
3575 : : * allocate a new relation descriptor and fill in basic state fields.
3576 : : */
95 michael@paquier.xyz 3577 :GNC 71502 : rel = palloc0_object(RelationData);
3578 : :
3579 : : /* make sure relation is marked as having no open file yet */
8069 tgl@sss.pgh.pa.us 3580 :CBC 71502 : rel->rd_smgr = NULL;
3581 : :
3582 : : /* mark it nailed if appropriate */
7640 3583 : 71502 : rel->rd_isnailed = nailit;
3584 : :
7911 3585 : 71502 : rel->rd_refcnt = nailit ? 1 : 0;
3586 : :
3587 : : /* it's being created in this transaction */
7850 3588 : 71502 : rel->rd_createSubid = GetCurrentSubTransactionId();
1348 rhaas@postgresql.org 3589 : 71502 : rel->rd_newRelfilelocatorSubid = InvalidSubTransactionId;
3590 : 71502 : rel->rd_firstRelfilelocatorSubid = InvalidSubTransactionId;
2171 noah@leadboat.com 3591 : 71502 : rel->rd_droppedSubid = InvalidSubTransactionId;
3592 : :
3593 : : /*
3594 : : * create a new tuple descriptor from the one passed in. We do this
3595 : : * partly to copy it into the cache context, and partly because the new
3596 : : * relation can't have any defaults or constraints yet; they have to be
3597 : : * added in later steps, because they require additions to multiple system
3598 : : * catalogs. We can copy attnotnull constraints here, however.
3599 : : */
8778 tgl@sss.pgh.pa.us 3600 : 71502 : rel->rd_att = CreateTupleDescCopy(tupDesc);
7212 3601 : 71502 : rel->rd_att->tdrefcount = 1; /* mark as refcounted */
8521 3602 : 71502 : has_not_null = false;
8778 3603 [ + + ]: 309489 : for (i = 0; i < natts; i++)
3604 : : {
3129 andres@anarazel.de 3605 : 237987 : Form_pg_attribute satt = TupleDescAttr(tupDesc, i);
3606 : 237987 : Form_pg_attribute datt = TupleDescAttr(rel->rd_att, i);
3607 : :
3608 : 237987 : datt->attidentity = satt->attidentity;
2542 peter@eisentraut.org 3609 : 237987 : datt->attgenerated = satt->attgenerated;
3129 andres@anarazel.de 3610 : 237987 : datt->attnotnull = satt->attnotnull;
3611 : 237987 : has_not_null |= satt->attnotnull;
450 drowley@postgresql.o 3612 : 237987 : populate_compact_attribute(rel->rd_att, i);
3613 : :
342 alvherre@alvh.no-ip. 3614 [ + + ]: 237987 : if (satt->attnotnull)
3615 : : {
3616 : 39665 : CompactAttribute *scatt = TupleDescCompactAttr(tupDesc, i);
3617 : 39665 : CompactAttribute *dcatt = TupleDescCompactAttr(rel->rd_att, i);
3618 : :
3619 : 39665 : dcatt->attnullability = scatt->attnullability;
3620 : : }
3621 : : }
3622 : :
8521 tgl@sss.pgh.pa.us 3623 [ + + ]: 71502 : if (has_not_null)
3624 : : {
95 michael@paquier.xyz 3625 :GNC 10878 : TupleConstr *constr = palloc0_object(TupleConstr);
3626 : :
8521 tgl@sss.pgh.pa.us 3627 :CBC 10878 : constr->has_not_null = true;
3628 : 10878 : rel->rd_att->constr = constr;
3629 : : }
3630 : :
3631 : : /*
3632 : : * initialize relation tuple form (caller may add/override data later)
3633 : : */
8523 bruce@momjian.us 3634 : 71502 : rel->rd_rel = (Form_pg_class) palloc0(CLASS_TUPLE_SIZE);
3635 : :
8755 tgl@sss.pgh.pa.us 3636 : 71502 : namestrcpy(&rel->rd_rel->relname, relname);
3637 : 71502 : rel->rd_rel->relnamespace = relnamespace;
3638 : :
5022 rhaas@postgresql.org 3639 : 71502 : rel->rd_rel->relkind = relkind;
9025 tgl@sss.pgh.pa.us 3640 : 71502 : rel->rd_rel->relnatts = natts;
3641 : 71502 : rel->rd_rel->reltype = InvalidOid;
3642 : : /* needed when bootstrapping: */
7506 3643 : 71502 : rel->rd_rel->relowner = BOOTSTRAP_SUPERUSERID;
3644 : :
3645 : : /* set up persistence and relcache fields dependent on it */
5571 rhaas@postgresql.org 3646 : 71502 : rel->rd_rel->relpersistence = relpersistence;
3647 [ + + - ]: 71502 : switch (relpersistence)
3648 : : {
5555 3649 : 68210 : case RELPERSISTENCE_UNLOGGED:
3650 : : case RELPERSISTENCE_PERMANENT:
742 heikki.linnakangas@i 3651 : 68210 : rel->rd_backend = INVALID_PROC_NUMBER;
4836 tgl@sss.pgh.pa.us 3652 : 68210 : rel->rd_islocaltemp = false;
5571 rhaas@postgresql.org 3653 : 68210 : break;
3654 : 3292 : case RELPERSISTENCE_TEMP:
4220 bruce@momjian.us 3655 [ - + ]: 3292 : Assert(isTempOrTempToastNamespace(relnamespace));
742 heikki.linnakangas@i 3656 [ + - ]: 3292 : rel->rd_backend = ProcNumberForTempRelations();
4836 tgl@sss.pgh.pa.us 3657 : 3292 : rel->rd_islocaltemp = true;
5571 rhaas@postgresql.org 3658 : 3292 : break;
5571 rhaas@postgresql.org 3659 :UBC 0 : default:
3660 [ # # ]: 0 : elog(ERROR, "invalid relpersistence: %c", relpersistence);
3661 : : break;
3662 : : }
3663 : :
3664 : : /* if it's a materialized view, it's not populated initially */
4696 tgl@sss.pgh.pa.us 3665 [ + + ]:CBC 71502 : if (relkind == RELKIND_MATVIEW)
3666 : 228 : rel->rd_rel->relispopulated = false;
3667 : : else
3668 : 71274 : rel->rd_rel->relispopulated = true;
3669 : :
3670 : : /* set replica identity -- system catalogs and non-tables don't have one */
2503 3671 [ + + + + ]: 71502 : if (!IsCatalogNamespace(relnamespace) &&
3385 rhaas@postgresql.org 3672 [ + + ]: 38658 : (relkind == RELKIND_RELATION ||
3673 [ + + ]: 38430 : relkind == RELKIND_MATVIEW ||
3674 : : relkind == RELKIND_PARTITIONED_TABLE))
4510 3675 : 22055 : rel->rd_rel->relreplident = REPLICA_IDENTITY_DEFAULT;
3676 : : else
3677 : 49447 : rel->rd_rel->relreplident = REPLICA_IDENTITY_NOTHING;
3678 : :
3679 : : /*
3680 : : * Insert relation physical and logical identifiers (OIDs) into the right
3681 : : * places. For a mapped relation, we set relfilenumber to zero and rely
3682 : : * on RelationInitPhysicalAddr to consult the map.
3683 : : */
7940 tgl@sss.pgh.pa.us 3684 : 71502 : rel->rd_rel->relisshared = shared_relation;
3685 : :
9025 3686 : 71502 : RelationGetRelid(rel) = relid;
3687 : :
3688 [ + + ]: 309489 : for (i = 0; i < natts; i++)
3129 andres@anarazel.de 3689 : 237987 : TupleDescAttr(rel->rd_att, i)->attrelid = relid;
3690 : :
7940 tgl@sss.pgh.pa.us 3691 : 71502 : rel->rd_rel->reltablespace = reltablespace;
3692 : :
5880 3693 [ + + ]: 71502 : if (mapped_relation)
3694 : : {
1348 rhaas@postgresql.org 3695 : 3298 : rel->rd_rel->relfilenode = InvalidRelFileNumber;
3696 : : /* Add it to the active mapping information */
3697 : 3298 : RelationMapUpdateMap(relid, relfilenumber, shared_relation, true);
3698 : : }
3699 : : else
3700 : 68204 : rel->rd_rel->relfilenode = relfilenumber;
3701 : :
8755 tgl@sss.pgh.pa.us 3702 : 71502 : RelationInitLockInfo(rel); /* see lmgr.c */
3703 : :
7940 3704 : 71502 : RelationInitPhysicalAddr(rel);
3705 : :
2566 andres@anarazel.de 3706 : 71502 : rel->rd_rel->relam = accessmtd;
3707 : :
3708 : : /*
3709 : : * RelationInitTableAccessMethod will do syscache lookups, so we mustn't
3710 : : * run it in CacheMemoryContext. Fortunately, the remaining steps don't
3711 : : * require a long-lived current context.
3712 : : */
1823 tgl@sss.pgh.pa.us 3713 : 71502 : MemoryContextSwitchTo(oldcxt);
3714 : :
1563 peter@eisentraut.org 3715 [ + + + + : 71502 : if (RELKIND_HAS_TABLE_AM(relkind) || relkind == RELKIND_SEQUENCE)
+ + + + ]
2566 andres@anarazel.de 3716 : 32618 : RelationInitTableAccessMethod(rel);
3717 : :
3718 : : /*
3719 : : * Leave index access method uninitialized, because the pg_index row has
3720 : : * not been inserted at this stage of index creation yet. The cache
3721 : : * invalidation after pg_index row has been inserted will initialize it.
3722 : : */
3723 : :
3724 : : /*
3725 : : * Okay to insert into the relcache hash table.
3726 : : *
3727 : : * Ordinarily, there should certainly not be an existing hash entry for
3728 : : * the same OID; but during bootstrap, when we create a "real" relcache
3729 : : * entry for one of the bootstrap relations, we'll be overwriting the
3730 : : * phony one created with formrdesc. So allow that to happen for nailed
3731 : : * rels.
3732 : : */
4319 tgl@sss.pgh.pa.us 3733 [ + + - + : 71502 : RelationCacheInsert(rel, nailit);
- + - + -
- ]
3734 : :
3735 : : /*
3736 : : * Flag relation as needing eoxact cleanup (to clear rd_createSubid). We
3737 : : * can't do this before storing relid in it.
3738 : : */
4802 3739 [ + + ]: 71502 : EOXactListAdd(rel);
3740 : :
3741 : : /* It's fully valid */
7869 3742 : 71502 : rel->rd_isvalid = true;
3743 : :
3744 : : /*
3745 : : * Caller expects us to pin the returned entry.
3746 : : */
7911 3747 : 71502 : RelationIncrementReferenceCount(rel);
3748 : :
9025 3749 : 71502 : return rel;
3750 : : }
3751 : :
3752 : :
3753 : : /*
3754 : : * RelationSetNewRelfilenumber
3755 : : *
3756 : : * Assign a new relfilenumber (physical file name), and possibly a new
3757 : : * persistence setting, to the relation.
3758 : : *
3759 : : * This allows a full rewrite of the relation to be done with transactional
3760 : : * safety (since the filenumber assignment can be rolled back). Note however
3761 : : * that there is no simple way to access the relation's old data for the
3762 : : * remainder of the current transaction. This limits the usefulness to cases
3763 : : * such as TRUNCATE or rebuilding an index from scratch.
3764 : : *
3765 : : * Caller must already hold exclusive lock on the relation.
3766 : : */
3767 : : void
1348 rhaas@postgresql.org 3768 : 6728 : RelationSetNewRelfilenumber(Relation relation, char persistence)
3769 : : {
3770 : : RelFileNumber newrelfilenumber;
3771 : : Relation pg_class;
3772 : : ItemPointerData otid;
3773 : : HeapTuple tuple;
3774 : : Form_pg_class classform;
2544 andres@anarazel.de 3775 : 6728 : MultiXactId minmulti = InvalidMultiXactId;
3776 : 6728 : TransactionId freezeXid = InvalidTransactionId;
3777 : : RelFileLocator newrlocator;
3778 : :
1326 rhaas@postgresql.org 3779 [ + + ]: 6728 : if (!IsBinaryUpgrade)
3780 : : {
3781 : : /* Allocate a new relfilenumber */
3782 : 6608 : newrelfilenumber = GetNewRelFileNumber(relation->rd_rel->reltablespace,
3783 : : NULL, persistence);
3784 : : }
3785 [ + + ]: 120 : else if (relation->rd_rel->relkind == RELKIND_INDEX)
3786 : : {
3787 [ - + ]: 60 : if (!OidIsValid(binary_upgrade_next_index_pg_class_relfilenumber))
1326 rhaas@postgresql.org 3788 [ # # ]:UBC 0 : ereport(ERROR,
3789 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3790 : : errmsg("index relfilenumber value not set when in binary upgrade mode")));
3791 : :
1326 rhaas@postgresql.org 3792 :CBC 60 : newrelfilenumber = binary_upgrade_next_index_pg_class_relfilenumber;
3793 : 60 : binary_upgrade_next_index_pg_class_relfilenumber = InvalidOid;
3794 : : }
3795 [ + - ]: 60 : else if (relation->rd_rel->relkind == RELKIND_RELATION)
3796 : : {
3797 [ - + ]: 60 : if (!OidIsValid(binary_upgrade_next_heap_pg_class_relfilenumber))
1326 rhaas@postgresql.org 3798 [ # # ]:UBC 0 : ereport(ERROR,
3799 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3800 : : errmsg("heap relfilenumber value not set when in binary upgrade mode")));
3801 : :
1326 rhaas@postgresql.org 3802 :CBC 60 : newrelfilenumber = binary_upgrade_next_heap_pg_class_relfilenumber;
3803 : 60 : binary_upgrade_next_heap_pg_class_relfilenumber = InvalidOid;
3804 : : }
3805 : : else
1326 rhaas@postgresql.org 3806 [ # # ]:UBC 0 : ereport(ERROR,
3807 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3808 : : errmsg("unexpected request for new relfilenumber in binary upgrade mode")));
3809 : :
3810 : : /*
3811 : : * Get a writable copy of the pg_class tuple for the given relation.
3812 : : */
2610 andres@anarazel.de 3813 :CBC 6728 : pg_class = table_open(RelationRelationId, RowExclusiveLock);
3814 : :
537 noah@leadboat.com 3815 : 6728 : tuple = SearchSysCacheLockedCopy1(RELOID,
3816 : : ObjectIdGetDatum(RelationGetRelid(relation)));
5884 tgl@sss.pgh.pa.us 3817 [ - + ]: 6728 : if (!HeapTupleIsValid(tuple))
5884 tgl@sss.pgh.pa.us 3818 [ # # ]:UBC 0 : elog(ERROR, "could not find tuple for relation %u",
3819 : : RelationGetRelid(relation));
537 noah@leadboat.com 3820 :CBC 6728 : otid = tuple->t_self;
5884 tgl@sss.pgh.pa.us 3821 : 6728 : classform = (Form_pg_class) GETSTRUCT(tuple);
3822 : :
3823 : : /*
3824 : : * Schedule unlinking of the old storage at transaction commit, except
3825 : : * when performing a binary upgrade, when we must do it immediately.
3826 : : */
1326 rhaas@postgresql.org 3827 [ + + ]: 6728 : if (IsBinaryUpgrade)
3828 : : {
3829 : : SMgrRelation srel;
3830 : :
3831 : : /*
3832 : : * During a binary upgrade, we use this code path to ensure that
3833 : : * pg_largeobject and its index have the same relfilenumbers as in the
3834 : : * old cluster. This is necessary because pg_upgrade treats
3835 : : * pg_largeobject like a user table, not a system table. It is however
3836 : : * possible that a table or index may need to end up with the same
3837 : : * relfilenumber in the new cluster as what it had in the old cluster.
3838 : : * Hence, we can't wait until commit time to remove the old storage.
3839 : : *
3840 : : * In general, this function needs to have transactional semantics,
3841 : : * and removing the old storage before commit time surely isn't.
3842 : : * However, it doesn't really matter, because if a binary upgrade
3843 : : * fails at this stage, the new cluster will need to be recreated
3844 : : * anyway.
3845 : : */
3846 : 120 : srel = smgropen(relation->rd_locator, relation->rd_backend);
3847 : 120 : smgrdounlinkall(&srel, 1, false);
3848 : 120 : smgrclose(srel);
3849 : : }
3850 : : else
3851 : : {
3852 : : /* Not a binary upgrade, so just schedule it to happen later. */
3853 : 6608 : RelationDropStorage(relation);
3854 : : }
3855 : :
3856 : : /*
3857 : : * Create storage for the main fork of the new relfilenumber. If it's a
3858 : : * table-like object, call into the table AM to do so, which'll also
3859 : : * create the table's init fork if needed.
3860 : : *
3861 : : * NOTE: If relevant for the AM, any conflict in relfilenumber value will
3862 : : * be caught here, if GetNewRelFileNumber messes up for any reason.
3863 : : */
1348 3864 : 6728 : newrlocator = relation->rd_locator;
3865 : 6728 : newrlocator.relNumber = newrelfilenumber;
3866 : :
1563 peter@eisentraut.org 3867 [ + + + + : 6728 : if (RELKIND_HAS_TABLE_AM(relation->rd_rel->relkind))
- + ]
3868 : : {
1348 rhaas@postgresql.org 3869 : 2434 : table_relation_set_new_filelocator(relation, &newrlocator,
3870 : : persistence,
3871 : : &freezeXid, &minmulti);
3872 : : }
1563 peter@eisentraut.org 3873 [ + - + + : 4294 : else if (RELKIND_HAS_STORAGE(relation->rd_rel->relkind))
- + - - -
- ]
3874 : 4294 : {
3875 : : /* handle these directly, at least for now */
3876 : : SMgrRelation srel;
3877 : :
1348 rhaas@postgresql.org 3878 : 4294 : srel = RelationCreateStorage(newrlocator, persistence, true);
1563 peter@eisentraut.org 3879 : 4294 : smgrclose(srel);
3880 : : }
3881 : : else
3882 : : {
3883 : : /* we shouldn't be called for anything else */
1563 peter@eisentraut.org 3884 [ # # ]:UBC 0 : elog(ERROR, "relation \"%s\" does not have storage",
3885 : : RelationGetRelationName(relation));
3886 : : }
3887 : :
3888 : : /*
3889 : : * If we're dealing with a mapped index, pg_class.relfilenode doesn't
3890 : : * change; instead we have to send the update to the relation mapper.
3891 : : *
3892 : : * For mapped indexes, we don't actually change the pg_class entry at all;
3893 : : * this is essential when reindexing pg_class itself. That leaves us with
3894 : : * possibly-inaccurate values of relpages etc, but those will be fixed up
3895 : : * later.
3896 : : */
2512 andres@anarazel.de 3897 [ + + + + :CBC 6728 : if (RelationIsMapped(relation))
+ + - + -
- + + ]
3898 : : {
3899 : : /* This case is only supported for indexes */
2509 tgl@sss.pgh.pa.us 3900 [ - + ]: 493 : Assert(relation->rd_rel->relkind == RELKIND_INDEX);
3901 : :
3902 : : /* Since we're not updating pg_class, these had better not change */
3903 [ - + ]: 493 : Assert(classform->relfrozenxid == freezeXid);
3904 [ - + ]: 493 : Assert(classform->relminmxid == minmulti);
3905 [ - + ]: 493 : Assert(classform->relpersistence == persistence);
3906 : :
3907 : : /*
3908 : : * In some code paths it's possible that the tuple update we'd
3909 : : * otherwise do here is the only thing that would assign an XID for
3910 : : * the current transaction. However, we must have an XID to delete
3911 : : * files, so make sure one is assigned.
3912 : : */
3913 : 493 : (void) GetCurrentTransactionId();
3914 : :
3915 : : /* Do the deed */
2512 andres@anarazel.de 3916 : 493 : RelationMapUpdateMap(RelationGetRelid(relation),
3917 : : newrelfilenumber,
3918 : 493 : relation->rd_rel->relisshared,
3919 : : false);
3920 : :
3921 : : /* Since we're not updating pg_class, must trigger inval manually */
2509 tgl@sss.pgh.pa.us 3922 : 493 : CacheInvalidateRelcache(relation);
3923 : : }
3924 : : else
3925 : : {
3926 : : /* Normal case, update the pg_class entry */
1348 rhaas@postgresql.org 3927 : 6235 : classform->relfilenode = newrelfilenumber;
3928 : :
3929 : : /* relpages etc. never change for sequences */
2509 tgl@sss.pgh.pa.us 3930 [ + + ]: 6235 : if (relation->rd_rel->relkind != RELKIND_SEQUENCE)
3931 : : {
3932 : 6092 : classform->relpages = 0; /* it's empty until further notice */
2023 3933 : 6092 : classform->reltuples = -1;
2509 3934 : 6092 : classform->relallvisible = 0;
377 melanieplageman@gmai 3935 : 6092 : classform->relallfrozen = 0;
3936 : : }
2509 tgl@sss.pgh.pa.us 3937 : 6235 : classform->relfrozenxid = freezeXid;
3938 : 6235 : classform->relminmxid = minmulti;
3939 : 6235 : classform->relpersistence = persistence;
3940 : :
537 noah@leadboat.com 3941 : 6235 : CatalogTupleUpdate(pg_class, &otid, tuple);
3942 : : }
3943 : :
3944 : 6728 : UnlockTuple(pg_class, &otid, InplaceUpdateTupleLock);
5884 tgl@sss.pgh.pa.us 3945 : 6728 : heap_freetuple(tuple);
3946 : :
2610 andres@anarazel.de 3947 : 6728 : table_close(pg_class, RowExclusiveLock);
3948 : :
3949 : : /*
3950 : : * Make the pg_class row change or relation map change visible. This will
3951 : : * cause the relcache entry to get updated, too.
3952 : : */
5884 tgl@sss.pgh.pa.us 3953 : 6728 : CommandCounterIncrement();
3954 : :
1348 rhaas@postgresql.org 3955 : 6728 : RelationAssumeNewRelfilelocator(relation);
2171 noah@leadboat.com 3956 : 6728 : }
3957 : :
3958 : : /*
3959 : : * RelationAssumeNewRelfilelocator
3960 : : *
3961 : : * Code that modifies pg_class.reltablespace or pg_class.relfilenode must call
3962 : : * this. The call shall precede any code that might insert WAL records whose
3963 : : * replay would modify bytes in the new RelFileLocator, and the call shall follow
3964 : : * any WAL modifying bytes in the prior RelFileLocator. See struct RelationData.
3965 : : * Ideally, call this as near as possible to the CommandCounterIncrement()
3966 : : * that makes the pg_class change visible (before it or after it); that
3967 : : * minimizes the chance of future development adding a forbidden WAL insertion
3968 : : * between RelationAssumeNewRelfilelocator() and CommandCounterIncrement().
3969 : : */
3970 : : void
1348 rhaas@postgresql.org 3971 : 8137 : RelationAssumeNewRelfilelocator(Relation relation)
3972 : : {
3973 : 8137 : relation->rd_newRelfilelocatorSubid = GetCurrentSubTransactionId();
3974 [ + + ]: 8137 : if (relation->rd_firstRelfilelocatorSubid == InvalidSubTransactionId)
3975 : 8078 : relation->rd_firstRelfilelocatorSubid = relation->rd_newRelfilelocatorSubid;
3976 : :
3977 : : /* Flag relation as needing eoxact cleanup (to clear these fields) */
4802 tgl@sss.pgh.pa.us 3978 [ + + ]: 8137 : EOXactListAdd(relation);
5884 3979 : 8137 : }
3980 : :
3981 : :
3982 : : /*
3983 : : * RelationCacheInitialize
3984 : : *
3985 : : * This initializes the relation descriptor cache. At the time
3986 : : * that this is invoked, we can't do database access yet (mainly
3987 : : * because the transaction subsystem is not up); all we are doing
3988 : : * is making an empty cache hashtable. This must be done before
3989 : : * starting the initialization transaction, because otherwise
3990 : : * AtEOXact_RelationCache would crash if that transaction aborts
3991 : : * before we can get the relcache set up.
3992 : : */
3993 : :
3994 : : #define INITRELCACHESIZE 400
3995 : :
3996 : : void
9352 3997 : 17036 : RelationCacheInitialize(void)
3998 : : {
3999 : : HASHCTL ctl;
4000 : : int allocsize;
4001 : :
4002 : : /*
4003 : : * make sure cache memory context exists
4004 : : */
9391 4005 [ + - ]: 17036 : if (!CacheMemoryContext)
4006 : 17036 : CreateCacheMemoryContext();
4007 : :
4008 : : /*
4009 : : * create hashtable that indexes the relcache
4010 : : */
10416 bruce@momjian.us 4011 : 17036 : ctl.keysize = sizeof(Oid);
8931 tgl@sss.pgh.pa.us 4012 : 17036 : ctl.entrysize = sizeof(RelIdCacheEnt);
8927 4013 : 17036 : RelationIdCache = hash_create("Relcache by OID", INITRELCACHESIZE,
4014 : : &ctl, HASH_ELEM | HASH_BLOBS);
4015 : :
4016 : : /*
4017 : : * reserve enough in_progress_list slots for many cases
4018 : : */
1604 noah@leadboat.com 4019 : 17036 : allocsize = 4;
4020 : 17036 : in_progress_list =
4021 : 17036 : MemoryContextAlloc(CacheMemoryContext,
4022 : : allocsize * sizeof(*in_progress_list));
4023 : 17036 : in_progress_list_maxlen = allocsize;
4024 : :
4025 : : /*
4026 : : * relation mapper needs to be initialized too
4027 : : */
5880 tgl@sss.pgh.pa.us 4028 : 17036 : RelationMapInitialize();
7255 4029 : 17036 : }
4030 : :
4031 : : /*
4032 : : * RelationCacheInitializePhase2
4033 : : *
4034 : : * This is called to prepare for access to shared catalogs during startup.
4035 : : * We must at least set up nailed reldescs for pg_database, pg_authid,
4036 : : * pg_auth_members, and pg_shseclabel. Ideally we'd like to have reldescs
4037 : : * for their indexes, too. We attempt to load this information from the
4038 : : * shared relcache init file. If that's missing or broken, just make
4039 : : * phony entries for the catalogs themselves.
4040 : : * RelationCacheInitializePhase3 will clean up as needed.
4041 : : */
4042 : : void
4043 : 17037 : RelationCacheInitializePhase2(void)
4044 : : {
4045 : : MemoryContext oldcxt;
4046 : :
4047 : : /*
4048 : : * relation mapper needs initialized too
4049 : : */
5880 4050 : 17037 : RelationMapInitializePhase2();
4051 : :
4052 : : /*
4053 : : * In bootstrap mode, the shared catalogs aren't there yet anyway, so do
4054 : : * nothing.
4055 : : */
6059 4056 [ + + ]: 17037 : if (IsBootstrapProcessingMode())
4057 : 51 : return;
4058 : :
4059 : : /*
4060 : : * switch to cache memory context
4061 : : */
4062 : 16986 : oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
4063 : :
4064 : : /*
4065 : : * Try to load the shared relcache cache file. If unsuccessful, bootstrap
4066 : : * the cache with pre-made descriptors for the critical shared catalogs.
4067 : : */
4068 [ + + ]: 16986 : if (!load_relcache_init_file(true))
4069 : : {
6014 4070 : 2296 : formrdesc("pg_database", DatabaseRelation_Rowtype_Id, true,
4071 : : Natts_pg_database, Desc_pg_database);
5808 4072 : 2296 : formrdesc("pg_authid", AuthIdRelation_Rowtype_Id, true,
4073 : : Natts_pg_authid, Desc_pg_authid);
4074 : 2296 : formrdesc("pg_auth_members", AuthMemRelation_Rowtype_Id, true,
4075 : : Natts_pg_auth_members, Desc_pg_auth_members);
3722 alvherre@alvh.no-ip. 4076 : 2296 : formrdesc("pg_shseclabel", SharedSecLabelRelation_Rowtype_Id, true,
4077 : : Natts_pg_shseclabel, Desc_pg_shseclabel);
3342 peter_e@gmx.net 4078 : 2296 : formrdesc("pg_subscription", SubscriptionRelation_Rowtype_Id, true,
4079 : : Natts_pg_subscription, Desc_pg_subscription);
4080 : :
4081 : : #define NUM_CRITICAL_SHARED_RELS 5 /* fix if you change list above */
4082 : : }
4083 : :
6059 tgl@sss.pgh.pa.us 4084 : 16986 : MemoryContextSwitchTo(oldcxt);
4085 : : }
4086 : :
4087 : : /*
4088 : : * RelationCacheInitializePhase3
4089 : : *
4090 : : * This is called as soon as the catcache and transaction system
4091 : : * are functional and we have determined MyDatabaseId. At this point
4092 : : * we can actually read data from the database's system catalogs.
4093 : : * We first try to read pre-computed relcache entries from the local
4094 : : * relcache init file. If that's missing or broken, make phony entries
4095 : : * for the minimum set of nailed-in-cache relations. Then (unless
4096 : : * bootstrapping) make sure we have entries for the critical system
4097 : : * indexes. Once we've done all this, we have enough infrastructure to
4098 : : * open any system catalog or use any catcache. The last step is to
4099 : : * rewrite the cache files if needed.
4100 : : */
4101 : : void
4102 : 15454 : RelationCacheInitializePhase3(void)
4103 : : {
4104 : : HASH_SEQ_STATUS status;
4105 : : RelIdCacheEnt *idhentry;
4106 : : MemoryContext oldcxt;
4107 : 15454 : bool needNewCacheFile = !criticalSharedRelcachesBuilt;
4108 : :
4109 : : /*
4110 : : * relation mapper needs initialized too
4111 : : */
5880 4112 : 15454 : RelationMapInitializePhase3();
4113 : :
4114 : : /*
4115 : : * switch to cache memory context
4116 : : */
7255 4117 : 15454 : oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
4118 : :
4119 : : /*
4120 : : * Try to load the local relcache cache file. If unsuccessful, bootstrap
4121 : : * the cache with pre-made descriptors for the critical "nailed-in" system
4122 : : * catalogs.
4123 : : */
8790 4124 [ + + ]: 15454 : if (IsBootstrapProcessingMode() ||
6059 4125 [ + + ]: 15403 : !load_relcache_init_file(false))
4126 : : {
7253 4127 : 1656 : needNewCacheFile = true;
4128 : :
6014 4129 : 1656 : formrdesc("pg_class", RelationRelation_Rowtype_Id, false,
4130 : : Natts_pg_class, Desc_pg_class);
4131 : 1656 : formrdesc("pg_attribute", AttributeRelation_Rowtype_Id, false,
4132 : : Natts_pg_attribute, Desc_pg_attribute);
4133 : 1656 : formrdesc("pg_proc", ProcedureRelation_Rowtype_Id, false,
4134 : : Natts_pg_proc, Desc_pg_proc);
4135 : 1656 : formrdesc("pg_type", TypeRelation_Rowtype_Id, false,
4136 : : Natts_pg_type, Desc_pg_type);
4137 : :
4138 : : #define NUM_CRITICAL_LOCAL_RELS 4 /* fix if you change list above */
4139 : : }
4140 : :
10416 bruce@momjian.us 4141 : 15454 : MemoryContextSwitchTo(oldcxt);
4142 : :
4143 : : /* In bootstrap mode, the faked-up formrdesc info is all we'll have */
8790 tgl@sss.pgh.pa.us 4144 [ + + ]: 15454 : if (IsBootstrapProcessingMode())
4145 : 51 : return;
4146 : :
4147 : : /*
4148 : : * If we didn't get the critical system indexes loaded into relcache, do
4149 : : * so now. These are critical because the catcache and/or opclass cache
4150 : : * depend on them for fetches done during relcache load. Thus, we have an
4151 : : * infinite-recursion problem. We can break the recursion by doing
4152 : : * heapscans instead of indexscans at certain key spots. To avoid hobbling
4153 : : * performance, we only want to do that until we have the critical indexes
4154 : : * loaded into relcache. Thus, the flag criticalRelcachesBuilt is used to
4155 : : * decide whether to do heapscan or indexscan at the key spots, and we set
4156 : : * it true after we've loaded the critical indexes.
4157 : : *
4158 : : * The critical indexes are marked as "nailed in cache", partly to make it
4159 : : * easy for load_relcache_init_file to count them, but mainly because we
4160 : : * cannot flush and rebuild them once we've set criticalRelcachesBuilt to
4161 : : * true. (NOTE: perhaps it would be possible to reload them by
4162 : : * temporarily setting criticalRelcachesBuilt to false again. For now,
4163 : : * though, we just nail 'em in.)
4164 : : *
4165 : : * RewriteRelRulenameIndexId and TriggerRelidNameIndexId are not critical
4166 : : * in the same way as the others, because the critical catalogs don't
4167 : : * (currently) have any rules or triggers, and so these indexes can be
4168 : : * rebuilt without inducing recursion. However they are used during
4169 : : * relcache load when a rel does have rules or triggers, so we choose to
4170 : : * nail them for performance reasons.
4171 : : */
8593 bruce@momjian.us 4172 [ + + ]: 15403 : if (!criticalRelcachesBuilt)
4173 : : {
5905 tgl@sss.pgh.pa.us 4174 : 1605 : load_critical_index(ClassOidIndexId,
4175 : : RelationRelationId);
4176 : 1603 : load_critical_index(AttributeRelidNumIndexId,
4177 : : AttributeRelationId);
4178 : 1603 : load_critical_index(IndexRelidIndexId,
4179 : : IndexRelationId);
4180 : 1603 : load_critical_index(OpclassOidIndexId,
4181 : : OperatorClassRelationId);
4182 : 1602 : load_critical_index(AccessMethodProcedureIndexId,
4183 : : AccessMethodProcedureRelationId);
4184 : 1602 : load_critical_index(RewriteRelRulenameIndexId,
4185 : : RewriteRelationId);
4186 : 1602 : load_critical_index(TriggerRelidNameIndexId,
4187 : : TriggerRelationId);
4188 : :
4189 : : #define NUM_CRITICAL_LOCAL_INDEXES 7 /* fix if you change list above */
4190 : :
8790 4191 : 1602 : criticalRelcachesBuilt = true;
4192 : : }
4193 : :
4194 : : /*
4195 : : * Process critical shared indexes too.
4196 : : *
4197 : : * DatabaseNameIndexId isn't critical for relcache loading, but rather for
4198 : : * initial lookup of MyDatabaseId, without which we'll never find any
4199 : : * non-shared catalogs at all. Autovacuum calls InitPostgres with a
4200 : : * database OID, so it instead depends on DatabaseOidIndexId. We also
4201 : : * need to nail up some indexes on pg_authid and pg_auth_members for use
4202 : : * during client authentication. SharedSecLabelObjectIndexId isn't
4203 : : * critical for the core system, but authentication hooks might be
4204 : : * interested in it.
4205 : : */
6059 4206 [ + + ]: 15400 : if (!criticalSharedRelcachesBuilt)
4207 : : {
5905 4208 : 1252 : load_critical_index(DatabaseNameIndexId,
4209 : : DatabaseRelationId);
4210 : 1252 : load_critical_index(DatabaseOidIndexId,
4211 : : DatabaseRelationId);
5808 4212 : 1252 : load_critical_index(AuthIdRolnameIndexId,
4213 : : AuthIdRelationId);
4214 : 1252 : load_critical_index(AuthIdOidIndexId,
4215 : : AuthIdRelationId);
4216 : 1252 : load_critical_index(AuthMemMemRoleIndexId,
4217 : : AuthMemRelationId);
3722 alvherre@alvh.no-ip. 4218 : 1252 : load_critical_index(SharedSecLabelObjectIndexId,
4219 : : SharedSecLabelRelationId);
4220 : :
4221 : : #define NUM_CRITICAL_SHARED_INDEXES 6 /* fix if you change list above */
4222 : :
6059 tgl@sss.pgh.pa.us 4223 : 1252 : criticalSharedRelcachesBuilt = true;
4224 : : }
4225 : :
4226 : : /*
4227 : : * Now, scan all the relcache entries and update anything that might be
4228 : : * wrong in the results from formrdesc or the relcache cache file. If we
4229 : : * faked up relcache entries using formrdesc, then read the real pg_class
4230 : : * rows and replace the fake entries with them. Also, if any of the
4231 : : * relcache entries have rules, triggers, or security policies, load that
4232 : : * info the hard way since it isn't recorded in the cache file.
4233 : : *
4234 : : * Whenever we access the catalogs to read data, there is a possibility of
4235 : : * a shared-inval cache flush causing relcache entries to be removed.
4236 : : * Since hash_seq_search only guarantees to still work after the *current*
4237 : : * entry is removed, it's unsafe to continue the hashtable scan afterward.
4238 : : * We handle this by restarting the scan from scratch after each access.
4239 : : * This is theoretically O(N^2), but the number of entries that actually
4240 : : * need to be fixed is small enough that it doesn't matter.
4241 : : */
8755 4242 : 15400 : hash_seq_init(&status, RelationIdCache);
4243 : :
4244 [ + + ]: 2198414 : while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
4245 : : {
4246 : 2167614 : Relation relation = idhentry->reldesc;
6014 4247 : 2167614 : bool restart = false;
4248 : :
4249 : : /*
4250 : : * Make sure *this* entry doesn't get flushed while we work with it.
4251 : : */
4252 : 2167614 : RelationIncrementReferenceCount(relation);
4253 : :
4254 : : /*
4255 : : * If it's a faked-up entry, read the real pg_class tuple.
4256 : : */
4257 [ + + ]: 2167614 : if (relation->rd_rel->relowner == InvalidOid)
4258 : : {
4259 : : HeapTuple htup;
4260 : : Form_pg_class relp;
4261 : :
5873 rhaas@postgresql.org 4262 : 12668 : htup = SearchSysCache1(RELOID,
4263 : : ObjectIdGetDatum(RelationGetRelid(relation)));
8790 tgl@sss.pgh.pa.us 4264 [ - + ]: 12668 : if (!HeapTupleIsValid(htup))
711 dgustafsson@postgres 4265 [ # # ]:UBC 0 : ereport(FATAL,
4266 : : errcode(ERRCODE_UNDEFINED_OBJECT),
4267 : : errmsg_internal("cache lookup failed for relation %u",
4268 : : RelationGetRelid(relation)));
8790 tgl@sss.pgh.pa.us 4269 :CBC 12668 : relp = (Form_pg_class) GETSTRUCT(htup);
4270 : :
4271 : : /*
4272 : : * Copy tuple to relation->rd_rel. (See notes in
4273 : : * AllocateRelationDesc())
4274 : : */
4275 : 12668 : memcpy((char *) relation->rd_rel, (char *) relp, CLASS_TUPLE_SIZE);
4276 : :
4277 : : /* Update rd_options while we have the tuple */
7195 4278 [ - + ]: 12668 : if (relation->rd_options)
7195 tgl@sss.pgh.pa.us 4279 :UBC 0 : pfree(relation->rd_options);
7195 tgl@sss.pgh.pa.us 4280 :CBC 12668 : RelationParseRelOptions(relation, htup);
4281 : :
4282 : : /*
4283 : : * Check the values in rd_att were set up correctly. (We cannot
4284 : : * just copy them over now: formrdesc must have set up the rd_att
4285 : : * data correctly to start with, because it may already have been
4286 : : * copied into one or more catcache entries.)
4287 : : */
6014 4288 [ - + ]: 12668 : Assert(relation->rd_att->tdtypeid == relp->reltype);
4289 [ - + ]: 12668 : Assert(relation->rd_att->tdtypmod == -1);
4290 : :
8790 4291 : 12668 : ReleaseSysCache(htup);
4292 : :
4293 : : /* relowner had better be OK now, else we'll loop forever */
6014 4294 [ - + ]: 12668 : if (relation->rd_rel->relowner == InvalidOid)
6014 tgl@sss.pgh.pa.us 4295 [ # # ]:UBC 0 : elog(ERROR, "invalid relowner in pg_class entry for \"%s\"",
4296 : : RelationGetRelationName(relation));
4297 : :
6014 tgl@sss.pgh.pa.us 4298 :CBC 12668 : restart = true;
4299 : : }
4300 : :
4301 : : /*
4302 : : * Fix data that isn't saved in relcache cache file.
4303 : : *
4304 : : * relhasrules or relhastriggers could possibly be wrong or out of
4305 : : * date. If we don't actually find any rules or triggers, clear the
4306 : : * local copy of the flag so that we don't get into an infinite loop
4307 : : * here. We don't make any attempt to fix the pg_class entry, though.
4308 : : */
8790 4309 [ - + - - ]: 2167614 : if (relation->rd_rel->relhasrules && relation->rd_rules == NULL)
4310 : : {
8790 tgl@sss.pgh.pa.us 4311 :UBC 0 : RelationBuildRuleLock(relation);
6014 4312 [ # # ]: 0 : if (relation->rd_rules == NULL)
4313 : 0 : relation->rd_rel->relhasrules = false;
4314 : 0 : restart = true;
4315 : : }
6335 tgl@sss.pgh.pa.us 4316 [ - + - - ]:CBC 2167614 : if (relation->rd_rel->relhastriggers && relation->trigdesc == NULL)
4317 : : {
8790 tgl@sss.pgh.pa.us 4318 :UBC 0 : RelationBuildTriggers(relation);
6014 4319 [ # # ]: 0 : if (relation->trigdesc == NULL)
4320 : 0 : relation->rd_rel->relhastriggers = false;
4321 : 0 : restart = true;
4322 : : }
4323 : :
4324 : : /*
4325 : : * Re-load the row security policies if the relation has them, since
4326 : : * they are not preserved in the cache. Note that we can never NOT
4327 : : * have a policy while relrowsecurity is true,
4328 : : * RelationBuildRowSecurity will create a single default-deny policy
4329 : : * if there is no policy defined in pg_policy.
4330 : : */
4139 sfrost@snowman.net 4331 [ - + - - ]:CBC 2167614 : if (relation->rd_rel->relrowsecurity && relation->rd_rsdesc == NULL)
4332 : : {
4195 sfrost@snowman.net 4333 :UBC 0 : RelationBuildRowSecurity(relation);
4334 : :
3949 bruce@momjian.us 4335 [ # # ]: 0 : Assert(relation->rd_rsdesc != NULL);
4195 sfrost@snowman.net 4336 : 0 : restart = true;
4337 : : }
4338 : :
4339 : : /* Reload tableam data if needed */
2566 andres@anarazel.de 4340 [ + + ]:CBC 2167614 : if (relation->rd_tableam == NULL &&
1563 peter@eisentraut.org 4341 [ + - + - : 1326144 : (RELKIND_HAS_TABLE_AM(relation->rd_rel->relkind) || relation->rd_rel->relkind == RELKIND_SEQUENCE))
+ - - + ]
4342 : : {
2566 andres@anarazel.de 4343 :UBC 0 : RelationInitTableAccessMethod(relation);
4344 [ # # ]: 0 : Assert(relation->rd_tableam != NULL);
4345 : :
4346 : 0 : restart = true;
4347 : : }
4348 : :
4349 : : /* Release hold on the relation */
6014 tgl@sss.pgh.pa.us 4350 :CBC 2167614 : RelationDecrementReferenceCount(relation);
4351 : :
4352 : : /* Now, restart the hashtable scan if needed */
4353 [ + + ]: 2167614 : if (restart)
4354 : : {
4355 : 12668 : hash_seq_term(&status);
4356 : 12668 : hash_seq_init(&status, RelationIdCache);
4357 : : }
4358 : : }
4359 : :
4360 : : /*
4361 : : * Lastly, write out new relcache cache files if needed. We don't bother
4362 : : * to distinguish cases where only one of the two needs an update.
4363 : : */
8790 4364 [ + + ]: 15400 : if (needNewCacheFile)
4365 : : {
4366 : : /*
4367 : : * Force all the catcaches to finish initializing and thereby open the
4368 : : * catalogs and indexes they use. This will preload the relcache with
4369 : : * entries for all the most important system catalogs and indexes, so
4370 : : * that the init files will be most useful for future backends.
4371 : : */
4372 : 1682 : InitCatalogCachePhase2();
4373 : :
4374 : : /* now write the files */
6059 4375 : 1682 : write_relcache_init_file(true);
4376 : 1682 : write_relcache_init_file(false);
4377 : : }
4378 : : }
4379 : :
4380 : : /*
4381 : : * Load one critical system index into the relcache
4382 : : *
4383 : : * indexoid is the OID of the target index, heapoid is the OID of the catalog
4384 : : * it belongs to.
4385 : : */
4386 : : static void
5905 4387 : 18732 : load_critical_index(Oid indexoid, Oid heapoid)
4388 : : {
4389 : : Relation ird;
4390 : :
4391 : : /*
4392 : : * We must lock the underlying catalog before locking the index to avoid
4393 : : * deadlock, since RelationBuildDesc might well need to read the catalog,
4394 : : * and if anyone else is exclusive-locking this catalog and index they'll
4395 : : * be doing it in that order.
4396 : : */
4397 : 18732 : LockRelationOid(heapoid, AccessShareLock);
6059 4398 : 18732 : LockRelationOid(indexoid, AccessShareLock);
5906 4399 : 18732 : ird = RelationBuildDesc(indexoid, true);
6059 4400 [ - + ]: 18729 : if (ird == NULL)
711 dgustafsson@postgres 4401 [ # # ]:UBC 0 : ereport(PANIC,
4402 : : errcode(ERRCODE_DATA_CORRUPTED),
4403 : : errmsg_internal("could not open critical system index %u", indexoid));
6059 tgl@sss.pgh.pa.us 4404 :CBC 18729 : ird->rd_isnailed = true;
4405 : 18729 : ird->rd_refcnt = 1;
4406 : 18729 : UnlockRelationOid(indexoid, AccessShareLock);
5905 4407 : 18729 : UnlockRelationOid(heapoid, AccessShareLock);
4408 : :
2176 akorotkov@postgresql 4409 : 18729 : (void) RelationGetIndexAttOptions(ird, false);
6059 tgl@sss.pgh.pa.us 4410 : 18729 : }
4411 : :
4412 : : /*
4413 : : * GetPgClassDescriptor -- get a predefined tuple descriptor for pg_class
4414 : : * GetPgIndexDescriptor -- get a predefined tuple descriptor for pg_index
4415 : : *
4416 : : * We need this kluge because we have to be able to access non-fixed-width
4417 : : * fields of pg_class and pg_index before we have the standard catalog caches
4418 : : * available. We use predefined data that's set up in just the same way as
4419 : : * the bootstrapped reldescs used by formrdesc(). The resulting tupdesc is
4420 : : * not 100% kosher: it does not have the correct rowtype OID in tdtypeid, nor
4421 : : * does it have a TupleConstr field. But it's good enough for the purpose of
4422 : : * extracting fields.
4423 : : */
4424 : : static TupleDesc
2672 andres@anarazel.de 4425 : 30904 : BuildHardcodedDescriptor(int natts, const FormData_pg_attribute *attrs)
4426 : : {
4427 : : TupleDesc result;
4428 : : MemoryContext oldcxt;
4429 : : int i;
4430 : :
7656 tgl@sss.pgh.pa.us 4431 : 30904 : oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
4432 : :
2672 andres@anarazel.de 4433 : 30904 : result = CreateTemplateTupleDesc(natts);
3189 tgl@sss.pgh.pa.us 4434 : 30904 : result->tdtypeid = RECORDOID; /* not right, but we don't care */
7195 4435 : 30904 : result->tdtypmod = -1;
4436 : :
4437 [ + + ]: 880764 : for (i = 0; i < natts; i++)
4438 : : {
3129 andres@anarazel.de 4439 : 849860 : memcpy(TupleDescAttr(result, i), &attrs[i], ATTRIBUTE_FIXED_PART_SIZE);
4440 : :
450 drowley@postgresql.o 4441 : 849860 : populate_compact_attribute(result, i);
4442 : : }
4443 : :
4444 : : /* initialize first attribute's attcacheoff, cf RelationBuildTupleDesc */
4445 : 30904 : TupleDescCompactAttr(result, 0)->attcacheoff = 0;
4446 : :
4447 : : /* Note: we don't bother to set up a TupleConstr entry */
4448 : :
7656 tgl@sss.pgh.pa.us 4449 : 30904 : MemoryContextSwitchTo(oldcxt);
4450 : :
7195 4451 : 30904 : return result;
4452 : : }
4453 : :
4454 : : static TupleDesc
4455 : 797618 : GetPgClassDescriptor(void)
4456 : : {
4457 : : static TupleDesc pgclassdesc = NULL;
4458 : :
4459 : : /* Already done? */
4460 [ + + ]: 797618 : if (pgclassdesc == NULL)
4461 : 15452 : pgclassdesc = BuildHardcodedDescriptor(Natts_pg_class,
4462 : : Desc_pg_class);
4463 : :
4464 : 797618 : return pgclassdesc;
4465 : : }
4466 : :
4467 : : static TupleDesc
4468 : 916332 : GetPgIndexDescriptor(void)
4469 : : {
4470 : : static TupleDesc pgindexdesc = NULL;
4471 : :
4472 : : /* Already done? */
4473 [ + + ]: 916332 : if (pgindexdesc == NULL)
4474 : 15452 : pgindexdesc = BuildHardcodedDescriptor(Natts_pg_index,
4475 : : Desc_pg_index);
4476 : :
7656 4477 : 916332 : return pgindexdesc;
4478 : : }
4479 : :
4480 : : /*
4481 : : * Load any default attribute value definitions for the relation.
4482 : : *
4483 : : * ndef is the number of attributes that were marked atthasdef.
4484 : : *
4485 : : * Note: we don't make it a hard error to be missing some pg_attrdef records.
4486 : : * We can limp along as long as nothing needs to use the default value. Code
4487 : : * that fails to find an expected AttrDefault record should throw an error.
4488 : : */
4489 : : static void
1804 4490 : 19444 : AttrDefaultFetch(Relation relation, int ndef)
4491 : : {
4492 : : AttrDefault *attrdef;
4493 : : Relation adrel;
4494 : : SysScanDesc adscan;
4495 : : ScanKeyData skey;
4496 : : HeapTuple htup;
4497 : 19444 : int found = 0;
4498 : :
4499 : : /* Allocate array with room for as many entries as expected */
4500 : : attrdef = (AttrDefault *)
4501 : 19444 : MemoryContextAllocZero(CacheMemoryContext,
4502 : : ndef * sizeof(AttrDefault));
4503 : :
4504 : : /* Search pg_attrdef for relevant entries */
8159 4505 : 19444 : ScanKeyInit(&skey,
4506 : : Anum_pg_attrdef_adrelid,
4507 : : BTEqualStrategyNumber, F_OIDEQ,
4508 : : ObjectIdGetDatum(RelationGetRelid(relation)));
4509 : :
2610 andres@anarazel.de 4510 : 19444 : adrel = table_open(AttrDefaultRelationId, AccessShareLock);
7640 tgl@sss.pgh.pa.us 4511 : 19444 : adscan = systable_beginscan(adrel, AttrDefaultIndexId, true,
4512 : : NULL, 1, &skey);
4513 : :
8790 4514 [ + + ]: 47028 : while (HeapTupleIsValid(htup = systable_getnext(adscan)))
4515 : : {
4516 : 27584 : Form_pg_attrdef adform = (Form_pg_attrdef) GETSTRUCT(htup);
4517 : : Datum val;
4518 : : bool isnull;
4519 : :
4520 : : /* protect limited size of array */
1804 4521 [ - + ]: 27584 : if (found >= ndef)
4522 : : {
1804 tgl@sss.pgh.pa.us 4523 [ # # ]:UBC 0 : elog(WARNING, "unexpected pg_attrdef record found for attribute %d of relation \"%s\"",
4524 : : adform->adnum, RelationGetRelationName(relation));
10416 bruce@momjian.us 4525 : 0 : break;
4526 : : }
4527 : :
1804 tgl@sss.pgh.pa.us 4528 :CBC 27584 : val = fastgetattr(htup,
4529 : : Anum_pg_attrdef_adbin,
4530 : : adrel->rd_att, &isnull);
4531 [ - + ]: 27584 : if (isnull)
1804 tgl@sss.pgh.pa.us 4532 [ # # ]:UBC 0 : elog(WARNING, "null adbin for attribute %d of relation \"%s\"",
4533 : : adform->adnum, RelationGetRelationName(relation));
4534 : : else
4535 : : {
4536 : : /* detoast and convert to cstring in caller's context */
1804 tgl@sss.pgh.pa.us 4537 :CBC 27584 : char *s = TextDatumGetCString(val);
4538 : :
4539 : 27584 : attrdef[found].adnum = adform->adnum;
4540 : 27584 : attrdef[found].adbin = MemoryContextStrdup(CacheMemoryContext, s);
4541 : 27584 : pfree(s);
4542 : 27584 : found++;
4543 : : }
4544 : : }
4545 : :
8790 4546 : 19444 : systable_endscan(adscan);
2610 andres@anarazel.de 4547 : 19444 : table_close(adrel, AccessShareLock);
4548 : :
1804 tgl@sss.pgh.pa.us 4549 [ - + ]: 19444 : if (found != ndef)
1804 tgl@sss.pgh.pa.us 4550 [ # # ]:UBC 0 : elog(WARNING, "%d pg_attrdef record(s) missing for relation \"%s\"",
4551 : : ndef - found, RelationGetRelationName(relation));
4552 : :
4553 : : /*
4554 : : * Sort the AttrDefault entries by adnum, for the convenience of
4555 : : * equalTupleDescs(). (Usually, they already will be in order, but this
4556 : : * might not be so if systable_getnext isn't using an index.)
4557 : : */
1804 tgl@sss.pgh.pa.us 4558 [ + + ]:CBC 19444 : if (found > 1)
4559 : 4657 : qsort(attrdef, found, sizeof(AttrDefault), AttrDefaultCmp);
4560 : :
4561 : : /* Install array only after it's fully valid */
4562 : 19444 : relation->rd_att->constr->defval = attrdef;
4563 : 19444 : relation->rd_att->constr->num_defval = found;
4564 : 19444 : }
4565 : :
4566 : : /*
4567 : : * qsort comparator to sort AttrDefault entries by adnum
4568 : : */
4569 : : static int
4570 : 8140 : AttrDefaultCmp(const void *a, const void *b)
4571 : : {
4572 : 8140 : const AttrDefault *ada = (const AttrDefault *) a;
4573 : 8140 : const AttrDefault *adb = (const AttrDefault *) b;
4574 : :
758 nathan@postgresql.or 4575 : 8140 : return pg_cmp_s16(ada->adnum, adb->adnum);
4576 : : }
4577 : :
4578 : : /*
4579 : : * Load any check constraints for the relation, and update not-null validity
4580 : : * of invalid constraints.
4581 : : *
4582 : : * As with defaults, if we don't find the expected number of them, just warn
4583 : : * here. The executor should throw an error if an INSERT/UPDATE is attempted.
4584 : : */
4585 : : static void
342 alvherre@alvh.no-ip. 4586 : 91073 : CheckNNConstraintFetch(Relation relation)
4587 : : {
4588 : : ConstrCheck *check;
1804 tgl@sss.pgh.pa.us 4589 : 91073 : int ncheck = relation->rd_rel->relchecks;
4590 : : Relation conrel;
4591 : : SysScanDesc conscan;
4592 : : ScanKeyData skey[1];
4593 : : HeapTuple htup;
8647 4594 : 91073 : int found = 0;
4595 : :
4596 : : /* Allocate array with room for as many entries as expected, if needed */
308 alvherre@kurilemu.de 4597 [ + + ]: 91073 : if (ncheck > 0)
4598 : : check = (ConstrCheck *)
4599 : 7135 : MemoryContextAllocZero(CacheMemoryContext,
4600 : : ncheck * sizeof(ConstrCheck));
4601 : : else
4602 : 83938 : check = NULL;
4603 : :
4604 : : /* Search pg_constraint for relevant entries */
8159 tgl@sss.pgh.pa.us 4605 : 91073 : ScanKeyInit(&skey[0],
4606 : : Anum_pg_constraint_conrelid,
4607 : : BTEqualStrategyNumber, F_OIDEQ,
4608 : : ObjectIdGetDatum(RelationGetRelid(relation)));
4609 : :
2610 andres@anarazel.de 4610 : 91073 : conrel = table_open(ConstraintRelationId, AccessShareLock);
2749 tgl@sss.pgh.pa.us 4611 : 91073 : conscan = systable_beginscan(conrel, ConstraintRelidTypidNameIndexId, true,
4612 : : NULL, 1, skey);
4613 : :
8647 4614 [ + + ]: 264041 : while (HeapTupleIsValid(htup = systable_getnext(conscan)))
4615 : : {
4616 : 172968 : Form_pg_constraint conform = (Form_pg_constraint) GETSTRUCT(htup);
4617 : : Datum val;
4618 : : bool isnull;
4619 : :
4620 : : /*
4621 : : * If this is a not-null constraint, then only look at it if it's
4622 : : * invalid, and if so, mark the TupleDesc entry as known invalid.
4623 : : * Otherwise move on. We'll mark any remaining columns that are still
4624 : : * in UNKNOWN state as known valid later. This allows us not to have
4625 : : * to extract the attnum from this constraint tuple in the vast
4626 : : * majority of cases.
4627 : : */
342 alvherre@alvh.no-ip. 4628 [ + + ]: 172968 : if (conform->contype == CONSTRAINT_NOTNULL)
4629 : : {
4630 [ + + ]: 95733 : if (!conform->convalidated)
4631 : : {
4632 : : AttrNumber attnum;
4633 : :
4634 : 598 : attnum = extractNotNullColumn(htup);
4635 [ - + ]: 598 : Assert(relation->rd_att->compact_attrs[attnum - 1].attnullability ==
4636 : : ATTNULLABLE_UNKNOWN);
4637 : 598 : relation->rd_att->compact_attrs[attnum - 1].attnullability =
4638 : : ATTNULLABLE_INVALID;
4639 : : }
4640 : :
4641 : 160638 : continue;
4642 : : }
4643 : :
4644 : : /* For what follows, consider check constraints only */
8647 tgl@sss.pgh.pa.us 4645 [ + + ]: 77235 : if (conform->contype != CONSTRAINT_CHECK)
4646 : 64905 : continue;
4647 : :
4648 : : /* protect limited size of array */
8269 4649 [ - + ]: 12330 : if (found >= ncheck)
4650 : : {
1804 tgl@sss.pgh.pa.us 4651 [ # # ]:UBC 0 : elog(WARNING, "unexpected pg_constraint record found for relation \"%s\"",
4652 : : RelationGetRelationName(relation));
4653 : 0 : break;
4654 : : }
4655 : :
4656 : : /* Grab and test conbin is actually set */
9384 tgl@sss.pgh.pa.us 4657 :CBC 12330 : val = fastgetattr(htup,
4658 : : Anum_pg_constraint_conbin,
4659 : : conrel->rd_att, &isnull);
10416 bruce@momjian.us 4660 [ - + ]: 12330 : if (isnull)
1804 tgl@sss.pgh.pa.us 4661 [ # # ]:UBC 0 : elog(WARNING, "null conbin for relation \"%s\"",
4662 : : RelationGetRelationName(relation));
4663 : : else
4664 : : {
4665 : : /* detoast and convert to cstring in caller's context */
1804 tgl@sss.pgh.pa.us 4666 :CBC 12330 : char *s = TextDatumGetCString(val);
4667 : :
137 alvherre@kurilemu.de 4668 :GNC 12330 : check[found].ccenforced = conform->conenforced;
4669 : 12330 : check[found].ccvalid = conform->convalidated;
4670 : 12330 : check[found].ccnoinherit = conform->connoinherit;
4671 : 24660 : check[found].ccname = MemoryContextStrdup(CacheMemoryContext,
4672 : 12330 : NameStr(conform->conname));
1804 tgl@sss.pgh.pa.us 4673 :CBC 12330 : check[found].ccbin = MemoryContextStrdup(CacheMemoryContext, s);
4674 : :
4675 : 12330 : pfree(s);
4676 : 12330 : found++;
4677 : : }
4678 : : }
4679 : :
8647 4680 : 91073 : systable_endscan(conscan);
2610 andres@anarazel.de 4681 : 91073 : table_close(conrel, AccessShareLock);
4682 : :
8790 tgl@sss.pgh.pa.us 4683 [ - + ]: 91073 : if (found != ncheck)
1804 tgl@sss.pgh.pa.us 4684 [ # # ]:UBC 0 : elog(WARNING, "%d pg_constraint record(s) missing for relation \"%s\"",
4685 : : ncheck - found, RelationGetRelationName(relation));
4686 : :
4687 : : /*
4688 : : * Sort the records by name. This ensures that CHECKs are applied in a
4689 : : * deterministic order, and it also makes equalTupleDescs() faster.
4690 : : */
1804 tgl@sss.pgh.pa.us 4691 [ + + ]:CBC 91073 : if (found > 1)
4692 : 2505 : qsort(check, found, sizeof(ConstrCheck), CheckConstraintCmp);
4693 : :
4694 : : /* Install array only after it's fully valid */
4695 : 91073 : relation->rd_att->constr->check = check;
4696 : 91073 : relation->rd_att->constr->num_check = found;
4010 4697 : 91073 : }
4698 : :
4699 : : /*
4700 : : * qsort comparator to sort ConstrCheck entries by name
4701 : : */
4702 : : static int
4703 : 5195 : CheckConstraintCmp(const void *a, const void *b)
4704 : : {
4705 : 5195 : const ConstrCheck *ca = (const ConstrCheck *) a;
4706 : 5195 : const ConstrCheck *cb = (const ConstrCheck *) b;
4707 : :
4708 : 5195 : return strcmp(ca->ccname, cb->ccname);
4709 : : }
4710 : :
4711 : : /*
4712 : : * RelationGetFKeyList -- get a list of foreign key info for the relation
4713 : : *
4714 : : * Returns a list of ForeignKeyCacheInfo structs, one per FK constraining
4715 : : * the given relation. This data is a direct copy of relevant fields from
4716 : : * pg_constraint. The list items are in no particular order.
4717 : : *
4718 : : * CAUTION: the returned list is part of the relcache's data, and could
4719 : : * vanish in a relcache entry reset. Callers must inspect or copy it
4720 : : * before doing anything that might trigger a cache flush, such as
4721 : : * system catalog accesses. copyObject() can be used if desired.
4722 : : * (We define it this way because current callers want to filter and
4723 : : * modify the list entries anyway, so copying would be a waste of time.)
4724 : : */
4725 : : List *
3557 4726 : 138250 : RelationGetFKeyList(Relation relation)
4727 : : {
4728 : : List *result;
4729 : : Relation conrel;
4730 : : SysScanDesc conscan;
4731 : : ScanKeyData skey;
4732 : : HeapTuple htup;
4733 : : List *oldlist;
4734 : : MemoryContext oldcxt;
4735 : :
4736 : : /* Quick exit if we already computed the list. */
4737 [ + + ]: 138250 : if (relation->rd_fkeyvalid)
4738 : 110169 : return relation->rd_fkeylist;
4739 : :
4740 : : /*
4741 : : * We build the list we intend to return (in the caller's context) while
4742 : : * doing the scan. After successfully completing the scan, we copy that
4743 : : * list into the relcache entry. This avoids cache-context memory leakage
4744 : : * if we get some sort of error partway through.
4745 : : */
4746 : 28081 : result = NIL;
4747 : :
4748 : : /* Prepare to scan pg_constraint for entries having conrelid = this rel. */
4749 : 28081 : ScanKeyInit(&skey,
4750 : : Anum_pg_constraint_conrelid,
4751 : : BTEqualStrategyNumber, F_OIDEQ,
4752 : : ObjectIdGetDatum(RelationGetRelid(relation)));
4753 : :
2610 andres@anarazel.de 4754 : 28081 : conrel = table_open(ConstraintRelationId, AccessShareLock);
2749 tgl@sss.pgh.pa.us 4755 : 28081 : conscan = systable_beginscan(conrel, ConstraintRelidTypidNameIndexId, true,
4756 : : NULL, 1, &skey);
4757 : :
3557 4758 [ + + ]: 89741 : while (HeapTupleIsValid(htup = systable_getnext(conscan)))
4759 : : {
4760 : 61660 : Form_pg_constraint constraint = (Form_pg_constraint) GETSTRUCT(htup);
4761 : : ForeignKeyCacheInfo *info;
4762 : :
4763 : : /* consider only foreign keys */
4764 [ + + ]: 61660 : if (constraint->contype != CONSTRAINT_FOREIGN)
4765 : 59769 : continue;
4766 : :
4767 : 1891 : info = makeNode(ForeignKeyCacheInfo);
2672 andres@anarazel.de 4768 : 1891 : info->conoid = constraint->oid;
3557 tgl@sss.pgh.pa.us 4769 : 1891 : info->conrelid = constraint->conrelid;
4770 : 1891 : info->confrelid = constraint->confrelid;
347 peter@eisentraut.org 4771 : 1891 : info->conenforced = constraint->conenforced;
4772 : :
2613 alvherre@alvh.no-ip. 4773 : 1891 : DeconstructFkConstraintRow(htup, &info->nkeys,
4774 : 1891 : info->conkey,
4775 : 1891 : info->confkey,
4776 : 1891 : info->conpfeqop,
4777 : : NULL, NULL, NULL, NULL);
4778 : :
4779 : : /* Add FK's node to the result list */
3557 tgl@sss.pgh.pa.us 4780 : 1891 : result = lappend(result, info);
4781 : : }
4782 : :
4783 : 28081 : systable_endscan(conscan);
2610 andres@anarazel.de 4784 : 28081 : table_close(conrel, AccessShareLock);
4785 : :
4786 : : /* Now save a copy of the completed list in the relcache entry. */
3557 tgl@sss.pgh.pa.us 4787 : 28081 : oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
4788 : 28081 : oldlist = relation->rd_fkeylist;
4789 : 28081 : relation->rd_fkeylist = copyObject(result);
4790 : 28081 : relation->rd_fkeyvalid = true;
4791 : 28081 : MemoryContextSwitchTo(oldcxt);
4792 : :
4793 : : /* Don't leak the old list, if there is one */
4794 : 28081 : list_free_deep(oldlist);
4795 : :
4796 : 28081 : return result;
4797 : : }
4798 : :
4799 : : /*
4800 : : * RelationGetIndexList -- get a list of OIDs of indexes on this relation
4801 : : *
4802 : : * The index list is created only if someone requests it. We scan pg_index
4803 : : * to find relevant indexes, and add the list to the relcache entry so that
4804 : : * we won't have to compute it again. Note that shared cache inval of a
4805 : : * relcache entry will delete the old list and set rd_indexvalid to false,
4806 : : * so that we must recompute the index list on next request. This handles
4807 : : * creation or deletion of an index.
4808 : : *
4809 : : * Indexes that are marked not indislive are omitted from the returned list.
4810 : : * Such indexes are expected to be dropped momentarily, and should not be
4811 : : * touched at all by any caller of this function.
4812 : : *
4813 : : * The returned list is guaranteed to be sorted in order by OID. This is
4814 : : * needed by the executor, since for index types that we obtain exclusive
4815 : : * locks on when updating the index, all backends must lock the indexes in
4816 : : * the same order or we will get deadlocks (see ExecOpenIndices()). Any
4817 : : * consistent ordering would do, but ordering by OID is easy.
4818 : : *
4819 : : * Since shared cache inval causes the relcache's copy of the list to go away,
4820 : : * we return a copy of the list palloc'd in the caller's context. The caller
4821 : : * may list_free() the returned list after scanning it. This is necessary
4822 : : * since the caller will typically be doing syscache lookups on the relevant
4823 : : * indexes, and syscache lookup could cause SI messages to be processed!
4824 : : *
4825 : : * In exactly the same way, we update rd_pkindex, which is the OID of the
4826 : : * relation's primary key index if any, else InvalidOid; and rd_replidindex,
4827 : : * which is the pg_class OID of an index to be used as the relation's
4828 : : * replication identity index, or InvalidOid if there is no such index.
4829 : : */
4830 : : List *
9402 4831 : 1269759 : RelationGetIndexList(Relation relation)
4832 : : {
4833 : : Relation indrel;
4834 : : SysScanDesc indscan;
4835 : : ScanKeyData skey;
4836 : : HeapTuple htup;
4837 : : List *result;
4838 : : List *oldlist;
4510 rhaas@postgresql.org 4839 : 1269759 : char replident = relation->rd_rel->relreplident;
4840 : 1269759 : Oid pkeyIndex = InvalidOid;
4841 : 1269759 : Oid candidateIndex = InvalidOid;
737 alvherre@alvh.no-ip. 4842 : 1269759 : bool pkdeferrable = false;
4843 : : MemoryContext oldcxt;
4844 : :
4845 : : /* Quick exit if we already computed the list. */
2508 tgl@sss.pgh.pa.us 4846 [ + + ]: 1269759 : if (relation->rd_indexvalid)
7959 neilc@samurai.com 4847 : 1167513 : return list_copy(relation->rd_indexlist);
4848 : :
4849 : : /*
4850 : : * We build the list we intend to return (in the caller's context) while
4851 : : * doing the scan. After successfully completing the scan, we copy that
4852 : : * list into the relcache entry. This avoids cache-context memory leakage
4853 : : * if we get some sort of error partway through.
4854 : : */
9402 tgl@sss.pgh.pa.us 4855 : 102246 : result = NIL;
4856 : :
4857 : : /* Prepare to scan pg_index for entries having indrelid = this rel. */
8159 4858 : 102246 : ScanKeyInit(&skey,
4859 : : Anum_pg_index_indrelid,
4860 : : BTEqualStrategyNumber, F_OIDEQ,
4861 : : ObjectIdGetDatum(RelationGetRelid(relation)));
4862 : :
2610 andres@anarazel.de 4863 : 102246 : indrel = table_open(IndexRelationId, AccessShareLock);
7640 tgl@sss.pgh.pa.us 4864 : 102246 : indscan = systable_beginscan(indrel, IndexIndrelidIndexId, true,
4865 : : NULL, 1, &skey);
4866 : :
8790 4867 [ + + ]: 251384 : while (HeapTupleIsValid(htup = systable_getnext(indscan)))
4868 : : {
4869 : 149138 : Form_pg_index index = (Form_pg_index) GETSTRUCT(htup);
4870 : :
4871 : : /*
4872 : : * Ignore any indexes that are currently being dropped. This will
4873 : : * prevent them from being searched, inserted into, or considered in
4874 : : * HOT-safety decisions. It's unsafe to touch such an index at all
4875 : : * since its catalog entries could disappear at any instant.
4876 : : */
2635 peter_e@gmx.net 4877 [ + + ]: 149138 : if (!index->indislive)
5091 simon@2ndQuadrant.co 4878 : 38 : continue;
4879 : :
4880 : : /* add index's OID to result list */
2434 tgl@sss.pgh.pa.us 4881 : 149100 : result = lappend_oid(result, index->indexrelid);
4882 : :
4883 : : /*
4884 : : * Non-unique or predicate indexes aren't interesting for either oid
4885 : : * indexes or replication identity indexes, so don't check them.
4886 : : * Deferred ones are not useful for replication identity either; but
4887 : : * we do include them if they are PKs.
4888 : : */
492 alvherre@alvh.no-ip. 4889 [ + + ]: 149100 : if (!index->indisunique ||
2909 andrew@dunslane.net 4890 [ + + ]: 120346 : !heap_attisnull(htup, Anum_pg_index_indpred, NULL))
4510 rhaas@postgresql.org 4891 : 28847 : continue;
4892 : :
4893 : : /*
4894 : : * Remember primary key index, if any. For regular tables we do this
4895 : : * only if the index is valid; but for partitioned tables, then we do
4896 : : * it even if it's invalid.
4897 : : *
4898 : : * The reason for returning invalid primary keys for partitioned
4899 : : * tables is that we need it to prevent drop of not-null constraints
4900 : : * that may underlie such a primary key, which is only a problem for
4901 : : * partitioned tables.
4902 : : */
492 alvherre@alvh.no-ip. 4903 [ + + ]: 120253 : if (index->indisprimary &&
4904 [ + + ]: 74163 : (index->indisvalid ||
4905 [ + - ]: 6 : relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE))
4906 : : {
4510 rhaas@postgresql.org 4907 : 74163 : pkeyIndex = index->indexrelid;
492 alvherre@alvh.no-ip. 4908 : 74163 : pkdeferrable = !index->indimmediate;
4909 : : }
4910 : :
4911 [ + + ]: 120253 : if (!index->indimmediate)
4912 : 70 : continue;
4913 : :
4914 [ + + ]: 120183 : if (!index->indisvalid)
4915 : 60 : continue;
4916 : :
4917 : : /* remember explicitly chosen replica index */
4510 rhaas@postgresql.org 4918 [ + + ]: 120123 : if (index->indisreplident)
4919 : 271 : candidateIndex = index->indexrelid;
4920 : : }
4921 : :
8790 tgl@sss.pgh.pa.us 4922 : 102246 : systable_endscan(indscan);
4923 : :
2610 andres@anarazel.de 4924 : 102246 : table_close(indrel, AccessShareLock);
4925 : :
4926 : : /* Sort the result list into OID order, per API spec. */
2434 tgl@sss.pgh.pa.us 4927 : 102246 : list_sort(result, list_oid_cmp);
4928 : :
4929 : : /* Now save a copy of the completed list in the relcache entry. */
9391 4930 : 102246 : oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
4232 4931 : 102246 : oldlist = relation->rd_indexlist;
7959 neilc@samurai.com 4932 : 102246 : relation->rd_indexlist = list_copy(result);
3342 peter_e@gmx.net 4933 : 102246 : relation->rd_pkindex = pkeyIndex;
737 alvherre@alvh.no-ip. 4934 : 102246 : relation->rd_ispkdeferrable = pkdeferrable;
4935 [ + + + + : 102246 : if (replident == REPLICA_IDENTITY_DEFAULT && OidIsValid(pkeyIndex) && !pkdeferrable)
+ + ]
4323 tgl@sss.pgh.pa.us 4936 : 13603 : relation->rd_replidindex = pkeyIndex;
4937 [ + + + + ]: 88643 : else if (replident == REPLICA_IDENTITY_INDEX && OidIsValid(candidateIndex))
4938 : 271 : relation->rd_replidindex = candidateIndex;
4939 : : else
4940 : 88372 : relation->rd_replidindex = InvalidOid;
2508 4941 : 102246 : relation->rd_indexvalid = true;
9402 4942 : 102246 : MemoryContextSwitchTo(oldcxt);
4943 : :
4944 : : /* Don't leak the old list, if there is one */
4232 4945 : 102246 : list_free(oldlist);
4946 : :
9402 4947 : 102246 : return result;
4948 : : }
4949 : :
4950 : : /*
4951 : : * RelationGetStatExtList
4952 : : * get a list of OIDs of statistics objects on this relation
4953 : : *
4954 : : * The statistics list is created only if someone requests it, in a way
4955 : : * similar to RelationGetIndexList(). We scan pg_statistic_ext to find
4956 : : * relevant statistics, and add the list to the relcache entry so that we
4957 : : * won't have to compute it again. Note that shared cache inval of a
4958 : : * relcache entry will delete the old list and set rd_statvalid to 0,
4959 : : * so that we must recompute the statistics list on next request. This
4960 : : * handles creation or deletion of a statistics object.
4961 : : *
4962 : : * The returned list is guaranteed to be sorted in order by OID, although
4963 : : * this is not currently needed.
4964 : : *
4965 : : * Since shared cache inval causes the relcache's copy of the list to go away,
4966 : : * we return a copy of the list palloc'd in the caller's context. The caller
4967 : : * may list_free() the returned list after scanning it. This is necessary
4968 : : * since the caller will typically be doing syscache lookups on the relevant
4969 : : * statistics, and syscache lookup could cause SI messages to be processed!
4970 : : */
4971 : : List *
3278 alvherre@alvh.no-ip. 4972 : 267199 : RelationGetStatExtList(Relation relation)
4973 : : {
4974 : : Relation indrel;
4975 : : SysScanDesc indscan;
4976 : : ScanKeyData skey;
4977 : : HeapTuple htup;
4978 : : List *result;
4979 : : List *oldlist;
4980 : : MemoryContext oldcxt;
4981 : :
4982 : : /* Quick exit if we already computed the list. */
4983 [ + + ]: 267199 : if (relation->rd_statvalid != 0)
4984 : 203947 : return list_copy(relation->rd_statlist);
4985 : :
4986 : : /*
4987 : : * We build the list we intend to return (in the caller's context) while
4988 : : * doing the scan. After successfully completing the scan, we copy that
4989 : : * list into the relcache entry. This avoids cache-context memory leakage
4990 : : * if we get some sort of error partway through.
4991 : : */
4992 : 63252 : result = NIL;
4993 : :
4994 : : /*
4995 : : * Prepare to scan pg_statistic_ext for entries having stxrelid = this
4996 : : * rel.
4997 : : */
4998 : 63252 : ScanKeyInit(&skey,
4999 : : Anum_pg_statistic_ext_stxrelid,
5000 : : BTEqualStrategyNumber, F_OIDEQ,
5001 : : ObjectIdGetDatum(RelationGetRelid(relation)));
5002 : :
2610 andres@anarazel.de 5003 : 63252 : indrel = table_open(StatisticExtRelationId, AccessShareLock);
3278 alvherre@alvh.no-ip. 5004 : 63252 : indscan = systable_beginscan(indrel, StatisticExtRelidIndexId, true,
5005 : : NULL, 1, &skey);
5006 : :
5007 [ + + ]: 63481 : while (HeapTupleIsValid(htup = systable_getnext(indscan)))
5008 : : {
2616 tgl@sss.pgh.pa.us 5009 : 229 : Oid oid = ((Form_pg_statistic_ext) GETSTRUCT(htup))->oid;
5010 : :
2434 5011 : 229 : result = lappend_oid(result, oid);
5012 : : }
5013 : :
3278 alvherre@alvh.no-ip. 5014 : 63252 : systable_endscan(indscan);
5015 : :
2610 andres@anarazel.de 5016 : 63252 : table_close(indrel, AccessShareLock);
5017 : :
5018 : : /* Sort the result list into OID order, per API spec. */
2434 tgl@sss.pgh.pa.us 5019 : 63252 : list_sort(result, list_oid_cmp);
5020 : :
5021 : : /* Now save a copy of the completed list in the relcache entry. */
3278 alvherre@alvh.no-ip. 5022 : 63252 : oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
5023 : 63252 : oldlist = relation->rd_statlist;
5024 : 63252 : relation->rd_statlist = list_copy(result);
5025 : :
5026 : 63252 : relation->rd_statvalid = true;
5027 : 63252 : MemoryContextSwitchTo(oldcxt);
5028 : :
5029 : : /* Don't leak the old list, if there is one */
5030 : 63252 : list_free(oldlist);
5031 : :
5032 : 63252 : return result;
5033 : : }
5034 : :
5035 : : /*
5036 : : * RelationGetPrimaryKeyIndex -- get OID of the relation's primary key index
5037 : : *
5038 : : * Returns InvalidOid if there is no such index, or if the primary key is
5039 : : * DEFERRABLE and the caller isn't OK with that.
5040 : : */
5041 : : Oid
492 5042 : 270 : RelationGetPrimaryKeyIndex(Relation relation, bool deferrable_ok)
5043 : : {
5044 : : List *ilist;
5045 : :
2508 tgl@sss.pgh.pa.us 5046 [ + + ]: 270 : if (!relation->rd_indexvalid)
5047 : : {
5048 : : /* RelationGetIndexList does the heavy lifting. */
3342 peter_e@gmx.net 5049 : 9 : ilist = RelationGetIndexList(relation);
5050 : 9 : list_free(ilist);
2508 tgl@sss.pgh.pa.us 5051 [ - + ]: 9 : Assert(relation->rd_indexvalid);
5052 : : }
5053 : :
492 alvherre@alvh.no-ip. 5054 [ + + ]: 270 : if (deferrable_ok)
5055 : 9 : return relation->rd_pkindex;
5056 [ - + ]: 261 : else if (relation->rd_ispkdeferrable)
492 alvherre@alvh.no-ip. 5057 :UBC 0 : return InvalidOid;
492 alvherre@alvh.no-ip. 5058 :CBC 261 : return relation->rd_pkindex;
5059 : : }
5060 : :
5061 : : /*
5062 : : * RelationGetReplicaIndex -- get OID of the relation's replica identity index
5063 : : *
5064 : : * Returns InvalidOid if there is no such index.
5065 : : */
5066 : : Oid
4323 tgl@sss.pgh.pa.us 5067 : 233673 : RelationGetReplicaIndex(Relation relation)
5068 : : {
5069 : : List *ilist;
5070 : :
2508 5071 [ + + ]: 233673 : if (!relation->rd_indexvalid)
5072 : : {
5073 : : /* RelationGetIndexList does the heavy lifting. */
4323 5074 : 2891 : ilist = RelationGetIndexList(relation);
5075 : 2891 : list_free(ilist);
2508 5076 [ - + ]: 2891 : Assert(relation->rd_indexvalid);
5077 : : }
5078 : :
4323 5079 : 233673 : return relation->rd_replidindex;
5080 : : }
5081 : :
5082 : : /*
5083 : : * RelationGetIndexExpressions -- get the index expressions for an index
5084 : : *
5085 : : * We cache the result of transforming pg_index.indexprs into a node tree.
5086 : : * If the rel is not an index or has no expressional columns, we return NIL.
5087 : : * Otherwise, the returned tree is copied into the caller's memory context.
5088 : : * (We don't want to return a pointer to the relcache copy, since it could
5089 : : * disappear due to relcache invalidation.)
5090 : : */
5091 : : List *
8327 5092 : 2356920 : RelationGetIndexExpressions(Relation relation)
5093 : : {
5094 : : List *result;
5095 : : Datum exprsDatum;
5096 : : bool isnull;
5097 : : char *exprsString;
5098 : : MemoryContext oldcxt;
5099 : :
5100 : : /* Quick exit if we already computed the result. */
5101 [ + + ]: 2356920 : if (relation->rd_indexprs)
3293 peter_e@gmx.net 5102 : 1767 : return copyObject(relation->rd_indexprs);
5103 : :
5104 : : /* Quick exit if there is nothing to do. */
8327 tgl@sss.pgh.pa.us 5105 [ + - + + ]: 4710306 : if (relation->rd_indextuple == NULL ||
2909 andrew@dunslane.net 5106 : 2355153 : heap_attisnull(relation->rd_indextuple, Anum_pg_index_indexprs, NULL))
8327 tgl@sss.pgh.pa.us 5107 : 2354280 : return NIL;
5108 : :
5109 : : /*
5110 : : * We build the tree we intend to return in the caller's context. After
5111 : : * successfully completing the work, we copy it into the relcache entry.
5112 : : * This avoids problems if we get some sort of error partway through.
5113 : : */
7656 5114 : 873 : exprsDatum = heap_getattr(relation->rd_indextuple,
5115 : : Anum_pg_index_indexprs,
5116 : : GetPgIndexDescriptor(),
5117 : : &isnull);
8327 5118 [ - + ]: 873 : Assert(!isnull);
6564 5119 : 873 : exprsString = TextDatumGetCString(exprsDatum);
8327 5120 : 873 : result = (List *) stringToNode(exprsString);
5121 : 873 : pfree(exprsString);
5122 : :
5123 : : /*
5124 : : * Run the expressions through eval_const_expressions. This is not just an
5125 : : * optimization, but is necessary, because the planner will be comparing
5126 : : * them to similarly-processed qual clauses, and may fail to detect valid
5127 : : * matches without this. We must not use canonicalize_qual, however,
5128 : : * since these aren't qual expressions.
5129 : : */
6557 5130 : 873 : result = (List *) eval_const_expressions(NULL, (Node *) result);
5131 : :
5132 : : /* May as well fix opfuncids too */
8327 5133 : 873 : fix_opfuncids((Node *) result);
5134 : :
5135 : : /* Now save a copy of the completed tree in the relcache entry. */
5906 5136 : 873 : oldcxt = MemoryContextSwitchTo(relation->rd_indexcxt);
3293 peter_e@gmx.net 5137 : 873 : relation->rd_indexprs = copyObject(result);
8327 tgl@sss.pgh.pa.us 5138 : 873 : MemoryContextSwitchTo(oldcxt);
5139 : :
5140 : 873 : return result;
5141 : : }
5142 : :
5143 : : /*
5144 : : * RelationGetDummyIndexExpressions -- get dummy expressions for an index
5145 : : *
5146 : : * Return a list of dummy expressions (just Const nodes) with the same
5147 : : * types/typmods/collations as the index's real expressions. This is
5148 : : * useful in situations where we don't want to run any user-defined code.
5149 : : */
5150 : : List *
2296 5151 : 117 : RelationGetDummyIndexExpressions(Relation relation)
5152 : : {
5153 : : List *result;
5154 : : Datum exprsDatum;
5155 : : bool isnull;
5156 : : char *exprsString;
5157 : : List *rawExprs;
5158 : : ListCell *lc;
5159 : :
5160 : : /* Quick exit if there is nothing to do. */
5161 [ + - + + ]: 234 : if (relation->rd_indextuple == NULL ||
5162 : 117 : heap_attisnull(relation->rd_indextuple, Anum_pg_index_indexprs, NULL))
5163 : 90 : return NIL;
5164 : :
5165 : : /* Extract raw node tree(s) from index tuple. */
5166 : 27 : exprsDatum = heap_getattr(relation->rd_indextuple,
5167 : : Anum_pg_index_indexprs,
5168 : : GetPgIndexDescriptor(),
5169 : : &isnull);
5170 [ - + ]: 27 : Assert(!isnull);
5171 : 27 : exprsString = TextDatumGetCString(exprsDatum);
5172 : 27 : rawExprs = (List *) stringToNode(exprsString);
5173 : 27 : pfree(exprsString);
5174 : :
5175 : : /* Construct null Consts; the typlen and typbyval are arbitrary. */
5176 : 27 : result = NIL;
5177 [ + - + + : 54 : foreach(lc, rawExprs)
+ + ]
5178 : : {
5179 : 27 : Node *rawExpr = (Node *) lfirst(lc);
5180 : :
5181 : 27 : result = lappend(result,
5182 : 27 : makeConst(exprType(rawExpr),
5183 : : exprTypmod(rawExpr),
5184 : : exprCollation(rawExpr),
5185 : : 1,
5186 : : (Datum) 0,
5187 : : true,
5188 : : true));
5189 : : }
5190 : :
5191 : 27 : return result;
5192 : : }
5193 : :
5194 : : /*
5195 : : * RelationGetIndexPredicate -- get the index predicate for an index
5196 : : *
5197 : : * We cache the result of transforming pg_index.indpred into an implicit-AND
5198 : : * node tree (suitable for use in planning).
5199 : : * If the rel is not an index or has no predicate, we return NIL.
5200 : : * Otherwise, the returned tree is copied into the caller's memory context.
5201 : : * (We don't want to return a pointer to the relcache copy, since it could
5202 : : * disappear due to relcache invalidation.)
5203 : : */
5204 : : List *
8327 5205 : 2356829 : RelationGetIndexPredicate(Relation relation)
5206 : : {
5207 : : List *result;
5208 : : Datum predDatum;
5209 : : bool isnull;
5210 : : char *predString;
5211 : : MemoryContext oldcxt;
5212 : :
5213 : : /* Quick exit if we already computed the result. */
5214 [ + + ]: 2356829 : if (relation->rd_indpred)
3293 peter_e@gmx.net 5215 : 700 : return copyObject(relation->rd_indpred);
5216 : :
5217 : : /* Quick exit if there is nothing to do. */
8327 tgl@sss.pgh.pa.us 5218 [ + - + + ]: 4712258 : if (relation->rd_indextuple == NULL ||
2909 andrew@dunslane.net 5219 : 2356129 : heap_attisnull(relation->rd_indextuple, Anum_pg_index_indpred, NULL))
8327 tgl@sss.pgh.pa.us 5220 : 2355619 : return NIL;
5221 : :
5222 : : /*
5223 : : * We build the tree we intend to return in the caller's context. After
5224 : : * successfully completing the work, we copy it into the relcache entry.
5225 : : * This avoids problems if we get some sort of error partway through.
5226 : : */
7656 5227 : 510 : predDatum = heap_getattr(relation->rd_indextuple,
5228 : : Anum_pg_index_indpred,
5229 : : GetPgIndexDescriptor(),
5230 : : &isnull);
8327 5231 [ - + ]: 510 : Assert(!isnull);
6564 5232 : 510 : predString = TextDatumGetCString(predDatum);
8327 5233 : 510 : result = (List *) stringToNode(predString);
5234 : 510 : pfree(predString);
5235 : :
5236 : : /*
5237 : : * Run the expression through const-simplification and canonicalization.
5238 : : * This is not just an optimization, but is necessary, because the planner
5239 : : * will be comparing it to similarly-processed qual clauses, and may fail
5240 : : * to detect valid matches without this. This must match the processing
5241 : : * done to qual clauses in preprocess_expression()! (We can skip the
5242 : : * stuff involving subqueries, however, since we don't allow any in index
5243 : : * predicates.)
5244 : : */
6557 5245 : 510 : result = (List *) eval_const_expressions(NULL, (Node *) result);
5246 : :
2926 5247 : 510 : result = (List *) canonicalize_qual((Expr *) result, false);
5248 : :
5249 : : /* Also convert to implicit-AND format */
8113 5250 : 510 : result = make_ands_implicit((Expr *) result);
5251 : :
5252 : : /* May as well fix opfuncids too */
8327 5253 : 510 : fix_opfuncids((Node *) result);
5254 : :
5255 : : /* Now save a copy of the completed tree in the relcache entry. */
5906 5256 : 510 : oldcxt = MemoryContextSwitchTo(relation->rd_indexcxt);
3293 peter_e@gmx.net 5257 : 510 : relation->rd_indpred = copyObject(result);
8327 tgl@sss.pgh.pa.us 5258 : 510 : MemoryContextSwitchTo(oldcxt);
5259 : :
5260 : 510 : return result;
5261 : : }
5262 : :
5263 : : /*
5264 : : * RelationGetIndexAttrBitmap -- get a bitmap of index attribute numbers
5265 : : *
5266 : : * The result has a bit set for each attribute used anywhere in the index
5267 : : * definitions of all the indexes on this relation. (This includes not only
5268 : : * simple index keys, but attributes used in expressions and partial-index
5269 : : * predicates.)
5270 : : *
5271 : : * Depending on attrKind, a bitmap covering attnums for certain columns is
5272 : : * returned:
5273 : : * INDEX_ATTR_BITMAP_KEY Columns in non-partial unique indexes not
5274 : : * in expressions (i.e., usable for FKs)
5275 : : * INDEX_ATTR_BITMAP_PRIMARY_KEY Columns in the table's primary key
5276 : : * (beware: even if PK is deferrable!)
5277 : : * INDEX_ATTR_BITMAP_IDENTITY_KEY Columns in the table's replica identity
5278 : : * index (empty if FULL)
5279 : : * INDEX_ATTR_BITMAP_HOT_BLOCKING Columns that block updates from being HOT
5280 : : * INDEX_ATTR_BITMAP_SUMMARIZED Columns included in summarizing indexes
5281 : : *
5282 : : * Attribute numbers are offset by FirstLowInvalidHeapAttributeNumber so that
5283 : : * we can include system attributes (e.g., OID) in the bitmap representation.
5284 : : *
5285 : : * Deferred indexes are considered for the primary key, but not for replica
5286 : : * identity.
5287 : : *
5288 : : * Caller had better hold at least RowExclusiveLock on the target relation
5289 : : * to ensure it is safe (deadlock-free) for us to take locks on the relation's
5290 : : * indexes. Note that since the introduction of CREATE INDEX CONCURRENTLY,
5291 : : * that lock level doesn't guarantee a stable set of indexes, so we have to
5292 : : * be prepared to retry here in case of a change in the set of indexes.
5293 : : *
5294 : : * The returned result is palloc'd in the caller's memory context and should
5295 : : * be bms_free'd when not needed anymore.
5296 : : */
5297 : : Bitmapset *
4478 rhaas@postgresql.org 5298 : 1295149 : RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind)
5299 : : {
5300 : : Bitmapset *uindexattrs; /* columns in unique indexes */
5301 : : Bitmapset *pkindexattrs; /* columns in the primary index */
5302 : : Bitmapset *idindexattrs; /* columns in the replica identity */
5303 : : Bitmapset *hotblockingattrs; /* columns with HOT blocking indexes */
5304 : : Bitmapset *summarizedattrs; /* columns with summarizing indexes */
5305 : : List *indexoidlist;
5306 : : List *newindexoidlist;
5307 : : Oid relpkindex;
5308 : : Oid relreplindex;
5309 : : ListCell *l;
5310 : : MemoryContext oldcxt;
5311 : :
5312 : : /* Quick exit if we already computed the result. */
1091 tomas.vondra@postgre 5313 [ + + ]: 1295149 : if (relation->rd_attrsvalid)
5314 : : {
4331 bruce@momjian.us 5315 [ + + + + : 1096155 : switch (attrKind)
+ - ]
5316 : : {
4323 tgl@sss.pgh.pa.us 5317 : 266281 : case INDEX_ATTR_BITMAP_KEY:
5318 : 266281 : return bms_copy(relation->rd_keyattr);
3342 peter_e@gmx.net 5319 : 38 : case INDEX_ATTR_BITMAP_PRIMARY_KEY:
5320 : 38 : return bms_copy(relation->rd_pkattr);
4323 tgl@sss.pgh.pa.us 5321 : 310132 : case INDEX_ATTR_BITMAP_IDENTITY_KEY:
5322 : 310132 : return bms_copy(relation->rd_idattr);
1091 tomas.vondra@postgre 5323 : 256715 : case INDEX_ATTR_BITMAP_HOT_BLOCKING:
5324 : 256715 : return bms_copy(relation->rd_hotblockingattr);
5325 : 262989 : case INDEX_ATTR_BITMAP_SUMMARIZED:
5326 : 262989 : return bms_copy(relation->rd_summarizedattr);
4478 rhaas@postgresql.org 5327 :UBC 0 : default:
5328 [ # # ]: 0 : elog(ERROR, "unknown attrKind %u", attrKind);
5329 : : }
5330 : : }
5331 : :
5332 : : /* Fast path if definitely no indexes */
6751 tgl@sss.pgh.pa.us 5333 [ + + ]:CBC 198994 : if (!RelationGetForm(relation)->relhasindex)
5334 : 190833 : return NULL;
5335 : :
5336 : : /*
5337 : : * Get cached list of index OIDs. If we have to start over, we do so here.
5338 : : */
3324 5339 : 8161 : restart:
6751 5340 : 8167 : indexoidlist = RelationGetIndexList(relation);
5341 : :
5342 : : /* Fall out if no indexes (but relhasindex was set) */
5343 [ + + ]: 8167 : if (indexoidlist == NIL)
5344 : 577 : return NULL;
5345 : :
5346 : : /*
5347 : : * Copy the rd_pkindex and rd_replidindex values computed by
5348 : : * RelationGetIndexList before proceeding. This is needed because a
5349 : : * relcache flush could occur inside index_open below, resetting the
5350 : : * fields managed by RelationGetIndexList. We need to do the work with
5351 : : * stable values of these fields.
5352 : : */
3342 peter_e@gmx.net 5353 : 7590 : relpkindex = relation->rd_pkindex;
4323 tgl@sss.pgh.pa.us 5354 : 7590 : relreplindex = relation->rd_replidindex;
5355 : :
5356 : : /*
5357 : : * For each index, add referenced attributes to indexattrs.
5358 : : *
5359 : : * Note: we consider all indexes returned by RelationGetIndexList, even if
5360 : : * they are not indisready or indisvalid. This is important because an
5361 : : * index for which CREATE INDEX CONCURRENTLY has just started must be
5362 : : * included in HOT-safety decisions (see README.HOT). If a DROP INDEX
5363 : : * CONCURRENTLY is far enough along that we should ignore the index, it
5364 : : * won't be returned at all by RelationGetIndexList.
5365 : : */
4799 alvherre@alvh.no-ip. 5366 : 7590 : uindexattrs = NULL;
3342 peter_e@gmx.net 5367 : 7590 : pkindexattrs = NULL;
4478 rhaas@postgresql.org 5368 : 7590 : idindexattrs = NULL;
1091 tomas.vondra@postgre 5369 : 7590 : hotblockingattrs = NULL;
5370 : 7590 : summarizedattrs = NULL;
6751 tgl@sss.pgh.pa.us 5371 [ + - + + : 21408 : foreach(l, indexoidlist)
+ + ]
5372 : : {
5373 : 13818 : Oid indexOid = lfirst_oid(l);
5374 : : Relation indexDesc;
5375 : : Datum datum;
5376 : : bool isnull;
5377 : : Node *indexExpressions;
5378 : : Node *indexPredicate;
5379 : : int i;
5380 : : bool isKey; /* candidate key */
5381 : : bool isPK; /* primary key */
5382 : : bool isIDKey; /* replica identity index */
5383 : : Bitmapset **attrs;
5384 : :
5385 : 13818 : indexDesc = index_open(indexOid, AccessShareLock);
5386 : :
5387 : : /*
5388 : : * Extract index expressions and index predicate. Note: Don't use
5389 : : * RelationGetIndexExpressions()/RelationGetIndexPredicate(), because
5390 : : * those might run constant expressions evaluation, which needs a
5391 : : * snapshot, which we might not have here. (Also, it's probably more
5392 : : * sound to collect the bitmaps before any transformations that might
5393 : : * eliminate columns, but the practical impact of this is limited.)
5394 : : */
5395 : :
2603 peter@eisentraut.org 5396 : 13818 : datum = heap_getattr(indexDesc->rd_indextuple, Anum_pg_index_indexprs,
5397 : : GetPgIndexDescriptor(), &isnull);
5398 [ + + ]: 13818 : if (!isnull)
5399 : 24 : indexExpressions = stringToNode(TextDatumGetCString(datum));
5400 : : else
5401 : 13794 : indexExpressions = NULL;
5402 : :
5403 : 13818 : datum = heap_getattr(indexDesc->rd_indextuple, Anum_pg_index_indpred,
5404 : : GetPgIndexDescriptor(), &isnull);
5405 [ + + ]: 13818 : if (!isnull)
5406 : 56 : indexPredicate = stringToNode(TextDatumGetCString(datum));
5407 : : else
5408 : 13762 : indexPredicate = NULL;
5409 : :
5410 : : /* Can this index be referenced by a foreign key? */
5411 [ + + ]: 10885 : isKey = indexDesc->rd_index->indisunique &&
5412 [ + + + + ]: 24703 : indexExpressions == NULL &&
5413 : : indexPredicate == NULL;
5414 : :
5415 : : /* Is this a primary key? */
3342 peter_e@gmx.net 5416 : 13818 : isPK = (indexOid == relpkindex);
5417 : :
5418 : : /* Is this index the configured (or default) replica identity? */
4323 tgl@sss.pgh.pa.us 5419 : 13818 : isIDKey = (indexOid == relreplindex);
5420 : :
5421 : : /*
5422 : : * If the index is summarizing, it doesn't block HOT updates, but we
5423 : : * may still need to update it (if the attributes were modified). So
5424 : : * decide which bitmap we'll update in the following loop.
5425 : : */
1091 tomas.vondra@postgre 5426 [ + + ]: 13818 : if (indexDesc->rd_indam->amsummarizing)
5427 : 39 : attrs = &summarizedattrs;
5428 : : else
5429 : 13779 : attrs = &hotblockingattrs;
5430 : :
5431 : : /* Collect simple attribute references */
2603 peter@eisentraut.org 5432 [ + + ]: 35392 : for (i = 0; i < indexDesc->rd_index->indnatts; i++)
5433 : : {
5434 : 21574 : int attrnum = indexDesc->rd_index->indkey.values[i];
5435 : :
5436 : : /*
5437 : : * Since we have covering indexes with non-key columns, we must
5438 : : * handle them accurately here. non-key columns must be added into
5439 : : * hotblockingattrs or summarizedattrs, since they are in index,
5440 : : * and update shouldn't miss them.
5441 : : *
5442 : : * Summarizing indexes do not block HOT, but do need to be updated
5443 : : * when the column value changes, thus require a separate
5444 : : * attribute bitmapset.
5445 : : *
5446 : : * Obviously, non-key columns couldn't be referenced by foreign
5447 : : * key or identity key. Hence we do not include them into
5448 : : * uindexattrs, pkindexattrs and idindexattrs bitmaps.
5449 : : */
6751 tgl@sss.pgh.pa.us 5450 [ + + ]: 21574 : if (attrnum != 0)
5451 : : {
1091 tomas.vondra@postgre 5452 : 21550 : *attrs = bms_add_member(*attrs,
5453 : : attrnum - FirstLowInvalidHeapAttributeNumber);
5454 : :
2603 peter@eisentraut.org 5455 [ + + + + ]: 21550 : if (isKey && i < indexDesc->rd_index->indnkeyatts)
4799 alvherre@alvh.no-ip. 5456 : 16162 : uindexattrs = bms_add_member(uindexattrs,
5457 : : attrnum - FirstLowInvalidHeapAttributeNumber);
5458 : :
2603 peter@eisentraut.org 5459 [ + + + + ]: 21550 : if (isPK && i < indexDesc->rd_index->indnkeyatts)
3342 peter_e@gmx.net 5460 : 8246 : pkindexattrs = bms_add_member(pkindexattrs,
5461 : : attrnum - FirstLowInvalidHeapAttributeNumber);
5462 : :
2603 peter@eisentraut.org 5463 [ + + + + ]: 21550 : if (isIDKey && i < indexDesc->rd_index->indnkeyatts)
4323 tgl@sss.pgh.pa.us 5464 : 2316 : idindexattrs = bms_add_member(idindexattrs,
5465 : : attrnum - FirstLowInvalidHeapAttributeNumber);
5466 : : }
5467 : : }
5468 : :
5469 : : /* Collect all attributes used in expressions, too */
1091 tomas.vondra@postgre 5470 : 13818 : pull_varattnos(indexExpressions, 1, attrs);
5471 : :
5472 : : /* Collect all attributes in the index predicate, too */
5473 : 13818 : pull_varattnos(indexPredicate, 1, attrs);
5474 : :
6751 tgl@sss.pgh.pa.us 5475 : 13818 : index_close(indexDesc, AccessShareLock);
5476 : : }
5477 : :
5478 : : /*
5479 : : * During one of the index_opens in the above loop, we might have received
5480 : : * a relcache flush event on this relcache entry, which might have been
5481 : : * signaling a change in the rel's index list. If so, we'd better start
5482 : : * over to ensure we deliver up-to-date attribute bitmaps.
5483 : : */
3324 5484 : 7590 : newindexoidlist = RelationGetIndexList(relation);
5485 [ + + ]: 7590 : if (equal(indexoidlist, newindexoidlist) &&
5486 [ + + ]: 7587 : relpkindex == relation->rd_pkindex &&
5487 [ + - ]: 7584 : relreplindex == relation->rd_replidindex)
5488 : : {
5489 : : /* Still the same index set, so proceed */
5490 : 7584 : list_free(newindexoidlist);
5491 : 7584 : list_free(indexoidlist);
5492 : : }
5493 : : else
5494 : : {
5495 : : /* Gotta do it over ... might as well not leak memory */
3324 tgl@sss.pgh.pa.us 5496 :GBC 6 : list_free(newindexoidlist);
5497 : 6 : list_free(indexoidlist);
5498 : 6 : bms_free(uindexattrs);
5499 : 6 : bms_free(pkindexattrs);
5500 : 6 : bms_free(idindexattrs);
1091 tomas.vondra@postgre 5501 : 6 : bms_free(hotblockingattrs);
5502 : 6 : bms_free(summarizedattrs);
5503 : :
3324 tgl@sss.pgh.pa.us 5504 : 6 : goto restart;
5505 : : }
5506 : :
5507 : : /* Don't leak the old values of these bitmaps, if any */
1091 tomas.vondra@postgre 5508 :CBC 7584 : relation->rd_attrsvalid = false;
4232 tgl@sss.pgh.pa.us 5509 : 7584 : bms_free(relation->rd_keyattr);
5510 : 7584 : relation->rd_keyattr = NULL;
3342 peter_e@gmx.net 5511 : 7584 : bms_free(relation->rd_pkattr);
5512 : 7584 : relation->rd_pkattr = NULL;
4232 tgl@sss.pgh.pa.us 5513 : 7584 : bms_free(relation->rd_idattr);
5514 : 7584 : relation->rd_idattr = NULL;
1091 tomas.vondra@postgre 5515 : 7584 : bms_free(relation->rd_hotblockingattr);
5516 : 7584 : relation->rd_hotblockingattr = NULL;
5517 : 7584 : bms_free(relation->rd_summarizedattr);
5518 : 7584 : relation->rd_summarizedattr = NULL;
5519 : :
5520 : : /*
5521 : : * Now save copies of the bitmaps in the relcache entry. We intentionally
5522 : : * set rd_attrsvalid last, because that's the one that signals validity of
5523 : : * the values; if we run out of memory before making that copy, we won't
5524 : : * leave the relcache entry looking like the other ones are valid but
5525 : : * empty.
5526 : : */
6751 tgl@sss.pgh.pa.us 5527 : 7584 : oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
4799 alvherre@alvh.no-ip. 5528 : 7584 : relation->rd_keyattr = bms_copy(uindexattrs);
3342 peter_e@gmx.net 5529 : 7584 : relation->rd_pkattr = bms_copy(pkindexattrs);
4478 rhaas@postgresql.org 5530 : 7584 : relation->rd_idattr = bms_copy(idindexattrs);
1091 tomas.vondra@postgre 5531 : 7584 : relation->rd_hotblockingattr = bms_copy(hotblockingattrs);
5532 : 7584 : relation->rd_summarizedattr = bms_copy(summarizedattrs);
5533 : 7584 : relation->rd_attrsvalid = true;
6751 tgl@sss.pgh.pa.us 5534 : 7584 : MemoryContextSwitchTo(oldcxt);
5535 : :
5536 : : /* We return our original working copy for caller to play with */
4331 bruce@momjian.us 5537 [ + + + + : 7584 : switch (attrKind)
- - ]
5538 : : {
4323 tgl@sss.pgh.pa.us 5539 : 606 : case INDEX_ATTR_BITMAP_KEY:
5540 : 606 : return uindexattrs;
3342 peter_e@gmx.net 5541 : 27 : case INDEX_ATTR_BITMAP_PRIMARY_KEY:
2610 tgl@sss.pgh.pa.us 5542 : 27 : return pkindexattrs;
4323 5543 : 677 : case INDEX_ATTR_BITMAP_IDENTITY_KEY:
5544 : 677 : return idindexattrs;
1091 tomas.vondra@postgre 5545 : 6274 : case INDEX_ATTR_BITMAP_HOT_BLOCKING:
5546 : 6274 : return hotblockingattrs;
1091 tomas.vondra@postgre 5547 :UBC 0 : case INDEX_ATTR_BITMAP_SUMMARIZED:
5548 : 0 : return summarizedattrs;
4478 rhaas@postgresql.org 5549 : 0 : default:
5550 [ # # ]: 0 : elog(ERROR, "unknown attrKind %u", attrKind);
5551 : : return NULL;
5552 : : }
5553 : : }
5554 : :
5555 : : /*
5556 : : * RelationGetIdentityKeyBitmap -- get a bitmap of replica identity attribute
5557 : : * numbers
5558 : : *
5559 : : * A bitmap of index attribute numbers for the configured replica identity
5560 : : * index is returned.
5561 : : *
5562 : : * See also comments of RelationGetIndexAttrBitmap().
5563 : : *
5564 : : * This is a special purpose function used during logical replication. Here,
5565 : : * unlike RelationGetIndexAttrBitmap(), we don't acquire a lock on the required
5566 : : * index as we build the cache entry using a historic snapshot and all the
5567 : : * later changes are absorbed while decoding WAL. Due to this reason, we don't
5568 : : * need to retry here in case of a change in the set of indexes.
5569 : : */
5570 : : Bitmapset *
1783 akapila@postgresql.o 5571 :CBC 337 : RelationGetIdentityKeyBitmap(Relation relation)
5572 : : {
5573 : 337 : Bitmapset *idindexattrs = NULL; /* columns in the replica identity */
5574 : : Relation indexDesc;
5575 : : int i;
5576 : : Oid replidindex;
5577 : : MemoryContext oldcxt;
5578 : :
5579 : : /* Quick exit if we already computed the result */
5580 [ + + ]: 337 : if (relation->rd_idattr != NULL)
5581 : 47 : return bms_copy(relation->rd_idattr);
5582 : :
5583 : : /* Fast path if definitely no indexes */
5584 [ + + ]: 290 : if (!RelationGetForm(relation)->relhasindex)
5585 : 69 : return NULL;
5586 : :
5587 : : /* Historic snapshot must be set. */
5588 [ - + ]: 221 : Assert(HistoricSnapshotActive());
5589 : :
1721 5590 : 221 : replidindex = RelationGetReplicaIndex(relation);
5591 : :
5592 : : /* Fall out if there is no replica identity index */
5593 [ + + ]: 221 : if (!OidIsValid(replidindex))
1730 5594 : 5 : return NULL;
5595 : :
5596 : : /* Look up the description for the replica identity index */
1721 5597 : 216 : indexDesc = RelationIdGetRelation(replidindex);
5598 : :
1730 5599 [ - + ]: 216 : if (!RelationIsValid(indexDesc))
1730 akapila@postgresql.o 5600 [ # # ]:UBC 0 : elog(ERROR, "could not open relation with OID %u",
5601 : : relation->rd_replidindex);
5602 : :
5603 : : /* Add referenced attributes to idindexattrs */
1783 akapila@postgresql.o 5604 [ + + ]:CBC 439 : for (i = 0; i < indexDesc->rd_index->indnatts; i++)
5605 : : {
5606 : 223 : int attrnum = indexDesc->rd_index->indkey.values[i];
5607 : :
5608 : : /*
5609 : : * We don't include non-key columns into idindexattrs bitmaps. See
5610 : : * RelationGetIndexAttrBitmap.
5611 : : */
5612 [ + - ]: 223 : if (attrnum != 0)
5613 : : {
5614 [ + + ]: 223 : if (i < indexDesc->rd_index->indnkeyatts)
5615 : 222 : idindexattrs = bms_add_member(idindexattrs,
5616 : : attrnum - FirstLowInvalidHeapAttributeNumber);
5617 : : }
5618 : : }
5619 : :
5620 : 216 : RelationClose(indexDesc);
5621 : :
5622 : : /* Don't leak the old values of these bitmaps, if any */
5623 : 216 : bms_free(relation->rd_idattr);
5624 : 216 : relation->rd_idattr = NULL;
5625 : :
5626 : : /* Now save copy of the bitmap in the relcache entry */
5627 : 216 : oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
5628 : 216 : relation->rd_idattr = bms_copy(idindexattrs);
5629 : 216 : MemoryContextSwitchTo(oldcxt);
5630 : :
5631 : : /* We return our original working copy for caller to play with */
5632 : 216 : return idindexattrs;
5633 : : }
5634 : :
5635 : : /*
5636 : : * RelationGetExclusionInfo -- get info about index's exclusion constraint
5637 : : *
5638 : : * This should be called only for an index that is known to have an associated
5639 : : * exclusion constraint or primary key/unique constraint using WITHOUT
5640 : : * OVERLAPS.
5641 : : *
5642 : : * It returns arrays (palloc'd in caller's context) of the exclusion operator
5643 : : * OIDs, their underlying functions' OIDs, and their strategy numbers in the
5644 : : * index's opclasses. We cache all this information since it requires a fair
5645 : : * amount of work to get.
5646 : : */
5647 : : void
5942 tgl@sss.pgh.pa.us 5648 : 1122 : RelationGetExclusionInfo(Relation indexRelation,
5649 : : Oid **operators,
5650 : : Oid **procs,
5651 : : uint16 **strategies)
5652 : : {
5653 : : int indnkeyatts;
5654 : : Oid *ops;
5655 : : Oid *funcs;
5656 : : uint16 *strats;
5657 : : Relation conrel;
5658 : : SysScanDesc conscan;
5659 : : ScanKeyData skey[1];
5660 : : HeapTuple htup;
5661 : : bool found;
5662 : : MemoryContext oldcxt;
5663 : : int i;
5664 : :
2899 teodor@sigaev.ru 5665 : 1122 : indnkeyatts = IndexRelationGetNumberOfKeyAttributes(indexRelation);
5666 : :
5667 : : /* Allocate result space in caller context */
95 michael@paquier.xyz 5668 :GNC 1122 : *operators = ops = palloc_array(Oid, indnkeyatts);
5669 : 1122 : *procs = funcs = palloc_array(Oid, indnkeyatts);
5670 : 1122 : *strategies = strats = palloc_array(uint16, indnkeyatts);
5671 : :
5672 : : /* Quick exit if we have the data cached already */
5942 tgl@sss.pgh.pa.us 5673 [ + + ]:CBC 1122 : if (indexRelation->rd_exclstrats != NULL)
5674 : : {
2899 teodor@sigaev.ru 5675 : 776 : memcpy(ops, indexRelation->rd_exclops, sizeof(Oid) * indnkeyatts);
5676 : 776 : memcpy(funcs, indexRelation->rd_exclprocs, sizeof(Oid) * indnkeyatts);
5677 : 776 : memcpy(strats, indexRelation->rd_exclstrats, sizeof(uint16) * indnkeyatts);
5942 tgl@sss.pgh.pa.us 5678 : 776 : return;
5679 : : }
5680 : :
5681 : : /*
5682 : : * Search pg_constraint for the constraint associated with the index. To
5683 : : * make this not too painfully slow, we use the index on conrelid; that
5684 : : * will hold the parent relation's OID not the index's own OID.
5685 : : *
5686 : : * Note: if we wanted to rely on the constraint name matching the index's
5687 : : * name, we could just do a direct lookup using pg_constraint's unique
5688 : : * index. For the moment it doesn't seem worth requiring that.
5689 : : */
5690 : 346 : ScanKeyInit(&skey[0],
5691 : : Anum_pg_constraint_conrelid,
5692 : : BTEqualStrategyNumber, F_OIDEQ,
5693 : 346 : ObjectIdGetDatum(indexRelation->rd_index->indrelid));
5694 : :
2610 andres@anarazel.de 5695 : 346 : conrel = table_open(ConstraintRelationId, AccessShareLock);
2749 tgl@sss.pgh.pa.us 5696 : 346 : conscan = systable_beginscan(conrel, ConstraintRelidTypidNameIndexId, true,
5697 : : NULL, 1, skey);
5942 5698 : 346 : found = false;
5699 : :
5700 [ + + ]: 1372 : while (HeapTupleIsValid(htup = systable_getnext(conscan)))
5701 : : {
5861 bruce@momjian.us 5702 : 1026 : Form_pg_constraint conform = (Form_pg_constraint) GETSTRUCT(htup);
5703 : : Datum val;
5704 : : bool isnull;
5705 : : ArrayType *arr;
5706 : : int nelem;
5707 : :
5708 : : /* We want the exclusion constraint owning the index */
544 peter@eisentraut.org 5709 [ + + ]: 1026 : if ((conform->contype != CONSTRAINT_EXCLUSION &&
474 alvherre@alvh.no-ip. 5710 [ + + + + ]: 911 : !(conform->conperiod && (conform->contype == CONSTRAINT_PRIMARY
544 peter@eisentraut.org 5711 [ + + ]: 115 : || conform->contype == CONSTRAINT_UNIQUE))) ||
5942 tgl@sss.pgh.pa.us 5712 [ + + ]: 412 : conform->conindid != RelationGetRelid(indexRelation))
5713 : 680 : continue;
5714 : :
5715 : : /* There should be only one */
5716 [ - + ]: 346 : if (found)
5942 tgl@sss.pgh.pa.us 5717 [ # # ]:UBC 0 : elog(ERROR, "unexpected exclusion constraint record found for rel %s",
5718 : : RelationGetRelationName(indexRelation));
5942 tgl@sss.pgh.pa.us 5719 :CBC 346 : found = true;
5720 : :
5721 : : /* Extract the operator OIDS from conexclop */
5722 : 346 : val = fastgetattr(htup,
5723 : : Anum_pg_constraint_conexclop,
5724 : : conrel->rd_att, &isnull);
5725 [ - + ]: 346 : if (isnull)
5942 tgl@sss.pgh.pa.us 5726 [ # # ]:UBC 0 : elog(ERROR, "null conexclop for rel %s",
5727 : : RelationGetRelationName(indexRelation));
5728 : :
5942 tgl@sss.pgh.pa.us 5729 :CBC 346 : arr = DatumGetArrayTypeP(val); /* ensure not toasted */
5730 : 346 : nelem = ARR_DIMS(arr)[0];
5731 [ + - + - ]: 346 : if (ARR_NDIM(arr) != 1 ||
2899 teodor@sigaev.ru 5732 : 346 : nelem != indnkeyatts ||
5942 tgl@sss.pgh.pa.us 5733 [ + - ]: 346 : ARR_HASNULL(arr) ||
5734 [ - + ]: 346 : ARR_ELEMTYPE(arr) != OIDOID)
5942 tgl@sss.pgh.pa.us 5735 [ # # ]:UBC 0 : elog(ERROR, "conexclop is not a 1-D Oid array");
5736 : :
2899 teodor@sigaev.ru 5737 [ - + ]:CBC 346 : memcpy(ops, ARR_DATA_PTR(arr), sizeof(Oid) * indnkeyatts);
5738 : : }
5739 : :
5942 tgl@sss.pgh.pa.us 5740 : 346 : systable_endscan(conscan);
2610 andres@anarazel.de 5741 : 346 : table_close(conrel, AccessShareLock);
5742 : :
5942 tgl@sss.pgh.pa.us 5743 [ - + ]: 346 : if (!found)
5942 tgl@sss.pgh.pa.us 5744 [ # # ]:UBC 0 : elog(ERROR, "exclusion constraint record missing for rel %s",
5745 : : RelationGetRelationName(indexRelation));
5746 : :
5747 : : /* We need the func OIDs and strategy numbers too */
2899 teodor@sigaev.ru 5748 [ + + ]:CBC 995 : for (i = 0; i < indnkeyatts; i++)
5749 : : {
5942 tgl@sss.pgh.pa.us 5750 : 649 : funcs[i] = get_opcode(ops[i]);
5751 : 1298 : strats[i] = get_op_opfamily_strategy(ops[i],
5752 : 649 : indexRelation->rd_opfamily[i]);
5753 : : /* shouldn't fail, since it was checked at index creation */
5754 [ - + ]: 649 : if (strats[i] == InvalidStrategy)
5942 tgl@sss.pgh.pa.us 5755 [ # # ]:UBC 0 : elog(ERROR, "could not find strategy for operator %u in family %u",
5756 : : ops[i], indexRelation->rd_opfamily[i]);
5757 : : }
5758 : :
5759 : : /* Save a copy of the results in the relcache entry. */
5942 tgl@sss.pgh.pa.us 5760 :CBC 346 : oldcxt = MemoryContextSwitchTo(indexRelation->rd_indexcxt);
95 michael@paquier.xyz 5761 :GNC 346 : indexRelation->rd_exclops = palloc_array(Oid, indnkeyatts);
5762 : 346 : indexRelation->rd_exclprocs = palloc_array(Oid, indnkeyatts);
5763 : 346 : indexRelation->rd_exclstrats = palloc_array(uint16, indnkeyatts);
2899 teodor@sigaev.ru 5764 :CBC 346 : memcpy(indexRelation->rd_exclops, ops, sizeof(Oid) * indnkeyatts);
5765 : 346 : memcpy(indexRelation->rd_exclprocs, funcs, sizeof(Oid) * indnkeyatts);
5766 : 346 : memcpy(indexRelation->rd_exclstrats, strats, sizeof(uint16) * indnkeyatts);
5942 tgl@sss.pgh.pa.us 5767 : 346 : MemoryContextSwitchTo(oldcxt);
5768 : : }
5769 : :
5770 : : /*
5771 : : * Get the publication information for the given relation.
5772 : : *
5773 : : * Traverse all the publications which the relation is in to get the
5774 : : * publication actions and validate:
5775 : : * 1. The row filter expressions for such publications if any. We consider the
5776 : : * row filter expression as invalid if it references any column which is not
5777 : : * part of REPLICA IDENTITY.
5778 : : * 2. The column list for such publication if any. We consider the column list
5779 : : * invalid if REPLICA IDENTITY contains any column that is not part of it.
5780 : : * 3. The generated columns of the relation for such publications. We consider
5781 : : * any reference of an unpublished generated column in REPLICA IDENTITY as
5782 : : * invalid.
5783 : : *
5784 : : * To avoid fetching the publication information repeatedly, we cache the
5785 : : * publication actions, row filter validation information, column list
5786 : : * validation information, and generated column validation information.
5787 : : */
5788 : : void
1482 akapila@postgresql.o 5789 : 88361 : RelationBuildPublicationDesc(Relation relation, PublicationDesc *pubdesc)
5790 : : {
11 akapila@postgresql.o 5791 :GNC 88361 : List *puboids = NIL;
5792 : 88361 : List *exceptpuboids = NIL;
5793 : : List *alltablespuboids;
5794 : : ListCell *lc;
5795 : : MemoryContext oldcxt;
5796 : : Oid schemaid;
1482 akapila@postgresql.o 5797 :CBC 88361 : List *ancestors = NIL;
5798 : 88361 : Oid relid = RelationGetRelid(relation);
5799 : :
5800 : : /*
5801 : : * If not publishable, it publishes no actions. (pgoutput_change() will
5802 : : * ignore it.)
5803 : : */
2525 peter@eisentraut.org 5804 [ + + ]: 88361 : if (!is_publishable_relation(relation))
5805 : : {
1482 akapila@postgresql.o 5806 : 3062 : memset(pubdesc, 0, sizeof(PublicationDesc));
5807 : 3062 : pubdesc->rf_valid_for_update = true;
5808 : 3062 : pubdesc->rf_valid_for_delete = true;
1450 tomas.vondra@postgre 5809 : 3062 : pubdesc->cols_valid_for_update = true;
5810 : 3062 : pubdesc->cols_valid_for_delete = true;
466 akapila@postgresql.o 5811 : 3062 : pubdesc->gencols_valid_for_update = true;
5812 : 3062 : pubdesc->gencols_valid_for_delete = true;
1482 5813 : 3062 : return;
5814 : : }
5815 : :
5816 [ + + ]: 85299 : if (relation->rd_pubdesc)
5817 : : {
5818 : 80649 : memcpy(pubdesc, relation->rd_pubdesc, sizeof(PublicationDesc));
5819 : 80649 : return;
5820 : : }
5821 : :
5822 : 4650 : memset(pubdesc, 0, sizeof(PublicationDesc));
5823 : 4650 : pubdesc->rf_valid_for_update = true;
5824 : 4650 : pubdesc->rf_valid_for_delete = true;
1450 tomas.vondra@postgre 5825 : 4650 : pubdesc->cols_valid_for_update = true;
5826 : 4650 : pubdesc->cols_valid_for_delete = true;
466 akapila@postgresql.o 5827 : 4650 : pubdesc->gencols_valid_for_update = true;
5828 : 4650 : pubdesc->gencols_valid_for_delete = true;
5829 : :
5830 : : /* Fetch the publication membership info. */
11 akapila@postgresql.o 5831 :GNC 4650 : puboids = GetRelationIncludedPublications(relid);
1600 akapila@postgresql.o 5832 :CBC 4650 : schemaid = RelationGetNamespace(relation);
1438 tomas.vondra@postgre 5833 : 4650 : puboids = list_concat_unique_oid(puboids, GetSchemaPublications(schemaid));
5834 : :
2167 peter@eisentraut.org 5835 [ + + ]: 4650 : if (relation->rd_rel->relispartition)
5836 : : {
5837 : : Oid last_ancestor_relid;
5838 : :
5839 : : /* Add publications that the ancestors are in too. */
1482 akapila@postgresql.o 5840 : 1171 : ancestors = get_partition_ancestors(relid);
11 akapila@postgresql.o 5841 :GNC 1171 : last_ancestor_relid = llast_oid(ancestors);
5842 : :
2167 peter@eisentraut.org 5843 [ + - + + :CBC 2694 : foreach(lc, ancestors)
+ + ]
5844 : : {
2131 tgl@sss.pgh.pa.us 5845 : 1523 : Oid ancestor = lfirst_oid(lc);
5846 : :
2167 peter@eisentraut.org 5847 : 1523 : puboids = list_concat_unique_oid(puboids,
11 akapila@postgresql.o 5848 :GNC 1523 : GetRelationIncludedPublications(ancestor));
1600 akapila@postgresql.o 5849 :CBC 1523 : schemaid = get_rel_namespace(ancestor);
5850 : 1523 : puboids = list_concat_unique_oid(puboids,
1438 tomas.vondra@postgre 5851 : 1523 : GetSchemaPublications(schemaid));
5852 : : }
5853 : :
5854 : : /*
5855 : : * Only the top-most ancestor can appear in the EXCEPT clause.
5856 : : * Therefore, for a partition, exclusion must be evaluated at the
5857 : : * top-most ancestor.
5858 : : */
11 akapila@postgresql.o 5859 :GNC 1171 : exceptpuboids = GetRelationExcludedPublications(last_ancestor_relid);
5860 : : }
5861 : : else
5862 : : {
5863 : : /*
5864 : : * For a regular table or a root partitioned table, check exclusion on
5865 : : * table itself.
5866 : : */
5867 : 3479 : exceptpuboids = GetRelationExcludedPublications(relid);
5868 : : }
5869 : :
5870 : 4650 : alltablespuboids = GetAllTablesPublications();
5871 : 4650 : puboids = list_concat_unique_oid(puboids,
5872 : 4650 : list_difference_oid(alltablespuboids,
5873 : : exceptpuboids));
3342 peter_e@gmx.net 5874 [ + + + + :CBC 5029 : foreach(lc, puboids)
+ + ]
5875 : : {
5876 : 475 : Oid pubid = lfirst_oid(lc);
5877 : : HeapTuple tup;
5878 : : Form_pg_publication pubform;
5879 : : bool invalid_column_list;
5880 : : bool invalid_gen_col;
5881 : :
5882 : 475 : tup = SearchSysCache1(PUBLICATIONOID, ObjectIdGetDatum(pubid));
5883 : :
5884 [ - + ]: 475 : if (!HeapTupleIsValid(tup))
3342 peter_e@gmx.net 5885 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for publication %u", pubid);
5886 : :
3342 peter_e@gmx.net 5887 :CBC 475 : pubform = (Form_pg_publication) GETSTRUCT(tup);
5888 : :
1482 akapila@postgresql.o 5889 : 475 : pubdesc->pubactions.pubinsert |= pubform->pubinsert;
5890 : 475 : pubdesc->pubactions.pubupdate |= pubform->pubupdate;
5891 : 475 : pubdesc->pubactions.pubdelete |= pubform->pubdelete;
5892 : 475 : pubdesc->pubactions.pubtruncate |= pubform->pubtruncate;
5893 : :
5894 : : /*
5895 : : * Check if all columns referenced in the filter expression are part
5896 : : * of the REPLICA IDENTITY index or not.
5897 : : *
5898 : : * If the publication is FOR ALL TABLES then it means the table has no
5899 : : * row filters and we can skip the validation.
5900 : : */
5901 [ + + ]: 475 : if (!pubform->puballtables &&
5902 [ + + + + : 732 : (pubform->pubupdate || pubform->pubdelete) &&
+ + ]
1450 tomas.vondra@postgre 5903 : 365 : pub_rf_contains_invalid_column(pubid, relation, ancestors,
1403 tgl@sss.pgh.pa.us 5904 : 365 : pubform->pubviaroot))
5905 : : {
1482 akapila@postgresql.o 5906 [ + - ]: 30 : if (pubform->pubupdate)
5907 : 30 : pubdesc->rf_valid_for_update = false;
5908 [ + - ]: 30 : if (pubform->pubdelete)
5909 : 30 : pubdesc->rf_valid_for_delete = false;
5910 : : }
5911 : :
5912 : : /*
5913 : : * Check if all columns are part of the REPLICA IDENTITY index or not.
5914 : : *
5915 : : * Check if all generated columns included in the REPLICA IDENTITY are
5916 : : * published.
5917 : : */
466 5918 [ + + + + : 948 : if ((pubform->pubupdate || pubform->pubdelete) &&
+ + ]
5919 : 473 : pub_contains_invalid_column(pubid, relation, ancestors,
5920 : 473 : pubform->pubviaroot,
411 5921 : 473 : pubform->pubgencols,
5922 : : &invalid_column_list,
5923 : : &invalid_gen_col))
5924 : : {
1450 tomas.vondra@postgre 5925 [ + - ]: 66 : if (pubform->pubupdate)
5926 : : {
466 akapila@postgresql.o 5927 : 66 : pubdesc->cols_valid_for_update = !invalid_column_list;
5928 : 66 : pubdesc->gencols_valid_for_update = !invalid_gen_col;
5929 : : }
5930 : :
1450 tomas.vondra@postgre 5931 [ + - ]: 66 : if (pubform->pubdelete)
5932 : : {
466 akapila@postgresql.o 5933 : 66 : pubdesc->cols_valid_for_delete = !invalid_column_list;
5934 : 66 : pubdesc->gencols_valid_for_delete = !invalid_gen_col;
5935 : : }
5936 : : }
5937 : :
3342 peter_e@gmx.net 5938 : 475 : ReleaseSysCache(tup);
5939 : :
5940 : : /*
5941 : : * If we know everything is replicated and the row filter is invalid
5942 : : * for update and delete, there is no point to check for other
5943 : : * publications.
5944 : : */
1482 akapila@postgresql.o 5945 [ + - + + ]: 475 : if (pubdesc->pubactions.pubinsert && pubdesc->pubactions.pubupdate &&
5946 [ + - + + ]: 472 : pubdesc->pubactions.pubdelete && pubdesc->pubactions.pubtruncate &&
5947 [ + + + - ]: 466 : !pubdesc->rf_valid_for_update && !pubdesc->rf_valid_for_delete)
3342 peter_e@gmx.net 5948 : 96 : break;
5949 : :
5950 : : /*
5951 : : * If we know everything is replicated and the column list is invalid
5952 : : * for update and delete, there is no point to check for other
5953 : : * publications.
5954 : : */
1450 tomas.vondra@postgre 5955 [ + - + + ]: 445 : if (pubdesc->pubactions.pubinsert && pubdesc->pubactions.pubupdate &&
5956 [ + - + + ]: 442 : pubdesc->pubactions.pubdelete && pubdesc->pubactions.pubtruncate &&
5957 [ + + + - ]: 436 : !pubdesc->cols_valid_for_update && !pubdesc->cols_valid_for_delete)
5958 : 54 : break;
5959 : :
5960 : : /*
5961 : : * If we know everything is replicated and replica identity has an
5962 : : * unpublished generated column, there is no point to check for other
5963 : : * publications.
5964 : : */
466 akapila@postgresql.o 5965 [ + - + + ]: 391 : if (pubdesc->pubactions.pubinsert && pubdesc->pubactions.pubupdate &&
5966 [ + - + + ]: 388 : pubdesc->pubactions.pubdelete && pubdesc->pubactions.pubtruncate &&
5967 [ + + ]: 382 : !pubdesc->gencols_valid_for_update &&
5968 [ + - ]: 12 : !pubdesc->gencols_valid_for_delete)
5969 : 12 : break;
5970 : : }
5971 : :
1482 5972 [ - + ]: 4650 : if (relation->rd_pubdesc)
5973 : : {
1482 akapila@postgresql.o 5974 :UBC 0 : pfree(relation->rd_pubdesc);
5975 : 0 : relation->rd_pubdesc = NULL;
5976 : : }
5977 : :
5978 : : /* Now save copy of the descriptor in the relcache entry. */
3342 peter_e@gmx.net 5979 :CBC 4650 : oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
95 michael@paquier.xyz 5980 :GNC 4650 : relation->rd_pubdesc = palloc_object(PublicationDesc);
1482 akapila@postgresql.o 5981 :CBC 4650 : memcpy(relation->rd_pubdesc, pubdesc, sizeof(PublicationDesc));
3342 peter_e@gmx.net 5982 : 4650 : MemoryContextSwitchTo(oldcxt);
5983 : : }
5984 : :
5985 : : static bytea **
2176 akorotkov@postgresql 5986 : 720141 : CopyIndexAttOptions(bytea **srcopts, int natts)
5987 : : {
95 michael@paquier.xyz 5988 :GNC 720141 : bytea **opts = palloc_array(bytea *, natts);
5989 : :
2176 akorotkov@postgresql 5990 [ + + ]:CBC 2003299 : for (int i = 0; i < natts; i++)
5991 : : {
5992 : 1283158 : bytea *opt = srcopts[i];
5993 : :
5994 [ + + ]: 1329252 : opts[i] = !opt ? NULL : (bytea *)
5995 : 46094 : DatumGetPointer(datumCopy(PointerGetDatum(opt), false, -1));
5996 : : }
5997 : :
5998 : 720141 : return opts;
5999 : : }
6000 : :
6001 : : /*
6002 : : * RelationGetIndexAttOptions
6003 : : * get AM/opclass-specific options for an index parsed into a binary form
6004 : : */
6005 : : bytea **
6006 : 1341074 : RelationGetIndexAttOptions(Relation relation, bool copy)
6007 : : {
6008 : : MemoryContext oldcxt;
6009 : 1341074 : bytea **opts = relation->rd_opcoptions;
6010 : 1341074 : Oid relid = RelationGetRelid(relation);
2131 tgl@sss.pgh.pa.us 6011 : 1341074 : int natts = RelationGetNumberOfAttributes(relation); /* XXX
6012 : : * IndexRelationGetNumberOfKeyAttributes */
6013 : : int i;
6014 : :
6015 : : /* Try to copy cached options. */
2176 akorotkov@postgresql 6016 [ + + ]: 1341074 : if (opts)
6017 [ + + ]: 1045312 : return copy ? CopyIndexAttOptions(opts, natts) : opts;
6018 : :
6019 : : /* Get and parse opclass options. */
95 michael@paquier.xyz 6020 :GNC 295762 : opts = palloc0_array(bytea *, natts);
6021 : :
2176 akorotkov@postgresql 6022 [ + + ]:CBC 796968 : for (i = 0; i < natts; i++)
6023 : : {
6024 [ + + + - ]: 501209 : if (criticalRelcachesBuilt && relid != AttributeRelidNumIndexId)
6025 : : {
6026 : 466354 : Datum attoptions = get_attoptions(relid, i + 1);
6027 : :
6028 : 466354 : opts[i] = index_opclass_options(relation, i + 1, attoptions, false);
6029 : :
6030 [ + + ]: 466351 : if (attoptions != (Datum) 0)
6031 : 146 : pfree(DatumGetPointer(attoptions));
6032 : : }
6033 : : }
6034 : :
6035 : : /* Copy parsed options to the cache. */
6036 : 295759 : oldcxt = MemoryContextSwitchTo(relation->rd_indexcxt);
6037 : 295759 : relation->rd_opcoptions = CopyIndexAttOptions(opts, natts);
6038 : 295759 : MemoryContextSwitchTo(oldcxt);
6039 : :
6040 [ - + ]: 295759 : if (copy)
2176 akorotkov@postgresql 6041 :UBC 0 : return opts;
6042 : :
2176 akorotkov@postgresql 6043 [ + + ]:CBC 796965 : for (i = 0; i < natts; i++)
6044 : : {
6045 [ + + ]: 501206 : if (opts[i])
6046 : 1096 : pfree(opts[i]);
6047 : : }
6048 : :
6049 : 295759 : pfree(opts);
6050 : :
6051 : 295759 : return relation->rd_opcoptions;
6052 : : }
6053 : :
6054 : : /*
6055 : : * Routines to support ereport() reports of relation-related errors
6056 : : *
6057 : : * These could have been put into elog.c, but it seems like a module layering
6058 : : * violation to have elog.c calling relcache or syscache stuff --- and we
6059 : : * definitely don't want elog.h including rel.h. So we put them here.
6060 : : */
6061 : :
6062 : : /*
6063 : : * errtable --- stores schema_name and table_name of a table
6064 : : * within the current errordata.
6065 : : */
6066 : : int
4793 tgl@sss.pgh.pa.us 6067 : 1858 : errtable(Relation rel)
6068 : : {
6069 : 1858 : err_generic_string(PG_DIAG_SCHEMA_NAME,
6070 : 1858 : get_namespace_name(RelationGetNamespace(rel)));
6071 : 1858 : err_generic_string(PG_DIAG_TABLE_NAME, RelationGetRelationName(rel));
6072 : :
4673 bruce@momjian.us 6073 : 1858 : return 0; /* return value does not matter */
6074 : : }
6075 : :
6076 : : /*
6077 : : * errtablecol --- stores schema_name, table_name and column_name
6078 : : * of a table column within the current errordata.
6079 : : *
6080 : : * The column is specified by attribute number --- for most callers, this is
6081 : : * easier and less error-prone than getting the column name for themselves.
6082 : : */
6083 : : int
4793 tgl@sss.pgh.pa.us 6084 : 274 : errtablecol(Relation rel, int attnum)
6085 : : {
6086 : 274 : TupleDesc reldesc = RelationGetDescr(rel);
6087 : : const char *colname;
6088 : :
6089 : : /* Use reldesc if it's a user attribute, else consult the catalogs */
6090 [ + - + - ]: 274 : if (attnum > 0 && attnum <= reldesc->natts)
3129 andres@anarazel.de 6091 : 274 : colname = NameStr(TupleDescAttr(reldesc, attnum - 1)->attname);
6092 : : else
2953 alvherre@alvh.no-ip. 6093 :UBC 0 : colname = get_attname(RelationGetRelid(rel), attnum, false);
6094 : :
4793 tgl@sss.pgh.pa.us 6095 :CBC 274 : return errtablecolname(rel, colname);
6096 : : }
6097 : :
6098 : : /*
6099 : : * errtablecolname --- stores schema_name, table_name and column_name
6100 : : * of a table column within the current errordata, where the column name is
6101 : : * given directly rather than extracted from the relation's catalog data.
6102 : : *
6103 : : * Don't use this directly unless errtablecol() is inconvenient for some
6104 : : * reason. This might possibly be needed during intermediate states in ALTER
6105 : : * TABLE, for instance.
6106 : : */
6107 : : int
6108 : 274 : errtablecolname(Relation rel, const char *colname)
6109 : : {
6110 : 274 : errtable(rel);
6111 : 274 : err_generic_string(PG_DIAG_COLUMN_NAME, colname);
6112 : :
4673 bruce@momjian.us 6113 : 274 : return 0; /* return value does not matter */
6114 : : }
6115 : :
6116 : : /*
6117 : : * errtableconstraint --- stores schema_name, table_name and constraint_name
6118 : : * of a table-related constraint within the current errordata.
6119 : : */
6120 : : int
4793 tgl@sss.pgh.pa.us 6121 : 1328 : errtableconstraint(Relation rel, const char *conname)
6122 : : {
6123 : 1328 : errtable(rel);
6124 : 1328 : err_generic_string(PG_DIAG_CONSTRAINT_NAME, conname);
6125 : :
4673 bruce@momjian.us 6126 : 1328 : return 0; /* return value does not matter */
6127 : : }
6128 : :
6129 : :
6130 : : /*
6131 : : * load_relcache_init_file, write_relcache_init_file
6132 : : *
6133 : : * In late 1992, we started regularly having databases with more than
6134 : : * a thousand classes in them. With this number of classes, it became
6135 : : * critical to do indexed lookups on the system catalogs.
6136 : : *
6137 : : * Bootstrapping these lookups is very hard. We want to be able to
6138 : : * use an index on pg_attribute, for example, but in order to do so,
6139 : : * we must have read pg_attribute for the attributes in the index,
6140 : : * which implies that we need to use the index.
6141 : : *
6142 : : * In order to get around the problem, we do the following:
6143 : : *
6144 : : * + When the database system is initialized (at initdb time), we
6145 : : * don't use indexes. We do sequential scans.
6146 : : *
6147 : : * + When the backend is started up in normal mode, we load an image
6148 : : * of the appropriate relation descriptors, in internal format,
6149 : : * from an initialization file in the data/base/... directory.
6150 : : *
6151 : : * + If the initialization file isn't there, then we create the
6152 : : * relation descriptors using sequential scans and write 'em to
6153 : : * the initialization file for use by subsequent backends.
6154 : : *
6155 : : * As of Postgres 9.0, there is one local initialization file in each
6156 : : * database, plus one shared initialization file for shared catalogs.
6157 : : *
6158 : : * We could dispense with the initialization files and just build the
6159 : : * critical reldescs the hard way on every backend startup, but that
6160 : : * slows down backend startup noticeably.
6161 : : *
6162 : : * We can in fact go further, and save more relcache entries than
6163 : : * just the ones that are absolutely critical; this allows us to speed
6164 : : * up backend startup by not having to build such entries the hard way.
6165 : : * Presently, all the catalog and index entries that are referred to
6166 : : * by catcaches are stored in the initialization files.
6167 : : *
6168 : : * The same mechanism that detects when catcache and relcache entries
6169 : : * need to be invalidated (due to catalog updates) also arranges to
6170 : : * unlink the initialization files when the contents may be out of date.
6171 : : * The files will then be rebuilt during the next backend startup.
6172 : : */
6173 : :
6174 : : /*
6175 : : * load_relcache_init_file -- attempt to load cache from the shared
6176 : : * or local cache init file
6177 : : *
6178 : : * If successful, return true and set criticalRelcachesBuilt or
6179 : : * criticalSharedRelcachesBuilt to true.
6180 : : * If not successful, return false.
6181 : : *
6182 : : * NOTE: we assume we are already switched into CacheMemoryContext.
6183 : : */
6184 : : static bool
6059 tgl@sss.pgh.pa.us 6185 : 32389 : load_relcache_init_file(bool shared)
6186 : : {
6187 : : FILE *fp;
6188 : : char initfilename[MAXPGPATH];
6189 : : Relation *rels;
6190 : : int relno,
6191 : : num_rels,
6192 : : max_rels,
6193 : : nailed_rels,
6194 : : nailed_indexes,
6195 : : magic;
6196 : : int i;
6197 : :
6198 [ + + ]: 32389 : if (shared)
6199 : 16986 : snprintf(initfilename, sizeof(initfilename), "global/%s",
6200 : : RELCACHE_INIT_FILENAME);
6201 : : else
6202 : 15403 : snprintf(initfilename, sizeof(initfilename), "%s/%s",
6203 : : DatabasePath, RELCACHE_INIT_FILENAME);
6204 : :
8790 6205 : 32389 : fp = AllocateFile(initfilename, PG_BINARY_R);
6206 [ + + ]: 32389 : if (fp == NULL)
6207 : 3901 : return false;
6208 : :
6209 : : /*
6210 : : * Read the index relcache entries from the file. Note we will not enter
6211 : : * any of them into the cache if the read fails partway through; this
6212 : : * helps to guard against broken init files.
6213 : : */
6214 : 28488 : max_rels = 100;
6215 : 28488 : rels = (Relation *) palloc(max_rels * sizeof(Relation));
6216 : 28488 : num_rels = 0;
6217 : 28488 : nailed_rels = nailed_indexes = 0;
6218 : :
6219 : : /* check for correct magic number (compatible version) */
8162 6220 [ - + ]: 28488 : if (fread(&magic, 1, sizeof(magic), fp) != sizeof(magic))
8162 tgl@sss.pgh.pa.us 6221 :UBC 0 : goto read_failed;
8162 tgl@sss.pgh.pa.us 6222 [ - + ]:CBC 28488 : if (magic != RELCACHE_INIT_FILEMAGIC)
8162 tgl@sss.pgh.pa.us 6223 :UBC 0 : goto read_failed;
6224 : :
8593 bruce@momjian.us 6225 :CBC 28488 : for (relno = 0;; relno++)
10416 6226 : 1909950 : {
6227 : : Size len;
6228 : : size_t nread;
6229 : : Relation rel;
6230 : : Form_pg_class relform;
6231 : : bool has_not_null;
6232 : :
6233 : : /* first read the relation descriptor length */
6041 tgl@sss.pgh.pa.us 6234 : 1938438 : nread = fread(&len, 1, sizeof(len), fp);
6235 [ + + ]: 1938438 : if (nread != sizeof(len))
6236 : : {
8790 6237 [ + - ]: 28488 : if (nread == 0)
6238 : 28488 : break; /* end of file */
8824 tgl@sss.pgh.pa.us 6239 :UBC 0 : goto read_failed;
6240 : : }
6241 : :
6242 : : /* safety check for incompatible relcache layout */
8926 tgl@sss.pgh.pa.us 6243 [ - + ]:CBC 1909950 : if (len != sizeof(RelationData))
8824 tgl@sss.pgh.pa.us 6244 :UBC 0 : goto read_failed;
6245 : :
6246 : : /* allocate another relcache header */
8790 tgl@sss.pgh.pa.us 6247 [ + + ]:CBC 1909950 : if (num_rels >= max_rels)
6248 : : {
6249 : 13798 : max_rels *= 2;
6250 : 13798 : rels = (Relation *) repalloc(rels, max_rels * sizeof(Relation));
6251 : : }
6252 : :
6253 : 1909950 : rel = rels[num_rels++] = (Relation) palloc(len);
6254 : :
6255 : : /* then, read the Relation structure */
6041 6256 [ - + ]: 1909950 : if (fread(rel, 1, len, fp) != len)
8824 tgl@sss.pgh.pa.us 6257 :UBC 0 : goto read_failed;
6258 : :
6259 : : /* next read the relation tuple form */
6041 tgl@sss.pgh.pa.us 6260 [ - + ]:CBC 1909950 : if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
8824 tgl@sss.pgh.pa.us 6261 :UBC 0 : goto read_failed;
6262 : :
10416 bruce@momjian.us 6263 :CBC 1909950 : relform = (Form_pg_class) palloc(len);
6041 tgl@sss.pgh.pa.us 6264 [ - + ]: 1909950 : if (fread(relform, 1, len, fp) != len)
8824 tgl@sss.pgh.pa.us 6265 :UBC 0 : goto read_failed;
6266 : :
8790 tgl@sss.pgh.pa.us 6267 :CBC 1909950 : rel->rd_rel = relform;
6268 : :
6269 : : /* initialize attribute tuple forms */
2672 andres@anarazel.de 6270 : 1909950 : rel->rd_att = CreateTemplateTupleDesc(relform->relnatts);
7212 tgl@sss.pgh.pa.us 6271 : 1909950 : rel->rd_att->tdrefcount = 1; /* mark as refcounted */
6272 : :
2077 6273 [ + + ]: 1909950 : rel->rd_att->tdtypeid = relform->reltype ? relform->reltype : RECORDOID;
6274 : 1909950 : rel->rd_att->tdtypmod = -1; /* just to be sure */
6275 : :
6276 : : /* next read all the attribute tuple form data entries */
8521 6277 : 1909950 : has_not_null = false;
10416 bruce@momjian.us 6278 [ + + ]: 11377330 : for (i = 0; i < relform->relnatts; i++)
6279 : : {
3129 andres@anarazel.de 6280 : 9467380 : Form_pg_attribute attr = TupleDescAttr(rel->rd_att, i);
6281 : :
6041 tgl@sss.pgh.pa.us 6282 [ - + ]: 9467380 : if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
8824 tgl@sss.pgh.pa.us 6283 :UBC 0 : goto read_failed;
6261 tgl@sss.pgh.pa.us 6284 [ - + ]:CBC 9467380 : if (len != ATTRIBUTE_FIXED_PART_SIZE)
7678 tgl@sss.pgh.pa.us 6285 :UBC 0 : goto read_failed;
3129 andres@anarazel.de 6286 [ - + ]:CBC 9467380 : if (fread(attr, 1, len, fp) != len)
8824 tgl@sss.pgh.pa.us 6287 :UBC 0 : goto read_failed;
6288 : :
3129 andres@anarazel.de 6289 :CBC 9467380 : has_not_null |= attr->attnotnull;
6290 : :
450 drowley@postgresql.o 6291 : 9467380 : populate_compact_attribute(rel->rd_att, i);
6292 : : }
6293 : :
6294 : : /* next read the access method specific field */
6041 tgl@sss.pgh.pa.us 6295 [ - + ]: 1909950 : if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
7196 bruce@momjian.us 6296 :UBC 0 : goto read_failed;
7196 bruce@momjian.us 6297 [ - + ]:CBC 1909950 : if (len > 0)
6298 : : {
7196 bruce@momjian.us 6299 :UBC 0 : rel->rd_options = palloc(len);
6041 tgl@sss.pgh.pa.us 6300 [ # # ]: 0 : if (fread(rel->rd_options, 1, len, fp) != len)
7196 bruce@momjian.us 6301 : 0 : goto read_failed;
6956 tgl@sss.pgh.pa.us 6302 [ # # ]: 0 : if (len != VARSIZE(rel->rd_options))
3189 6303 : 0 : goto read_failed; /* sanity check */
6304 : : }
6305 : : else
6306 : : {
7196 bruce@momjian.us 6307 :CBC 1909950 : rel->rd_options = NULL;
6308 : : }
6309 : :
6310 : : /* mark not-null status */
8521 tgl@sss.pgh.pa.us 6311 [ + + ]: 1909950 : if (has_not_null)
6312 : : {
95 michael@paquier.xyz 6313 :GNC 710834 : TupleConstr *constr = palloc0_object(TupleConstr);
6314 : :
8521 tgl@sss.pgh.pa.us 6315 :CBC 710834 : constr->has_not_null = true;
6316 : 710834 : rel->rd_att->constr = constr;
6317 : : }
6318 : :
6319 : : /*
6320 : : * If it's an index, there's more to do. Note we explicitly ignore
6321 : : * partitioned indexes here.
6322 : : */
8790 6323 [ + + ]: 1909950 : if (rel->rd_rel->relkind == RELKIND_INDEX)
6324 : : {
6325 : : MemoryContext indexcxt;
6326 : : Oid *opfamily;
6327 : : Oid *opcintype;
6328 : : RegProcedure *support;
6329 : : int nsupport;
6330 : : int16 *indoption;
6331 : : Oid *indcollation;
6332 : :
6333 : : /* Count nailed indexes to ensure we have 'em all */
6334 [ + + ]: 1199116 : if (rel->rd_isnailed)
6335 : 184726 : nailed_indexes++;
6336 : :
6337 : : /* read the pg_index tuple */
6041 6338 [ - + ]: 1199116 : if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
8790 tgl@sss.pgh.pa.us 6339 :UBC 0 : goto read_failed;
6340 : :
8327 tgl@sss.pgh.pa.us 6341 :CBC 1199116 : rel->rd_indextuple = (HeapTuple) palloc(len);
6041 6342 [ - + ]: 1199116 : if (fread(rel->rd_indextuple, 1, len, fp) != len)
8790 tgl@sss.pgh.pa.us 6343 :UBC 0 : goto read_failed;
6344 : :
6345 : : /* Fix up internal pointers in the tuple -- see heap_copytuple */
8327 tgl@sss.pgh.pa.us 6346 :CBC 1199116 : rel->rd_indextuple->t_data = (HeapTupleHeader) ((char *) rel->rd_indextuple + HEAPTUPLESIZE);
6347 : 1199116 : rel->rd_index = (Form_pg_index) GETSTRUCT(rel->rd_indextuple);
6348 : :
6349 : : /*
6350 : : * prepare index info context --- parameters should match
6351 : : * RelationInitIndexAccessInfo
6352 : : */
2910 6353 : 1199116 : indexcxt = AllocSetContextCreate(CacheMemoryContext,
6354 : : "index info",
6355 : : ALLOCSET_SMALL_SIZES);
8790 6356 : 1199116 : rel->rd_indexcxt = indexcxt;
2900 peter_e@gmx.net 6357 : 1199116 : MemoryContextCopyAndSetIdentifier(indexcxt,
6358 : : RelationGetRelationName(rel));
6359 : :
6360 : : /*
6361 : : * Now we can fetch the index AM's API struct. (We can't store
6362 : : * that in the init file, since it contains function pointers that
6363 : : * might vary across server executions. Fortunately, it should be
6364 : : * safe to call the amhandler even while bootstrapping indexes.)
6365 : : */
3710 tgl@sss.pgh.pa.us 6366 : 1199116 : InitIndexAmRoutine(rel);
6367 : :
6368 : : /* read the vector of opfamily OIDs */
6041 6369 [ - + ]: 1199116 : if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
7022 tgl@sss.pgh.pa.us 6370 :UBC 0 : goto read_failed;
6371 : :
7022 tgl@sss.pgh.pa.us 6372 :CBC 1199116 : opfamily = (Oid *) MemoryContextAlloc(indexcxt, len);
6041 6373 [ - + ]: 1199116 : if (fread(opfamily, 1, len, fp) != len)
7022 tgl@sss.pgh.pa.us 6374 :UBC 0 : goto read_failed;
6375 : :
7022 tgl@sss.pgh.pa.us 6376 :CBC 1199116 : rel->rd_opfamily = opfamily;
6377 : :
6378 : : /* read the vector of opcintype OIDs */
6041 6379 [ - + ]: 1199116 : if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
7022 tgl@sss.pgh.pa.us 6380 :UBC 0 : goto read_failed;
6381 : :
7022 tgl@sss.pgh.pa.us 6382 :CBC 1199116 : opcintype = (Oid *) MemoryContextAlloc(indexcxt, len);
6041 6383 [ - + ]: 1199116 : if (fread(opcintype, 1, len, fp) != len)
7022 tgl@sss.pgh.pa.us 6384 :UBC 0 : goto read_failed;
6385 : :
7022 tgl@sss.pgh.pa.us 6386 :CBC 1199116 : rel->rd_opcintype = opcintype;
6387 : :
6388 : : /* read the vector of support procedure OIDs */
6041 6389 [ - + ]: 1199116 : if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
8790 tgl@sss.pgh.pa.us 6390 :UBC 0 : goto read_failed;
8790 tgl@sss.pgh.pa.us 6391 :CBC 1199116 : support = (RegProcedure *) MemoryContextAlloc(indexcxt, len);
6041 6392 [ - + ]: 1199116 : if (fread(support, 1, len, fp) != len)
8790 tgl@sss.pgh.pa.us 6393 :UBC 0 : goto read_failed;
6394 : :
8790 tgl@sss.pgh.pa.us 6395 :CBC 1199116 : rel->rd_support = support;
6396 : :
6397 : : /* read the vector of collation OIDs */
5514 peter_e@gmx.net 6398 [ - + ]: 1199116 : if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
5514 peter_e@gmx.net 6399 :UBC 0 : goto read_failed;
6400 : :
5514 peter_e@gmx.net 6401 :CBC 1199116 : indcollation = (Oid *) MemoryContextAlloc(indexcxt, len);
6402 [ - + ]: 1199116 : if (fread(indcollation, 1, len, fp) != len)
5514 peter_e@gmx.net 6403 :UBC 0 : goto read_failed;
6404 : :
5514 peter_e@gmx.net 6405 :CBC 1199116 : rel->rd_indcollation = indcollation;
6406 : :
6407 : : /* read the vector of indoption values */
6041 tgl@sss.pgh.pa.us 6408 [ - + ]: 1199116 : if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
7005 tgl@sss.pgh.pa.us 6409 :UBC 0 : goto read_failed;
6410 : :
7005 tgl@sss.pgh.pa.us 6411 :CBC 1199116 : indoption = (int16 *) MemoryContextAlloc(indexcxt, len);
6041 6412 [ - + ]: 1199116 : if (fread(indoption, 1, len, fp) != len)
7005 tgl@sss.pgh.pa.us 6413 :UBC 0 : goto read_failed;
6414 : :
7005 tgl@sss.pgh.pa.us 6415 :CBC 1199116 : rel->rd_indoption = indoption;
6416 : :
6417 : : /* read the vector of opcoptions values */
2176 akorotkov@postgresql 6418 : 1199116 : rel->rd_opcoptions = (bytea **)
6419 : 1199116 : MemoryContextAllocZero(indexcxt, sizeof(*rel->rd_opcoptions) * relform->relnatts);
6420 : :
6421 [ + + ]: 3163366 : for (i = 0; i < relform->relnatts; i++)
6422 : : {
6423 [ - + ]: 1964250 : if (fread(&len, 1, sizeof(len), fp) != sizeof(len))
2176 akorotkov@postgresql 6424 :UBC 0 : goto read_failed;
6425 : :
2176 akorotkov@postgresql 6426 [ - + ]:CBC 1964250 : if (len > 0)
6427 : : {
2176 akorotkov@postgresql 6428 :UBC 0 : rel->rd_opcoptions[i] = (bytea *) MemoryContextAlloc(indexcxt, len);
6429 [ # # ]: 0 : if (fread(rel->rd_opcoptions[i], 1, len, fp) != len)
6430 : 0 : goto read_failed;
6431 : : }
6432 : : }
6433 : :
6434 : : /* set up zeroed fmgr-info vector */
2176 akorotkov@postgresql 6435 :CBC 1199116 : nsupport = relform->relnatts * rel->rd_indam->amsupport;
8790 tgl@sss.pgh.pa.us 6436 : 1199116 : rel->rd_supportinfo = (FmgrInfo *)
8162 6437 : 1199116 : MemoryContextAllocZero(indexcxt, nsupport * sizeof(FmgrInfo));
6438 : : }
6439 : : else
6440 : : {
6441 : : /* Count nailed rels to ensure we have 'em all */
8790 6442 [ + + ]: 710834 : if (rel->rd_isnailed)
6443 : 128642 : nailed_rels++;
6444 : :
6445 : : /* Load table AM data */
1563 peter@eisentraut.org 6446 [ - + - - : 710834 : if (RELKIND_HAS_TABLE_AM(rel->rd_rel->relkind) || rel->rd_rel->relkind == RELKIND_SEQUENCE)
- - - - ]
2566 andres@anarazel.de 6447 : 710834 : RelationInitTableAccessMethod(rel);
6448 : :
8790 tgl@sss.pgh.pa.us 6449 [ - + ]: 710834 : Assert(rel->rd_index == NULL);
8327 6450 [ - + ]: 710834 : Assert(rel->rd_indextuple == NULL);
8790 6451 [ - + ]: 710834 : Assert(rel->rd_indexcxt == NULL);
2610 andres@anarazel.de 6452 [ - + ]: 710834 : Assert(rel->rd_indam == NULL);
7022 tgl@sss.pgh.pa.us 6453 [ - + ]: 710834 : Assert(rel->rd_opfamily == NULL);
6454 [ - + ]: 710834 : Assert(rel->rd_opcintype == NULL);
8790 6455 [ - + ]: 710834 : Assert(rel->rd_support == NULL);
6456 [ - + ]: 710834 : Assert(rel->rd_supportinfo == NULL);
7005 6457 [ - + ]: 710834 : Assert(rel->rd_indoption == NULL);
5514 peter_e@gmx.net 6458 [ - + ]: 710834 : Assert(rel->rd_indcollation == NULL);
2176 akorotkov@postgresql 6459 [ - + ]: 710834 : Assert(rel->rd_opcoptions == NULL);
6460 : : }
6461 : :
6462 : : /*
6463 : : * Rules and triggers are not saved (mainly because the internal
6464 : : * format is complex and subject to change). They must be rebuilt if
6465 : : * needed by RelationCacheInitializePhase3. This is not expected to
6466 : : * be a big performance hit since few system catalogs have such. Ditto
6467 : : * for RLS policy data, partition info, index expressions, predicates,
6468 : : * exclusion info, and FDW info.
6469 : : */
8790 tgl@sss.pgh.pa.us 6470 : 1909950 : rel->rd_rules = NULL;
6471 : 1909950 : rel->rd_rulescxt = NULL;
6472 : 1909950 : rel->trigdesc = NULL;
4139 sfrost@snowman.net 6473 : 1909950 : rel->rd_rsdesc = NULL;
3385 rhaas@postgresql.org 6474 : 1909950 : rel->rd_partkey = NULL;
2528 tgl@sss.pgh.pa.us 6475 : 1909950 : rel->rd_partkeycxt = NULL;
3385 rhaas@postgresql.org 6476 : 1909950 : rel->rd_partdesc = NULL;
1782 alvherre@alvh.no-ip. 6477 : 1909950 : rel->rd_partdesc_nodetached = NULL;
6478 : 1909950 : rel->rd_partdesc_nodetached_xmin = InvalidTransactionId;
2528 tgl@sss.pgh.pa.us 6479 : 1909950 : rel->rd_pdcxt = NULL;
1782 alvherre@alvh.no-ip. 6480 : 1909950 : rel->rd_pddcxt = NULL;
3385 rhaas@postgresql.org 6481 : 1909950 : rel->rd_partcheck = NIL;
2528 tgl@sss.pgh.pa.us 6482 : 1909950 : rel->rd_partcheckvalid = false;
6483 : 1909950 : rel->rd_partcheckcxt = NULL;
8327 6484 : 1909950 : rel->rd_indexprs = NIL;
6485 : 1909950 : rel->rd_indpred = NIL;
5942 6486 : 1909950 : rel->rd_exclops = NULL;
6487 : 1909950 : rel->rd_exclprocs = NULL;
6488 : 1909950 : rel->rd_exclstrats = NULL;
4757 6489 : 1909950 : rel->rd_fdwroutine = NULL;
6490 : :
6491 : : /*
6492 : : * Reset transient-state fields in the relcache entry
6493 : : */
8069 6494 : 1909950 : rel->rd_smgr = NULL;
8790 6495 [ + + ]: 1909950 : if (rel->rd_isnailed)
7911 6496 : 313368 : rel->rd_refcnt = 1;
6497 : : else
6498 : 1596582 : rel->rd_refcnt = 0;
2508 6499 : 1909950 : rel->rd_indexvalid = false;
8790 6500 : 1909950 : rel->rd_indexlist = NIL;
3342 peter_e@gmx.net 6501 : 1909950 : rel->rd_pkindex = InvalidOid;
4323 tgl@sss.pgh.pa.us 6502 : 1909950 : rel->rd_replidindex = InvalidOid;
1091 tomas.vondra@postgre 6503 : 1909950 : rel->rd_attrsvalid = false;
4323 tgl@sss.pgh.pa.us 6504 : 1909950 : rel->rd_keyattr = NULL;
3342 peter_e@gmx.net 6505 : 1909950 : rel->rd_pkattr = NULL;
4323 tgl@sss.pgh.pa.us 6506 : 1909950 : rel->rd_idattr = NULL;
1482 akapila@postgresql.o 6507 : 1909950 : rel->rd_pubdesc = NULL;
3278 alvherre@alvh.no-ip. 6508 : 1909950 : rel->rd_statvalid = false;
6509 : 1909950 : rel->rd_statlist = NIL;
2508 tgl@sss.pgh.pa.us 6510 : 1909950 : rel->rd_fkeyvalid = false;
6511 : 1909950 : rel->rd_fkeylist = NIL;
7785 6512 : 1909950 : rel->rd_createSubid = InvalidSubTransactionId;
1348 rhaas@postgresql.org 6513 : 1909950 : rel->rd_newRelfilelocatorSubid = InvalidSubTransactionId;
6514 : 1909950 : rel->rd_firstRelfilelocatorSubid = InvalidSubTransactionId;
2171 noah@leadboat.com 6515 : 1909950 : rel->rd_droppedSubid = InvalidSubTransactionId;
7264 tgl@sss.pgh.pa.us 6516 : 1909950 : rel->rd_amcache = NULL;
1354 peter@eisentraut.org 6517 : 1909950 : rel->pgstat_info = NULL;
6518 : :
6519 : : /*
6520 : : * Recompute lock and physical addressing info. This is needed in
6521 : : * case the pg_internal.init file was copied from some other database
6522 : : * by CREATE DATABASE.
6523 : : */
8790 tgl@sss.pgh.pa.us 6524 : 1909950 : RelationInitLockInfo(rel);
7940 6525 : 1909950 : RelationInitPhysicalAddr(rel);
6526 : : }
6527 : :
6528 : : /*
6529 : : * We reached the end of the init file without apparent problem. Did we
6530 : : * get the right number of nailed items? This is a useful crosscheck in
6531 : : * case the set of critical rels or indexes changes. However, that should
6532 : : * not happen in a normally-running system, so let's bleat if it does.
6533 : : *
6534 : : * For the shared init file, we're called before client authentication is
6535 : : * done, which means that elog(WARNING) will go only to the postmaster
6536 : : * log, where it's easily missed. To ensure that developers notice bad
6537 : : * values of NUM_CRITICAL_SHARED_RELS/NUM_CRITICAL_SHARED_INDEXES, we put
6538 : : * an Assert(false) there.
6539 : : */
6059 6540 [ + + ]: 28488 : if (shared)
6541 : : {
6542 [ + - - + ]: 14690 : if (nailed_rels != NUM_CRITICAL_SHARED_RELS ||
6543 : : nailed_indexes != NUM_CRITICAL_SHARED_INDEXES)
6544 : : {
3916 tgl@sss.pgh.pa.us 6545 [ # # ]:UBC 0 : elog(WARNING, "found %d nailed shared rels and %d nailed shared indexes in init file, but expected %d and %d respectively",
6546 : : nailed_rels, nailed_indexes,
6547 : : NUM_CRITICAL_SHARED_RELS, NUM_CRITICAL_SHARED_INDEXES);
6548 : : /* Make sure we get developers' attention about this */
3777 6549 : 0 : Assert(false);
6550 : : /* In production builds, recover by bootstrapping the relcache */
6551 : : goto read_failed;
6552 : : }
6553 : : }
6554 : : else
6555 : : {
6059 tgl@sss.pgh.pa.us 6556 [ + - - + ]:CBC 13798 : if (nailed_rels != NUM_CRITICAL_LOCAL_RELS ||
6557 : : nailed_indexes != NUM_CRITICAL_LOCAL_INDEXES)
6558 : : {
3916 tgl@sss.pgh.pa.us 6559 [ # # ]:UBC 0 : elog(WARNING, "found %d nailed rels and %d nailed indexes in init file, but expected %d and %d respectively",
6560 : : nailed_rels, nailed_indexes,
6561 : : NUM_CRITICAL_LOCAL_RELS, NUM_CRITICAL_LOCAL_INDEXES);
6562 : : /* We don't need an Assert() in this case */
6059 6563 : 0 : goto read_failed;
6564 : : }
6565 : : }
6566 : :
6567 : : /*
6568 : : * OK, all appears well.
6569 : : *
6570 : : * Now insert all the new relcache entries into the cache.
6571 : : */
8790 tgl@sss.pgh.pa.us 6572 [ + + ]:CBC 1938438 : for (relno = 0; relno < num_rels; relno++)
6573 : : {
4319 6574 [ - + ]: 1909950 : RelationCacheInsert(rels[relno], false);
6575 : : }
6576 : :
8790 6577 : 28488 : pfree(rels);
6578 : 28488 : FreeFile(fp);
6579 : :
6059 6580 [ + + ]: 28488 : if (shared)
6581 : 14690 : criticalSharedRelcachesBuilt = true;
6582 : : else
6583 : 13798 : criticalRelcachesBuilt = true;
8790 6584 : 28488 : return true;
6585 : :
6586 : : /*
6587 : : * init file is broken, so do it the hard way. We don't bother trying to
6588 : : * free the clutter we just allocated; it's not in the relcache so it
6589 : : * won't hurt.
6590 : : */
8824 tgl@sss.pgh.pa.us 6591 :UBC 0 : read_failed:
8790 6592 : 0 : pfree(rels);
6593 : 0 : FreeFile(fp);
6594 : :
6595 : 0 : return false;
6596 : : }
6597 : :
6598 : : /*
6599 : : * Write out a new initialization file with the current contents
6600 : : * of the relcache (either shared rels or local rels, as indicated).
6601 : : */
6602 : : static void
6059 tgl@sss.pgh.pa.us 6603 :CBC 3364 : write_relcache_init_file(bool shared)
6604 : : {
6605 : : FILE *fp;
6606 : : char tempfilename[MAXPGPATH];
6607 : : char finalfilename[MAXPGPATH];
6608 : : int magic;
6609 : : HASH_SEQ_STATUS status;
6610 : : RelIdCacheEnt *idhentry;
6611 : : int i;
6612 : :
6613 : : /*
6614 : : * If we have already received any relcache inval events, there's no
6615 : : * chance of succeeding so we may as well skip the whole thing.
6616 : : */
3934 6617 [ + + ]: 3364 : if (relcacheInvalsReceived != 0L)
6618 : 18 : return;
6619 : :
6620 : : /*
6621 : : * We must write a temporary file and rename it into place. Otherwise,
6622 : : * another backend starting at about the same time might crash trying to
6623 : : * read the partially-complete file.
6624 : : */
6059 6625 [ + + ]: 3346 : if (shared)
6626 : : {
6627 : 1673 : snprintf(tempfilename, sizeof(tempfilename), "global/%s.%d",
6628 : : RELCACHE_INIT_FILENAME, MyProcPid);
6629 : 1673 : snprintf(finalfilename, sizeof(finalfilename), "global/%s",
6630 : : RELCACHE_INIT_FILENAME);
6631 : : }
6632 : : else
6633 : : {
6634 : 1673 : snprintf(tempfilename, sizeof(tempfilename), "%s/%s.%d",
6635 : : DatabasePath, RELCACHE_INIT_FILENAME, MyProcPid);
6636 : 1673 : snprintf(finalfilename, sizeof(finalfilename), "%s/%s",
6637 : : DatabasePath, RELCACHE_INIT_FILENAME);
6638 : : }
6639 : :
8790 6640 : 3346 : unlink(tempfilename); /* in case it exists w/wrong permissions */
6641 : :
6642 : 3346 : fp = AllocateFile(tempfilename, PG_BINARY_W);
6643 [ - + ]: 3346 : if (fp == NULL)
6644 : : {
6645 : : /*
6646 : : * We used to consider this a fatal error, but we might as well
6647 : : * continue with backend startup ...
6648 : : */
8269 tgl@sss.pgh.pa.us 6649 [ # # ]:UBC 0 : ereport(WARNING,
6650 : : (errcode_for_file_access(),
6651 : : errmsg("could not create relation-cache initialization file \"%s\": %m",
6652 : : tempfilename),
6653 : : errdetail("Continuing anyway, but there's something wrong.")));
9400 6654 : 0 : return;
6655 : : }
6656 : :
6657 : : /*
6658 : : * Write a magic number to serve as a file version identifier. We can
6659 : : * change the magic number whenever the relcache layout changes.
6660 : : */
8162 tgl@sss.pgh.pa.us 6661 :CBC 3346 : magic = RELCACHE_INIT_FILEMAGIC;
6662 [ - + ]: 3346 : if (fwrite(&magic, 1, sizeof(magic), fp) != sizeof(magic))
711 dgustafsson@postgres 6663 [ # # ]:UBC 0 : ereport(FATAL,
6664 : : errcode_for_file_access(),
6665 : : errmsg_internal("could not write init file: %m"));
6666 : :
6667 : : /*
6668 : : * Write all the appropriate reldescs (in no particular order).
6669 : : */
8755 tgl@sss.pgh.pa.us 6670 :CBC 3346 : hash_seq_init(&status, RelationIdCache);
6671 : :
6672 [ + + ]: 461748 : while ((idhentry = (RelIdCacheEnt *) hash_seq_search(&status)) != NULL)
6673 : : {
6674 : 458402 : Relation rel = idhentry->reldesc;
8790 6675 : 458402 : Form_pg_class relform = rel->rd_rel;
6676 : :
6677 : : /* ignore if not correct group */
6059 6678 [ + + ]: 458402 : if (relform->relisshared != shared)
6679 : 229201 : continue;
6680 : :
6681 : : /*
6682 : : * Ignore if not supposed to be in init file. We can allow any shared
6683 : : * relation that's been loaded so far to be in the shared init file,
6684 : : * but unshared relations must be ones that should be in the local
6685 : : * file per RelationIdIsInInitFile. (Note: if you want to change the
6686 : : * criterion for rels to be kept in the init file, see also inval.c.
6687 : : * The reason for filtering here is to be sure that we don't put
6688 : : * anything into the local init file for which a relcache inval would
6689 : : * not cause invalidation of that init file.)
6690 : : */
3916 6691 [ + + - + ]: 229201 : if (!shared && !RelationIdIsInInitFile(RelationGetRelid(rel)))
6692 : : {
6693 : : /* Nailed rels had better get stored. */
3916 tgl@sss.pgh.pa.us 6694 [ # # ]:UBC 0 : Assert(!rel->rd_isnailed);
3934 6695 : 0 : continue;
6696 : : }
6697 : :
6698 : : /* first write the relcache entry proper */
7196 bruce@momjian.us 6699 :CBC 229201 : write_item(rel, sizeof(RelationData), fp);
6700 : :
6701 : : /* next write the relation tuple form */
6702 : 229201 : write_item(relform, CLASS_TUPLE_SIZE, fp);
6703 : :
6704 : : /* next, do all the attribute tuple form data entries */
10416 6705 [ + + ]: 1366841 : for (i = 0; i < relform->relnatts; i++)
6706 : : {
3129 andres@anarazel.de 6707 : 1137640 : write_item(TupleDescAttr(rel->rd_att, i),
6708 : : ATTRIBUTE_FIXED_PART_SIZE, fp);
6709 : : }
6710 : :
6711 : : /* next, do the access method specific field */
7196 bruce@momjian.us 6712 : 229201 : write_item(rel->rd_options,
6956 tgl@sss.pgh.pa.us 6713 [ - + ]: 229201 : (rel->rd_options ? VARSIZE(rel->rd_options) : 0),
6714 : : fp);
6715 : :
6716 : : /*
6717 : : * If it's an index, there's more to do. Note we explicitly ignore
6718 : : * partitioned indexes here.
6719 : : */
8790 6720 [ + + ]: 229201 : if (rel->rd_rel->relkind == RELKIND_INDEX)
6721 : : {
6722 : : /* write the pg_index tuple */
6723 : : /* we assume this was created by heap_copytuple! */
7196 bruce@momjian.us 6724 : 143878 : write_item(rel->rd_indextuple,
7195 tgl@sss.pgh.pa.us 6725 : 143878 : HEAPTUPLESIZE + rel->rd_indextuple->t_len,
6726 : : fp);
6727 : :
6728 : : /* write the vector of opfamily OIDs */
7022 6729 : 143878 : write_item(rel->rd_opfamily,
6730 : 143878 : relform->relnatts * sizeof(Oid),
6731 : : fp);
6732 : :
6733 : : /* write the vector of opcintype OIDs */
6734 : 143878 : write_item(rel->rd_opcintype,
6735 : 143878 : relform->relnatts * sizeof(Oid),
6736 : : fp);
6737 : :
6738 : : /* write the vector of support procedure OIDs */
7195 6739 : 143878 : write_item(rel->rd_support,
2176 akorotkov@postgresql 6740 : 143878 : relform->relnatts * (rel->rd_indam->amsupport * sizeof(RegProcedure)),
6741 : : fp);
6742 : :
6743 : : /* write the vector of collation OIDs */
5514 peter_e@gmx.net 6744 : 143878 : write_item(rel->rd_indcollation,
6745 : 143878 : relform->relnatts * sizeof(Oid),
6746 : : fp);
6747 : :
6748 : : /* write the vector of indoption values */
7005 tgl@sss.pgh.pa.us 6749 : 143878 : write_item(rel->rd_indoption,
6750 : 143878 : relform->relnatts * sizeof(int16),
6751 : : fp);
6752 : :
2176 akorotkov@postgresql 6753 [ - + ]: 143878 : Assert(rel->rd_opcoptions);
6754 : :
6755 : : /* write the vector of opcoptions values */
6756 [ + + ]: 379771 : for (i = 0; i < relform->relnatts; i++)
6757 : : {
6758 : 235893 : bytea *opt = rel->rd_opcoptions[i];
6759 : :
6760 [ - + ]: 235893 : write_item(opt, opt ? VARSIZE(opt) : 0, fp);
6761 : : }
6762 : : }
6763 : : }
6764 : :
8084 tgl@sss.pgh.pa.us 6765 [ - + ]: 3346 : if (FreeFile(fp))
711 dgustafsson@postgres 6766 [ # # ]:UBC 0 : ereport(FATAL,
6767 : : errcode_for_file_access(),
6768 : : errmsg_internal("could not write init file: %m"));
6769 : :
6770 : : /*
6771 : : * Now we have to check whether the data we've so painstakingly
6772 : : * accumulated is already obsolete due to someone else's just-committed
6773 : : * catalog changes. If so, we just delete the temp file and leave it to
6774 : : * the next backend to try again. (Our own relcache entries will be
6775 : : * updated by SI message processing, but we can't be sure whether what we
6776 : : * wrote out was up-to-date.)
6777 : : *
6778 : : * This mustn't run concurrently with the code that unlinks an init file
6779 : : * and sends SI messages, so grab a serialization lock for the duration.
6780 : : */
8790 tgl@sss.pgh.pa.us 6781 :CBC 3346 : LWLockAcquire(RelCacheInitLock, LW_EXCLUSIVE);
6782 : :
6783 : : /* Make sure we have seen all incoming SI messages */
6784 : 3346 : AcceptInvalidationMessages();
6785 : :
6786 : : /*
6787 : : * If we have received any SI relcache invals since backend start, assume
6788 : : * we may have written out-of-date data.
6789 : : */
6790 [ + - ]: 3346 : if (relcacheInvalsReceived == 0L)
6791 : : {
6792 : : /*
6793 : : * OK, rename the temp file to its final name, deleting any
6794 : : * previously-existing init file.
6795 : : *
6796 : : * Note: a failure here is possible under Cygwin, if some other
6797 : : * backend is holding open an unlinked-but-not-yet-gone init file. So
6798 : : * treat this as a noncritical failure; just remove the useless temp
6799 : : * file on failure.
6800 : : */
7763 6801 [ - + ]: 3346 : if (rename(tempfilename, finalfilename) < 0)
7763 tgl@sss.pgh.pa.us 6802 :UBC 0 : unlink(tempfilename);
6803 : : }
6804 : : else
6805 : : {
6806 : : /* Delete the already-obsolete temp file */
8825 tgl@sss.pgh.pa.us 6807 :LBC (1) : unlink(tempfilename);
6808 : : }
6809 : :
7763 tgl@sss.pgh.pa.us 6810 :CBC 3346 : LWLockRelease(RelCacheInitLock);
6811 : : }
6812 : :
6813 : : /* write a chunk of data preceded by its length */
6814 : : static void
7195 6815 : 2924404 : write_item(const void *data, Size len, FILE *fp)
6816 : : {
6817 [ - + ]: 2924404 : if (fwrite(&len, 1, sizeof(len), fp) != sizeof(len))
711 dgustafsson@postgres 6818 [ # # ]:UBC 0 : ereport(FATAL,
6819 : : errcode_for_file_access(),
6820 : : errmsg_internal("could not write init file: %m"));
1453 andres@anarazel.de 6821 [ + + - + ]:CBC 2924404 : if (len > 0 && fwrite(data, 1, len, fp) != len)
711 dgustafsson@postgres 6822 [ # # ]:UBC 0 : ereport(FATAL,
6823 : : errcode_for_file_access(),
6824 : : errmsg_internal("could not write init file: %m"));
7195 tgl@sss.pgh.pa.us 6825 :CBC 2924404 : }
6826 : :
6827 : : /*
6828 : : * Determine whether a given relation (identified by OID) is one of the ones
6829 : : * we should store in a relcache init file.
6830 : : *
6831 : : * We must cache all nailed rels, and for efficiency we should cache every rel
6832 : : * that supports a syscache. The former set is almost but not quite a subset
6833 : : * of the latter. The special cases are relations where
6834 : : * RelationCacheInitializePhase2/3 chooses to nail for efficiency reasons, but
6835 : : * which do not support any syscache.
6836 : : */
6837 : : bool
3916 6838 : 1252068 : RelationIdIsInInitFile(Oid relationId)
6839 : : {
2833 andres@anarazel.de 6840 [ + + + + ]: 1252068 : if (relationId == SharedSecLabelRelationId ||
6841 [ + + ]: 1249387 : relationId == TriggerRelidNameIndexId ||
6842 [ + + ]: 1249230 : relationId == DatabaseNameIndexId ||
6843 : : relationId == SharedSecLabelObjectIndexId)
6844 : : {
6845 : : /*
6846 : : * If this Assert fails, we don't need the applicable special case
6847 : : * anymore.
6848 : : */
3916 tgl@sss.pgh.pa.us 6849 [ - + ]: 3003 : Assert(!RelationSupportsSysCache(relationId));
6850 : 3003 : return true;
6851 : : }
6852 : 1249065 : return RelationSupportsSysCache(relationId);
6853 : : }
6854 : :
6855 : : /*
6856 : : * Invalidate (remove) the init file during commit of a transaction that
6857 : : * changed one or more of the relation cache entries that are kept in the
6858 : : * local init file.
6859 : : *
6860 : : * To be safe against concurrent inspection or rewriting of the init file,
6861 : : * we must take RelCacheInitLock, then remove the old init file, then send
6862 : : * the SI messages that include relcache inval for such relations, and then
6863 : : * release RelCacheInitLock. This serializes the whole affair against
6864 : : * write_relcache_init_file, so that we can be sure that any other process
6865 : : * that's concurrently trying to create a new init file won't move an
6866 : : * already-stale version into place after we unlink. Also, because we unlink
6867 : : * before sending the SI messages, a backend that's currently starting cannot
6868 : : * read the now-obsolete init file and then miss the SI messages that will
6869 : : * force it to update its relcache entries. (This works because the backend
6870 : : * startup sequence gets into the sinval array before trying to load the init
6871 : : * file.)
6872 : : *
6873 : : * We take the lock and do the unlink in RelationCacheInitFilePreInvalidate,
6874 : : * then release the lock in RelationCacheInitFilePostInvalidate. Caller must
6875 : : * send any pending SI messages between those calls.
6876 : : */
6877 : : void
5325 6878 : 20707 : RelationCacheInitFilePreInvalidate(void)
6879 : : {
6880 : : char localinitfname[MAXPGPATH];
6881 : : char sharedinitfname[MAXPGPATH];
6882 : :
2833 andres@anarazel.de 6883 [ + - ]: 20707 : if (DatabasePath)
6884 : 20707 : snprintf(localinitfname, sizeof(localinitfname), "%s/%s",
6885 : : DatabasePath, RELCACHE_INIT_FILENAME);
6886 : 20707 : snprintf(sharedinitfname, sizeof(sharedinitfname), "global/%s",
6887 : : RELCACHE_INIT_FILENAME);
6888 : :
5325 tgl@sss.pgh.pa.us 6889 : 20707 : LWLockAcquire(RelCacheInitLock, LW_EXCLUSIVE);
6890 : :
6891 : : /*
6892 : : * The files might not be there if no backend has been started since the
6893 : : * last removal. But complain about failures other than ENOENT with
6894 : : * ERROR. Fortunately, it's not too late to abort the transaction if we
6895 : : * can't get rid of the would-be-obsolete init file.
6896 : : */
2833 andres@anarazel.de 6897 [ + - ]: 20707 : if (DatabasePath)
6898 : 20707 : unlink_initfile(localinitfname, ERROR);
6899 : 20707 : unlink_initfile(sharedinitfname, ERROR);
10841 scrappy@hub.org 6900 : 20707 : }
6901 : :
6902 : : void
5325 tgl@sss.pgh.pa.us 6903 : 20707 : RelationCacheInitFilePostInvalidate(void)
6904 : : {
6905 : 20707 : LWLockRelease(RelCacheInitLock);
6906 : 20707 : }
6907 : :
6908 : : /*
6909 : : * Remove the init files during postmaster startup.
6910 : : *
6911 : : * We used to keep the init files across restarts, but that is unsafe in PITR
6912 : : * scenarios, and even in simple crash-recovery cases there are windows for
6913 : : * the init files to become out-of-sync with the database. So now we just
6914 : : * remove them during startup and expect the first backend launch to rebuild
6915 : : * them. Of course, this has to happen in each database of the cluster.
6916 : : */
6917 : : void
6059 6918 : 1002 : RelationCacheInitFileRemove(void)
6919 : : {
558 michael@paquier.xyz 6920 : 1002 : const char *tblspcdir = PG_TBLSPC_DIR;
6921 : : DIR *dir;
6922 : : struct dirent *de;
6923 : : char path[MAXPGPATH + sizeof(PG_TBLSPC_DIR) + sizeof(TABLESPACE_VERSION_DIRECTORY)];
6924 : :
6059 tgl@sss.pgh.pa.us 6925 : 1002 : snprintf(path, sizeof(path), "global/%s",
6926 : : RELCACHE_INIT_FILENAME);
2833 andres@anarazel.de 6927 : 1002 : unlink_initfile(path, LOG);
6928 : :
6929 : : /* Scan everything in the default tablespace */
6059 tgl@sss.pgh.pa.us 6930 : 1002 : RelationCacheInitFileRemoveInDir("base");
6931 : :
6932 : : /* Scan the tablespace link directory to find non-default tablespaces */
6933 : 1002 : dir = AllocateDir(tblspcdir);
6934 : :
3023 6935 [ + + ]: 4069 : while ((de = ReadDirExtended(dir, tblspcdir, LOG)) != NULL)
6936 : : {
6059 6937 [ + + ]: 2065 : if (strspn(de->d_name, "0123456789") == strlen(de->d_name))
6938 : : {
6939 : : /* Scan the tablespace dir for per-database dirs */
5906 bruce@momjian.us 6940 : 61 : snprintf(path, sizeof(path), "%s/%s/%s",
6941 : 61 : tblspcdir, de->d_name, TABLESPACE_VERSION_DIRECTORY);
6059 tgl@sss.pgh.pa.us 6942 : 61 : RelationCacheInitFileRemoveInDir(path);
6943 : : }
6944 : : }
6945 : :
6946 : 1002 : FreeDir(dir);
6947 : 1002 : }
6948 : :
6949 : : /* Process one per-tablespace directory for RelationCacheInitFileRemove */
6950 : : static void
6951 : 1063 : RelationCacheInitFileRemoveInDir(const char *tblspcpath)
6952 : : {
6953 : : DIR *dir;
6954 : : struct dirent *de;
6955 : : char initfilename[MAXPGPATH * 2];
6956 : :
6957 : : /* Scan the tablespace directory to find per-database directories */
6958 : 1063 : dir = AllocateDir(tblspcpath);
6959 : :
3023 6960 [ + + ]: 7497 : while ((de = ReadDirExtended(dir, tblspcpath, LOG)) != NULL)
6961 : : {
6059 6962 [ + + ]: 5371 : if (strspn(de->d_name, "0123456789") == strlen(de->d_name))
6963 : : {
6964 : : /* Try to remove the init file in each database */
6965 : 3161 : snprintf(initfilename, sizeof(initfilename), "%s/%s/%s",
6966 : 3161 : tblspcpath, de->d_name, RELCACHE_INIT_FILENAME);
2833 andres@anarazel.de 6967 : 3161 : unlink_initfile(initfilename, LOG);
6968 : : }
6969 : : }
6970 : :
6059 tgl@sss.pgh.pa.us 6971 : 1063 : FreeDir(dir);
6972 : 1063 : }
6973 : :
6974 : : static void
2833 andres@anarazel.de 6975 : 45577 : unlink_initfile(const char *initfilename, int elevel)
6976 : : {
6059 tgl@sss.pgh.pa.us 6977 [ + + ]: 45577 : if (unlink(initfilename) < 0)
6978 : : {
6979 : : /* It might not be there, but log any error other than ENOENT */
6980 [ - + ]: 44243 : if (errno != ENOENT)
2833 andres@anarazel.de 6981 [ # # ]:UBC 0 : ereport(elevel,
6982 : : (errcode_for_file_access(),
6983 : : errmsg("could not remove cache file \"%s\": %m",
6984 : : initfilename)));
6985 : : }
7070 tgl@sss.pgh.pa.us 6986 :CBC 45577 : }
6987 : :
6988 : : /*
6989 : : * ResourceOwner callbacks
6990 : : */
6991 : : static char *
858 heikki.linnakangas@i 6992 : 5 : ResOwnerPrintRelCache(Datum res)
6993 : : {
6994 : 5 : Relation rel = (Relation) DatumGetPointer(res);
6995 : :
6996 : 5 : return psprintf("relation \"%s\"", RelationGetRelationName(rel));
6997 : : }
6998 : :
6999 : : static void
7000 : 23426 : ResOwnerReleaseRelation(Datum res)
7001 : : {
7002 : 23426 : Relation rel = (Relation) DatumGetPointer(res);
7003 : :
7004 : : /*
7005 : : * This reference has already been removed from the resource owner, so
7006 : : * just decrement reference count without calling
7007 : : * ResourceOwnerForgetRelationRef.
7008 : : */
7009 [ - + ]: 23426 : Assert(rel->rd_refcnt > 0);
7010 : 23426 : rel->rd_refcnt -= 1;
7011 : :
219 peter@eisentraut.org 7012 :GNC 23426 : RelationCloseCleanup((Relation) DatumGetPointer(res));
858 heikki.linnakangas@i 7013 :CBC 23426 : }
|