Skip to content

Commit 6f38c43

Browse files
committed
Avoid stack overflow in ShowTransactionStateRec()
The function recurses, but didn't perform stack-depth checks. It's just a debugging aid, so instead of the usual check_stack_depth() call, stop the printing if we'd risk stack overflow. Here's an example of how to test this: (n=1000000; printf "BEGIN;"; for ((i=1;i<=$n;i++)); do printf "SAVEPOINT s$i;"; done; printf "SET log_min_messages = 'DEBUG5'; SAVEPOINT sp;") | psql >/dev/null In the passing, swap building the list of child XIDs and recursing to parent. That saves memory while recursing, reducing the risk of out of memory errors with lots of subtransactions. The saving is not very significant in practice, but this order seems more logical anyway. Report by Egor Chindyaskin and Alexander Lakhin. Discussion: https://www.postgresql.org/message-id/1672760457.940462079%40f306.i.mail.ru Author: Heikki Linnakangas Reviewed-by: Robert Haas, Andres Freund, Alexander Korotkov
1 parent fefd9a3 commit 6f38c43

File tree

1 file changed

+15
-6
lines changed
  • src/backend/access/transam

1 file changed

+15
-6
lines changed

src/backend/access/transam/xact.c

+15-6
Original file line numberDiff line numberDiff line change
@@ -5583,8 +5583,22 @@ ShowTransactionStateRec(const char *str, TransactionState s)
55835583
{
55845584
StringInfoData buf;
55855585

5586-
initStringInfo(&buf);
5586+
if (s->parent)
5587+
{
5588+
/*
5589+
* Since this function recurses, it could be driven to stack overflow.
5590+
* This is just a debugging aid, so we can leave out some details
5591+
* instead of erroring out with check_stack_depth().
5592+
*/
5593+
if (stack_is_too_deep())
5594+
ereport(DEBUG5,
5595+
(errmsg_internal("%s(%d): parent omitted to avoid stack overflow",
5596+
str, s->nestingLevel)));
5597+
else
5598+
ShowTransactionStateRec(str, s->parent);
5599+
}
55875600

5601+
initStringInfo(&buf);
55885602
if (s->nChildXids > 0)
55895603
{
55905604
int i;
@@ -5593,10 +5607,6 @@ ShowTransactionStateRec(const char *str, TransactionState s)
55935607
for (i = 1; i < s->nChildXids; i++)
55945608
appendStringInfo(&buf, " %u", s->childXids[i]);
55955609
}
5596-
5597-
if (s->parent)
5598-
ShowTransactionStateRec(str, s->parent);
5599-
56005610
ereport(DEBUG5,
56015611
(errmsg_internal("%s(%d) name: %s; blockState: %s; state: %s, xid/subid/cid: %u/%u/%u%s%s",
56025612
str, s->nestingLevel,
@@ -5608,7 +5618,6 @@ ShowTransactionStateRec(const char *str, TransactionState s)
56085618
(unsigned int) currentCommandId,
56095619
currentCommandIdUsed ? " (used)" : "",
56105620
buf.data)));
5611-
56125621
pfree(buf.data);
56135622
}
56145623

0 commit comments

Comments
 (0)