|
| 1 | +## PostgreSQL 17 preview - 增加GUC standby_slot_names , 保证这些standby已接收并flush所有逻辑slot向下游发送逻辑数据对应的WAL |
| 2 | + |
| 3 | +### 作者 |
| 4 | +digoal |
| 5 | + |
| 6 | +### 日期 |
| 7 | +2024-03-09 |
| 8 | + |
| 9 | +### 标签 |
| 10 | +PostgreSQL , PolarDB , DuckDB , 逻辑复制 , 切换 , 堵塞wal sender |
| 11 | + |
| 12 | +---- |
| 13 | + |
| 14 | +## 背景 |
| 15 | +PostgreSQL 自从16版本开始支持standby开启逻辑复制, 17版本对这个功能进行了进一步的更新, 例如primary节点支持将逻辑复制slot的LSN状态同步给standby, 以便在数据库出现primary-standby HA 切换后新的primary下游逻辑subscriber可以不多不少的继续从slot订阅数据. |
| 16 | + |
| 17 | +[《PostgreSQL 17 preview - Add a new slot sync worker to synchronize logical slots》](../202402/20240223_01.md) |
| 18 | + |
| 19 | +[《PostgreSQL 17 preview - sync logical replication slot LSN, Failover & Switchover》](../202402/20240214_02.md) |
| 20 | + |
| 21 | +[《PostgreSQL 17 preview - 支持逻辑复制槽failover to 流复制standby节点. `pg_create_logical_replication_slot(... failover = true|false ...)`》](../202401/20240126_01.md) |
| 22 | + |
| 23 | +[《PostgreSQL 16 preview - standby 支持 logical decoding》](../202304/20230410_07.md) |
| 24 | + |
| 25 | +本文提到的patch Introduce a new GUC 'standby_slot_names' 就是要让数据库主从正常切换可以保证切换后新的primary下游逻辑subscriber可以不多不少的继续从slot订阅数据. |
| 26 | + |
| 27 | +原理: |
| 28 | +- wal sender在向下游发送逻辑日志信息前, 必须保证在standby_slot_names中的standby已经接收并flush了逻辑日志对应的WAL. |
| 29 | +- 所以不要随意配置standby_slot_names, 最好是性能好、网络好、稳定的standby配置在里面, 否则逻辑复制容易出现堵塞或较大delay. |
| 30 | + |
| 31 | +https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=bf279ddd1c28ce0251446ee90043a4cb96e5db0f |
| 32 | +``` |
| 33 | +Introduce a new GUC 'standby_slot_names'. |
| 34 | + |
| 35 | +author Amit Kapila <akapila@postgresql.org> |
| 36 | +Fri, 8 Mar 2024 02:40:45 +0000 (08:10 +0530) |
| 37 | +committer Amit Kapila <akapila@postgresql.org> |
| 38 | +Fri, 8 Mar 2024 02:40:45 +0000 (08:10 +0530) |
| 39 | +commit bf279ddd1c28ce0251446ee90043a4cb96e5db0f |
| 40 | +tree 642eb01435995795793fc61cedcc66949c824f1d tree |
| 41 | +parent 453c46873774219243501b8efc16b2b5a5e9d194 commit | diff |
| 42 | +Introduce a new GUC 'standby_slot_names'. |
| 43 | + |
| 44 | +This patch provides a way to ensure that physical standbys that are |
| 45 | +potential failover candidates have received and flushed changes before |
| 46 | +the primary server making them visible to subscribers. Doing so guarantees |
| 47 | +that the promoted standby server is not lagging behind the subscribers |
| 48 | +when a failover is necessary. |
| 49 | + |
| 50 | +The logical walsender now guarantees that all local changes are sent and |
| 51 | +flushed to the standby servers corresponding to the replication slots |
| 52 | +specified in 'standby_slot_names' before sending those changes to the |
| 53 | +subscriber. |
| 54 | + |
| 55 | +Additionally, the SQL functions pg_logical_slot_get_changes, |
| 56 | +pg_logical_slot_peek_changes and pg_replication_slot_advance are modified |
| 57 | +to ensure that they process changes for failover slots only after physical |
| 58 | +slots specified in 'standby_slot_names' have confirmed WAL receipt for those. |
| 59 | + |
| 60 | +Author: Hou Zhijie and Shveta Malik |
| 61 | +Reviewed-by: Masahiko Sawada, Peter Smith, Bertrand Drouvot, Ajin Cherian, Nisha Moond, Amit Kapila |
| 62 | +Discussion: https://postgr.es/m/514f6f2f-6833-4539-39f1-96cd1e011f23@enterprisedb.com |
| 63 | +``` |
| 64 | + |
| 65 | +``` |
| 66 | ++#standby_slot_names = '' # streaming replication standby server slot names that |
| 67 | ++ # logical walsender processes will wait for |
| 68 | +``` |
| 69 | + |
| 70 | +``` |
| 71 | ++ <varlistentry id="guc-standby-slot-names" xreflabel="standby_slot_names"> |
| 72 | ++ <term><varname>standby_slot_names</varname> (<type>string</type>) |
| 73 | ++ <indexterm> |
| 74 | ++ <primary><varname>standby_slot_names</varname> configuration parameter</primary> |
| 75 | ++ </indexterm> |
| 76 | ++ </term> |
| 77 | ++ <listitem> |
| 78 | ++ <para> |
| 79 | ++ A comma-separated list of streaming replication standby server slot names |
| 80 | ++ that logical WAL sender processes will wait for. Logical WAL sender processes |
| 81 | ++ will send decoded changes to plugins only after the specified replication |
| 82 | ++ slots confirm receiving WAL. This guarantees that logical replication |
| 83 | ++ failover slots do not consume changes until those changes are received |
| 84 | ++ and flushed to corresponding physical standbys. If a |
| 85 | ++ logical replication connection is meant to switch to a physical standby |
| 86 | ++ after the standby is promoted, the physical replication slot for the |
| 87 | ++ standby should be listed here. Note that logical replication will not |
| 88 | ++ proceed if the slots specified in the |
| 89 | ++ <varname>standby_slot_names</varname> do not exist or are invalidated. |
| 90 | ++ Additionally, the replication management functions |
| 91 | ++ <link linkend="pg-replication-slot-advance"> |
| 92 | ++ <function>pg_replication_slot_advance</function></link>, |
| 93 | ++ <link linkend="pg-logical-slot-get-changes"> |
| 94 | ++ <function>pg_logical_slot_get_changes</function></link>, and |
| 95 | ++ <link linkend="pg-logical-slot-peek-changes"> |
| 96 | ++ <function>pg_logical_slot_peek_changes</function></link>, |
| 97 | ++ when used with logical failover slots, will block until all |
| 98 | ++ physical slots specified in <varname>standby_slot_names</varname> have |
| 99 | ++ confirmed WAL receipt. |
| 100 | ++ </para> |
| 101 | ++ <para> |
| 102 | ++ The standbys corresponding to the physical replication slots in |
| 103 | ++ <varname>standby_slot_names</varname> must configure |
| 104 | ++ <literal>sync_replication_slots = true</literal> so they can receive |
| 105 | ++ logical failover slot changes from the primary. |
| 106 | ++ </para> |
| 107 | ++ </listitem> |
| 108 | ++ </varlistentry> |
| 109 | +``` |
| 110 | + |
| 111 | +``` |
| 112 | ++ It's highly recommended that the said physical replication slot is named in |
| 113 | ++ <link linkend="guc-standby-slot-names"><varname>standby_slot_names</varname></link> |
| 114 | ++ list on the primary, to prevent the subscriber from consuming changes |
| 115 | ++ faster than the hot standby. Even when correctly configured, some latency |
| 116 | ++ is expected when sending changes to logical subscribers due to the waiting |
| 117 | ++ on slots named in |
| 118 | ++ <link linkend="guc-standby-slot-names"><varname>standby_slot_names</varname></link>. |
| 119 | ++ When <varname>standby_slot_names</varname> is utilized, the |
| 120 | ++ primary server will not completely shut down until the corresponding |
| 121 | ++ standbys, associated with the physical replication slots specified |
| 122 | ++ in <varname>standby_slot_names</varname>, have confirmed |
| 123 | ++ receiving the WAL up to the latest flushed position on the primary server. |
| 124 | +``` |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | +#### [期望 PostgreSQL|开源PolarDB 增加什么功能?](https://github.com/digoal/blog/issues/76 "269ac3d1c492e938c0191101c7238216") |
| 129 | + |
| 130 | + |
| 131 | +#### [PolarDB 开源数据库](https://openpolardb.com/home "57258f76c37864c6e6d23383d05714ea") |
| 132 | + |
| 133 | + |
| 134 | +#### [PolarDB 学习图谱](https://www.aliyun.com/database/openpolardb/activity "8642f60e04ed0c814bf9cb9677976bd4") |
| 135 | + |
| 136 | + |
| 137 | +#### [购买PolarDB云服务折扣活动进行中, 55元起](https://www.aliyun.com/activity/new/polardb-yunparter?userCode=bsb3t4al "e0495c413bedacabb75ff1e880be465a") |
| 138 | + |
| 139 | + |
| 140 | +#### [PostgreSQL 解决方案集合](../201706/20170601_02.md "40cff096e9ed7122c512b35d8561d9c8") |
| 141 | + |
| 142 | + |
| 143 | +#### [德哥 / digoal's Github - 公益是一辈子的事.](https://github.com/digoal/blog/blob/master/README.md "22709685feb7cab07d30f30387f0a9ae") |
| 144 | + |
| 145 | + |
| 146 | +#### [About 德哥](https://github.com/digoal/blog/blob/master/me/readme.md "a37735981e7704886ffd590565582dd0") |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + |
0 commit comments