Skip to content

Commit 9f5c4f5

Browse files
committed
Don't try to rebuild invalid indexes
Closes ticket #9
1 parent df12c37 commit 9f5c4f5

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

bin/expected/reorg.out

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ CREATE TABLE tbl_with_dropped_toast (
4646
PRIMARY KEY (i, j)
4747
);
4848
ALTER TABLE tbl_with_dropped_toast CLUSTER ON tbl_with_dropped_toast_pkey;
49+
CREATE TABLE tbl_badindex (
50+
id integer PRIMARY KEY,
51+
n integer
52+
);
4953
--
5054
-- insert data
5155
--
@@ -71,6 +75,11 @@ CREATE VIEW view_for_dropped_column AS
7175
INSERT INTO tbl_with_dropped_toast VALUES(1, 10, 'abc');
7276
INSERT INTO tbl_with_dropped_toast VALUES(2, 20, sqrt(2::numeric(1000,999))::text || sqrt(3::numeric(1000,999))::text);
7377
ALTER TABLE tbl_with_dropped_toast DROP COLUMN t;
78+
INSERT INTO tbl_badindex VALUES(1, 10);
79+
INSERT INTO tbl_badindex VALUES(2, 10);
80+
CREATE UNIQUE INDEX CONCURRENTLY idx_badindex_n ON tbl_badindex (n);
81+
ERROR: could not create unique index "idx_badindex_n"
82+
DETAIL: Key (n)=(10) is duplicated.
7483
--
7584
-- before
7685
--
@@ -99,6 +108,7 @@ SELECT * FROM tbl_with_dropped_toast;
99108
-- do reorg
100109
--
101110
\! pg_reorg --dbname=contrib_regression --no-order
111+
WARNING: skipping invalid index: CREATE UNIQUE INDEX idx_badindex_n ON tbl_badindex USING btree (n)
102112
\! pg_reorg --dbname=contrib_regression
103113
\! pg_reorg --dbname=contrib_regression --table=tbl_cluster
104114
--

bin/pg_reorg.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,17 +460,28 @@ reorg_one_table(const reorg_table *table, const char *orderby)
460460

461461
params[0] = utoa(table->target_oid, buffer);
462462
res = execute("SELECT indexrelid,"
463-
" reorg.reorg_indexdef(indexrelid, indrelid)"
463+
" reorg.reorg_indexdef(indexrelid, indrelid),"
464+
" indisvalid,"
465+
" pg_get_indexdef(indexrelid)"
464466
" FROM pg_index WHERE indrelid = $1", 1, params);
465467

466468
num = PQntuples(res);
467469
for (i = 0; i < num; i++)
468470
{
469471
reorg_index index;
470472
int c = 0;
473+
const char *isvalid;
474+
const char *indexdef;
471475

472476
index.target_oid = getoid(res, i, c++);
473477
index.create_index = getstr(res, i, c++);
478+
isvalid = getstr(res, i, c++);
479+
indexdef = getstr(res, i, c++);
480+
481+
if (isvalid && isvalid[0] == 'f') {
482+
elog(WARNING, "skipping invalid index: %s", indexdef);
483+
continue;
484+
}
474485

475486
elog(DEBUG2, "[%d]", i);
476487
elog(DEBUG2, "target_oid : %u", index.target_oid);

bin/sql/reorg.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ CREATE TABLE tbl_with_dropped_toast (
5555
);
5656
ALTER TABLE tbl_with_dropped_toast CLUSTER ON tbl_with_dropped_toast_pkey;
5757

58+
CREATE TABLE tbl_badindex (
59+
id integer PRIMARY KEY,
60+
n integer
61+
);
62+
5863
--
5964
-- insert data
6065
--
@@ -86,6 +91,11 @@ CREATE VIEW view_for_dropped_column AS
8691
INSERT INTO tbl_with_dropped_toast VALUES(1, 10, 'abc');
8792
INSERT INTO tbl_with_dropped_toast VALUES(2, 20, sqrt(2::numeric(1000,999))::text || sqrt(3::numeric(1000,999))::text);
8893
ALTER TABLE tbl_with_dropped_toast DROP COLUMN t;
94+
95+
INSERT INTO tbl_badindex VALUES(1, 10);
96+
INSERT INTO tbl_badindex VALUES(2, 10);
97+
CREATE UNIQUE INDEX CONCURRENTLY idx_badindex_n ON tbl_badindex (n);
98+
8999
--
90100
-- before
91101
--

lib/reorg.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ reorg_swap(PG_FUNCTION_ARGS)
705705
" pg_catalog.pg_class Y"
706706
" WHERE I.indrelid = $1"
707707
" AND I.indexrelid = X.oid"
708+
" AND I.indisvalid"
708709
" AND Y.oid = ('reorg.index_' || X.oid)::regclass",
709710
1, argtypes, values, nulls);
710711

0 commit comments

Comments
 (0)