diff options
Diffstat (limited to 'src/backend/utils/cache/inval.c')
-rw-r--r-- | src/backend/utils/cache/inval.c | 259 |
1 files changed, 71 insertions, 188 deletions
diff --git a/src/backend/utils/cache/inval.c b/src/backend/utils/cache/inval.c index f9570f4b1ce..e2b9976e931 100644 --- a/src/backend/utils/cache/inval.c +++ b/src/backend/utils/cache/inval.c @@ -99,10 +99,6 @@ * worth trying to avoid sending such inval traffic in the future, if those * problems can be overcome cheaply. * - * When making a nontransactional change to a cacheable object, we must - * likewise send the invalidation immediately, before ending the change's - * critical section. This includes inplace heap updates, relmap, and smgr. - * * * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California @@ -154,7 +150,7 @@ typedef struct InvalidationListHeader } InvalidationListHeader; /*---------------- - * Transactional invalidation info is divided into two lists: + * Invalidation info is divided into two lists: * 1) events so far in current command, not yet reflected to caches. * 2) events in previous commands of current transaction; these have * been reflected to local caches, and must be either broadcast to @@ -170,36 +166,26 @@ typedef struct InvalidationListHeader *---------------- */ -/* fields common to both transactional and inplace invalidation */ -typedef struct InvalidationInfo -{ - /* head of current-command event list */ - InvalidationListHeader CurrentCmdInvalidMsgs; - - /* init file must be invalidated? */ - bool RelcacheInitFileInval; -} InvalidationInfo; - -/* subclass adding fields specific to transactional invalidation */ typedef struct TransInvalidationInfo { - /* Base class */ - struct InvalidationInfo ii; - - /* head of previous-commands event list */ - InvalidationListHeader PriorCmdInvalidMsgs; - /* Back link to parent transaction's info */ struct TransInvalidationInfo *parent; /* Subtransaction nesting depth */ int my_level; + + /* head of current-command event list */ + InvalidationListHeader CurrentCmdInvalidMsgs; + + /* head of previous-commands event list */ + InvalidationListHeader PriorCmdInvalidMsgs; + + /* init file must be invalidated? */ + bool RelcacheInitFileInval; } TransInvalidationInfo; static TransInvalidationInfo *transInvalInfo = NULL; -static InvalidationInfo *inplaceInvalInfo = NULL; - static SharedInvalidationMessage *SharedInvalidMessagesArray; static int numSharedInvalidMessagesArray; static int maxSharedInvalidMessagesArray; @@ -513,12 +499,9 @@ ProcessInvalidationMessagesMulti(InvalidationListHeader *hdr, static void RegisterCatcacheInvalidation(int cacheId, uint32 hashValue, - Oid dbId, - void *context) + Oid dbId) { - InvalidationInfo *info = (InvalidationInfo *) context; - - AddCatcacheInvalidationMessage(&info->CurrentCmdInvalidMsgs, + AddCatcacheInvalidationMessage(&transInvalInfo->CurrentCmdInvalidMsgs, cacheId, hashValue, dbId); } @@ -528,9 +511,10 @@ RegisterCatcacheInvalidation(int cacheId, * Register an invalidation event for all catcache entries from a catalog. */ static void -RegisterCatalogInvalidation(InvalidationInfo *info, Oid dbId, Oid catId) +RegisterCatalogInvalidation(Oid dbId, Oid catId) { - AddCatalogInvalidationMessage(&info->CurrentCmdInvalidMsgs, dbId, catId); + AddCatalogInvalidationMessage(&transInvalInfo->CurrentCmdInvalidMsgs, + dbId, catId); } /* @@ -539,9 +523,10 @@ RegisterCatalogInvalidation(InvalidationInfo *info, Oid dbId, Oid catId) * As above, but register a relcache invalidation event. */ static void -RegisterRelcacheInvalidation(InvalidationInfo *info, Oid dbId, Oid relId) +RegisterRelcacheInvalidation(Oid dbId, Oid relId) { - AddRelcacheInvalidationMessage(&info->CurrentCmdInvalidMsgs, dbId, relId); + AddRelcacheInvalidationMessage(&transInvalInfo->CurrentCmdInvalidMsgs, + dbId, relId); /* * Most of the time, relcache invalidation is associated with system @@ -558,7 +543,7 @@ RegisterRelcacheInvalidation(InvalidationInfo *info, Oid dbId, Oid relId) * as well. Also zap when we are invalidating whole relcache. */ if (relId == InvalidOid || RelationIdIsInInitFile(relId)) - info->RelcacheInitFileInval = true; + transInvalInfo->RelcacheInitFileInval = true; } /* @@ -568,9 +553,10 @@ RegisterRelcacheInvalidation(InvalidationInfo *info, Oid dbId, Oid relId) * Only needed for catalogs that don't have catcaches. */ static void -RegisterSnapshotInvalidation(InvalidationInfo *info, Oid dbId, Oid relId) +RegisterSnapshotInvalidation(Oid dbId, Oid relId) { - AddSnapshotInvalidationMessage(&info->CurrentCmdInvalidMsgs, dbId, relId); + AddSnapshotInvalidationMessage(&transInvalInfo->CurrentCmdInvalidMsgs, + dbId, relId); } /* @@ -762,18 +748,14 @@ AcceptInvalidationMessages(void) * PrepareInvalidationState * Initialize inval lists for the current (sub)transaction. */ -static InvalidationInfo * +static void PrepareInvalidationState(void) { TransInvalidationInfo *myInfo; - Assert(IsTransactionState()); - /* Can't queue transactional message while collecting inplace messages. */ - Assert(inplaceInvalInfo == NULL); - if (transInvalInfo != NULL && transInvalInfo->my_level == GetCurrentTransactionNestLevel()) - return (InvalidationInfo *) transInvalInfo; + return; myInfo = (TransInvalidationInfo *) MemoryContextAllocZero(TopTransactionContext, @@ -789,29 +771,6 @@ PrepareInvalidationState(void) myInfo->my_level > transInvalInfo->my_level); transInvalInfo = myInfo; - return (InvalidationInfo *) myInfo; -} - -/* - * PrepareInplaceInvalidationState - * Initialize inval data for an inplace update. - * - * See previous function for more background. - */ -static InvalidationInfo * -PrepareInplaceInvalidationState(void) -{ - InvalidationInfo *myInfo; - - Assert(IsTransactionState()); - /* limit of one inplace update under assembly */ - Assert(inplaceInvalInfo == NULL); - - /* gone after WAL insertion CritSection ends, so use current context */ - myInfo = (InvalidationInfo *) palloc0(sizeof(InvalidationInfo)); - - inplaceInvalInfo = myInfo; - return myInfo; } /* @@ -907,7 +866,7 @@ xactGetCommittedInvalidationMessages(SharedInvalidationMessage **msgs, * after we send the SI messages. However, we need not do anything unless * we committed. */ - *RelcacheInitFileInval = transInvalInfo->ii.RelcacheInitFileInval; + *RelcacheInitFileInval = transInvalInfo->RelcacheInitFileInval; /* * Walk through TransInvalidationInfo to collect all the messages into a @@ -919,7 +878,7 @@ xactGetCommittedInvalidationMessages(SharedInvalidationMessage **msgs, */ oldcontext = MemoryContextSwitchTo(CurTransactionContext); - ProcessInvalidationMessagesMulti(&transInvalInfo->ii.CurrentCmdInvalidMsgs, + ProcessInvalidationMessagesMulti(&transInvalInfo->CurrentCmdInvalidMsgs, MakeSharedInvalidMessagesArray); ProcessInvalidationMessagesMulti(&transInvalInfo->PriorCmdInvalidMsgs, MakeSharedInvalidMessagesArray); @@ -1009,9 +968,7 @@ ProcessCommittedInvalidationMessages(SharedInvalidationMessage *msgs, void AtEOXact_Inval(bool isCommit) { - inplaceInvalInfo = NULL; - - /* Quick exit if no transactional messages */ + /* Quick exit if no messages */ if (transInvalInfo == NULL) return; @@ -1025,16 +982,16 @@ AtEOXact_Inval(bool isCommit) * after we send the SI messages. However, we need not do anything * unless we committed. */ - if (transInvalInfo->ii.RelcacheInitFileInval) + if (transInvalInfo->RelcacheInitFileInval) RelationCacheInitFilePreInvalidate(); AppendInvalidationMessages(&transInvalInfo->PriorCmdInvalidMsgs, - &transInvalInfo->ii.CurrentCmdInvalidMsgs); + &transInvalInfo->CurrentCmdInvalidMsgs); ProcessInvalidationMessagesMulti(&transInvalInfo->PriorCmdInvalidMsgs, SendSharedInvalidMessages); - if (transInvalInfo->ii.RelcacheInitFileInval) + if (transInvalInfo->RelcacheInitFileInval) RelationCacheInitFilePostInvalidate(); } else @@ -1050,45 +1007,6 @@ AtEOXact_Inval(bool isCommit) } /* - * PreInplace_Inval - * Process queued-up invalidation before inplace update critical section. - * - * Tasks belong here if they are safe even if the inplace update does not - * complete. Currently, this just unlinks a cache file, which can fail. The - * sum of this and AtInplace_Inval() mirrors AtEOXact_Inval(isCommit=true). - */ -void -PreInplace_Inval(void) -{ - Assert(CritSectionCount == 0); - - if (inplaceInvalInfo && inplaceInvalInfo->RelcacheInitFileInval) - RelationCacheInitFilePreInvalidate(); -} - -/* - * AtInplace_Inval - * Process queued-up invalidations after inplace update buffer mutation. - */ -void -AtInplace_Inval(void) -{ - Assert(CritSectionCount > 0); - - if (inplaceInvalInfo == NULL) - return; - - ProcessInvalidationMessagesMulti(&inplaceInvalInfo->CurrentCmdInvalidMsgs, - SendSharedInvalidMessages); - - if (inplaceInvalInfo->RelcacheInitFileInval) - RelationCacheInitFilePostInvalidate(); - - inplaceInvalInfo = NULL; - /* inplace doesn't use SharedInvalidMessagesArray */ -} - -/* * AtEOSubXact_Inval * Process queued-up invalidation messages at end of subtransaction. * @@ -1110,20 +1028,9 @@ void AtEOSubXact_Inval(bool isCommit) { int my_level; - TransInvalidationInfo *myInfo; + TransInvalidationInfo *myInfo = transInvalInfo; - /* - * Successful inplace update must clear this, but we clear it on abort. - * Inplace updates allocate this in CurrentMemoryContext, which has - * lifespan <= subtransaction lifespan. Hence, don't free it explicitly. - */ - if (isCommit) - Assert(inplaceInvalInfo == NULL); - else - inplaceInvalInfo = NULL; - - /* Quick exit if no transactional messages. */ - myInfo = transInvalInfo; + /* Quick exit if no messages. */ if (myInfo == NULL) return; @@ -1157,8 +1064,8 @@ AtEOSubXact_Inval(bool isCommit) &myInfo->PriorCmdInvalidMsgs); /* Pending relcache inval becomes parent's problem too */ - if (myInfo->ii.RelcacheInitFileInval) - myInfo->parent->ii.RelcacheInitFileInval = true; + if (myInfo->RelcacheInitFileInval) + myInfo->parent->RelcacheInitFileInval = true; /* Pop the transaction state stack */ transInvalInfo = myInfo->parent; @@ -1205,24 +1112,29 @@ CommandEndInvalidationMessages(void) if (transInvalInfo == NULL) return; - ProcessInvalidationMessages(&transInvalInfo->ii.CurrentCmdInvalidMsgs, + ProcessInvalidationMessages(&transInvalInfo->CurrentCmdInvalidMsgs, LocalExecuteInvalidationMessage); AppendInvalidationMessages(&transInvalInfo->PriorCmdInvalidMsgs, - &transInvalInfo->ii.CurrentCmdInvalidMsgs); + &transInvalInfo->CurrentCmdInvalidMsgs); } /* - * CacheInvalidateHeapTupleCommon - * Common logic for end-of-command and inplace variants. + * CacheInvalidateHeapTuple + * Register the given tuple for invalidation at end of command + * (ie, current command is creating or outdating this tuple). + * Also, detect whether a relcache invalidation is implied. + * + * For an insert or delete, tuple is the target tuple and newtuple is NULL. + * For an update, we are called just once, with tuple being the old tuple + * version and newtuple the new version. This allows avoidance of duplicate + * effort during an update. */ -static void -CacheInvalidateHeapTupleCommon(Relation relation, - HeapTuple tuple, - HeapTuple newtuple, - InvalidationInfo *(*prepare_callback) (void)) +void +CacheInvalidateHeapTuple(Relation relation, + HeapTuple tuple, + HeapTuple newtuple) { - InvalidationInfo *info; Oid tupleRelId; Oid databaseId; Oid relationId; @@ -1246,8 +1158,11 @@ CacheInvalidateHeapTupleCommon(Relation relation, if (IsToastRelation(relation)) return; - /* Allocate any required resources. */ - info = prepare_callback(); + /* + * If we're not prepared to queue invalidation messages for this + * subtransaction level, get ready now. + */ + PrepareInvalidationState(); /* * First let the catcache do its thing @@ -1256,12 +1171,11 @@ CacheInvalidateHeapTupleCommon(Relation relation, if (RelationInvalidatesSnapshotsOnly(tupleRelId)) { databaseId = IsSharedRelation(tupleRelId) ? InvalidOid : MyDatabaseId; - RegisterSnapshotInvalidation(info, databaseId, tupleRelId); + RegisterSnapshotInvalidation(databaseId, tupleRelId); } else PrepareToInvalidateCacheTuple(relation, tuple, newtuple, - RegisterCatcacheInvalidation, - (void *) info); + RegisterCatcacheInvalidation); /* * Now, is this tuple one of the primary definers of a relcache entry? See @@ -1334,44 +1248,7 @@ CacheInvalidateHeapTupleCommon(Relation relation, /* * Yes. We need to register a relcache invalidation event. */ - RegisterRelcacheInvalidation(info, databaseId, relationId); -} - -/* - * CacheInvalidateHeapTuple - * Register the given tuple for invalidation at end of command - * (ie, current command is creating or outdating this tuple) and end of - * transaction. Also, detect whether a relcache invalidation is implied. - * - * For an insert or delete, tuple is the target tuple and newtuple is NULL. - * For an update, we are called just once, with tuple being the old tuple - * version and newtuple the new version. This allows avoidance of duplicate - * effort during an update. - */ -void -CacheInvalidateHeapTuple(Relation relation, - HeapTuple tuple, - HeapTuple newtuple) -{ - CacheInvalidateHeapTupleCommon(relation, tuple, newtuple, - PrepareInvalidationState); -} - -/* - * CacheInvalidateHeapTupleInplace - * Register the given tuple for nontransactional invalidation pertaining - * to an inplace update. Also, detect whether a relcache invalidation is - * implied. - * - * Like CacheInvalidateHeapTuple(), but for inplace updates. - */ -void -CacheInvalidateHeapTupleInplace(Relation relation, - HeapTuple tuple, - HeapTuple newtuple) -{ - CacheInvalidateHeapTupleCommon(relation, tuple, newtuple, - PrepareInplaceInvalidationState); + RegisterRelcacheInvalidation(databaseId, relationId); } /* @@ -1390,13 +1267,14 @@ CacheInvalidateCatalog(Oid catalogId) { Oid databaseId; + PrepareInvalidationState(); + if (IsSharedRelation(catalogId)) databaseId = InvalidOid; else databaseId = MyDatabaseId; - RegisterCatalogInvalidation(PrepareInvalidationState(), - databaseId, catalogId); + RegisterCatalogInvalidation(databaseId, catalogId); } /* @@ -1414,14 +1292,15 @@ CacheInvalidateRelcache(Relation relation) Oid databaseId; Oid relationId; + PrepareInvalidationState(); + relationId = RelationGetRelid(relation); if (relation->rd_rel->relisshared) databaseId = InvalidOid; else databaseId = MyDatabaseId; - RegisterRelcacheInvalidation(PrepareInvalidationState(), - databaseId, relationId); + RegisterRelcacheInvalidation(databaseId, relationId); } /* @@ -1434,8 +1313,9 @@ CacheInvalidateRelcache(Relation relation) void CacheInvalidateRelcacheAll(void) { - RegisterRelcacheInvalidation(PrepareInvalidationState(), - InvalidOid, InvalidOid); + PrepareInvalidationState(); + + RegisterRelcacheInvalidation(InvalidOid, InvalidOid); } /* @@ -1449,13 +1329,14 @@ CacheInvalidateRelcacheByTuple(HeapTuple classTuple) Oid databaseId; Oid relationId; + PrepareInvalidationState(); + relationId = classtup->oid; if (classtup->relisshared) databaseId = InvalidOid; else databaseId = MyDatabaseId; - RegisterRelcacheInvalidation(PrepareInvalidationState(), - databaseId, relationId); + RegisterRelcacheInvalidation(databaseId, relationId); } /* @@ -1469,6 +1350,8 @@ CacheInvalidateRelcacheByRelid(Oid relid) { HeapTuple tup; + PrepareInvalidationState(); + tup = SearchSysCache1(RELOID, ObjectIdGetDatum(relid)); if (!HeapTupleIsValid(tup)) elog(ERROR, "cache lookup failed for relation %u", relid); |