8000 Avoid deep recursion when assigning XIDs to multiple levels of subxacts. · percona/postgres@202c89c · GitHub
[go: up one dir, main page]

Skip to content

Commit 202c89c

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 b9ded24 commit 202c89c

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.215.2.7 2010/01/24 21:49:58 tgl Exp $
13+
* $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.215.2.8 2010/07/23 00:43:44 rhaas Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -346,8 +346,35 @@ AssignSubTransactionId(TransactionState s)
346346

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

352379
/*
353380
* Generate a new Xid and record it in PG_PROC and pg_subtrans.

0 commit comments

Comments
 (0)
0