PostgreSQL:如何在不延迟插入响应的情况下执行插入触发器?
有谁知道如何设置插入触发器,以便当从我的应用程序执行插入时,即使在触发器完成执行之前,数据也会被插入并且 postgres 返回?
Does anyone knows how can I set up an insert trigger so when a perform an insert from my application, the data gets inserted and postgres returns, even before the trigger finishes executing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对此没有内置支持;你将不得不破解一些东西。选项包括:
用 C、Perl 或 Python 编写触发器,并让它启动一个单独的进程来执行您想要的操作。这可能会变得很棘手,并且可能对您的数据库系统有点危险,并且只有当您想要做的事情在数据库之外时它才有效。
编写一个轻量级触发器函数,仅将条目记录到日志或任务表中,并有一个单独的作业或守护程序按自己的计划查看该表并从那里执行操作。这或多或少就是 Slony 的工作原理。
There is no built-in support for this; you will have to hack something up. Options include:
Write the trigger in C, Perl, or Python and have it launch a separate process to do the things you want. This can get tricky and possibly slightly dangerous to your database system, and it only works if the things you want to do are outside of the database.
Write a lightweight trigger function that only records an entry into a log or task table, and have a separate job or daemon that looks into that table on its own schedule and executes things from there. That's more or less how Slony works.
问题是:为什么需要它?触发器应该很快。如果您需要做一些复杂的事情,请编写触发器来向执行复杂部分的某些守护进程发送通知 - 例如使用 PostgreSQL 的 LISTEN/NOTIFY 功能。
The question is : why do you need it? Triggers should be fast. If you need to do something complicated, write trigger that send notification to some daemon that does the complex part - for example using LISTEN/NOTIFY feature of PostgreSQL.