我希望实现一个触发器,在 PostgreSQL 中的表更改时发出通知,但我无法找到它是否可以在集群中工作的任何明确答案。
PostgreSQL 监听/通知是否可以在 AWS Aurora(预配置和无服务器)上工作,我是否可以期望所有客户端都收到通知,无论它们连接到哪个实例?
如果是,是否存在任何主要的性能问题?
谢谢 :)
目标:
当一条新记录插入到特定的 PostgreSQL 表中时,我希望 PostgreSQL 通知我的 Node.js Web 应用程序,以便它可以启动对外部服务的 API 调用。
据我了解,基本步骤是:
这是我对每一步的尝试:
notification_app_after_table_insert.pgsql中的触发函数
CREATE OR REPLACE FUNCTION notify_app_after_table_insert()
RETURNS TRIGGER AS
$BODY$
BEGIN
PERFORM pg_notify('channel', row_to_json(NEW)::text);
RETURN new;
END;
$BODY$
LANGUAGE plpgsql
Run Code Online (Sandbox Code Playgroud)
在trigger_notify_app_after_table_insert.sql中触发
CREATE TRIGGER trigger_notify_app_after_table_insert
AFTER INSERT
ON table
FOR EACH ROW
EXECUTE PROCEDURE notify_app_after_table_insert();
Run Code Online (Sandbox Code Playgroud)
index.js 中的侦听器机制(在我的网络应用程序的后端内)
//tools
const express = require('express');
const app = express();
const cors = require('cors');
const bodyParser = require('body-parser');
const port = …Run Code Online (Sandbox Code Playgroud)I've read https://www.postgresql.org/docs/9.6/static/sql-notify.html and the channel name is just described as an 'identifier'.
I'm using NodeJS and pg https://www.npmjs.com/package/pg to access postgres.
If I use a lower case word in both, e.g. pg_notify('foo', ... and LISTEN foo it works. I've tested all combinations:
pg_notify LISTEN outcome
lower lower works
lower upper works
upper lower fails
upper upper fails
Run Code Online (Sandbox Code Playgroud)
Is this a bug, or is it a logical result of being an 'identifier'? (If so, should this be documented on pg_notify …