一、创建测试表

  1. CREATE TABLE weather(
  2. city varchar(80),
  3. temp_lo int, --最低温度
  4. temp_hi int, --最高温度
  5. prcp real, --湿度
  6. date date
  7. );

二、创建触发器函数

  1. create or replace function table_update_notify() returns trigger as $$
  2. begin
  3. perform pg_notify('table_update',json_build_object('table',TG_TABLE_NAME,'timestamp',current_timestamp)::text);
  4. return new;
  5. end;
  6. $$ language plpgsql;

三、创建触发器

  1. drop trigger if exists n_weather_u on weather;
  2. create trigger n_weather_u after insert or update or delete on weather
  3. for each statement execute procedure table_update_notify();

四、应用程序代码

代码有重连机制,数据库服务器停止或者网络断开应用程序不退出,数据库服务起来或者网络恢复后应用程序会自动重连并重新订阅消息

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <libpq-fe.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <sys/time.h>
  7.  
  8. static void exit_nicely(PGconn *conn)
  9. {
  10. PQfinish(conn);
  11. exit();
  12. }
  13.  
  14. int main(int argc,char **argv)
  15. {
  16. const char *conninfo;
  17. PGconn *conn;
  18. PGresult *res;
  19. PGnotify *notify;
  20.  
  21. conninfo = "hostaddr=192.168.147.1 port=5432 dbname=postgres user=postgres password=123456";
  22. conn = PQconnectdb(conninfo);
  23. if(PQstatus(conn) != CONNECTION_OK)
  24. {
  25. fprintf(stderr,"connection to database failed:%s",PQerrorMessage(conn));
  26. exit_nicely(conn);
  27. }
  28.  
  29. res = PQexec(conn,"listen table_update");
  30. if(PQresultStatus(res) != PGRES_COMMAND_OK)
  31. {
  32. fprintf(stderr,"listen command failed:%s",PQerrorMessage(conn));
  33. PQclear(res);
  34. exit_nicely(conn);
  35. }
  36.  
  37. PQclear(res);
  38.  
  39. while()
  40. {
  41. //执行select 1命令来判断数据库连接是否正常,不正常则自动重连,并重新订阅通知
  42. PGresult *res_getallrows;
  43. res_getallrows = PQexec(conn,"select 1;");
  44. // printf("PQresultStatus(res_getallrows)=%d\n",PQresultStatus(res_getallrows));
  45. if(PQresultStatus(res_getallrows) != PGRES_TUPLES_OK)
  46. {
  47. conn = PQconnectdb(conninfo);
  48. if(PQstatus(conn) != CONNECTION_OK)
  49. {
  50. usleep();
  51. continue;
  52. }
  53. else
  54. {
  55. res = PQexec(conn,"listen table_update");
  56. if(PQresultStatus(res) != PGRES_COMMAND_OK)
  57. {
  58. PQclear(res);
  59. usleep();
  60. continue;
  61. }
  62. }
  63. }
  64.  
  65. PQconsumeInput(conn);
  66. while((notify = PQnotifies(conn)) != NULL)
  67. {
  68. fprintf(stderr,"async notify of '%s' received from backend PID %d,extra:%s\n",notify->relname,notify->be_pid,notify->extra);
  69. PQfreemem(notify);
  70. }
  71.  
  72. usleep();
  73. }
  74.  
  75. fprintf(stderr,"Done.\n");
  76. PQfinish(conn);
  77. return ;
  78. }

编译:gcc -I/opt/pgsql/include -L/opt/pgsql/lib -o test testpq.c -lpq

五、测试

服务器端执行sql

  1. insert into weather values('nanjing',20,40,0.25,'2018-06-29');
  2. update weather set temp_lo = 18 where city = 'nanjing';
  3. 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. Javascript原型介绍

    原型及原型链 原型基础概念 function Person () { this.name = 'John'; } var person = new Person(); Person.prototype ...

  2. IntelliJ IDLE

    目录 IntelliJ IDLE java项目结构 代码生成 psvm sout 快捷键 更改代码提示快捷键 代码相关快捷键 窗口相关快捷键 Debug快捷键 主题导入 下载主题 导入主题 应用主题 ...

  3. RabbitMQ几种队列模式

  4. mysql支持的存储引擎

    1.InnoDB 存储引擎 支持事务,其设计目标主要面向联机事务处理(OLTP)的应用.其特点是行锁设计.支持外键,并支持类似 Oracle 的非锁定读,即默认读取操作不会产生锁. 从 MySQL 5 ...

  5. Java 组件化(gradle)

    组件化什么是组件化,直接看下面两张图. 上面是非组件化的项目,下面是组件化的项目. 非组件化的问题如果项目本身有多个互相不影响的模块,甚至有多人分开负责各个模块的开发时,非组件化项目的弊端就会暴露出来 ...

  6. python 查询Neo4j多节点的多层关系

    需求:查询出满足3人及3案有关系的集合 # -*- coding: utf-8 -*- from py2neo import Graph import psycopg2 # 二维数组查找 def fi ...

  7. 下载xlsx文件打开一直提示文件已损坏

    这是office受保护视图导致的原因所造成的,按照以下操作,问题就不是问题了 解决办法如下: 1.在打开excel2018数据表格时,出现提示“文件已损坏,无法打开”,点击确定按钮 2.进入空白程序界 ...

  8. vue2.0 之 生命周期

     一.vue1.x与vue2.x生命周期的变化区别及含义表(图表摘自网络)   二.vue2.x生命周期图和各阶段具体含义 beforecreated:el 和 data 并未初始化 created: ...

  9. git上拉项目

  10. Python 基本数据类型详解

    1.数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647在64位系统上,整数的位数为64位,取值范围为-2* ...