Datum get_rcv_replication_stat(PG_FUNCTION_ARGS) { Assert(PG_NARGS() == 0); // 表示没有输入参数
if (!RecoveryInProgress()) // 在数据库处于恢复状态下时运行,否则不允许 ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("recovery is not in progress"), errhint("This functions can only be executed during recovery.")));
3、创建函数和视图 postgres=# create or replace function get_rcv_replication_stat() returns record as '$libdir/libget_upstream_conninfo.so', 'get_rcv_replication_stat' language C STRICT; postgres=# create or replace view get_rcv_replication_stat as select now(), extract(epoch from now()) as now_epoch, last_walend_time, last_recv_lsn, last_apply_lsn, last_apply_delay_ms, receiver_pid, case receiver_state when 0 then 'stopped' when 1 then 'starting' when 2 then 'streaming' when 3 then 'waiting' when 4 then 'restarting' when 5 then 'stopping' else null end as receiver_state, receiver_start_epoch, conninfo from get_rcv_replication_stat() as t (last_walend_time timestamptz, last_recv_lsn pg_lsn, last_apply_lsn pg_lsn, last_apply_delay_ms int, receiver_pid int, receiver_state int, receiver_start_epoch int8, conninfo text );
赋权限: grant usage on schema public to trace; grant select on all tables in schema public to trace;
4、在主节点查询 postgres=# select * from get_rcv_replication_stat ; ERROR: recovery is not in progress HINT: This functions can only be executed during recovery.