简体   繁体   English

可以在Postgres中禁用删除所有查询吗?

[英]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? 是否可以只允许那些在其中包含WHERE子句的DELETE查询?

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: 我已经尝试使用RULE ,但我无法弄清楚要放入什么内容的WHERE子句,现在这个RULE禁用所有DELETE查询:

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 从用户撤消删除访问并创建调用DELETE存储过程的问题是我不想重写使用DB的应用程序代码

As discussed in the comments, requiring a WHERE clause in all delete statements isn't going to help much. 正如在注释中所讨论的那样,在所有delete语句中要求WHERE子句并没有多大帮助。 The problem is that the user can always declare a WHERE clause that is always true - for example: 问题是用户总是可以声明一个始终为true的WHERE子句 - 例如:

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. 一些替代方案可能是:通过对数据进行版本控制来记录表的所有更改,或者禁止删除任何行,只需通过添加“已删除”列修改应用程序以将行标记为已删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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