1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* sinval.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* POSTGRES shared cache invalidation communication code.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
1999-09-06 21:37:38 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/sinval.c,v 1.18 1999/09/06 19:37:38 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
/* #define INVALIDDEBUG 1 */
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1996-11-03 06:08:01 +01:00
|
|
|
#include <sys/types.h>
|
1996-10-31 06:58:01 +01:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
1996-11-06 07:52:23 +01:00
|
|
|
#include "storage/backendid.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "storage/sinval.h"
|
|
|
|
#include "storage/sinvaladt.h"
|
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
SPINLOCK SInvalLock = (SPINLOCK) NULL;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/****************************************************************************/
|
1999-05-28 19:03:31 +02:00
|
|
|
/* CreateSharedInvalidationState() Create a buffer segment */
|
1997-09-07 07:04:48 +02:00
|
|
|
/* */
|
|
|
|
/* should be called only by the POSTMASTER */
|
1996-07-09 08:22:35 +02:00
|
|
|
/****************************************************************************/
|
|
|
|
void
|
1999-05-28 19:03:31 +02:00
|
|
|
CreateSharedInvalidationState(IPCKey key, int maxBackends)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int status;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* SInvalLock gets set in spin.c, during spinlock init */
|
1999-05-28 19:03:31 +02:00
|
|
|
status = SISegmentInit(true, IPCKeyGetSIBufferMemoryBlock(key),
|
|
|
|
maxBackends);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (status == -1)
|
|
|
|
elog(FATAL, "CreateSharedInvalidationState: failed segment init");
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/****************************************************************************/
|
1999-09-06 21:37:38 +02:00
|
|
|
/* AttachSharedInvalidationState(key) Attach to existing buffer segment */
|
1997-09-07 07:04:48 +02:00
|
|
|
/* */
|
1999-09-06 21:37:38 +02:00
|
|
|
/* should be called by each backend during startup */
|
1996-07-09 08:22:35 +02:00
|
|
|
/****************************************************************************/
|
|
|
|
void
|
|
|
|
AttachSharedInvalidationState(IPCKey key)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int status;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (key == PrivateIPCKey)
|
|
|
|
{
|
1999-05-28 19:03:31 +02:00
|
|
|
CreateSharedInvalidationState(key, 16);
|
1997-09-07 07:04:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* SInvalLock gets set in spin.c, during spinlock init */
|
1999-05-28 19:03:31 +02:00
|
|
|
status = SISegmentInit(false, IPCKeyGetSIBufferMemoryBlock(key), 0);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (status == -1)
|
|
|
|
elog(FATAL, "AttachSharedInvalidationState: failed segment init");
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1999-09-06 21:37:38 +02:00
|
|
|
/*
|
|
|
|
* InitSharedInvalidationState
|
|
|
|
* Initialize new backend's state info in buffer segment.
|
|
|
|
* Must be called after AttachSharedInvalidationState().
|
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
void
|
1996-11-10 04:06:38 +01:00
|
|
|
InitSharedInvalidationState(void)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
SpinAcquire(SInvalLock);
|
|
|
|
if (!SIBackendInit(shmInvalBuffer))
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
SpinRelease(SInvalLock);
|
|
|
|
elog(FATAL, "Backend cache invalidation initialization failed");
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
SpinRelease(SInvalLock);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* RegisterSharedInvalid
|
1999-09-06 21:37:38 +02:00
|
|
|
* Add a shared-cache-invalidation message to the global SI message queue.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* Note:
|
1997-09-07 07:04:48 +02:00
|
|
|
* Assumes hash index is valid.
|
|
|
|
* Assumes item pointer is valid.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
void
|
1997-09-07 07:04:48 +02:00
|
|
|
RegisterSharedInvalid(int cacheId, /* XXX */
|
|
|
|
Index hashIndex,
|
|
|
|
ItemPointer pointer)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1999-09-06 21:37:38 +02:00
|
|
|
SharedInvalidData newInvalid;
|
|
|
|
bool insertOK;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This code has been hacked to accept two types of messages. This
|
|
|
|
* might be treated more generally in the future.
|
|
|
|
*
|
|
|
|
* (1) cacheId= system cache id hashIndex= system cache hash index for a
|
|
|
|
* (possibly) cached tuple pointer= pointer of (possibly) cached tuple
|
|
|
|
*
|
|
|
|
* (2) cacheId= special non-syscache id hashIndex= object id contained in
|
|
|
|
* (possibly) cached relation descriptor pointer= null
|
|
|
|
*/
|
|
|
|
|
|
|
|
newInvalid.cacheId = cacheId;
|
|
|
|
newInvalid.hashIndex = hashIndex;
|
|
|
|
|
|
|
|
if (ItemPointerIsValid(pointer))
|
|
|
|
ItemPointerCopy(pointer, &newInvalid.pointerData);
|
|
|
|
else
|
|
|
|
ItemPointerSetInvalid(&newInvalid.pointerData);
|
|
|
|
|
|
|
|
SpinAcquire(SInvalLock);
|
1999-09-06 21:37:38 +02:00
|
|
|
insertOK = SIInsertDataEntry(shmInvalBuffer, &newInvalid);
|
1997-09-07 07:04:48 +02:00
|
|
|
SpinRelease(SInvalLock);
|
1999-09-06 21:37:38 +02:00
|
|
|
if (! insertOK)
|
|
|
|
elog(NOTICE, "RegisterSharedInvalid: SI buffer overflow");
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* InvalidateSharedInvalid
|
1999-09-06 21:37:38 +02:00
|
|
|
* Process shared-cache-invalidation messages waiting for this backend
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
void
|
1999-09-04 20:36:45 +02:00
|
|
|
InvalidateSharedInvalid(void (*invalFunction) (),
|
|
|
|
void (*resetFunction) ())
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1999-09-04 20:36:45 +02:00
|
|
|
SharedInvalidData data;
|
|
|
|
int getResult;
|
|
|
|
bool gotMessage = false;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-09-04 20:36:45 +02:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
SpinAcquire(SInvalLock);
|
|
|
|
getResult = SIGetDataEntry(shmInvalBuffer, MyBackendId, &data);
|
|
|
|
SpinRelease(SInvalLock);
|
|
|
|
if (getResult == 0)
|
|
|
|
break; /* nothing more to do */
|
|
|
|
if (getResult < 0)
|
|
|
|
{
|
|
|
|
/* got a reset message */
|
|
|
|
elog(NOTICE, "InvalidateSharedInvalid: cache state reset");
|
|
|
|
resetFunction();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* got a normal data message */
|
|
|
|
invalFunction(data.cacheId,
|
|
|
|
data.hashIndex,
|
|
|
|
&data.pointerData);
|
|
|
|
}
|
|
|
|
gotMessage = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we got any messages, try to release dead messages */
|
|
|
|
if (gotMessage)
|
|
|
|
{
|
|
|
|
SpinAcquire(SInvalLock);
|
|
|
|
SIDelExpiredDataEntries(shmInvalBuffer);
|
|
|
|
SpinRelease(SInvalLock);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|