监听器

PostgREST 使用 LISTEN 通过 NOTIFY 重新加载其 模式缓存配置。这在无法发送 SIGUSR1 或 SIGUSR2 Unix 信号的环境中很有用。例如在云托管容器或 Windows 系统上。

NOTIFY pgrst, 'reload schema'; -- reload schema cache
NOTIFY pgrst, 'reload config'; -- reload config
NOTIFY pgrst;                  -- reload both

默认情况下,LISTEN 通道已启用 (db-channel-enabled) 且名为 pgrst (db-channel)。

读取副本上的监听器

LISTENNOTIFY 命令在 PostgreSQL 读取副本上不起作用。因此,如果您将 PostgREST 连接到读取副本,监听器将无法启动。

-- check if the instance is a replica
postgres=# select pg_is_in_recovery();
 pg_is_in_recovery
-------------------
 t
(1 row)

postgres=# LISTEN pgrst;
ERROR:  cannot execute LISTEN during recovery

为了解决这个问题,您可以将 Listener 连接到主服务器,同时仍然在副本服务器上使用 连接池

这可以通过使用标准的 libpq 多主机target_session_attrs 在您的 连接字符串 中实现。

db-uri = "postgres://read_replica.host,primary.host/mydb?target_session_attrs=read-only"

这将导致 连接池 连接到读副本主机,并在备用主服务器上执行 LISTEN 操作。

注意

在幕后,PostgREST 强制 target_session_attrs=read-write 用于 LISTEN 会话。