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)是一种消息范式,消息的发送者(发布者)不是计划发送其消息给特定的接收者(订阅者).而是发布的消息分为不同的类别,而不需要知道什么 ...
随机推荐
- 【JZOJ 3910】Idiot 的间谍网络
题面: Description 作为一名高级特工,Idiot 苦心经营多年,终于在敌国建立起一张共有n 名特工的庞大间谍网络. 当然,出于保密性的要求,间谍网络中的每名特工最多只会有一名直接领导.现在 ...
- 剑指offer-顺序打印二叉树节点(系列)-树-python
转载自 https://blog.csdn.net/u010005281/article/details/79761056 非常感谢! 首先创建二叉树,然后按各种方式打印: class treeNo ...
- Spring基础10——Bean之间关系
1.前言 不同的Bean之间存在两种关系:继承和依赖,这里的继承与java中的继承不同,它指的是配置上的继承. 2.继承bean配置 Spring允许继承bean的配置,被继承的bean成为父bean ...
- java程序员究竟应该掌握点什么
JAVA程序设计(基础部分) JAVA程序设计(专题)
- Nginx 的全局和虚拟主机配置
Httpd.conf nginx.conf my-heavy-innode-4G.cnf php.ini 用中文注释 # user:指定 Nginx Worker 进程运行用户和用户组,默认 nob ...
- bash_profile和bashrc区别
[.bash_profile 与 .bashrc 的区别].bash_profile is executed for login shells, while .bashrc is executed f ...
- idea的使用技巧
* 简介:程序员每日都会花费数小时使用ide编写和调试代码,其中很多操作都是机械重复且频率非常高,本着"工欲善其事必先利其器"的精神,闷头写代码之外花点时间研究一下自己用的ide, ...
- 04-A的LU分解
一.矩阵$AB$的逆 $(AB)^{-1}=B^{-1}A^{-1}$,顺序正好相反 二.$A=LU$ 如矩阵: $\left[\begin{array}{ll}{2} & {1} \\ {8 ...
- GUI学习之二十八—QMessageBox
今天来学习下QMessageBox. QMessageBox主要用来通知用户或者请求用户提问和接收应答一个模态对话框. 一.对话框的构成 图标是有标准图标的,可以直接调用. 我们声明的消息框,初始状态 ...
- mnist 卷积神经网络
# from keras.models import Sequential# from keras.layers.core import Dense,Activation,Flatten #creat ...