gpt4 book ai didi

database - 在 PostgreSQL 中使用 pg_notify(text, text) 收听/通知

转载 作者:太空狗 更新时间:2023-10-30 01:41:33 25 4
gpt4 key购买 nike

我一直在玩 PostgreSQL 的通知系统,但终究无法弄清楚为什么 pg_notify(text, text) 永远不起作用。这个功能没有过多的记录,我找不到很多在野外使用它的例子,所以我想没有人会介意我在这里问。

运行以下命令完全符合预期:

LISTEN my_channel;

NOTIFY my_channel, 'my message text';

然而,使用 pg_notify() 函数会返回一个空值,并且不会发送任何通知。也没有报错。使用的一个例子是:

SELECT pg_notify('my_channel', 'my message text');

我可以使用 NOTIFY 函数,但我的目标是将通知简化为如下查询:

select pg_notify(get_player_error_channel(username)::TEXT, 'test'::TEXT)
from player;

我想我一定遗漏了一些荒谬的东西,但我一直没有找到原因。可以在此处找到讨论 NOTIFY 的页面:http://www.postgresql.org/docs/9.0/static/sql-notify.html

在上面,它提到了 pg_notify(),这让我假设不会有太大的不同。

pg_notify To send a notification you can also use the function pg_notify(text, text). The function takes the channel name as the first argument and the payload as the second. The function is much easier to use than the NOTIFY command if you need to work with non-constant channel names and payloads.

一如既往地感谢您的帮助

编辑:数据库版本为:“i686-pc-linux-gnu 上的 PostgreSQL 9.0.3,由 GCC gcc (GCC) 4.2.4,32 位编译”

最佳答案

我已经在 PostgreSQL 邮件列表 ( http://archives.postgresql.org/pgsql-bugs/2011-03/msg00041.php ) 上讨论了这个问题,并被告知了该行为的原因。

他们的回答是“..你必须双引号 relnames(听“测试”)。如果你希望服务器不要折叠它们。 pg_notify 接受一个字符串,而不是一个relname,它使用不同的规则。”(感谢 Merlin 和 Tom)

这意味着以下内容有效,因为 channel 始终强制为小写

LISTEN ERRORCHANNEL;

NOTIFY ERRORCHANNEL, 'something!';
NOTIFY eRrorChanNel, 'something!';

如果您要在 channel 名称周围添加双引号,大小写将保持不变。

因此,对于以下内容,您会收到第一个通知,但不会收到第二个通知:

LISTEN "ERRORCHANNEL";

NOTIFY "ERRORCHANNEL", 'something!';
NOTIFY "eRrorChanNel", 'something!';

同样,以下将起作用,因为双引号强制保持 ERRORCHANNEL 的大小写:

LISTEN "ERRORCHANNEL";

SELECT pg_notify('ERRORCHANNEL', 'something!');

虽然这行不通:

LISTEN ERRORCHANNEL;

SELECT pg_notify('ERRORCHANNEL', 'something!');

在这种情况下,ERRORCHANNEL 不在 LISTEN 命令中的双引号中,因此 PostgreSQL 强制将其小写。 channel 参数的类型是文本而不是 relname,因此在 pg_notify() 函数中大小写保持不变。 channel 一起不匹配 (ERRORCHANNE != errorchannel) 因此永远不会收到通知。

关于database - 在 PostgreSQL 中使用 pg_notify(text, text) 收听/通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5173323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com