1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>29.3. Logical Replication Failover</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="logical-replication-subscription.html" title="29.2. Subscription" /><link rel="next" href="logical-replication-row-filter.html" title="29.4. Row Filters" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">29.3. Logical Replication Failover</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="logical-replication-subscription.html" title="29.2. Subscription">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="logical-replication.html" title="Chapter 29. Logical Replication">Up</a></td><th width="60%" align="center">Chapter 29. Logical Replication</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="logical-replication-row-filter.html" title="29.4. Row Filters">Next</a></td></tr></table><hr /></div><div class="sect1" id="LOGICAL-REPLICATION-FAILOVER"><div class="titlepage"><div><div><h2 class="title" style="clear: both">29.3. Logical Replication Failover <a href="#LOGICAL-REPLICATION-FAILOVER" class="id_link">#</a></h2></div></div></div><p>
3 To allow subscriber nodes to continue replicating data from the publisher
4 node even when the publisher node goes down, there must be a physical standby
5 corresponding to the publisher node. The logical slots on the primary server
6 corresponding to the subscriptions can be synchronized to the standby server by
7 specifying <code class="literal">failover = true</code> when creating subscriptions. See
8 <a class="xref" href="logicaldecoding-explanation.html#LOGICALDECODING-REPLICATION-SLOTS-SYNCHRONIZATION" title="47.2.3. Replication Slot Synchronization">Section 47.2.3</a> for details.
10 <a class="link" href="sql-createsubscription.html#SQL-CREATESUBSCRIPTION-PARAMS-WITH-FAILOVER"><code class="literal">failover</code></a>
11 parameter ensures a seamless transition of those subscriptions after the
12 standby is promoted. They can continue subscribing to publications on the
15 Because the slot synchronization logic copies asynchronously, it is
16 necessary to confirm that replication slots have been synced to the standby
17 server before the failover happens. To ensure a successful failover, the
18 standby server must be ahead of the subscriber. This can be achieved by
20 <a class="link" href="runtime-config-replication.html#GUC-SYNCHRONIZED-STANDBY-SLOTS"><code class="varname">synchronized_standby_slots</code></a>.
22 To confirm that the standby server is indeed ready for failover for a given subscriber, follow these
23 steps to verify that all the logical replication slots required by that subscriber have been
24 synchronized to the standby server:
25 </p><div class="procedure"><ol class="procedure" type="1"><li class="step"><p>
26 On the subscriber node, use the following SQL to identify which replication
27 slots should be synced to the standby that we plan to promote. This query
28 will return the relevant replication slots associated with the
29 failover-enabled subscriptions.
30 </p><pre class="programlisting">
32 array_agg(quote_literal(s.subslotname)) AS slots
33 FROM pg_subscription s
34 WHERE s.subfailover AND
35 s.subslotname IS NOT NULL;
38 {'sub1','sub2','sub3'}
40 </pre></li><li class="step"><p>
41 On the subscriber node, use the following SQL to identify which table
42 synchronization slots should be synced to the standby that we plan to promote.
43 This query needs to be run on each database that includes the failover-enabled
44 subscription(s). Note that the table sync slot should be synced to the standby
45 server only if the table copy is finished
46 (See <a class="xref" href="catalog-pg-subscription-rel.html" title="52.55. pg_subscription_rel">Section 52.55</a>).
47 We don't need to ensure that the table sync slots are synced in other scenarios
48 as they will either be dropped or re-created on the new primary server in those
50 </p><pre class="programlisting">
52 array_agg(quote_literal(slot_name)) AS slots
55 SELECT CONCAT('pg_', srsubid, '_sync_', srrelid, '_', ctl.system_identifier) AS slot_name
56 FROM pg_control_system() ctl, pg_subscription_rel r, pg_subscription s
57 WHERE r.srsubstate = 'f' AND s.oid = r.srsubid AND s.subfailover
61 {'pg_16394_sync_16385_7394666715149055164'}
63 </pre></li><li class="step"><p>
64 Check that the logical replication slots identified above exist on
65 the standby server and are ready for failover.
66 </p><pre class="programlisting">
67 /* standby # */ SELECT slot_name, (synced AND NOT temporary AND invalidation_reason IS NULL) AS failover_ready
68 FROM pg_replication_slots
70 ('sub1','sub2','sub3', 'pg_16394_sync_16385_7394666715149055164');
71 slot_name | failover_ready
72 --------------------------------------------+----------------
76 pg_16394_sync_16385_7394666715149055164 | t
78 </pre></li></ol></div><p>
79 If all the slots are present on the standby server and the result
80 (<code class="literal">failover_ready</code>) of the above SQL query is true, then
81 existing subscriptions can continue subscribing to publications on the new
84 The first two steps in the above procedure are meant for a
85 <span class="productname">PostgreSQL</span> subscriber. It is recommended to run
86 these steps on each subscriber node, that will be served by the designated
87 standby after failover, to obtain the complete list of replication
88 slots. This list can then be verified in Step 3 to ensure failover readiness.
89 Non-<span class="productname">PostgreSQL</span> subscribers, on the other hand, may
90 use their own methods to identify the replication slots used by their
91 respective subscriptions.
93 In some cases, such as during a planned failover, it is necessary to confirm
94 that all subscribers, whether <span class="productname">PostgreSQL</span> or
95 non-<span class="productname">PostgreSQL</span>, will be able to continue
96 replication after failover to a given standby server. In such cases, use the
97 following SQL, instead of performing the first two steps above, to identify
98 which replication slots on the primary need to be synced to the standby that
99 is intended for promotion. This query returns the relevant replication slots
100 associated with all the failover-enabled subscriptions.
102 </p><pre class="programlisting">
103 /* primary # */ SELECT array_agg(quote_literal(r.slot_name)) AS slots
104 FROM pg_replication_slots r
105 WHERE r.failover AND NOT r.temporary;
108 {'sub1','sub2','sub3', 'pg_16394_sync_16385_7394666715149055164'}
110 </pre></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="logical-replication-subscription.html" title="29.2. Subscription">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="logical-replication.html" title="Chapter 29. Logical Replication">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="logical-replication-row-filter.html" title="29.4. Row Filters">Next</a></td></tr><tr><td width="40%" align="left" valign="top">29.2. Subscription </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 29.4. Row Filters</td></tr></table></div></body></html>