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

Remove global variable scanCommandId in favor of storing a command ID

in snapshots, per my proposal of a few days ago.  Also, tweak heapam.c
routines (heap_insert, heap_update, heap_delete, heap_mark4update) to
be passed the command ID to use, instead of doing GetCurrentCommandID.
For catalog updates they'll still get passed current command ID, but
for updates generated from the main executor they'll get passed the
command ID saved in the snapshot the query is using.  This should fix
some corner cases associated with functions and triggers that advance
current command ID while an outer query is still in progress.
This commit is contained in:
Tom Lane
2002-05-21 22:05:55 +00:00
parent 0a2682445e
commit 959e61e917
37 changed files with 191 additions and 235 deletions

View File

@ -27,7 +27,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.161 2002/05/12 20:10:02 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.162 2002/05/21 22:05:55 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -101,6 +101,7 @@ TupleDesc
ExecutorStart(QueryDesc *queryDesc, EState *estate)
{
TupleDesc result;
Snapshot es_snapshot;
/* sanity checks */
Assert(queryDesc != NULL);
@ -114,22 +115,28 @@ ExecutorStart(QueryDesc *queryDesc, EState *estate)
}
/*
* Make our own private copy of the current queries snapshot data
* Make our own private copy of the current query snapshot data.
*
* This "freezes" our idea of which tuples are good and which are not
* for the life of this query, even if it outlives the current command
* and current snapshot.
*/
if (QuerySnapshot == NULL)
estate->es_snapshot = NULL;
else
if (QuerySnapshot == NULL) /* should be set already, but... */
SetQuerySnapshot();
es_snapshot = (Snapshot) palloc(sizeof(SnapshotData));
memcpy(es_snapshot, QuerySnapshot, sizeof(SnapshotData));
if (es_snapshot->xcnt > 0)
{
estate->es_snapshot = (Snapshot) palloc(sizeof(SnapshotData));
memcpy(estate->es_snapshot, QuerySnapshot, sizeof(SnapshotData));
if (estate->es_snapshot->xcnt > 0)
{
estate->es_snapshot->xip = (TransactionId *)
palloc(estate->es_snapshot->xcnt * sizeof(TransactionId));
memcpy(estate->es_snapshot->xip, QuerySnapshot->xip,
estate->es_snapshot->xcnt * sizeof(TransactionId));
}
es_snapshot->xip = (TransactionId *)
palloc(es_snapshot->xcnt * sizeof(TransactionId));
memcpy(es_snapshot->xip, QuerySnapshot->xip,
es_snapshot->xcnt * sizeof(TransactionId));
}
else
es_snapshot->xip = NULL;
estate->es_snapshot = es_snapshot;
/*
* Initialize the plan
@ -1031,7 +1038,8 @@ lnext: ;
erm->resname);
tuple.t_self = *((ItemPointer) DatumGetPointer(datum));
test = heap_mark4update(erm->relation, &tuple, &buffer);
test = heap_mark4update(erm->relation, &tuple, &buffer,
estate->es_snapshot->curcid);
ReleaseBuffer(buffer);
switch (test)
{
@ -1163,7 +1171,8 @@ ExecRetrieve(TupleTableSlot *slot,
*/
if (estate->es_into_relation_descriptor != NULL)
{
heap_insert(estate->es_into_relation_descriptor, tuple);
heap_insert(estate->es_into_relation_descriptor, tuple,
estate->es_snapshot->curcid);
IncrAppended();
}
@ -1239,7 +1248,8 @@ ExecAppend(TupleTableSlot *slot,
/*
* insert the tuple
*/
newId = heap_insert(resultRelationDesc, tuple);
newId = heap_insert(resultRelationDesc, tuple,
estate->es_snapshot->curcid);
IncrAppended();
(estate->es_processed)++;
@ -1301,7 +1311,9 @@ ExecDelete(TupleTableSlot *slot,
* delete the tuple
*/
ldelete:;
result = heap_delete(resultRelationDesc, tupleid, &ctid);
result = heap_delete(resultRelationDesc, tupleid,
&ctid,
estate->es_snapshot->curcid);
switch (result)
{
case HeapTupleSelfUpdated:
@ -1433,7 +1445,9 @@ lreplace:;
/*
* replace the heap tuple
*/
result = heap_update(resultRelationDesc, tupleid, tuple, &ctid);
result = heap_update(resultRelationDesc, tupleid, tuple,
&ctid,
estate->es_snapshot->curcid);
switch (result)
{
case HeapTupleSelfUpdated:

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/executor/functions.c,v 1.50 2002/05/12 20:10:02 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/executor/functions.c,v 1.51 2002/05/21 22:05:55 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -465,7 +465,6 @@ fmgr_sql(PG_FUNCTION_ARGS)
SQLFunctionCachePtr fcache;
execution_state *es;
Datum result = 0;
CommandId savedId;
/*
* Switch to context in which the fcache lives. This ensures that
@ -474,14 +473,6 @@ fmgr_sql(PG_FUNCTION_ARGS)
*/
oldcontext = MemoryContextSwitchTo(fcinfo->flinfo->fn_mcxt);
/*
* Before we start do anything we must save CurrentScanCommandId to
* restore it before return to upper Executor. Also, we have to set
* CurrentScanCommandId equal to CurrentCommandId. - vadim 08/29/97
*/
savedId = GetScanCommandId();
SetScanCommandId(GetCurrentCommandId());
/*
* Initialize fcache and execution state if first time through.
*/
@ -515,11 +506,6 @@ fmgr_sql(PG_FUNCTION_ARGS)
es = es->next;
}
/*
* Restore outer command ID.
*/
SetScanCommandId(savedId);
/*
* If we've gone through every command in this function, we are done.
*/

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/executor/spi.c,v 1.69 2002/04/15 05:22:04 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/executor/spi.c,v 1.70 2002/05/21 22:05:55 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -106,11 +106,7 @@ SPI_connect(void)
/* ... and switch to procedure's context */
_SPI_current->savedcxt = MemoryContextSwitchTo(_SPI_current->procCxt);
_SPI_current->savedId = GetScanCommandId();
SetScanCommandId(GetCurrentCommandId());
return SPI_OK_CONNECT;
}
int
@ -129,8 +125,6 @@ SPI_finish(void)
MemoryContextDelete(_SPI_current->execCxt);
MemoryContextDelete(_SPI_current->procCxt);
SetScanCommandId(_SPI_current->savedId);
/*
* After _SPI_begin_call _SPI_connected == _SPI_curid. Now we are
* closing connection to SPI and returning to upper Executor and so
@ -1233,7 +1227,6 @@ _SPI_cursor_operation(Portal portal, bool forward, int count,
EState *estate;
MemoryContext oldcontext;
ScanDirection direction;
CommandId savedId;
CommandDest olddest;
/* Check that the portal is valid */
@ -1260,14 +1253,6 @@ _SPI_cursor_operation(Portal portal, bool forward, int count,
olddest = querydesc->dest;
querydesc->dest = dest;
/*
* Restore the scanCommandId that was current when the cursor was
* opened. This ensures that we see the same tuples throughout the
* execution of the cursor.
*/
savedId = GetScanCommandId();
SetScanCommandId(PortalGetCommandId(portal));
/* Run the executor like PerformPortalFetch and remember states */
if (forward)
{
@ -1300,11 +1285,6 @@ _SPI_cursor_operation(Portal portal, bool forward, int count,
_SPI_current->processed = estate->es_processed;
/*
* Restore outer command ID.
*/
SetScanCommandId(savedId);
/* Restore the old command destination and switch back to callers */
/* memory context */
querydesc->dest = olddest;