| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* subscriptioncmds.c |
| 4 |
* subscription catalog manipulation functions |
| 5 |
* |
| 6 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 7 |
* Portions Copyright (c) 1994, Regents of the University of California |
| 8 |
* |
| 9 |
* IDENTIFICATION |
| 10 |
* src/backend/commands/subscriptioncmds.c |
| 11 |
* |
| 12 |
*------------------------------------------------------------------------- |
| 13 |
*/ |
| 14 |
|
| 15 |
#include "postgres.h" |
| 16 |
|
| 17 |
#include "access/htup_details.h" |
| 18 |
#include "access/table.h" |
| 19 |
#include "access/twophase.h" |
| 20 |
#include "access/xact.h" |
| 21 |
#include "catalog/catalog.h" |
| 22 |
#include "catalog/dependency.h" |
| 23 |
#include "catalog/indexing.h" |
| 24 |
#include "catalog/namespace.h" |
| 25 |
#include "catalog/objectaccess.h" |
| 26 |
#include "catalog/objectaddress.h" |
| 27 |
#include "catalog/pg_authid_d.h" |
| 28 |
#include "catalog/pg_database_d.h" |
| 29 |
#include "catalog/pg_subscription.h" |
| 30 |
#include "catalog/pg_subscription_rel.h" |
| 31 |
#include "catalog/pg_type.h" |
| 32 |
#include "commands/dbcommands.h" |
| 33 |
#include "commands/defrem.h" |
| 34 |
#include "commands/event_trigger.h" |
| 35 |
#include "commands/subscriptioncmds.h" |
| 36 |
#include "executor/executor.h" |
| 37 |
#include "miscadmin.h" |
| 38 |
#include "nodes/makefuncs.h" |
| 39 |
#include "pgstat.h" |
| 40 |
#include "replication/logicallauncher.h" |
| 41 |
#include "replication/logicalworker.h" |
| 42 |
#include "replication/origin.h" |
| 43 |
#include "replication/slot.h" |
| 44 |
#include "replication/walreceiver.h" |
| 45 |
#include "replication/walsender.h" |
| 46 |
#include "replication/worker_internal.h" |
| 47 |
#include "storage/lmgr.h" |
| 48 |
#include "utils/acl.h" |
| 49 |
#include "utils/builtins.h" |
| 50 |
#include "utils/guc.h" |
| 51 |
#include "utils/lsyscache.h" |
| 52 |
#include "utils/memutils.h" |
| 53 |
#include "utils/pg_lsn.h" |
| 54 |
#include "utils/syscache.h" |
| 55 |
|
| 56 |
/* |
| 57 |
* Options that can be specified by the user in CREATE/ALTER SUBSCRIPTION |
| 58 |
* command. |
| 59 |
*/ |
| 60 |
#define SUBOPT_CONNECT 0x00000001 |
| 61 |
#define SUBOPT_ENABLED 0x00000002 |
| 62 |
#define SUBOPT_CREATE_SLOT 0x00000004 |
| 63 |
#define SUBOPT_SLOT_NAME 0x00000008 |
| 64 |
#define SUBOPT_COPY_DATA 0x00000010 |
| 65 |
#define SUBOPT_SYNCHRONOUS_COMMIT 0x00000020 |
| 66 |
#define SUBOPT_REFRESH 0x00000040 |
| 67 |
#define SUBOPT_BINARY 0x00000080 |
| 68 |
#define SUBOPT_STREAMING 0x00000100 |
| 69 |
#define SUBOPT_TWOPHASE_COMMIT 0x00000200 |
| 70 |
#define SUBOPT_DISABLE_ON_ERR 0x00000400 |
| 71 |
#define SUBOPT_PASSWORD_REQUIRED 0x00000800 |
| 72 |
#define SUBOPT_RUN_AS_OWNER 0x00001000 |
| 73 |
#define SUBOPT_FAILOVER 0x00002000 |
| 74 |
#define SUBOPT_LSN 0x00004000 |
| 75 |
#define SUBOPT_ORIGIN 0x00008000 |
| 76 |
|
| 77 |
/* check if the 'val' has 'bits' set */ |
| 78 |
#define IsSet(val, bits) (((val) & (bits)) == (bits)) |
| 79 |
|
| 80 |
/* |
| 81 |
* Structure to hold a bitmap representing the user-provided CREATE/ALTER |
| 82 |
* SUBSCRIPTION command options and the parsed/default values of each of them. |
| 83 |
*/ |
| 84 |
typedef struct SubOpts |
| 85 |
{ |
| 86 |
bits32 specified_opts; |
| 87 |
char *slot_name; |
| 88 |
char *synchronous_commit; |
| 89 |
bool connect; |
| 90 |
bool enabled; |
| 91 |
bool create_slot; |
| 92 |
bool copy_data; |
| 93 |
bool refresh; |
| 94 |
bool binary; |
| 95 |
char streaming; |
| 96 |
bool twophase; |
| 97 |
bool disableonerr; |
| 98 |
bool passwordrequired; |
| 99 |
bool runasowner; |
| 100 |
bool failover; |
| 101 |
char *origin; |
| 102 |
XLogRecPtr lsn; |
| 103 |
} SubOpts; |
| 104 |
|
| 105 |
static List *fetch_table_list(WalReceiverConn *wrconn, List *publications); |
| 106 |
static void check_publications_origin(WalReceiverConn *wrconn, |
| 107 |
List *publications, bool copydata, |
| 108 |
char *origin, Oid *subrel_local_oids, |
| 109 |
int subrel_count, char *subname); |
| 110 |
static void check_duplicates_in_publist(List *publist, Datum *datums); |
| 111 |
static List *merge_publications(List *oldpublist, List *newpublist, bool addpub, const char *subname); |
| 112 |
static void ReportSlotConnectionError(List *rstates, Oid subid, char *slotname, char *err); |
| 113 |
static void CheckAlterSubOption(Subscription *sub, const char *option, |
| 114 |
bool slot_needs_update, bool isTopLevel); |
| 115 |
|
| 116 |
|
| 117 |
/* |
| 118 |
* Common option parsing function for CREATE and ALTER SUBSCRIPTION commands. |
| 119 |
* |
| 120 |
* Since not all options can be specified in both commands, this function |
| 121 |
* will report an error if mutually exclusive options are specified. |
| 122 |
*/ |
| 123 |
static void |
| 124 |
parse_subscription_options(ParseState *pstate, List *stmt_options, |
| 125 |
bits32 supported_opts, SubOpts *opts) |
| 126 |
{ |
| 127 |
ListCell *lc; |
| 128 |
|
| 129 |
/* Start out with cleared opts. */ |
| 130 |
memset(opts, 0, sizeof(SubOpts)); |
| 131 |
|
| 132 |
/* caller must expect some option */ |
| 133 |
Assert(supported_opts != 0); |
| 134 |
|
| 135 |
/* If connect option is supported, these others also need to be. */ |
| 136 |
Assert(!IsSet(supported_opts, SUBOPT_CONNECT) || |
| 137 |
IsSet(supported_opts, SUBOPT_ENABLED | SUBOPT_CREATE_SLOT | |
| 138 |
SUBOPT_COPY_DATA)); |
| 139 |
|
| 140 |
/* Set default values for the supported options. */ |
| 141 |
if (IsSet(supported_opts, SUBOPT_CONNECT)) |
| 142 |
opts->connect = true; |
| 143 |
if (IsSet(supported_opts, SUBOPT_ENABLED)) |
| 144 |
opts->enabled = true; |
| 145 |
if (IsSet(supported_opts, SUBOPT_CREATE_SLOT)) |
| 146 |
opts->create_slot = true; |
| 147 |
if (IsSet(supported_opts, SUBOPT_COPY_DATA)) |
| 148 |
opts->copy_data = true; |
| 149 |
if (IsSet(supported_opts, SUBOPT_REFRESH)) |
| 150 |
opts->refresh = true; |
| 151 |
if (IsSet(supported_opts, SUBOPT_BINARY)) |
| 152 |
opts->binary = false; |
| 153 |
if (IsSet(supported_opts, SUBOPT_STREAMING)) |
| 154 |
opts->streaming = LOGICALREP_STREAM_PARALLEL; |
| 155 |
if (IsSet(supported_opts, SUBOPT_TWOPHASE_COMMIT)) |
| 156 |
opts->twophase = false; |
| 157 |
if (IsSet(supported_opts, SUBOPT_DISABLE_ON_ERR)) |
| 158 |
opts->disableonerr = false; |
| 159 |
if (IsSet(supported_opts, SUBOPT_PASSWORD_REQUIRED)) |
| 160 |
opts->passwordrequired = true; |
| 161 |
if (IsSet(supported_opts, SUBOPT_RUN_AS_OWNER)) |
| 162 |
opts->runasowner = false; |
| 163 |
if (IsSet(supported_opts, SUBOPT_FAILOVER)) |
| 164 |
opts->failover = false; |
| 165 |
if (IsSet(supported_opts, SUBOPT_ORIGIN)) |
| 166 |
opts->origin = pstrdup(LOGICALREP_ORIGIN_ANY); |
| 167 |
|
| 168 |
/* Parse options */ |
| 169 |
foreach(lc, stmt_options) |
| 170 |
{ |
| 171 |
DefElem *defel = (DefElem *) lfirst(lc); |
| 172 |
|
| 173 |
if (IsSet(supported_opts, SUBOPT_CONNECT) && |
| 174 |
strcmp(defel->defname, "connect") == 0) |
| 175 |
{ |
| 176 |
if (IsSet(opts->specified_opts, SUBOPT_CONNECT)) |
| 177 |
errorConflictingDefElem(defel, pstate); |
| 178 |
|
| 179 |
opts->specified_opts |= SUBOPT_CONNECT; |
| 180 |
opts->connect = defGetBoolean(defel); |
| 181 |
} |
| 182 |
else if (IsSet(supported_opts, SUBOPT_ENABLED) && |
| 183 |
strcmp(defel->defname, "enabled") == 0) |
| 184 |
{ |
| 185 |
if (IsSet(opts->specified_opts, SUBOPT_ENABLED)) |
| 186 |
errorConflictingDefElem(defel, pstate); |
| 187 |
|
| 188 |
opts->specified_opts |= SUBOPT_ENABLED; |
| 189 |
opts->enabled = defGetBoolean(defel); |
| 190 |
} |
| 191 |
else if (IsSet(supported_opts, SUBOPT_CREATE_SLOT) && |
| 192 |
strcmp(defel->defname, "create_slot") == 0) |
| 193 |
{ |
| 194 |
if (IsSet(opts->specified_opts, SUBOPT_CREATE_SLOT)) |
| 195 |
errorConflictingDefElem(defel, pstate); |
| 196 |
|
| 197 |
opts->specified_opts |= SUBOPT_CREATE_SLOT; |
| 198 |
opts->create_slot = defGetBoolean(defel); |
| 199 |
} |
| 200 |
else if (IsSet(supported_opts, SUBOPT_SLOT_NAME) && |
| 201 |
strcmp(defel->defname, "slot_name") == 0) |
| 202 |
{ |
| 203 |
if (IsSet(opts->specified_opts, SUBOPT_SLOT_NAME)) |
| 204 |
errorConflictingDefElem(defel, pstate); |
| 205 |
|
| 206 |
opts->specified_opts |= SUBOPT_SLOT_NAME; |
| 207 |
opts->slot_name = defGetString(defel); |
| 208 |
|
| 209 |
/* Setting slot_name = NONE is treated as no slot name. */ |
| 210 |
if (strcmp(opts->slot_name, "none") == 0) |
| 211 |
opts->slot_name = NULL; |
| 212 |
else |
| 213 |
ReplicationSlotValidateName(opts->slot_name, ERROR); |
| 214 |
} |
| 215 |
else if (IsSet(supported_opts, SUBOPT_COPY_DATA) && |
| 216 |
strcmp(defel->defname, "copy_data") == 0) |
| 217 |
{ |
| 218 |
if (IsSet(opts->specified_opts, SUBOPT_COPY_DATA)) |
| 219 |
errorConflictingDefElem(defel, pstate); |
| 220 |
|
| 221 |
opts->specified_opts |= SUBOPT_COPY_DATA; |
| 222 |
opts->copy_data = defGetBoolean(defel); |
| 223 |
} |
| 224 |
else if (IsSet(supported_opts, SUBOPT_SYNCHRONOUS_COMMIT) && |
| 225 |
strcmp(defel->defname, "synchronous_commit") == 0) |
| 226 |
{ |
| 227 |
if (IsSet(opts->specified_opts, SUBOPT_SYNCHRONOUS_COMMIT)) |
| 228 |
errorConflictingDefElem(defel, pstate); |
| 229 |
|
| 230 |
opts->specified_opts |= SUBOPT_SYNCHRONOUS_COMMIT; |
| 231 |
opts->synchronous_commit = defGetString(defel); |
| 232 |
|
| 233 |
/* Test if the given value is valid for synchronous_commit GUC. */ |
| 234 |
(void) set_config_option("synchronous_commit", opts->synchronous_commit, |
| 235 |
PGC_BACKEND, PGC_S_TEST, GUC_ACTION_SET, |
| 236 |
false, 0, false); |
| 237 |
} |
| 238 |
else if (IsSet(supported_opts, SUBOPT_REFRESH) && |
| 239 |
strcmp(defel->defname, "refresh") == 0) |
| 240 |
{ |
| 241 |
if (IsSet(opts->specified_opts, SUBOPT_REFRESH)) |
| 242 |
errorConflictingDefElem(defel, pstate); |
| 243 |
|
| 244 |
opts->specified_opts |= SUBOPT_REFRESH; |
| 245 |
opts->refresh = defGetBoolean(defel); |
| 246 |
} |
| 247 |
else if (IsSet(supported_opts, SUBOPT_BINARY) && |
| 248 |
strcmp(defel->defname, "binary") == 0) |
| 249 |
{ |
| 250 |
if (IsSet(opts->specified_opts, SUBOPT_BINARY)) |
| 251 |
errorConflictingDefElem(defel, pstate); |
| 252 |
|
| 253 |
opts->specified_opts |= SUBOPT_BINARY; |
| 254 |
opts->binary = defGetBoolean(defel); |
| 255 |
} |
| 256 |
else if (IsSet(supported_opts, SUBOPT_STREAMING) && |
| 257 |
strcmp(defel->defname, "streaming") == 0) |
| 258 |
{ |
| 259 |
if (IsSet(opts->specified_opts, SUBOPT_STREAMING)) |
| 260 |
errorConflictingDefElem(defel, pstate); |
| 261 |
|
| 262 |
opts->specified_opts |= SUBOPT_STREAMING; |
| 263 |
opts->streaming = defGetStreamingMode(defel); |
| 264 |
} |
| 265 |
else if (IsSet(supported_opts, SUBOPT_TWOPHASE_COMMIT) && |
| 266 |
strcmp(defel->defname, "two_phase") == 0) |
| 267 |
{ |
| 268 |
if (IsSet(opts->specified_opts, SUBOPT_TWOPHASE_COMMIT)) |
| 269 |
errorConflictingDefElem(defel, pstate); |
| 270 |
|
| 271 |
opts->specified_opts |= SUBOPT_TWOPHASE_COMMIT; |
| 272 |
opts->twophase = defGetBoolean(defel); |
| 273 |
} |
| 274 |
else if (IsSet(supported_opts, SUBOPT_DISABLE_ON_ERR) && |
| 275 |
strcmp(defel->defname, "disable_on_error") == 0) |
| 276 |
{ |
| 277 |
if (IsSet(opts->specified_opts, SUBOPT_DISABLE_ON_ERR)) |
| 278 |
errorConflictingDefElem(defel, pstate); |
| 279 |
|
| 280 |
opts->specified_opts |= SUBOPT_DISABLE_ON_ERR; |
| 281 |
opts->disableonerr = defGetBoolean(defel); |
| 282 |
} |
| 283 |
else if (IsSet(supported_opts, SUBOPT_PASSWORD_REQUIRED) && |
| 284 |
strcmp(defel->defname, "password_required") == 0) |
| 285 |
{ |
| 286 |
if (IsSet(opts->specified_opts, SUBOPT_PASSWORD_REQUIRED)) |
| 287 |
errorConflictingDefElem(defel, pstate); |
| 288 |
|
| 289 |
opts->specified_opts |= SUBOPT_PASSWORD_REQUIRED; |
| 290 |
opts->passwordrequired = defGetBoolean(defel); |
| 291 |
} |
| 292 |
else if (IsSet(supported_opts, SUBOPT_RUN_AS_OWNER) && |
| 293 |
strcmp(defel->defname, "run_as_owner") == 0) |
| 294 |
{ |
| 295 |
if (IsSet(opts->specified_opts, SUBOPT_RUN_AS_OWNER)) |
| 296 |
errorConflictingDefElem(defel, pstate); |
| 297 |
|
| 298 |
opts->specified_opts |= SUBOPT_RUN_AS_OWNER; |
| 299 |
opts->runasowner = defGetBoolean(defel); |
| 300 |
} |
| 301 |
else if (IsSet(supported_opts, SUBOPT_FAILOVER) && |
| 302 |
strcmp(defel->defname, "failover") == 0) |
| 303 |
{ |
| 304 |
if (IsSet(opts->specified_opts, SUBOPT_FAILOVER)) |
| 305 |
errorConflictingDefElem(defel, pstate); |
| 306 |
|
| 307 |
opts->specified_opts |= SUBOPT_FAILOVER; |
| 308 |
opts->failover = defGetBoolean(defel); |
| 309 |
} |
| 310 |
else if (IsSet(supported_opts, SUBOPT_ORIGIN) && |
| 311 |
strcmp(defel->defname, "origin") == 0) |
| 312 |
{ |
| 313 |
if (IsSet(opts->specified_opts, SUBOPT_ORIGIN)) |
| 314 |
errorConflictingDefElem(defel, pstate); |
| 315 |
|
| 316 |
opts->specified_opts |= SUBOPT_ORIGIN; |
| 317 |
pfree(opts->origin); |
| 318 |
|
| 319 |
/* |
| 320 |
* Even though the "origin" parameter allows only "none" and "any" |
| 321 |
* values, it is implemented as a string type so that the |
| 322 |
* parameter can be extended in future versions to support |
| 323 |
* filtering using origin names specified by the user. |
| 324 |
*/ |
| 325 |
opts->origin = defGetString(defel); |
| 326 |
|
| 327 |
if ((pg_strcasecmp(opts->origin, LOGICALREP_ORIGIN_NONE) != 0) && |
| 328 |
(pg_strcasecmp(opts->origin, LOGICALREP_ORIGIN_ANY) != 0)) |
| 329 |
ereport(ERROR, |
| 330 |
errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 331 |
errmsg("unrecognized origin value: \"%s\"", opts->origin)); |
| 332 |
} |
| 333 |
else if (IsSet(supported_opts, SUBOPT_LSN) && |
| 334 |
strcmp(defel->defname, "lsn") == 0) |
| 335 |
{ |
| 336 |
char *lsn_str = defGetString(defel); |
| 337 |
XLogRecPtr lsn; |
| 338 |
|
| 339 |
if (IsSet(opts->specified_opts, SUBOPT_LSN)) |
| 340 |
errorConflictingDefElem(defel, pstate); |
| 341 |
|
| 342 |
/* Setting lsn = NONE is treated as resetting LSN */ |
| 343 |
if (strcmp(lsn_str, "none") == 0) |
| 344 |
lsn = InvalidXLogRecPtr; |
| 345 |
else |
| 346 |
{ |
| 347 |
/* Parse the argument as LSN */ |
| 348 |
lsn = DatumGetLSN(DirectFunctionCall1(pg_lsn_in, |
| 349 |
CStringGetDatum(lsn_str))); |
| 350 |
|
| 351 |
if (XLogRecPtrIsInvalid(lsn)) |
| 352 |
ereport(ERROR, |
| 353 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 354 |
errmsg("invalid WAL location (LSN): %s", lsn_str))); |
| 355 |
} |
| 356 |
|
| 357 |
opts->specified_opts |= SUBOPT_LSN; |
| 358 |
opts->lsn = lsn; |
| 359 |
} |
| 360 |
else |
| 361 |
ereport(ERROR, |
| 362 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 363 |
errmsg("unrecognized subscription parameter: \"%s\"", defel->defname))); |
| 364 |
} |
| 365 |
|
| 366 |
/* |
| 367 |
* We've been explicitly asked to not connect, that requires some |
| 368 |
* additional processing. |
| 369 |
*/ |
| 370 |
if (!opts->connect && IsSet(supported_opts, SUBOPT_CONNECT)) |
| 371 |
{ |
| 372 |
/* Check for incompatible options from the user. */ |
| 373 |
if (opts->enabled && |
| 374 |
IsSet(opts->specified_opts, SUBOPT_ENABLED)) |
| 375 |
ereport(ERROR, |
| 376 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 377 |
/*- translator: both %s are strings of the form "option = value" */ |
| 378 |
errmsg("%s and %s are mutually exclusive options", |
| 379 |
"connect = false", "enabled = true"))); |
| 380 |
|
| 381 |
if (opts->create_slot && |
| 382 |
IsSet(opts->specified_opts, SUBOPT_CREATE_SLOT)) |
| 383 |
ereport(ERROR, |
| 384 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 385 |
errmsg("%s and %s are mutually exclusive options", |
| 386 |
"connect = false", "create_slot = true"))); |
| 387 |
|
| 388 |
if (opts->copy_data && |
| 389 |
IsSet(opts->specified_opts, SUBOPT_COPY_DATA)) |
| 390 |
ereport(ERROR, |
| 391 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 392 |
errmsg("%s and %s are mutually exclusive options", |
| 393 |
"connect = false", "copy_data = true"))); |
| 394 |
|
| 395 |
/* Change the defaults of other options. */ |
| 396 |
opts->enabled = false; |
| 397 |
opts->create_slot = false; |
| 398 |
opts->copy_data = false; |
| 399 |
} |
| 400 |
|
| 401 |
/* |
| 402 |
* Do additional checking for disallowed combination when slot_name = NONE |
| 403 |
* was used. |
| 404 |
*/ |
| 405 |
if (!opts->slot_name && |
| 406 |
IsSet(opts->specified_opts, SUBOPT_SLOT_NAME)) |
| 407 |
{ |
| 408 |
if (opts->enabled) |
| 409 |
{ |
| 410 |
if (IsSet(opts->specified_opts, SUBOPT_ENABLED)) |
| 411 |
ereport(ERROR, |
| 412 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 413 |
/*- translator: both %s are strings of the form "option = value" */ |
| 414 |
errmsg("%s and %s are mutually exclusive options", |
| 415 |
"slot_name = NONE", "enabled = true"))); |
| 416 |
else |
| 417 |
ereport(ERROR, |
| 418 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 419 |
/*- translator: both %s are strings of the form "option = value" */ |
| 420 |
errmsg("subscription with %s must also set %s", |
| 421 |
"slot_name = NONE", "enabled = false"))); |
| 422 |
} |
| 423 |
|
| 424 |
if (opts->create_slot) |
| 425 |
{ |
| 426 |
if (IsSet(opts->specified_opts, SUBOPT_CREATE_SLOT)) |
| 427 |
ereport(ERROR, |
| 428 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 429 |
/*- translator: both %s are strings of the form "option = value" */ |
| 430 |
errmsg("%s and %s are mutually exclusive options", |
| 431 |
"slot_name = NONE", "create_slot = true"))); |
| 432 |
else |
| 433 |
ereport(ERROR, |
| 434 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 435 |
/*- translator: both %s are strings of the form "option = value" */ |
| 436 |
errmsg("subscription with %s must also set %s", |
| 437 |
"slot_name = NONE", "create_slot = false"))); |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
/* |
| 443 |
* Append a suitably-quoted identifier or string literal to buf. |
| 444 |
* "quote" should be either a double-quote or single-quote character. |
| 445 |
* |
| 446 |
* Caution: this quoting logic is sufficient for identifiers and literals |
| 447 |
* in the replication grammar, but not always in regular SQL. Specifically, |
| 448 |
* it'd fail for a string literal if standard_conforming_strings is off. |
| 449 |
*/ |
| 450 |
static void |
| 451 |
appendQuotedString(StringInfo buf, const char *str, char quote) |
| 452 |
{ |
| 453 |
appendStringInfoChar(buf, quote); |
| 454 |
while (*str) |
| 455 |
{ |
| 456 |
char c = *str++; |
| 457 |
|
| 458 |
if (c == quote) |
| 459 |
appendStringInfoChar(buf, c); |
| 460 |
appendStringInfoChar(buf, c); |
| 461 |
} |
| 462 |
appendStringInfoChar(buf, quote); |
| 463 |
} |
| 464 |
|
| 465 |
#define appendQuotedIdentifier(b, s) appendQuotedString(b, s, '"') |
| 466 |
#define appendQuotedLiteral(b, s) appendQuotedString(b, s, '\'') |
| 467 |
|
| 468 |
/* |
| 469 |
* Check that the specified publications are present on the publisher. |
| 470 |
*/ |
| 471 |
static void |
| 472 |
check_publications(WalReceiverConn *wrconn, List *publications) |
| 473 |
{ |
| 474 |
WalRcvExecResult *res; |
| 475 |
StringInfo cmd; |
| 476 |
TupleTableSlot *slot; |
| 477 |
List *publicationsCopy = NIL; |
| 478 |
Oid tableRow[1] = {TEXTOID}; |
| 479 |
|
| 480 |
cmd = makeStringInfo(); |
| 481 |
appendStringInfoString(cmd, "SELECT t.pubname FROM\n" |
| 482 |
" pg_catalog.pg_publication t WHERE\n" |
| 483 |
" t.pubname IN ("); |
| 484 |
GetPublicationsStr(publications, cmd, true); |
| 485 |
appendStringInfoChar(cmd, ')'); |
| 486 |
|
| 487 |
res = walrcv_exec(wrconn, cmd->data, 1, tableRow); |
| 488 |
destroyStringInfo(cmd); |
| 489 |
|
| 490 |
if (res->status != WALRCV_OK_TUPLES) |
| 491 |
ereport(ERROR, |
| 492 |
errmsg("could not receive list of publications from the publisher: %s", |
| 493 |
res->err)); |
| 494 |
|
| 495 |
publicationsCopy = list_copy(publications); |
| 496 |
|
| 497 |
/* Process publication(s). */ |
| 498 |
slot = MakeSingleTupleTableSlot(res->tupledesc, &TTSOpsMinimalTuple); |
| 499 |
while (tuplestore_gettupleslot(res->tuplestore, true, false, slot)) |
| 500 |
{ |
| 501 |
char *pubname; |
| 502 |
bool isnull; |
| 503 |
|
| 504 |
pubname = TextDatumGetCString(slot_getattr(slot, 1, &isnull)); |
| 505 |
Assert(!isnull); |
| 506 |
|
| 507 |
/* Delete the publication present in publisher from the list. */ |
| 508 |
publicationsCopy = list_delete(publicationsCopy, makeString(pubname)); |
| 509 |
ExecClearTuple(slot); |
| 510 |
} |
| 511 |
|
| 512 |
ExecDropSingleTupleTableSlot(slot); |
| 513 |
|
| 514 |
walrcv_clear_result(res); |
| 515 |
|
| 516 |
if (list_length(publicationsCopy)) |
| 517 |
{ |
| 518 |
/* Prepare the list of non-existent publication(s) for error message. */ |
| 519 |
StringInfo pubnames = makeStringInfo(); |
| 520 |
|
| 521 |
GetPublicationsStr(publicationsCopy, pubnames, false); |
| 522 |
ereport(WARNING, |
| 523 |
errcode(ERRCODE_UNDEFINED_OBJECT), |
| 524 |
errmsg_plural("publication %s does not exist on the publisher", |
| 525 |
"publications %s do not exist on the publisher", |
| 526 |
list_length(publicationsCopy), |
| 527 |
pubnames->data)); |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
/* |
| 532 |
* Auxiliary function to build a text array out of a list of String nodes. |
| 533 |
*/ |
| 534 |
static Datum |
| 535 |
publicationListToArray(List *publist) |
| 536 |
{ |
| 537 |
ArrayType *arr; |
| 538 |
Datum *datums; |
| 539 |
MemoryContext memcxt; |
| 540 |
MemoryContext oldcxt; |
| 541 |
|
| 542 |
/* Create memory context for temporary allocations. */ |
| 543 |
memcxt = AllocSetContextCreate(CurrentMemoryContext, |
| 544 |
"publicationListToArray to array", |
| 545 |
ALLOCSET_DEFAULT_SIZES); |
| 546 |
oldcxt = MemoryContextSwitchTo(memcxt); |
| 547 |
|
| 548 |
datums = (Datum *) palloc(sizeof(Datum) * list_length(publist)); |
| 549 |
|
| 550 |
check_duplicates_in_publist(publist, datums); |
| 551 |
|
| 552 |
MemoryContextSwitchTo(oldcxt); |
| 553 |
|
| 554 |
arr = construct_array_builtin(datums, list_length(publist), TEXTOID); |
| 555 |
|
| 556 |
MemoryContextDelete(memcxt); |
| 557 |
|
| 558 |
return PointerGetDatum(arr); |
| 559 |
} |
| 560 |
|
| 561 |
/* |
| 562 |
* Create new subscription. |
| 563 |
*/ |
| 564 |
ObjectAddress |
| 565 |
CreateSubscription(ParseState *pstate, CreateSubscriptionStmt *stmt, |
| 566 |
bool isTopLevel) |
| 567 |
{ |
| 568 |
Relation rel; |
| 569 |
ObjectAddress myself; |
| 570 |
Oid subid; |
| 571 |
bool nulls[Natts_pg_subscription]; |
| 572 |
Datum values[Natts_pg_subscription]; |
| 573 |
Oid owner = GetUserId(); |
| 574 |
HeapTuple tup; |
| 575 |
char *conninfo; |
| 576 |
char originname[NAMEDATALEN]; |
| 577 |
List *publications; |
| 578 |
bits32 supported_opts; |
| 579 |
SubOpts opts = {0}; |
| 580 |
AclResult aclresult; |
| 581 |
|
| 582 |
/* |
| 583 |
* Parse and check options. |
| 584 |
* |
| 585 |
* Connection and publication should not be specified here. |
| 586 |
*/ |
| 587 |
supported_opts = (SUBOPT_CONNECT | SUBOPT_ENABLED | SUBOPT_CREATE_SLOT | |
| 588 |
SUBOPT_SLOT_NAME | SUBOPT_COPY_DATA | |
| 589 |
SUBOPT_SYNCHRONOUS_COMMIT | SUBOPT_BINARY | |
| 590 |
SUBOPT_STREAMING | SUBOPT_TWOPHASE_COMMIT | |
| 591 |
SUBOPT_DISABLE_ON_ERR | SUBOPT_PASSWORD_REQUIRED | |
| 592 |
SUBOPT_RUN_AS_OWNER | SUBOPT_FAILOVER | SUBOPT_ORIGIN); |
| 593 |
parse_subscription_options(pstate, stmt->options, supported_opts, &opts); |
| 594 |
|
| 595 |
/* |
| 596 |
* Since creating a replication slot is not transactional, rolling back |
| 597 |
* the transaction leaves the created replication slot. So we cannot run |
| 598 |
* CREATE SUBSCRIPTION inside a transaction block if creating a |
| 599 |
* replication slot. |
| 600 |
*/ |
| 601 |
if (opts.create_slot) |
| 602 |
PreventInTransactionBlock(isTopLevel, "CREATE SUBSCRIPTION ... WITH (create_slot = true)"); |
| 603 |
|
| 604 |
/* |
| 605 |
* We don't want to allow unprivileged users to be able to trigger |
| 606 |
* attempts to access arbitrary network destinations, so require the user |
| 607 |
* to have been specifically authorized to create subscriptions. |
| 608 |
*/ |
| 609 |
if (!has_privs_of_role(owner, ROLE_PG_CREATE_SUBSCRIPTION)) |
| 610 |
ereport(ERROR, |
| 611 |
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 612 |
errmsg("permission denied to create subscription"), |
| 613 |
errdetail("Only roles with privileges of the \"%s\" role may create subscriptions.", |
| 614 |
"pg_create_subscription"))); |
| 615 |
|
| 616 |
/* |
| 617 |
* Since a subscription is a database object, we also check for CREATE |
| 618 |
* permission on the database. |
| 619 |
*/ |
| 620 |
aclresult = object_aclcheck(DatabaseRelationId, MyDatabaseId, |
| 621 |
owner, ACL_CREATE); |
| 622 |
if (aclresult != ACLCHECK_OK) |
| 623 |
aclcheck_error(aclresult, OBJECT_DATABASE, |
| 624 |
get_database_name(MyDatabaseId)); |
| 625 |
|
| 626 |
/* |
| 627 |
* Non-superusers are required to set a password for authentication, and |
| 628 |
* that password must be used by the target server, but the superuser can |
| 629 |
* exempt a subscription from this requirement. |
| 630 |
*/ |
| 631 |
if (!opts.passwordrequired && !superuser_arg(owner)) |
| 632 |
ereport(ERROR, |
| 633 |
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 634 |
errmsg("password_required=false is superuser-only"), |
| 635 |
errhint("Subscriptions with the password_required option set to false may only be created or modified by the superuser."))); |
| 636 |
|
| 637 |
/* |
| 638 |
* If built with appropriate switch, whine when regression-testing |
| 639 |
* conventions for subscription names are violated. |
| 640 |
*/ |
| 641 |
#ifdef ENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS |
| 642 |
if (strncmp(stmt->subname, "regress_", 8) != 0) |
| 643 |
elog(WARNING, "subscriptions created by regression test cases should have names starting with \"regress_\""); |
| 644 |
#endif |
| 645 |
|
| 646 |
rel = table_open(SubscriptionRelationId, RowExclusiveLock); |
| 647 |
|
| 648 |
/* Check if name is used */ |
| 649 |
subid = GetSysCacheOid2(SUBSCRIPTIONNAME, Anum_pg_subscription_oid, |
| 650 |
MyDatabaseId, CStringGetDatum(stmt->subname)); |
| 651 |
if (OidIsValid(subid)) |
| 652 |
{ |
| 653 |
ereport(ERROR, |
| 654 |
(errcode(ERRCODE_DUPLICATE_OBJECT), |
| 655 |
errmsg("subscription \"%s\" already exists", |
| 656 |
stmt->subname))); |
| 657 |
} |
| 658 |
|
| 659 |
if (!IsSet(opts.specified_opts, SUBOPT_SLOT_NAME) && |
| 660 |
opts.slot_name == NULL) |
| 661 |
opts.slot_name = stmt->subname; |
| 662 |
|
| 663 |
/* The default for synchronous_commit of subscriptions is off. */ |
| 664 |
if (opts.synchronous_commit == NULL) |
| 665 |
opts.synchronous_commit = "off"; |
| 666 |
|
| 667 |
conninfo = stmt->conninfo; |
| 668 |
publications = stmt->publication; |
| 669 |
|
| 670 |
/* Load the library providing us libpq calls. */ |
| 671 |
load_file("libpqwalreceiver", false); |
| 672 |
|
| 673 |
/* Check the connection info string. */ |
| 674 |
walrcv_check_conninfo(conninfo, opts.passwordrequired && !superuser()); |
| 675 |
|
| 676 |
/* Everything ok, form a new tuple. */ |
| 677 |
memset(values, 0, sizeof(values)); |
| 678 |
memset(nulls, false, sizeof(nulls)); |
| 679 |
|
| 680 |
subid = GetNewOidWithIndex(rel, SubscriptionObjectIndexId, |
| 681 |
Anum_pg_subscription_oid); |
| 682 |
values[Anum_pg_subscription_oid - 1] = ObjectIdGetDatum(subid); |
| 683 |
values[Anum_pg_subscription_subdbid - 1] = ObjectIdGetDatum(MyDatabaseId); |
| 684 |
values[Anum_pg_subscription_subskiplsn - 1] = LSNGetDatum(InvalidXLogRecPtr); |
| 685 |
values[Anum_pg_subscription_subname - 1] = |
| 686 |
DirectFunctionCall1(namein, CStringGetDatum(stmt->subname)); |
| 687 |
values[Anum_pg_subscription_subowner - 1] = ObjectIdGetDatum(owner); |
| 688 |
values[Anum_pg_subscription_subenabled - 1] = BoolGetDatum(opts.enabled); |
| 689 |
values[Anum_pg_subscription_subbinary - 1] = BoolGetDatum(opts.binary); |
| 690 |
values[Anum_pg_subscription_substream - 1] = CharGetDatum(opts.streaming); |
| 691 |
values[Anum_pg_subscription_subtwophasestate - 1] = |
| 692 |
CharGetDatum(opts.twophase ? |
| 693 |
LOGICALREP_TWOPHASE_STATE_PENDING : |
| 694 |
LOGICALREP_TWOPHASE_STATE_DISABLED); |
| 695 |
values[Anum_pg_subscription_subdisableonerr - 1] = BoolGetDatum(opts.disableonerr); |
| 696 |
values[Anum_pg_subscription_subpasswordrequired - 1] = BoolGetDatum(opts.passwordrequired); |
| 697 |
values[Anum_pg_subscription_subrunasowner - 1] = BoolGetDatum(opts.runasowner); |
| 698 |
values[Anum_pg_subscription_subfailover - 1] = BoolGetDatum(opts.failover); |
| 699 |
values[Anum_pg_subscription_subconninfo - 1] = |
| 700 |
CStringGetTextDatum(conninfo); |
| 701 |
if (opts.slot_name) |
| 702 |
values[Anum_pg_subscription_subslotname - 1] = |
| 703 |
DirectFunctionCall1(namein, CStringGetDatum(opts.slot_name)); |
| 704 |
else |
| 705 |
nulls[Anum_pg_subscription_subslotname - 1] = true; |
| 706 |
values[Anum_pg_subscription_subsynccommit - 1] = |
| 707 |
CStringGetTextDatum(opts.synchronous_commit); |
| 708 |
values[Anum_pg_subscription_subpublications - 1] = |
| 709 |
publicationListToArray(publications); |
| 710 |
values[Anum_pg_subscription_suborigin - 1] = |
| 711 |
CStringGetTextDatum(opts.origin); |
| 712 |
|
| 713 |
tup = heap_form_tuple(RelationGetDescr(rel), values, nulls); |
| 714 |
|
| 715 |
/* Insert tuple into catalog. */ |
| 716 |
CatalogTupleInsert(rel, tup); |
| 717 |
heap_freetuple(tup); |
| 718 |
|
| 719 |
recordDependencyOnOwner(SubscriptionRelationId, subid, owner); |
| 720 |
|
| 721 |
ReplicationOriginNameForLogicalRep(subid, InvalidOid, originname, sizeof(originname)); |
| 722 |
replorigin_create(originname); |
| 723 |
|
| 724 |
/* |
| 725 |
* Connect to remote side to execute requested commands and fetch table |
| 726 |
* info. |
| 727 |
*/ |
| 728 |
if (opts.connect) |
| 729 |
{ |
| 730 |
char *err; |
| 731 |
WalReceiverConn *wrconn; |
| 732 |
List *tables; |
| 733 |
ListCell *lc; |
| 734 |
char table_state; |
| 735 |
bool must_use_password; |
| 736 |
|
| 737 |
/* Try to connect to the publisher. */ |
| 738 |
must_use_password = !superuser_arg(owner) && opts.passwordrequired; |
| 739 |
wrconn = walrcv_connect(conninfo, true, true, must_use_password, |
| 740 |
stmt->subname, &err); |
| 741 |
if (!wrconn) |
| 742 |
ereport(ERROR, |
| 743 |
(errcode(ERRCODE_CONNECTION_FAILURE), |
| 744 |
errmsg("subscription \"%s\" could not connect to the publisher: %s", |
| 745 |
stmt->subname, err))); |
| 746 |
|
| 747 |
PG_TRY(); |
| 748 |
{ |
| 749 |
check_publications(wrconn, publications); |
| 750 |
check_publications_origin(wrconn, publications, opts.copy_data, |
| 751 |
opts.origin, NULL, 0, stmt->subname); |
| 752 |
|
| 753 |
/* |
| 754 |
* Set sync state based on if we were asked to do data copy or |
| 755 |
* not. |
| 756 |
*/ |
| 757 |
table_state = opts.copy_data ? SUBREL_STATE_INIT : SUBREL_STATE_READY; |
| 758 |
|
| 759 |
/* |
| 760 |
* Get the table list from publisher and build local table status |
| 761 |
* info. |
| 762 |
*/ |
| 763 |
tables = fetch_table_list(wrconn, publications); |
| 764 |
foreach(lc, tables) |
| 765 |
{ |
| 766 |
RangeVar *rv = (RangeVar *) lfirst(lc); |
| 767 |
Oid relid; |
| 768 |
|
| 769 |
relid = RangeVarGetRelid(rv, AccessShareLock, false); |
| 770 |
|
| 771 |
/* Check for supported relkind. */ |
| 772 |
CheckSubscriptionRelkind(get_rel_relkind(relid), |
| 773 |
rv->schemaname, rv->relname); |
| 774 |
|
| 775 |
AddSubscriptionRelState(subid, relid, table_state, |
| 776 |
InvalidXLogRecPtr, true); |
| 777 |
} |
| 778 |
|
| 779 |
/* |
| 780 |
* If requested, create permanent slot for the subscription. We |
| 781 |
* won't use the initial snapshot for anything, so no need to |
| 782 |
* export it. |
| 783 |
*/ |
| 784 |
if (opts.create_slot) |
| 785 |
{ |
| 786 |
bool twophase_enabled = false; |
| 787 |
|
| 788 |
Assert(opts.slot_name); |
| 789 |
|
| 790 |
/* |
| 791 |
* Even if two_phase is set, don't create the slot with |
| 792 |
* two-phase enabled. Will enable it once all the tables are |
| 793 |
* synced and ready. This avoids race-conditions like prepared |
| 794 |
* transactions being skipped due to changes not being applied |
| 795 |
* due to checks in should_apply_changes_for_rel() when |
| 796 |
* tablesync for the corresponding tables are in progress. See |
| 797 |
* comments atop worker.c. |
| 798 |
* |
| 799 |
* Note that if tables were specified but copy_data is false |
| 800 |
* then it is safe to enable two_phase up-front because those |
| 801 |
* tables are already initially in READY state. When the |
| 802 |
* subscription has no tables, we leave the twophase state as |
| 803 |
* PENDING, to allow ALTER SUBSCRIPTION ... REFRESH |
| 804 |
* PUBLICATION to work. |
| 805 |
*/ |
| 806 |
if (opts.twophase && !opts.copy_data && tables != NIL) |
| 807 |
twophase_enabled = true; |
| 808 |
|
| 809 |
walrcv_create_slot(wrconn, opts.slot_name, false, twophase_enabled, |
| 810 |
opts.failover, CRS_NOEXPORT_SNAPSHOT, NULL); |
| 811 |
|
| 812 |
if (twophase_enabled) |
| 813 |
UpdateTwoPhaseState(subid, LOGICALREP_TWOPHASE_STATE_ENABLED); |
| 814 |
|
| 815 |
ereport(NOTICE, |
| 816 |
(errmsg("created replication slot \"%s\" on publisher", |
| 817 |
opts.slot_name))); |
| 818 |
} |
| 819 |
} |
| 820 |
PG_FINALLY(); |
| 821 |
{ |
| 822 |
walrcv_disconnect(wrconn); |
| 823 |
} |
| 824 |
PG_END_TRY(); |
| 825 |
} |
| 826 |
else |
| 827 |
ereport(WARNING, |
| 828 |
(errmsg("subscription was created, but is not connected"), |
| 829 |
errhint("To initiate replication, you must manually create the replication slot, enable the subscription, and refresh the subscription."))); |
| 830 |
|
| 831 |
table_close(rel, RowExclusiveLock); |
| 832 |
|
| 833 |
pgstat_create_subscription(subid); |
| 834 |
|
| 835 |
if (opts.enabled) |
| 836 |
ApplyLauncherWakeupAtCommit(); |
| 837 |
|
| 838 |
ObjectAddressSet(myself, SubscriptionRelationId, subid); |
| 839 |
|
| 840 |
InvokeObjectPostCreateHook(SubscriptionRelationId, subid, 0); |
| 841 |
|
| 842 |
return myself; |
| 843 |
} |
| 844 |
|
| 845 |
static void |
| 846 |
AlterSubscription_refresh(Subscription *sub, bool copy_data, |
| 847 |
List *validate_publications) |
| 848 |
{ |
| 849 |
char *err; |
| 850 |
List *pubrel_names; |
| 851 |
List *subrel_states; |
| 852 |
Oid *subrel_local_oids; |
| 853 |
Oid *pubrel_local_oids; |
| 854 |
ListCell *lc; |
| 855 |
int off; |
| 856 |
int remove_rel_len; |
| 857 |
int subrel_count; |
| 858 |
Relation rel = NULL; |
| 859 |
typedef struct SubRemoveRels |
| 860 |
{ |
| 861 |
Oid relid; |
| 862 |
char state; |
| 863 |
} SubRemoveRels; |
| 864 |
SubRemoveRels *sub_remove_rels; |
| 865 |
WalReceiverConn *wrconn; |
| 866 |
bool must_use_password; |
| 867 |
|
| 868 |
/* Load the library providing us libpq calls. */ |
| 869 |
load_file("libpqwalreceiver", false); |
| 870 |
|
| 871 |
/* Try to connect to the publisher. */ |
| 872 |
must_use_password = sub->passwordrequired && !sub->ownersuperuser; |
| 873 |
wrconn = walrcv_connect(sub->conninfo, true, true, must_use_password, |
| 874 |
sub->name, &err); |
| 875 |
if (!wrconn) |
| 876 |
ereport(ERROR, |
| 877 |
(errcode(ERRCODE_CONNECTION_FAILURE), |
| 878 |
errmsg("subscription \"%s\" could not connect to the publisher: %s", |
| 879 |
sub->name, err))); |
| 880 |
|
| 881 |
PG_TRY(); |
| 882 |
{ |
| 883 |
if (validate_publications) |
| 884 |
check_publications(wrconn, validate_publications); |
| 885 |
|
| 886 |
/* Get the table list from publisher. */ |
| 887 |
pubrel_names = fetch_table_list(wrconn, sub->publications); |
| 888 |
|
| 889 |
/* Get local table list. */ |
| 890 |
subrel_states = GetSubscriptionRelations(sub->oid, false); |
| 891 |
subrel_count = list_length(subrel_states); |
| 892 |
|
| 893 |
/* |
| 894 |
* Build qsorted array of local table oids for faster lookup. This can |
| 895 |
* potentially contain all tables in the database so speed of lookup |
| 896 |
* is important. |
| 897 |
*/ |
| 898 |
subrel_local_oids = palloc(subrel_count * sizeof(Oid)); |
| 899 |
off = 0; |
| 900 |
foreach(lc, subrel_states) |
| 901 |
{ |
| 902 |
SubscriptionRelState *relstate = (SubscriptionRelState *) lfirst(lc); |
| 903 |
|
| 904 |
subrel_local_oids[off++] = relstate->relid; |
| 905 |
} |
| 906 |
qsort(subrel_local_oids, subrel_count, |
| 907 |
sizeof(Oid), oid_cmp); |
| 908 |
|
| 909 |
check_publications_origin(wrconn, sub->publications, copy_data, |
| 910 |
sub->origin, subrel_local_oids, |
| 911 |
subrel_count, sub->name); |
| 912 |
|
| 913 |
/* |
| 914 |
* Rels that we want to remove from subscription and drop any slots |
| 915 |
* and origins corresponding to them. |
| 916 |
*/ |
| 917 |
sub_remove_rels = palloc(subrel_count * sizeof(SubRemoveRels)); |
| 918 |
|
| 919 |
/* |
| 920 |
* Walk over the remote tables and try to match them to locally known |
| 921 |
* tables. If the table is not known locally create a new state for |
| 922 |
* it. |
| 923 |
* |
| 924 |
* Also builds array of local oids of remote tables for the next step. |
| 925 |
*/ |
| 926 |
off = 0; |
| 927 |
pubrel_local_oids = palloc(list_length(pubrel_names) * sizeof(Oid)); |
| 928 |
|
| 929 |
foreach(lc, pubrel_names) |
| 930 |
{ |
| 931 |
RangeVar *rv = (RangeVar *) lfirst(lc); |
| 932 |
Oid relid; |
| 933 |
|
| 934 |
relid = RangeVarGetRelid(rv, AccessShareLock, false); |
| 935 |
|
| 936 |
/* Check for supported relkind. */ |
| 937 |
CheckSubscriptionRelkind(get_rel_relkind(relid), |
| 938 |
rv->schemaname, rv->relname); |
| 939 |
|
| 940 |
pubrel_local_oids[off++] = relid; |
| 941 |
|
| 942 |
if (!bsearch(&relid, subrel_local_oids, |
| 943 |
subrel_count, sizeof(Oid), oid_cmp)) |
| 944 |
{ |
| 945 |
AddSubscriptionRelState(sub->oid, relid, |
| 946 |
copy_data ? SUBREL_STATE_INIT : SUBREL_STATE_READY, |
| 947 |
InvalidXLogRecPtr, true); |
| 948 |
ereport(DEBUG1, |
| 949 |
(errmsg_internal("table \"%s.%s\" added to subscription \"%s\"", |
| 950 |
rv->schemaname, rv->relname, sub->name))); |
| 951 |
} |
| 952 |
} |
| 953 |
|
| 954 |
/* |
| 955 |
* Next remove state for tables we should not care about anymore using |
| 956 |
* the data we collected above |
| 957 |
*/ |
| 958 |
qsort(pubrel_local_oids, list_length(pubrel_names), |
| 959 |
sizeof(Oid), oid_cmp); |
| 960 |
|
| 961 |
remove_rel_len = 0; |
| 962 |
for (off = 0; off < subrel_count; off++) |
| 963 |
{ |
| 964 |
Oid relid = subrel_local_oids[off]; |
| 965 |
|
| 966 |
if (!bsearch(&relid, pubrel_local_oids, |
| 967 |
list_length(pubrel_names), sizeof(Oid), oid_cmp)) |
| 968 |
{ |
| 969 |
char state; |
| 970 |
XLogRecPtr statelsn; |
| 971 |
|
| 972 |
/* |
| 973 |
* Lock pg_subscription_rel with AccessExclusiveLock to |
| 974 |
* prevent any race conditions with the apply worker |
| 975 |
* re-launching workers at the same time this code is trying |
| 976 |
* to remove those tables. |
| 977 |
* |
| 978 |
* Even if new worker for this particular rel is restarted it |
| 979 |
* won't be able to make any progress as we hold exclusive |
| 980 |
* lock on pg_subscription_rel till the transaction end. It |
| 981 |
* will simply exit as there is no corresponding rel entry. |
| 982 |
* |
| 983 |
* This locking also ensures that the state of rels won't |
| 984 |
* change till we are done with this refresh operation. |
| 985 |
*/ |
| 986 |
if (!rel) |
| 987 |
rel = table_open(SubscriptionRelRelationId, AccessExclusiveLock); |
| 988 |
|
| 989 |
/* Last known rel state. */ |
| 990 |
state = GetSubscriptionRelState(sub->oid, relid, &statelsn); |
| 991 |
|
| 992 |
sub_remove_rels[remove_rel_len].relid = relid; |
| 993 |
sub_remove_rels[remove_rel_len++].state = state; |
| 994 |
|
| 995 |
RemoveSubscriptionRel(sub->oid, relid); |
| 996 |
|
| 997 |
logicalrep_worker_stop(sub->oid, relid); |
| 998 |
|
| 999 |
/* |
| 1000 |
* For READY state, we would have already dropped the |
| 1001 |
* tablesync origin. |
| 1002 |
*/ |
| 1003 |
if (state != SUBREL_STATE_READY) |
| 1004 |
{ |
| 1005 |
char originname[NAMEDATALEN]; |
| 1006 |
|
| 1007 |
/* |
| 1008 |
* Drop the tablesync's origin tracking if exists. |
| 1009 |
* |
| 1010 |
* It is possible that the origin is not yet created for |
| 1011 |
* tablesync worker, this can happen for the states before |
| 1012 |
* SUBREL_STATE_DATASYNC. The tablesync worker or apply |
| 1013 |
* worker can also concurrently try to drop the origin and |
| 1014 |
* by this time the origin might be already removed. For |
| 1015 |
* these reasons, passing missing_ok = true. |
| 1016 |
*/ |
| 1017 |
ReplicationOriginNameForLogicalRep(sub->oid, relid, originname, |
| 1018 |
sizeof(originname)); |
| 1019 |
replorigin_drop_by_name(originname, true, false); |
| 1020 |
} |
| 1021 |
|
| 1022 |
ereport(DEBUG1, |
| 1023 |
(errmsg_internal("table \"%s.%s\" removed from subscription \"%s\"", |
| 1024 |
get_namespace_name(get_rel_namespace(relid)), |
| 1025 |
get_rel_name(relid), |
| 1026 |
sub->name))); |
| 1027 |
} |
| 1028 |
} |
| 1029 |
|
| 1030 |
/* |
| 1031 |
* Drop the tablesync slots associated with removed tables. This has |
| 1032 |
* to be at the end because otherwise if there is an error while doing |
| 1033 |
* the database operations we won't be able to rollback dropped slots. |
| 1034 |
*/ |
| 1035 |
for (off = 0; off < remove_rel_len; off++) |
| 1036 |
{ |
| 1037 |
if (sub_remove_rels[off].state != SUBREL_STATE_READY && |
| 1038 |
sub_remove_rels[off].state != SUBREL_STATE_SYNCDONE) |
| 1039 |
{ |
| 1040 |
char syncslotname[NAMEDATALEN] = {0}; |
| 1041 |
|
| 1042 |
/* |
| 1043 |
* For READY/SYNCDONE states we know the tablesync slot has |
| 1044 |
* already been dropped by the tablesync worker. |
| 1045 |
* |
| 1046 |
* For other states, there is no certainty, maybe the slot |
| 1047 |
* does not exist yet. Also, if we fail after removing some of |
| 1048 |
* the slots, next time, it will again try to drop already |
| 1049 |
* dropped slots and fail. For these reasons, we allow |
| 1050 |
* missing_ok = true for the drop. |
| 1051 |
*/ |
| 1052 |
ReplicationSlotNameForTablesync(sub->oid, sub_remove_rels[off].relid, |
| 1053 |
syncslotname, sizeof(syncslotname)); |
| 1054 |
ReplicationSlotDropAtPubNode(wrconn, syncslotname, true); |
| 1055 |
} |
| 1056 |
} |
| 1057 |
} |
| 1058 |
PG_FINALLY(); |
| 1059 |
{ |
| 1060 |
walrcv_disconnect(wrconn); |
| 1061 |
} |
| 1062 |
PG_END_TRY(); |
| 1063 |
|
| 1064 |
if (rel) |
| 1065 |
table_close(rel, NoLock); |
| 1066 |
} |
| 1067 |
|
| 1068 |
/* |
| 1069 |
* Common checks for altering failover and two_phase options. |
| 1070 |
*/ |
| 1071 |
static void |
| 1072 |
CheckAlterSubOption(Subscription *sub, const char *option, |
| 1073 |
bool slot_needs_update, bool isTopLevel) |
| 1074 |
{ |
| 1075 |
/* |
| 1076 |
* The checks in this function are required only for failover and |
| 1077 |
* two_phase options. |
| 1078 |
*/ |
| 1079 |
Assert(strcmp(option, "failover") == 0 || |
| 1080 |
strcmp(option, "two_phase") == 0); |
| 1081 |
|
| 1082 |
/* |
| 1083 |
* Do not allow changing the option if the subscription is enabled. This |
| 1084 |
* is because both failover and two_phase options of the slot on the |
| 1085 |
* publisher cannot be modified if the slot is currently acquired by the |
| 1086 |
* existing walsender. |
| 1087 |
* |
| 1088 |
* Note that two_phase is enabled (aka changed from 'false' to 'true') on |
| 1089 |
* the publisher by the existing walsender, so we could have allowed that |
| 1090 |
* even when the subscription is enabled. But we kept this restriction for |
| 1091 |
* the sake of consistency and simplicity. |
| 1092 |
*/ |
| 1093 |
if (sub->enabled) |
| 1094 |
ereport(ERROR, |
| 1095 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 1096 |
errmsg("cannot set option \"%s\" for enabled subscription", |
| 1097 |
option))); |
| 1098 |
|
| 1099 |
if (slot_needs_update) |
| 1100 |
{ |
| 1101 |
StringInfoData cmd; |
| 1102 |
|
| 1103 |
/* |
| 1104 |
* A valid slot must be associated with the subscription for us to |
| 1105 |
* modify any of the slot's properties. |
| 1106 |
*/ |
| 1107 |
if (!sub->slotname) |
| 1108 |
ereport(ERROR, |
| 1109 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 1110 |
errmsg("cannot set option \"%s\" for a subscription that does not have a slot name", |
| 1111 |
option))); |
| 1112 |
|
| 1113 |
/* The changed option of the slot can't be rolled back. */ |
| 1114 |
initStringInfo(&cmd); |
| 1115 |
appendStringInfo(&cmd, "ALTER SUBSCRIPTION ... SET (%s)", option); |
| 1116 |
|
| 1117 |
PreventInTransactionBlock(isTopLevel, cmd.data); |
| 1118 |
pfree(cmd.data); |
| 1119 |
} |
| 1120 |
} |
| 1121 |
|
| 1122 |
/* |
| 1123 |
* Alter the existing subscription. |
| 1124 |
*/ |
| 1125 |
ObjectAddress |
| 1126 |
AlterSubscription(ParseState *pstate, AlterSubscriptionStmt *stmt, |
| 1127 |
bool isTopLevel) |
| 1128 |
{ |
| 1129 |
Relation rel; |
| 1130 |
ObjectAddress myself; |
| 1131 |
bool nulls[Natts_pg_subscription]; |
| 1132 |
bool replaces[Natts_pg_subscription]; |
| 1133 |
Datum values[Natts_pg_subscription]; |
| 1134 |
HeapTuple tup; |
| 1135 |
Oid subid; |
| 1136 |
bool update_tuple = false; |
| 1137 |
bool update_failover = false; |
| 1138 |
bool update_two_phase = false; |
| 1139 |
Subscription *sub; |
| 1140 |
Form_pg_subscription form; |
| 1141 |
bits32 supported_opts; |
| 1142 |
SubOpts opts = {0}; |
| 1143 |
|
| 1144 |
rel = table_open(SubscriptionRelationId, RowExclusiveLock); |
| 1145 |
|
| 1146 |
/* Fetch the existing tuple. */ |
| 1147 |
tup = SearchSysCacheCopy2(SUBSCRIPTIONNAME, MyDatabaseId, |
| 1148 |
CStringGetDatum(stmt->subname)); |
| 1149 |
|
| 1150 |
if (!HeapTupleIsValid(tup)) |
| 1151 |
ereport(ERROR, |
| 1152 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 1153 |
errmsg("subscription \"%s\" does not exist", |
| 1154 |
stmt->subname))); |
| 1155 |
|
| 1156 |
form = (Form_pg_subscription) GETSTRUCT(tup); |
| 1157 |
subid = form->oid; |
| 1158 |
|
| 1159 |
/* must be owner */ |
| 1160 |
if (!object_ownercheck(SubscriptionRelationId, subid, GetUserId())) |
| 1161 |
aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_SUBSCRIPTION, |
| 1162 |
stmt->subname); |
| 1163 |
|
| 1164 |
sub = GetSubscription(subid, false); |
| 1165 |
|
| 1166 |
/* |
| 1167 |
* Don't allow non-superuser modification of a subscription with |
| 1168 |
* password_required=false. |
| 1169 |
*/ |
| 1170 |
if (!sub->passwordrequired && !superuser()) |
| 1171 |
ereport(ERROR, |
| 1172 |
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 1173 |
errmsg("password_required=false is superuser-only"), |
| 1174 |
errhint("Subscriptions with the password_required option set to false may only be created or modified by the superuser."))); |
| 1175 |
|
| 1176 |
/* Lock the subscription so nobody else can do anything with it. */ |
| 1177 |
LockSharedObject(SubscriptionRelationId, subid, 0, AccessExclusiveLock); |
| 1178 |
|
| 1179 |
/* Form a new tuple. */ |
| 1180 |
memset(values, 0, sizeof(values)); |
| 1181 |
memset(nulls, false, sizeof(nulls)); |
| 1182 |
memset(replaces, false, sizeof(replaces)); |
| 1183 |
|
| 1184 |
switch (stmt->kind) |
| 1185 |
{ |
| 1186 |
case ALTER_SUBSCRIPTION_OPTIONS: |
| 1187 |
{ |
| 1188 |
supported_opts = (SUBOPT_SLOT_NAME | |
| 1189 |
SUBOPT_SYNCHRONOUS_COMMIT | SUBOPT_BINARY | |
| 1190 |
SUBOPT_STREAMING | SUBOPT_TWOPHASE_COMMIT | |
| 1191 |
SUBOPT_DISABLE_ON_ERR | |
| 1192 |
SUBOPT_PASSWORD_REQUIRED | |
| 1193 |
SUBOPT_RUN_AS_OWNER | SUBOPT_FAILOVER | |
| 1194 |
SUBOPT_ORIGIN); |
| 1195 |
|
| 1196 |
parse_subscription_options(pstate, stmt->options, |
| 1197 |
supported_opts, &opts); |
| 1198 |
|
| 1199 |
if (IsSet(opts.specified_opts, SUBOPT_SLOT_NAME)) |
| 1200 |
{ |
| 1201 |
/* |
| 1202 |
* The subscription must be disabled to allow slot_name as |
| 1203 |
* 'none', otherwise, the apply worker will repeatedly try |
| 1204 |
* to stream the data using that slot_name which neither |
| 1205 |
* exists on the publisher nor the user will be allowed to |
| 1206 |
* create it. |
| 1207 |
*/ |
| 1208 |
if (sub->enabled && !opts.slot_name) |
| 1209 |
ereport(ERROR, |
| 1210 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 1211 |
errmsg("cannot set %s for enabled subscription", |
| 1212 |
"slot_name = NONE"))); |
| 1213 |
|
| 1214 |
if (opts.slot_name) |
| 1215 |
values[Anum_pg_subscription_subslotname - 1] = |
| 1216 |
DirectFunctionCall1(namein, CStringGetDatum(opts.slot_name)); |
| 1217 |
else |
| 1218 |
nulls[Anum_pg_subscription_subslotname - 1] = true; |
| 1219 |
replaces[Anum_pg_subscription_subslotname - 1] = true; |
| 1220 |
} |
| 1221 |
|
| 1222 |
if (opts.synchronous_commit) |
| 1223 |
{ |
| 1224 |
values[Anum_pg_subscription_subsynccommit - 1] = |
| 1225 |
CStringGetTextDatum(opts.synchronous_commit); |
| 1226 |
replaces[Anum_pg_subscription_subsynccommit - 1] = true; |
| 1227 |
} |
| 1228 |
|
| 1229 |
if (IsSet(opts.specified_opts, SUBOPT_BINARY)) |
| 1230 |
{ |
| 1231 |
values[Anum_pg_subscription_subbinary - 1] = |
| 1232 |
BoolGetDatum(opts.binary); |
| 1233 |
replaces[Anum_pg_subscription_subbinary - 1] = true; |
| 1234 |
} |
| 1235 |
|
| 1236 |
if (IsSet(opts.specified_opts, SUBOPT_STREAMING)) |
| 1237 |
{ |
| 1238 |
values[Anum_pg_subscription_substream - 1] = |
| 1239 |
CharGetDatum(opts.streaming); |
| 1240 |
replaces[Anum_pg_subscription_substream - 1] = true; |
| 1241 |
} |
| 1242 |
|
| 1243 |
if (IsSet(opts.specified_opts, SUBOPT_DISABLE_ON_ERR)) |
| 1244 |
{ |
| 1245 |
values[Anum_pg_subscription_subdisableonerr - 1] |
| 1246 |
= BoolGetDatum(opts.disableonerr); |
| 1247 |
replaces[Anum_pg_subscription_subdisableonerr - 1] |
| 1248 |
= true; |
| 1249 |
} |
| 1250 |
|
| 1251 |
if (IsSet(opts.specified_opts, SUBOPT_PASSWORD_REQUIRED)) |
| 1252 |
{ |
| 1253 |
/* Non-superuser may not disable password_required. */ |
| 1254 |
if (!opts.passwordrequired && !superuser()) |
| 1255 |
ereport(ERROR, |
| 1256 |
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 1257 |
errmsg("password_required=false is superuser-only"), |
| 1258 |
errhint("Subscriptions with the password_required option set to false may only be created or modified by the superuser."))); |
| 1259 |
|
| 1260 |
values[Anum_pg_subscription_subpasswordrequired - 1] |
| 1261 |
= BoolGetDatum(opts.passwordrequired); |
| 1262 |
replaces[Anum_pg_subscription_subpasswordrequired - 1] |
| 1263 |
= true; |
| 1264 |
} |
| 1265 |
|
| 1266 |
if (IsSet(opts.specified_opts, SUBOPT_RUN_AS_OWNER)) |
| 1267 |
{ |
| 1268 |
values[Anum_pg_subscription_subrunasowner - 1] = |
| 1269 |
BoolGetDatum(opts.runasowner); |
| 1270 |
replaces[Anum_pg_subscription_subrunasowner - 1] = true; |
| 1271 |
} |
| 1272 |
|
| 1273 |
if (IsSet(opts.specified_opts, SUBOPT_TWOPHASE_COMMIT)) |
| 1274 |
{ |
| 1275 |
/* |
| 1276 |
* We need to update both the slot and the subscription |
| 1277 |
* for the two_phase option. We can enable the two_phase |
| 1278 |
* option for a slot only once the initial data |
| 1279 |
* synchronization is done. This is to avoid missing some |
| 1280 |
* data as explained in comments atop worker.c. |
| 1281 |
*/ |
| 1282 |
update_two_phase = !opts.twophase; |
| 1283 |
|
| 1284 |
CheckAlterSubOption(sub, "two_phase", update_two_phase, |
| 1285 |
isTopLevel); |
| 1286 |
|
| 1287 |
/* |
| 1288 |
* Modifying the two_phase slot option requires a slot |
| 1289 |
* lookup by slot name, so changing the slot name at the |
| 1290 |
* same time is not allowed. |
| 1291 |
*/ |
| 1292 |
if (update_two_phase && |
| 1293 |
IsSet(opts.specified_opts, SUBOPT_SLOT_NAME)) |
| 1294 |
ereport(ERROR, |
| 1295 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 1296 |
errmsg("\"slot_name\" and \"two_phase\" cannot be altered at the same time"))); |
| 1297 |
|
| 1298 |
/* |
| 1299 |
* Note that workers may still survive even if the |
| 1300 |
* subscription has been disabled. |
| 1301 |
* |
| 1302 |
* Ensure workers have already been exited to avoid |
| 1303 |
* getting prepared transactions while we are disabling |
| 1304 |
* the two_phase option. Otherwise, the changes of an |
| 1305 |
* already prepared transaction can be replicated again |
| 1306 |
* along with its corresponding commit, leading to |
| 1307 |
* duplicate data or errors. |
| 1308 |
*/ |
| 1309 |
if (logicalrep_workers_find(subid, true, true)) |
| 1310 |
ereport(ERROR, |
| 1311 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 1312 |
errmsg("cannot alter \"two_phase\" when logical replication worker is still running"), |
| 1313 |
errhint("Try again after some time."))); |
| 1314 |
|
| 1315 |
/* |
| 1316 |
* two_phase cannot be disabled if there are any |
| 1317 |
* uncommitted prepared transactions present otherwise it |
| 1318 |
* can lead to duplicate data or errors as explained in |
| 1319 |
* the comment above. |
| 1320 |
*/ |
| 1321 |
if (update_two_phase && |
| 1322 |
sub->twophasestate == LOGICALREP_TWOPHASE_STATE_ENABLED && |
| 1323 |
LookupGXactBySubid(subid)) |
| 1324 |
ereport(ERROR, |
| 1325 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 1326 |
errmsg("cannot disable \"two_phase\" when prepared transactions exist"), |
| 1327 |
errhint("Resolve these transactions and try again."))); |
| 1328 |
|
| 1329 |
/* Change system catalog accordingly */ |
| 1330 |
values[Anum_pg_subscription_subtwophasestate - 1] = |
| 1331 |
CharGetDatum(opts.twophase ? |
| 1332 |
LOGICALREP_TWOPHASE_STATE_PENDING : |
| 1333 |
LOGICALREP_TWOPHASE_STATE_DISABLED); |
| 1334 |
replaces[Anum_pg_subscription_subtwophasestate - 1] = true; |
| 1335 |
} |
| 1336 |
|
| 1337 |
if (IsSet(opts.specified_opts, SUBOPT_FAILOVER)) |
| 1338 |
{ |
| 1339 |
/* |
| 1340 |
* Similar to the two_phase case above, we need to update |
| 1341 |
* the failover option for both the slot and the |
| 1342 |
* subscription. |
| 1343 |
*/ |
| 1344 |
update_failover = true; |
| 1345 |
|
| 1346 |
CheckAlterSubOption(sub, "failover", update_failover, |
| 1347 |
isTopLevel); |
| 1348 |
|
| 1349 |
values[Anum_pg_subscription_subfailover - 1] = |
| 1350 |
BoolGetDatum(opts.failover); |
| 1351 |
replaces[Anum_pg_subscription_subfailover - 1] = true; |
| 1352 |
} |
| 1353 |
|
| 1354 |
if (IsSet(opts.specified_opts, SUBOPT_ORIGIN)) |
| 1355 |
{ |
| 1356 |
values[Anum_pg_subscription_suborigin - 1] = |
| 1357 |
CStringGetTextDatum(opts.origin); |
| 1358 |
replaces[Anum_pg_subscription_suborigin - 1] = true; |
| 1359 |
} |
| 1360 |
|
| 1361 |
update_tuple = true; |
| 1362 |
break; |
| 1363 |
} |
| 1364 |
|
| 1365 |
case ALTER_SUBSCRIPTION_ENABLED: |
| 1366 |
{ |
| 1367 |
parse_subscription_options(pstate, stmt->options, |
| 1368 |
SUBOPT_ENABLED, &opts); |
| 1369 |
Assert(IsSet(opts.specified_opts, SUBOPT_ENABLED)); |
| 1370 |
|
| 1371 |
if (!sub->slotname && opts.enabled) |
| 1372 |
ereport(ERROR, |
| 1373 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 1374 |
errmsg("cannot enable subscription that does not have a slot name"))); |
| 1375 |
|
| 1376 |
values[Anum_pg_subscription_subenabled - 1] = |
| 1377 |
BoolGetDatum(opts.enabled); |
| 1378 |
replaces[Anum_pg_subscription_subenabled - 1] = true; |
| 1379 |
|
| 1380 |
if (opts.enabled) |
| 1381 |
ApplyLauncherWakeupAtCommit(); |
| 1382 |
|
| 1383 |
update_tuple = true; |
| 1384 |
break; |
| 1385 |
} |
| 1386 |
|
| 1387 |
case ALTER_SUBSCRIPTION_CONNECTION: |
| 1388 |
/* Load the library providing us libpq calls. */ |
| 1389 |
load_file("libpqwalreceiver", false); |
| 1390 |
/* Check the connection info string. */ |
| 1391 |
walrcv_check_conninfo(stmt->conninfo, |
| 1392 |
sub->passwordrequired && !sub->ownersuperuser); |
| 1393 |
|
| 1394 |
values[Anum_pg_subscription_subconninfo - 1] = |
| 1395 |
CStringGetTextDatum(stmt->conninfo); |
| 1396 |
replaces[Anum_pg_subscription_subconninfo - 1] = true; |
| 1397 |
update_tuple = true; |
| 1398 |
break; |
| 1399 |
|
| 1400 |
case ALTER_SUBSCRIPTION_SET_PUBLICATION: |
| 1401 |
{ |
| 1402 |
supported_opts = SUBOPT_COPY_DATA | SUBOPT_REFRESH; |
| 1403 |
parse_subscription_options(pstate, stmt->options, |
| 1404 |
supported_opts, &opts); |
| 1405 |
|
| 1406 |
values[Anum_pg_subscription_subpublications - 1] = |
| 1407 |
publicationListToArray(stmt->publication); |
| 1408 |
replaces[Anum_pg_subscription_subpublications - 1] = true; |
| 1409 |
|
| 1410 |
update_tuple = true; |
| 1411 |
|
| 1412 |
/* Refresh if user asked us to. */ |
| 1413 |
if (opts.refresh) |
| 1414 |
{ |
| 1415 |
if (!sub->enabled) |
| 1416 |
ereport(ERROR, |
| 1417 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 1418 |
errmsg("ALTER SUBSCRIPTION with refresh is not allowed for disabled subscriptions"), |
| 1419 |
errhint("Use ALTER SUBSCRIPTION ... SET PUBLICATION ... WITH (refresh = false)."))); |
| 1420 |
|
| 1421 |
/* |
| 1422 |
* See ALTER_SUBSCRIPTION_REFRESH for details why this is |
| 1423 |
* not allowed. |
| 1424 |
*/ |
| 1425 |
if (sub->twophasestate == LOGICALREP_TWOPHASE_STATE_ENABLED && opts.copy_data) |
| 1426 |
ereport(ERROR, |
| 1427 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 1428 |
errmsg("ALTER SUBSCRIPTION with refresh and copy_data is not allowed when two_phase is enabled"), |
| 1429 |
errhint("Use ALTER SUBSCRIPTION ... SET PUBLICATION with refresh = false, or with copy_data = false, or use DROP/CREATE SUBSCRIPTION."))); |
| 1430 |
|
| 1431 |
PreventInTransactionBlock(isTopLevel, "ALTER SUBSCRIPTION with refresh"); |
| 1432 |
|
| 1433 |
/* Make sure refresh sees the new list of publications. */ |
| 1434 |
sub->publications = stmt->publication; |
| 1435 |
|
| 1436 |
AlterSubscription_refresh(sub, opts.copy_data, |
| 1437 |
stmt->publication); |
| 1438 |
} |
| 1439 |
|
| 1440 |
break; |
| 1441 |
} |
| 1442 |
|
| 1443 |
case ALTER_SUBSCRIPTION_ADD_PUBLICATION: |
| 1444 |
case ALTER_SUBSCRIPTION_DROP_PUBLICATION: |
| 1445 |
{ |
| 1446 |
List *publist; |
| 1447 |
bool isadd = stmt->kind == ALTER_SUBSCRIPTION_ADD_PUBLICATION; |
| 1448 |
|
| 1449 |
supported_opts = SUBOPT_REFRESH | SUBOPT_COPY_DATA; |
| 1450 |
parse_subscription_options(pstate, stmt->options, |
| 1451 |
supported_opts, &opts); |
| 1452 |
|
| 1453 |
publist = merge_publications(sub->publications, stmt->publication, isadd, stmt->subname); |
| 1454 |
values[Anum_pg_subscription_subpublications - 1] = |
| 1455 |
publicationListToArray(publist); |
| 1456 |
replaces[Anum_pg_subscription_subpublications - 1] = true; |
| 1457 |
|
| 1458 |
update_tuple = true; |
| 1459 |
|
| 1460 |
/* Refresh if user asked us to. */ |
| 1461 |
if (opts.refresh) |
| 1462 |
{ |
| 1463 |
/* We only need to validate user specified publications. */ |
| 1464 |
List *validate_publications = (isadd) ? stmt->publication : NULL; |
| 1465 |
|
| 1466 |
if (!sub->enabled) |
| 1467 |
ereport(ERROR, |
| 1468 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 1469 |
errmsg("ALTER SUBSCRIPTION with refresh is not allowed for disabled subscriptions"), |
| 1470 |
/* translator: %s is an SQL ALTER command */ |
| 1471 |
errhint("Use %s instead.", |
| 1472 |
isadd ? |
| 1473 |
"ALTER SUBSCRIPTION ... ADD PUBLICATION ... WITH (refresh = false)" : |
| 1474 |
"ALTER SUBSCRIPTION ... DROP PUBLICATION ... WITH (refresh = false)"))); |
| 1475 |
|
| 1476 |
/* |
| 1477 |
* See ALTER_SUBSCRIPTION_REFRESH for details why this is |
| 1478 |
* not allowed. |
| 1479 |
*/ |
| 1480 |
if (sub->twophasestate == LOGICALREP_TWOPHASE_STATE_ENABLED && opts.copy_data) |
| 1481 |
ereport(ERROR, |
| 1482 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 1483 |
errmsg("ALTER SUBSCRIPTION with refresh and copy_data is not allowed when two_phase is enabled"), |
| 1484 |
/* translator: %s is an SQL ALTER command */ |
| 1485 |
errhint("Use %s with refresh = false, or with copy_data = false, or use DROP/CREATE SUBSCRIPTION.", |
| 1486 |
isadd ? |
| 1487 |
"ALTER SUBSCRIPTION ... ADD PUBLICATION" : |
| 1488 |
"ALTER SUBSCRIPTION ... DROP PUBLICATION"))); |
| 1489 |
|
| 1490 |
PreventInTransactionBlock(isTopLevel, "ALTER SUBSCRIPTION with refresh"); |
| 1491 |
|
| 1492 |
/* Refresh the new list of publications. */ |
| 1493 |
sub->publications = publist; |
| 1494 |
|
| 1495 |
AlterSubscription_refresh(sub, opts.copy_data, |
| 1496 |
validate_publications); |
| 1497 |
} |
| 1498 |
|
| 1499 |
break; |
| 1500 |
} |
| 1501 |
|
| 1502 |
case ALTER_SUBSCRIPTION_REFRESH: |
| 1503 |
{ |
| 1504 |
if (!sub->enabled) |
| 1505 |
ereport(ERROR, |
| 1506 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 1507 |
errmsg("ALTER SUBSCRIPTION ... REFRESH is not allowed for disabled subscriptions"))); |
| 1508 |
|
| 1509 |
parse_subscription_options(pstate, stmt->options, |
| 1510 |
SUBOPT_COPY_DATA, &opts); |
| 1511 |
|
| 1512 |
/* |
| 1513 |
* The subscription option "two_phase" requires that |
| 1514 |
* replication has passed the initial table synchronization |
| 1515 |
* phase before the two_phase becomes properly enabled. |
| 1516 |
* |
| 1517 |
* But, having reached this two-phase commit "enabled" state |
| 1518 |
* we must not allow any subsequent table initialization to |
| 1519 |
* occur. So the ALTER SUBSCRIPTION ... REFRESH is disallowed |
| 1520 |
* when the user had requested two_phase = on mode. |
| 1521 |
* |
| 1522 |
* The exception to this restriction is when copy_data = |
| 1523 |
* false, because when copy_data is false the tablesync will |
| 1524 |
* start already in READY state and will exit directly without |
| 1525 |
* doing anything. |
| 1526 |
* |
| 1527 |
* For more details see comments atop worker.c. |
| 1528 |
*/ |
| 1529 |
if (sub->twophasestate == LOGICALREP_TWOPHASE_STATE_ENABLED && opts.copy_data) |
| 1530 |
ereport(ERROR, |
| 1531 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 1532 |
errmsg("ALTER SUBSCRIPTION ... REFRESH with copy_data is not allowed when two_phase is enabled"), |
| 1533 |
errhint("Use ALTER SUBSCRIPTION ... REFRESH with copy_data = false, or use DROP/CREATE SUBSCRIPTION."))); |
| 1534 |
|
| 1535 |
PreventInTransactionBlock(isTopLevel, "ALTER SUBSCRIPTION ... REFRESH"); |
| 1536 |
|
| 1537 |
AlterSubscription_refresh(sub, opts.copy_data, NULL); |
| 1538 |
|
| 1539 |
break; |
| 1540 |
} |
| 1541 |
|
| 1542 |
case ALTER_SUBSCRIPTION_SKIP: |
| 1543 |
{ |
| 1544 |
parse_subscription_options(pstate, stmt->options, SUBOPT_LSN, &opts); |
| 1545 |
|
| 1546 |
/* ALTER SUBSCRIPTION ... SKIP supports only LSN option */ |
| 1547 |
Assert(IsSet(opts.specified_opts, SUBOPT_LSN)); |
| 1548 |
|
| 1549 |
/* |
| 1550 |
* If the user sets subskiplsn, we do a sanity check to make |
| 1551 |
* sure that the specified LSN is a probable value. |
| 1552 |
*/ |
| 1553 |
if (!XLogRecPtrIsInvalid(opts.lsn)) |
| 1554 |
{ |
| 1555 |
RepOriginId originid; |
| 1556 |
char originname[NAMEDATALEN]; |
| 1557 |
XLogRecPtr remote_lsn; |
| 1558 |
|
| 1559 |
ReplicationOriginNameForLogicalRep(subid, InvalidOid, |
| 1560 |
originname, sizeof(originname)); |
| 1561 |
originid = replorigin_by_name(originname, false); |
| 1562 |
remote_lsn = replorigin_get_progress(originid, false); |
| 1563 |
|
| 1564 |
/* Check the given LSN is at least a future LSN */ |
| 1565 |
if (!XLogRecPtrIsInvalid(remote_lsn) && opts.lsn < remote_lsn) |
| 1566 |
ereport(ERROR, |
| 1567 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 1568 |
errmsg("skip WAL location (LSN %X/%X) must be greater than origin LSN %X/%X", |
| 1569 |
LSN_FORMAT_ARGS(opts.lsn), |
| 1570 |
LSN_FORMAT_ARGS(remote_lsn)))); |
| 1571 |
} |
| 1572 |
|
| 1573 |
values[Anum_pg_subscription_subskiplsn - 1] = LSNGetDatum(opts.lsn); |
| 1574 |
replaces[Anum_pg_subscription_subskiplsn - 1] = true; |
| 1575 |
|
| 1576 |
update_tuple = true; |
| 1577 |
break; |
| 1578 |
} |
| 1579 |
|
| 1580 |
default: |
| 1581 |
elog(ERROR, "unrecognized ALTER SUBSCRIPTION kind %d", |
| 1582 |
stmt->kind); |
| 1583 |
} |
| 1584 |
|
| 1585 |
/* Update the catalog if needed. */ |
| 1586 |
if (update_tuple) |
| 1587 |
{ |
| 1588 |
tup = heap_modify_tuple(tup, RelationGetDescr(rel), values, nulls, |
| 1589 |
replaces); |
| 1590 |
|
| 1591 |
CatalogTupleUpdate(rel, &tup->t_self, tup); |
| 1592 |
|
| 1593 |
heap_freetuple(tup); |
| 1594 |
} |
| 1595 |
|
| 1596 |
/* |
| 1597 |
* Try to acquire the connection necessary for altering the slot, if |
| 1598 |
* needed. |
| 1599 |
* |
| 1600 |
* This has to be at the end because otherwise if there is an error while |
| 1601 |
* doing the database operations we won't be able to rollback altered |
| 1602 |
* slot. |
| 1603 |
*/ |
| 1604 |
if (update_failover || update_two_phase) |
| 1605 |
{ |
| 1606 |
bool must_use_password; |
| 1607 |
char *err; |
| 1608 |
WalReceiverConn *wrconn; |
| 1609 |
|
| 1610 |
/* Load the library providing us libpq calls. */ |
| 1611 |
load_file("libpqwalreceiver", false); |
| 1612 |
|
| 1613 |
/* Try to connect to the publisher. */ |
| 1614 |
must_use_password = sub->passwordrequired && !sub->ownersuperuser; |
| 1615 |
wrconn = walrcv_connect(sub->conninfo, true, true, must_use_password, |
| 1616 |
sub->name, &err); |
| 1617 |
if (!wrconn) |
| 1618 |
ereport(ERROR, |
| 1619 |
(errcode(ERRCODE_CONNECTION_FAILURE), |
| 1620 |
errmsg("subscription \"%s\" could not connect to the publisher: %s", |
| 1621 |
sub->name, err))); |
| 1622 |
|
| 1623 |
PG_TRY(); |
| 1624 |
{ |
| 1625 |
walrcv_alter_slot(wrconn, sub->slotname, |
| 1626 |
update_failover ? &opts.failover : NULL, |
| 1627 |
update_two_phase ? &opts.twophase : NULL); |
| 1628 |
} |
| 1629 |
PG_FINALLY(); |
| 1630 |
{ |
| 1631 |
walrcv_disconnect(wrconn); |
| 1632 |
} |
| 1633 |
PG_END_TRY(); |
| 1634 |
} |
| 1635 |
|
| 1636 |
table_close(rel, RowExclusiveLock); |
| 1637 |
|
| 1638 |
ObjectAddressSet(myself, SubscriptionRelationId, subid); |
| 1639 |
|
| 1640 |
InvokeObjectPostAlterHook(SubscriptionRelationId, subid, 0); |
| 1641 |
|
| 1642 |
/* Wake up related replication workers to handle this change quickly. */ |
| 1643 |
LogicalRepWorkersWakeupAtCommit(subid); |
| 1644 |
|
| 1645 |
return myself; |
| 1646 |
} |
| 1647 |
|
| 1648 |
/* |
| 1649 |
* Drop a subscription |
| 1650 |
*/ |
| 1651 |
void |
| 1652 |
DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel) |
| 1653 |
{ |
| 1654 |
Relation rel; |
| 1655 |
ObjectAddress myself; |
| 1656 |
HeapTuple tup; |
| 1657 |
Oid subid; |
| 1658 |
Oid subowner; |
| 1659 |
Datum datum; |
| 1660 |
bool isnull; |
| 1661 |
char *subname; |
| 1662 |
char *conninfo; |
| 1663 |
char *slotname; |
| 1664 |
List *subworkers; |
| 1665 |
ListCell *lc; |
| 1666 |
char originname[NAMEDATALEN]; |
| 1667 |
char *err = NULL; |
| 1668 |
WalReceiverConn *wrconn; |
| 1669 |
Form_pg_subscription form; |
| 1670 |
List *rstates; |
| 1671 |
bool must_use_password; |
| 1672 |
|
| 1673 |
/* |
| 1674 |
* The launcher may concurrently start a new worker for this subscription. |
| 1675 |
* During initialization, the worker checks for subscription validity and |
| 1676 |
* exits if the subscription has already been dropped. See |
| 1677 |
* InitializeLogRepWorker. |
| 1678 |
*/ |
| 1679 |
rel = table_open(SubscriptionRelationId, RowExclusiveLock); |
| 1680 |
|
| 1681 |
tup = SearchSysCache2(SUBSCRIPTIONNAME, MyDatabaseId, |
| 1682 |
CStringGetDatum(stmt->subname)); |
| 1683 |
|
| 1684 |
if (!HeapTupleIsValid(tup)) |
| 1685 |
{ |
| 1686 |
table_close(rel, NoLock); |
| 1687 |
|
| 1688 |
if (!stmt->missing_ok) |
| 1689 |
ereport(ERROR, |
| 1690 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 1691 |
errmsg("subscription \"%s\" does not exist", |
| 1692 |
stmt->subname))); |
| 1693 |
else |
| 1694 |
ereport(NOTICE, |
| 1695 |
(errmsg("subscription \"%s\" does not exist, skipping", |
| 1696 |
stmt->subname))); |
| 1697 |
|
| 1698 |
return; |
| 1699 |
} |
| 1700 |
|
| 1701 |
form = (Form_pg_subscription) GETSTRUCT(tup); |
| 1702 |
subid = form->oid; |
| 1703 |
subowner = form->subowner; |
| 1704 |
must_use_password = !superuser_arg(subowner) && form->subpasswordrequired; |
| 1705 |
|
| 1706 |
/* must be owner */ |
| 1707 |
if (!object_ownercheck(SubscriptionRelationId, subid, GetUserId())) |
| 1708 |
aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_SUBSCRIPTION, |
| 1709 |
stmt->subname); |
| 1710 |
|
| 1711 |
/* DROP hook for the subscription being removed */ |
| 1712 |
InvokeObjectDropHook(SubscriptionRelationId, subid, 0); |
| 1713 |
|
| 1714 |
/* |
| 1715 |
* Lock the subscription so nobody else can do anything with it (including |
| 1716 |
* the replication workers). |
| 1717 |
*/ |
| 1718 |
LockSharedObject(SubscriptionRelationId, subid, 0, AccessExclusiveLock); |
| 1719 |
|
| 1720 |
/* Get subname */ |
| 1721 |
datum = SysCacheGetAttrNotNull(SUBSCRIPTIONOID, tup, |
| 1722 |
Anum_pg_subscription_subname); |
| 1723 |
subname = pstrdup(NameStr(*DatumGetName(datum))); |
| 1724 |
|
| 1725 |
/* Get conninfo */ |
| 1726 |
datum = SysCacheGetAttrNotNull(SUBSCRIPTIONOID, tup, |
| 1727 |
Anum_pg_subscription_subconninfo); |
| 1728 |
conninfo = TextDatumGetCString(datum); |
| 1729 |
|
| 1730 |
/* Get slotname */ |
| 1731 |
datum = SysCacheGetAttr(SUBSCRIPTIONOID, tup, |
| 1732 |
Anum_pg_subscription_subslotname, &isnull); |
| 1733 |
if (!isnull) |
| 1734 |
slotname = pstrdup(NameStr(*DatumGetName(datum))); |
| 1735 |
else |
| 1736 |
slotname = NULL; |
| 1737 |
|
| 1738 |
/* |
| 1739 |
* Since dropping a replication slot is not transactional, the replication |
| 1740 |
* slot stays dropped even if the transaction rolls back. So we cannot |
| 1741 |
* run DROP SUBSCRIPTION inside a transaction block if dropping the |
| 1742 |
* replication slot. Also, in this case, we report a message for dropping |
| 1743 |
* the subscription to the cumulative stats system. |
| 1744 |
* |
| 1745 |
* XXX The command name should really be something like "DROP SUBSCRIPTION |
| 1746 |
* of a subscription that is associated with a replication slot", but we |
| 1747 |
* don't have the proper facilities for that. |
| 1748 |
*/ |
| 1749 |
if (slotname) |
| 1750 |
PreventInTransactionBlock(isTopLevel, "DROP SUBSCRIPTION"); |
| 1751 |
|
| 1752 |
ObjectAddressSet(myself, SubscriptionRelationId, subid); |
| 1753 |
EventTriggerSQLDropAddObject(&myself, true, true); |
| 1754 |
|
| 1755 |
/* Remove the tuple from catalog. */ |
| 1756 |
CatalogTupleDelete(rel, &tup->t_self); |
| 1757 |
|
| 1758 |
ReleaseSysCache(tup); |
| 1759 |
|
| 1760 |
/* |
| 1761 |
* Stop all the subscription workers immediately. |
| 1762 |
* |
| 1763 |
* This is necessary if we are dropping the replication slot, so that the |
| 1764 |
* slot becomes accessible. |
| 1765 |
* |
| 1766 |
* It is also necessary if the subscription is disabled and was disabled |
| 1767 |
* in the same transaction. Then the workers haven't seen the disabling |
| 1768 |
* yet and will still be running, leading to hangs later when we want to |
| 1769 |
* drop the replication origin. If the subscription was disabled before |
| 1770 |
* this transaction, then there shouldn't be any workers left, so this |
| 1771 |
* won't make a difference. |
| 1772 |
* |
| 1773 |
* New workers won't be started because we hold an exclusive lock on the |
| 1774 |
* subscription till the end of the transaction. |
| 1775 |
*/ |
| 1776 |
subworkers = logicalrep_workers_find(subid, false, true); |
| 1777 |
foreach(lc, subworkers) |
| 1778 |
{ |
| 1779 |
LogicalRepWorker *w = (LogicalRepWorker *) lfirst(lc); |
| 1780 |
|
| 1781 |
logicalrep_worker_stop(w->subid, w->relid); |
| 1782 |
} |
| 1783 |
list_free(subworkers); |
| 1784 |
|
| 1785 |
/* |
| 1786 |
* Remove the no-longer-useful entry in the launcher's table of apply |
| 1787 |
* worker start times. |
| 1788 |
* |
| 1789 |
* If this transaction rolls back, the launcher might restart a failed |
| 1790 |
* apply worker before wal_retrieve_retry_interval milliseconds have |
| 1791 |
* elapsed, but that's pretty harmless. |
| 1792 |
*/ |
| 1793 |
ApplyLauncherForgetWorkerStartTime(subid); |
| 1794 |
|
| 1795 |
/* |
| 1796 |
* Cleanup of tablesync replication origins. |
| 1797 |
* |
| 1798 |
* Any READY-state relations would already have dealt with clean-ups. |
| 1799 |
* |
| 1800 |
* Note that the state can't change because we have already stopped both |
| 1801 |
* the apply and tablesync workers and they can't restart because of |
| 1802 |
* exclusive lock on the subscription. |
| 1803 |
*/ |
| 1804 |
rstates = GetSubscriptionRelations(subid, true); |
| 1805 |
foreach(lc, rstates) |
| 1806 |
{ |
| 1807 |
SubscriptionRelState *rstate = (SubscriptionRelState *) lfirst(lc); |
| 1808 |
Oid relid = rstate->relid; |
| 1809 |
|
| 1810 |
/* Only cleanup resources of tablesync workers */ |
| 1811 |
if (!OidIsValid(relid)) |
| 1812 |
continue; |
| 1813 |
|
| 1814 |
/* |
| 1815 |
* Drop the tablesync's origin tracking if exists. |
| 1816 |
* |
| 1817 |
* It is possible that the origin is not yet created for tablesync |
| 1818 |
* worker so passing missing_ok = true. This can happen for the states |
| 1819 |
* before SUBREL_STATE_DATASYNC. |
| 1820 |
*/ |
| 1821 |
ReplicationOriginNameForLogicalRep(subid, relid, originname, |
| 1822 |
sizeof(originname)); |
| 1823 |
replorigin_drop_by_name(originname, true, false); |
| 1824 |
} |
| 1825 |
|
| 1826 |
/* Clean up dependencies */ |
| 1827 |
deleteSharedDependencyRecordsFor(SubscriptionRelationId, subid, 0); |
| 1828 |
|
| 1829 |
/* Remove any associated relation synchronization states. */ |
| 1830 |
RemoveSubscriptionRel(subid, InvalidOid); |
| 1831 |
|
| 1832 |
/* Remove the origin tracking if exists. */ |
| 1833 |
ReplicationOriginNameForLogicalRep(subid, InvalidOid, originname, sizeof(originname)); |
| 1834 |
replorigin_drop_by_name(originname, true, false); |
| 1835 |
|
| 1836 |
/* |
| 1837 |
* Tell the cumulative stats system that the subscription is getting |
| 1838 |
* dropped. |
| 1839 |
*/ |
| 1840 |
pgstat_drop_subscription(subid); |
| 1841 |
|
| 1842 |
/* |
| 1843 |
* If there is no slot associated with the subscription, we can finish |
| 1844 |
* here. |
| 1845 |
*/ |
| 1846 |
if (!slotname && rstates == NIL) |
| 1847 |
{ |
| 1848 |
table_close(rel, NoLock); |
| 1849 |
return; |
| 1850 |
} |
| 1851 |
|
| 1852 |
/* |
| 1853 |
* Try to acquire the connection necessary for dropping slots. |
| 1854 |
* |
| 1855 |
* Note: If the slotname is NONE/NULL then we allow the command to finish |
| 1856 |
* and users need to manually cleanup the apply and tablesync worker slots |
| 1857 |
* later. |
| 1858 |
* |
| 1859 |
* This has to be at the end because otherwise if there is an error while |
| 1860 |
* doing the database operations we won't be able to rollback dropped |
| 1861 |
* slot. |
| 1862 |
*/ |
| 1863 |
load_file("libpqwalreceiver", false); |
| 1864 |
|
| 1865 |
wrconn = walrcv_connect(conninfo, true, true, must_use_password, |
| 1866 |
subname, &err); |
| 1867 |
if (wrconn == NULL) |
| 1868 |
{ |
| 1869 |
if (!slotname) |
| 1870 |
{ |
| 1871 |
/* be tidy */ |
| 1872 |
list_free(rstates); |
| 1873 |
table_close(rel, NoLock); |
| 1874 |
return; |
| 1875 |
} |
| 1876 |
else |
| 1877 |
{ |
| 1878 |
ReportSlotConnectionError(rstates, subid, slotname, err); |
| 1879 |
} |
| 1880 |
} |
| 1881 |
|
| 1882 |
PG_TRY(); |
| 1883 |
{ |
| 1884 |
foreach(lc, rstates) |
| 1885 |
{ |
| 1886 |
SubscriptionRelState *rstate = (SubscriptionRelState *) lfirst(lc); |
| 1887 |
Oid relid = rstate->relid; |
| 1888 |
|
| 1889 |
/* Only cleanup resources of tablesync workers */ |
| 1890 |
if (!OidIsValid(relid)) |
| 1891 |
continue; |
| 1892 |
|
| 1893 |
/* |
| 1894 |
* Drop the tablesync slots associated with removed tables. |
| 1895 |
* |
| 1896 |
* For SYNCDONE/READY states, the tablesync slot is known to have |
| 1897 |
* already been dropped by the tablesync worker. |
| 1898 |
* |
| 1899 |
* For other states, there is no certainty, maybe the slot does |
| 1900 |
* not exist yet. Also, if we fail after removing some of the |
| 1901 |
* slots, next time, it will again try to drop already dropped |
| 1902 |
* slots and fail. For these reasons, we allow missing_ok = true |
| 1903 |
* for the drop. |
| 1904 |
*/ |
| 1905 |
if (rstate->state != SUBREL_STATE_SYNCDONE) |
| 1906 |
{ |
| 1907 |
char syncslotname[NAMEDATALEN] = {0}; |
| 1908 |
|
| 1909 |
ReplicationSlotNameForTablesync(subid, relid, syncslotname, |
| 1910 |
sizeof(syncslotname)); |
| 1911 |
ReplicationSlotDropAtPubNode(wrconn, syncslotname, true); |
| 1912 |
} |
| 1913 |
} |
| 1914 |
|
| 1915 |
list_free(rstates); |
| 1916 |
|
| 1917 |
/* |
| 1918 |
* If there is a slot associated with the subscription, then drop the |
| 1919 |
* replication slot at the publisher. |
| 1920 |
*/ |
| 1921 |
if (slotname) |
| 1922 |
ReplicationSlotDropAtPubNode(wrconn, slotname, false); |
| 1923 |
} |
| 1924 |
PG_FINALLY(); |
| 1925 |
{ |
| 1926 |
walrcv_disconnect(wrconn); |
| 1927 |
} |
| 1928 |
PG_END_TRY(); |
| 1929 |
|
| 1930 |
table_close(rel, NoLock); |
| 1931 |
} |
| 1932 |
|
| 1933 |
/* |
| 1934 |
* Drop the replication slot at the publisher node using the replication |
| 1935 |
* connection. |
| 1936 |
* |
| 1937 |
* missing_ok - if true then only issue a LOG message if the slot doesn't |
| 1938 |
* exist. |
| 1939 |
*/ |
| 1940 |
void |
| 1941 |
ReplicationSlotDropAtPubNode(WalReceiverConn *wrconn, char *slotname, bool missing_ok) |
| 1942 |
{ |
| 1943 |
StringInfoData cmd; |
| 1944 |
|
| 1945 |
Assert(wrconn); |
| 1946 |
|
| 1947 |
load_file("libpqwalreceiver", false); |
| 1948 |
|
| 1949 |
initStringInfo(&cmd); |
| 1950 |
appendStringInfoString(&cmd, "DROP_REPLICATION_SLOT "); |
| 1951 |
appendQuotedIdentifier(&cmd, slotname); |
| 1952 |
appendStringInfoString(&cmd, " WAIT"); |
| 1953 |
|
| 1954 |
PG_TRY(); |
| 1955 |
{ |
| 1956 |
WalRcvExecResult *res; |
| 1957 |
|
| 1958 |
res = walrcv_exec(wrconn, cmd.data, 0, NULL); |
| 1959 |
|
| 1960 |
if (res->status == WALRCV_OK_COMMAND) |
| 1961 |
{ |
| 1962 |
/* NOTICE. Success. */ |
| 1963 |
ereport(NOTICE, |
| 1964 |
(errmsg("dropped replication slot \"%s\" on publisher", |
| 1965 |
slotname))); |
| 1966 |
} |
| 1967 |
else if (res->status == WALRCV_ERROR && |
| 1968 |
missing_ok && |
| 1969 |
res->sqlstate == ERRCODE_UNDEFINED_OBJECT) |
| 1970 |
{ |
| 1971 |
/* LOG. Error, but missing_ok = true. */ |
| 1972 |
ereport(LOG, |
| 1973 |
(errmsg("could not drop replication slot \"%s\" on publisher: %s", |
| 1974 |
slotname, res->err))); |
| 1975 |
} |
| 1976 |
else |
| 1977 |
{ |
| 1978 |
/* ERROR. */ |
| 1979 |
ereport(ERROR, |
| 1980 |
(errcode(ERRCODE_CONNECTION_FAILURE), |
| 1981 |
errmsg("could not drop replication slot \"%s\" on publisher: %s", |
| 1982 |
slotname, res->err))); |
| 1983 |
} |
| 1984 |
|
| 1985 |
walrcv_clear_result(res); |
| 1986 |
} |
| 1987 |
PG_FINALLY(); |
| 1988 |
{ |
| 1989 |
pfree(cmd.data); |
| 1990 |
} |
| 1991 |
PG_END_TRY(); |
| 1992 |
} |
| 1993 |
|
| 1994 |
/* |
| 1995 |
* Internal workhorse for changing a subscription owner |
| 1996 |
*/ |
| 1997 |
static void |
| 1998 |
AlterSubscriptionOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId) |
| 1999 |
{ |
| 2000 |
Form_pg_subscription form; |
| 2001 |
AclResult aclresult; |
| 2002 |
|
| 2003 |
form = (Form_pg_subscription) GETSTRUCT(tup); |
| 2004 |
|
| 2005 |
if (form->subowner == newOwnerId) |
| 2006 |
return; |
| 2007 |
|
| 2008 |
if (!object_ownercheck(SubscriptionRelationId, form->oid, GetUserId())) |
| 2009 |
aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_SUBSCRIPTION, |
| 2010 |
NameStr(form->subname)); |
| 2011 |
|
| 2012 |
/* |
| 2013 |
* Don't allow non-superuser modification of a subscription with |
| 2014 |
* password_required=false. |
| 2015 |
*/ |
| 2016 |
if (!form->subpasswordrequired && !superuser()) |
| 2017 |
ereport(ERROR, |
| 2018 |
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 2019 |
errmsg("password_required=false is superuser-only"), |
| 2020 |
errhint("Subscriptions with the password_required option set to false may only be created or modified by the superuser."))); |
| 2021 |
|
| 2022 |
/* Must be able to become new owner */ |
| 2023 |
check_can_set_role(GetUserId(), newOwnerId); |
| 2024 |
|
| 2025 |
/* |
| 2026 |
* current owner must have CREATE on database |
| 2027 |
* |
| 2028 |
* This is consistent with how ALTER SCHEMA ... OWNER TO works, but some |
| 2029 |
* other object types behave differently (e.g. you can't give a table to a |
| 2030 |
* user who lacks CREATE privileges on a schema). |
| 2031 |
*/ |
| 2032 |
aclresult = object_aclcheck(DatabaseRelationId, MyDatabaseId, |
| 2033 |
GetUserId(), ACL_CREATE); |
| 2034 |
if (aclresult != ACLCHECK_OK) |
| 2035 |
aclcheck_error(aclresult, OBJECT_DATABASE, |
| 2036 |
get_database_name(MyDatabaseId)); |
| 2037 |
|
| 2038 |
form->subowner = newOwnerId; |
| 2039 |
CatalogTupleUpdate(rel, &tup->t_self, tup); |
| 2040 |
|
| 2041 |
/* Update owner dependency reference */ |
| 2042 |
changeDependencyOnOwner(SubscriptionRelationId, |
| 2043 |
form->oid, |
| 2044 |
newOwnerId); |
| 2045 |
|
| 2046 |
InvokeObjectPostAlterHook(SubscriptionRelationId, |
| 2047 |
form->oid, 0); |
| 2048 |
|
| 2049 |
/* Wake up related background processes to handle this change quickly. */ |
| 2050 |
ApplyLauncherWakeupAtCommit(); |
| 2051 |
LogicalRepWorkersWakeupAtCommit(form->oid); |
| 2052 |
} |
| 2053 |
|
| 2054 |
/* |
| 2055 |
* Change subscription owner -- by name |
| 2056 |
*/ |
| 2057 |
ObjectAddress |
| 2058 |
AlterSubscriptionOwner(const char *name, Oid newOwnerId) |
| 2059 |
{ |
| 2060 |
Oid subid; |
| 2061 |
HeapTuple tup; |
| 2062 |
Relation rel; |
| 2063 |
ObjectAddress address; |
| 2064 |
Form_pg_subscription form; |
| 2065 |
|
| 2066 |
rel = table_open(SubscriptionRelationId, RowExclusiveLock); |
| 2067 |
|
| 2068 |
tup = SearchSysCacheCopy2(SUBSCRIPTIONNAME, MyDatabaseId, |
| 2069 |
CStringGetDatum(name)); |
| 2070 |
|
| 2071 |
if (!HeapTupleIsValid(tup)) |
| 2072 |
ereport(ERROR, |
| 2073 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 2074 |
errmsg("subscription \"%s\" does not exist", name))); |
| 2075 |
|
| 2076 |
form = (Form_pg_subscription) GETSTRUCT(tup); |
| 2077 |
subid = form->oid; |
| 2078 |
|
| 2079 |
AlterSubscriptionOwner_internal(rel, tup, newOwnerId); |
| 2080 |
|
| 2081 |
ObjectAddressSet(address, SubscriptionRelationId, subid); |
| 2082 |
|
| 2083 |
heap_freetuple(tup); |
| 2084 |
|
| 2085 |
table_close(rel, RowExclusiveLock); |
| 2086 |
|
| 2087 |
return address; |
| 2088 |
} |
| 2089 |
|
| 2090 |
/* |
| 2091 |
* Change subscription owner -- by OID |
| 2092 |
*/ |
| 2093 |
void |
| 2094 |
AlterSubscriptionOwner_oid(Oid subid, Oid newOwnerId) |
| 2095 |
{ |
| 2096 |
HeapTuple tup; |
| 2097 |
Relation rel; |
| 2098 |
|
| 2099 |
rel = table_open(SubscriptionRelationId, RowExclusiveLock); |
| 2100 |
|
| 2101 |
tup = SearchSysCacheCopy1(SUBSCRIPTIONOID, ObjectIdGetDatum(subid)); |
| 2102 |
|
| 2103 |
if (!HeapTupleIsValid(tup)) |
| 2104 |
ereport(ERROR, |
| 2105 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 2106 |
errmsg("subscription with OID %u does not exist", subid))); |
| 2107 |
|
| 2108 |
AlterSubscriptionOwner_internal(rel, tup, newOwnerId); |
| 2109 |
|
| 2110 |
heap_freetuple(tup); |
| 2111 |
|
| 2112 |
table_close(rel, RowExclusiveLock); |
| 2113 |
} |
| 2114 |
|
| 2115 |
/* |
| 2116 |
* Check and log a warning if the publisher has subscribed to the same table, |
| 2117 |
* its partition ancestors (if it's a partition), or its partition children (if |
| 2118 |
* it's a partitioned table), from some other publishers. This check is |
| 2119 |
* required only if "copy_data = true" and "origin = none" for CREATE |
| 2120 |
* SUBSCRIPTION and ALTER SUBSCRIPTION ... REFRESH statements to notify the |
| 2121 |
* user that data having origin might have been copied. |
| 2122 |
* |
| 2123 |
* This check need not be performed on the tables that are already added |
| 2124 |
* because incremental sync for those tables will happen through WAL and the |
| 2125 |
* origin of the data can be identified from the WAL records. |
| 2126 |
* |
| 2127 |
* subrel_local_oids contains the list of relation oids that are already |
| 2128 |
* present on the subscriber. |
| 2129 |
*/ |
| 2130 |
static void |
| 2131 |
check_publications_origin(WalReceiverConn *wrconn, List *publications, |
| 2132 |
bool copydata, char *origin, Oid *subrel_local_oids, |
| 2133 |
int subrel_count, char *subname) |
| 2134 |
{ |
| 2135 |
WalRcvExecResult *res; |
| 2136 |
StringInfoData cmd; |
| 2137 |
TupleTableSlot *slot; |
| 2138 |
Oid tableRow[1] = {TEXTOID}; |
| 2139 |
List *publist = NIL; |
| 2140 |
int i; |
| 2141 |
|
| 2142 |
if (!copydata || !origin || |
| 2143 |
(pg_strcasecmp(origin, LOGICALREP_ORIGIN_NONE) != 0)) |
| 2144 |
return; |
| 2145 |
|
| 2146 |
initStringInfo(&cmd); |
| 2147 |
appendStringInfoString(&cmd, |
| 2148 |
"SELECT DISTINCT P.pubname AS pubname\n" |
| 2149 |
"FROM pg_publication P,\n" |
| 2150 |
" LATERAL pg_get_publication_tables(P.pubname) GPT\n" |
| 2151 |
" JOIN pg_subscription_rel PS ON (GPT.relid = PS.srrelid OR" |
| 2152 |
" GPT.relid IN (SELECT relid FROM pg_partition_ancestors(PS.srrelid) UNION" |
| 2153 |
" SELECT relid FROM pg_partition_tree(PS.srrelid))),\n" |
| 2154 |
" pg_class C JOIN pg_namespace N ON (N.oid = C.relnamespace)\n" |
| 2155 |
"WHERE C.oid = GPT.relid AND P.pubname IN ("); |
| 2156 |
GetPublicationsStr(publications, &cmd, true); |
| 2157 |
appendStringInfoString(&cmd, ")\n"); |
| 2158 |
|
| 2159 |
/* |
| 2160 |
* In case of ALTER SUBSCRIPTION ... REFRESH, subrel_local_oids contains |
| 2161 |
* the list of relation oids that are already present on the subscriber. |
| 2162 |
* This check should be skipped for these tables. |
| 2163 |
*/ |
| 2164 |
for (i = 0; i < subrel_count; i++) |
| 2165 |
{ |
| 2166 |
Oid relid = subrel_local_oids[i]; |
| 2167 |
char *schemaname = get_namespace_name(get_rel_namespace(relid)); |
| 2168 |
char *tablename = get_rel_name(relid); |
| 2169 |
char *schemaname_lit = quote_literal_cstr(schemaname); |
| 2170 |
char *tablename_lit = quote_literal_cstr(tablename); |
| 2171 |
|
| 2172 |
appendStringInfo(&cmd, "AND NOT (N.nspname = %s AND C.relname = %s)\n", |
| 2173 |
schemaname_lit, tablename_lit); |
| 2174 |
|
| 2175 |
pfree(schemaname_lit); |
| 2176 |
pfree(tablename_lit); |
| 2177 |
} |
| 2178 |
|
| 2179 |
res = walrcv_exec(wrconn, cmd.data, 1, tableRow); |
| 2180 |
pfree(cmd.data); |
| 2181 |
|
| 2182 |
if (res->status != WALRCV_OK_TUPLES) |
| 2183 |
ereport(ERROR, |
| 2184 |
(errcode(ERRCODE_CONNECTION_FAILURE), |
| 2185 |
errmsg("could not receive list of replicated tables from the publisher: %s", |
| 2186 |
res->err))); |
| 2187 |
|
| 2188 |
/* Process tables. */ |
| 2189 |
slot = MakeSingleTupleTableSlot(res->tupledesc, &TTSOpsMinimalTuple); |
| 2190 |
while (tuplestore_gettupleslot(res->tuplestore, true, false, slot)) |
| 2191 |
{ |
| 2192 |
char *pubname; |
| 2193 |
bool isnull; |
| 2194 |
|
| 2195 |
pubname = TextDatumGetCString(slot_getattr(slot, 1, &isnull)); |
| 2196 |
Assert(!isnull); |
| 2197 |
|
| 2198 |
ExecClearTuple(slot); |
| 2199 |
publist = list_append_unique(publist, makeString(pubname)); |
| 2200 |
} |
| 2201 |
|
| 2202 |
/* |
| 2203 |
* Log a warning if the publisher has subscribed to the same table from |
| 2204 |
* some other publisher. We cannot know the origin of data during the |
| 2205 |
* initial sync. Data origins can be found only from the WAL by looking at |
| 2206 |
* the origin id. |
| 2207 |
* |
| 2208 |
* XXX: For simplicity, we don't check whether the table has any data or |
| 2209 |
* not. If the table doesn't have any data then we don't need to |
| 2210 |
* distinguish between data having origin and data not having origin so we |
| 2211 |
* can avoid logging a warning in that case. |
| 2212 |
*/ |
| 2213 |
if (publist) |
| 2214 |
{ |
| 2215 |
StringInfo pubnames = makeStringInfo(); |
| 2216 |
|
| 2217 |
/* Prepare the list of publication(s) for warning message. */ |
| 2218 |
GetPublicationsStr(publist, pubnames, false); |
| 2219 |
ereport(WARNING, |
| 2220 |
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 2221 |
errmsg("subscription \"%s\" requested copy_data with origin = NONE but might copy data that had a different origin", |
| 2222 |
subname), |
| 2223 |
errdetail_plural("The subscription being created subscribes to a publication (%s) that contains tables that are written to by other subscriptions.", |
| 2224 |
"The subscription being created subscribes to publications (%s) that contain tables that are written to by other subscriptions.", |
| 2225 |
list_length(publist), pubnames->data), |
| 2226 |
errhint("Verify that initial data copied from the publisher tables did not come from other origins.")); |
| 2227 |
} |
| 2228 |
|
| 2229 |
ExecDropSingleTupleTableSlot(slot); |
| 2230 |
|
| 2231 |
walrcv_clear_result(res); |
| 2232 |
} |
| 2233 |
|
| 2234 |
/* |
| 2235 |
* Get the list of tables which belong to specified publications on the |
| 2236 |
* publisher connection. |
| 2237 |
* |
| 2238 |
* Note that we don't support the case where the column list is different for |
| 2239 |
* the same table in different publications to avoid sending unwanted column |
| 2240 |
* information for some of the rows. This can happen when both the column |
| 2241 |
* list and row filter are specified for different publications. |
| 2242 |
*/ |
| 2243 |
static List * |
| 2244 |
fetch_table_list(WalReceiverConn *wrconn, List *publications) |
| 2245 |
{ |
| 2246 |
WalRcvExecResult *res; |
| 2247 |
StringInfoData cmd; |
| 2248 |
TupleTableSlot *slot; |
| 2249 |
Oid tableRow[3] = {TEXTOID, TEXTOID, InvalidOid}; |
| 2250 |
List *tablelist = NIL; |
| 2251 |
int server_version = walrcv_server_version(wrconn); |
| 2252 |
bool check_columnlist = (server_version >= 150000); |
| 2253 |
StringInfo pub_names = makeStringInfo(); |
| 2254 |
|
| 2255 |
initStringInfo(&cmd); |
| 2256 |
|
| 2257 |
/* Build the pub_names comma-separated string. */ |
| 2258 |
GetPublicationsStr(publications, pub_names, true); |
| 2259 |
|
| 2260 |
/* Get the list of tables from the publisher. */ |
| 2261 |
if (server_version >= 160000) |
| 2262 |
{ |
| 2263 |
tableRow[2] = INT2VECTOROID; |
| 2264 |
|
| 2265 |
/* |
| 2266 |
* From version 16, we allowed passing multiple publications to the |
| 2267 |
* function pg_get_publication_tables. This helped to filter out the |
| 2268 |
* partition table whose ancestor is also published in this |
| 2269 |
* publication array. |
| 2270 |
* |
| 2271 |
* Join pg_get_publication_tables with pg_publication to exclude |
| 2272 |
* non-existing publications. |
| 2273 |
* |
| 2274 |
* Note that attrs are always stored in sorted order so we don't need |
| 2275 |
* to worry if different publications have specified them in a |
| 2276 |
* different order. See pub_collist_validate. |
| 2277 |
*/ |
| 2278 |
appendStringInfo(&cmd, "SELECT DISTINCT n.nspname, c.relname, gpt.attrs\n" |
| 2279 |
" FROM pg_class c\n" |
| 2280 |
" JOIN pg_namespace n ON n.oid = c.relnamespace\n" |
| 2281 |
" JOIN ( SELECT (pg_get_publication_tables(VARIADIC array_agg(pubname::text))).*\n" |
| 2282 |
" FROM pg_publication\n" |
| 2283 |
" WHERE pubname IN ( %s )) AS gpt\n" |
| 2284 |
" ON gpt.relid = c.oid\n", |
| 2285 |
pub_names->data); |
| 2286 |
} |
| 2287 |
else |
| 2288 |
{ |
| 2289 |
tableRow[2] = NAMEARRAYOID; |
| 2290 |
appendStringInfoString(&cmd, "SELECT DISTINCT t.schemaname, t.tablename \n"); |
| 2291 |
|
| 2292 |
/* Get column lists for each relation if the publisher supports it */ |
| 2293 |
if (check_columnlist) |
| 2294 |
appendStringInfoString(&cmd, ", t.attnames\n"); |
| 2295 |
|
| 2296 |
appendStringInfo(&cmd, "FROM pg_catalog.pg_publication_tables t\n" |
| 2297 |
" WHERE t.pubname IN ( %s )", |
| 2298 |
pub_names->data); |
| 2299 |
} |
| 2300 |
|
| 2301 |
destroyStringInfo(pub_names); |
| 2302 |
|
| 2303 |
res = walrcv_exec(wrconn, cmd.data, check_columnlist ? 3 : 2, tableRow); |
| 2304 |
pfree(cmd.data); |
| 2305 |
|
| 2306 |
if (res->status != WALRCV_OK_TUPLES) |
| 2307 |
ereport(ERROR, |
| 2308 |
(errcode(ERRCODE_CONNECTION_FAILURE), |
| 2309 |
errmsg("could not receive list of replicated tables from the publisher: %s", |
| 2310 |
res->err))); |
| 2311 |
|
| 2312 |
/* Process tables. */ |
| 2313 |
slot = MakeSingleTupleTableSlot(res->tupledesc, &TTSOpsMinimalTuple); |
| 2314 |
while (tuplestore_gettupleslot(res->tuplestore, true, false, slot)) |
| 2315 |
{ |
| 2316 |
char *nspname; |
| 2317 |
char *relname; |
| 2318 |
bool isnull; |
| 2319 |
RangeVar *rv; |
| 2320 |
|
| 2321 |
nspname = TextDatumGetCString(slot_getattr(slot, 1, &isnull)); |
| 2322 |
Assert(!isnull); |
| 2323 |
relname = TextDatumGetCString(slot_getattr(slot, 2, &isnull)); |
| 2324 |
Assert(!isnull); |
| 2325 |
|
| 2326 |
rv = makeRangeVar(nspname, relname, -1); |
| 2327 |
|
| 2328 |
if (check_columnlist && list_member(tablelist, rv)) |
| 2329 |
ereport(ERROR, |
| 2330 |
errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 2331 |
errmsg("cannot use different column lists for table \"%s.%s\" in different publications", |
| 2332 |
nspname, relname)); |
| 2333 |
else |
| 2334 |
tablelist = lappend(tablelist, rv); |
| 2335 |
|
| 2336 |
ExecClearTuple(slot); |
| 2337 |
} |
| 2338 |
ExecDropSingleTupleTableSlot(slot); |
| 2339 |
|
| 2340 |
walrcv_clear_result(res); |
| 2341 |
|
| 2342 |
return tablelist; |
| 2343 |
} |
| 2344 |
|
| 2345 |
/* |
| 2346 |
* This is to report the connection failure while dropping replication slots. |
| 2347 |
* Here, we report the WARNING for all tablesync slots so that user can drop |
| 2348 |
* them manually, if required. |
| 2349 |
*/ |
| 2350 |
static void |
| 2351 |
ReportSlotConnectionError(List *rstates, Oid subid, char *slotname, char *err) |
| 2352 |
{ |
| 2353 |
ListCell *lc; |
| 2354 |
|
| 2355 |
foreach(lc, rstates) |
| 2356 |
{ |
| 2357 |
SubscriptionRelState *rstate = (SubscriptionRelState *) lfirst(lc); |
| 2358 |
Oid relid = rstate->relid; |
| 2359 |
|
| 2360 |
/* Only cleanup resources of tablesync workers */ |
| 2361 |
if (!OidIsValid(relid)) |
| 2362 |
continue; |
| 2363 |
|
| 2364 |
/* |
| 2365 |
* Caller needs to ensure that relstate doesn't change underneath us. |
| 2366 |
* See DropSubscription where we get the relstates. |
| 2367 |
*/ |
| 2368 |
if (rstate->state != SUBREL_STATE_SYNCDONE) |
| 2369 |
{ |
| 2370 |
char syncslotname[NAMEDATALEN] = {0}; |
| 2371 |
|
| 2372 |
ReplicationSlotNameForTablesync(subid, relid, syncslotname, |
| 2373 |
sizeof(syncslotname)); |
| 2374 |
elog(WARNING, "could not drop tablesync replication slot \"%s\"", |
| 2375 |
syncslotname); |
| 2376 |
} |
| 2377 |
} |
| 2378 |
|
| 2379 |
ereport(ERROR, |
| 2380 |
(errcode(ERRCODE_CONNECTION_FAILURE), |
| 2381 |
errmsg("could not connect to publisher when attempting to drop replication slot \"%s\": %s", |
| 2382 |
slotname, err), |
| 2383 |
/* translator: %s is an SQL ALTER command */ |
| 2384 |
errhint("Use %s to disable the subscription, and then use %s to disassociate it from the slot.", |
| 2385 |
"ALTER SUBSCRIPTION ... DISABLE", |
| 2386 |
"ALTER SUBSCRIPTION ... SET (slot_name = NONE)"))); |
| 2387 |
} |
| 2388 |
|
| 2389 |
/* |
| 2390 |
* Check for duplicates in the given list of publications and error out if |
| 2391 |
* found one. Add publications to datums as text datums, if datums is not |
| 2392 |
* NULL. |
| 2393 |
*/ |
| 2394 |
static void |
| 2395 |
check_duplicates_in_publist(List *publist, Datum *datums) |
| 2396 |
{ |
| 2397 |
ListCell *cell; |
| 2398 |
int j = 0; |
| 2399 |
|
| 2400 |
foreach(cell, publist) |
| 2401 |
{ |
| 2402 |
char *name = strVal(lfirst(cell)); |
| 2403 |
ListCell *pcell; |
| 2404 |
|
| 2405 |
foreach(pcell, publist) |
| 2406 |
{ |
| 2407 |
char *pname = strVal(lfirst(pcell)); |
| 2408 |
|
| 2409 |
if (pcell == cell) |
| 2410 |
break; |
| 2411 |
|
| 2412 |
if (strcmp(name, pname) == 0) |
| 2413 |
ereport(ERROR, |
| 2414 |
(errcode(ERRCODE_DUPLICATE_OBJECT), |
| 2415 |
errmsg("publication name \"%s\" used more than once", |
| 2416 |
pname))); |
| 2417 |
} |
| 2418 |
|
| 2419 |
if (datums) |
| 2420 |
datums[j++] = CStringGetTextDatum(name); |
| 2421 |
} |
| 2422 |
} |
| 2423 |
|
| 2424 |
/* |
| 2425 |
* Merge current subscription's publications and user-specified publications |
| 2426 |
* from ADD/DROP PUBLICATIONS. |
| 2427 |
* |
| 2428 |
* If addpub is true, we will add the list of publications into oldpublist. |
| 2429 |
* Otherwise, we will delete the list of publications from oldpublist. The |
| 2430 |
* returned list is a copy, oldpublist itself is not changed. |
| 2431 |
* |
| 2432 |
* subname is the subscription name, for error messages. |
| 2433 |
*/ |
| 2434 |
static List * |
| 2435 |
merge_publications(List *oldpublist, List *newpublist, bool addpub, const char *subname) |
| 2436 |
{ |
| 2437 |
ListCell *lc; |
| 2438 |
|
| 2439 |
oldpublist = list_copy(oldpublist); |
| 2440 |
|
| 2441 |
check_duplicates_in_publist(newpublist, NULL); |
| 2442 |
|
| 2443 |
foreach(lc, newpublist) |
| 2444 |
{ |
| 2445 |
char *name = strVal(lfirst(lc)); |
| 2446 |
ListCell *lc2; |
| 2447 |
bool found = false; |
| 2448 |
|
| 2449 |
foreach(lc2, oldpublist) |
| 2450 |
{ |
| 2451 |
char *pubname = strVal(lfirst(lc2)); |
| 2452 |
|
| 2453 |
if (strcmp(name, pubname) == 0) |
| 2454 |
{ |
| 2455 |
found = true; |
| 2456 |
if (addpub) |
| 2457 |
ereport(ERROR, |
| 2458 |
(errcode(ERRCODE_DUPLICATE_OBJECT), |
| 2459 |
errmsg("publication \"%s\" is already in subscription \"%s\"", |
| 2460 |
name, subname))); |
| 2461 |
else |
| 2462 |
oldpublist = foreach_delete_current(oldpublist, lc2); |
| 2463 |
|
| 2464 |
break; |
| 2465 |
} |
| 2466 |
} |
| 2467 |
|
| 2468 |
if (addpub && !found) |
| 2469 |
oldpublist = lappend(oldpublist, makeString(name)); |
| 2470 |
else if (!addpub && !found) |
| 2471 |
ereport(ERROR, |
| 2472 |
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION), |
| 2473 |
errmsg("publication \"%s\" is not in subscription \"%s\"", |
| 2474 |
name, subname))); |
| 2475 |
} |
| 2476 |
|
| 2477 |
/* |
| 2478 |
* XXX Probably no strong reason for this, but for now it's to make ALTER |
| 2479 |
* SUBSCRIPTION ... DROP PUBLICATION consistent with SET PUBLICATION. |
| 2480 |
*/ |
| 2481 |
if (!oldpublist) |
| 2482 |
ereport(ERROR, |
| 2483 |
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION), |
| 2484 |
errmsg("cannot drop all the publications from a subscription"))); |
| 2485 |
|
| 2486 |
return oldpublist; |
| 2487 |
} |
| 2488 |
|
| 2489 |
/* |
| 2490 |
* Extract the streaming mode value from a DefElem. This is like |
| 2491 |
* defGetBoolean() but also accepts the special value of "parallel". |
| 2492 |
*/ |
| 2493 |
char |
| 2494 |
defGetStreamingMode(DefElem *def) |
| 2495 |
{ |
| 2496 |
/* |
| 2497 |
* If no parameter value given, assume "true" is meant. |
| 2498 |
*/ |
| 2499 |
if (!def->arg) |
| 2500 |
return LOGICALREP_STREAM_ON; |
| 2501 |
|
| 2502 |
/* |
| 2503 |
* Allow 0, 1, "false", "true", "off", "on" or "parallel". |
| 2504 |
*/ |
| 2505 |
switch (nodeTag(def->arg)) |
| 2506 |
{ |
| 2507 |
case T_Integer: |
| 2508 |
switch (intVal(def->arg)) |
| 2509 |
{ |
| 2510 |
case 0: |
| 2511 |
return LOGICALREP_STREAM_OFF; |
| 2512 |
case 1: |
| 2513 |
return LOGICALREP_STREAM_ON; |
| 2514 |
default: |
| 2515 |
/* otherwise, error out below */ |
| 2516 |
break; |
| 2517 |
} |
| 2518 |
break; |
| 2519 |
default: |
| 2520 |
{ |
| 2521 |
char *sval = defGetString(def); |
| 2522 |
|
| 2523 |
/* |
| 2524 |
* The set of strings accepted here should match up with the |
| 2525 |
* grammar's opt_boolean_or_string production. |
| 2526 |
*/ |
| 2527 |
if (pg_strcasecmp(sval, "false") == 0 || |
| 2528 |
pg_strcasecmp(sval, "off") == 0) |
| 2529 |
return LOGICALREP_STREAM_OFF; |
| 2530 |
if (pg_strcasecmp(sval, "true") == 0 || |
| 2531 |
pg_strcasecmp(sval, "on") == 0) |
| 2532 |
return LOGICALREP_STREAM_ON; |
| 2533 |
if (pg_strcasecmp(sval, "parallel") == 0) |
| 2534 |
return LOGICALREP_STREAM_PARALLEL; |
| 2535 |
} |
| 2536 |
break; |
| 2537 |
} |
| 2538 |
|
| 2539 |
ereport(ERROR, |
| 2540 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 2541 |
errmsg("%s requires a Boolean value or \"parallel\"", |
| 2542 |
def->defname))); |
| 2543 |
return LOGICALREP_STREAM_OFF; /* keep compiler quiet */ |
| 2544 |
} |