mirror of
https://github.com/postgres/postgres.git
synced 2025-07-08 11:42:09 +03:00
Restructure subtransaction handling to reduce resource consumption,
as per recent discussions. Invent SubTransactionIds that are managed like CommandIds (ie, counter is reset at start of each top transaction), and use these instead of TransactionIds to keep track of subtransaction status in those modules that need it. This means that a subtransaction does not need an XID unless it actually inserts/modifies rows in the database. Accordingly, don't assign it an XID nor take a lock on the XID until it tries to do that. This saves a lot of overhead for subtransactions that are only used for error recovery (eg plpgsql exceptions). Also, arrange to release a subtransaction's XID lock as soon as the subtransaction exits, in both the commit and abort cases. This avoids holding many unique locks after a long series of subtransactions. The price is some additional overhead in XactLockTableWait, but that seems acceptable. Finally, restructure the state machine in xact.c to have a more orthogonal set of states for subtransactions.
This commit is contained in:
@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/commands/sequence.c,v 1.116 2004/08/29 05:06:41 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/commands/sequence.c,v 1.117 2004/09/16 16:58:28 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -23,6 +23,8 @@
|
||||
#include "miscadmin.h"
|
||||
#include "utils/acl.h"
|
||||
#include "utils/builtins.h"
|
||||
#include "utils/resowner.h"
|
||||
|
||||
|
||||
/*
|
||||
* We don't want to log each fetching of a value from a sequence,
|
||||
@ -754,10 +756,21 @@ static void
|
||||
init_sequence(RangeVar *relation, SeqTable *p_elm, Relation *p_rel)
|
||||
{
|
||||
Oid relid = RangeVarGetRelid(relation, false);
|
||||
TransactionId thisxid = GetCurrentTransactionId();
|
||||
SeqTable elm;
|
||||
TransactionId thisxid = GetTopTransactionId();
|
||||
volatile SeqTable elm;
|
||||
Relation seqrel;
|
||||
|
||||
/*
|
||||
* Open the sequence relation.
|
||||
*/
|
||||
seqrel = relation_open(relid, NoLock);
|
||||
|
||||
if (seqrel->rd_rel->relkind != RELKIND_SEQUENCE)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
||||
errmsg("\"%s\" is not a sequence",
|
||||
relation->relname)));
|
||||
|
||||
/* Look to see if we already have a seqtable entry for relation */
|
||||
for (elm = seqtab; elm != NULL; elm = elm->next)
|
||||
{
|
||||
@ -765,21 +778,6 @@ init_sequence(RangeVar *relation, SeqTable *p_elm, Relation *p_rel)
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* Open the sequence relation, acquiring AccessShareLock if we don't
|
||||
* already have a lock in the current xact.
|
||||
*/
|
||||
if (elm == NULL || elm->xid != thisxid)
|
||||
seqrel = relation_open(relid, AccessShareLock);
|
||||
else
|
||||
seqrel = relation_open(relid, NoLock);
|
||||
|
||||
if (seqrel->rd_rel->relkind != RELKIND_SEQUENCE)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
||||
errmsg("\"%s\" is not a sequence",
|
||||
relation->relname)));
|
||||
|
||||
/*
|
||||
* Allocate new seqtable entry if we didn't find one.
|
||||
*
|
||||
@ -799,14 +797,42 @@ init_sequence(RangeVar *relation, SeqTable *p_elm, Relation *p_rel)
|
||||
(errcode(ERRCODE_OUT_OF_MEMORY),
|
||||
errmsg("out of memory")));
|
||||
elm->relid = relid;
|
||||
elm->xid = InvalidTransactionId;
|
||||
/* increment is set to 0 until we do read_info (see currval) */
|
||||
elm->last = elm->cached = elm->increment = 0;
|
||||
elm->next = seqtab;
|
||||
seqtab = elm;
|
||||
}
|
||||
|
||||
/* Flag that we have a lock in the current xact. */
|
||||
elm->xid = thisxid;
|
||||
/*
|
||||
* If we haven't touched the sequence already in this transaction,
|
||||
* we need to acquire AccessShareLock. We arrange for the lock to
|
||||
* be owned by the top transaction, so that we don't need to do it
|
||||
* more than once per xact.
|
||||
*/
|
||||
if (elm->xid != thisxid)
|
||||
{
|
||||
ResourceOwner currentOwner;
|
||||
|
||||
currentOwner = CurrentResourceOwner;
|
||||
PG_TRY();
|
||||
{
|
||||
CurrentResourceOwner = TopTransactionResourceOwner;
|
||||
|
||||
LockRelation(seqrel, AccessShareLock);
|
||||
}
|
||||
PG_CATCH();
|
||||
{
|
||||
/* Ensure CurrentResourceOwner is restored on error */
|
||||
CurrentResourceOwner = currentOwner;
|
||||
PG_RE_THROW();
|
||||
}
|
||||
PG_END_TRY();
|
||||
CurrentResourceOwner = currentOwner;
|
||||
|
||||
/* Flag that we have a lock in the current xact. */
|
||||
elm->xid = thisxid;
|
||||
}
|
||||
|
||||
*p_elm = elm;
|
||||
*p_rel = seqrel;
|
||||
|
Reference in New Issue
Block a user