实现说明
这条语句是 SELECT。
核心语句
SELECT pg_listening_channels() AS my_channels;
NOTIFY order_events, 'will be rolled back';
NOTIFY order_events, 'order 42 shipped';
代码示例
-- LISTEN/NOTIFY 是会话间异步通道:消息不落表,无人监听即丢弃
LISTEN order_events;
NOTIFY order_events, 'order 42 shipped';
-- payload 是字符串字面量,上限 8000 字节;通道名是不加引号就转小写的标识符
SELECT pg_listening_channels() AS my_channels;
-- 事务语义:NOTIFY 只在提交时送达,回滚的通知永远不会发出
BEGIN;
NOTIFY order_events, 'will be rolled back';
ROLLBACK;
-- 清理:不再监听时显式取消
UNLISTEN order_events;