PostgreSQL逻辑复制在CREATE SUBSCRIPTION上出现停滞。

huangapple go评论66阅读模式
英文:

PostgreSQL logical replication hangs on CREATE SUBSCRIPTION

问题

我有一个关于PostgreSQL逻辑复制的问题,版本15(我也在v10和v12上进行了测试,但问题相同)。
它需要进行测试的复制,因此源数据库和目标数据库位于同一服务器上。

在配置文件中,我已经设置了:

postgresql.conf

wal_level = logical  # 如果我设置为“replica”,则无法订阅

pg_hba.conf

local replication all
host replication all 127.0.0.1/32 trust
host replication all ::1/128   trust

在源数据库中,我执行以下操作:

CREATE PUBLICATION repl_name
   FOR TABLE table1, table2, table3, ...;

在pgAdmin中,此时我有:

CREATE PUBLICATION
Query returned successfully in 59 msec.

然而,在日志中,对于每个表,我都有以下消息:

2023-05-17 17:02:07.537 CEST [25356] ERROR: role "backup_user" does not exist
2023-05-17 17:02:07.537 CEST [25356] STATEMENT:  GRANT SELECT ON SEQUENCE public.table1 TO backup_user;

这可能与从此复制正常工作的客户端还原备份到我的计算机有关。

但我不知道这是否会有影响,因为在键入以下命令后:

select * from pg_catalog.pg_publication;

我可以看到我的发布:

"20438"   "stack_repl"     "10"    false   true    true    true    true    false

但我在这里看不到:

select * from pg_stat_replication;

我认为这可能与尚未存在订阅有关。

当我尝试创建订阅时,真正的问题出现了。
首先,它还原了具有类似table1table2table3等的表的数据库。自然,表是空的。

我输入以下命令:

CREATE SUBSCRIPTION sub_test
   CONNECTION 'dbname=dbname host=localhost port=5432 user=postgres password=12345'
   PUBLICATION repl_name;

而pgAdmin一直在无限旋转。即使我把电脑留了一个周末,它也无法完成。

在日志中显示:

2023-05-17 17:22:24.178 CEST [25376] LOG:  logical decoding found initial starting point at 0/A0D6F338
2023-05-17 17:22:24.178 CEST [25376] DETAIL:  Waiting for transactions (approximately 1) older than 3712 to end.
2023-05-17 17:22:24.178 CEST [25376] STATEMENT:  CREATE_REPLICATION_SLOT "sub_test" LOGICAL pgoutput (SNAPSHOT 'nothing')

我向您保证,我已经为此问题努力了很长时间,我需要支持/提示,以便了解如何开始复制数据。

英文:

I have a problem with PostgreSQL logical replication, version 15. (I also tested on v10 and v12, but had the same problem).
It needs replication for testing, so the source and target databases are on the same server.

In the configuration files I have set:

postgresql.conf:

wal_level = logical  # if I have "replica", I can't subscribe

pg_hba.conf:

local replication all
host replication all 127.0.0.1/32 trust
host replication all ::1/128               trust

In the source database I do:

CREATE PUBLICATION repl_name
   FOR TABLE table1, table2, table3, ...;

In pgAdmin at this stage I have:

CREATE PUBLICATION
Query returned successfully in 59 msec.

However, in the logs I have a message for each table:

2023-05-17 17:02:07.537 CEST [25356] ERROR: role "backup_user" does not exist
2023-05-17 17:02:07.537 CEST [25356] STATEMENT:  GRANT SELECT ON SEQUENCE public.table1 TO backup_user;

This may be related to the fact that on my computer it is restoring the backup from the client where this replication works.

But I don't know if it bothers you, because after typing:

select * from pg_catalog.pg_publication;

I can see my publications:

"20438"   "stack_repl"    "10"   false   true    true   true   true   false

But I don't see it here:

select * from pg_stat_replication;

I think it may have to do with the fact that there is no subscription yet.

The real problem occurs when I want to make a subscription.
First, it restores the database with tables like table1, table2, table3 etc. Naturally, the tables are empty.

I enter the command:

CREATE SUBSCRIPTION sub_test
   CONNECTION 'dbname=dbname host=localhost port=5432 user=postgres password=12345'
   PUBLICATION repl_name;

And pgAdmin spins endlessly. Even when I left the computer for the weekend, it couldn't finish.

It shows up in the logs:

2023-05-17 17:22:24.178 CEST [25376] LOG:  logical decoding found initial starting point at 0/A0D6F338
2023-05-17 17:22:24.178 CEST [25376] DETAIL:  Waiting for transactions (approximately 1) older than 3712 to end.
2023-05-17 17:22:24.178 CEST [25376] STATEMENT:  CREATE_REPLICATION_SLOT "sub_test" LOGICAL pgoutput (SNAPSHOT 'nothing')

I assure you that I have been struggling with this problem for a long time and I need support / hints on what I can do to make the data start replicating.

答案1

得分: 2

你可能正在设置两个数据库间的逻辑复制,这会导致CREATE SUBSCRIPTION永远挂起,正如文档所述:

> 创建一个连接到相同数据库集群的订阅(例如,在同一集群中的数据库之间复制,或在同一数据库内复制)只有在不作为同一命令的一部分创建复制槽时才会成功。否则,CREATE SUBSCRIPTION调用将挂起。要使其正常工作,请单独创建复制槽(使用函数pg_create_logical_replication_slot和插件名称pgoutput),然后使用参数create_slot = false创建订阅。这是可能在将来的版本中取消的实现限制。

因此,这是成功的步骤:

  • 连接到主数据库并创建槽:

    SELECT pg_create_logical_replication_slot('sub_test', 'pgoutput');
    
  • 然后连接到备用数据库并运行:

    CREATE SUBSCRIPTION sub_test
       CONNECTION 'dbname=dbname host=localhost port=5432 user=postgres password=12345'
       PUBLICATION repl_name
       WITH (create_slot = false);
    
英文:

You are probably setting up logical replication between two databases in the same database cluster. That makes CREATE SUBSCRIPTION hang forever, as the documentation describes:

> Creating a subscription that connects to the same database cluster (for example, to replicate between databases in the same cluster or to replicate within the same database) will only succeed if the replication slot is not created as part of the same command. Otherwise, the CREATE SUBSCRIPTION call will hang. To make this work, create the replication slot separately (using the function pg_create_logical_replication_slot with the plugin name pgoutput) and create the subscription using the parameter create_slot = false. This is an implementation restriction that might be lifted in a future release.

So this is the route to success:

  • connect to the primary database and create the slot:

    SELECT pg_create_logical_replication_slot('sub_test', 'pgoutput');
    
  • then connect to the standby database and run:

    CREATE SUBSCRIPTION sub_test
       CONNECTION 'dbname=dbname host=localhost port=5432 user=postgres password=12345'
       PUBLICATION repl_name
       WITH (create_slot = false);
    

huangapple
  • 本文由 发表于 2023年5月18日 00:18:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定