oracle 日志解析 增量,PostgreSQL通过解析日志,获取数据库增量变化,pg_recvlogical...

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想修改 pg_hba.conf 和 postgresql.conf 文件以便连接 PostgreSQL 数据库客户端,可以按照以下步骤进行: 1. 修改 pg_hba.conf 配置文件 打开 pg_hba.conf 配置文件,修改以下内容: ``` # TYPE DATABASE USER ADDRESS METHOD local all all trust host all all 127.0.0.1/32 trust host all all ::1/128 trust ``` 将所有的认证方法(METHOD)都改为 `trust`,这样客户端连接 PostgreSQL 时就不需要输入用户名和密码了。当然,这种配置方式不够安全,建议生产环境下不要这样配置。 修改完毕后,记得保存并退出该文件。 2. 修改 postgresql.conf 配置文件 打开 postgresql.conf 配置文件,修改以下内容: ``` listen_addresses = '*' ``` 将 listen_addresses 参数的值改为 `*`,表示监听所有的 IP 地址。这样客户端就可以使用任何 IP 地址连接 PostgreSQL 数据库了。 修改完毕后,同样要保存并退出该文件。 3. 重启 PostgreSQL 服务 修改完这两个配置文件后,需要重启 PostgreSQL 服务才能生效。你可以使用以下命令重启 PostgreSQL 服务: ``` sudo systemctl restart postgresql ``` 修改完以上配置后,你就可以使用任何客户端连接到 PostgreSQL 数据库了。在连接时,你可以使用以下命令: ``` psql -h <server_ip> -U <username> -d <database_name> ``` 其中,`<server_ip>` 是 PostgreSQL 服务器的 IP 地址或主机名,`<username>` 是要连接的 PostgreSQL 用户名,`<database_name>` 是要连接的 PostgreSQL 数据库名称。根据你的实际情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值