diff --git a/bin/expected/reorg.out b/bin/expected/reorg.out index 8793c48..86d7a0e 100755 --- a/bin/expected/reorg.out +++ b/bin/expected/reorg.out @@ -46,6 +46,10 @@ CREATE TABLE tbl_with_dropped_toast ( PRIMARY KEY (i, j) ); ALTER TABLE tbl_with_dropped_toast CLUSTER ON tbl_with_dropped_toast_pkey; +CREATE TABLE tbl_badindex ( + id integer PRIMARY KEY, + n integer +); -- -- insert data -- @@ -71,6 +75,11 @@ CREATE VIEW view_for_dropped_column AS INSERT INTO tbl_with_dropped_toast VALUES(1, 10, 'abc'); INSERT INTO tbl_with_dropped_toast VALUES(2, 20, sqrt(2::numeric(1000,999))::text || sqrt(3::numeric(1000,999))::text); ALTER TABLE tbl_with_dropped_toast DROP COLUMN t; +INSERT INTO tbl_badindex VALUES(1, 10); +INSERT INTO tbl_badindex VALUES(2, 10); +CREATE UNIQUE INDEX CONCURRENTLY idx_badindex_n ON tbl_badindex (n); +ERROR: could not create unique index "idx_badindex_n" +DETAIL: Key (n)=(10) is duplicated. -- -- before -- @@ -99,6 +108,7 @@ SELECT * FROM tbl_with_dropped_toast; -- do reorg -- \! pg_reorg --dbname=contrib_regression --no-order +WARNING: skipping invalid index: CREATE UNIQUE INDEX idx_badindex_n ON tbl_badindex USING btree (n) \! pg_reorg --dbname=contrib_regression \! pg_reorg --dbname=contrib_regression --table=tbl_cluster -- diff --git a/bin/pg_reorg.c b/bin/pg_reorg.c index 4875976..2939f4c 100755 --- a/bin/pg_reorg.c +++ b/bin/pg_reorg.c @@ -460,7 +460,9 @@ reorg_one_table(const reorg_table *table, const char *orderby) params[0] = utoa(table->target_oid, buffer); res = execute("SELECT indexrelid," - " reorg.reorg_indexdef(indexrelid, indrelid)" + " reorg.reorg_indexdef(indexrelid, indrelid)," + " indisvalid," + " pg_get_indexdef(indexrelid)" " FROM pg_index WHERE indrelid = $1", 1, params); num = PQntuples(res); @@ -468,9 +470,18 @@ reorg_one_table(const reorg_table *table, const char *orderby) { reorg_index index; int c = 0; + const char *isvalid; + const char *indexdef; index.target_oid = getoid(res, i, c++); index.create_index = getstr(res, i, c++); + isvalid = getstr(res, i, c++); + indexdef = getstr(res, i, c++); + + if (isvalid && isvalid[0] == 'f') { + elog(WARNING, "skipping invalid index: %s", indexdef); + continue; + } elog(DEBUG2, "[%d]", i); elog(DEBUG2, "target_oid : %u", index.target_oid); diff --git a/bin/sql/reorg.sql b/bin/sql/reorg.sql index 1760c98..863d2ce 100755 --- a/bin/sql/reorg.sql +++ b/bin/sql/reorg.sql @@ -55,6 +55,11 @@ CREATE TABLE tbl_with_dropped_toast ( ); ALTER TABLE tbl_with_dropped_toast CLUSTER ON tbl_with_dropped_toast_pkey; +CREATE TABLE tbl_badindex ( + id integer PRIMARY KEY, + n integer +); + -- -- insert data -- @@ -86,6 +91,11 @@ CREATE VIEW view_for_dropped_column AS INSERT INTO tbl_with_dropped_toast VALUES(1, 10, 'abc'); INSERT INTO tbl_with_dropped_toast VALUES(2, 20, sqrt(2::numeric(1000,999))::text || sqrt(3::numeric(1000,999))::text); ALTER TABLE tbl_with_dropped_toast DROP COLUMN t; + +INSERT INTO tbl_badindex VALUES(1, 10); +INSERT INTO tbl_badindex VALUES(2, 10); +CREATE UNIQUE INDEX CONCURRENTLY idx_badindex_n ON tbl_badindex (n); + -- -- before -- diff --git a/lib/reorg.c b/lib/reorg.c index 3986f0b..22aa5c7 100755 --- a/lib/reorg.c +++ b/lib/reorg.c @@ -705,6 +705,7 @@ reorg_swap(PG_FUNCTION_ARGS) " pg_catalog.pg_class Y" " WHERE I.indrelid = $1" " AND I.indexrelid = X.oid" + " AND I.indisvalid" " AND Y.oid = ('reorg.index_' || X.oid)::regclass", 1, argtypes, values, nulls);