From: jack5505 (@jack5505) <noreply+jack5505@github.com>
To: reorg/pg_repack <noreply+reorg-pg_repack@github.com>
Subject: [reorg/pg_repack] PR #488: Exit with an error when a requested index cannot be repacked (#468)
Date: Fri, 30 Oct 2026 12:50:53 +0000
Message-ID: <gh-reorg-pg_repack-488@github.com> (raw)
Fixes #468.
With `--index` pointing to a non-existent relation, or to a relation that is not an index, pg_repack printed a WARNING and exited with code 0, so scripts could not detect the failure:
$ pg_repack -d mydb --index=public.my_index_not_exist
WARNING: ERROR: relation "public.my_index_not_exist" does not exist
$ echo $?
0
Changes:
- `is_requested_relation_exists()` now also validates `--index` arguments, the same way `--table` and `--parent-table` are validated, so a typo fails before any work is done (consistent with the `--table` behaviour, exit code is now non-zero).
- `repack_all_indexes()` counts failures (query errors, "is not a valid index", `repack_table_indexes()` returning false) and returns false when any requested relation could not be processed, so `main()` reports an error instead of exiting 0. A table with `--only-indexes` that simply has no indexes is still a warning only.
- New regression test `error-on-missing-index`. The `_1` expected-output variant is for PostgreSQL 9.5, which has no `to_regclass()` and therefore skips the upfront check and fails inside the loop instead.
Not changed on purpose: the `ERROR: ERROR: relation ... does not exist` message format of the index path in `main()` is pre-existing and already present in `repack-check_2.out`; changing it would touch unrelated expected outputs.
Tested with `make installcheck` on PostgreSQL 14.17 (all 14 tests pass); the 9.5 variant is based on the code path only and relies on CI for confirmation.
inline patch 1: bin/pg_repack.c, regress/Makefile, regress/expected/error-on-missing-index.out and 3 more (+109/-4 lines) download inline-1.patchdiff --git a/bin/pg_repack.c b/bin/pg_repack.cindex 9ff415f..d910699 100644--- a/bin/pg_repack.c+++ b/bin/pg_repack.c@@ -615,7 +615,8 @@ preliminary_checks(char *errbuf, size_t errsize){
/*
* Check the presence of tables specified by --parent-table and --table
- * otherwise format user-friendly message+ * and of indexes specified by --index, otherwise format user-friendly+ * message
*/
static bool
is_requested_relation_exists(char *errbuf, size_t errsize){
@@ -628,7 +629,8 @@ is_requested_relation_exists(char *errbuf, size_t errsize){
SimpleStringListCell *cell;
num_relations = simple_string_list_size(parent_table_list) +
- simple_string_list_size(table_list);+ simple_string_list_size(table_list) ++ simple_string_list_size(r_index);
/* nothing was implicitly requested, so nothing to do here */
if (num_relations == 0)
@@ -656,6 +658,13 @@ is_requested_relation_exists(char *errbuf, size_t errsize){
if (iparam < num_relations)
appendStringInfoChar(&sql, ',');
}
+ for (cell = r_index.head; cell; cell = cell->next)+ {+ appendStringInfo(&sql, "($%d, 'i')", iparam + 1);+ params[iparam++] = cell->val;+ if (iparam < num_relations)+ appendStringInfoChar(&sql, ',');+ }
appendStringInfoString(&sql,
") AS given_t(r,kind) WHERE"
/* regular --table relation or inherited --parent-table */
@@ -664,6 +673,8 @@ is_requested_relation_exists(char *errbuf, size_t errsize){
/* declarative partitioned --parent-table */
" AND NOT EXISTS("
" SELECT FROM pg_catalog.pg_class c WHERE c.oid=to_regclass(given_t.r) AND c.relkind = given_t.kind AND given_t.kind = 'p')"
+ /* --index: the relation must exist, whether it is an index is checked later */+ " AND NOT (given_t.kind = 'i' AND to_regclass(given_t.r) IS NOT NULL)"
);
/* double check the parameters array is sane */
@@ -2345,6 +2356,7 @@ repack_all_indexes(char *errbuf, size_t errsize)
StringInfoData sql;
SimpleStringListCell *cell = NULL;
const char *params[1];
+ int nfailed = 0;
initStringInfo(&sql);
reconnect(ERROR);
@@ -2400,6 +2412,7 @@ repack_all_indexes(char *errbuf, size_t errsize)
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
elog(WARNING, "%s", PQerrorMessage(connection));
+ nfailed++;
continue;
}
@@ -2408,6 +2421,7 @@ repack_all_indexes(char *errbuf, size_t errsize)
if (nchildren == 0)
{
elog(WARNING, "relation \"%s\" does not exist", cell->val);
+ nfailed++;
continue;
}
@@ -2429,6 +2443,7 @@ repack_all_indexes(char *errbuf, size_t errsize)
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
elog(WARNING, "%s", PQerrorMessage(connection));
+ nfailed++;
continue;
}
@@ -2438,8 +2453,11 @@ repack_all_indexes(char *errbuf, size_t errsize)
elog(WARNING, "\"%s\" does not have any indexes",
cell->val);
else if(r_index.head)
+ {
elog(WARNING, "\"%s\" is not a valid index",
cell->val);
+ nfailed++;+ }
continue;
}
@@ -2448,11 +2466,24 @@ repack_all_indexes(char *errbuf, size_t errsize)
elog(INFO, "repacking indexes of \"%s\"", cell->val);
if (!repack_table_indexes(res))
+ {
elog(WARNING, "repack failed for \"%s\"", cell->val);
+ nfailed++;+ }
CLEARPGRES(res);
}
- ret = true;++ /*+ * Warnings alone would leave the exit code at 0, which hides failures+ * from scripts; make sure the caller reports an error (issue #468).+ */+ if (nfailed == 0)+ ret = true;+ else if (errbuf)+ snprintf(errbuf, errsize,+ "failed to repack %d relation(s), see the warnings above",+ nfailed);
cleanup:
disconnect();
diff --git a/regress/Makefile b/regress/Makefileindex 43c4d33..8cc8b54 100644--- a/regress/Makefile+++ b/regress/Makefile@@ -17,7 +17,7 @@ INTVERSION := $(shell echo $$(($$(echo $(VERSION).0 | sed 's/\([[:digit:]]\{1,\}
# Test suite
#
-REGRESS := init-extension repack-setup repack-run error-on-invalid-idx no-error-on-invalid-idx after-schema repack-check nosuper tablespace get_order_by trigger trigger_privs publication+REGRESS := init-extension repack-setup repack-run error-on-invalid-idx no-error-on-invalid-idx error-on-missing-index after-schema repack-check nosuper tablespace get_order_by trigger trigger_privs publication
USE_PGXS = 1 # use pgxs if not in contrib directory
PGXS := $(shell $(PG_CONFIG) --pgxs)
diff --git a/regress/expected/error-on-missing-index.out b/regress/expected/error-on-missing-index.outnew file mode 100644index 0000000..89fd7b8--- /dev/null+++ b/regress/expected/error-on-missing-index.out@@ -0,0 +1,25 @@+--+-- pg_repack must exit with an error when a requested index does not exist+-- or is not an index (issue #468)+--+SET client_min_messages = warning;+CREATE TABLE tbl_missing_idx (id integer PRIMARY KEY, t text);+CREATE INDEX tbl_missing_idx_t ON tbl_missing_idx (t);+-- => ERROR: a non-existent index+\! pg_repack --dbname=contrib_regression --index=idx_does_not_exist || echo "pg_repack exited with an error"+ERROR: ERROR: relation "idx_does_not_exist" does not exist+pg_repack exited with an error+-- => ERROR: a valid index together with a non-existent one+\! pg_repack --dbname=contrib_regression --index=tbl_missing_idx_t --index=idx_does_not_exist || echo "pg_repack exited with an error"+ERROR: ERROR: relation "idx_does_not_exist" does not exist+pg_repack exited with an error+-- => ERROR: a table given instead of an index+\! pg_repack --dbname=contrib_regression --index=tbl_missing_idx || echo "pg_repack exited with an error"+WARNING: "tbl_missing_idx" is not a valid index+ERROR: failed to repack 1 relation(s), see the warnings above+pg_repack exited with an error+-- => OK: an existing index+\! pg_repack --dbname=contrib_regression --index=tbl_missing_idx_t && echo "pg_repack succeeded"+INFO: repacking index "public.tbl_missing_idx_t"+pg_repack succeeded+DROP TABLE tbl_missing_idx;diff --git a/regress/expected/error-on-missing-index_1.out b/regress/expected/error-on-missing-index_1.outnew file mode 100644index 0000000..09a8320--- /dev/null+++ b/regress/expected/error-on-missing-index_1.out@@ -0,0 +1,28 @@+--+-- pg_repack must exit with an error when a requested index does not exist+-- or is not an index (issue #468)+--+SET client_min_messages = warning;+CREATE TABLE tbl_missing_idx (id integer PRIMARY KEY, t text);+CREATE INDEX tbl_missing_idx_t ON tbl_missing_idx (t);+-- => ERROR: a non-existent index+\! pg_repack --dbname=contrib_regression --index=idx_does_not_exist || echo "pg_repack exited with an error"+WARNING: ERROR: relation "idx_does_not_exist" does not exist+ERROR: failed to repack 1 relation(s), see the warnings above+pg_repack exited with an error+-- => ERROR: a valid index together with a non-existent one+\! pg_repack --dbname=contrib_regression --index=tbl_missing_idx_t --index=idx_does_not_exist || echo "pg_repack exited with an error"+INFO: repacking index "public.tbl_missing_idx_t"+WARNING: ERROR: relation "idx_does_not_exist" does not exist+ERROR: failed to repack 1 relation(s), see the warnings above+pg_repack exited with an error+-- => ERROR: a table given instead of an index+\! pg_repack --dbname=contrib_regression --index=tbl_missing_idx || echo "pg_repack exited with an error"+WARNING: "tbl_missing_idx" is not a valid index+ERROR: failed to repack 1 relation(s), see the warnings above+pg_repack exited with an error+-- => OK: an existing index+\! pg_repack --dbname=contrib_regression --index=tbl_missing_idx_t && echo "pg_repack succeeded"+INFO: repacking index "public.tbl_missing_idx_t"+pg_repack succeeded+DROP TABLE tbl_missing_idx;diff --git a/regress/expected/repack-check_3.out b/regress/expected/repack-check_3.outindex 079a756..1f29dff 100644--- a/regress/expected/repack-check_3.out+++ b/regress/expected/repack-check_3.out@@ -390,6 +390,7 @@ ERROR: pg_repack failed with error: ERROR: relation "partitioned_a" does not ex
-- => OK
\! pg_repack --dbname=contrib_regression --parent-table=partitioned_a --only-indexes
WARNING: ERROR: relation "partitioned_a" does not exist
+ERROR: failed to repack 1 relation(s), see the warnings above
-- => OK
\! pg_repack --dbname=contrib_regression --parent-table=partitioned_a --parent-table=parent_a
ERROR: pg_repack failed with error: ERROR: relation "partitioned_a" does not exist
@@ -402,3 +403,4 @@ INFO: repacking indexes of "public.child_a_2"
INFO: repacking index "public.child_a_2_pkey"
INFO: repacking indexes of "public.parent_a"
INFO: repacking index "public.parent_a_pkey"
+ERROR: failed to repack 1 relation(s), see the warnings abovediff --git a/regress/sql/error-on-missing-index.sql b/regress/sql/error-on-missing-index.sqlnew file mode 100644index 0000000..a472d99--- /dev/null+++ b/regress/sql/error-on-missing-index.sql@@ -0,0 +1,19 @@+--+-- pg_repack must exit with an error when a requested index does not exist+-- or is not an index (issue #468)+--+SET client_min_messages = warning;++CREATE TABLE tbl_missing_idx (id integer PRIMARY KEY, t text);+CREATE INDEX tbl_missing_idx_t ON tbl_missing_idx (t);++-- => ERROR: a non-existent index+\! pg_repack --dbname=contrib_regression --index=idx_does_not_exist || echo "pg_repack exited with an error"+-- => ERROR: a valid index together with a non-existent one+\! pg_repack --dbname=contrib_regression --index=tbl_missing_idx_t --index=idx_does_not_exist || echo "pg_repack exited with an error"+-- => ERROR: a table given instead of an index+\! pg_repack --dbname=contrib_regression --index=tbl_missing_idx || echo "pg_repack exited with an error"+-- => OK: an existing index+\! pg_repack --dbname=contrib_regression --index=tbl_missing_idx_t && echo "pg_repack succeeded"++DROP TABLE tbl_missing_idx;