In PostgreSQL when you drop column in a table, all indexes and constraints which use this column will be also dropped silently. If you overlook such situation, you can get serious problems in production.
Here is a demonstration:
CREATE TABLE test (
id INT NOT NULL PRIMARY KEY,
a VARCHAR NOT NULL,
b VARCHAR NOT NULL
);
CREATE INDEX test_index ON test(a, b);
SELECT * FROM pg_indexes WHERE tablename='test';
/*
tablename | indexname
---------------------
test | test_pkey
test | test_index
*/
ALTER TABLE test DROP COLUMN b;
SELECT * FROM pg_indexes WHERE tablename='test';
/*
tablename | indexname
---------------------
test | test_pkey
*/
Is it possible to make PostgreSQL return error when you drop a column with an index on? Or maybe some other ways to prevent described situation?