简体   繁体   English

Postgres - 触发后删除记录

[英]Postgres - delete record after trigger

I have a postgres table with some triggers that fire if records are udpated or deleted.我有一个带有一些触发器的 postgres 表,如果记录被更新或删除,这些触发器就会触发。 They basically archive the records so they are not trully deleted - they just change certain attributes so the records are not displayed in corresponding views.他们基本上将记录存档,因此它们不会被真正删除 - 它们只是更改某些属性,因此记录不会显示在相应的视图中。

Sometimes I want to manually delete a record for good, but I can't do it because the trigger fires and does it's thing if I execute DELETE query.有时我想手动删除一条记录,但我不能这样做,因为触发器会触发并且如果我执行 DELETE 查询就会这样做。

Example: DELETE FROM records WHERE operator = 20示例: DELETE FROM records WHERE operator = 20

Is there a way to run DELETE query and bypass a trigger that fires on DELETE?有没有办法运行 DELETE 查询并绕过在 DELETE 上触发的触发器?

With a setup like this, I think the typical approach is to avoid granting any direct privileges on the underlying table, and put your INSERT / UPDATE / DELETE triggers on the view (allowing the table owner to change it as needed).通过这样的设置,我认为典型的方法是避免授予对基础表的任何直接权限,并将您的INSERT / UPDATE / DELETE触发器放在视图上(允许表所有者根据需要更改它)。

If you are the table owner, you can use ALTER TABLE to temporarily DISABLE and re- ENABLE the trigger.如果您是表所有者,则可以使用ALTER TABLE临时DISABLE和重新ENABLE触发器。 As long as you do all of this within a transaction, you'll be safe against concurrent DELETE s, though they'll block until your transaction commits.只要您在事务中执行所有这些操作,您就可以安全地避免并发DELETE ,尽管它们会阻塞直到您的事务提交。

If you have superuser privileges, you can also prevent triggers from firing by setting session_replication_role to replica , provided that the trigger in question hasn't been configured to fire on replicated databases.如果您拥有超级用户权限,还可以通过将session_replication_role设置为replica来防止触发器触发,前提是相关触发器尚未配置为在复制数据库上触发。

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

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