一、创建测试表

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. W10: Warning: Changing a readonly file 解决办法

    在linux上编辑文件的时候,明明是使用的root登录的,可是这种至高无上的权限在按下i的时候被那串红色错误亵渎了W10: Warning: Changing a readonly file. 困扰两 ...

  2. T-聊天止于呵呵

    (现代版)俗话说:流言止于智者,聊天止于呵呵.输入一段聊天记录,你的任务是数一数有 多少段对话“止于呵呵”,即对话的最后一句话包含单词 hehe 或者它的变形. 具体来说,我们首先提取出对话的最后一句 ...

  3. goaccess实现nginx实现日志可视化

    一:goaccess模块 goaccess模块是将nginx的日志定向到goaccess模块里实现,原理很简单,只要将文件定向到那里就可以,goaccess是已经集成好显示数据的一个页面,非常的美观和 ...

  4. 统计学习方法——第二章的c++实现

    1.东西搞丢了,只写一部分 2.算法那收敛性 证明逻辑: a.γ是yi*(wopt*Xi)最小值 b.R是Xi最小值 c.k<=(R/γ)^2 难打公式,直接说,Wk由Wk-1迭代而来,所以事实 ...

  5. .net Core——SqlSugar使用

    一.DbContext配置 public class DbContext { public DbContext() { Db = new SqlSugarClient(new ConnectionCo ...

  6. CentOS 7 环境下部署 SVN 并实现自动更新(版本库放在Tomcat下)

    1.安装 SVN 1.1先检查是否有安装 svn rpm -qa subversion #没有什么显示就说明没有安装过yum remove subversion #如果有安装就运行删除老版本yum i ...

  7. Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition (实现,贪心,排序)

    C. Multi-Subject Competition time limit per test2 seconds memory limit per test256 megabytes inputst ...

  8. 自学semantic UI个人博客首页模板

    以下是代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <m ...

  9. python 7行代码实现微信机器人自动回复

    首先 需要去 图灵 网站注册 获取api_key 先上代码 from wxpy import * bot = Bot() tuling = Tuling(api_key='你的api_key') @b ...

  10. :last-child----represents the last element among a group of sibling elements.

    CSS伪类 :last-child 代表在一群兄弟元素中的最后一个元素. 举个例子: 从代码和图可以看出:last-child选择了最后一个li标签. 同时,我们换另外一段代码,看看是否还有这样的效果 ...