| From: | Antonin Houska <ah(at)cybertec(dot)at> |
|---|---|
| To: | "Alberto Piai" <alberto(dot)piai(at)gmail(dot)com> |
| Cc: | "Manu" <manuelreyesbravo(at)gmail(dot)com>, "Rahila Syed" <rahilasyed90(at)gmail(dot)com>, "Fujii Masao" <masao(dot)fujii(at)gmail(dot)com>, alvherre(at)kurilemu(dot)de, pgsql-hackers(at)lists(dot)postgresql(dot)org, bharath(dot)rupireddyforpostgres(at)gmail(dot)com, mihailnikalayeu(at)gmail(dot)com, adam8157(at)gmail(dot)com |
| Subject: | Re: Allow progress tracking of sub-commands |
| Date: | 2026-11-26 16:33:19 |
| Message-ID: | 115231.1795710799@chrudos |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Alberto Piai <alberto(dot)piai(at)gmail(dot)com> wrote:
> I started reviewing v3, and noticed a couple of problems and have some
> thoughts:
>
> ..
> phase | waiting for readers before dropping
> It seems like the calls to start/end command in
> ReindexRelationConcurrently() are not balanced, leaving an entry stuck
> there after the command has completed.
>
> Re-running with the following fixes it:
> ```
> diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
> index ce6900c5b74..be29ef56b5c 100644
> --- a/src/backend/commands/indexcmds.c
> +++ b/src/backend/commands/indexcmds.c
> @@ -4456,6 +4456,7 @@ ReindexRelationConcurrently(const ReindexStmt *stmt,
> Oid relationOid, const Rein
> * transaction, and do our wait before any snapshot has been
> taken in
> * it.
> */
> + pgstat_progress_end_command(false);
> CommitTransactionCommand();
> StartTransactionCommand();
>
> @@ -4468,6 +4469,7 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
> * Because we don't take a snapshot or Xid in this transaction,
> * there's no need to set the PROC_IN_SAFE_IC flag here.
> */
> + pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, newidx->tableId);
> pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
> PROGRESS_CREATEIDX_PHASE_WAIT_3);
> WaitForOlderSnapshots(limitXmin, true);
I'm not sure I understand. Per backend/catalog/system_views.sql the phase
number is 9:
WHEN 9 THEN 'waiting for readers before dropping'
which is WAIT_5 (progress.h)
#define PROGRESS_CREATEIDX_PHASE_WAIT_5 9
The progress monitoring is turned off right after this waiting is complete
(indexcmds.c):
/*
* Phase 6 of REINDEX CONCURRENTLY
*
* Drop the old indexes.
*/
INJECTION_POINT("reindex-relation-concurrently-before-drop", NULL);
pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
PROGRESS_CREATEIDX_PHASE_WAIT_5);
WaitForLockersMultiple(lockTags, AccessExclusiveLock, true);
pgstat_progress_end_command(false);
> @@ -4595,6 +4597,7 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, =
> Oid relationOid, const Rein
> }
>
> /* Commit this transaction to make the updates visible. */
> + pgstat_progress_end_command(false);
> CommitTransactionCommand();
> StartTransactionCommand();
>
> @@ -4611,6 +4614,7 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, =
> Oid relationOid, const Rein
> */
>
> INJECTION_POINT("reindex-relation-concurrently-before-drop", NULL);
> + pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, relati=
> onOid);
> pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
> PROGRESS_CREATEIDX_PHASE_WAIT_5);
> WaitForLockersMultiple(lockTags, AccessExclusiveLock, true);
> ```
Likewise, this change is located before WAIT_5 starts, so it should not affect
for how long it is displayed.
> 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.
Right, I only wanted to call pgstat_progress_end_command() and
pgstat_progress_start_command() where it was obvious that another index is to
be processed. (Or when the previous reporting was related to the heap and the
next will be for an index, or vice versa.)
Maybe the waiting really took long time during one test run, and the somehow
happened to be short during the next run?
> This is not possible anymore (correct me if I'm not reading this right)
> because StartTransaction() and StartSubTransaction() reset
> CurrentTransactionState->progress_depth to 0. One problem with this is
> that progress_depth and the actual stack in beentry->st_progress can get
> out of sync, but the fundamental change here is that progress reporting
> is now tied to committing/starting a transaction, whereas before it was
> only reset in Abort(Sub)Transaction.
Commit(Sub)Transaction() does not end the monitoring. In this case, the
explicit call of pgstat_progress_end_command() should do that.
> 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?
Right.
> In fact, I wonder why do we need to expose this in xact.c at all? Would
> it be possible to keep all state internal to backend_progress.c and
> cleanup the whole stack / only the inner-most item depending only on
> whether the user called ...end_command(xact) with xact true or false?
>
> If we reset the state only on abort, we could probably do this without
> touching xact.c and the transaction state, which would also get rid of
> the problem of it going out of sync with the stack in the backend state.
I agree, xact.c should not care, except for calling callbacks
AtStart_BackendProgress(), AtEOXact_BackendProgress(), ... Please see v04.
--
Antonin Houska
Web: https://www.cybertec-postgresql.com
| Attachment | Content-Type | Size |
|---|---|---|
| v04-0001-Allow-progress-tracking-of-sub-commands.patch | application/mbox | 49.2 KB |
| v04-0002-Do-not-report-progress-of-two-or-more-COPY-commands-.patch | application/mbox | 5.1 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Diego | 2026-11-26 16:48:01 | Re: [PATCH] libpq: Add PQpassfileLookup() |
| Previous Message | Greg Burd | 2026-11-26 16:24:42 | Re: Interrupts vs signals |