postgresql获取表最后更新时间(通过发布订阅机制将消息发送给应用程序)
一、创建测试表
CREATE TABLE weather(
city varchar(80),
temp_lo int, --最低温度
temp_hi int, --最高温度
prcp real, --湿度
date date
);
二、创建触发器函数
create or replace function table_update_notify() returns trigger as $$
begin
perform pg_notify('table_update',json_build_object('table',TG_TABLE_NAME,'timestamp',current_timestamp)::text);
return new;
end;
$$ language plpgsql;
三、创建触发器
drop trigger if exists n_weather_u on weather;
create trigger n_weather_u after insert or update or delete on weather
for each statement execute procedure table_update_notify();
四、应用程序代码
代码有重连机制,数据库服务器停止或者网络断开应用程序不退出,数据库服务起来或者网络恢复后应用程序会自动重连并重新订阅消息
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
#include <errno.h>
#include <string.h>
#include <sys/time.h> static void exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit();
} int main(int argc,char **argv)
{
const char *conninfo;
PGconn *conn;
PGresult *res;
PGnotify *notify; conninfo = "hostaddr=192.168.147.1 port=5432 dbname=postgres user=postgres password=123456";
conn = PQconnectdb(conninfo);
if(PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr,"connection to database failed:%s",PQerrorMessage(conn));
exit_nicely(conn);
} res = PQexec(conn,"listen table_update");
if(PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr,"listen command failed:%s",PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
} PQclear(res); while()
{
//执行select 1命令来判断数据库连接是否正常,不正常则自动重连,并重新订阅通知
PGresult *res_getallrows;
res_getallrows = PQexec(conn,"select 1;");
// printf("PQresultStatus(res_getallrows)=%d\n",PQresultStatus(res_getallrows));
if(PQresultStatus(res_getallrows) != PGRES_TUPLES_OK)
{
conn = PQconnectdb(conninfo);
if(PQstatus(conn) != CONNECTION_OK)
{
usleep();
continue;
}
else
{
res = PQexec(conn,"listen table_update");
if(PQresultStatus(res) != PGRES_COMMAND_OK)
{
PQclear(res);
usleep();
continue;
}
}
} PQconsumeInput(conn);
while((notify = PQnotifies(conn)) != NULL)
{
fprintf(stderr,"async notify of '%s' received from backend PID %d,extra:%s\n",notify->relname,notify->be_pid,notify->extra);
PQfreemem(notify);
} usleep();
} fprintf(stderr,"Done.\n");
PQfinish(conn);
return ;
}
编译:gcc -I/opt/pgsql/include -L/opt/pgsql/lib -o test testpq.c -lpq
五、测试
服务器端执行sql
insert into weather values('nanjing',20,40,0.25,'2018-06-29');
update weather set temp_lo = 18 where city = 'nanjing';
delete from weather where city = 'nanjing'
应用程序获取表变化时间

postgresql获取表最后更新时间(通过发布订阅机制将消息发送给应用程序)的更多相关文章
- postgresql获取表最后更新时间(通过触发器将时间写入另外一张表)
通过触发器方式获取表最后更新时间,并将时间信息写入到另外一张表 一.创建测试表和表记录更新时间表 CREATE TABLE weather( city varchar(80), temp_lo int ...
- postgresql获取表最后更新时间(通过表磁盘存储文件时间)
一.创建获取表更新时间的函数 --获取表记录更新时间(通过表磁盘存储文件时间) create or replace function table_file_access_info( IN schema ...
- Postgresql两表联结更新
Postgresql两表联合更新近日使用Postgresql感到有点不好用,一个联合更新非要这样写语法才对:update d_routetripset name=b.name , descrip ...
- MYSQL查看数据表最后更新时间
MYSQL查看数据表最后更新时间 - 拨云见日 - CSDN博客 https://blog.csdn.net/warnerwu/article/details/73352774 mysql> S ...
- Akka-Cluster(2)- distributed pub/sub mechanism 分布式发布/订阅机制
上期我们介绍了cluster singleton,它的作用是保证在一个集群环境里永远会有唯一一个singleton实例存在.具体使用方式是在集群所有节点部署ClusterSingletonManage ...
- Redis发布订阅机制
1. 什么是Redis Redis是一个开源的内存数据库,它以键值对的形式存储数据.由于数据存储在内存中,因此Redis的速度很快,但是每次重启Redis服务时,其中的数据也会丢失,因此,Redis也 ...
- dedecms 获取文章发布时间和获取文章最后更新时间
文章发布时间:[field:senddate function=MyDate('m-d',@me)/] 文章最后更新时间:[field:pubdate function=MyDate('m-d',@m ...
- Redis 发布/订阅机制原理分析
Redis 通过 PUBLISH. SUBSCRIBE 和 PSUBSCRIBE 等命令实现发布和订阅功能. 这些命令被广泛用于构建即时通信应用,比如网络聊天室(chatroom)和实时广播.实时 ...
- NATS—发布/订阅机制
概念 发布/订阅(Publish/subscribe 或pub/sub)是一种消息范式,消息的发送者(发布者)不是计划发送其消息给特定的接收者(订阅者).而是发布的消息分为不同的类别,而不需要知道什么 ...
随机推荐
- 2019JS必看面试题
2019JS必看面试题:https://www.jianshu.com/p/f1f39d5b2a2e 1. javascript的typeof返回哪些数据类型. 答案:string,boolean,n ...
- 《剑指offer》面试题26 复杂链表的复制 Java版
(定义一个新的数据结构,每个节点除了具有普通链表的next域外,还有一个额外的引用指向任意节点.我们要对由该特殊数据结构形成的链表进行复制.) 我的方法:也就是克隆一个这种特殊链表,很快想到先不考虑原 ...
- 《剑指offer》面试题24 二叉搜索树的后序遍历序列 Java版
(判断一个元素均不相同的序列是否为一个BST的LRD) 书中方法:首先对于二叉搜索树,左子树中的所有元素小于根节点小于右子树中的所有元素,然后后序遍历序列最后一个元素是根节点,这是我们已知的条件.这道 ...
- exosip2 build
Build eXosip on Win 1. download exosip http://savannah.nongnu.org/projects/exosip/ 2. download libc ...
- [FJOI2007]轮状病毒 题解(dp(找规律)+高精度)
[FJOI2007]轮状病毒 题解(dp(找规律)+高精度) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1335733 没什么好说的,直接把规律找出来,有 ...
- TMS320F28335——SCI串口
一.IO配置 以SCIA为例:使用的是GPIO35--SCITXDA GPIOA36--SCIRXDA 使用寄存器: GPBPUD :设置上拉 GPIO32-GPIO63 对应位0 使能上 ...
- Self-Driving Database
最近一直在做 ML in Database 相关的工作.偶然发现CMU 19spring的15-721课程竟然专门安排了这个专题,不禁欣喜若狂,赶紧去学习了一下. Andy提出了self-drivin ...
- java字符流读取文件
package ba; import java.io.*; public class zifuTest { public static void main(String[] args) { FileI ...
- HTML-图片和多媒体
1.图片和多媒体 (1) 图片:img元素 src 属性:图片路径: alt 属性:图片无法显示时使用的替代文字: title:鼠标悬停时显示的文字 : <img src="图片 ...
- AFNetworking2.0源码解析<一>
本篇先看看AFURLConnectionOperation,AFURLConnectionOperation继承自NSOperation,是一个封装好的任务单元,在这里构建了NSURLConnecti ...