1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* inval.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* POSTGRES cache invalidation dispatcher code.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2001-01-05 23:54:37 +01:00
|
|
|
* This is subtle stuff, so pay attention:
|
|
|
|
*
|
|
|
|
* When a tuple is updated or deleted, our time qualification rules consider
|
|
|
|
* that it is *still valid* so long as we are in the same command, ie,
|
|
|
|
* until the next CommandCounterIncrement() or transaction commit.
|
|
|
|
* (See utils/time/tqual.c.) At the command boundary, the old tuple stops
|
|
|
|
* being valid and the new version, if any, becomes valid. Therefore,
|
|
|
|
* we cannot simply flush a tuple from the system caches during heap_update()
|
|
|
|
* or heap_delete(). The tuple is still good at that point; what's more,
|
|
|
|
* even if we did flush it, it might be reloaded into the caches by a later
|
|
|
|
* request in the same command. So the correct behavior is to keep a list
|
|
|
|
* of outdated (updated/deleted) tuples and then do the required cache
|
|
|
|
* flushes at the next command boundary. Similarly, we need a list of
|
|
|
|
* inserted tuples (including new versions of updated tuples), which we will
|
|
|
|
* use to flush those tuples out of the caches if we abort the transaction.
|
|
|
|
* Notice that the first list lives only till command boundary, whereas the
|
|
|
|
* second lives till end of transaction. Finally, we need a third list of
|
|
|
|
* all tuples outdated in the current transaction; if we commit, we send
|
|
|
|
* those invalidation events to all other backends (via the SI message queue)
|
2001-10-25 07:50:21 +02:00
|
|
|
* so that they can flush obsolete entries from their caches. This list
|
2001-06-19 21:42:16 +02:00
|
|
|
* definitely can't be processed until after we commit, otherwise the other
|
|
|
|
* backends won't see our updated tuples as good.
|
2001-01-05 23:54:37 +01:00
|
|
|
*
|
|
|
|
* We do not need to register EVERY tuple operation in this way, just those
|
2001-10-25 07:50:21 +02:00
|
|
|
* on tuples in relations that have associated catcaches. We do, however,
|
2001-06-19 21:42:16 +02:00
|
|
|
* have to register every operation on every tuple that *could* be in a
|
2001-10-25 07:50:21 +02:00
|
|
|
* catcache, whether or not it currently is in our cache. Also, if the
|
2001-06-19 21:42:16 +02:00
|
|
|
* tuple is in a relation that has multiple catcaches, we need to register
|
|
|
|
* an invalidation message for each such catcache. catcache.c's
|
|
|
|
* PrepareToInvalidateCacheTuple() routine provides the knowledge of which
|
|
|
|
* catcaches may need invalidation for a given tuple.
|
|
|
|
*
|
|
|
|
* Also, whenever we see an operation on a pg_class or pg_attribute tuple,
|
|
|
|
* we register a relcache flush operation for the relation described by that
|
|
|
|
* tuple.
|
|
|
|
*
|
|
|
|
* We keep the relcache flush requests in lists separate from the catcache
|
|
|
|
* tuple flush requests. This allows us to issue all the pending catcache
|
|
|
|
* flushes before we issue relcache flushes, which saves us from loading
|
|
|
|
* a catcache tuple during relcache load only to flush it again right away.
|
|
|
|
* Also, we avoid queuing multiple relcache flush requests for the same
|
|
|
|
* relation, since a relcache flush is relatively expensive to do.
|
|
|
|
* (XXX is it worth testing likewise for duplicate catcache flush entries?
|
|
|
|
* Probably not.)
|
|
|
|
*
|
|
|
|
* All the request lists are kept in TopTransactionContext memory, since
|
|
|
|
* they need not live beyond the end of the current transaction.
|
2001-01-05 23:54:37 +01:00
|
|
|
*
|
|
|
|
*
|
2001-01-24 20:43:33 +01:00
|
|
|
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2001-11-17 00:30:35 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/utils/cache/inval.c,v 1.47 2001/11/16 23:30:35 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
1996-11-06 11:32:10 +01:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "catalog/catalog.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "miscadmin.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "storage/sinval.h"
|
|
|
|
#include "utils/catcache.h"
|
|
|
|
#include "utils/inval.h"
|
2001-06-19 21:42:16 +02:00
|
|
|
#include "utils/memutils.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "utils/relcache.h"
|
|
|
|
|
1997-08-19 23:40:56 +02:00
|
|
|
|
2001-02-22 19:39:20 +01:00
|
|
|
/*
|
2001-06-19 21:42:16 +02:00
|
|
|
* To minimize palloc traffic, we keep pending requests in successively-
|
|
|
|
* larger chunks (a slightly more sophisticated version of an expansible
|
2001-10-25 07:50:21 +02:00
|
|
|
* array). All request types can be stored as SharedInvalidationMessage
|
2001-06-19 21:42:16 +02:00
|
|
|
* records.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2001-06-19 21:42:16 +02:00
|
|
|
typedef struct InvalidationChunk
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
struct InvalidationChunk *next; /* list link */
|
2001-06-19 21:42:16 +02:00
|
|
|
int nitems; /* # items currently stored in chunk */
|
|
|
|
int maxitems; /* size of allocated array in this chunk */
|
2001-10-25 07:50:21 +02:00
|
|
|
SharedInvalidationMessage msgs[1]; /* VARIABLE LENGTH ARRAY */
|
2001-06-19 21:42:16 +02:00
|
|
|
} InvalidationChunk; /* VARIABLE LENGTH STRUCTURE */
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2001-06-19 21:42:16 +02:00
|
|
|
typedef struct InvalidationListHeader
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-06-19 21:42:16 +02:00
|
|
|
InvalidationChunk *cclist; /* list of chunks holding catcache msgs */
|
|
|
|
InvalidationChunk *rclist; /* list of chunks holding relcache msgs */
|
|
|
|
} InvalidationListHeader;
|
2000-01-10 07:30:56 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ----------------
|
2000-11-08 23:10:03 +01:00
|
|
|
* Invalidation info is divided into three parts.
|
2001-06-19 21:42:16 +02:00
|
|
|
* 1) shared invalidation to be sent to all backends at commit
|
2001-01-05 23:54:37 +01:00
|
|
|
* 2) local invalidation for the transaction itself (actually, just
|
|
|
|
* for the current command within the transaction)
|
2000-11-08 23:10:03 +01:00
|
|
|
* 3) rollback information for the transaction itself (in case we abort)
|
2000-01-10 07:30:56 +01:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2001-06-19 21:42:16 +02:00
|
|
|
* head of invalidation message list for all backends
|
2000-01-10 07:30:56 +01:00
|
|
|
* eaten by AtCommit_Cache() in CommitTransaction()
|
|
|
|
*/
|
2001-06-19 21:42:16 +02:00
|
|
|
static InvalidationListHeader GlobalInvalidMsgs;
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-01-10 07:30:56 +01:00
|
|
|
/*
|
2001-06-19 21:42:16 +02:00
|
|
|
* head of invalidation message list for the current command
|
2000-01-10 07:30:56 +01:00
|
|
|
* eaten by AtCommit_LocalCache() in CommandCounterIncrement()
|
|
|
|
*/
|
2001-06-19 21:42:16 +02:00
|
|
|
static InvalidationListHeader LocalInvalidMsgs;
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-01-10 07:30:56 +01:00
|
|
|
/*
|
2001-06-19 21:42:16 +02:00
|
|
|
* head of rollback message list for abort-time processing
|
2000-01-10 07:30:56 +01:00
|
|
|
* eaten by AtAbort_Cache() in AbortTransaction()
|
|
|
|
*/
|
2001-06-19 21:42:16 +02:00
|
|
|
static InvalidationListHeader RollbackMsgs;
|
1999-11-21 02:58:22 +01:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
2001-06-19 21:42:16 +02:00
|
|
|
* Invalidation list support functions
|
|
|
|
*
|
|
|
|
* These three routines encapsulate processing of the "chunked"
|
|
|
|
* representation of what is logically just a list of messages.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2001-02-22 19:39:20 +01:00
|
|
|
/*
|
2001-06-19 21:42:16 +02:00
|
|
|
* AddInvalidationMessage
|
|
|
|
* Add an invalidation message to a list (of chunks).
|
|
|
|
*
|
|
|
|
* Note that we do not pay any great attention to maintaining the original
|
|
|
|
* ordering of the messages.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2001-06-19 21:42:16 +02:00
|
|
|
static void
|
|
|
|
AddInvalidationMessage(InvalidationChunk **listHdr,
|
|
|
|
SharedInvalidationMessage *msg)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-06-19 21:42:16 +02:00
|
|
|
InvalidationChunk *chunk = *listHdr;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-06-19 21:42:16 +02:00
|
|
|
if (chunk == NULL)
|
|
|
|
{
|
|
|
|
/* First time through; create initial chunk */
|
|
|
|
#define FIRSTCHUNKSIZE 16
|
|
|
|
chunk = (InvalidationChunk *)
|
|
|
|
MemoryContextAlloc(TopTransactionContext,
|
|
|
|
sizeof(InvalidationChunk) +
|
2001-10-25 07:50:21 +02:00
|
|
|
(FIRSTCHUNKSIZE - 1) *sizeof(SharedInvalidationMessage));
|
2001-06-19 21:42:16 +02:00
|
|
|
chunk->nitems = 0;
|
|
|
|
chunk->maxitems = FIRSTCHUNKSIZE;
|
|
|
|
chunk->next = *listHdr;
|
|
|
|
*listHdr = chunk;
|
|
|
|
}
|
|
|
|
else if (chunk->nitems >= chunk->maxitems)
|
|
|
|
{
|
|
|
|
/* Need another chunk; double size of last chunk */
|
2001-10-25 07:50:21 +02:00
|
|
|
int chunksize = 2 * chunk->maxitems;
|
2001-06-19 21:42:16 +02:00
|
|
|
|
|
|
|
chunk = (InvalidationChunk *)
|
|
|
|
MemoryContextAlloc(TopTransactionContext,
|
|
|
|
sizeof(InvalidationChunk) +
|
2001-10-25 07:50:21 +02:00
|
|
|
(chunksize - 1) *sizeof(SharedInvalidationMessage));
|
2001-06-19 21:42:16 +02:00
|
|
|
chunk->nitems = 0;
|
|
|
|
chunk->maxitems = chunksize;
|
|
|
|
chunk->next = *listHdr;
|
|
|
|
*listHdr = chunk;
|
|
|
|
}
|
|
|
|
/* Okay, add message to current chunk */
|
|
|
|
chunk->msgs[chunk->nitems] = *msg;
|
|
|
|
chunk->nitems++;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2001-02-22 19:39:20 +01:00
|
|
|
/*
|
2001-06-19 21:42:16 +02:00
|
|
|
* Free a list of inval message chunks.
|
|
|
|
*
|
|
|
|
* NOTE: when we are about to commit or abort a transaction, it's
|
|
|
|
* not really necessary to pfree the lists explicitly, since they will
|
|
|
|
* go away anyway when TopTransactionContext is destroyed.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-08-19 23:40:56 +02:00
|
|
|
static void
|
2001-06-19 21:42:16 +02:00
|
|
|
FreeInvalidationMessageList(InvalidationChunk **listHdr)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-06-19 21:42:16 +02:00
|
|
|
InvalidationChunk *chunk = *listHdr;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-06-19 21:42:16 +02:00
|
|
|
*listHdr = NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-06-19 21:42:16 +02:00
|
|
|
while (chunk != NULL)
|
|
|
|
{
|
|
|
|
InvalidationChunk *nextchunk = chunk->next;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-06-19 21:42:16 +02:00
|
|
|
pfree(chunk);
|
|
|
|
chunk = nextchunk;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-19 21:42:16 +02:00
|
|
|
/*
|
|
|
|
* Process a list of invalidation messages.
|
|
|
|
*
|
|
|
|
* This is a macro that executes the given code fragment for each message in
|
|
|
|
* a message chunk list. The fragment should refer to the message as *msg.
|
|
|
|
*/
|
|
|
|
#define ProcessMessageList(listHdr, codeFragment) \
|
|
|
|
do { \
|
|
|
|
InvalidationChunk *_chunk; \
|
|
|
|
for (_chunk = (listHdr); _chunk != NULL; _chunk = _chunk->next) \
|
|
|
|
{ \
|
|
|
|
int _cindex; \
|
|
|
|
for (_cindex = 0; _cindex < _chunk->nitems; _cindex++) \
|
|
|
|
{ \
|
|
|
|
SharedInvalidationMessage *msg = &_chunk->msgs[_cindex]; \
|
|
|
|
codeFragment; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2000-01-10 07:30:56 +01:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------------------------------------------------------
|
2001-06-19 21:42:16 +02:00
|
|
|
* Invalidation set support functions
|
|
|
|
*
|
|
|
|
* These routines understand about the division of a logical invalidation
|
|
|
|
* list into separate physical lists for catcache and relcache entries.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-02-22 19:39:20 +01:00
|
|
|
/*
|
2001-06-19 21:42:16 +02:00
|
|
|
* Add a catcache inval entry
|
2000-01-10 07:30:56 +01:00
|
|
|
*/
|
2001-06-19 21:42:16 +02:00
|
|
|
static void
|
|
|
|
AddCatcacheInvalidationMessage(InvalidationListHeader *hdr,
|
|
|
|
int id, Index hashIndex,
|
|
|
|
ItemPointer tuplePtr, Oid dbId)
|
2000-01-10 07:30:56 +01:00
|
|
|
{
|
2001-06-19 21:42:16 +02:00
|
|
|
SharedInvalidationMessage msg;
|
2000-01-10 07:30:56 +01:00
|
|
|
|
2001-06-19 21:42:16 +02:00
|
|
|
msg.cc.id = (int16) id;
|
|
|
|
msg.cc.hashIndex = (uint16) hashIndex;
|
|
|
|
msg.cc.dbId = dbId;
|
|
|
|
msg.cc.tuplePtr = *tuplePtr;
|
|
|
|
AddInvalidationMessage(&hdr->cclist, &msg);
|
2000-01-10 07:30:56 +01:00
|
|
|
}
|
|
|
|
|
2001-02-22 19:39:20 +01:00
|
|
|
/*
|
2001-06-19 21:42:16 +02:00
|
|
|
* Add a relcache inval entry
|
2000-01-10 07:30:56 +01:00
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
static void
|
2001-06-19 21:42:16 +02:00
|
|
|
AddRelcacheInvalidationMessage(InvalidationListHeader *hdr,
|
|
|
|
Oid dbId, Oid relId)
|
2000-01-10 07:30:56 +01:00
|
|
|
{
|
2001-06-19 21:42:16 +02:00
|
|
|
SharedInvalidationMessage msg;
|
|
|
|
|
|
|
|
/* Don't add a duplicate item */
|
|
|
|
/* We assume comparing relId is sufficient, needn't check dbId */
|
|
|
|
ProcessMessageList(hdr->rclist,
|
|
|
|
if (msg->rc.relId == relId) return);
|
|
|
|
|
|
|
|
/* OK, add the item */
|
|
|
|
msg.rc.id = SHAREDINVALRELCACHE_ID;
|
|
|
|
msg.rc.dbId = dbId;
|
|
|
|
msg.rc.relId = relId;
|
|
|
|
AddInvalidationMessage(&hdr->rclist, &msg);
|
2000-01-10 07:30:56 +01:00
|
|
|
}
|
|
|
|
|
2001-02-22 19:39:20 +01:00
|
|
|
/*
|
2001-06-19 21:42:16 +02:00
|
|
|
* Reset an invalidation list to empty
|
|
|
|
*
|
|
|
|
* physicalFree may be set false if caller knows transaction is ending
|
2000-01-10 07:30:56 +01:00
|
|
|
*/
|
|
|
|
static void
|
2001-06-19 21:42:16 +02:00
|
|
|
DiscardInvalidationMessages(InvalidationListHeader *hdr, bool physicalFree)
|
2000-01-10 07:30:56 +01:00
|
|
|
{
|
2001-06-19 21:42:16 +02:00
|
|
|
if (physicalFree)
|
|
|
|
{
|
|
|
|
/* Physically pfree the list data */
|
|
|
|
FreeInvalidationMessageList(&hdr->cclist);
|
|
|
|
FreeInvalidationMessageList(&hdr->rclist);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
/*
|
|
|
|
* Assume the storage will go away at xact end, just reset
|
|
|
|
* pointers
|
|
|
|
*/
|
2001-06-19 21:42:16 +02:00
|
|
|
hdr->cclist = NULL;
|
|
|
|
hdr->rclist = NULL;
|
|
|
|
}
|
2000-01-10 07:30:56 +01:00
|
|
|
}
|
|
|
|
|
2001-02-22 19:39:20 +01:00
|
|
|
/*
|
2001-06-19 21:42:16 +02:00
|
|
|
* Execute the given function for all the messages in an invalidation list.
|
|
|
|
* The list is not altered.
|
|
|
|
*
|
|
|
|
* catcache entries are processed first, for reasons mentioned above.
|
2000-01-10 07:30:56 +01:00
|
|
|
*/
|
2001-06-19 21:42:16 +02:00
|
|
|
static void
|
|
|
|
ProcessInvalidationMessages(InvalidationListHeader *hdr,
|
|
|
|
void (*func) (SharedInvalidationMessage *msg))
|
2000-01-10 07:30:56 +01:00
|
|
|
{
|
2001-06-19 21:42:16 +02:00
|
|
|
ProcessMessageList(hdr->cclist, func(msg));
|
|
|
|
ProcessMessageList(hdr->rclist, func(msg));
|
|
|
|
}
|
2000-01-10 07:30:56 +01:00
|
|
|
|
2001-06-19 21:42:16 +02:00
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* private support functions
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-06-19 21:42:16 +02:00
|
|
|
/*
|
|
|
|
* RegisterCatcacheInvalidation
|
|
|
|
*
|
|
|
|
* Register an invalidation event for an updated/deleted catcache entry.
|
|
|
|
* We insert the event into both GlobalInvalidMsgs (for transmission
|
|
|
|
* to other backends at transaction commit) and LocalInvalidMsgs (for
|
|
|
|
* my local invalidation at end of command within xact).
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
RegisterCatcacheInvalidation(int cacheId,
|
|
|
|
Index hashIndex,
|
|
|
|
ItemPointer tuplePtr,
|
|
|
|
Oid dbId)
|
|
|
|
{
|
|
|
|
AddCatcacheInvalidationMessage(&GlobalInvalidMsgs,
|
|
|
|
cacheId, hashIndex, tuplePtr, dbId);
|
|
|
|
AddCatcacheInvalidationMessage(&LocalInvalidMsgs,
|
|
|
|
cacheId, hashIndex, tuplePtr, dbId);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2001-02-22 19:39:20 +01:00
|
|
|
/*
|
2001-06-19 21:42:16 +02:00
|
|
|
* RegisterRelcacheInvalidation
|
|
|
|
*
|
|
|
|
* As above, but register a relcache invalidation event.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
static void
|
2001-06-19 21:42:16 +02:00
|
|
|
RegisterRelcacheInvalidation(Oid dbId, Oid relId)
|
2000-01-10 07:30:56 +01:00
|
|
|
{
|
2001-06-19 21:42:16 +02:00
|
|
|
AddRelcacheInvalidationMessage(&GlobalInvalidMsgs,
|
|
|
|
dbId, relId);
|
|
|
|
AddRelcacheInvalidationMessage(&LocalInvalidMsgs,
|
|
|
|
dbId, relId);
|
2000-01-10 07:30:56 +01:00
|
|
|
}
|
|
|
|
|
2001-02-22 19:39:20 +01:00
|
|
|
/*
|
2001-06-19 21:42:16 +02:00
|
|
|
* RegisterCatcacheRollback
|
|
|
|
*
|
|
|
|
* Register an invalidation event for an inserted catcache entry.
|
|
|
|
* This only needs to be flushed out of my local catcache, if I abort.
|
2000-01-10 07:30:56 +01:00
|
|
|
*/
|
|
|
|
static void
|
2001-06-19 21:42:16 +02:00
|
|
|
RegisterCatcacheRollback(int cacheId,
|
|
|
|
Index hashIndex,
|
|
|
|
ItemPointer tuplePtr,
|
|
|
|
Oid dbId)
|
2000-01-10 07:30:56 +01:00
|
|
|
{
|
2001-06-19 21:42:16 +02:00
|
|
|
AddCatcacheInvalidationMessage(&RollbackMsgs,
|
|
|
|
cacheId, hashIndex, tuplePtr, dbId);
|
2000-01-10 07:30:56 +01:00
|
|
|
}
|
|
|
|
|
2001-02-22 19:39:20 +01:00
|
|
|
/*
|
2001-06-19 21:42:16 +02:00
|
|
|
* RegisterRelcacheRollback
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2001-06-19 21:42:16 +02:00
|
|
|
* As above, but register a relcache invalidation event.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
static void
|
2001-06-19 21:42:16 +02:00
|
|
|
RegisterRelcacheRollback(Oid dbId, Oid relId)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-06-19 21:42:16 +02:00
|
|
|
AddRelcacheInvalidationMessage(&RollbackMsgs,
|
|
|
|
dbId, relId);
|
|
|
|
}
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2001-06-19 21:42:16 +02:00
|
|
|
/*
|
|
|
|
* LocalExecuteInvalidationMessage
|
|
|
|
*
|
|
|
|
* Process a single invalidation message (which could be either type).
|
|
|
|
* Only the local caches are flushed; this does not transmit the message
|
|
|
|
* to other backends.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
LocalExecuteInvalidationMessage(SharedInvalidationMessage *msg)
|
|
|
|
{
|
|
|
|
if (msg->id >= 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-06-19 21:42:16 +02:00
|
|
|
if (msg->cc.dbId == MyDatabaseId || msg->cc.dbId == 0)
|
|
|
|
CatalogCacheIdInvalidate(msg->cc.id,
|
|
|
|
msg->cc.hashIndex,
|
|
|
|
&msg->cc.tuplePtr);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2001-06-19 21:42:16 +02:00
|
|
|
else if (msg->id == SHAREDINVALRELCACHE_ID)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-06-19 21:42:16 +02:00
|
|
|
if (msg->rc.dbId == MyDatabaseId || msg->rc.dbId == 0)
|
|
|
|
RelationIdInvalidateRelationCacheByRelationId(msg->rc.relId);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2001-06-19 21:42:16 +02:00
|
|
|
else
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-06-19 21:42:16 +02:00
|
|
|
elog(FATAL, "ExecuteInvalidationMessage: bogus message id %d",
|
|
|
|
msg->id);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2001-02-22 19:39:20 +01:00
|
|
|
/*
|
2001-06-18 05:35:07 +02:00
|
|
|
* InvalidateSystemCaches
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-01-29 20:51:59 +01:00
|
|
|
* This blows away all tuples in the system catalog caches and
|
|
|
|
* all the cached relation descriptors (and closes their files too).
|
|
|
|
* Relation descriptors that have positive refcounts are then rebuilt.
|
2001-06-18 05:35:07 +02:00
|
|
|
*
|
|
|
|
* We call this when we see a shared-inval-queue overflow signal,
|
|
|
|
* since that tells us we've lost some shared-inval messages and hence
|
|
|
|
* don't know what needs to be invalidated.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
static void
|
2001-06-18 05:35:07 +02:00
|
|
|
InvalidateSystemCaches(void)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-06-18 05:35:07 +02:00
|
|
|
ResetCatalogCaches();
|
2000-01-31 05:35:57 +01:00
|
|
|
RelationCacheInvalidate();
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2001-02-22 19:39:20 +01:00
|
|
|
/*
|
2001-06-19 21:42:16 +02:00
|
|
|
* PrepareForTupleInvalidation
|
|
|
|
* Invoke functions for the tuple which register invalidation
|
|
|
|
* of catalog/relation cache.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
static void
|
2001-06-19 21:42:16 +02:00
|
|
|
PrepareForTupleInvalidation(Relation relation, HeapTuple tuple,
|
|
|
|
void (*CacheIdRegisterFunc) (int, Index,
|
2001-10-25 07:50:21 +02:00
|
|
|
ItemPointer, Oid),
|
2001-06-19 21:42:16 +02:00
|
|
|
void (*RelationIdRegisterFunc) (Oid, Oid))
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-06-19 21:42:16 +02:00
|
|
|
Oid tupleRelId;
|
|
|
|
Oid relationId;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-06-19 21:42:16 +02:00
|
|
|
if (IsBootstrapProcessingMode())
|
|
|
|
return;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2001-06-19 21:42:16 +02:00
|
|
|
/*
|
|
|
|
* We only need to worry about invalidation for tuples that are in
|
|
|
|
* system relations; user-relation tuples are never in catcaches and
|
|
|
|
* can't affect the relcache either.
|
2001-11-17 00:30:35 +01:00
|
|
|
*
|
|
|
|
* TOAST tuples can likewise be ignored here.
|
2001-06-19 21:42:16 +02:00
|
|
|
*/
|
|
|
|
if (!IsSystemRelationName(NameStr(RelationGetForm(relation)->relname)))
|
|
|
|
return;
|
2001-11-17 00:30:35 +01:00
|
|
|
if (IsToastRelationName(NameStr(RelationGetForm(relation)->relname)))
|
|
|
|
return;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-02-22 19:39:20 +01:00
|
|
|
/*
|
2001-06-19 21:42:16 +02:00
|
|
|
* First let the catcache do its thing
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2001-06-19 21:42:16 +02:00
|
|
|
PrepareToInvalidateCacheTuple(relation, tuple,
|
|
|
|
CacheIdRegisterFunc);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-02-22 19:39:20 +01:00
|
|
|
/*
|
2001-06-19 21:42:16 +02:00
|
|
|
* Now, is this tuple one of the primary definers of a relcache entry?
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2001-06-19 21:42:16 +02:00
|
|
|
tupleRelId = RelationGetRelid(relation);
|
|
|
|
|
|
|
|
if (tupleRelId == RelOid_pg_class)
|
|
|
|
relationId = tuple->t_data->t_oid;
|
|
|
|
else if (tupleRelId == RelOid_pg_attribute)
|
|
|
|
relationId = ((Form_pg_attribute) GETSTRUCT(tuple))->attrelid;
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
|
|
|
return;
|
|
|
|
|
2001-02-22 19:39:20 +01:00
|
|
|
/*
|
2001-06-19 21:42:16 +02:00
|
|
|
* Yes. We need to register a relcache invalidation event for the
|
|
|
|
* relation identified by relationId.
|
|
|
|
*
|
2001-10-25 07:50:21 +02:00
|
|
|
* KLUGE ALERT: we always send the relcache event with MyDatabaseId, even
|
|
|
|
* if the rel in question is shared. This essentially means that only
|
|
|
|
* backends in this same database will react to the relcache flush
|
|
|
|
* request. This is in fact appropriate, since only those backends
|
|
|
|
* could see our pg_class or pg_attribute change anyway. It looks a
|
|
|
|
* bit ugly though.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2001-06-19 21:42:16 +02:00
|
|
|
(*RelationIdRegisterFunc) (MyDatabaseId, relationId);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1998-02-23 18:44:24 +01:00
|
|
|
|
2001-06-19 21:42:16 +02:00
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* public functions
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
2001-06-19 21:42:16 +02:00
|
|
|
* AcceptInvalidationMessages
|
|
|
|
* Read and process invalidation messages from the shared invalidation
|
|
|
|
* message queue.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* Note:
|
1997-09-07 07:04:48 +02:00
|
|
|
* This should be called as the first step in processing a transaction.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
void
|
2001-06-19 21:42:16 +02:00
|
|
|
AcceptInvalidationMessages(void)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-06-19 21:42:16 +02:00
|
|
|
ReceiveSharedInvalidMessages(LocalExecuteInvalidationMessage,
|
|
|
|
InvalidateSystemCaches);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-06-19 21:42:16 +02:00
|
|
|
* AtEOXactInvalidationMessages
|
|
|
|
* Process queued-up invalidation messages at end of transaction.
|
|
|
|
*
|
|
|
|
* If isCommit, we must send out the messages in our GlobalInvalidMsgs list
|
|
|
|
* to the shared invalidation message queue. Note that these will be read
|
|
|
|
* not only by other backends, but also by our own backend at the next
|
2001-10-25 07:50:21 +02:00
|
|
|
* transaction start (via AcceptInvalidationMessages). Therefore, it's okay
|
2001-06-19 21:42:16 +02:00
|
|
|
* to discard any pending LocalInvalidMsgs, since these will be redundant
|
|
|
|
* with the global list.
|
|
|
|
*
|
|
|
|
* If not isCommit, we are aborting, and must locally process the messages
|
|
|
|
* in our RollbackMsgs list. No messages need be sent to other backends,
|
|
|
|
* since they'll not have seen our changed tuples anyway.
|
|
|
|
*
|
|
|
|
* In any case, reset the various lists to empty. We need not physically
|
|
|
|
* free memory here, since TopTransactionContext is about to be emptied
|
|
|
|
* anyway.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* Note:
|
1997-09-07 07:04:48 +02:00
|
|
|
* This should be called as the last step in processing a transaction.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
void
|
2001-06-19 21:42:16 +02:00
|
|
|
AtEOXactInvalidationMessages(bool isCommit)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-06-19 21:42:16 +02:00
|
|
|
if (isCommit)
|
2000-01-10 07:30:56 +01:00
|
|
|
{
|
2001-06-19 21:42:16 +02:00
|
|
|
ProcessInvalidationMessages(&GlobalInvalidMsgs,
|
|
|
|
SendSharedInvalidMessage);
|
2000-01-10 07:30:56 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
2000-01-10 07:30:56 +01:00
|
|
|
{
|
2001-06-19 21:42:16 +02:00
|
|
|
ProcessInvalidationMessages(&RollbackMsgs,
|
|
|
|
LocalExecuteInvalidationMessage);
|
2000-01-10 07:30:56 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-06-19 21:42:16 +02:00
|
|
|
DiscardInvalidationMessages(&GlobalInvalidMsgs, false);
|
|
|
|
DiscardInvalidationMessages(&LocalInvalidMsgs, false);
|
|
|
|
DiscardInvalidationMessages(&RollbackMsgs, false);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-06-19 21:42:16 +02:00
|
|
|
* CommandEndInvalidationMessages
|
|
|
|
* Process queued-up invalidation messages at end of one command
|
|
|
|
* in a transaction.
|
|
|
|
*
|
|
|
|
* Here, we send no messages to the shared queue, since we don't know yet if
|
2001-10-25 07:50:21 +02:00
|
|
|
* we will commit. But we do need to locally process the LocalInvalidMsgs
|
2001-06-19 21:42:16 +02:00
|
|
|
* list, so as to flush our caches of any tuples we have outdated in the
|
|
|
|
* current command.
|
|
|
|
*
|
|
|
|
* The isCommit = false case is not currently used, but may someday be
|
|
|
|
* needed to support rollback to a savepoint within a transaction.
|
|
|
|
* (I suspect it needs more work first --- tgl.)
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* Note:
|
2001-01-05 23:54:37 +01:00
|
|
|
* This should be called during CommandCounterIncrement(),
|
|
|
|
* after we have advanced the command ID.
|
2000-01-10 07:30:56 +01:00
|
|
|
*/
|
|
|
|
void
|
2001-06-19 21:42:16 +02:00
|
|
|
CommandEndInvalidationMessages(bool isCommit)
|
2000-01-10 07:30:56 +01:00
|
|
|
{
|
2001-06-19 21:42:16 +02:00
|
|
|
if (isCommit)
|
2000-01-10 07:30:56 +01:00
|
|
|
{
|
2001-06-19 21:42:16 +02:00
|
|
|
ProcessInvalidationMessages(&LocalInvalidMsgs,
|
|
|
|
LocalExecuteInvalidationMessage);
|
2000-01-10 07:30:56 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-06-19 21:42:16 +02:00
|
|
|
ProcessInvalidationMessages(&RollbackMsgs,
|
|
|
|
LocalExecuteInvalidationMessage);
|
2000-01-10 07:30:56 +01:00
|
|
|
}
|
2001-10-25 07:50:21 +02:00
|
|
|
|
2001-02-22 19:39:20 +01:00
|
|
|
/*
|
2001-10-25 07:50:21 +02:00
|
|
|
* LocalInvalidMsgs list is not interesting anymore, so flush it (for
|
|
|
|
* real). Do *not* clear GlobalInvalidMsgs or RollbackMsgs.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2001-06-19 21:42:16 +02:00
|
|
|
DiscardInvalidationMessages(&LocalInvalidMsgs, true);
|
2000-01-10 07:30:56 +01:00
|
|
|
}
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-01-10 07:30:56 +01:00
|
|
|
/*
|
|
|
|
* RelationInvalidateHeapTuple
|
2001-01-05 23:54:37 +01:00
|
|
|
* Register the given tuple for invalidation at end of command
|
|
|
|
* (ie, current command is outdating this tuple).
|
2000-01-10 07:30:56 +01:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
RelationInvalidateHeapTuple(Relation relation, HeapTuple tuple)
|
|
|
|
{
|
2001-01-05 23:54:37 +01:00
|
|
|
PrepareForTupleInvalidation(relation, tuple,
|
2001-06-19 21:42:16 +02:00
|
|
|
RegisterCatcacheInvalidation,
|
|
|
|
RegisterRelcacheInvalidation);
|
2000-01-10 07:30:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RelationMark4RollbackHeapTuple
|
2001-01-05 23:54:37 +01:00
|
|
|
* Register the given tuple for invalidation in case of abort
|
|
|
|
* (ie, current command is creating this tuple).
|
2000-01-10 07:30:56 +01:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
RelationMark4RollbackHeapTuple(Relation relation, HeapTuple tuple)
|
|
|
|
{
|
2001-01-05 23:54:37 +01:00
|
|
|
PrepareForTupleInvalidation(relation, tuple,
|
2001-06-19 21:42:16 +02:00
|
|
|
RegisterCatcacheRollback,
|
|
|
|
RegisterRelcacheRollback);
|
2000-01-10 07:30:56 +01:00
|
|
|
}
|