Hi Alberto,
I tried your idea on top of v03: keep the state in backend_progress.c
and clean up only on abort. It removes the rows that Adam and you
found after CREATE INDEX CONCURRENTLY and REINDEX CONCURRENTLY, and it
also fixes the cursor case from my last mail, without the change I
proposed there. The part that needs care is the abort of a
subtransaction.
One correction to my last mail: the tree I described there leaves
Adam's rows behind too. "Fixed all of these" only covered my cases.
On Wed, Nov 25, 2026 at 12:59 PM Alberto Piai
<[email protected]> wrote:
> Perhaps it would be enough to reset the state only on abort, so that
> commands like ReindexRelationConcurrently() can keep reporting one
> command with multiple phases?
If AbortSubTransaction() empties the whole stack, an error caught
inside a command ends that command's report:
CREATE FUNCTION f(x int) RETURNS int LANGUAGE plpgsql IMMUTABLE AS
$$ BEGIN RETURN 1 / (x % 2);
EXCEPTION WHEN division_by_zero THEN RETURN 0; END $$;
CREATE TABLE t AS SELECT generate_series(1, 300000) a;
CREATE INDEX ON t (f(a));
The CREATE INDEX row goes away at the first caught error. Master
does the same; v03 keeps the row. Popping only the top entry gives
the same result here, because the subtransaction started no command.
Popping one entry fails the other way when the subtransaction started
two. A COPY into t22 has a trigger that runs a COPY into t23 inside
BEGIN ... EXCEPTION, and t23's own trigger runs a COPY that fails.
Only one entry is popped, so for the rest of the outer COPY the view
shows t23, and when it ends a t22 row stays behind, also after a
later CREATE INDEX.
What worked: count the commands started so far, save that count when
a subtransaction starts, and on its abort end the commands started
after it. With that, both cases above, Adam's four, your REINDEX
INDEX CONCURRENTLY and my cursor case leave no row. It still needs
one field in TransactionStateData, but nothing that must be kept in
step with the stack. The diff is attached, on top of Antonin's two
patches. make check, isolation, file_fdw and injection_points pass,
with progress.spec added to the schedule.
> however I'm not sure ReindexRelationConcurrently() needs to create
> all these subcommands. It would be enough to update the current
> phase of the last command, as it was doing before the patch.
I agree, and the row during the command shows why. With another
session holding ACCESS SHARE on the table, REINDEX INDEX CONCURRENTLY
shows command NULL and index_relid 0 while it waits for that reader,
and between some phases there is no row at all. Master shows REINDEX
CONCURRENTLY the whole time. Fixing the leftover row does not change
this, and with your diff the wait for old snapshots shows command NULL
too.
About Adam's parallel BRIN build: v03 adds a start_command() /
end_command() pair around the scan in _brin_parallel_scan_and_build(),
which runs in the leader and in each worker. In the leader the new
entry starts with all parameters at zero, and the view prefers it to
the outer one, so the row shows command NULL and "initializing" while
the block counters move. Without those lines the rows match master.
The file_fdw problems from my last mail are separate: they come from
active_nodes and pgstat_track_progress in file_fdw, and they show up
with all of these approaches.
Regards,
Manu
diff --git a/src/backend/access/transam/xact.c
b/src/backend/access/transam/xact.c
index 3edc84eafda..5bd4d48988e 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -218,8 +218,7 @@ typedef struct TransactionStateData
bool parallelChildXact; /* is any parent transaction
parallel? */
bool chain; /* start a new block after this
one */
bool topXidLogged; /* for a subxact: is top-level XID
logged? */
- int progress_depth; /* progress tracking state
before subxact
- * started */
+ uint64 progress_mark; /* last progress command started before
*/
struct TransactionStateData *parent; /* back link to parent */
} TransactionStateData;
@@ -2270,12 +2269,6 @@ StartTransaction(void)
*/
s->state = TRANS_INPROGRESS;
- /*
- * Top transaction always terminates progress tracking on abort, so this
- * field is not needed, but be tidy.
- */
- s->progress_depth = 0;
-
/* Schedule transaction timeout */
if (TransactionTimeout > 0)
enable_timeout_after(TRANSACTION_TIMEOUT, TransactionTimeout);
@@ -5191,8 +5184,7 @@ StartSubTransaction(void)
AfterTriggerBeginSubXact();
s->state = TRANS_INPROGRESS;
-
- s->progress_depth = 0;
+ s->progress_mark = pgstat_progress_mark();
/*
* Call start-of-subxact callbacks
@@ -5353,7 +5345,7 @@ AbortSubTransaction(void)
WaitLSNCleanup();
pgstat_report_wait_end();
- pgstat_progress_end_command(true);
+ pgstat_progress_end_since(s->progress_mark);
pgaio_error_cleanup();
@@ -6558,38 +6550,3 @@ xact_redo(XLogReaderState *record)
else
elog(PANIC, "xact_redo: unknown op code %u", info);
}
-
-
-/*
- * XactStartProgressReporting
- *
- * Start progress reporting in the current transaction.
- */
-void
-XactStartProgressReporting(void)
-{
- TransactionState s = CurrentTransactionState;
-
- s->progress_depth++;
-}
-
-/*
- * XactEndProgressReporting
- *
- * End progress reporting in the current transaction.
- *
- * Return true if the nesting level was decreased, false if it's already zero.
- */
-bool
-XactEndProgressReporting(void)
-{
- TransactionState s = CurrentTransactionState;
-
- if (s->progress_depth == 0)
- return false;
-
- Assert(s->progress_depth > 0);
- s->progress_depth--;
-
- return true;
-}
diff --git a/src/backend/utils/activity/backend_progress.c
b/src/backend/utils/activity/backend_progress.c
index fc9de95e7f0..88cde2f9a80 100644
--- a/src/backend/utils/activity/backend_progress.c
+++ b/src/backend/utils/activity/backend_progress.c
@@ -11,7 +11,6 @@
#include "postgres.h"
#include "access/parallel.h"
-#include "access/xact.h"
#include "libpq/pqformat.h"
#include "storage/proc.h"
#include "utils/backend_progress.h"
@@ -36,6 +35,8 @@ typedef struct PgBackendProgresElt
{
PgBackendProgress progress;
+ uint64 serial; /* start order of the saved
command */
+
struct PgBackendProgresElt *parent;
} PgBackendProgresElt;
@@ -55,6 +56,10 @@ typedef struct PgBackendProgresElt
*/
static PgBackendProgresElt *parent_commands = NULL;
+/* Commands started so far, and start order of the current one. */
+static uint64 progress_serial = 0;
+static uint64 current_serial = 0;
+
/*-----------
* pgstat_progress_start_command() -
*
@@ -89,6 +94,7 @@ pgstat_progress_start_command(ProgressCommandType cmdtype,
Oid relid)
if (beentry->st_progress.command != PROGRESS_COMMAND_INVALID)
{
parent_new->progress = beentry->st_progress;
+ parent_new->serial = current_serial;
parent_new->parent = parent_commands;
parent_commands = parent_new;
@@ -100,6 +106,7 @@ pgstat_progress_start_command(ProgressCommandType cmdtype,
Oid relid)
beentry->st_progress_top = beentry->st_progress;
}
beentry->st_progress.command = cmdtype;
+ current_serial = ++progress_serial;
beentry->st_progress.command_target = relid;
for (int i = 0; i < PGSTAT_NUM_PROGRESS_PARAM; i++)
beentry->st_progress.param[i] = 0;
@@ -107,7 +114,6 @@ pgstat_progress_start_command(ProgressCommandType cmdtype,
Oid relid)
if (parent_new)
pfree(parent_new);
- XactStartProgressReporting();
}
/*-----------
@@ -240,7 +246,7 @@ pgstat_progress_end_command(bool xact)
INJECTION_POINT("progress-end-command-nested", NULL);
PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
- while (XactEndProgressReporting())
+ for (;;)
{
if (parent_commands)
{
@@ -249,6 +255,7 @@ pgstat_progress_end_command(bool xact)
/* Restore the status of the parent command. */
parent = parent_commands;
beentry->st_progress = parent->progress;
+ current_serial = parent->serial;
parent_commands = parent->parent;
pfree(parent);
@@ -267,14 +274,41 @@ pgstat_progress_end_command(bool xact)
/* End the top-level command. */
beentry->st_progress.command = PROGRESS_COMMAND_INVALID;
beentry->st_progress.command_target = InvalidOid;
+ current_serial = 0;
}
/*
* If this is not the end of transaction, the user is only
interested
* in ending the inner-most command.
*/
- if (!xact)
+ if (!xact ||
+ beentry->st_progress.command ==
PROGRESS_COMMAND_INVALID)
break;
}
PGSTAT_END_WRITE_ACTIVITY(beentry);
}
+
+/*
+ * Mark to pass to pgstat_progress_end_since() later.
+ */
+uint64
+pgstat_progress_mark(void)
+{
+ return progress_serial;
+}
+
+/*
+ * End the commands started after the mark was taken (subtransaction abort).
+ */
+void
+pgstat_progress_end_since(uint64 mark)
+{
+ while (current_serial > mark)
+ {
+ uint64 before = current_serial;
+
+ pgstat_progress_end_command(false);
+ if (current_serial == before)
+ break;
+ }
+}
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index 0b584dba6b7..a8cbdf247c8 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -535,7 +535,4 @@ extern void EnterParallelMode(void);
extern void ExitParallelMode(void);
extern bool IsInParallelMode(void);
-extern void XactStartProgressReporting(void);
-extern bool XactEndProgressReporting(void);
-
#endif /* XACT_H */
diff --git a/src/include/utils/backend_progress.h
b/src/include/utils/backend_progress.h
index 8fb38c3f372..3d1ca021c12 100644
--- a/src/include/utils/backend_progress.h
+++ b/src/include/utils/backend_progress.h
@@ -42,6 +42,8 @@ extern void pgstat_progress_parallel_incr_param(int index,
int64 incr);
extern void pgstat_progress_update_multi_param(int nparam, const int *index,
const int64 *val);
extern void pgstat_progress_end_command(bool xact);
+extern uint64 pgstat_progress_mark(void);
+extern void pgstat_progress_end_since(uint64 mark);
#endif /* BACKEND_PROGRESS_H */