grayhemp / pgtoolkit

Tools for PostgreSQL maintenance
Other
184 stars 16 forks source link

Reindex queries seem to drop index in use #11

Closed ivanovv closed 9 years ago

ivanovv commented 9 years ago

command run: ./pgcompact --dbname dbname --table tablename -u --reindex

output (non relevant parts were skipped):

--  Fri May 22 10:17:11 2015 dbname, tablename NOTICE Reindex queries: tablename_pkey, initial size 1921318 pages (15 GB).
CREATE UNIQUE INDEX CONCURRENTLY pgcompact_index_26822 ON schema_name.tablename USING btree (id); -- dbname
BEGIN; -- dbname
SET LOCAL statement_timeout TO 1000; -- dbname
ALTER TABLE schema_name.tablename DROP CONSTRAINT tablename_pkey; -- dbname
ALTER TABLE schema_name.tablename ADD CONSTRAINT tablename_pkey PRIMARY KEY USING INDEX pgcompact_index_26822;  -- dbname
END; -- dbname

--  Fri May 22 10:17:11 2015 dbname, tablename NOTICE Reindex queries: schema_nameindex_tablename_date_id_customer_subscription_id, initial size 2182219 pages (17 GB).
CREATE UNIQUE INDEX CONCURRENTLY pgcompact_index_26822 ON schema_name.tablename USING btree (date_id, customer_subscription_id); -- dbname
BEGIN; -- dbname
SET LOCAL statement_timeout TO 1000; -- dbname
ALTER INDEX schema_name.pgcompact_index_26822 RENAME TO pgcompact_swap_index_26822;
ALTER INDEX schema_name.index_tablename_date_id_customer_subscription_id RENAME TO pgcompact_index_26822;
ALTER INDEX schema_name.pgcompact_swap_index_26822 RENAME TO index_tablename_date_id_customer_subscription_id; -- dbname
END; -- dbname
DROP INDEX CONCURRENTLY schema_name.pgcompact_index_26822; -- dbname

What bothers me here is that we have ALTER TABLE schema_name.tablename ADD CONSTRAINT tablename_pkey PRIMARY KEY USING INDEX pgcompact_index_26822; -- dbname and on the next line CREATE UNIQUE INDEX CONCURRENTLY pgcompact_index_26822 ON schema_name.tablename USING btree (date_id, customer_subscription_id); -- dbname

So, we used an index as a PK and then try to create the index with the same name, but with another set of columns. It won't be the issue with other cases as that temp index name is used to swap indices and dropped later, but in the case with PK seems like an error in the script.

ivanovv commented 9 years ago

My bad, must read the docs

If a constraint name is provided then the index will be renamed to match the constraint name. Otherwise the constraint will be named the same as the index.

Closing.

grayhemp commented 9 years ago

Sorry for not replying for do long, was on vacation. Right, it is renamed automatically when creating constraint, so no intentional renaming needed.