mirror of
https://github.com/postgres/postgres.git
synced 2025-11-10 17:42:29 +03:00
Several changes here, not very related but touching some of the same files.
* Buffer refcount cleanup (per my "progress report" to pghackers, 9/22). * Add links to backend PROC structs to sinval's array of per-backend info, and use these links for routines that need to check the state of all backends (rather than the slow, complicated search of the ShmemIndex hashtable that was used before). Add databaseOID to PROC structs. * Use this to implement an interlock that prevents DESTROY DATABASE of a database containing running backends. (It's a little tricky to prevent a concurrently-starting backend from getting in there, since the new backend is not able to lock anything at the time it tries to look up its database in pg_database. My solution is to recheck that the DB is OK at the end of InitPostgres. It may not be a 100% solution, but it's a lot better than no interlock at all...) * In ALTER TABLE RENAME, flush buffers for the relation before doing the rename of the physical files, to ensure we don't get failures later from mdblindwrt(). * Update TRUNCATE patch so that it actually compiles against current sources :-(. You should do "make clean all" after pulling these changes.
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/shmem.c,v 1.45 1999/07/17 20:17:44 momjian Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/shmem.c,v 1.46 1999/09/24 00:24:35 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -579,174 +579,3 @@ ShmemInitStruct(char *name, unsigned long size, bool *foundPtr)
|
||||
SpinRelease(ShmemIndexLock);
|
||||
return structPtr;
|
||||
}
|
||||
|
||||
/*
|
||||
* TransactionIdIsInProgress -- is given transaction running by some backend
|
||||
*
|
||||
* Strange place for this func, but we have to lookup process data structures
|
||||
* for all running backends. - vadim 11/26/96
|
||||
*
|
||||
* We should keep all PROC structs not in ShmemIndex - this is too
|
||||
* general hash table...
|
||||
*
|
||||
*/
|
||||
bool
|
||||
TransactionIdIsInProgress(TransactionId xid)
|
||||
{
|
||||
ShmemIndexEnt *result;
|
||||
PROC *proc;
|
||||
|
||||
Assert(ShmemIndex);
|
||||
|
||||
SpinAcquire(ShmemIndexLock);
|
||||
|
||||
hash_seq((HTAB *) NULL);
|
||||
while ((result = (ShmemIndexEnt *) hash_seq(ShmemIndex)) != NULL)
|
||||
{
|
||||
if (result == (ShmemIndexEnt *) TRUE)
|
||||
{
|
||||
SpinRelease(ShmemIndexLock);
|
||||
return false;
|
||||
}
|
||||
if (result->location == INVALID_OFFSET ||
|
||||
strncmp(result->key, "PID ", 4) != 0)
|
||||
continue;
|
||||
proc = (PROC *) MAKE_PTR(result->location);
|
||||
if (proc->xid == xid)
|
||||
{
|
||||
SpinRelease(ShmemIndexLock);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
SpinRelease(ShmemIndexLock);
|
||||
elog(ERROR, "TransactionIdIsInProgress: ShmemIndex corrupted");
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* GetSnapshotData -- returns information about running transactions.
|
||||
*
|
||||
* Yet another strange func for this place... - vadim 07/21/98
|
||||
*/
|
||||
Snapshot
|
||||
GetSnapshotData(bool serializable)
|
||||
{
|
||||
Snapshot snapshot = (Snapshot) malloc(sizeof(SnapshotData));
|
||||
ShmemIndexEnt *result;
|
||||
PROC *proc;
|
||||
TransactionId cid = GetCurrentTransactionId();
|
||||
TransactionId xid;
|
||||
uint32 count = 0;
|
||||
uint32 have = 32;
|
||||
|
||||
Assert(ShmemIndex);
|
||||
|
||||
snapshot->xip = (TransactionId *) malloc(have * sizeof(TransactionId));
|
||||
snapshot->xmin = cid;
|
||||
|
||||
SpinAcquire(ShmemIndexLock);
|
||||
/*
|
||||
* Unfortunately, we have to call ReadNewTransactionId()
|
||||
* after acquiring ShmemIndexLock above. It's not good because of
|
||||
* ReadNewTransactionId() does SpinAcquire(OidGenLockId) but
|
||||
* _necessary_.
|
||||
*/
|
||||
ReadNewTransactionId(&(snapshot->xmax));
|
||||
|
||||
hash_seq((HTAB *) NULL);
|
||||
while ((result = (ShmemIndexEnt *) hash_seq(ShmemIndex)) != NULL)
|
||||
{
|
||||
if (result == (ShmemIndexEnt *) TRUE)
|
||||
{
|
||||
if (serializable)
|
||||
MyProc->xmin = snapshot->xmin;
|
||||
/* Serializable snapshot must be computed before any other... */
|
||||
Assert(MyProc->xmin != InvalidTransactionId);
|
||||
SpinRelease(ShmemIndexLock);
|
||||
snapshot->xcnt = count;
|
||||
return snapshot;
|
||||
}
|
||||
if (result->location == INVALID_OFFSET ||
|
||||
strncmp(result->key, "PID ", 4) != 0)
|
||||
continue;
|
||||
proc = (PROC *) MAKE_PTR(result->location);
|
||||
/*
|
||||
* We don't use spin-locking when changing proc->xid
|
||||
* in GetNewTransactionId() and in AbortTransaction() !..
|
||||
*/
|
||||
xid = proc->xid;
|
||||
if (proc == MyProc ||
|
||||
xid < FirstTransactionId || xid >= snapshot->xmax)
|
||||
{
|
||||
/*
|
||||
* Seems that there is no sense to store xid >= snapshot->xmax
|
||||
* (what we got from ReadNewTransactionId above) in snapshot->xip
|
||||
* - we just assume that all xacts with such xid-s are running
|
||||
* and may be ignored.
|
||||
*/
|
||||
continue;
|
||||
}
|
||||
if (xid < snapshot->xmin)
|
||||
snapshot->xmin = xid;
|
||||
if (have == 0)
|
||||
{
|
||||
snapshot->xip = (TransactionId *) realloc(snapshot->xip,
|
||||
(count + 32) * sizeof(TransactionId));
|
||||
have = 32;
|
||||
}
|
||||
snapshot->xip[count] = xid;
|
||||
have--;
|
||||
count++;
|
||||
}
|
||||
|
||||
SpinRelease(ShmemIndexLock);
|
||||
free(snapshot->xip);
|
||||
free(snapshot);
|
||||
elog(ERROR, "GetSnapshotData: ShmemIndex corrupted");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* GetXmaxRecent -- returns oldest transaction that was running
|
||||
* when all current transaction was started.
|
||||
* It's used by vacuum to decide what deleted
|
||||
* tuples must be preserved in a table.
|
||||
*
|
||||
* And yet another strange func for this place... - vadim 03/18/99
|
||||
*/
|
||||
void
|
||||
GetXmaxRecent(TransactionId *XmaxRecent)
|
||||
{
|
||||
ShmemIndexEnt *result;
|
||||
PROC *proc;
|
||||
TransactionId xmin;
|
||||
|
||||
Assert(ShmemIndex);
|
||||
|
||||
*XmaxRecent = GetCurrentTransactionId();
|
||||
|
||||
SpinAcquire(ShmemIndexLock);
|
||||
|
||||
hash_seq((HTAB *) NULL);
|
||||
while ((result = (ShmemIndexEnt *) hash_seq(ShmemIndex)) != NULL)
|
||||
{
|
||||
if (result == (ShmemIndexEnt *) TRUE)
|
||||
{
|
||||
SpinRelease(ShmemIndexLock);
|
||||
return;
|
||||
}
|
||||
if (result->location == INVALID_OFFSET ||
|
||||
strncmp(result->key, "PID ", 4) != 0)
|
||||
continue;
|
||||
proc = (PROC *) MAKE_PTR(result->location);
|
||||
xmin = proc->xmin; /* we don't use spin-locking in AbortTransaction() ! */
|
||||
if (proc == MyProc || xmin < FirstTransactionId)
|
||||
continue;
|
||||
if (xmin < *XmaxRecent)
|
||||
*XmaxRecent = xmin;
|
||||
}
|
||||
|
||||
SpinRelease(ShmemIndexLock);
|
||||
elog(ERROR, "GetXmaxRecent: ShmemIndex corrupted");
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/sinval.c,v 1.18 1999/09/06 19:37:38 tgl Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/sinval.c,v 1.19 1999/09/24 00:24:35 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -18,8 +18,10 @@
|
||||
#include "postgres.h"
|
||||
|
||||
#include "storage/backendid.h"
|
||||
#include "storage/proc.h"
|
||||
#include "storage/sinval.h"
|
||||
#include "storage/sinvaladt.h"
|
||||
#include "utils/tqual.h"
|
||||
|
||||
SPINLOCK SInvalLock = (SPINLOCK) NULL;
|
||||
|
||||
@@ -165,3 +167,201 @@ InvalidateSharedInvalid(void (*invalFunction) (),
|
||||
SpinRelease(SInvalLock);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/****************************************************************************/
|
||||
/* Functions that need to scan the PROC structures of all running backends. */
|
||||
/* It's a bit strange to keep these in sinval.c, since they don't have any */
|
||||
/* direct relationship to shared-cache invalidation. But the procState */
|
||||
/* array in the SI segment is the only place in the system where we have */
|
||||
/* an array of per-backend data, so it is the most convenient place to keep */
|
||||
/* pointers to the backends' PROC structures. We used to implement these */
|
||||
/* functions with a slow, ugly search through the ShmemIndex hash table --- */
|
||||
/* now they are simple loops over the SI ProcState array. */
|
||||
/****************************************************************************/
|
||||
|
||||
|
||||
/*
|
||||
* DatabaseHasActiveBackends -- are there any backends running in the given DB
|
||||
*
|
||||
* This function is used to interlock DROP DATABASE against there being
|
||||
* any active backends in the target DB --- dropping the DB while active
|
||||
* backends remain would be a Bad Thing. Note that we cannot detect here
|
||||
* the possibility of a newly-started backend that is trying to connect
|
||||
* to the doomed database, so additional interlocking is needed during
|
||||
* backend startup.
|
||||
*/
|
||||
|
||||
bool
|
||||
DatabaseHasActiveBackends(Oid databaseId)
|
||||
{
|
||||
bool result = false;
|
||||
SISeg *segP = shmInvalBuffer;
|
||||
ProcState *stateP = segP->procState;
|
||||
int index;
|
||||
|
||||
SpinAcquire(SInvalLock);
|
||||
|
||||
for (index = 0; index < segP->maxBackends; index++)
|
||||
{
|
||||
SHMEM_OFFSET pOffset = stateP[index].procStruct;
|
||||
|
||||
if (pOffset != INVALID_OFFSET)
|
||||
{
|
||||
PROC *proc = (PROC *) MAKE_PTR(pOffset);
|
||||
|
||||
if (proc->databaseId == databaseId)
|
||||
{
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SpinRelease(SInvalLock);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* TransactionIdIsInProgress -- is given transaction running by some backend
|
||||
*/
|
||||
bool
|
||||
TransactionIdIsInProgress(TransactionId xid)
|
||||
{
|
||||
bool result = false;
|
||||
SISeg *segP = shmInvalBuffer;
|
||||
ProcState *stateP = segP->procState;
|
||||
int index;
|
||||
|
||||
SpinAcquire(SInvalLock);
|
||||
|
||||
for (index = 0; index < segP->maxBackends; index++)
|
||||
{
|
||||
SHMEM_OFFSET pOffset = stateP[index].procStruct;
|
||||
|
||||
if (pOffset != INVALID_OFFSET)
|
||||
{
|
||||
PROC *proc = (PROC *) MAKE_PTR(pOffset);
|
||||
|
||||
if (proc->xid == xid)
|
||||
{
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SpinRelease(SInvalLock);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* GetXmaxRecent -- returns oldest transaction that was running
|
||||
* when all current transaction was started.
|
||||
* It's used by vacuum to decide what deleted
|
||||
* tuples must be preserved in a table.
|
||||
*/
|
||||
void
|
||||
GetXmaxRecent(TransactionId *XmaxRecent)
|
||||
{
|
||||
SISeg *segP = shmInvalBuffer;
|
||||
ProcState *stateP = segP->procState;
|
||||
int index;
|
||||
|
||||
*XmaxRecent = GetCurrentTransactionId();
|
||||
|
||||
SpinAcquire(SInvalLock);
|
||||
|
||||
for (index = 0; index < segP->maxBackends; index++)
|
||||
{
|
||||
SHMEM_OFFSET pOffset = stateP[index].procStruct;
|
||||
|
||||
if (pOffset != INVALID_OFFSET)
|
||||
{
|
||||
PROC *proc = (PROC *) MAKE_PTR(pOffset);
|
||||
TransactionId xmin;
|
||||
|
||||
xmin = proc->xmin; /* we don't use spin-locking in AbortTransaction() ! */
|
||||
if (proc == MyProc || xmin < FirstTransactionId)
|
||||
continue;
|
||||
if (xmin < *XmaxRecent)
|
||||
*XmaxRecent = xmin;
|
||||
}
|
||||
}
|
||||
|
||||
SpinRelease(SInvalLock);
|
||||
}
|
||||
|
||||
/*
|
||||
* GetSnapshotData -- returns information about running transactions.
|
||||
*/
|
||||
Snapshot
|
||||
GetSnapshotData(bool serializable)
|
||||
{
|
||||
Snapshot snapshot = (Snapshot) malloc(sizeof(SnapshotData));
|
||||
SISeg *segP = shmInvalBuffer;
|
||||
ProcState *stateP = segP->procState;
|
||||
int index;
|
||||
int count = 0;
|
||||
|
||||
/* There can be no more than maxBackends active transactions,
|
||||
* so this is enough space:
|
||||
*/
|
||||
snapshot->xip = (TransactionId *)
|
||||
malloc(segP->maxBackends * sizeof(TransactionId));
|
||||
snapshot->xmin = GetCurrentTransactionId();
|
||||
|
||||
SpinAcquire(SInvalLock);
|
||||
|
||||
/*
|
||||
* Unfortunately, we have to call ReadNewTransactionId()
|
||||
* after acquiring SInvalLock above. It's not good because
|
||||
* ReadNewTransactionId() does SpinAcquire(OidGenLockId) but
|
||||
* _necessary_.
|
||||
*/
|
||||
ReadNewTransactionId(&(snapshot->xmax));
|
||||
|
||||
for (index = 0; index < segP->maxBackends; index++)
|
||||
{
|
||||
SHMEM_OFFSET pOffset = stateP[index].procStruct;
|
||||
|
||||
if (pOffset != INVALID_OFFSET)
|
||||
{
|
||||
PROC *proc = (PROC *) MAKE_PTR(pOffset);
|
||||
TransactionId xid;
|
||||
|
||||
/*
|
||||
* We don't use spin-locking when changing proc->xid
|
||||
* in GetNewTransactionId() and in AbortTransaction() !..
|
||||
*/
|
||||
xid = proc->xid;
|
||||
if (proc == MyProc ||
|
||||
xid < FirstTransactionId || xid >= snapshot->xmax)
|
||||
{
|
||||
/*
|
||||
* Seems that there is no sense to store xid >= snapshot->xmax
|
||||
* (what we got from ReadNewTransactionId above) in
|
||||
* snapshot->xip - we just assume that all xacts with such
|
||||
* xid-s are running and may be ignored.
|
||||
*/
|
||||
continue;
|
||||
}
|
||||
if (xid < snapshot->xmin)
|
||||
snapshot->xmin = xid;
|
||||
snapshot->xip[count] = xid;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
if (serializable)
|
||||
MyProc->xmin = snapshot->xmin;
|
||||
/* Serializable snapshot must be computed before any other... */
|
||||
Assert(MyProc->xmin != InvalidTransactionId);
|
||||
|
||||
SpinRelease(SInvalLock);
|
||||
|
||||
snapshot->xcnt = count;
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/sinvaladt.c,v 1.26 1999/09/09 14:56:06 tgl Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/sinvaladt.c,v 1.27 1999/09/24 00:24:35 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -19,6 +19,8 @@
|
||||
#include "miscadmin.h"
|
||||
#include "storage/backendid.h"
|
||||
#include "storage/lmgr.h"
|
||||
#include "storage/proc.h"
|
||||
#include "storage/sinval.h"
|
||||
#include "storage/sinvaladt.h"
|
||||
#include "utils/trace.h"
|
||||
|
||||
@@ -125,6 +127,7 @@ SISegInit(SISeg *segP, int maxBackends)
|
||||
segP->procState[i].nextMsgNum = -1; /* inactive */
|
||||
segP->procState[i].resetState = false;
|
||||
segP->procState[i].tag = InvalidBackendTag;
|
||||
segP->procState[i].procStruct = INVALID_OFFSET;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,8 +164,8 @@ SIBackendInit(SISeg *segP)
|
||||
}
|
||||
}
|
||||
|
||||
/* elog() with spinlock held is probably not too cool, but these
|
||||
* conditions should never happen anyway.
|
||||
/* elog() with spinlock held is probably not too cool, but this
|
||||
* condition should never happen anyway.
|
||||
*/
|
||||
if (stateP == NULL)
|
||||
{
|
||||
@@ -179,9 +182,10 @@ SIBackendInit(SISeg *segP)
|
||||
#endif /* INVALIDDEBUG */
|
||||
|
||||
/* mark myself active, with all extant messages already read */
|
||||
stateP->tag = MyBackendTag;
|
||||
stateP->resetState = false;
|
||||
stateP->nextMsgNum = segP->maxMsgNum;
|
||||
stateP->resetState = false;
|
||||
stateP->tag = MyBackendTag;
|
||||
stateP->procStruct = MAKE_OFFSET(MyProc);
|
||||
|
||||
/* register exit routine to mark my entry inactive at exit */
|
||||
on_shmem_exit(CleanupInvalidationState, (caddr_t) segP);
|
||||
@@ -193,7 +197,8 @@ SIBackendInit(SISeg *segP)
|
||||
* CleanupInvalidationState
|
||||
* Mark the current backend as no longer active.
|
||||
*
|
||||
* This function is called via on_shmem_exit() during backend shutdown.
|
||||
* This function is called via on_shmem_exit() during backend shutdown,
|
||||
* so the caller has NOT acquired the lock for us.
|
||||
*/
|
||||
static void
|
||||
CleanupInvalidationState(int status,
|
||||
@@ -201,13 +206,14 @@ CleanupInvalidationState(int status,
|
||||
{
|
||||
Assert(PointerIsValid(segP));
|
||||
|
||||
/* XXX we probably oughta grab the SInval spinlock for this...
|
||||
* but I think it is safe not to.
|
||||
*/
|
||||
SpinAcquire(SInvalLock);
|
||||
|
||||
segP->procState[MyBackendId - 1].nextMsgNum = -1;
|
||||
segP->procState[MyBackendId - 1].resetState = false;
|
||||
segP->procState[MyBackendId - 1].tag = InvalidBackendTag;
|
||||
segP->procState[MyBackendId - 1].procStruct = INVALID_OFFSET;
|
||||
|
||||
SpinRelease(SInvalLock);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user