简体   繁体   中英

Is it possible to disable delete all query in Postgres?

Is it possible to allow only those DELETE queries that have a WHERE clause in them?

I've tried to use RULE for that, but I can't figure out what to put into it's WHERE clause, right now this RULE disables all DELETE queries:

create rule prevent_delete_all  as on DELETE to site 
DO INSTEAD NOTHING

Edit

Problem with revoking delete access from users and creating stored procedures for calling DELETE is that I don't want to re-write application code that is using the DB

As discussed in the comments, requiring a WHERE clause in all delete statements isn't going to help much. The problem is that the user can always declare a WHERE clause that is always true - for example:

Delete from students where 1=1;

I've tried to detect clauses like this before - and they can be arbitrarily complex so consistently detecting them is very hard, if not impossible.

Instead, I would suggest that you reconsider what it is that you are trying to do. Some alternatives might be: keep a record of all changes to the table by versioning the data, or disallow deleting any rows and just modify the application to mark rows as deleted by adding a 'Deleted' column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM