From 12f3758567dc1c921b96285a43c68c821012e9c0 Mon Sep 17 00:00:00 2001 From: Andrey Kolkov Date: Fri, 27 Nov 2026 17:26:19 +0700 Subject: [PATCH] fix(postgres): skip a stale index while another session builds on its table A REINDEX CONCURRENTLY started by an init-job run keeps going after the Job hits its deadline, because its backend only notices the lost client when the rebuild ends. The next run looks the index up by OID but hands REINDEX its name; REINDEX then waits behind the orphaned rebuild, and once that rebuild swaps in the new index, the name resolves to it and the already rebuilt index is rebuilt again, holding its table until the new run's own deadline. Skip such an index and report it like a failed rebuild while pg_stat_progress_create_index shows another backend building an index of its table. The check is the first statement of the session, before the index is looked up: a rebuild that ended before it has removed the index the session looks for, and an orphaned backend never starts another statement after it. The next run then finds the rebuilt index above the resume mark and treats it as done. The check comes before an in-place attempt is recorded, so a skip never counts towards leaving an exclusion constraint's index to an operator, and before the _ccnew leftover count, which the build in progress would otherwise inflate. Assisted-By: LLM Signed-off-by: Andrey Kolkov --- packages/apps/postgres/templates/init-script.yaml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/apps/postgres/templates/init-script.yaml b/packages/apps/postgres/templates/init-script.yaml index 35b4d3043..efbbfd6eb 100644 --- a/packages/apps/postgres/templates/init-script.yaml +++ b/packages/apps/postgres/templates/init-script.yaml @@ -648,6 +648,17 @@ stringData: minute in reserve: the deadline counts from the start of the Job, $SECONDS from the start of this script. + Such a rebuild outlives the run that started it: its backend does not + notice that the Job is gone until the rebuild ends. REINDEX finds an + index by name, and once it has waited behind that rebuild the name + belongs to the index the rebuild put in place, which would then be + rebuilt a second time. An index is therefore skipped, and reported like + a failed rebuild, while another session builds an index of its table; + the next run finds the new index above the mark. That check comes + first in the session, before the index is looked up: a rebuild that + ended before it has removed the index the session looks for, and an + orphaned one cannot start another after it. + One index must be rebuilt within one run of the Job, and a retry would otherwise start a rebuild that cannot finish over and over. An interrupted or failed REINDEX CONCURRENTLY leaves an invalid index with @@ -782,6 +793,10 @@ stringData: SET client_min_messages = error; SET search_path = pg_catalog, pg_temp; SET lock_timeout = :'lock_wait'; + SELECT EXISTS (SELECT FROM pg_index i JOIN pg_stat_progress_create_index p ON p.relid = i.indrelid JOIN pg_database d ON d.oid = p.datid WHERE i.indexrelid = :'index'::oid AND d.datname = current_database() AND p.pid <> pg_backend_pid()) AS building \gset + \if :building + SELECT format('DO %L', format('BEGIN RAISE EXCEPTION %L, %L; END', '%', format('index %s in database %s was skipped: another session is building an index of its table, as a rebuild left by an earlier run of the init-job does; the next run of the init-job carries on', :'index'::oid::regclass, current_database()))) \gexec + \endif SELECT count(*) = 1 AS present FROM pg_index WHERE indexrelid = :'index'::oid \gset \if :present SELECT coalesce(current_setting('cozystack.collation_repair', true), '') AS recorded \gset