1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-21 16:02:15 +03:00

Up to now, SerializableSnapshot and QuerySnapshot are malloc'ed and

free'd for every transaction or statement, respectively.  This patch
puts these data structures into static memory, thus saving a few CPU
cycles and two malloc calls per transaction or (in isolation level
READ COMMITTED) per query.

Manfred Koizar
This commit is contained in:
Bruce Momjian
2003-06-12 01:42:21 +00:00
parent 752a4dac50
commit acd1536d9f
3 changed files with 30 additions and 33 deletions

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/sinval.c,v 1.55 2003/05/27 17:49:46 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/storage/ipc/sinval.c,v 1.56 2003/06/12 01:42:19 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -305,9 +305,8 @@ GetOldestXmin(bool allDbs)
*---------- *----------
*/ */
Snapshot Snapshot
GetSnapshotData(bool serializable) GetSnapshotData(Snapshot snapshot, bool serializable)
{ {
Snapshot snapshot = (Snapshot) malloc(sizeof(SnapshotData));
SISeg *segP = shmInvalBuffer; SISeg *segP = shmInvalBuffer;
ProcState *stateP = segP->procState; ProcState *stateP = segP->procState;
TransactionId xmin; TransactionId xmin;
@ -316,18 +315,29 @@ GetSnapshotData(bool serializable)
int index; int index;
int count = 0; int count = 0;
if (snapshot == NULL) Assert(snapshot != NULL);
elog(ERROR, "Memory exhausted in GetSnapshotData");
/* /*
* Allocating space for MaxBackends xids is usually overkill; * Allocating space for MaxBackends xids is usually overkill;
* lastBackend would be sufficient. But it seems better to do the * lastBackend would be sufficient. But it seems better to do the
* malloc while not holding the lock, so we can't look at lastBackend. * malloc while not holding the lock, so we can't look at lastBackend.
*
* if (snapshot->xip != NULL)
* no need to free and reallocate xip;
*
* We can reuse the old xip array, because MaxBackends does not change
* at runtime.
*/ */
snapshot->xip = (TransactionId *)
malloc(MaxBackends * sizeof(TransactionId));
if (snapshot->xip == NULL) if (snapshot->xip == NULL)
elog(ERROR, "Memory exhausted in GetSnapshotData"); {
/*
* First call for this snapshot
*/
snapshot->xip = (TransactionId *)
malloc(MaxBackends * sizeof(TransactionId));
if (snapshot->xip == NULL)
elog(ERROR, "Memory exhausted in GetSnapshotData");
}
globalxmin = xmin = GetCurrentTransactionId(); globalxmin = xmin = GetCurrentTransactionId();

View File

@ -16,7 +16,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/time/tqual.c,v 1.62 2003/02/23 23:20:52 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/time/tqual.c,v 1.63 2003/06/12 01:42:20 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -30,6 +30,8 @@
static SnapshotData SnapshotDirtyData; static SnapshotData SnapshotDirtyData;
Snapshot SnapshotDirty = &SnapshotDirtyData; Snapshot SnapshotDirty = &SnapshotDirtyData;
static SnapshotData QuerySnapshotData;
static SnapshotData SerializableSnapshotData;
Snapshot QuerySnapshot = NULL; Snapshot QuerySnapshot = NULL;
Snapshot SerializableSnapshot = NULL; Snapshot SerializableSnapshot = NULL;
@ -941,23 +943,16 @@ SetQuerySnapshot(void)
/* 1st call in xaction? */ /* 1st call in xaction? */
if (SerializableSnapshot == NULL) if (SerializableSnapshot == NULL)
{ {
SerializableSnapshot = GetSnapshotData(true); SerializableSnapshot = GetSnapshotData(&SerializableSnapshotData, true);
QuerySnapshot = SerializableSnapshot; QuerySnapshot = SerializableSnapshot;
Assert(QuerySnapshot != NULL); Assert(QuerySnapshot != NULL);
return; return;
} }
if (QuerySnapshot != SerializableSnapshot)
{
free(QuerySnapshot->xip);
free(QuerySnapshot);
QuerySnapshot = NULL;
}
if (XactIsoLevel == XACT_SERIALIZABLE) if (XactIsoLevel == XACT_SERIALIZABLE)
QuerySnapshot = SerializableSnapshot; QuerySnapshot = SerializableSnapshot;
else else
QuerySnapshot = GetSnapshotData(false); QuerySnapshot = GetSnapshotData(&QuerySnapshotData, false);
Assert(QuerySnapshot != NULL); Assert(QuerySnapshot != NULL);
} }
@ -1003,19 +998,11 @@ CopyQuerySnapshot(void)
void void
FreeXactSnapshot(void) FreeXactSnapshot(void)
{ {
if (QuerySnapshot != NULL && QuerySnapshot != SerializableSnapshot) /*
{ * We do not free(QuerySnapshot->xip);
free(QuerySnapshot->xip); * or free(SerializableSnapshot->xip);
free(QuerySnapshot); * they will be reused soon
} */
QuerySnapshot = NULL; QuerySnapshot = NULL;
if (SerializableSnapshot != NULL)
{
free(SerializableSnapshot->xip);
free(SerializableSnapshot);
}
SerializableSnapshot = NULL; SerializableSnapshot = NULL;
} }

View File

@ -8,7 +8,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: tqual.h,v 1.44 2003/02/23 23:20:52 tgl Exp $ * $Id: tqual.h,v 1.45 2003/06/12 01:42:21 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -113,7 +113,7 @@ extern int HeapTupleSatisfiesUpdate(HeapTuple tuple,
extern HTSV_Result HeapTupleSatisfiesVacuum(HeapTupleHeader tuple, extern HTSV_Result HeapTupleSatisfiesVacuum(HeapTupleHeader tuple,
TransactionId OldestXmin); TransactionId OldestXmin);
extern Snapshot GetSnapshotData(bool serializable); extern Snapshot GetSnapshotData(Snapshot snapshot, bool serializable);
extern void SetQuerySnapshot(void); extern void SetQuerySnapshot(void);
extern Snapshot CopyQuerySnapshot(void); extern Snapshot CopyQuerySnapshot(void);
extern void FreeXactSnapshot(void); extern void FreeXactSnapshot(void);