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 */