@@ -38,6 +38,7 @@ struct CreateSubscriberOptions
38
38
char * socket_dir ; /* directory for Unix-domain socket, if any */
39
39
char * sub_port ; /* subscriber port number */
40
40
const char * sub_username ; /* subscriber username */
41
+ bool two_phase ; /* enable-two-phase option */
41
42
SimpleStringList database_names ; /* list of database names */
42
43
SimpleStringList pub_names ; /* list of publication names */
43
44
SimpleStringList sub_names ; /* list of subscription names */
@@ -58,6 +59,12 @@ struct LogicalRepInfo
58
59
bool made_publication ; /* publication was created */
59
60
};
60
61
62
+ struct LogicalRepInfos
63
+ {
64
+ struct LogicalRepInfo * dbinfo ;
65
+ bool two_phase ; /* enable-two-phase option */
66
+ };
67
+
61
68
static void cleanup_objects_atexit (void );
62
69
static void usage ();
63
70
static char * get_base_conninfo (const char * conninfo , char * * dbname );
@@ -117,7 +124,7 @@ static bool dry_run = false;
117
124
118
125
static bool success = false;
119
126
120
- static struct LogicalRepInfo * dbinfo ;
127
+ static struct LogicalRepInfos dbinfos ;
121
128
static int num_dbs = 0 ; /* number of specified databases */
122
129
static int num_pubs = 0 ; /* number of specified publications */
123
130
static int num_subs = 0 ; /* number of specified subscriptions */
@@ -172,17 +179,17 @@ cleanup_objects_atexit(void)
172
179
173
180
for (int i = 0 ; i < num_dbs ; i ++ )
174
181
{
175
- if (dbinfo [i ].made_publication || dbinfo [i ].made_replslot )
182
+ if (dbinfos . dbinfo [i ].made_publication || dbinfos . dbinfo [i ].made_replslot )
176
183
{
177
184
PGconn * conn ;
178
185
179
- conn = connect_database (dbinfo [i ].pubconninfo , false);
186
+ conn = connect_database (dbinfos . dbinfo [i ].pubconninfo , false);
180
187
if (conn != NULL )
181
188
{
182
- if (dbinfo [i ].made_publication )
183
- drop_publication (conn , & dbinfo [i ]);
184
- if (dbinfo [i ].made_replslot )
185
- drop_replication_slot (conn , & dbinfo [i ], dbinfo [i ].replslotname );
189
+ if (dbinfos . dbinfo [i ].made_publication )
190
+ drop_publication (conn , & dbinfos . dbinfo [i ]);
191
+ if (dbinfos . dbinfo [i ].made_replslot )
192
+ drop_replication_slot (conn , & dbinfos . dbinfo [i ], dbinfos . dbinfo [i ].replslotname );
186
193
disconnect_database (conn , false);
187
194
}
188
195
else
@@ -192,16 +199,18 @@ cleanup_objects_atexit(void)
192
199
* that some objects were left on primary and should be
193
200
* removed before trying again.
194
201
*/
195
- if (dbinfo [i ].made_publication )
202
+ if (dbinfos . dbinfo [i ].made_publication )
196
203
{
197
204
pg_log_warning ("publication \"%s\" created in database \"%s\" on primary was left behind" ,
198
- dbinfo [i ].pubname , dbinfo [i ].dbname );
205
+ dbinfos .dbinfo [i ].pubname ,
206
+ dbinfos .dbinfo [i ].dbname );
199
207
pg_log_warning_hint ("Drop this publication before trying again." );
200
208
}
201
- if (dbinfo [i ].made_replslot )
209
+ if (dbinfos . dbinfo [i ].made_replslot )
202
210
{
203
211
pg_log_warning ("replication slot \"%s\" created in database \"%s\" on primary was left behind" ,
204
- dbinfo [i ].replslotname , dbinfo [i ].dbname );
212
+ dbinfos .dbinfo [i ].replslotname ,
213
+ dbinfos .dbinfo [i ].dbname );
205
214
pg_log_warning_hint ("Drop this replication slot soon to avoid retention of WAL files." );
206
215
}
207
216
}
@@ -227,6 +236,7 @@ usage(void)
227
236
printf (_ (" -P, --publisher-server=CONNSTR publisher connection string\n" ));
228
237
printf (_ (" -s, --socketdir=DIR socket directory to use (default current dir.)\n" ));
229
238
printf (_ (" -t, --recovery-timeout=SECS seconds to wait for recovery to end\n" ));
239
+ printf (_ (" -T, --enable-two-phase enable two-phase commit for all subscriptions\n" ));
230
240
printf (_ (" -U, --subscriber-username=NAME user name for subscriber connection\n" ));
231
241
printf (_ (" -v, --verbose output verbose messages\n" ));
232
242
printf (_ (" --config-file=FILENAME use specified main server configuration\n"
@@ -479,9 +489,10 @@ store_pub_sub_info(const struct CreateSubscriberOptions *opt,
479
489
dbinfo [i ].pubname ? dbinfo [i ].pubname : "(auto)" ,
480
490
dbinfo [i ].replslotname ? dbinfo [i ].replslotname : "(auto)" ,
481
491
dbinfo [i ].pubconninfo );
482
- pg_log_debug ("subscriber(%d): subscription: %s ; connection string: %s" , i ,
492
+ pg_log_debug ("subscriber(%d): subscription: %s ; connection string: %s, two_phase: %s " , i ,
483
493
dbinfo [i ].subname ? dbinfo [i ].subname : "(auto)" ,
484
- dbinfo [i ].subconninfo );
494
+ dbinfo [i ].subconninfo ,
495
+ dbinfos .two_phase ? "true" : "false" );
485
496
486
497
if (num_pubs > 0 )
487
498
pubcell = pubcell -> next ;
@@ -938,11 +949,12 @@ check_publisher(const struct LogicalRepInfo *dbinfo)
938
949
failed = true;
939
950
}
940
951
941
- if (max_prepared_transactions != 0 )
952
+ if (max_prepared_transactions != 0 && dbinfos . two_phase )
942
953
{
943
954
pg_log_warning ("two_phase option will not be enabled for replication slots" );
944
955
pg_log_warning_detail ("Subscriptions will be created with the two_phase option disabled. "
945
956
"Prepared transactions will be replicated at COMMIT PREPARED." );
957
+ pg_log_warning_hint ("You can use --enable-two-phase switch to enable two_phase." );
946
958
}
947
959
948
960
/*
@@ -1345,8 +1357,9 @@ create_logical_replication_slot(PGconn *conn, struct LogicalRepInfo *dbinfo)
1345
1357
slot_name_esc = PQescapeLiteral (conn , slot_name , strlen (slot_name ));
1346
1358
1347
1359
appendPQExpBuffer (str ,
1348
- "SELECT lsn FROM pg_catalog.pg_create_logical_replication_slot(%s, 'pgoutput', false, false, false)" ,
1349
- slot_name_esc );
1360
+ "SELECT lsn FROM pg_catalog.pg_create_logical_replication_slot(%s, 'pgoutput', false, %s, false)" ,
1361
+ slot_name_esc ,
1362
+ dbinfos .two_phase ? "true" : "false" );
1350
1363
1351
1364
PQfreemem (slot_name_esc );
1352
1365
@@ -1722,8 +1735,9 @@ create_subscription(PGconn *conn, const struct LogicalRepInfo *dbinfo)
1722
1735
appendPQExpBuffer (str ,
1723
1736
"CREATE SUBSCRIPTION %s CONNECTION %s PUBLICATION %s "
1724
1737
"WITH (create_slot = false, enabled = false, "
1725
- "slot_name = %s, copy_data = false)" ,
1726
- subname_esc , pubconninfo_esc , pubname_esc , replslotname_esc );
1738
+ "slot_name = %s, copy_data = false, two_phase = %s)" ,
1739
+ subname_esc , pubconninfo_esc , pubname_esc , replslotname_esc ,
1740
+ dbinfos .two_phase ? "true" : "false" );
1727
1741
1728
1742
PQfreemem (pubname_esc );
1729
1743
PQfreemem (subname_esc );
@@ -1895,6 +1909,7 @@ main(int argc, char **argv)
1895
1909
{"publisher-server" , required_argument , NULL , 'P' },
1896
1910
{"socketdir" , required_argument , NULL , 's' },
1897
1911
{"recovery-timeout" , required_argument , NULL , 't' },
1912
+ {"enable-two-phase" , no_argument , NULL , 'T' },
1898
1913
{"subscriber-username" , required_argument , NULL , 'U' },
1899
1914
{"verbose" , no_argument , NULL , 'v' },
1900
1915
{"version" , no_argument , NULL , 'V' },
@@ -1950,6 +1965,7 @@ main(int argc, char **argv)
1950
1965
opt .socket_dir = NULL ;
1951
1966
opt .sub_port = DEFAULT_SUB_PORT ;
1952
1967
opt .sub_username = NULL ;
1968
+ opt .two_phase = false;
1953
1969
opt .database_names = (SimpleStringList )
1954
1970
{
1955
1971
0
@@ -1972,7 +1988,7 @@ main(int argc, char **argv)
1972
1988
1973
1989
get_restricted_token ();
1974
1990
1975
- while ((c = getopt_long (argc , argv , "d:D:np:P:s:t:U :v" ,
1991
+ while ((c = getopt_long (argc , argv , "d:D:np:P:s:t:TU :v" ,
1976
1992
long_options , & option_index )) != -1 )
1977
1993
{
1978
1994
switch (c )
@@ -2009,6 +2025,9 @@ main(int argc, char **argv)
2009
2025
case 't' :
2010
2026
opt .recovery_timeout = atoi (optarg );
2011
2027
break ;
2028
+ case 'T' :
2029
+ opt .two_phase = true;
2030
+ break ;
2012
2031
case 'U' :
2013
2032
opt .sub_username = pg_strdup (optarg );
2014
2033
break ;
@@ -2170,12 +2189,14 @@ main(int argc, char **argv)
2170
2189
/* Rudimentary check for a data directory */
2171
2190
check_data_directory (subscriber_dir );
2172
2191
2192
+ dbinfos .two_phase = opt .two_phase ;
2193
+
2173
2194
/*
2174
2195
* Store database information for publisher and subscriber. It should be
2175
2196
* called before atexit() because its return is used in the
2176
2197
* cleanup_objects_atexit().
2177
2198
*/
2178
- dbinfo = store_pub_sub_info (& opt , pub_base_conninfo , sub_base_conninfo );
2199
+ dbinfos . dbinfo = store_pub_sub_info (& opt , pub_base_conninfo , sub_base_conninfo );
2179
2200
2180
2201
/* Register a function to clean up objects in case of failure */
2181
2202
atexit (cleanup_objects_atexit );
@@ -2184,7 +2205,7 @@ main(int argc, char **argv)
2184
2205
* Check if the subscriber data directory has the same system identifier
2185
2206
* than the publisher data directory.
2186
2207
*/
2187
- pub_sysid = get_primary_sysid (dbinfo [0 ].pubconninfo );
2208
+ pub_sysid = get_primary_sysid (dbinfos . dbinfo [0 ].pubconninfo );
2188
2209
sub_sysid = get_standby_sysid (subscriber_dir );
2189
2210
if (pub_sysid != sub_sysid )
2190
2211
pg_fatal ("subscriber data directory is not a copy of the source database cluster" );
@@ -2214,10 +2235,10 @@ main(int argc, char **argv)
2214
2235
start_standby_server (& opt , true, false);
2215
2236
2216
2237
/* Check if the standby server is ready for logical replication */
2217
- check_subscriber (dbinfo );
2238
+ check_subscriber (dbinfos . dbinfo );
2218
2239
2219
2240
/* Check if the primary server is ready for logical replication */
2220
- check_publisher (dbinfo );
2241
+ check_publisher (dbinfos . dbinfo );
2221
2242
2222
2243
/*
2223
2244
* Stop the target server. The recovery process requires that the server
@@ -2230,10 +2251,10 @@ main(int argc, char **argv)
2230
2251
stop_standby_server (subscriber_dir );
2231
2252
2232
2253
/* Create the required objects for each database on publisher */
2233
- consistent_lsn = setup_publisher (dbinfo );
2254
+ consistent_lsn = setup_publisher (dbinfos . dbinfo );
2234
2255
2235
2256
/* Write the required recovery parameters */
2236
- setup_recovery (dbinfo , subscriber_dir , consistent_lsn );
2257
+ setup_recovery (dbinfos . dbinfo , subscriber_dir , consistent_lsn );
2237
2258
2238
2259
/*
2239
2260
* Start subscriber so the recovery parameters will take effect. Wait
@@ -2244,21 +2265,21 @@ main(int argc, char **argv)
2244
2265
start_standby_server (& opt , true, true);
2245
2266
2246
2267
/* Waiting the subscriber to be promoted */
2247
- wait_for_end_recovery (dbinfo [0 ].subconninfo , & opt );
2268
+ wait_for_end_recovery (dbinfos . dbinfo [0 ].subconninfo , & opt );
2248
2269
2249
2270
/*
2250
2271
* Create the subscription for each database on subscriber. It does not
2251
2272
* enable it immediately because it needs to adjust the replication start
2252
2273
* point to the LSN reported by setup_publisher(). It also cleans up
2253
2274
* publications created by this tool and replication to the standby.
2254
2275
*/
2255
- setup_subscriber (dbinfo , consistent_lsn );
2276
+ setup_subscriber (dbinfos . dbinfo , consistent_lsn );
2256
2277
2257
2278
/* Remove primary_slot_name if it exists on primary */
2258
- drop_primary_replication_slot (dbinfo , primary_slot_name );
2279
+ drop_primary_replication_slot (dbinfos . dbinfo , primary_slot_name );
2259
2280
2260
2281
/* Remove failover replication slots if they exist on subscriber */
2261
- drop_failover_replication_slots (dbinfo );
2282
+ drop_failover_replication_slots (dbinfos . dbinfo );
2262
2283
2263
2284
/* Stop the subscriber */
2264
2285
pg_log_info ("stopping the subscriber" );
0 commit comments