inval.c 19.8 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * inval.c
4
 *	  POSTGRES cache invalidation dispatcher code.
5
 *
6 7
 *	This is subtle stuff, so pay attention:
 *
8 9 10 11 12 13
 *	When a tuple is updated or deleted, our standard 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, and note that system catalogs are generally
 *	scanned under SnapshotNow rules by the system, or plain user snapshots
 *	for user queries.)  At the command boundary, the old tuple stops
14 15 16 17 18 19
 *	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
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 *	flushes at the next command boundary.  We must also keep track of
 *	inserted tuples so that we can flush "negative" cache entries that match
 *	the new tuples; again, that mustn't happen until end of command.
 *
 *	Once we have finished the command, we still need to remember inserted
 *	tuples (including new versions of updated tuples), so that we can flush
 *	them from the caches if we abort the transaction.  Similarly, we'd better
 *	be able to flush "negative" cache entries that may have been loaded in
 *	place of deleted tuples, so we still need the deleted ones too.
 *
 *	If we successfully complete the transaction, we have to broadcast all
 *	these invalidation events to other backends (via the SI message queue)
 *	so that they can flush obsolete entries from their caches.  Note we have
 *	to record the transaction commit before sending SI messages, otherwise
 *	the other backends won't see our updated tuples as good.
 *
 *	In short, we need to remember until xact end every insert or delete
 *	of a tuple that might be in the system caches.  Updates are treated as
 *	two events, delete + insert, for simplicity.  (There are cases where
 *	it'd be possible to record just one event, but we don't currently try.)
40 41
 *
 *	We do not need to register EVERY tuple operation in this way, just those
42
 *	on tuples in relations that have associated catcaches.	We do, however,
43
 *	have to register every operation on every tuple that *could* be in a
44
 *	catcache, whether or not it currently is in our cache.	Also, if the
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
 *	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.)
 *
63 64 65 66 67 68
 *	If a relcache flush is issued for a system relation that we preload
 *	from the relcache init file, we must also delete the init file so that
 *	it will be rebuilt during the next backend restart.  The actual work of
 *	manipulating the init file is in relcache.c, but we keep track of the
 *	need for it here.
 *
69 70
 *	All the request lists are kept in TopTransactionContext memory, since
 *	they need not live beyond the end of the current transaction.
71 72
 *
 *
73
 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
Bruce Momjian's avatar
Add:  
Bruce Momjian committed
74
 * Portions Copyright (c) 1994, Regents of the University of California
75 76
 *
 * IDENTIFICATION
77
 *	  $Header: /cvsroot/pgsql/src/backend/utils/cache/inval.c,v 1.50 2002/04/12 20:38:28 tgl Exp $
78 79 80 81
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"
82

83
#include "catalog/catalog.h"
Bruce Momjian's avatar
Bruce Momjian committed
84
#include "miscadmin.h"
85 86 87
#include "storage/sinval.h"
#include "utils/catcache.h"
#include "utils/inval.h"
88
#include "utils/memutils.h"
89 90
#include "utils/relcache.h"

91

92
/*
93 94
 * To minimize palloc traffic, we keep pending requests in successively-
 * larger chunks (a slightly more sophisticated version of an expansible
95
 * array).	All request types can be stored as SharedInvalidationMessage
96
 * records.
97
 */
98
typedef struct InvalidationChunk
99
{
100
	struct InvalidationChunk *next;		/* list link */
101 102
	int			nitems;			/* # items currently stored in chunk */
	int			maxitems;		/* size of allocated array in this chunk */
103
	SharedInvalidationMessage msgs[1];	/* VARIABLE LENGTH ARRAY */
104
} InvalidationChunk;			/* VARIABLE LENGTH STRUCTURE */
105

106
typedef struct InvalidationListHeader
107
{
108 109 110
	InvalidationChunk *cclist;	/* list of chunks holding catcache msgs */
	InvalidationChunk *rclist;	/* list of chunks holding relcache msgs */
} InvalidationListHeader;
111 112 113

/*
 * ----------------
114 115 116 117 118 119 120 121 122 123
 *	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
 *	   other backends or rolled back from local cache when we commit
 *	   or abort the transaction.
 *
 * The relcache-file-invalidated flag can just be a simple boolean,
 * since we only act on it at transaction commit; we don't care which
 * command of the transaction set it.
124 125 126
 * ----------------
 */

127 128
/* head of current-command event list */
static InvalidationListHeader CurrentCmdInvalidMsgs;
129

130 131
/* head of previous-commands event list */
static InvalidationListHeader PriorCmdInvalidMsgs;
132

133
static bool RelcacheInitFileInval; /* init file must be invalidated? */
134

135 136

/* ----------------------------------------------------------------
137 138 139 140
 *				Invalidation list support functions
 *
 * These three routines encapsulate processing of the "chunked"
 * representation of what is logically just a list of messages.
141 142 143
 * ----------------------------------------------------------------
 */

144
/*
145 146 147 148 149
 * 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.
150
 */
151 152 153
static void
AddInvalidationMessage(InvalidationChunk **listHdr,
					   SharedInvalidationMessage *msg)
154
{
155
	InvalidationChunk *chunk = *listHdr;
156

157 158 159 160 161 162 163
	if (chunk == NULL)
	{
		/* First time through; create initial chunk */
#define FIRSTCHUNKSIZE 16
		chunk = (InvalidationChunk *)
			MemoryContextAlloc(TopTransactionContext,
							   sizeof(InvalidationChunk) +
164
				(FIRSTCHUNKSIZE - 1) *sizeof(SharedInvalidationMessage));
165 166 167 168 169 170 171 172
		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 */
173
		int			chunksize = 2 * chunk->maxitems;
174 175 176 177

		chunk = (InvalidationChunk *)
			MemoryContextAlloc(TopTransactionContext,
							   sizeof(InvalidationChunk) +
178
					 (chunksize - 1) *sizeof(SharedInvalidationMessage));
179 180 181 182 183 184 185 186
		chunk->nitems = 0;
		chunk->maxitems = chunksize;
		chunk->next = *listHdr;
		*listHdr = chunk;
	}
	/* Okay, add message to current chunk */
	chunk->msgs[chunk->nitems] = *msg;
	chunk->nitems++;
187 188
}

189
/*
190 191 192 193 194
 * 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.
195
 */
196
static void
197
FreeInvalidationMessageList(InvalidationChunk **listHdr)
198
{
199
	InvalidationChunk *chunk = *listHdr;
200

201
	*listHdr = NULL;
202

203 204 205
	while (chunk != NULL)
	{
		InvalidationChunk *nextchunk = chunk->next;
206

207 208
		pfree(chunk);
		chunk = nextchunk;
209 210 211
	}
}

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
/*
 * Append one list of invalidation message chunks to another, resetting
 * the source chunk-list pointer to NULL.
 */
static void
AppendInvalidationMessageList(InvalidationChunk **destHdr,
							  InvalidationChunk **srcHdr)
{
	InvalidationChunk *chunk = *srcHdr;

	if (chunk == NULL)
		return;					/* nothing to do */

	while (chunk->next != NULL)
		chunk = chunk->next;

	chunk->next = *destHdr;

	*destHdr = *srcHdr;

	*srcHdr = NULL;
}

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
/*
 * 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)

255

256
/* ----------------------------------------------------------------
257 258 259 260
 *				Invalidation set support functions
 *
 * These routines understand about the division of a logical invalidation
 * list into separate physical lists for catcache and relcache entries.
261 262
 * ----------------------------------------------------------------
 */
263

264
/*
265
 * Add a catcache inval entry
266
 */
267 268
static void
AddCatcacheInvalidationMessage(InvalidationListHeader *hdr,
269
							   int id, uint32 hashValue,
270
							   ItemPointer tuplePtr, Oid dbId)
271
{
272
	SharedInvalidationMessage msg;
273

274 275
	msg.cc.id = (int16) id;
	msg.cc.tuplePtr = *tuplePtr;
276 277
	msg.cc.dbId = dbId;
	msg.cc.hashValue = hashValue;
278
	AddInvalidationMessage(&hdr->cclist, &msg);
279 280
}

281
/*
282
 * Add a relcache inval entry
283
 */
284
static void
285 286
AddRelcacheInvalidationMessage(InvalidationListHeader *hdr,
							   Oid dbId, Oid relId)
287
{
288 289 290 291 292 293 294 295 296 297 298 299
	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);
300 301
}

302 303 304 305 306 307 308 309 310 311 312 313
/*
 * Append one list of invalidation messages to another, resetting
 * the source list to empty.
 */
static void
AppendInvalidationMessages(InvalidationListHeader *dest,
						   InvalidationListHeader *src)
{
	AppendInvalidationMessageList(&dest->cclist, &src->cclist);
	AppendInvalidationMessageList(&dest->rclist, &src->rclist);
}

314
/*
315 316 317
 * Reset an invalidation list to empty
 *
 * physicalFree may be set false if caller knows transaction is ending
318 319
 */
static void
320
DiscardInvalidationMessages(InvalidationListHeader *hdr, bool physicalFree)
321
{
322 323 324 325 326 327 328 329
	if (physicalFree)
	{
		/* Physically pfree the list data */
		FreeInvalidationMessageList(&hdr->cclist);
		FreeInvalidationMessageList(&hdr->rclist);
	}
	else
	{
330 331 332 333
		/*
		 * Assume the storage will go away at xact end, just reset
		 * pointers
		 */
334 335 336
		hdr->cclist = NULL;
		hdr->rclist = NULL;
	}
337 338
}

339
/*
340 341 342 343
 * 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.
344
 */
345 346 347
static void
ProcessInvalidationMessages(InvalidationListHeader *hdr,
							void (*func) (SharedInvalidationMessage *msg))
348
{
349 350 351
	ProcessMessageList(hdr->cclist, func(msg));
	ProcessMessageList(hdr->rclist, func(msg));
}
352

353 354 355 356
/* ----------------------------------------------------------------
 *					  private support functions
 * ----------------------------------------------------------------
 */
357

358 359 360
/*
 * RegisterCatcacheInvalidation
 *
361
 * Register an invalidation event for a catcache tuple entry.
362 363 364
 */
static void
RegisterCatcacheInvalidation(int cacheId,
365
							 uint32 hashValue,
366 367 368
							 ItemPointer tuplePtr,
							 Oid dbId)
{
369 370
	AddCatcacheInvalidationMessage(&CurrentCmdInvalidMsgs,
								   cacheId, hashValue, tuplePtr, dbId);
371 372
}

373
/*
374 375 376
 * RegisterRelcacheInvalidation
 *
 * As above, but register a relcache invalidation event.
377 378
 */
static void
379
RegisterRelcacheInvalidation(Oid dbId, Oid relId)
380
{
381
	AddRelcacheInvalidationMessage(&CurrentCmdInvalidMsgs,
382
								   dbId, relId);
383 384 385 386 387 388
	/*
	 * If the relation being invalidated is one of those cached in the
	 * relcache init file, mark that we need to zap that file at commit.
	 */
	if (RelationIdIsInInitFile(relId))
		RelcacheInitFileInval = true;
389 390
}

391 392 393 394 395 396 397 398 399 400 401
/*
 * 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)
402
	{
403 404
		if (msg->cc.dbId == MyDatabaseId || msg->cc.dbId == 0)
			CatalogCacheIdInvalidate(msg->cc.id,
405
									 msg->cc.hashValue,
406
									 &msg->cc.tuplePtr);
407
	}
408
	else if (msg->id == SHAREDINVALRELCACHE_ID)
409
	{
410 411
		if (msg->rc.dbId == MyDatabaseId || msg->rc.dbId == 0)
			RelationIdInvalidateRelationCacheByRelationId(msg->rc.relId);
412
	}
413
	else
414
	{
415 416
		elog(FATAL, "ExecuteInvalidationMessage: bogus message id %d",
			 msg->id);
417
	}
418 419
}

420
/*
421
 *		InvalidateSystemCaches
422
 *
423 424 425
 *		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.
426 427 428 429
 *
 *		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.
430 431
 */
static void
432
InvalidateSystemCaches(void)
433
{
434
	ResetCatalogCaches();
435
	RelationCacheInvalidate();
436 437
}

438
/*
439
 * PrepareForTupleInvalidation
440 441
 *		Detect whether invalidation of this tuple implies invalidation
 *		of catalog/relation cache entries; if so, register inval events.
442 443
 */
static void
444
PrepareForTupleInvalidation(Relation relation, HeapTuple tuple,
445
							void (*CacheIdRegisterFunc) (int, uint32,
446
													   ItemPointer, Oid),
447
							void (*RelationIdRegisterFunc) (Oid, Oid))
448
{
449 450
	Oid			tupleRelId;
	Oid			relationId;
451

452 453
	if (IsBootstrapProcessingMode())
		return;
454

455 456 457 458 459
	/*
	 * 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.
	 */
460
	if (!IsSystemRelation(relation))
461
		return;
462 463 464 465 466 467
	/* 
	 * TOAST tuples can likewise be ignored here.
	 * Note that TOAST tables are considered system relations
	 * so they are not filtered by the above test.
	 */
	if (IsToastRelation(relation))
468
		return;
469

470
	/*
471
	 * First let the catcache do its thing
472
	 */
473 474
	PrepareToInvalidateCacheTuple(relation, tuple,
								  CacheIdRegisterFunc);
475

476
	/*
477
	 * Now, is this tuple one of the primary definers of a relcache entry?
478
	 */
479 480 481 482 483 484
	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;
485 486 487
	else
		return;

488
	/*
489 490 491
	 * Yes.  We need to register a relcache invalidation event for the
	 * relation identified by relationId.
	 *
492 493 494 495 496 497
	 * 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.
498
	 */
499
	(*RelationIdRegisterFunc) (MyDatabaseId, relationId);
500 501
}

502

503 504 505 506 507
/* ----------------------------------------------------------------
 *					  public functions
 * ----------------------------------------------------------------
 */

508
/*
509 510 511
 * AcceptInvalidationMessages
 *		Read and process invalidation messages from the shared invalidation
 *		message queue.
512 513
 *
 * Note:
514
 *		This should be called as the first step in processing a transaction.
515 516
 */
void
517
AcceptInvalidationMessages(void)
518
{
519 520
	ReceiveSharedInvalidMessages(LocalExecuteInvalidationMessage,
								 InvalidateSystemCaches);
521 522 523
}

/*
524 525 526
 * AtEOXactInvalidationMessages
 *		Process queued-up invalidation messages at end of transaction.
 *
527
 * If isCommit, we must send out the messages in our PriorCmdInvalidMsgs list
528 529
 * 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
530 531 532
 * transaction start (via AcceptInvalidationMessages).  This means that
 * we can skip immediate local processing of anything that's still in
 * CurrentCmdInvalidMsgs, and just send that list out too.
533 534
 *
 * If not isCommit, we are aborting, and must locally process the messages
535 536 537 538
 * in PriorCmdInvalidMsgs.  No messages need be sent to other backends,
 * since they'll not have seen our changed tuples anyway.  We can forget
 * about CurrentCmdInvalidMsgs too, since those changes haven't touched
 * the caches yet.
539 540 541 542
 *
 * In any case, reset the various lists to empty.  We need not physically
 * free memory here, since TopTransactionContext is about to be emptied
 * anyway.
543 544
 *
 * Note:
545
 *		This should be called as the last step in processing a transaction.
546 547
 */
void
548
AtEOXactInvalidationMessages(bool isCommit)
549
{
550
	if (isCommit)
551
	{
552 553 554 555 556 557 558 559
		/*
		 * Relcache init file invalidation requires processing both
		 * before and after we send the SI messages.  However, we need
		 * not do anything unless we committed.
		 */
		if (RelcacheInitFileInval)
			RelationCacheInitFileInvalidate(true);

560 561 562 563
		AppendInvalidationMessages(&PriorCmdInvalidMsgs,
								   &CurrentCmdInvalidMsgs);

		ProcessInvalidationMessages(&PriorCmdInvalidMsgs,
564
									SendSharedInvalidMessage);
565 566 567

		if (RelcacheInitFileInval)
			RelationCacheInitFileInvalidate(false);
568
	}
569
	else
570
	{
571
		ProcessInvalidationMessages(&PriorCmdInvalidMsgs,
572
									LocalExecuteInvalidationMessage);
573
	}
574

575 576
	RelcacheInitFileInval = false;

577 578
	DiscardInvalidationMessages(&PriorCmdInvalidMsgs, false);
	DiscardInvalidationMessages(&CurrentCmdInvalidMsgs, false);
579 580 581
}

/*
582 583 584 585 586
 * 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
587 588 589 590
 * we will commit.	We do need to locally process the CurrentCmdInvalidMsgs
 * list, so as to flush our caches of any entries we have outdated in the
 * current command.  We then move the current-cmd list over to become part
 * of the prior-cmds list.
591 592 593
 *
 * The isCommit = false case is not currently used, but may someday be
 * needed to support rollback to a savepoint within a transaction.
594 595
 *
 * Note:
596 597
 *		This should be called during CommandCounterIncrement(),
 *		after we have advanced the command ID.
598 599
 */
void
600
CommandEndInvalidationMessages(bool isCommit)
601
{
602
	if (isCommit)
603
	{
604
		ProcessInvalidationMessages(&CurrentCmdInvalidMsgs,
605
									LocalExecuteInvalidationMessage);
606 607
		AppendInvalidationMessages(&PriorCmdInvalidMsgs,
								   &CurrentCmdInvalidMsgs);
608 609 610
	}
	else
	{
611
		/* XXX what needs to be done here? */
612 613
	}
}
614

615
/*
616
 * CacheInvalidateHeapTuple
617 618
 *		Register the given tuple for invalidation at end of command
 *		(ie, current command is outdating this tuple).
619 620
 */
void
621
CacheInvalidateHeapTuple(Relation relation, HeapTuple tuple)
622
{
623
	PrepareForTupleInvalidation(relation, tuple,
624 625
								RegisterCatcacheInvalidation,
								RegisterRelcacheInvalidation);
626 627 628
}

/*
629 630 631 632 633 634 635
 * CacheInvalidateRelcache
 *		Register invalidation of the specified relation's relcache entry
 *		at end of command.
 *
 * This is used in places that need to force relcache rebuild but aren't
 * changing any of the tuples recognized as contributors to the relcache
 * entry by PrepareForTupleInvalidation.  (An example is dropping an index.)
636 637
 */
void
638
CacheInvalidateRelcache(Oid relationId)
639
{
640 641
	/* See KLUGE ALERT in PrepareForTupleInvalidation */
	RegisterRelcacheInvalidation(MyDatabaseId, relationId);
642
}