1.首先用该工具来看我们的日志变化,需要先将test_decoding插件编译并安装(进入contrib,编译安装即可)

创建一个slot

SELECT * FROM pg_create_logical_replication_slot('regression_slot', 'test_decoding’);

获取slot记录的变更日志:

postgres=# insert into test values(1, 'test', now());

INSERT 0 1

postgres=# insert into test values(1, 'test', now());

INSERT 0 1

只获取变化,不消费slot

postgres=# SELECT * FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL);

lsn    | xid |                                                             data

-----------+-----+-------------------------------------------------------------------------------------------------------------------------------

0/16637F8 | 572 | BEGIN 572

0/16637F8 | 572 | table public.test: INSERT: id[integer]:1 info[text]:'test' crt_time[timestamp without time zone]:'2019-05-27 11:52:08.147527'

0/1663B20 | 572 | COMMIT 572

0/1663B58 | 573 | BEGIN 573

0/1663B58 | 573 | table public.test: INSERT: id[integer]:1 info[text]:'test' crt_time[timestamp without time zone]:'2019-05-27 11:52:09.67556'

0/1663BD8 | 573 | COMMIT 573

(6 rows)

postgres=# select * from pg_replication_slots ;

slot_name    |    plugin     | slot_type | datoid | database | temporary | active | active_pid | xmin | catalog_xmin | restart_lsn | confirmed_flush_lsn

-----------------+---------------+-----------+--------+----------+-----------+--------+------------+------+--------------+-------------+---------------------

regression_slot | test_decoding | logical   |  13237 | postgres | f         | f      |            |      |          572 | 0/16637C0   | 0/16637F8

(1 row)

可以看到xmin还是572,且能再次查询到变化:

postgres=# select * from pg_replication_slots ;

slot_name    |    plugin     | slot_type | datoid | database | temporary | active | active_pid | xmin | catalog_xmin | restart_lsn | confirmed_flush_lsn

-----------------+---------------+-----------+--------+----------+-----------+--------+------------+------+--------------+-------------+---------------------

regression_slot | test_decoding | logical   |  13237 | postgres | f         | f      |            |      |          573 | 0/1663B20   | 0/1663CF0

(1 row)

postgres=# select pg_logical_slot_get_changes('regression_slot', NULL, null); pg_logical_slot_get_changes

-----------------------------

(0 rows)

2.远程使用pg_recvlogical工具来回放:

创建slot,当然也可以使用原来有的:

pg_recvlogical --create-slot -S test_logical -h pg36 -d postgres -p 5433

postgres=# select * from pg_replication_slots ;

slot_name    |    plugin     | slot_type | datoid | database | temporary | active | active_pid | xmin | catalog_xmin | restart_lsn | confirmed_flush_lsn

-----------------+---------------+-----------+--------+----------+-----------+--------+------------+------+--------------+-------------+---------------------

regression_slot | test_decoding | logical   |  13237 | postgres | f         | f      |            |      |          574 | 0/1663CB8   | 0/1663CF0

test_logical    | test_decoding | logical   |  13237 | postgres | f         | f      |            |      |          574 | 0/1663CF0   | 0/1663D28

(2 rows)

-bash-4.1$ pg_recvlogical  -S test_logical -h pg36 -d postgres -p 5433 --start -f -

Password:

BEGIN 574

table public.test: DELETE: (no-tuple-data)

table public.test: DELETE: (no-tuple-data)

table public.test: DELETE: (no-tuple-data)

COMMIT 574

BEGIN 575

table public.test: INSERT: id[integer]:1 info[text]:'test' crt_time[timestamp without time zone]:'2019-05-27 14:03:02.985599'

COMMIT 575

如果slot已经建立好了,而远端的pg_recvlogical停止,停止的这段时间产生的数据库变化是不会被消费的,启动后可以接收到所有的变化。

3.可以设置从什么时候开始接收回放日志:

postgres=# select pg_current_wal_lsn();

pg_current_wal_lsn

--------------------

0/1664FB8

(1 row)

postgres=# delete from test where id = 1;

DELETE 7

postgres=# select txid_current();

txid_current

--------------

585

(1 row)

postgres=# select txid_current();

txid_current

--------------

586

(1 row)

postgres=#

postgres=#

postgres=# insert into test values(1, 'test 2', now());

INSERT 0 1

postgres=# select pg_current_wal_lsn();

pg_current_wal_lsn

--------------------

0/16652C0

(1 row)

postgres=# insert into test values(1, 'test 3', now());

INSERT 0 1

-bash-4.1$ pg_recvlogical  -S test_logical -h pg36 -d postgres -p 5433 --startpos=0/16652C0 --start -f -

Password:

BEGIN 588

table public.test: INSERT: id[integer]:1 info[text]:'test 3' crt_time[timestamp without time zone]:'2019-05-27 14:37:31.246539'

COMMIT 588

可以看到,只是从0/16652C0开始进行接收的

4.不用的时候,删除复制槽

postgres=# select * from pg_replication_slots ;

slot_name    |    plugin     | slot_type | datoid | database | temporary | active | active_pid | xmin | catalog_xmin | restart_lsn | confirmed_flush_lsn

-----------------+---------------+-----------+--------+----------+-----------+--------+------------+------+--------------+-------------+---------------------

regression_slot | test_decoding | logical   |  13237 | postgres | f         | f      |            |      |          574 | 0/1663CB8   | 0/1663CF0

test_logical    | test_decoding | logical   |  13237 | postgres | f         | t      |      13155 |      |          589 | 0/1665420   | 0/1665458

(2 rows)

postgres=# select pg_drop_replication_slot('regression_slot');

pg_drop_replication_slot

--------------------------

(1 row)

postgres=# select pg_drop_replication_slot('test_logical');

pg_drop_replication_slot

--------------------------

(1 row)

postgres=# select * from pg_replication_slots ;

slot_name | plugin | slot_type | datoid | database | temporary | active | active_pid | xmin | catalog_xmin | restart_lsn | confirmed_flush_lsn

-----------+--------+-----------+--------+----------+-----------+--------+------------+------+--------------+-------------+---------------------

(0 rows)

4.可以使用wal2json工具来解析日志,目前使用的是test_decoding

PostgreSQL通过解析日志,获取数据库增量变化,pg_recvlogical的更多相关文章

  1. canal 基于Mysql数据库增量日志解析

    canal 基于Mysql数据库增量日志解析  1.前言  最近太多事情 工作的事情,以及终身大事等等 耽误更新,由于最近做项目需要同步监听 未来电视 mysql的变更了解到公司会用canal做增量监 ...

  2. 数据库增量日志监听canal

    概述 canal是阿里巴巴旗下的一款开源项目,纯Java开发.基于数据库增量日志解析,提供增量数据订阅&消费,目前主要支持了MySQL(也支持mariaDB). 起源:早期,阿里巴巴B2B公司 ...

  3. PostgreSQL Replication之第二章 理解PostgreSQL的事务日志(1)

    在前面的章节中,我们已经理解了各种复制概念.这不仅仅是一个为了接下来将要介绍的东西而增强您的意识的理论概述,还将为您介绍大体的主题. 在本章,我们将更加接近实际的解决方案,并了解PostgreSQL内 ...

  4. (转)ASP.NET缓存全解析6:数据库缓存依赖

    ASP.NET缓存全解析文章索引 ASP.NET缓存全解析1:缓存的概述 ASP.NET缓存全解析2:页面输出缓存 ASP.NET缓存全解析3:页面局部缓存 ASP.NET缓存全解析4:应用程序数据缓 ...

  5. IIS日志存入数据库之二:ETW

    在上一篇文章<IIS日志存入数据库之一:ODBC>中,我提到了ODBC方式保存的缺点,即:无法保存响应时间以及接收和响应的字节数. 如果一定要获取响应时间以及接收和响应的字节数的话,就要另 ...

  6. PHP面试题及答案解析(3)—MySQL数据库

    1.mysql_num_rows()和mysql_affected_rows()的区别. mysql_num_rows()和mysql_affected_rows(),这两个函数都作用于 mysql_ ...

  7. PostgreSQL Replication之第二章 理解PostgreSQL的事务日志(5)

    2.5 XLOG的内部结构 我们将使用事务贯穿本书,并让您在技术层面上更深地洞察事情是如果工作的,我们已经增加了这部分专门处理XLOG的内部工作机制.我们会尽量避免前往下降到C级,因为这将超出本书的范 ...

  8. PostgreSQL Replication之第二章 理解PostgreSQL的事务日志(2)

    2.2 XLOG和复制 在本章中,您已经了解到PostgreSQL的事务日志已经对数据库做了所有的更改.事务日志本身被打包为易用的16MB段. 使用这种更改集来复制数据的想法是不牵强的.事实上,这是在 ...

  9. sql sever获取数据库还原时间语句

    --只获取数据库名称和最后的还原时间 SELECT sdb.Name AS DatabaseName , ), ), '-') AS LastBackUpTime FROM sys.sysdataba ...

随机推荐

  1. itertools模块、排列、组合、算法

    关于列表重组的python小题 题目一:给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集).             说明:解集不能包含重复的子集. 示例:输入: nums = ...

  2. Java中截取字符串中小数点前面的字符

    通过下标获取 String number = "2563.2154"; int index = number.indexOf("."); String intN ...

  3. BZOJ 2655 calc (组合计数、DP、多项式、拉格朗日插值)

    题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2655 题解 据说有一种神仙容斥做法,但我不会. 以及貌似网上大多数人的dp和我的做法都不 ...

  4. maven-profile多环境配置

    http://www.cnblogs.com/hwaggLee/p/4579462.html 具体项目激活配置环境 pom.xml文件中激活 <activation> <active ...

  5. 分布式-信息方式-ActiveMQ的Destination高级特性1

    ActiveMQ的Destination高级特性 Destination高级特性----->Composite Destinations 组合队列Composite Destinations : ...

  6. 国内著名的vue-element-admin-layout框架的使用

    vue-element-admin-layout 是一个后台前端解决方案,它基于 vue 和 element-ui实现.它使用了最新的前端技术栈,内置了 i18 国际化解决方案,动态路由,权限验证,提 ...

  7. GC详解

    GC,即就是Java垃圾回收机制.目前主流的JVM(HotSpot)采用的是分代收集算法.与C++不同的是,Java采用的是类似于树形结构的可达性分析法来判断对象是否还存在引用.即:从gcroot开始 ...

  8. JavaWeb之上传与下载

    文件上传概述: 1,文件上传对页面的要求: 必须使用表单,而不能是超链接 表单的method必须是post 表单的enctype必须是multipart/form-data 在表单中添加file表单字 ...

  9. 为Windows编译libobjc2(通过交叉编译的方式)

    前提:Linux系统.git.clang-8.g++-mingw-w64-x86-64.gobjc++-mingw-w64-x86-64. 一.下载源代码 git clone https://gith ...

  10. C# 强命名程序集,防止dll被修改,混淆下发布

    未能加载文件或程序集“Jonckers.Service.RedisCacheEngineExtend, Version=1.0.0.0, Culture=neutral, PublicKeyToken ...