John Smith
John Smith

Reputation: 173

Prevent drop table if conditions are not met

I'd like to ask if it is possible to handle dropping table command and break it if some conditions are not met?

Upvotes: 0

Views: 472

Answers (1)

Craig Ringer
Craig Ringer

Reputation: 324375

I'm going to guess that you are trying to ask:

How do I prevent a DROP TABLE from succeeding based on certain application-defined conditions

If so, your only built-in option is to use permissions. See GRANT and REVOKE in the PostgreSQL manual.

If you want something more complex, you can write a ProcessUtility_hook, but this requires that you write an extension in C that is compiled and loaded into the server.

Writing a ProcessUtility_hook isn't actually too hard, but there are differences between the PostgreSQL 9.2 and 9.3 definitions that mean you'll need separate extensions. Here's a basic example: https://github.com/ringerc/scrapcode/tree/master/postgresql/example_processutility_hook and here's a ProcessUtility hook that actually does something useful: https://github.com/ringerc/postgres/blob/bdr-reject-unsafe-commands/contrib/bdr/bdr_commandfilter.c

If you don't have C programming experience and some time, a ProcessUtility_hook isn't for you.

See also: How to prevent table from being dropped?

Upvotes: 2

Related Questions