pg.ddx.io reorg/pg_repack GitHub issues and pull requests
help / color / mirror / Atom feed[reorg/pg_repack] PR #489: Cancel in-progress index builds on SIGINT with --jobs
4+ messages / 2 participants
[nested] [flat]
* [reorg/pg_repack] PR #489: Cancel in-progress index builds on SIGINT with --jobs
@ 2026-11-09 23:26 "ritojoao (@ritojoao)" <noreply+ritojoao@github.com>
0 siblings, 0 replies; 4+ messages in thread
From: ritojoao (@ritojoao) @ 2026-11-09 23:26 UTC (permalink / raw)
To: reorg/pg_repack <noreply+reorg-pg_repack@github.com>
Fixes #486.
## Problem
With `--jobs > 1`, interrupting pg_repack (SIGINT / Ctrl-C) during the index phase does not stop it:
- the index builds run on the worker connections, which `setup_workers()` deliberately keeps out of
`pgut_connections`, so `on_interrupt()` never sends them a cancel request;
- `rebuild_indexes()` ignores `EINTR` and the `interrupted` flag after `poll()`/`select()`, so it keeps
handing the remaining indexes to the freed workers.
pg_repack exits only after every index of the table is built. If it is killed in the meantime (e.g. a
Kubernetes grace period), the worker `CREATE INDEX` backends keep running on the server. The `XXX` comment
in `rebuild_indexes()` already described this.
## Fix
After each `poll()`/`select()` in `rebuild_indexes()`, check `interrupted`. When set,
`cancel_index_builds()` sends a cancel request (`PQgetCancel`/`PQcancel`) to every worker with a build in
progress, then drains each worker's results so no `CREATE INDEX` is left holding locks on the temp table.
Then `CHECK_FOR_INTERRUPTS()` raises the same `FATAL: interrupted` as before, and the usual cleanup
callback drops the temporary objects.
The worker connections stay out of `pgut_connections`; the cancel is done from the loop, not from the
signal handler. On Windows, where the console handler does not interrupt `select()`, the loop notices the
flag within `POLL_TIMEOUT` (3 s).
## Testing
Reproduced on PostgreSQL 16.15 and 17.11 (official Docker images) with a table whose index builds take
~15 s each (expression indexes on a plpgsql function calling `pg_sleep`), `--jobs=2`, SIGINT 2 s into the
index phase:
| Build | PG | Time to exit after SIGINT | Leftovers |
|---|---|---|---|
| master | 17 | 62.8 s (assigned index #3 after the signal) | none |
| this PR | 17 | 0.1 s | none |
| this PR | 16 | 0.1 s | none |
"Leftovers" = objects in schema `repack`, `repack_trigger` on the table, active `CREATE INDEX` backends.
`--jobs=1` behaves as before.
On master (SIGINT sent at 22:52:17.849):
22:52:48.104 INFO: Assigning worker 1 to build index #3: CREATE INDEX index_16780 ...
22:53:20.591 INFO: Command finished in worker 1: CREATE INDEX index_16780 ...
22:53:20.593 FATAL: interrupted
With this PR:
WARNING: Cancel request sent to worker 1
WARNING: Cancel request sent to worker 0
FATAL: interrupted
`make installcheck` passes on 16 and 17. I did not add a regression test: the suite runs pg_repack
synchronously through `pg_regress`, and a SIGINT test would need a background process and timing, which
would be slow and flaky. Repro script:
CREATE EXTENSION pg_repack;
CREATE FUNCTION slow_key(i bigint) RETURNS bigint LANGUAGE plpgsql IMMUTABLE
AS $$ BEGIN PERFORM pg_sleep(0.001); RETURN i; END $$;
CREATE TABLE stop_t (id bigint PRIMARY KEY, a int, payload text);
INSERT INTO stop_t SELECT g, g % 1000, repeat(md5(g::text), 4) FROM generate_series(1, 400000) g;
CREATE INDEX ON stop_t (slow_key(id)) WHERE id <= 30000;
CREATE INDEX ON stop_t (slow_key(id + 1)) WHERE id <= 30000;
CREATE INDEX ON stop_t (slow_key(id + 2)) WHERE id <= 30000;
DELETE FROM stop_t WHERE id % 2 = 0;
pg_repack --jobs=2 --table=stop_t postgres &
# once a CREATE INDEX on repack.table_* shows up in pg_stat_activity:
kill -INT %1
inline patch 1: bin/pg_repack.c (+53/-7 lines) download inline-1.patch
diff --git a/bin/pg_repack.c b/bin/pg_repack.c
index 9ff415f..f740497 100644
--- a/bin/pg_repack.c
+++ b/bin/pg_repack.c
@@ -223,6 +223,7 @@ static void repack_cleanup(bool fatal, const repack_table *table);
static void repack_cleanup_callback(bool fatal, void *userdata);
static void repack_cleanup_index(bool fatal, void *userdata);
static bool rebuild_indexes(const repack_table *table);
+static void cancel_index_builds(repack_index *index_jobs, int num_indexes);
static char *getstr(PGresult *res, int row, int col);
static Oid getoid(PGresult *res, int row, int col);
@@ -1165,18 +1166,23 @@ rebuild_indexes(const repack_table *table)
ret = select(max_fd + 1, &input_mask, NULL, NULL, &timeout);
#endif
- /* XXX: the errno != EINTR check means we won't bail
- * out on SIGINT. We should probably just remove this
- * check, though it seems we also need to fix up
- * the on_interrupt handling for workers' index
- * builds (those PGconns don't seem to have c->cancel
- * set, so we don't cancel the in-progress builds).
- */
if (ret < 0 && errno != EINTR)
elog(ERROR, "poll() failed: %d, %d", ret, errno);
elog(DEBUG2, "Poll returned: %d", ret);
+ /* On SIGINT, cancel the index builds still running in the
+ * workers and bail out, instead of handing the remaining
+ * indexes to the freed workers. on_interrupt() can't do this
+ * for us, since the worker connections are not registered in
+ * pgut_connections.
+ */
+ if (interrupted)
+ {
+ cancel_index_builds(index_jobs, num_indexes);
+ CHECK_FOR_INTERRUPTS();
+ }
+
for (i = 0; i < num_indexes; i++)
{
if (index_jobs[i].status == INPROGRESS)
@@ -1256,6 +1262,46 @@ rebuild_indexes(const repack_table *table)
return (!have_error);
}
+/*
+ * Cancel the index builds in progress in the worker connections, and wait
+ * for every worker to finish, so that no CREATE INDEX is left running on
+ * the server (holding locks on the temp table) while we clean up.
+ */
+static void
+cancel_index_builds(repack_index *index_jobs, int num_indexes)
+{
+ PGresult *res;
+ PGcancel *cancel;
+ char errbuf[256];
+ int i;
+
+ for (i = 0; i < num_indexes; i++)
+ {
+ if (index_jobs[i].status != INPROGRESS)
+ continue;
+
+ errbuf[0] = '\0';
+ cancel = PQgetCancel(workers.conns[index_jobs[i].worker_idx]);
+ if (cancel != NULL && PQcancel(cancel, errbuf, sizeof(errbuf)))
+ elog(WARNING, "Cancel request sent to worker %d",
+ index_jobs[i].worker_idx);
+ else
+ elog(WARNING, "Could not send cancel request to worker %d: %s",
+ index_jobs[i].worker_idx, errbuf);
+ PQfreeCancel(cancel);
+ }
+
+ for (i = 0; i < num_indexes; i++)
+ {
+ if (index_jobs[i].status != INPROGRESS)
+ continue;
+
+ while ((res = PQgetResult(workers.conns[index_jobs[i].worker_idx])))
+ CLEARPGRES(res);
+ index_jobs[i].status = FINISHED;
+ }
+}
+
/*
* Re-organize one table.
^ permalink raw [nested|flat] 4+ messages in thread
* Re: [reorg/pg_repack] PR #489: Cancel in-progress index builds on SIGINT with --jobs
@ 2026-11-12 12:37 ` "za-arthur (@za-arthur)" <noreply+za-arthur@github.com>
2 siblings, 0 replies; 4+ messages in thread
From: za-arthur (@za-arthur) @ 2026-11-12 12:37 UTC (permalink / raw)
To: reorg/pg_repack <noreply+reorg-pg_repack@github.com>
Thanks! Merging with my minor fix.
^ permalink raw [nested|flat] 4+ messages in thread
* Re: [reorg/pg_repack] PR #489: Cancel in-progress index builds on SIGINT with --jobs
@ 2026-11-12 12:38 ` "za-arthur (@za-arthur)" <noreply+za-arthur@github.com>
2 siblings, 0 replies; 4+ messages in thread
From: za-arthur (@za-arthur) @ 2026-11-12 12:38 UTC (permalink / raw)
To: reorg/pg_repack <noreply+reorg-pg_repack@github.com>
@za-arthur merged this (commit 9cf21c30da0f).
^ permalink raw [nested|flat] 4+ messages in thread
* Re: [reorg/pg_repack] PR #489: Cancel in-progress index builds on SIGINT with --jobs
@ 2026-11-12 12:38 ` "za-arthur (@za-arthur)" <noreply+za-arthur@github.com>
2 siblings, 0 replies; 4+ messages in thread
From: za-arthur (@za-arthur) @ 2026-11-12 12:38 UTC (permalink / raw)
To: reorg/pg_repack <noreply+reorg-pg_repack@github.com>
@za-arthur closed this.
^ permalink raw [nested|flat] 4+ messages in thread
end of thread, other threads:[~2026-11-12 12:38 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-09 23:26 [reorg/pg_repack] PR #489: Cancel in-progress index builds on SIGINT with --jobs "ritojoao (@ritojoao)" <noreply+ritojoao@github.com>
2026-11-12 12:37 ` "za-arthur (@za-arthur)" <noreply+za-arthur@github.com>
2026-11-12 12:38 ` "za-arthur (@za-arthur)" <noreply+za-arthur@github.com>
2026-11-12 12:38 ` "za-arthur (@za-arthur)" <noreply+za-arthur@github.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox