From: Tom Lane Date: Tue, 15 Feb 2011 20:49:54 +0000 (-0500) Subject: Add CheckTableNotInUse calls in DROP TABLE and DROP INDEX. X-Git-Tag: REL9_1_ALPHA4~180 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=eff027c43288d15307676b1bd4736ab11f34c407;p=postgresql Add CheckTableNotInUse calls in DROP TABLE and DROP INDEX. Recent releases had a check on rel->rd_refcnt in heap_drop_with_catalog, but failed to cover the possibility of pending trigger events at DROP time. (Before 8.4 we didn't even check the refcnt.) When the trigger events were eventually fired, you'd get "could not open relation with OID nnn" errors, as in recent report from strk. Better to throw a suitable error when the DROP is attempted. Also add a similar check in DROP INDEX. Back-patch to all supported branches. --- diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c index 2cf210d82c..bfffd6ef4c 100644 --- a/src/backend/catalog/heap.c +++ b/src/backend/catalog/heap.c @@ -1603,14 +1603,10 @@ heap_drop_with_catalog(Oid relid) /* * There can no longer be anyone *else* touching the relation, but we - * might still have open queries or cursors in our own session. + * might still have open queries or cursors, or pending trigger events, + * in our own session. */ - if (rel->rd_refcnt != 1) - ereport(ERROR, - (errcode(ERRCODE_OBJECT_IN_USE), - errmsg("cannot drop \"%s\" because " - "it is being used by active queries in this session", - RelationGetRelationName(rel)))); + CheckTableNotInUse(rel, "DROP TABLE"); /* * Delete pg_foreign_table tuple first. diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index 5979a65092..58265129e6 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -1298,6 +1298,12 @@ index_drop(Oid indexId) userIndexRelation = index_open(indexId, AccessExclusiveLock); + /* + * There can no longer be anyone *else* touching the index, but we + * might still have open queries using it in our own session. + */ + CheckTableNotInUse(userIndexRelation, "DROP INDEX"); + /* * Schedule physical removal of the files */ diff --git a/src/interfaces/ecpg/test/expected/sql-fetch.stderr b/src/interfaces/ecpg/test/expected/sql-fetch.stderr index 038b61e71c..228421835d 100644 --- a/src/interfaces/ecpg/test/expected/sql-fetch.stderr +++ b/src/interfaces/ecpg/test/expected/sql-fetch.stderr @@ -138,10 +138,10 @@ [NO_PID]: sqlca: code: 0, state: 00000 [NO_PID]: ecpg_execute on line 53: using PQexec [NO_PID]: sqlca: code: 0, state: 00000 -[NO_PID]: ecpg_check_PQresult on line 53: bad response - ERROR: cannot drop "my_table" because it is being used by active queries in this session +[NO_PID]: ecpg_check_PQresult on line 53: bad response - ERROR: cannot DROP TABLE "my_table" because it is being used by active queries in this session [NO_PID]: sqlca: code: 0, state: 00000 -[NO_PID]: raising sqlstate 55006 (sqlcode -400): cannot drop "my_table" because it is being used by active queries in this session on line 53 +[NO_PID]: raising sqlstate 55006 (sqlcode -400): cannot DROP TABLE "my_table" because it is being used by active queries in this session on line 53 [NO_PID]: sqlca: code: -400, state: 55006 -SQL error: cannot drop "my_table" because it is being used by active queries in this session on line 53 +SQL error: cannot DROP TABLE "my_table" because it is being used by active queries in this session on line 53 [NO_PID]: ecpg_finish: connection regress1 closed [NO_PID]: sqlca: code: 0, state: 00000