一、创建测试表

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获取表最后更新时间(通过发布订阅机制将消息发送给应用程序)的更多相关文章

  1. postgresql获取表最后更新时间(通过触发器将时间写入另外一张表)

    通过触发器方式获取表最后更新时间,并将时间信息写入到另外一张表 一.创建测试表和表记录更新时间表 CREATE TABLE weather( city varchar(80), temp_lo int ...

  2. postgresql获取表最后更新时间(通过表磁盘存储文件时间)

    一.创建获取表更新时间的函数 --获取表记录更新时间(通过表磁盘存储文件时间) create or replace function table_file_access_info( IN schema ...

  3. Postgresql两表联结更新

    Postgresql两表联合更新近日使用Postgresql感到有点不好用,一个联合更新非要这样写语法才对:update d_routetripset name=b.name ,    descrip ...

  4. MYSQL查看数据表最后更新时间

    MYSQL查看数据表最后更新时间 - 拨云见日 - CSDN博客 https://blog.csdn.net/warnerwu/article/details/73352774 mysql> S ...

  5. Akka-Cluster(2)- distributed pub/sub mechanism 分布式发布/订阅机制

    上期我们介绍了cluster singleton,它的作用是保证在一个集群环境里永远会有唯一一个singleton实例存在.具体使用方式是在集群所有节点部署ClusterSingletonManage ...

  6. Redis发布订阅机制

    1. 什么是Redis Redis是一个开源的内存数据库,它以键值对的形式存储数据.由于数据存储在内存中,因此Redis的速度很快,但是每次重启Redis服务时,其中的数据也会丢失,因此,Redis也 ...

  7. dedecms 获取文章发布时间和获取文章最后更新时间

    文章发布时间:[field:senddate function=MyDate('m-d',@me)/] 文章最后更新时间:[field:pubdate function=MyDate('m-d',@m ...

  8. Redis 发布/订阅机制原理分析

    Redis 通过 PUBLISH. SUBSCRIBE 和 PSUBSCRIBE 等命令实现发布和订阅功能.   这些命令被广泛用于构建即时通信应用,比如网络聊天室(chatroom)和实时广播.实时 ...

  9. NATS—发布/订阅机制

    概念 发布/订阅(Publish/subscribe 或pub/sub)是一种消息范式,消息的发送者(发布者)不是计划发送其消息给特定的接收者(订阅者).而是发布的消息分为不同的类别,而不需要知道什么 ...

随机推荐

  1. vue vuex应用

    vue结构图: vuex为vue的一个插件,用来管理共享数据的,局部数据声明在自己组件内部. 没有使用vuex时,所有共享数据和操作数据的方法都声明在父组件内,数据的通信用props及pubsub等. ...

  2. AcWing 875. 快速幂

    题目链接:https://www.acwing.com/problem/content/description/877/ 快速幂模板题,计算ab mod p 的值,a,b,p大概1e9左右,可以快速计 ...

  3. C++中的const分析

    1,C 语言中的 const: 1,const 修饰的变量是只读的,本质还是变量: 1,C 语言中的 const 使变量具有只读属性: 2,const 只在编译期有用,在运行期无用: 3,const ...

  4. Thinkphp3.2 Redis缓存session

    Thinkphpsession缓存没有redis类库 Redis.class.php放在Library/Think/Session/Driver/下: <?php /** * +-------- ...

  5. centos7 安装redis 出现cc: command not found错误解决

    安装过程 1. 下载并解压 cd /root/software wget http://download.redis.io/releases/redis-3.2.4.tar.gz tar -zxvf ...

  6. 模板 - 无旋Treap

    一般而言作为一棵平衡树只需要插入,删除,值求排名,排名求值,前驱,后继,六个接口. #include<bits/stdc++.h> using namespace std; typedef ...

  7. Views的补充

    views的补充 请求头一般与请求内容用/r/n/r/n隔开 请求头包含的内容 request.Meta(...) 一般在下面几种方法里面取不到的东西需要去原生的头里面去取,比如用户的终端类型 req ...

  8. linux中的文件类型以及查看文件类型的方法

    Linux文件类型和文件的文件名所代表的意义是两个不同的概念,在linux中文件类型与文件扩展名没有关系.它不像Windows那样是依靠文件后缀名来区分文件类型的,在linux中文件名只是为了方便操作 ...

  9. debian利用snmp监控服务器异常状态

    1.安装snmp apt-get install snmp snmpd 2.配置snmp vi /etc/snmp/snmpd.conf 注释15行 #agentAddress udp:127.0.0 ...

  10. netserver启动时报错 "Unable to start netserver with 'IN(6)ADDR_ANY' port '12865' and family AF_UNSPEC'"

    netperf启动netserver时报错 "Unable to start netserver with 'IN(6)ADDR_ANY' port '12865' and family A ...