| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* publicationcmds.c |
| 4 |
* publication manipulation |
| 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/publicationcmds.c |
| 11 |
* |
| 12 |
*------------------------------------------------------------------------- |
| 13 |
*/ |
| 14 |
|
| 15 |
#include "postgres.h" |
| 16 |
|
| 17 |
#include "access/htup_details.h" |
| 18 |
#include "access/table.h" |
| 19 |
#include "access/xact.h" |
| 20 |
#include "catalog/catalog.h" |
| 21 |
#include "catalog/indexing.h" |
| 22 |
#include "catalog/namespace.h" |
| 23 |
#include "catalog/objectaccess.h" |
| 24 |
#include "catalog/objectaddress.h" |
| 25 |
#include "catalog/pg_database.h" |
| 26 |
#include "catalog/pg_inherits.h" |
| 27 |
#include "catalog/pg_namespace.h" |
| 28 |
#include "catalog/pg_proc.h" |
| 29 |
#include "catalog/pg_publication.h" |
| 30 |
#include "catalog/pg_publication_namespace.h" |
| 31 |
#include "catalog/pg_publication_rel.h" |
| 32 |
#include "commands/dbcommands.h" |
| 33 |
#include "commands/defrem.h" |
| 34 |
#include "commands/event_trigger.h" |
| 35 |
#include "commands/publicationcmds.h" |
| 36 |
#include "miscadmin.h" |
| 37 |
#include "nodes/nodeFuncs.h" |
| 38 |
#include "parser/parse_clause.h" |
| 39 |
#include "parser/parse_collate.h" |
| 40 |
#include "parser/parse_relation.h" |
| 41 |
#include "rewrite/rewriteHandler.h" |
| 42 |
#include "storage/lmgr.h" |
| 43 |
#include "utils/acl.h" |
| 44 |
#include "utils/builtins.h" |
| 45 |
#include "utils/inval.h" |
| 46 |
#include "utils/lsyscache.h" |
| 47 |
#include "utils/rel.h" |
| 48 |
#include "utils/syscache.h" |
| 49 |
#include "utils/varlena.h" |
| 50 |
|
| 51 |
|
| 52 |
/* |
| 53 |
* Information used to validate the columns in the row filter expression. See |
| 54 |
* contain_invalid_rfcolumn_walker for details. |
| 55 |
*/ |
| 56 |
typedef struct rf_context |
| 57 |
{ |
| 58 |
Bitmapset *bms_replident; /* bitset of replica identity columns */ |
| 59 |
bool pubviaroot; /* true if we are validating the parent |
| 60 |
* relation's row filter */ |
| 61 |
Oid relid; /* relid of the relation */ |
| 62 |
Oid parentid; /* relid of the parent relation */ |
| 63 |
} rf_context; |
| 64 |
|
| 65 |
static List *OpenTableList(List *tables); |
| 66 |
static void CloseTableList(List *rels); |
| 67 |
static void LockSchemaList(List *schemalist); |
| 68 |
static void PublicationAddTables(Oid pubid, List *rels, bool if_not_exists, |
| 69 |
AlterPublicationStmt *stmt); |
| 70 |
static void PublicationDropTables(Oid pubid, List *rels, bool missing_ok); |
| 71 |
static void PublicationAddSchemas(Oid pubid, List *schemas, bool if_not_exists, |
| 72 |
AlterPublicationStmt *stmt); |
| 73 |
static void PublicationDropSchemas(Oid pubid, List *schemas, bool missing_ok); |
| 74 |
static char defGetGeneratedColsOption(DefElem *def); |
| 75 |
|
| 76 |
|
| 77 |
static void |
| 78 |
parse_publication_options(ParseState *pstate, |
| 79 |
List *options, |
| 80 |
bool *publish_given, |
| 81 |
PublicationActions *pubactions, |
| 82 |
bool *publish_via_partition_root_given, |
| 83 |
bool *publish_via_partition_root, |
| 84 |
bool *publish_generated_columns_given, |
| 85 |
char *publish_generated_columns) |
| 86 |
{ |
| 87 |
ListCell *lc; |
| 88 |
|
| 89 |
*publish_given = false; |
| 90 |
*publish_via_partition_root_given = false; |
| 91 |
*publish_generated_columns_given = false; |
| 92 |
|
| 93 |
/* defaults */ |
| 94 |
pubactions->pubinsert = true; |
| 95 |
pubactions->pubupdate = true; |
| 96 |
pubactions->pubdelete = true; |
| 97 |
pubactions->pubtruncate = true; |
| 98 |
*publish_via_partition_root = false; |
| 99 |
*publish_generated_columns = PUBLISH_GENCOLS_NONE; |
| 100 |
|
| 101 |
/* Parse options */ |
| 102 |
foreach(lc, options) |
| 103 |
{ |
| 104 |
DefElem *defel = (DefElem *) lfirst(lc); |
| 105 |
|
| 106 |
if (strcmp(defel->defname, "publish") == 0) |
| 107 |
{ |
| 108 |
char *publish; |
| 109 |
List *publish_list; |
| 110 |
ListCell *lc2; |
| 111 |
|
| 112 |
if (*publish_given) |
| 113 |
errorConflictingDefElem(defel, pstate); |
| 114 |
|
| 115 |
/* |
| 116 |
* If publish option was given only the explicitly listed actions |
| 117 |
* should be published. |
| 118 |
*/ |
| 119 |
pubactions->pubinsert = false; |
| 120 |
pubactions->pubupdate = false; |
| 121 |
pubactions->pubdelete = false; |
| 122 |
pubactions->pubtruncate = false; |
| 123 |
|
| 124 |
*publish_given = true; |
| 125 |
|
| 126 |
/* |
| 127 |
* SplitIdentifierString destructively modifies its input, so make |
| 128 |
* a copy so we don't modify the memory of the executing statement |
| 129 |
*/ |
| 130 |
publish = pstrdup(defGetString(defel)); |
| 131 |
|
| 132 |
if (!SplitIdentifierString(publish, ',', &publish_list)) |
| 133 |
ereport(ERROR, |
| 134 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 135 |
errmsg("invalid list syntax in parameter \"%s\"", |
| 136 |
"publish"))); |
| 137 |
|
| 138 |
/* Process the option list. */ |
| 139 |
foreach(lc2, publish_list) |
| 140 |
{ |
| 141 |
char *publish_opt = (char *) lfirst(lc2); |
| 142 |
|
| 143 |
if (strcmp(publish_opt, "insert") == 0) |
| 144 |
pubactions->pubinsert = true; |
| 145 |
else if (strcmp(publish_opt, "update") == 0) |
| 146 |
pubactions->pubupdate = true; |
| 147 |
else if (strcmp(publish_opt, "delete") == 0) |
| 148 |
pubactions->pubdelete = true; |
| 149 |
else if (strcmp(publish_opt, "truncate") == 0) |
| 150 |
pubactions->pubtruncate = true; |
| 151 |
else |
| 152 |
ereport(ERROR, |
| 153 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 154 |
errmsg("unrecognized value for publication option \"%s\": \"%s\"", |
| 155 |
"publish", publish_opt))); |
| 156 |
} |
| 157 |
} |
| 158 |
else if (strcmp(defel->defname, "publish_via_partition_root") == 0) |
| 159 |
{ |
| 160 |
if (*publish_via_partition_root_given) |
| 161 |
errorConflictingDefElem(defel, pstate); |
| 162 |
*publish_via_partition_root_given = true; |
| 163 |
*publish_via_partition_root = defGetBoolean(defel); |
| 164 |
} |
| 165 |
else if (strcmp(defel->defname, "publish_generated_columns") == 0) |
| 166 |
{ |
| 167 |
if (*publish_generated_columns_given) |
| 168 |
errorConflictingDefElem(defel, pstate); |
| 169 |
*publish_generated_columns_given = true; |
| 170 |
*publish_generated_columns = defGetGeneratedColsOption(defel); |
| 171 |
} |
| 172 |
else |
| 173 |
ereport(ERROR, |
| 174 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 175 |
errmsg("unrecognized publication parameter: \"%s\"", defel->defname))); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
/* |
| 180 |
* Convert the PublicationObjSpecType list into schema oid list and |
| 181 |
* PublicationTable list. |
| 182 |
*/ |
| 183 |
static void |
| 184 |
ObjectsInPublicationToOids(List *pubobjspec_list, ParseState *pstate, |
| 185 |
List **rels, List **schemas) |
| 186 |
{ |
| 187 |
ListCell *cell; |
| 188 |
PublicationObjSpec *pubobj; |
| 189 |
|
| 190 |
if (!pubobjspec_list) |
| 191 |
return; |
| 192 |
|
| 193 |
foreach(cell, pubobjspec_list) |
| 194 |
{ |
| 195 |
Oid schemaid; |
| 196 |
List *search_path; |
| 197 |
|
| 198 |
pubobj = (PublicationObjSpec *) lfirst(cell); |
| 199 |
|
| 200 |
switch (pubobj->pubobjtype) |
| 201 |
{ |
| 202 |
case PUBLICATIONOBJ_TABLE: |
| 203 |
*rels = lappend(*rels, pubobj->pubtable); |
| 204 |
break; |
| 205 |
case PUBLICATIONOBJ_TABLES_IN_SCHEMA: |
| 206 |
schemaid = get_namespace_oid(pubobj->name, false); |
| 207 |
|
| 208 |
/* Filter out duplicates if user specifies "sch1, sch1" */ |
| 209 |
*schemas = list_append_unique_oid(*schemas, schemaid); |
| 210 |
break; |
| 211 |
case PUBLICATIONOBJ_TABLES_IN_CUR_SCHEMA: |
| 212 |
search_path = fetch_search_path(false); |
| 213 |
if (search_path == NIL) /* nothing valid in search_path? */ |
| 214 |
ereport(ERROR, |
| 215 |
errcode(ERRCODE_UNDEFINED_SCHEMA), |
| 216 |
errmsg("no schema has been selected for CURRENT_SCHEMA")); |
| 217 |
|
| 218 |
schemaid = linitial_oid(search_path); |
| 219 |
list_free(search_path); |
| 220 |
|
| 221 |
/* Filter out duplicates if user specifies "sch1, sch1" */ |
| 222 |
*schemas = list_append_unique_oid(*schemas, schemaid); |
| 223 |
break; |
| 224 |
default: |
| 225 |
/* shouldn't happen */ |
| 226 |
elog(ERROR, "invalid publication object type %d", pubobj->pubobjtype); |
| 227 |
break; |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
/* |
| 233 |
* Returns true if any of the columns used in the row filter WHERE expression is |
| 234 |
* not part of REPLICA IDENTITY, false otherwise. |
| 235 |
*/ |
| 236 |
static bool |
| 237 |
contain_invalid_rfcolumn_walker(Node *node, rf_context *context) |
| 238 |
{ |
| 239 |
if (node == NULL) |
| 240 |
return false; |
| 241 |
|
| 242 |
if (IsA(node, Var)) |
| 243 |
{ |
| 244 |
Var *var = (Var *) node; |
| 245 |
AttrNumber attnum = var->varattno; |
| 246 |
|
| 247 |
/* |
| 248 |
* If pubviaroot is true, we are validating the row filter of the |
| 249 |
* parent table, but the bitmap contains the replica identity |
| 250 |
* information of the child table. So, get the column number of the |
| 251 |
* child table as parent and child column order could be different. |
| 252 |
*/ |
| 253 |
if (context->pubviaroot) |
| 254 |
{ |
| 255 |
char *colname = get_attname(context->parentid, attnum, false); |
| 256 |
|
| 257 |
attnum = get_attnum(context->relid, colname); |
| 258 |
} |
| 259 |
|
| 260 |
if (!bms_is_member(attnum - FirstLowInvalidHeapAttributeNumber, |
| 261 |
context->bms_replident)) |
| 262 |
return true; |
| 263 |
} |
| 264 |
|
| 265 |
return expression_tree_walker(node, contain_invalid_rfcolumn_walker, |
| 266 |
context); |
| 267 |
} |
| 268 |
|
| 269 |
/* |
| 270 |
* Check if all columns referenced in the filter expression are part of the |
| 271 |
* REPLICA IDENTITY index or not. |
| 272 |
* |
| 273 |
* Returns true if any invalid column is found. |
| 274 |
*/ |
| 275 |
bool |
| 276 |
pub_rf_contains_invalid_column(Oid pubid, Relation relation, List *ancestors, |
| 277 |
bool pubviaroot) |
| 278 |
{ |
| 279 |
HeapTuple rftuple; |
| 280 |
Oid relid = RelationGetRelid(relation); |
| 281 |
Oid publish_as_relid = RelationGetRelid(relation); |
| 282 |
bool result = false; |
| 283 |
Datum rfdatum; |
| 284 |
bool rfisnull; |
| 285 |
|
| 286 |
/* |
| 287 |
* FULL means all columns are in the REPLICA IDENTITY, so all columns are |
| 288 |
* allowed in the row filter and we can skip the validation. |
| 289 |
*/ |
| 290 |
if (relation->rd_rel->relreplident == REPLICA_IDENTITY_FULL) |
| 291 |
return false; |
| 292 |
|
| 293 |
/* |
| 294 |
* For a partition, if pubviaroot is true, find the topmost ancestor that |
| 295 |
* is published via this publication as we need to use its row filter |
| 296 |
* expression to filter the partition's changes. |
| 297 |
* |
| 298 |
* Note that even though the row filter used is for an ancestor, the |
| 299 |
* REPLICA IDENTITY used will be for the actual child table. |
| 300 |
*/ |
| 301 |
if (pubviaroot && relation->rd_rel->relispartition) |
| 302 |
{ |
| 303 |
publish_as_relid |
| 304 |
= GetTopMostAncestorInPublication(pubid, ancestors, NULL); |
| 305 |
|
| 306 |
if (!OidIsValid(publish_as_relid)) |
| 307 |
publish_as_relid = relid; |
| 308 |
} |
| 309 |
|
| 310 |
rftuple = SearchSysCache2(PUBLICATIONRELMAP, |
| 311 |
ObjectIdGetDatum(publish_as_relid), |
| 312 |
ObjectIdGetDatum(pubid)); |
| 313 |
|
| 314 |
if (!HeapTupleIsValid(rftuple)) |
| 315 |
return false; |
| 316 |
|
| 317 |
rfdatum = SysCacheGetAttr(PUBLICATIONRELMAP, rftuple, |
| 318 |
Anum_pg_publication_rel_prqual, |
| 319 |
&rfisnull); |
| 320 |
|
| 321 |
if (!rfisnull) |
| 322 |
{ |
| 323 |
rf_context context = {0}; |
| 324 |
Node *rfnode; |
| 325 |
Bitmapset *bms = NULL; |
| 326 |
|
| 327 |
context.pubviaroot = pubviaroot; |
| 328 |
context.parentid = publish_as_relid; |
| 329 |
context.relid = relid; |
| 330 |
|
| 331 |
/* Remember columns that are part of the REPLICA IDENTITY */ |
| 332 |
bms = RelationGetIndexAttrBitmap(relation, |
| 333 |
INDEX_ATTR_BITMAP_IDENTITY_KEY); |
| 334 |
|
| 335 |
context.bms_replident = bms; |
| 336 |
rfnode = stringToNode(TextDatumGetCString(rfdatum)); |
| 337 |
result = contain_invalid_rfcolumn_walker(rfnode, &context); |
| 338 |
} |
| 339 |
|
| 340 |
ReleaseSysCache(rftuple); |
| 341 |
|
| 342 |
return result; |
| 343 |
} |
| 344 |
|
| 345 |
/* |
| 346 |
* Check for invalid columns in the publication table definition. |
| 347 |
* |
| 348 |
* This function evaluates two conditions: |
| 349 |
* |
| 350 |
* 1. Ensures that all columns referenced in the REPLICA IDENTITY are covered |
| 351 |
* by the column list. If any column is missing, *invalid_column_list is set |
| 352 |
* to true. |
| 353 |
* 2. Ensures that all the generated columns referenced in the REPLICA IDENTITY |
| 354 |
* are published, either by being explicitly named in the column list or, if |
| 355 |
* no column list is specified, by setting the option |
| 356 |
* publish_generated_columns to stored. If any unpublished |
| 357 |
* generated column is found, *invalid_gen_col is set to true. |
| 358 |
* |
| 359 |
* Returns true if any of the above conditions are not met. |
| 360 |
*/ |
| 361 |
bool |
| 362 |
pub_contains_invalid_column(Oid pubid, Relation relation, List *ancestors, |
| 363 |
bool pubviaroot, char pubgencols_type, |
| 364 |
bool *invalid_column_list, |
| 365 |
bool *invalid_gen_col) |
| 366 |
{ |
| 367 |
Oid relid = RelationGetRelid(relation); |
| 368 |
Oid publish_as_relid = RelationGetRelid(relation); |
| 369 |
Bitmapset *idattrs; |
| 370 |
Bitmapset *columns = NULL; |
| 371 |
TupleDesc desc = RelationGetDescr(relation); |
| 372 |
Publication *pub; |
| 373 |
int x; |
| 374 |
|
| 375 |
*invalid_column_list = false; |
| 376 |
*invalid_gen_col = false; |
| 377 |
|
| 378 |
/* |
| 379 |
* For a partition, if pubviaroot is true, find the topmost ancestor that |
| 380 |
* is published via this publication as we need to use its column list for |
| 381 |
* the changes. |
| 382 |
* |
| 383 |
* Note that even though the column list used is for an ancestor, the |
| 384 |
* REPLICA IDENTITY used will be for the actual child table. |
| 385 |
*/ |
| 386 |
if (pubviaroot && relation->rd_rel->relispartition) |
| 387 |
{ |
| 388 |
publish_as_relid = GetTopMostAncestorInPublication(pubid, ancestors, NULL); |
| 389 |
|
| 390 |
if (!OidIsValid(publish_as_relid)) |
| 391 |
publish_as_relid = relid; |
| 392 |
} |
| 393 |
|
| 394 |
/* Fetch the column list */ |
| 395 |
pub = GetPublication(pubid); |
| 396 |
check_and_fetch_column_list(pub, publish_as_relid, NULL, &columns); |
| 397 |
|
| 398 |
if (relation->rd_rel->relreplident == REPLICA_IDENTITY_FULL) |
| 399 |
{ |
| 400 |
/* With REPLICA IDENTITY FULL, no column list is allowed. */ |
| 401 |
*invalid_column_list = (columns != NULL); |
| 402 |
|
| 403 |
/* |
| 404 |
* As we don't allow a column list with REPLICA IDENTITY FULL, the |
| 405 |
* publish_generated_columns option must be set to stored if the table |
| 406 |
* has any stored generated columns. |
| 407 |
*/ |
| 408 |
if (pubgencols_type != PUBLISH_GENCOLS_STORED && |
| 409 |
relation->rd_att->constr && |
| 410 |
relation->rd_att->constr->has_generated_stored) |
| 411 |
*invalid_gen_col = true; |
| 412 |
|
| 413 |
/* |
| 414 |
* Virtual generated columns are currently not supported for logical |
| 415 |
* replication at all. |
| 416 |
*/ |
| 417 |
if (relation->rd_att->constr && |
| 418 |
relation->rd_att->constr->has_generated_virtual) |
| 419 |
*invalid_gen_col = true; |
| 420 |
|
| 421 |
if (*invalid_gen_col && *invalid_column_list) |
| 422 |
return true; |
| 423 |
} |
| 424 |
|
| 425 |
/* Remember columns that are part of the REPLICA IDENTITY */ |
| 426 |
idattrs = RelationGetIndexAttrBitmap(relation, |
| 427 |
INDEX_ATTR_BITMAP_IDENTITY_KEY); |
| 428 |
|
| 429 |
/* |
| 430 |
* Attnums in the bitmap returned by RelationGetIndexAttrBitmap are offset |
| 431 |
* (to handle system columns the usual way), while column list does not |
| 432 |
* use offset, so we can't do bms_is_subset(). Instead, we have to loop |
| 433 |
* over the idattrs and check all of them are in the list. |
| 434 |
*/ |
| 435 |
x = -1; |
| 436 |
while ((x = bms_next_member(idattrs, x)) >= 0) |
| 437 |
{ |
| 438 |
AttrNumber attnum = (x + FirstLowInvalidHeapAttributeNumber); |
| 439 |
Form_pg_attribute att = TupleDescAttr(desc, attnum - 1); |
| 440 |
|
| 441 |
if (columns == NULL) |
| 442 |
{ |
| 443 |
/* |
| 444 |
* The publish_generated_columns option must be set to stored if |
| 445 |
* the REPLICA IDENTITY contains any stored generated column. |
| 446 |
*/ |
| 447 |
if (att->attgenerated == ATTRIBUTE_GENERATED_STORED && pubgencols_type != PUBLISH_GENCOLS_STORED) |
| 448 |
{ |
| 449 |
*invalid_gen_col = true; |
| 450 |
break; |
| 451 |
} |
| 452 |
|
| 453 |
/* |
| 454 |
* The equivalent setting for virtual generated columns does not |
| 455 |
* exist yet. |
| 456 |
*/ |
| 457 |
if (att->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL) |
| 458 |
{ |
| 459 |
*invalid_gen_col = true; |
| 460 |
break; |
| 461 |
} |
| 462 |
|
| 463 |
/* Skip validating the column list since it is not defined */ |
| 464 |
continue; |
| 465 |
} |
| 466 |
|
| 467 |
/* |
| 468 |
* If pubviaroot is true, we are validating the column list of the |
| 469 |
* parent table, but the bitmap contains the replica identity |
| 470 |
* information of the child table. The parent/child attnums may not |
| 471 |
* match, so translate them to the parent - get the attname from the |
| 472 |
* child, and look it up in the parent. |
| 473 |
*/ |
| 474 |
if (pubviaroot) |
| 475 |
{ |
| 476 |
/* attribute name in the child table */ |
| 477 |
char *colname = get_attname(relid, attnum, false); |
| 478 |
|
| 479 |
/* |
| 480 |
* Determine the attnum for the attribute name in parent (we are |
| 481 |
* using the column list defined on the parent). |
| 482 |
*/ |
| 483 |
attnum = get_attnum(publish_as_relid, colname); |
| 484 |
} |
| 485 |
|
| 486 |
/* replica identity column, not covered by the column list */ |
| 487 |
*invalid_column_list |= !bms_is_member(attnum, columns); |
| 488 |
|
| 489 |
if (*invalid_column_list && *invalid_gen_col) |
| 490 |
break; |
| 491 |
} |
| 492 |
|
| 493 |
bms_free(columns); |
| 494 |
bms_free(idattrs); |
| 495 |
|
| 496 |
return *invalid_column_list || *invalid_gen_col; |
| 497 |
} |
| 498 |
|
| 499 |
/* |
| 500 |
* Invalidate entries in the RelationSyncCache for relations included in the |
| 501 |
* specified publication, either via FOR TABLE or FOR TABLES IN SCHEMA. |
| 502 |
* |
| 503 |
* If 'puballtables' is true, invalidate all cache entries. |
| 504 |
*/ |
| 505 |
void |
| 506 |
InvalidatePubRelSyncCache(Oid pubid, bool puballtables) |
| 507 |
{ |
| 508 |
if (puballtables) |
| 509 |
{ |
| 510 |
CacheInvalidateRelSyncAll(); |
| 511 |
} |
| 512 |
else |
| 513 |
{ |
| 514 |
List *relids = NIL; |
| 515 |
List *schemarelids = NIL; |
| 516 |
|
| 517 |
/* |
| 518 |
* For partitioned tables, we must invalidate all partitions and |
| 519 |
* itself. WAL records for INSERT/UPDATE/DELETE specify leaf tables as |
| 520 |
* a target. However, WAL records for TRUNCATE specify both a root and |
| 521 |
* its leaves. |
| 522 |
*/ |
| 523 |
relids = GetPublicationRelations(pubid, |
| 524 |
PUBLICATION_PART_ALL); |
| 525 |
schemarelids = GetAllSchemaPublicationRelations(pubid, |
| 526 |
PUBLICATION_PART_ALL); |
| 527 |
|
| 528 |
relids = list_concat_unique_oid(relids, schemarelids); |
| 529 |
|
| 530 |
/* Invalidate the relsyncache */ |
| 531 |
foreach_oid(relid, relids) |
| 532 |
CacheInvalidateRelSync(relid); |
| 533 |
} |
| 534 |
|
| 535 |
return; |
| 536 |
} |
| 537 |
|
| 538 |
/* check_functions_in_node callback */ |
| 539 |
static bool |
| 540 |
contain_mutable_or_user_functions_checker(Oid func_id, void *context) |
| 541 |
{ |
| 542 |
return (func_volatile(func_id) != PROVOLATILE_IMMUTABLE || |
| 543 |
func_id >= FirstNormalObjectId); |
| 544 |
} |
| 545 |
|
| 546 |
/* |
| 547 |
* The row filter walker checks if the row filter expression is a "simple |
| 548 |
* expression". |
| 549 |
* |
| 550 |
* It allows only simple or compound expressions such as: |
| 551 |
* - (Var Op Const) |
| 552 |
* - (Var Op Var) |
| 553 |
* - (Var Op Const) AND/OR (Var Op Const) |
| 554 |
* - etc |
| 555 |
* (where Var is a column of the table this filter belongs to) |
| 556 |
* |
| 557 |
* The simple expression has the following restrictions: |
| 558 |
* - User-defined operators are not allowed; |
| 559 |
* - User-defined functions are not allowed; |
| 560 |
* - User-defined types are not allowed; |
| 561 |
* - User-defined collations are not allowed; |
| 562 |
* - Non-immutable built-in functions are not allowed; |
| 563 |
* - System columns are not allowed. |
| 564 |
* |
| 565 |
* NOTES |
| 566 |
* |
| 567 |
* We don't allow user-defined functions/operators/types/collations because |
| 568 |
* (a) if a user drops a user-defined object used in a row filter expression or |
| 569 |
* if there is any other error while using it, the logical decoding |
| 570 |
* infrastructure won't be able to recover from such an error even if the |
| 571 |
* object is recreated again because a historic snapshot is used to evaluate |
| 572 |
* the row filter; |
| 573 |
* (b) a user-defined function can be used to access tables that could have |
| 574 |
* unpleasant results because a historic snapshot is used. That's why only |
| 575 |
* immutable built-in functions are allowed in row filter expressions. |
| 576 |
* |
| 577 |
* We don't allow system columns because currently, we don't have that |
| 578 |
* information in the tuple passed to downstream. Also, as we don't replicate |
| 579 |
* those to subscribers, there doesn't seem to be a need for a filter on those |
| 580 |
* columns. |
| 581 |
* |
| 582 |
* We can allow other node types after more analysis and testing. |
| 583 |
*/ |
| 584 |
static bool |
| 585 |
check_simple_rowfilter_expr_walker(Node *node, ParseState *pstate) |
| 586 |
{ |
| 587 |
char *errdetail_msg = NULL; |
| 588 |
|
| 589 |
if (node == NULL) |
| 590 |
return false; |
| 591 |
|
| 592 |
switch (nodeTag(node)) |
| 593 |
{ |
| 594 |
case T_Var: |
| 595 |
/* System columns are not allowed. */ |
| 596 |
if (((Var *) node)->varattno < InvalidAttrNumber) |
| 597 |
errdetail_msg = _("System columns are not allowed."); |
| 598 |
break; |
| 599 |
case T_OpExpr: |
| 600 |
case T_DistinctExpr: |
| 601 |
case T_NullIfExpr: |
| 602 |
/* OK, except user-defined operators are not allowed. */ |
| 603 |
if (((OpExpr *) node)->opno >= FirstNormalObjectId) |
| 604 |
errdetail_msg = _("User-defined operators are not allowed."); |
| 605 |
break; |
| 606 |
case T_ScalarArrayOpExpr: |
| 607 |
/* OK, except user-defined operators are not allowed. */ |
| 608 |
if (((ScalarArrayOpExpr *) node)->opno >= FirstNormalObjectId) |
| 609 |
errdetail_msg = _("User-defined operators are not allowed."); |
| 610 |
|
| 611 |
/* |
| 612 |
* We don't need to check the hashfuncid and negfuncid of |
| 613 |
* ScalarArrayOpExpr as those functions are only built for a |
| 614 |
* subquery. |
| 615 |
*/ |
| 616 |
break; |
| 617 |
case T_RowCompareExpr: |
| 618 |
{ |
| 619 |
ListCell *opid; |
| 620 |
|
| 621 |
/* OK, except user-defined operators are not allowed. */ |
| 622 |
foreach(opid, ((RowCompareExpr *) node)->opnos) |
| 623 |
{ |
| 624 |
if (lfirst_oid(opid) >= FirstNormalObjectId) |
| 625 |
{ |
| 626 |
errdetail_msg = _("User-defined operators are not allowed."); |
| 627 |
break; |
| 628 |
} |
| 629 |
} |
| 630 |
} |
| 631 |
break; |
| 632 |
case T_Const: |
| 633 |
case T_FuncExpr: |
| 634 |
case T_BoolExpr: |
| 635 |
case T_RelabelType: |
| 636 |
case T_CollateExpr: |
| 637 |
case T_CaseExpr: |
| 638 |
case T_CaseTestExpr: |
| 639 |
case T_ArrayExpr: |
| 640 |
case T_RowExpr: |
| 641 |
case T_CoalesceExpr: |
| 642 |
case T_MinMaxExpr: |
| 643 |
case T_XmlExpr: |
| 644 |
case T_NullTest: |
| 645 |
case T_BooleanTest: |
| 646 |
case T_List: |
| 647 |
/* OK, supported */ |
| 648 |
break; |
| 649 |
default: |
| 650 |
errdetail_msg = _("Only columns, constants, built-in operators, built-in data types, built-in collations, and immutable built-in functions are allowed."); |
| 651 |
break; |
| 652 |
} |
| 653 |
|
| 654 |
/* |
| 655 |
* For all the supported nodes, if we haven't already found a problem, |
| 656 |
* check the types, functions, and collations used in it. We check List |
| 657 |
* by walking through each element. |
| 658 |
*/ |
| 659 |
if (!errdetail_msg && !IsA(node, List)) |
| 660 |
{ |
| 661 |
if (exprType(node) >= FirstNormalObjectId) |
| 662 |
errdetail_msg = _("User-defined types are not allowed."); |
| 663 |
else if (check_functions_in_node(node, contain_mutable_or_user_functions_checker, |
| 664 |
pstate)) |
| 665 |
errdetail_msg = _("User-defined or built-in mutable functions are not allowed."); |
| 666 |
else if (exprCollation(node) >= FirstNormalObjectId || |
| 667 |
exprInputCollation(node) >= FirstNormalObjectId) |
| 668 |
errdetail_msg = _("User-defined collations are not allowed."); |
| 669 |
} |
| 670 |
|
| 671 |
/* |
| 672 |
* If we found a problem in this node, throw error now. Otherwise keep |
| 673 |
* going. |
| 674 |
*/ |
| 675 |
if (errdetail_msg) |
| 676 |
ereport(ERROR, |
| 677 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 678 |
errmsg("invalid publication WHERE expression"), |
| 679 |
errdetail_internal("%s", errdetail_msg), |
| 680 |
parser_errposition(pstate, exprLocation(node)))); |
| 681 |
|
| 682 |
return expression_tree_walker(node, check_simple_rowfilter_expr_walker, |
| 683 |
pstate); |
| 684 |
} |
| 685 |
|
| 686 |
/* |
| 687 |
* Check if the row filter expression is a "simple expression". |
| 688 |
* |
| 689 |
* See check_simple_rowfilter_expr_walker for details. |
| 690 |
*/ |
| 691 |
static bool |
| 692 |
check_simple_rowfilter_expr(Node *node, ParseState *pstate) |
| 693 |
{ |
| 694 |
return check_simple_rowfilter_expr_walker(node, pstate); |
| 695 |
} |
| 696 |
|
| 697 |
/* |
| 698 |
* Transform the publication WHERE expression for all the relations in the list, |
| 699 |
* ensuring it is coerced to boolean and necessary collation information is |
| 700 |
* added if required, and add a new nsitem/RTE for the associated relation to |
| 701 |
* the ParseState's namespace list. |
| 702 |
* |
| 703 |
* Also check the publication row filter expression and throw an error if |
| 704 |
* anything not permitted or unexpected is encountered. |
| 705 |
*/ |
| 706 |
static void |
| 707 |
TransformPubWhereClauses(List *tables, const char *queryString, |
| 708 |
bool pubviaroot) |
| 709 |
{ |
| 710 |
ListCell *lc; |
| 711 |
|
| 712 |
foreach(lc, tables) |
| 713 |
{ |
| 714 |
ParseNamespaceItem *nsitem; |
| 715 |
Node *whereclause = NULL; |
| 716 |
ParseState *pstate; |
| 717 |
PublicationRelInfo *pri = (PublicationRelInfo *) lfirst(lc); |
| 718 |
|
| 719 |
if (pri->whereClause == NULL) |
| 720 |
continue; |
| 721 |
|
| 722 |
/* |
| 723 |
* If the publication doesn't publish changes via the root partitioned |
| 724 |
* table, the partition's row filter will be used. So disallow using |
| 725 |
* WHERE clause on partitioned table in this case. |
| 726 |
*/ |
| 727 |
if (!pubviaroot && |
| 728 |
pri->relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE) |
| 729 |
ereport(ERROR, |
| 730 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 731 |
errmsg("cannot use publication WHERE clause for relation \"%s\"", |
| 732 |
RelationGetRelationName(pri->relation)), |
| 733 |
errdetail("WHERE clause cannot be used for a partitioned table when %s is false.", |
| 734 |
"publish_via_partition_root"))); |
| 735 |
|
| 736 |
/* |
| 737 |
* A fresh pstate is required so that we only have "this" table in its |
| 738 |
* rangetable |
| 739 |
*/ |
| 740 |
pstate = make_parsestate(NULL); |
| 741 |
pstate->p_sourcetext = queryString; |
| 742 |
nsitem = addRangeTableEntryForRelation(pstate, pri->relation, |
| 743 |
AccessShareLock, NULL, |
| 744 |
false, false); |
| 745 |
addNSItemToQuery(pstate, nsitem, false, true, true); |
| 746 |
|
| 747 |
whereclause = transformWhereClause(pstate, |
| 748 |
copyObject(pri->whereClause), |
| 749 |
EXPR_KIND_WHERE, |
| 750 |
"PUBLICATION WHERE"); |
| 751 |
|
| 752 |
/* Fix up collation information */ |
| 753 |
assign_expr_collations(pstate, whereclause); |
| 754 |
|
| 755 |
whereclause = expand_generated_columns_in_expr(whereclause, pri->relation, 1); |
| 756 |
|
| 757 |
/* |
| 758 |
* We allow only simple expressions in row filters. See |
| 759 |
* check_simple_rowfilter_expr_walker. |
| 760 |
*/ |
| 761 |
check_simple_rowfilter_expr(whereclause, pstate); |
| 762 |
|
| 763 |
free_parsestate(pstate); |
| 764 |
|
| 765 |
pri->whereClause = whereclause; |
| 766 |
} |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
/* |
| 771 |
* Given a list of tables that are going to be added to a publication, |
| 772 |
* verify that they fulfill the necessary preconditions, namely: no tables |
| 773 |
* have a column list if any schema is published; and partitioned tables do |
| 774 |
* not have column lists if publish_via_partition_root is not set. |
| 775 |
* |
| 776 |
* 'publish_schema' indicates that the publication contains any TABLES IN |
| 777 |
* SCHEMA elements (newly added in this command, or preexisting). |
| 778 |
* 'pubviaroot' is the value of publish_via_partition_root. |
| 779 |
*/ |
| 780 |
static void |
| 781 |
CheckPubRelationColumnList(char *pubname, List *tables, |
| 782 |
bool publish_schema, bool pubviaroot) |
| 783 |
{ |
| 784 |
ListCell *lc; |
| 785 |
|
| 786 |
foreach(lc, tables) |
| 787 |
{ |
| 788 |
PublicationRelInfo *pri = (PublicationRelInfo *) lfirst(lc); |
| 789 |
|
| 790 |
if (pri->columns == NIL) |
| 791 |
continue; |
| 792 |
|
| 793 |
/* |
| 794 |
* Disallow specifying column list if any schema is in the |
| 795 |
* publication. |
| 796 |
* |
| 797 |
* XXX We could instead just forbid the case when the publication |
| 798 |
* tries to publish the table with a column list and a schema for that |
| 799 |
* table. However, if we do that then we need a restriction during |
| 800 |
* ALTER TABLE ... SET SCHEMA to prevent such a case which doesn't |
| 801 |
* seem to be a good idea. |
| 802 |
*/ |
| 803 |
if (publish_schema) |
| 804 |
ereport(ERROR, |
| 805 |
errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 806 |
errmsg("cannot use column list for relation \"%s.%s\" in publication \"%s\"", |
| 807 |
get_namespace_name(RelationGetNamespace(pri->relation)), |
| 808 |
RelationGetRelationName(pri->relation), pubname), |
| 809 |
errdetail("Column lists cannot be specified in publications containing FOR TABLES IN SCHEMA elements.")); |
| 810 |
|
| 811 |
/* |
| 812 |
* If the publication doesn't publish changes via the root partitioned |
| 813 |
* table, the partition's column list will be used. So disallow using |
| 814 |
* a column list on the partitioned table in this case. |
| 815 |
*/ |
| 816 |
if (!pubviaroot && |
| 817 |
pri->relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE) |
| 818 |
ereport(ERROR, |
| 819 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 820 |
errmsg("cannot use column list for relation \"%s.%s\" in publication \"%s\"", |
| 821 |
get_namespace_name(RelationGetNamespace(pri->relation)), |
| 822 |
RelationGetRelationName(pri->relation), pubname), |
| 823 |
errdetail("Column lists cannot be specified for partitioned tables when %s is false.", |
| 824 |
"publish_via_partition_root"))); |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
/* |
| 829 |
* Create new publication. |
| 830 |
*/ |
| 831 |
ObjectAddress |
| 832 |
CreatePublication(ParseState *pstate, CreatePublicationStmt *stmt) |
| 833 |
{ |
| 834 |
Relation rel; |
| 835 |
ObjectAddress myself; |
| 836 |
Oid puboid; |
| 837 |
bool nulls[Natts_pg_publication]; |
| 838 |
Datum values[Natts_pg_publication]; |
| 839 |
HeapTuple tup; |
| 840 |
bool publish_given; |
| 841 |
PublicationActions pubactions; |
| 842 |
bool publish_via_partition_root_given; |
| 843 |
bool publish_via_partition_root; |
| 844 |
bool publish_generated_columns_given; |
| 845 |
char publish_generated_columns; |
| 846 |
AclResult aclresult; |
| 847 |
List *relations = NIL; |
| 848 |
List *schemaidlist = NIL; |
| 849 |
|
| 850 |
/* must have CREATE privilege on database */ |
| 851 |
aclresult = object_aclcheck(DatabaseRelationId, MyDatabaseId, GetUserId(), ACL_CREATE); |
| 852 |
if (aclresult != ACLCHECK_OK) |
| 853 |
aclcheck_error(aclresult, OBJECT_DATABASE, |
| 854 |
get_database_name(MyDatabaseId)); |
| 855 |
|
| 856 |
/* FOR ALL TABLES requires superuser */ |
| 857 |
if (stmt->for_all_tables && !superuser()) |
| 858 |
ereport(ERROR, |
| 859 |
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 860 |
errmsg("must be superuser to create FOR ALL TABLES publication"))); |
| 861 |
|
| 862 |
rel = table_open(PublicationRelationId, RowExclusiveLock); |
| 863 |
|
| 864 |
/* Check if name is used */ |
| 865 |
puboid = GetSysCacheOid1(PUBLICATIONNAME, Anum_pg_publication_oid, |
| 866 |
CStringGetDatum(stmt->pubname)); |
| 867 |
if (OidIsValid(puboid)) |
| 868 |
ereport(ERROR, |
| 869 |
(errcode(ERRCODE_DUPLICATE_OBJECT), |
| 870 |
errmsg("publication \"%s\" already exists", |
| 871 |
stmt->pubname))); |
| 872 |
|
| 873 |
/* Form a tuple. */ |
| 874 |
memset(values, 0, sizeof(values)); |
| 875 |
memset(nulls, false, sizeof(nulls)); |
| 876 |
|
| 877 |
values[Anum_pg_publication_pubname - 1] = |
| 878 |
DirectFunctionCall1(namein, CStringGetDatum(stmt->pubname)); |
| 879 |
values[Anum_pg_publication_pubowner - 1] = ObjectIdGetDatum(GetUserId()); |
| 880 |
|
| 881 |
parse_publication_options(pstate, |
| 882 |
stmt->options, |
| 883 |
&publish_given, &pubactions, |
| 884 |
&publish_via_partition_root_given, |
| 885 |
&publish_via_partition_root, |
| 886 |
&publish_generated_columns_given, |
| 887 |
&publish_generated_columns); |
| 888 |
|
| 889 |
puboid = GetNewOidWithIndex(rel, PublicationObjectIndexId, |
| 890 |
Anum_pg_publication_oid); |
| 891 |
values[Anum_pg_publication_oid - 1] = ObjectIdGetDatum(puboid); |
| 892 |
values[Anum_pg_publication_puballtables - 1] = |
| 893 |
BoolGetDatum(stmt->for_all_tables); |
| 894 |
values[Anum_pg_publication_pubinsert - 1] = |
| 895 |
BoolGetDatum(pubactions.pubinsert); |
| 896 |
values[Anum_pg_publication_pubupdate - 1] = |
| 897 |
BoolGetDatum(pubactions.pubupdate); |
| 898 |
values[Anum_pg_publication_pubdelete - 1] = |
| 899 |
BoolGetDatum(pubactions.pubdelete); |
| 900 |
values[Anum_pg_publication_pubtruncate - 1] = |
| 901 |
BoolGetDatum(pubactions.pubtruncate); |
| 902 |
values[Anum_pg_publication_pubviaroot - 1] = |
| 903 |
BoolGetDatum(publish_via_partition_root); |
| 904 |
values[Anum_pg_publication_pubgencols - 1] = |
| 905 |
CharGetDatum(publish_generated_columns); |
| 906 |
|
| 907 |
tup = heap_form_tuple(RelationGetDescr(rel), values, nulls); |
| 908 |
|
| 909 |
/* Insert tuple into catalog. */ |
| 910 |
CatalogTupleInsert(rel, tup); |
| 911 |
heap_freetuple(tup); |
| 912 |
|
| 913 |
recordDependencyOnOwner(PublicationRelationId, puboid, GetUserId()); |
| 914 |
|
| 915 |
ObjectAddressSet(myself, PublicationRelationId, puboid); |
| 916 |
|
| 917 |
/* Make the changes visible. */ |
| 918 |
CommandCounterIncrement(); |
| 919 |
|
| 920 |
/* Associate objects with the publication. */ |
| 921 |
if (stmt->for_all_tables) |
| 922 |
{ |
| 923 |
/* Invalidate relcache so that publication info is rebuilt. */ |
| 924 |
CacheInvalidateRelcacheAll(); |
| 925 |
} |
| 926 |
else |
| 927 |
{ |
| 928 |
ObjectsInPublicationToOids(stmt->pubobjects, pstate, &relations, |
| 929 |
&schemaidlist); |
| 930 |
|
| 931 |
/* FOR TABLES IN SCHEMA requires superuser */ |
| 932 |
if (schemaidlist != NIL && !superuser()) |
| 933 |
ereport(ERROR, |
| 934 |
errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 935 |
errmsg("must be superuser to create FOR TABLES IN SCHEMA publication")); |
| 936 |
|
| 937 |
if (relations != NIL) |
| 938 |
{ |
| 939 |
List *rels; |
| 940 |
|
| 941 |
rels = OpenTableList(relations); |
| 942 |
TransformPubWhereClauses(rels, pstate->p_sourcetext, |
| 943 |
publish_via_partition_root); |
| 944 |
|
| 945 |
CheckPubRelationColumnList(stmt->pubname, rels, |
| 946 |
schemaidlist != NIL, |
| 947 |
publish_via_partition_root); |
| 948 |
|
| 949 |
PublicationAddTables(puboid, rels, true, NULL); |
| 950 |
CloseTableList(rels); |
| 951 |
} |
| 952 |
|
| 953 |
if (schemaidlist != NIL) |
| 954 |
{ |
| 955 |
/* |
| 956 |
* Schema lock is held until the publication is created to prevent |
| 957 |
* concurrent schema deletion. |
| 958 |
*/ |
| 959 |
LockSchemaList(schemaidlist); |
| 960 |
PublicationAddSchemas(puboid, schemaidlist, true, NULL); |
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
table_close(rel, RowExclusiveLock); |
| 965 |
|
| 966 |
InvokeObjectPostCreateHook(PublicationRelationId, puboid, 0); |
| 967 |
|
| 968 |
if (wal_level != WAL_LEVEL_LOGICAL) |
| 969 |
ereport(WARNING, |
| 970 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 971 |
errmsg("\"wal_level\" is insufficient to publish logical changes"), |
| 972 |
errhint("Set \"wal_level\" to \"logical\" before creating subscriptions."))); |
| 973 |
|
| 974 |
return myself; |
| 975 |
} |
| 976 |
|
| 977 |
/* |
| 978 |
* Change options of a publication. |
| 979 |
*/ |
| 980 |
static void |
| 981 |
AlterPublicationOptions(ParseState *pstate, AlterPublicationStmt *stmt, |
| 982 |
Relation rel, HeapTuple tup) |
| 983 |
{ |
| 984 |
bool nulls[Natts_pg_publication]; |
| 985 |
bool replaces[Natts_pg_publication]; |
| 986 |
Datum values[Natts_pg_publication]; |
| 987 |
bool publish_given; |
| 988 |
PublicationActions pubactions; |
| 989 |
bool publish_via_partition_root_given; |
| 990 |
bool publish_via_partition_root; |
| 991 |
bool publish_generated_columns_given; |
| 992 |
char publish_generated_columns; |
| 993 |
ObjectAddress obj; |
| 994 |
Form_pg_publication pubform; |
| 995 |
List *root_relids = NIL; |
| 996 |
ListCell *lc; |
| 997 |
|
| 998 |
parse_publication_options(pstate, |
| 999 |
stmt->options, |
| 1000 |
&publish_given, &pubactions, |
| 1001 |
&publish_via_partition_root_given, |
| 1002 |
&publish_via_partition_root, |
| 1003 |
&publish_generated_columns_given, |
| 1004 |
&publish_generated_columns); |
| 1005 |
|
| 1006 |
pubform = (Form_pg_publication) GETSTRUCT(tup); |
| 1007 |
|
| 1008 |
/* |
| 1009 |
* If the publication doesn't publish changes via the root partitioned |
| 1010 |
* table, the partition's row filter and column list will be used. So |
| 1011 |
* disallow using WHERE clause and column lists on partitioned table in |
| 1012 |
* this case. |
| 1013 |
*/ |
| 1014 |
if (!pubform->puballtables && publish_via_partition_root_given && |
| 1015 |
!publish_via_partition_root) |
| 1016 |
{ |
| 1017 |
/* |
| 1018 |
* Lock the publication so nobody else can do anything with it. This |
| 1019 |
* prevents concurrent alter to add partitioned table(s) with WHERE |
| 1020 |
* clause(s) and/or column lists which we don't allow when not |
| 1021 |
* publishing via root. |
| 1022 |
*/ |
| 1023 |
LockDatabaseObject(PublicationRelationId, pubform->oid, 0, |
| 1024 |
AccessShareLock); |
| 1025 |
|
| 1026 |
root_relids = GetPublicationRelations(pubform->oid, |
| 1027 |
PUBLICATION_PART_ROOT); |
| 1028 |
|
| 1029 |
foreach(lc, root_relids) |
| 1030 |
{ |
| 1031 |
Oid relid = lfirst_oid(lc); |
| 1032 |
HeapTuple rftuple; |
| 1033 |
char relkind; |
| 1034 |
char *relname; |
| 1035 |
bool has_rowfilter; |
| 1036 |
bool has_collist; |
| 1037 |
|
| 1038 |
/* |
| 1039 |
* Beware: we don't have lock on the relations, so cope silently |
| 1040 |
* with the cache lookups returning NULL. |
| 1041 |
*/ |
| 1042 |
|
| 1043 |
rftuple = SearchSysCache2(PUBLICATIONRELMAP, |
| 1044 |
ObjectIdGetDatum(relid), |
| 1045 |
ObjectIdGetDatum(pubform->oid)); |
| 1046 |
if (!HeapTupleIsValid(rftuple)) |
| 1047 |
continue; |
| 1048 |
has_rowfilter = !heap_attisnull(rftuple, Anum_pg_publication_rel_prqual, NULL); |
| 1049 |
has_collist = !heap_attisnull(rftuple, Anum_pg_publication_rel_prattrs, NULL); |
| 1050 |
if (!has_rowfilter && !has_collist) |
| 1051 |
{ |
| 1052 |
ReleaseSysCache(rftuple); |
| 1053 |
continue; |
| 1054 |
} |
| 1055 |
|
| 1056 |
relkind = get_rel_relkind(relid); |
| 1057 |
if (relkind != RELKIND_PARTITIONED_TABLE) |
| 1058 |
{ |
| 1059 |
ReleaseSysCache(rftuple); |
| 1060 |
continue; |
| 1061 |
} |
| 1062 |
relname = get_rel_name(relid); |
| 1063 |
if (relname == NULL) /* table concurrently dropped */ |
| 1064 |
{ |
| 1065 |
ReleaseSysCache(rftuple); |
| 1066 |
continue; |
| 1067 |
} |
| 1068 |
|
| 1069 |
if (has_rowfilter) |
| 1070 |
ereport(ERROR, |
| 1071 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 1072 |
errmsg("cannot set parameter \"%s\" to false for publication \"%s\"", |
| 1073 |
"publish_via_partition_root", |
| 1074 |
stmt->pubname), |
| 1075 |
errdetail("The publication contains a WHERE clause for partitioned table \"%s\", which is not allowed when \"%s\" is false.", |
| 1076 |
relname, "publish_via_partition_root"))); |
| 1077 |
Assert(has_collist); |
| 1078 |
ereport(ERROR, |
| 1079 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 1080 |
errmsg("cannot set parameter \"%s\" to false for publication \"%s\"", |
| 1081 |
"publish_via_partition_root", |
| 1082 |
stmt->pubname), |
| 1083 |
errdetail("The publication contains a column list for partitioned table \"%s\", which is not allowed when \"%s\" is false.", |
| 1084 |
relname, "publish_via_partition_root"))); |
| 1085 |
} |
| 1086 |
} |
| 1087 |
|
| 1088 |
/* Everything ok, form a new tuple. */ |
| 1089 |
memset(values, 0, sizeof(values)); |
| 1090 |
memset(nulls, false, sizeof(nulls)); |
| 1091 |
memset(replaces, false, sizeof(replaces)); |
| 1092 |
|
| 1093 |
if (publish_given) |
| 1094 |
{ |
| 1095 |
values[Anum_pg_publication_pubinsert - 1] = BoolGetDatum(pubactions.pubinsert); |
| 1096 |
replaces[Anum_pg_publication_pubinsert - 1] = true; |
| 1097 |
|
| 1098 |
values[Anum_pg_publication_pubupdate - 1] = BoolGetDatum(pubactions.pubupdate); |
| 1099 |
replaces[Anum_pg_publication_pubupdate - 1] = true; |
| 1100 |
|
| 1101 |
values[Anum_pg_publication_pubdelete - 1] = BoolGetDatum(pubactions.pubdelete); |
| 1102 |
replaces[Anum_pg_publication_pubdelete - 1] = true; |
| 1103 |
|
| 1104 |
values[Anum_pg_publication_pubtruncate - 1] = BoolGetDatum(pubactions.pubtruncate); |
| 1105 |
replaces[Anum_pg_publication_pubtruncate - 1] = true; |
| 1106 |
} |
| 1107 |
|
| 1108 |
if (publish_via_partition_root_given) |
| 1109 |
{ |
| 1110 |
values[Anum_pg_publication_pubviaroot - 1] = BoolGetDatum(publish_via_partition_root); |
| 1111 |
replaces[Anum_pg_publication_pubviaroot - 1] = true; |
| 1112 |
} |
| 1113 |
|
| 1114 |
if (publish_generated_columns_given) |
| 1115 |
{ |
| 1116 |
values[Anum_pg_publication_pubgencols - 1] = CharGetDatum(publish_generated_columns); |
| 1117 |
replaces[Anum_pg_publication_pubgencols - 1] = true; |
| 1118 |
} |
| 1119 |
|
| 1120 |
tup = heap_modify_tuple(tup, RelationGetDescr(rel), values, nulls, |
| 1121 |
replaces); |
| 1122 |
|
| 1123 |
/* Update the catalog. */ |
| 1124 |
CatalogTupleUpdate(rel, &tup->t_self, tup); |
| 1125 |
|
| 1126 |
CommandCounterIncrement(); |
| 1127 |
|
| 1128 |
pubform = (Form_pg_publication) GETSTRUCT(tup); |
| 1129 |
|
| 1130 |
/* Invalidate the relcache. */ |
| 1131 |
if (pubform->puballtables) |
| 1132 |
{ |
| 1133 |
CacheInvalidateRelcacheAll(); |
| 1134 |
} |
| 1135 |
else |
| 1136 |
{ |
| 1137 |
List *relids = NIL; |
| 1138 |
List *schemarelids = NIL; |
| 1139 |
|
| 1140 |
/* |
| 1141 |
* For any partitioned tables contained in the publication, we must |
| 1142 |
* invalidate all partitions contained in the respective partition |
| 1143 |
* trees, not just those explicitly mentioned in the publication. |
| 1144 |
*/ |
| 1145 |
if (root_relids == NIL) |
| 1146 |
relids = GetPublicationRelations(pubform->oid, |
| 1147 |
PUBLICATION_PART_ALL); |
| 1148 |
else |
| 1149 |
{ |
| 1150 |
/* |
| 1151 |
* We already got tables explicitly mentioned in the publication. |
| 1152 |
* Now get all partitions for the partitioned table in the list. |
| 1153 |
*/ |
| 1154 |
foreach(lc, root_relids) |
| 1155 |
relids = GetPubPartitionOptionRelations(relids, |
| 1156 |
PUBLICATION_PART_ALL, |
| 1157 |
lfirst_oid(lc)); |
| 1158 |
} |
| 1159 |
|
| 1160 |
schemarelids = GetAllSchemaPublicationRelations(pubform->oid, |
| 1161 |
PUBLICATION_PART_ALL); |
| 1162 |
relids = list_concat_unique_oid(relids, schemarelids); |
| 1163 |
|
| 1164 |
InvalidatePublicationRels(relids); |
| 1165 |
} |
| 1166 |
|
| 1167 |
ObjectAddressSet(obj, PublicationRelationId, pubform->oid); |
| 1168 |
EventTriggerCollectSimpleCommand(obj, InvalidObjectAddress, |
| 1169 |
(Node *) stmt); |
| 1170 |
|
| 1171 |
InvokeObjectPostAlterHook(PublicationRelationId, pubform->oid, 0); |
| 1172 |
} |
| 1173 |
|
| 1174 |
/* |
| 1175 |
* Invalidate the relations. |
| 1176 |
*/ |
| 1177 |
void |
| 1178 |
InvalidatePublicationRels(List *relids) |
| 1179 |
{ |
| 1180 |
/* |
| 1181 |
* We don't want to send too many individual messages, at some point it's |
| 1182 |
* cheaper to just reset whole relcache. |
| 1183 |
*/ |
| 1184 |
if (list_length(relids) < MAX_RELCACHE_INVAL_MSGS) |
| 1185 |
{ |
| 1186 |
ListCell *lc; |
| 1187 |
|
| 1188 |
foreach(lc, relids) |
| 1189 |
CacheInvalidateRelcacheByRelid(lfirst_oid(lc)); |
| 1190 |
} |
| 1191 |
else |
| 1192 |
CacheInvalidateRelcacheAll(); |
| 1193 |
} |
| 1194 |
|
| 1195 |
/* |
| 1196 |
* Add or remove table to/from publication. |
| 1197 |
*/ |
| 1198 |
static void |
| 1199 |
AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, |
| 1200 |
List *tables, const char *queryString, |
| 1201 |
bool publish_schema) |
| 1202 |
{ |
| 1203 |
List *rels = NIL; |
| 1204 |
Form_pg_publication pubform = (Form_pg_publication) GETSTRUCT(tup); |
| 1205 |
Oid pubid = pubform->oid; |
| 1206 |
|
| 1207 |
/* |
| 1208 |
* Nothing to do if no objects, except in SET: for that it is quite |
| 1209 |
* possible that user has not specified any tables in which case we need |
| 1210 |
* to remove all the existing tables. |
| 1211 |
*/ |
| 1212 |
if (!tables && stmt->action != AP_SetObjects) |
| 1213 |
return; |
| 1214 |
|
| 1215 |
rels = OpenTableList(tables); |
| 1216 |
|
| 1217 |
if (stmt->action == AP_AddObjects) |
| 1218 |
{ |
| 1219 |
TransformPubWhereClauses(rels, queryString, pubform->pubviaroot); |
| 1220 |
|
| 1221 |
publish_schema |= is_schema_publication(pubid); |
| 1222 |
|
| 1223 |
CheckPubRelationColumnList(stmt->pubname, rels, publish_schema, |
| 1224 |
pubform->pubviaroot); |
| 1225 |
|
| 1226 |
PublicationAddTables(pubid, rels, false, stmt); |
| 1227 |
} |
| 1228 |
else if (stmt->action == AP_DropObjects) |
| 1229 |
PublicationDropTables(pubid, rels, false); |
| 1230 |
else /* AP_SetObjects */ |
| 1231 |
{ |
| 1232 |
List *oldrelids = GetPublicationRelations(pubid, |
| 1233 |
PUBLICATION_PART_ROOT); |
| 1234 |
List *delrels = NIL; |
| 1235 |
ListCell *oldlc; |
| 1236 |
|
| 1237 |
TransformPubWhereClauses(rels, queryString, pubform->pubviaroot); |
| 1238 |
|
| 1239 |
CheckPubRelationColumnList(stmt->pubname, rels, publish_schema, |
| 1240 |
pubform->pubviaroot); |
| 1241 |
|
| 1242 |
/* |
| 1243 |
* To recreate the relation list for the publication, look for |
| 1244 |
* existing relations that do not need to be dropped. |
| 1245 |
*/ |
| 1246 |
foreach(oldlc, oldrelids) |
| 1247 |
{ |
| 1248 |
Oid oldrelid = lfirst_oid(oldlc); |
| 1249 |
ListCell *newlc; |
| 1250 |
PublicationRelInfo *oldrel; |
| 1251 |
bool found = false; |
| 1252 |
HeapTuple rftuple; |
| 1253 |
Node *oldrelwhereclause = NULL; |
| 1254 |
Bitmapset *oldcolumns = NULL; |
| 1255 |
|
| 1256 |
/* look up the cache for the old relmap */ |
| 1257 |
rftuple = SearchSysCache2(PUBLICATIONRELMAP, |
| 1258 |
ObjectIdGetDatum(oldrelid), |
| 1259 |
ObjectIdGetDatum(pubid)); |
| 1260 |
|
| 1261 |
/* |
| 1262 |
* See if the existing relation currently has a WHERE clause or a |
| 1263 |
* column list. We need to compare those too. |
| 1264 |
*/ |
| 1265 |
if (HeapTupleIsValid(rftuple)) |
| 1266 |
{ |
| 1267 |
bool isnull = true; |
| 1268 |
Datum whereClauseDatum; |
| 1269 |
Datum columnListDatum; |
| 1270 |
|
| 1271 |
/* Load the WHERE clause for this table. */ |
| 1272 |
whereClauseDatum = SysCacheGetAttr(PUBLICATIONRELMAP, rftuple, |
| 1273 |
Anum_pg_publication_rel_prqual, |
| 1274 |
&isnull); |
| 1275 |
if (!isnull) |
| 1276 |
oldrelwhereclause = stringToNode(TextDatumGetCString(whereClauseDatum)); |
| 1277 |
|
| 1278 |
/* Transform the int2vector column list to a bitmap. */ |
| 1279 |
columnListDatum = SysCacheGetAttr(PUBLICATIONRELMAP, rftuple, |
| 1280 |
Anum_pg_publication_rel_prattrs, |
| 1281 |
&isnull); |
| 1282 |
|
| 1283 |
if (!isnull) |
| 1284 |
oldcolumns = pub_collist_to_bitmapset(NULL, columnListDatum, NULL); |
| 1285 |
|
| 1286 |
ReleaseSysCache(rftuple); |
| 1287 |
} |
| 1288 |
|
| 1289 |
foreach(newlc, rels) |
| 1290 |
{ |
| 1291 |
PublicationRelInfo *newpubrel; |
| 1292 |
Oid newrelid; |
| 1293 |
Bitmapset *newcolumns = NULL; |
| 1294 |
|
| 1295 |
newpubrel = (PublicationRelInfo *) lfirst(newlc); |
| 1296 |
newrelid = RelationGetRelid(newpubrel->relation); |
| 1297 |
|
| 1298 |
/* |
| 1299 |
* Validate the column list. If the column list or WHERE |
| 1300 |
* clause changes, then the validation done here will be |
| 1301 |
* duplicated inside PublicationAddTables(). The validation |
| 1302 |
* is cheap enough that that seems harmless. |
| 1303 |
*/ |
| 1304 |
newcolumns = pub_collist_validate(newpubrel->relation, |
| 1305 |
newpubrel->columns); |
| 1306 |
|
| 1307 |
/* |
| 1308 |
* Check if any of the new set of relations matches with the |
| 1309 |
* existing relations in the publication. Additionally, if the |
| 1310 |
* relation has an associated WHERE clause, check the WHERE |
| 1311 |
* expressions also match. Same for the column list. Drop the |
| 1312 |
* rest. |
| 1313 |
*/ |
| 1314 |
if (newrelid == oldrelid) |
| 1315 |
{ |
| 1316 |
if (equal(oldrelwhereclause, newpubrel->whereClause) && |
| 1317 |
bms_equal(oldcolumns, newcolumns)) |
| 1318 |
{ |
| 1319 |
found = true; |
| 1320 |
break; |
| 1321 |
} |
| 1322 |
} |
| 1323 |
} |
| 1324 |
|
| 1325 |
/* |
| 1326 |
* Add the non-matched relations to a list so that they can be |
| 1327 |
* dropped. |
| 1328 |
*/ |
| 1329 |
if (!found) |
| 1330 |
{ |
| 1331 |
oldrel = palloc(sizeof(PublicationRelInfo)); |
| 1332 |
oldrel->whereClause = NULL; |
| 1333 |
oldrel->columns = NIL; |
| 1334 |
oldrel->relation = table_open(oldrelid, |
| 1335 |
ShareUpdateExclusiveLock); |
| 1336 |
delrels = lappend(delrels, oldrel); |
| 1337 |
} |
| 1338 |
} |
| 1339 |
|
| 1340 |
/* And drop them. */ |
| 1341 |
PublicationDropTables(pubid, delrels, true); |
| 1342 |
|
| 1343 |
/* |
| 1344 |
* Don't bother calculating the difference for adding, we'll catch and |
| 1345 |
* skip existing ones when doing catalog update. |
| 1346 |
*/ |
| 1347 |
PublicationAddTables(pubid, rels, true, stmt); |
| 1348 |
|
| 1349 |
CloseTableList(delrels); |
| 1350 |
} |
| 1351 |
|
| 1352 |
CloseTableList(rels); |
| 1353 |
} |
| 1354 |
|
| 1355 |
/* |
| 1356 |
* Alter the publication schemas. |
| 1357 |
* |
| 1358 |
* Add or remove schemas to/from publication. |
| 1359 |
*/ |
| 1360 |
static void |
| 1361 |
AlterPublicationSchemas(AlterPublicationStmt *stmt, |
| 1362 |
HeapTuple tup, List *schemaidlist) |
| 1363 |
{ |
| 1364 |
Form_pg_publication pubform = (Form_pg_publication) GETSTRUCT(tup); |
| 1365 |
|
| 1366 |
/* |
| 1367 |
* Nothing to do if no objects, except in SET: for that it is quite |
| 1368 |
* possible that user has not specified any schemas in which case we need |
| 1369 |
* to remove all the existing schemas. |
| 1370 |
*/ |
| 1371 |
if (!schemaidlist && stmt->action != AP_SetObjects) |
| 1372 |
return; |
| 1373 |
|
| 1374 |
/* |
| 1375 |
* Schema lock is held until the publication is altered to prevent |
| 1376 |
* concurrent schema deletion. |
| 1377 |
*/ |
| 1378 |
LockSchemaList(schemaidlist); |
| 1379 |
if (stmt->action == AP_AddObjects) |
| 1380 |
{ |
| 1381 |
ListCell *lc; |
| 1382 |
List *reloids; |
| 1383 |
|
| 1384 |
reloids = GetPublicationRelations(pubform->oid, PUBLICATION_PART_ROOT); |
| 1385 |
|
| 1386 |
foreach(lc, reloids) |
| 1387 |
{ |
| 1388 |
HeapTuple coltuple; |
| 1389 |
|
| 1390 |
coltuple = SearchSysCache2(PUBLICATIONRELMAP, |
| 1391 |
ObjectIdGetDatum(lfirst_oid(lc)), |
| 1392 |
ObjectIdGetDatum(pubform->oid)); |
| 1393 |
|
| 1394 |
if (!HeapTupleIsValid(coltuple)) |
| 1395 |
continue; |
| 1396 |
|
| 1397 |
/* |
| 1398 |
* Disallow adding schema if column list is already part of the |
| 1399 |
* publication. See CheckPubRelationColumnList. |
| 1400 |
*/ |
| 1401 |
if (!heap_attisnull(coltuple, Anum_pg_publication_rel_prattrs, NULL)) |
| 1402 |
ereport(ERROR, |
| 1403 |
errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 1404 |
errmsg("cannot add schema to publication \"%s\"", |
| 1405 |
stmt->pubname), |
| 1406 |
errdetail("Schemas cannot be added if any tables that specify a column list are already part of the publication.")); |
| 1407 |
|
| 1408 |
ReleaseSysCache(coltuple); |
| 1409 |
} |
| 1410 |
|
| 1411 |
PublicationAddSchemas(pubform->oid, schemaidlist, false, stmt); |
| 1412 |
} |
| 1413 |
else if (stmt->action == AP_DropObjects) |
| 1414 |
PublicationDropSchemas(pubform->oid, schemaidlist, false); |
| 1415 |
else /* AP_SetObjects */ |
| 1416 |
{ |
| 1417 |
List *oldschemaids = GetPublicationSchemas(pubform->oid); |
| 1418 |
List *delschemas = NIL; |
| 1419 |
|
| 1420 |
/* Identify which schemas should be dropped */ |
| 1421 |
delschemas = list_difference_oid(oldschemaids, schemaidlist); |
| 1422 |
|
| 1423 |
/* |
| 1424 |
* Schema lock is held until the publication is altered to prevent |
| 1425 |
* concurrent schema deletion. |
| 1426 |
*/ |
| 1427 |
LockSchemaList(delschemas); |
| 1428 |
|
| 1429 |
/* And drop them */ |
| 1430 |
PublicationDropSchemas(pubform->oid, delschemas, true); |
| 1431 |
|
| 1432 |
/* |
| 1433 |
* Don't bother calculating the difference for adding, we'll catch and |
| 1434 |
* skip existing ones when doing catalog update. |
| 1435 |
*/ |
| 1436 |
PublicationAddSchemas(pubform->oid, schemaidlist, true, stmt); |
| 1437 |
} |
| 1438 |
} |
| 1439 |
|
| 1440 |
/* |
| 1441 |
* Check if relations and schemas can be in a given publication and throw |
| 1442 |
* appropriate error if not. |
| 1443 |
*/ |
| 1444 |
static void |
| 1445 |
CheckAlterPublication(AlterPublicationStmt *stmt, HeapTuple tup, |
| 1446 |
List *tables, List *schemaidlist) |
| 1447 |
{ |
| 1448 |
Form_pg_publication pubform = (Form_pg_publication) GETSTRUCT(tup); |
| 1449 |
|
| 1450 |
if ((stmt->action == AP_AddObjects || stmt->action == AP_SetObjects) && |
| 1451 |
schemaidlist && !superuser()) |
| 1452 |
ereport(ERROR, |
| 1453 |
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 1454 |
errmsg("must be superuser to add or set schemas"))); |
| 1455 |
|
| 1456 |
/* |
| 1457 |
* Check that user is allowed to manipulate the publication tables in |
| 1458 |
* schema |
| 1459 |
*/ |
| 1460 |
if (schemaidlist && pubform->puballtables) |
| 1461 |
ereport(ERROR, |
| 1462 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 1463 |
errmsg("publication \"%s\" is defined as FOR ALL TABLES", |
| 1464 |
NameStr(pubform->pubname)), |
| 1465 |
errdetail("Schemas cannot be added to or dropped from FOR ALL TABLES publications."))); |
| 1466 |
|
| 1467 |
/* Check that user is allowed to manipulate the publication tables. */ |
| 1468 |
if (tables && pubform->puballtables) |
| 1469 |
ereport(ERROR, |
| 1470 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 1471 |
errmsg("publication \"%s\" is defined as FOR ALL TABLES", |
| 1472 |
NameStr(pubform->pubname)), |
| 1473 |
errdetail("Tables cannot be added to or dropped from FOR ALL TABLES publications."))); |
| 1474 |
} |
| 1475 |
|
| 1476 |
/* |
| 1477 |
* Alter the existing publication. |
| 1478 |
* |
| 1479 |
* This is dispatcher function for AlterPublicationOptions, |
| 1480 |
* AlterPublicationSchemas and AlterPublicationTables. |
| 1481 |
*/ |
| 1482 |
void |
| 1483 |
AlterPublication(ParseState *pstate, AlterPublicationStmt *stmt) |
| 1484 |
{ |
| 1485 |
Relation rel; |
| 1486 |
HeapTuple tup; |
| 1487 |
Form_pg_publication pubform; |
| 1488 |
|
| 1489 |
rel = table_open(PublicationRelationId, RowExclusiveLock); |
| 1490 |
|
| 1491 |
tup = SearchSysCacheCopy1(PUBLICATIONNAME, |
| 1492 |
CStringGetDatum(stmt->pubname)); |
| 1493 |
|
| 1494 |
if (!HeapTupleIsValid(tup)) |
| 1495 |
ereport(ERROR, |
| 1496 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 1497 |
errmsg("publication \"%s\" does not exist", |
| 1498 |
stmt->pubname))); |
| 1499 |
|
| 1500 |
pubform = (Form_pg_publication) GETSTRUCT(tup); |
| 1501 |
|
| 1502 |
/* must be owner */ |
| 1503 |
if (!object_ownercheck(PublicationRelationId, pubform->oid, GetUserId())) |
| 1504 |
aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_PUBLICATION, |
| 1505 |
stmt->pubname); |
| 1506 |
|
| 1507 |
if (stmt->options) |
| 1508 |
AlterPublicationOptions(pstate, stmt, rel, tup); |
| 1509 |
else |
| 1510 |
{ |
| 1511 |
List *relations = NIL; |
| 1512 |
List *schemaidlist = NIL; |
| 1513 |
Oid pubid = pubform->oid; |
| 1514 |
|
| 1515 |
ObjectsInPublicationToOids(stmt->pubobjects, pstate, &relations, |
| 1516 |
&schemaidlist); |
| 1517 |
|
| 1518 |
CheckAlterPublication(stmt, tup, relations, schemaidlist); |
| 1519 |
|
| 1520 |
heap_freetuple(tup); |
| 1521 |
|
| 1522 |
/* Lock the publication so nobody else can do anything with it. */ |
| 1523 |
LockDatabaseObject(PublicationRelationId, pubid, 0, |
| 1524 |
AccessExclusiveLock); |
| 1525 |
|
| 1526 |
/* |
| 1527 |
* It is possible that by the time we acquire the lock on publication, |
| 1528 |
* concurrent DDL has removed it. We can test this by checking the |
| 1529 |
* existence of publication. We get the tuple again to avoid the risk |
| 1530 |
* of any publication option getting changed. |
| 1531 |
*/ |
| 1532 |
tup = SearchSysCacheCopy1(PUBLICATIONOID, ObjectIdGetDatum(pubid)); |
| 1533 |
if (!HeapTupleIsValid(tup)) |
| 1534 |
ereport(ERROR, |
| 1535 |
errcode(ERRCODE_UNDEFINED_OBJECT), |
| 1536 |
errmsg("publication \"%s\" does not exist", |
| 1537 |
stmt->pubname)); |
| 1538 |
|
| 1539 |
AlterPublicationTables(stmt, tup, relations, pstate->p_sourcetext, |
| 1540 |
schemaidlist != NIL); |
| 1541 |
AlterPublicationSchemas(stmt, tup, schemaidlist); |
| 1542 |
} |
| 1543 |
|
| 1544 |
/* Cleanup. */ |
| 1545 |
heap_freetuple(tup); |
| 1546 |
table_close(rel, RowExclusiveLock); |
| 1547 |
} |
| 1548 |
|
| 1549 |
/* |
| 1550 |
* Remove relation from publication by mapping OID. |
| 1551 |
*/ |
| 1552 |
void |
| 1553 |
RemovePublicationRelById(Oid proid) |
| 1554 |
{ |
| 1555 |
Relation rel; |
| 1556 |
HeapTuple tup; |
| 1557 |
Form_pg_publication_rel pubrel; |
| 1558 |
List *relids = NIL; |
| 1559 |
|
| 1560 |
rel = table_open(PublicationRelRelationId, RowExclusiveLock); |
| 1561 |
|
| 1562 |
tup = SearchSysCache1(PUBLICATIONREL, ObjectIdGetDatum(proid)); |
| 1563 |
|
| 1564 |
if (!HeapTupleIsValid(tup)) |
| 1565 |
elog(ERROR, "cache lookup failed for publication table %u", |
| 1566 |
proid); |
| 1567 |
|
| 1568 |
pubrel = (Form_pg_publication_rel) GETSTRUCT(tup); |
| 1569 |
|
| 1570 |
/* |
| 1571 |
* Invalidate relcache so that publication info is rebuilt. |
| 1572 |
* |
| 1573 |
* For the partitioned tables, we must invalidate all partitions contained |
| 1574 |
* in the respective partition hierarchies, not just the one explicitly |
| 1575 |
* mentioned in the publication. This is required because we implicitly |
| 1576 |
* publish the child tables when the parent table is published. |
| 1577 |
*/ |
| 1578 |
relids = GetPubPartitionOptionRelations(relids, PUBLICATION_PART_ALL, |
| 1579 |
pubrel->prrelid); |
| 1580 |
|
| 1581 |
InvalidatePublicationRels(relids); |
| 1582 |
|
| 1583 |
CatalogTupleDelete(rel, &tup->t_self); |
| 1584 |
|
| 1585 |
ReleaseSysCache(tup); |
| 1586 |
|
| 1587 |
table_close(rel, RowExclusiveLock); |
| 1588 |
} |
| 1589 |
|
| 1590 |
/* |
| 1591 |
* Remove the publication by mapping OID. |
| 1592 |
*/ |
| 1593 |
void |
| 1594 |
RemovePublicationById(Oid pubid) |
| 1595 |
{ |
| 1596 |
Relation rel; |
| 1597 |
HeapTuple tup; |
| 1598 |
Form_pg_publication pubform; |
| 1599 |
|
| 1600 |
rel = table_open(PublicationRelationId, RowExclusiveLock); |
| 1601 |
|
| 1602 |
tup = SearchSysCache1(PUBLICATIONOID, ObjectIdGetDatum(pubid)); |
| 1603 |
if (!HeapTupleIsValid(tup)) |
| 1604 |
elog(ERROR, "cache lookup failed for publication %u", pubid); |
| 1605 |
|
| 1606 |
pubform = (Form_pg_publication) GETSTRUCT(tup); |
| 1607 |
|
| 1608 |
/* Invalidate relcache so that publication info is rebuilt. */ |
| 1609 |
if (pubform->puballtables) |
| 1610 |
CacheInvalidateRelcacheAll(); |
| 1611 |
|
| 1612 |
CatalogTupleDelete(rel, &tup->t_self); |
| 1613 |
|
| 1614 |
ReleaseSysCache(tup); |
| 1615 |
|
| 1616 |
table_close(rel, RowExclusiveLock); |
| 1617 |
} |
| 1618 |
|
| 1619 |
/* |
| 1620 |
* Remove schema from publication by mapping OID. |
| 1621 |
*/ |
| 1622 |
void |
| 1623 |
RemovePublicationSchemaById(Oid psoid) |
| 1624 |
{ |
| 1625 |
Relation rel; |
| 1626 |
HeapTuple tup; |
| 1627 |
List *schemaRels = NIL; |
| 1628 |
Form_pg_publication_namespace pubsch; |
| 1629 |
|
| 1630 |
rel = table_open(PublicationNamespaceRelationId, RowExclusiveLock); |
| 1631 |
|
| 1632 |
tup = SearchSysCache1(PUBLICATIONNAMESPACE, ObjectIdGetDatum(psoid)); |
| 1633 |
|
| 1634 |
if (!HeapTupleIsValid(tup)) |
| 1635 |
elog(ERROR, "cache lookup failed for publication schema %u", psoid); |
| 1636 |
|
| 1637 |
pubsch = (Form_pg_publication_namespace) GETSTRUCT(tup); |
| 1638 |
|
| 1639 |
/* |
| 1640 |
* Invalidate relcache so that publication info is rebuilt. See |
| 1641 |
* RemovePublicationRelById for why we need to consider all the |
| 1642 |
* partitions. |
| 1643 |
*/ |
| 1644 |
schemaRels = GetSchemaPublicationRelations(pubsch->pnnspid, |
| 1645 |
PUBLICATION_PART_ALL); |
| 1646 |
InvalidatePublicationRels(schemaRels); |
| 1647 |
|
| 1648 |
CatalogTupleDelete(rel, &tup->t_self); |
| 1649 |
|
| 1650 |
ReleaseSysCache(tup); |
| 1651 |
|
| 1652 |
table_close(rel, RowExclusiveLock); |
| 1653 |
} |
| 1654 |
|
| 1655 |
/* |
| 1656 |
* Open relations specified by a PublicationTable list. |
| 1657 |
* The returned tables are locked in ShareUpdateExclusiveLock mode in order to |
| 1658 |
* add them to a publication. |
| 1659 |
*/ |
| 1660 |
static List * |
| 1661 |
OpenTableList(List *tables) |
| 1662 |
{ |
| 1663 |
List *relids = NIL; |
| 1664 |
List *rels = NIL; |
| 1665 |
ListCell *lc; |
| 1666 |
List *relids_with_rf = NIL; |
| 1667 |
List *relids_with_collist = NIL; |
| 1668 |
|
| 1669 |
/* |
| 1670 |
* Open, share-lock, and check all the explicitly-specified relations |
| 1671 |
*/ |
| 1672 |
foreach(lc, tables) |
| 1673 |
{ |
| 1674 |
PublicationTable *t = lfirst_node(PublicationTable, lc); |
| 1675 |
bool recurse = t->relation->inh; |
| 1676 |
Relation rel; |
| 1677 |
Oid myrelid; |
| 1678 |
PublicationRelInfo *pub_rel; |
| 1679 |
|
| 1680 |
/* Allow query cancel in case this takes a long time */ |
| 1681 |
CHECK_FOR_INTERRUPTS(); |
| 1682 |
|
| 1683 |
rel = table_openrv(t->relation, ShareUpdateExclusiveLock); |
| 1684 |
myrelid = RelationGetRelid(rel); |
| 1685 |
|
| 1686 |
/* |
| 1687 |
* Filter out duplicates if user specifies "foo, foo". |
| 1688 |
* |
| 1689 |
* Note that this algorithm is known to not be very efficient (O(N^2)) |
| 1690 |
* but given that it only works on list of tables given to us by user |
| 1691 |
* it's deemed acceptable. |
| 1692 |
*/ |
| 1693 |
if (list_member_oid(relids, myrelid)) |
| 1694 |
{ |
| 1695 |
/* Disallow duplicate tables if there are any with row filters. */ |
| 1696 |
if (t->whereClause || list_member_oid(relids_with_rf, myrelid)) |
| 1697 |
ereport(ERROR, |
| 1698 |
(errcode(ERRCODE_DUPLICATE_OBJECT), |
| 1699 |
errmsg("conflicting or redundant WHERE clauses for table \"%s\"", |
| 1700 |
RelationGetRelationName(rel)))); |
| 1701 |
|
| 1702 |
/* Disallow duplicate tables if there are any with column lists. */ |
| 1703 |
if (t->columns || list_member_oid(relids_with_collist, myrelid)) |
| 1704 |
ereport(ERROR, |
| 1705 |
(errcode(ERRCODE_DUPLICATE_OBJECT), |
| 1706 |
errmsg("conflicting or redundant column lists for table \"%s\"", |
| 1707 |
RelationGetRelationName(rel)))); |
| 1708 |
|
| 1709 |
table_close(rel, ShareUpdateExclusiveLock); |
| 1710 |
continue; |
| 1711 |
} |
| 1712 |
|
| 1713 |
pub_rel = palloc(sizeof(PublicationRelInfo)); |
| 1714 |
pub_rel->relation = rel; |
| 1715 |
pub_rel->whereClause = t->whereClause; |
| 1716 |
pub_rel->columns = t->columns; |
| 1717 |
rels = lappend(rels, pub_rel); |
| 1718 |
relids = lappend_oid(relids, myrelid); |
| 1719 |
|
| 1720 |
if (t->whereClause) |
| 1721 |
relids_with_rf = lappend_oid(relids_with_rf, myrelid); |
| 1722 |
|
| 1723 |
if (t->columns) |
| 1724 |
relids_with_collist = lappend_oid(relids_with_collist, myrelid); |
| 1725 |
|
| 1726 |
/* |
| 1727 |
* Add children of this rel, if requested, so that they too are added |
| 1728 |
* to the publication. A partitioned table can't have any inheritance |
| 1729 |
* children other than its partitions, which need not be explicitly |
| 1730 |
* added to the publication. |
| 1731 |
*/ |
| 1732 |
if (recurse && rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE) |
| 1733 |
{ |
| 1734 |
List *children; |
| 1735 |
ListCell *child; |
| 1736 |
|
| 1737 |
children = find_all_inheritors(myrelid, ShareUpdateExclusiveLock, |
| 1738 |
NULL); |
| 1739 |
|
| 1740 |
foreach(child, children) |
| 1741 |
{ |
| 1742 |
Oid childrelid = lfirst_oid(child); |
| 1743 |
|
| 1744 |
/* Allow query cancel in case this takes a long time */ |
| 1745 |
CHECK_FOR_INTERRUPTS(); |
| 1746 |
|
| 1747 |
/* |
| 1748 |
* Skip duplicates if user specified both parent and child |
| 1749 |
* tables. |
| 1750 |
*/ |
| 1751 |
if (list_member_oid(relids, childrelid)) |
| 1752 |
{ |
| 1753 |
/* |
| 1754 |
* We don't allow to specify row filter for both parent |
| 1755 |
* and child table at the same time as it is not very |
| 1756 |
* clear which one should be given preference. |
| 1757 |
*/ |
| 1758 |
if (childrelid != myrelid && |
| 1759 |
(t->whereClause || list_member_oid(relids_with_rf, childrelid))) |
| 1760 |
ereport(ERROR, |
| 1761 |
(errcode(ERRCODE_DUPLICATE_OBJECT), |
| 1762 |
errmsg("conflicting or redundant WHERE clauses for table \"%s\"", |
| 1763 |
RelationGetRelationName(rel)))); |
| 1764 |
|
| 1765 |
/* |
| 1766 |
* We don't allow to specify column list for both parent |
| 1767 |
* and child table at the same time as it is not very |
| 1768 |
* clear which one should be given preference. |
| 1769 |
*/ |
| 1770 |
if (childrelid != myrelid && |
| 1771 |
(t->columns || list_member_oid(relids_with_collist, childrelid))) |
| 1772 |
ereport(ERROR, |
| 1773 |
(errcode(ERRCODE_DUPLICATE_OBJECT), |
| 1774 |
errmsg("conflicting or redundant column lists for table \"%s\"", |
| 1775 |
RelationGetRelationName(rel)))); |
| 1776 |
|
| 1777 |
continue; |
| 1778 |
} |
| 1779 |
|
| 1780 |
/* find_all_inheritors already got lock */ |
| 1781 |
rel = table_open(childrelid, NoLock); |
| 1782 |
pub_rel = palloc(sizeof(PublicationRelInfo)); |
| 1783 |
pub_rel->relation = rel; |
| 1784 |
/* child inherits WHERE clause from parent */ |
| 1785 |
pub_rel->whereClause = t->whereClause; |
| 1786 |
|
| 1787 |
/* child inherits column list from parent */ |
| 1788 |
pub_rel->columns = t->columns; |
| 1789 |
rels = lappend(rels, pub_rel); |
| 1790 |
relids = lappend_oid(relids, childrelid); |
| 1791 |
|
| 1792 |
if (t->whereClause) |
| 1793 |
relids_with_rf = lappend_oid(relids_with_rf, childrelid); |
| 1794 |
|
| 1795 |
if (t->columns) |
| 1796 |
relids_with_collist = lappend_oid(relids_with_collist, childrelid); |
| 1797 |
} |
| 1798 |
} |
| 1799 |
} |
| 1800 |
|
| 1801 |
list_free(relids); |
| 1802 |
list_free(relids_with_rf); |
| 1803 |
|
| 1804 |
return rels; |
| 1805 |
} |
| 1806 |
|
| 1807 |
/* |
| 1808 |
* Close all relations in the list. |
| 1809 |
*/ |
| 1810 |
static void |
| 1811 |
CloseTableList(List *rels) |
| 1812 |
{ |
| 1813 |
ListCell *lc; |
| 1814 |
|
| 1815 |
foreach(lc, rels) |
| 1816 |
{ |
| 1817 |
PublicationRelInfo *pub_rel; |
| 1818 |
|
| 1819 |
pub_rel = (PublicationRelInfo *) lfirst(lc); |
| 1820 |
table_close(pub_rel->relation, NoLock); |
| 1821 |
} |
| 1822 |
|
| 1823 |
list_free_deep(rels); |
| 1824 |
} |
| 1825 |
|
| 1826 |
/* |
| 1827 |
* Lock the schemas specified in the schema list in AccessShareLock mode in |
| 1828 |
* order to prevent concurrent schema deletion. |
| 1829 |
*/ |
| 1830 |
static void |
| 1831 |
LockSchemaList(List *schemalist) |
| 1832 |
{ |
| 1833 |
ListCell *lc; |
| 1834 |
|
| 1835 |
foreach(lc, schemalist) |
| 1836 |
{ |
| 1837 |
Oid schemaid = lfirst_oid(lc); |
| 1838 |
|
| 1839 |
/* Allow query cancel in case this takes a long time */ |
| 1840 |
CHECK_FOR_INTERRUPTS(); |
| 1841 |
LockDatabaseObject(NamespaceRelationId, schemaid, 0, AccessShareLock); |
| 1842 |
|
| 1843 |
/* |
| 1844 |
* It is possible that by the time we acquire the lock on schema, |
| 1845 |
* concurrent DDL has removed it. We can test this by checking the |
| 1846 |
* existence of schema. |
| 1847 |
*/ |
| 1848 |
if (!SearchSysCacheExists1(NAMESPACEOID, ObjectIdGetDatum(schemaid))) |
| 1849 |
ereport(ERROR, |
| 1850 |
errcode(ERRCODE_UNDEFINED_SCHEMA), |
| 1851 |
errmsg("schema with OID %u does not exist", schemaid)); |
| 1852 |
} |
| 1853 |
} |
| 1854 |
|
| 1855 |
/* |
| 1856 |
* Add listed tables to the publication. |
| 1857 |
*/ |
| 1858 |
static void |
| 1859 |
PublicationAddTables(Oid pubid, List *rels, bool if_not_exists, |
| 1860 |
AlterPublicationStmt *stmt) |
| 1861 |
{ |
| 1862 |
ListCell *lc; |
| 1863 |
|
| 1864 |
Assert(!stmt || !stmt->for_all_tables); |
| 1865 |
|
| 1866 |
foreach(lc, rels) |
| 1867 |
{ |
| 1868 |
PublicationRelInfo *pub_rel = (PublicationRelInfo *) lfirst(lc); |
| 1869 |
Relation rel = pub_rel->relation; |
| 1870 |
ObjectAddress obj; |
| 1871 |
|
| 1872 |
/* Must be owner of the table or superuser. */ |
| 1873 |
if (!object_ownercheck(RelationRelationId, RelationGetRelid(rel), GetUserId())) |
| 1874 |
aclcheck_error(ACLCHECK_NOT_OWNER, get_relkind_objtype(rel->rd_rel->relkind), |
| 1875 |
RelationGetRelationName(rel)); |
| 1876 |
|
| 1877 |
obj = publication_add_relation(pubid, pub_rel, if_not_exists); |
| 1878 |
if (stmt) |
| 1879 |
{ |
| 1880 |
EventTriggerCollectSimpleCommand(obj, InvalidObjectAddress, |
| 1881 |
(Node *) stmt); |
| 1882 |
|
| 1883 |
InvokeObjectPostCreateHook(PublicationRelRelationId, |
| 1884 |
obj.objectId, 0); |
| 1885 |
} |
| 1886 |
} |
| 1887 |
} |
| 1888 |
|
| 1889 |
/* |
| 1890 |
* Remove listed tables from the publication. |
| 1891 |
*/ |
| 1892 |
static void |
| 1893 |
PublicationDropTables(Oid pubid, List *rels, bool missing_ok) |
| 1894 |
{ |
| 1895 |
ObjectAddress obj; |
| 1896 |
ListCell *lc; |
| 1897 |
Oid prid; |
| 1898 |
|
| 1899 |
foreach(lc, rels) |
| 1900 |
{ |
| 1901 |
PublicationRelInfo *pubrel = (PublicationRelInfo *) lfirst(lc); |
| 1902 |
Relation rel = pubrel->relation; |
| 1903 |
Oid relid = RelationGetRelid(rel); |
| 1904 |
|
| 1905 |
if (pubrel->columns) |
| 1906 |
ereport(ERROR, |
| 1907 |
errcode(ERRCODE_SYNTAX_ERROR), |
| 1908 |
errmsg("column list must not be specified in ALTER PUBLICATION ... DROP")); |
| 1909 |
|
| 1910 |
prid = GetSysCacheOid2(PUBLICATIONRELMAP, Anum_pg_publication_rel_oid, |
| 1911 |
ObjectIdGetDatum(relid), |
| 1912 |
ObjectIdGetDatum(pubid)); |
| 1913 |
if (!OidIsValid(prid)) |
| 1914 |
{ |
| 1915 |
if (missing_ok) |
| 1916 |
continue; |
| 1917 |
|
| 1918 |
ereport(ERROR, |
| 1919 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 1920 |
errmsg("relation \"%s\" is not part of the publication", |
| 1921 |
RelationGetRelationName(rel)))); |
| 1922 |
} |
| 1923 |
|
| 1924 |
if (pubrel->whereClause) |
| 1925 |
ereport(ERROR, |
| 1926 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 1927 |
errmsg("cannot use a WHERE clause when removing a table from a publication"))); |
| 1928 |
|
| 1929 |
ObjectAddressSet(obj, PublicationRelRelationId, prid); |
| 1930 |
performDeletion(&obj, DROP_CASCADE, 0); |
| 1931 |
} |
| 1932 |
} |
| 1933 |
|
| 1934 |
/* |
| 1935 |
* Add listed schemas to the publication. |
| 1936 |
*/ |
| 1937 |
static void |
| 1938 |
PublicationAddSchemas(Oid pubid, List *schemas, bool if_not_exists, |
| 1939 |
AlterPublicationStmt *stmt) |
| 1940 |
{ |
| 1941 |
ListCell *lc; |
| 1942 |
|
| 1943 |
Assert(!stmt || !stmt->for_all_tables); |
| 1944 |
|
| 1945 |
foreach(lc, schemas) |
| 1946 |
{ |
| 1947 |
Oid schemaid = lfirst_oid(lc); |
| 1948 |
ObjectAddress obj; |
| 1949 |
|
| 1950 |
obj = publication_add_schema(pubid, schemaid, if_not_exists); |
| 1951 |
if (stmt) |
| 1952 |
{ |
| 1953 |
EventTriggerCollectSimpleCommand(obj, InvalidObjectAddress, |
| 1954 |
(Node *) stmt); |
| 1955 |
|
| 1956 |
InvokeObjectPostCreateHook(PublicationNamespaceRelationId, |
| 1957 |
obj.objectId, 0); |
| 1958 |
} |
| 1959 |
} |
| 1960 |
} |
| 1961 |
|
| 1962 |
/* |
| 1963 |
* Remove listed schemas from the publication. |
| 1964 |
*/ |
| 1965 |
static void |
| 1966 |
PublicationDropSchemas(Oid pubid, List *schemas, bool missing_ok) |
| 1967 |
{ |
| 1968 |
ObjectAddress obj; |
| 1969 |
ListCell *lc; |
| 1970 |
Oid psid; |
| 1971 |
|
| 1972 |
foreach(lc, schemas) |
| 1973 |
{ |
| 1974 |
Oid schemaid = lfirst_oid(lc); |
| 1975 |
|
| 1976 |
psid = GetSysCacheOid2(PUBLICATIONNAMESPACEMAP, |
| 1977 |
Anum_pg_publication_namespace_oid, |
| 1978 |
ObjectIdGetDatum(schemaid), |
| 1979 |
ObjectIdGetDatum(pubid)); |
| 1980 |
if (!OidIsValid(psid)) |
| 1981 |
{ |
| 1982 |
if (missing_ok) |
| 1983 |
continue; |
| 1984 |
|
| 1985 |
ereport(ERROR, |
| 1986 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 1987 |
errmsg("tables from schema \"%s\" are not part of the publication", |
| 1988 |
get_namespace_name(schemaid)))); |
| 1989 |
} |
| 1990 |
|
| 1991 |
ObjectAddressSet(obj, PublicationNamespaceRelationId, psid); |
| 1992 |
performDeletion(&obj, DROP_CASCADE, 0); |
| 1993 |
} |
| 1994 |
} |
| 1995 |
|
| 1996 |
/* |
| 1997 |
* Internal workhorse for changing a publication owner |
| 1998 |
*/ |
| 1999 |
static void |
| 2000 |
AlterPublicationOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId) |
| 2001 |
{ |
| 2002 |
Form_pg_publication form; |
| 2003 |
|
| 2004 |
form = (Form_pg_publication) GETSTRUCT(tup); |
| 2005 |
|
| 2006 |
if (form->pubowner == newOwnerId) |
| 2007 |
return; |
| 2008 |
|
| 2009 |
if (!superuser()) |
| 2010 |
{ |
| 2011 |
AclResult aclresult; |
| 2012 |
|
| 2013 |
/* Must be owner */ |
| 2014 |
if (!object_ownercheck(PublicationRelationId, form->oid, GetUserId())) |
| 2015 |
aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_PUBLICATION, |
| 2016 |
NameStr(form->pubname)); |
| 2017 |
|
| 2018 |
/* Must be able to become new owner */ |
| 2019 |
check_can_set_role(GetUserId(), newOwnerId); |
| 2020 |
|
| 2021 |
/* New owner must have CREATE privilege on database */ |
| 2022 |
aclresult = object_aclcheck(DatabaseRelationId, MyDatabaseId, newOwnerId, ACL_CREATE); |
| 2023 |
if (aclresult != ACLCHECK_OK) |
| 2024 |
aclcheck_error(aclresult, OBJECT_DATABASE, |
| 2025 |
get_database_name(MyDatabaseId)); |
| 2026 |
|
| 2027 |
if (form->puballtables && !superuser_arg(newOwnerId)) |
| 2028 |
ereport(ERROR, |
| 2029 |
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 2030 |
errmsg("permission denied to change owner of publication \"%s\"", |
| 2031 |
NameStr(form->pubname)), |
| 2032 |
errhint("The owner of a FOR ALL TABLES publication must be a superuser."))); |
| 2033 |
|
| 2034 |
if (!superuser_arg(newOwnerId) && is_schema_publication(form->oid)) |
| 2035 |
ereport(ERROR, |
| 2036 |
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 2037 |
errmsg("permission denied to change owner of publication \"%s\"", |
| 2038 |
NameStr(form->pubname)), |
| 2039 |
errhint("The owner of a FOR TABLES IN SCHEMA publication must be a superuser."))); |
| 2040 |
} |
| 2041 |
|
| 2042 |
form->pubowner = newOwnerId; |
| 2043 |
CatalogTupleUpdate(rel, &tup->t_self, tup); |
| 2044 |
|
| 2045 |
/* Update owner dependency reference */ |
| 2046 |
changeDependencyOnOwner(PublicationRelationId, |
| 2047 |
form->oid, |
| 2048 |
newOwnerId); |
| 2049 |
|
| 2050 |
InvokeObjectPostAlterHook(PublicationRelationId, |
| 2051 |
form->oid, 0); |
| 2052 |
} |
| 2053 |
|
| 2054 |
/* |
| 2055 |
* Change publication owner -- by name |
| 2056 |
*/ |
| 2057 |
ObjectAddress |
| 2058 |
AlterPublicationOwner(const char *name, Oid newOwnerId) |
| 2059 |
{ |
| 2060 |
Oid pubid; |
| 2061 |
HeapTuple tup; |
| 2062 |
Relation rel; |
| 2063 |
ObjectAddress address; |
| 2064 |
Form_pg_publication pubform; |
| 2065 |
|
| 2066 |
rel = table_open(PublicationRelationId, RowExclusiveLock); |
| 2067 |
|
| 2068 |
tup = SearchSysCacheCopy1(PUBLICATIONNAME, CStringGetDatum(name)); |
| 2069 |
|
| 2070 |
if (!HeapTupleIsValid(tup)) |
| 2071 |
ereport(ERROR, |
| 2072 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 2073 |
errmsg("publication \"%s\" does not exist", name))); |
| 2074 |
|
| 2075 |
pubform = (Form_pg_publication) GETSTRUCT(tup); |
| 2076 |
pubid = pubform->oid; |
| 2077 |
|
| 2078 |
AlterPublicationOwner_internal(rel, tup, newOwnerId); |
| 2079 |
|
| 2080 |
ObjectAddressSet(address, PublicationRelationId, pubid); |
| 2081 |
|
| 2082 |
heap_freetuple(tup); |
| 2083 |
|
| 2084 |
table_close(rel, RowExclusiveLock); |
| 2085 |
|
| 2086 |
return address; |
| 2087 |
} |
| 2088 |
|
| 2089 |
/* |
| 2090 |
* Change publication owner -- by OID |
| 2091 |
*/ |
| 2092 |
void |
| 2093 |
AlterPublicationOwner_oid(Oid pubid, Oid newOwnerId) |
| 2094 |
{ |
| 2095 |
HeapTuple tup; |
| 2096 |
Relation rel; |
| 2097 |
|
| 2098 |
rel = table_open(PublicationRelationId, RowExclusiveLock); |
| 2099 |
|
| 2100 |
tup = SearchSysCacheCopy1(PUBLICATIONOID, ObjectIdGetDatum(pubid)); |
| 2101 |
|
| 2102 |
if (!HeapTupleIsValid(tup)) |
| 2103 |
ereport(ERROR, |
| 2104 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 2105 |
errmsg("publication with OID %u does not exist", pubid))); |
| 2106 |
|
| 2107 |
AlterPublicationOwner_internal(rel, tup, newOwnerId); |
| 2108 |
|
| 2109 |
heap_freetuple(tup); |
| 2110 |
|
| 2111 |
table_close(rel, RowExclusiveLock); |
| 2112 |
} |
| 2113 |
|
| 2114 |
/* |
| 2115 |
* Extract the publish_generated_columns option value from a DefElem. "stored" |
| 2116 |
* and "none" values are accepted. |
| 2117 |
*/ |
| 2118 |
static char |
| 2119 |
defGetGeneratedColsOption(DefElem *def) |
| 2120 |
{ |
| 2121 |
char *sval = ""; |
| 2122 |
|
| 2123 |
/* |
| 2124 |
* A parameter value is required. |
| 2125 |
*/ |
| 2126 |
if (def->arg) |
| 2127 |
{ |
| 2128 |
sval = defGetString(def); |
| 2129 |
|
| 2130 |
if (pg_strcasecmp(sval, "none") == 0) |
| 2131 |
return PUBLISH_GENCOLS_NONE; |
| 2132 |
if (pg_strcasecmp(sval, "stored") == 0) |
| 2133 |
return PUBLISH_GENCOLS_STORED; |
| 2134 |
} |
| 2135 |
|
| 2136 |
ereport(ERROR, |
| 2137 |
errcode(ERRCODE_SYNTAX_ERROR), |
| 2138 |
errmsg("invalid value for publication parameter \"%s\": \"%s\"", def->defname, sval), |
| 2139 |
errdetail("Valid values are \"%s\" and \"%s\".", "none", "stored")); |
| 2140 |
|
| 2141 |
return PUBLISH_GENCOLS_NONE; /* keep compiler quiet */ |
| 2142 |
} |