Skip to content

Commit b26ed35

Browse files
committed
Avoid deep recursion when assigning XIDs to multiple levels of subxacts.
Backpatch to 8.0. Andres Freund, with cleanup and adjustment for older branches by me.
1 parent b239670 commit b26ed35

File tree

1 file changed

+29
-2
lines changed
  • src/backend/access/transam

1 file changed

+29
-2
lines changed

src/backend/access/transam/xact.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.195.4.6 2010/01/24 21:50:06 tgl Exp $
13+
* $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.195.4.7 2010/07/23 00:43:52 rhaas Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -339,8 +339,35 @@ AssignSubTransactionId(TransactionState s)
339339

340340
Assert(s->parent != NULL);
341341
Assert(s->state == TRANS_INPROGRESS);
342+
343+
/*
344+
* Ensure parent(s) have XIDs, so that a child always has an XID later
345+
* than its parent. Musn't recurse here, or we might get a stack overflow
346+
* if we're at the bottom of a huge stack of subtransactions none of which
347+
* have XIDs yet.
348+
*/
342349
if (!TransactionIdIsValid(s->parent->transactionId))
343-
AssignSubTransactionId(s->parent);
350+
{
351+
TransactionState p = s->parent;
352+
TransactionState *parents;
353+
size_t parentOffset = 0;
354+
355+
parents = palloc(sizeof(TransactionState) * s->nestingLevel);
356+
while (p != NULL && !TransactionIdIsValid(p->transactionId))
357+
{
358+
parents[parentOffset++] = p;
359+
p = p->parent;
360+
}
361+
362+
/*
363+
* This is technically a recursive call, but the recursion will
364+
* never be more than one layer deep.
365+
*/
366+
while (parentOffset != 0)
367+
AssignSubTransactionId(parents[--parentOffset]);
368+
369+
pfree(parents);
370+
}
344371

345372
/*
346373
* Generate a new Xid and record it in PG_PROC and pg_subtrans.

0 commit comments

Comments
 (0)