| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* pg_publication.c |
| 4 |
* publication C API 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/catalog/pg_publication.c |
| 11 |
* |
| 12 |
*------------------------------------------------------------------------- |
| 13 |
*/ |
| 14 |
|
| 15 |
#include "postgres.h" |
| 16 |
|
| 17 |
#include "access/genam.h" |
| 18 |
#include "access/heapam.h" |
| 19 |
#include "access/htup_details.h" |
| 20 |
#include "access/tableam.h" |
| 21 |
#include "catalog/catalog.h" |
| 22 |
#include "catalog/dependency.h" |
| 23 |
#include "catalog/indexing.h" |
| 24 |
#include "catalog/namespace.h" |
| 25 |
#include "catalog/objectaddress.h" |
| 26 |
#include "catalog/partition.h" |
| 27 |
#include "catalog/pg_inherits.h" |
| 28 |
#include "catalog/pg_namespace.h" |
| 29 |
#include "catalog/pg_publication.h" |
| 30 |
#include "catalog/pg_publication_namespace.h" |
| 31 |
#include "catalog/pg_publication_rel.h" |
| 32 |
#include "catalog/pg_type.h" |
| 33 |
#include "commands/publicationcmds.h" |
| 34 |
#include "funcapi.h" |
| 35 |
#include "miscadmin.h" |
| 36 |
#include "utils/array.h" |
| 37 |
#include "utils/builtins.h" |
| 38 |
#include "utils/catcache.h" |
| 39 |
#include "utils/fmgroids.h" |
| 40 |
#include "utils/lsyscache.h" |
| 41 |
#include "utils/rel.h" |
| 42 |
#include "utils/syscache.h" |
| 43 |
|
| 44 |
/* Records association between publication and published table */ |
| 45 |
typedef struct |
| 46 |
{ |
| 47 |
Oid relid; /* OID of published table */ |
| 48 |
Oid pubid; /* OID of publication that publishes this |
| 49 |
* table. */ |
| 50 |
} published_rel; |
| 51 |
|
| 52 |
/* |
| 53 |
* Check if relation can be in given publication and throws appropriate |
| 54 |
* error if not. |
| 55 |
*/ |
| 56 |
static void |
| 57 |
check_publication_add_relation(Relation targetrel) |
| 58 |
{ |
| 59 |
/* Must be a regular or partitioned table */ |
| 60 |
if (RelationGetForm(targetrel)->relkind != RELKIND_RELATION && |
| 61 |
RelationGetForm(targetrel)->relkind != RELKIND_PARTITIONED_TABLE) |
| 62 |
ereport(ERROR, |
| 63 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 64 |
errmsg("cannot add relation \"%s\" to publication", |
| 65 |
RelationGetRelationName(targetrel)), |
| 66 |
errdetail_relkind_not_supported(RelationGetForm(targetrel)->relkind))); |
| 67 |
|
| 68 |
/* Can't be system table */ |
| 69 |
if (IsCatalogRelation(targetrel)) |
| 70 |
ereport(ERROR, |
| 71 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 72 |
errmsg("cannot add relation \"%s\" to publication", |
| 73 |
RelationGetRelationName(targetrel)), |
| 74 |
errdetail("This operation is not supported for system tables."))); |
| 75 |
|
| 76 |
/* UNLOGGED and TEMP relations cannot be part of publication. */ |
| 77 |
if (targetrel->rd_rel->relpersistence == RELPERSISTENCE_TEMP) |
| 78 |
ereport(ERROR, |
| 79 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 80 |
errmsg("cannot add relation \"%s\" to publication", |
| 81 |
RelationGetRelationName(targetrel)), |
| 82 |
errdetail("This operation is not supported for temporary tables."))); |
| 83 |
else if (targetrel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED) |
| 84 |
ereport(ERROR, |
| 85 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 86 |
errmsg("cannot add relation \"%s\" to publication", |
| 87 |
RelationGetRelationName(targetrel)), |
| 88 |
errdetail("This operation is not supported for unlogged tables."))); |
| 89 |
} |
| 90 |
|
| 91 |
/* |
| 92 |
* Check if schema can be in given publication and throw appropriate error if |
| 93 |
* not. |
| 94 |
*/ |
| 95 |
static void |
| 96 |
check_publication_add_schema(Oid schemaid) |
| 97 |
{ |
| 98 |
/* Can't be system namespace */ |
| 99 |
if (IsCatalogNamespace(schemaid) || IsToastNamespace(schemaid)) |
| 100 |
ereport(ERROR, |
| 101 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 102 |
errmsg("cannot add schema \"%s\" to publication", |
| 103 |
get_namespace_name(schemaid)), |
| 104 |
errdetail("This operation is not supported for system schemas."))); |
| 105 |
|
| 106 |
/* Can't be temporary namespace */ |
| 107 |
if (isAnyTempNamespace(schemaid)) |
| 108 |
ereport(ERROR, |
| 109 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 110 |
errmsg("cannot add schema \"%s\" to publication", |
| 111 |
get_namespace_name(schemaid)), |
| 112 |
errdetail("Temporary schemas cannot be replicated."))); |
| 113 |
} |
| 114 |
|
| 115 |
/* |
| 116 |
* Returns if relation represented by oid and Form_pg_class entry |
| 117 |
* is publishable. |
| 118 |
* |
| 119 |
* Does same checks as check_publication_add_relation() above, but does not |
| 120 |
* need relation to be opened and also does not throw errors. |
| 121 |
* |
| 122 |
* XXX This also excludes all tables with relid < FirstNormalObjectId, |
| 123 |
* ie all tables created during initdb. This mainly affects the preinstalled |
| 124 |
* information_schema. IsCatalogRelationOid() only excludes tables with |
| 125 |
* relid < FirstUnpinnedObjectId, making that test rather redundant, |
| 126 |
* but really we should get rid of the FirstNormalObjectId test not |
| 127 |
* IsCatalogRelationOid. We can't do so today because we don't want |
| 128 |
* information_schema tables to be considered publishable; but this test |
| 129 |
* is really inadequate for that, since the information_schema could be |
| 130 |
* dropped and reloaded and then it'll be considered publishable. The best |
| 131 |
* long-term solution may be to add a "relispublishable" bool to pg_class, |
| 132 |
* and depend on that instead of OID checks. |
| 133 |
*/ |
| 134 |
static bool |
| 135 |
is_publishable_class(Oid relid, Form_pg_class reltuple) |
| 136 |
{ |
| 137 |
return (reltuple->relkind == RELKIND_RELATION || |
| 138 |
reltuple->relkind == RELKIND_PARTITIONED_TABLE) && |
| 139 |
!IsCatalogRelationOid(relid) && |
| 140 |
reltuple->relpersistence == RELPERSISTENCE_PERMANENT && |
| 141 |
relid >= FirstNormalObjectId; |
| 142 |
} |
| 143 |
|
| 144 |
/* |
| 145 |
* Another variant of is_publishable_class(), taking a Relation. |
| 146 |
*/ |
| 147 |
bool |
| 148 |
is_publishable_relation(Relation rel) |
| 149 |
{ |
| 150 |
return is_publishable_class(RelationGetRelid(rel), rel->rd_rel); |
| 151 |
} |
| 152 |
|
| 153 |
/* |
| 154 |
* SQL-callable variant of the above |
| 155 |
* |
| 156 |
* This returns null when the relation does not exist. This is intended to be |
| 157 |
* used for example in psql to avoid gratuitous errors when there are |
| 158 |
* concurrent catalog changes. |
| 159 |
*/ |
| 160 |
Datum |
| 161 |
pg_relation_is_publishable(PG_FUNCTION_ARGS) |
| 162 |
{ |
| 163 |
Oid relid = PG_GETARG_OID(0); |
| 164 |
HeapTuple tuple; |
| 165 |
bool result; |
| 166 |
|
| 167 |
tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid)); |
| 168 |
if (!HeapTupleIsValid(tuple)) |
| 169 |
PG_RETURN_NULL(); |
| 170 |
result = is_publishable_class(relid, (Form_pg_class) GETSTRUCT(tuple)); |
| 171 |
ReleaseSysCache(tuple); |
| 172 |
PG_RETURN_BOOL(result); |
| 173 |
} |
| 174 |
|
| 175 |
/* |
| 176 |
* Returns true if the ancestor is in the list of published relations. |
| 177 |
* Otherwise, returns false. |
| 178 |
*/ |
| 179 |
static bool |
| 180 |
is_ancestor_member_tableinfos(Oid ancestor, List *table_infos) |
| 181 |
{ |
| 182 |
ListCell *lc; |
| 183 |
|
| 184 |
foreach(lc, table_infos) |
| 185 |
{ |
| 186 |
Oid relid = ((published_rel *) lfirst(lc))->relid; |
| 187 |
|
| 188 |
if (relid == ancestor) |
| 189 |
return true; |
| 190 |
} |
| 191 |
|
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
/* |
| 196 |
* Filter out the partitions whose parent tables are also present in the list. |
| 197 |
*/ |
| 198 |
static void |
| 199 |
filter_partitions(List *table_infos) |
| 200 |
{ |
| 201 |
ListCell *lc; |
| 202 |
|
| 203 |
foreach(lc, table_infos) |
| 204 |
{ |
| 205 |
bool skip = false; |
| 206 |
List *ancestors = NIL; |
| 207 |
ListCell *lc2; |
| 208 |
published_rel *table_info = (published_rel *) lfirst(lc); |
| 209 |
|
| 210 |
if (get_rel_relispartition(table_info->relid)) |
| 211 |
ancestors = get_partition_ancestors(table_info->relid); |
| 212 |
|
| 213 |
foreach(lc2, ancestors) |
| 214 |
{ |
| 215 |
Oid ancestor = lfirst_oid(lc2); |
| 216 |
|
| 217 |
if (is_ancestor_member_tableinfos(ancestor, table_infos)) |
| 218 |
{ |
| 219 |
skip = true; |
| 220 |
break; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
if (skip) |
| 225 |
table_infos = foreach_delete_current(table_infos, lc); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
/* |
| 230 |
* Returns true if any schema is associated with the publication, false if no |
| 231 |
* schema is associated with the publication. |
| 232 |
*/ |
| 233 |
bool |
| 234 |
is_schema_publication(Oid pubid) |
| 235 |
{ |
| 236 |
Relation pubschsrel; |
| 237 |
ScanKeyData scankey; |
| 238 |
SysScanDesc scan; |
| 239 |
HeapTuple tup; |
| 240 |
bool result = false; |
| 241 |
|
| 242 |
pubschsrel = table_open(PublicationNamespaceRelationId, AccessShareLock); |
| 243 |
ScanKeyInit(&scankey, |
| 244 |
Anum_pg_publication_namespace_pnpubid, |
| 245 |
BTEqualStrategyNumber, F_OIDEQ, |
| 246 |
ObjectIdGetDatum(pubid)); |
| 247 |
|
| 248 |
scan = systable_beginscan(pubschsrel, |
| 249 |
PublicationNamespacePnnspidPnpubidIndexId, |
| 250 |
true, NULL, 1, &scankey); |
| 251 |
tup = systable_getnext(scan); |
| 252 |
result = HeapTupleIsValid(tup); |
| 253 |
|
| 254 |
systable_endscan(scan); |
| 255 |
table_close(pubschsrel, AccessShareLock); |
| 256 |
|
| 257 |
return result; |
| 258 |
} |
| 259 |
|
| 260 |
/* |
| 261 |
* Returns true if the relation has column list associated with the |
| 262 |
* publication, false otherwise. |
| 263 |
* |
| 264 |
* If a column list is found, the corresponding bitmap is returned through the |
| 265 |
* cols parameter, if provided. The bitmap is constructed within the given |
| 266 |
* memory context (mcxt). |
| 267 |
*/ |
| 268 |
bool |
| 269 |
check_and_fetch_column_list(Publication *pub, Oid relid, MemoryContext mcxt, |
| 270 |
Bitmapset **cols) |
| 271 |
{ |
| 272 |
HeapTuple cftuple; |
| 273 |
bool found = false; |
| 274 |
|
| 275 |
if (pub->alltables) |
| 276 |
return false; |
| 277 |
|
| 278 |
cftuple = SearchSysCache2(PUBLICATIONRELMAP, |
| 279 |
ObjectIdGetDatum(relid), |
| 280 |
ObjectIdGetDatum(pub->oid)); |
| 281 |
if (HeapTupleIsValid(cftuple)) |
| 282 |
{ |
| 283 |
Datum cfdatum; |
| 284 |
bool isnull; |
| 285 |
|
| 286 |
/* Lookup the column list attribute. */ |
| 287 |
cfdatum = SysCacheGetAttr(PUBLICATIONRELMAP, cftuple, |
| 288 |
Anum_pg_publication_rel_prattrs, &isnull); |
| 289 |
|
| 290 |
/* Was a column list found? */ |
| 291 |
if (!isnull) |
| 292 |
{ |
| 293 |
/* Build the column list bitmap in the given memory context. */ |
| 294 |
if (cols) |
| 295 |
*cols = pub_collist_to_bitmapset(*cols, cfdatum, mcxt); |
| 296 |
|
| 297 |
found = true; |
| 298 |
} |
| 299 |
|
| 300 |
ReleaseSysCache(cftuple); |
| 301 |
} |
| 302 |
|
| 303 |
return found; |
| 304 |
} |
| 305 |
|
| 306 |
/* |
| 307 |
* Gets the relations based on the publication partition option for a specified |
| 308 |
* relation. |
| 309 |
*/ |
| 310 |
List * |
| 311 |
GetPubPartitionOptionRelations(List *result, PublicationPartOpt pub_partopt, |
| 312 |
Oid relid) |
| 313 |
{ |
| 314 |
if (get_rel_relkind(relid) == RELKIND_PARTITIONED_TABLE && |
| 315 |
pub_partopt != PUBLICATION_PART_ROOT) |
| 316 |
{ |
| 317 |
List *all_parts = find_all_inheritors(relid, NoLock, |
| 318 |
NULL); |
| 319 |
|
| 320 |
if (pub_partopt == PUBLICATION_PART_ALL) |
| 321 |
result = list_concat(result, all_parts); |
| 322 |
else if (pub_partopt == PUBLICATION_PART_LEAF) |
| 323 |
{ |
| 324 |
ListCell *lc; |
| 325 |
|
| 326 |
foreach(lc, all_parts) |
| 327 |
{ |
| 328 |
Oid partOid = lfirst_oid(lc); |
| 329 |
|
| 330 |
if (get_rel_relkind(partOid) != RELKIND_PARTITIONED_TABLE) |
| 331 |
result = lappend_oid(result, partOid); |
| 332 |
} |
| 333 |
} |
| 334 |
else |
| 335 |
Assert(false); |
| 336 |
} |
| 337 |
else |
| 338 |
result = lappend_oid(result, relid); |
| 339 |
|
| 340 |
return result; |
| 341 |
} |
| 342 |
|
| 343 |
/* |
| 344 |
* Returns the relid of the topmost ancestor that is published via this |
| 345 |
* publication if any and set its ancestor level to ancestor_level, |
| 346 |
* otherwise returns InvalidOid. |
| 347 |
* |
| 348 |
* The ancestor_level value allows us to compare the results for multiple |
| 349 |
* publications, and decide which value is higher up. |
| 350 |
* |
| 351 |
* Note that the list of ancestors should be ordered such that the topmost |
| 352 |
* ancestor is at the end of the list. |
| 353 |
*/ |
| 354 |
Oid |
| 355 |
GetTopMostAncestorInPublication(Oid puboid, List *ancestors, int *ancestor_level) |
| 356 |
{ |
| 357 |
ListCell *lc; |
| 358 |
Oid topmost_relid = InvalidOid; |
| 359 |
int level = 0; |
| 360 |
|
| 361 |
/* |
| 362 |
* Find the "topmost" ancestor that is in this publication. |
| 363 |
*/ |
| 364 |
foreach(lc, ancestors) |
| 365 |
{ |
| 366 |
Oid ancestor = lfirst_oid(lc); |
| 367 |
List *apubids = GetRelationPublications(ancestor); |
| 368 |
List *aschemaPubids = NIL; |
| 369 |
|
| 370 |
level++; |
| 371 |
|
| 372 |
if (list_member_oid(apubids, puboid)) |
| 373 |
{ |
| 374 |
topmost_relid = ancestor; |
| 375 |
|
| 376 |
if (ancestor_level) |
| 377 |
*ancestor_level = level; |
| 378 |
} |
| 379 |
else |
| 380 |
{ |
| 381 |
aschemaPubids = GetSchemaPublications(get_rel_namespace(ancestor)); |
| 382 |
if (list_member_oid(aschemaPubids, puboid)) |
| 383 |
{ |
| 384 |
topmost_relid = ancestor; |
| 385 |
|
| 386 |
if (ancestor_level) |
| 387 |
*ancestor_level = level; |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
list_free(apubids); |
| 392 |
list_free(aschemaPubids); |
| 393 |
} |
| 394 |
|
| 395 |
return topmost_relid; |
| 396 |
} |
| 397 |
|
| 398 |
/* |
| 399 |
* attnumstoint2vector |
| 400 |
* Convert a Bitmapset of AttrNumbers into an int2vector. |
| 401 |
* |
| 402 |
* AttrNumber numbers are 0-based, i.e., not offset by |
| 403 |
* FirstLowInvalidHeapAttributeNumber. |
| 404 |
*/ |
| 405 |
static int2vector * |
| 406 |
attnumstoint2vector(Bitmapset *attrs) |
| 407 |
{ |
| 408 |
int2vector *result; |
| 409 |
int n = bms_num_members(attrs); |
| 410 |
int i = -1; |
| 411 |
int j = 0; |
| 412 |
|
| 413 |
result = buildint2vector(NULL, n); |
| 414 |
|
| 415 |
while ((i = bms_next_member(attrs, i)) >= 0) |
| 416 |
{ |
| 417 |
Assert(i <= PG_INT16_MAX); |
| 418 |
|
| 419 |
result->values[j++] = (int16) i; |
| 420 |
} |
| 421 |
|
| 422 |
return result; |
| 423 |
} |
| 424 |
|
| 425 |
/* |
| 426 |
* Insert new publication / relation mapping. |
| 427 |
*/ |
| 428 |
ObjectAddress |
| 429 |
publication_add_relation(Oid pubid, PublicationRelInfo *pri, |
| 430 |
bool if_not_exists) |
| 431 |
{ |
| 432 |
Relation rel; |
| 433 |
HeapTuple tup; |
| 434 |
Datum values[Natts_pg_publication_rel]; |
| 435 |
bool nulls[Natts_pg_publication_rel]; |
| 436 |
Relation targetrel = pri->relation; |
| 437 |
Oid relid = RelationGetRelid(targetrel); |
| 438 |
Oid pubreloid; |
| 439 |
Bitmapset *attnums; |
| 440 |
Publication *pub = GetPublication(pubid); |
| 441 |
ObjectAddress myself, |
| 442 |
referenced; |
| 443 |
List *relids = NIL; |
| 444 |
int i; |
| 445 |
|
| 446 |
rel = table_open(PublicationRelRelationId, RowExclusiveLock); |
| 447 |
|
| 448 |
/* |
| 449 |
* Check for duplicates. Note that this does not really prevent |
| 450 |
* duplicates, it's here just to provide nicer error message in common |
| 451 |
* case. The real protection is the unique key on the catalog. |
| 452 |
*/ |
| 453 |
if (SearchSysCacheExists2(PUBLICATIONRELMAP, ObjectIdGetDatum(relid), |
| 454 |
ObjectIdGetDatum(pubid))) |
| 455 |
{ |
| 456 |
table_close(rel, RowExclusiveLock); |
| 457 |
|
| 458 |
if (if_not_exists) |
| 459 |
return InvalidObjectAddress; |
| 460 |
|
| 461 |
ereport(ERROR, |
| 462 |
(errcode(ERRCODE_DUPLICATE_OBJECT), |
| 463 |
errmsg("relation \"%s\" is already member of publication \"%s\"", |
| 464 |
RelationGetRelationName(targetrel), pub->name))); |
| 465 |
} |
| 466 |
|
| 467 |
check_publication_add_relation(targetrel); |
| 468 |
|
| 469 |
/* Validate and translate column names into a Bitmapset of attnums. */ |
| 470 |
attnums = pub_collist_validate(pri->relation, pri->columns); |
| 471 |
|
| 472 |
/* Form a tuple. */ |
| 473 |
memset(values, 0, sizeof(values)); |
| 474 |
memset(nulls, false, sizeof(nulls)); |
| 475 |
|
| 476 |
pubreloid = GetNewOidWithIndex(rel, PublicationRelObjectIndexId, |
| 477 |
Anum_pg_publication_rel_oid); |
| 478 |
values[Anum_pg_publication_rel_oid - 1] = ObjectIdGetDatum(pubreloid); |
| 479 |
values[Anum_pg_publication_rel_prpubid - 1] = |
| 480 |
ObjectIdGetDatum(pubid); |
| 481 |
values[Anum_pg_publication_rel_prrelid - 1] = |
| 482 |
ObjectIdGetDatum(relid); |
| 483 |
|
| 484 |
/* Add qualifications, if available */ |
| 485 |
if (pri->whereClause != NULL) |
| 486 |
values[Anum_pg_publication_rel_prqual - 1] = CStringGetTextDatum(nodeToString(pri->whereClause)); |
| 487 |
else |
| 488 |
nulls[Anum_pg_publication_rel_prqual - 1] = true; |
| 489 |
|
| 490 |
/* Add column list, if available */ |
| 491 |
if (pri->columns) |
| 492 |
values[Anum_pg_publication_rel_prattrs - 1] = PointerGetDatum(attnumstoint2vector(attnums)); |
| 493 |
else |
| 494 |
nulls[Anum_pg_publication_rel_prattrs - 1] = true; |
| 495 |
|
| 496 |
tup = heap_form_tuple(RelationGetDescr(rel), values, nulls); |
| 497 |
|
| 498 |
/* Insert tuple into catalog. */ |
| 499 |
CatalogTupleInsert(rel, tup); |
| 500 |
heap_freetuple(tup); |
| 501 |
|
| 502 |
/* Register dependencies as needed */ |
| 503 |
ObjectAddressSet(myself, PublicationRelRelationId, pubreloid); |
| 504 |
|
| 505 |
/* Add dependency on the publication */ |
| 506 |
ObjectAddressSet(referenced, PublicationRelationId, pubid); |
| 507 |
recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO); |
| 508 |
|
| 509 |
/* Add dependency on the relation */ |
| 510 |
ObjectAddressSet(referenced, RelationRelationId, relid); |
| 511 |
recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO); |
| 512 |
|
| 513 |
/* Add dependency on the objects mentioned in the qualifications */ |
| 514 |
if (pri->whereClause) |
| 515 |
{ |
| 516 |
CheckUsageOnTypesInSingleRelExpr(pri->whereClause, relid, GetUserId()); |
| 517 |
recordDependencyOnSingleRelExpr(&myself, pri->whereClause, relid, |
| 518 |
DEPENDENCY_NORMAL, DEPENDENCY_NORMAL, |
| 519 |
false); |
| 520 |
} |
| 521 |
|
| 522 |
/* Add dependency on the columns, if any are listed */ |
| 523 |
i = -1; |
| 524 |
while ((i = bms_next_member(attnums, i)) >= 0) |
| 525 |
{ |
| 526 |
ObjectAddressSubSet(referenced, RelationRelationId, relid, i); |
| 527 |
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL); |
| 528 |
} |
| 529 |
|
| 530 |
/* Close the table. */ |
| 531 |
table_close(rel, RowExclusiveLock); |
| 532 |
|
| 533 |
/* |
| 534 |
* Invalidate relcache so that publication info is rebuilt. |
| 535 |
* |
| 536 |
* For the partitioned tables, we must invalidate all partitions contained |
| 537 |
* in the respective partition hierarchies, not just the one explicitly |
| 538 |
* mentioned in the publication. This is required because we implicitly |
| 539 |
* publish the child tables when the parent table is published. |
| 540 |
*/ |
| 541 |
relids = GetPubPartitionOptionRelations(relids, PUBLICATION_PART_ALL, |
| 542 |
relid); |
| 543 |
|
| 544 |
InvalidatePublicationRels(relids); |
| 545 |
|
| 546 |
return myself; |
| 547 |
} |
| 548 |
|
| 549 |
/* |
| 550 |
* pub_collist_validate |
| 551 |
* Process and validate the 'columns' list and ensure the columns are all |
| 552 |
* valid to use for a publication. Checks for and raises an ERROR for |
| 553 |
* any unknown columns, system columns, duplicate columns, or virtual |
| 554 |
* generated columns. |
| 555 |
* |
| 556 |
* Looks up each column's attnum and returns a 0-based Bitmapset of the |
| 557 |
* corresponding attnums. |
| 558 |
*/ |
| 559 |
Bitmapset * |
| 560 |
pub_collist_validate(Relation targetrel, List *columns) |
| 561 |
{ |
| 562 |
Bitmapset *set = NULL; |
| 563 |
ListCell *lc; |
| 564 |
TupleDesc tupdesc = RelationGetDescr(targetrel); |
| 565 |
|
| 566 |
foreach(lc, columns) |
| 567 |
{ |
| 568 |
char *colname = strVal(lfirst(lc)); |
| 569 |
AttrNumber attnum = get_attnum(RelationGetRelid(targetrel), colname); |
| 570 |
|
| 571 |
if (attnum == InvalidAttrNumber) |
| 572 |
ereport(ERROR, |
| 573 |
errcode(ERRCODE_UNDEFINED_COLUMN), |
| 574 |
errmsg("column \"%s\" of relation \"%s\" does not exist", |
| 575 |
colname, RelationGetRelationName(targetrel))); |
| 576 |
|
| 577 |
if (!AttrNumberIsForUserDefinedAttr(attnum)) |
| 578 |
ereport(ERROR, |
| 579 |
errcode(ERRCODE_INVALID_COLUMN_REFERENCE), |
| 580 |
errmsg("cannot use system column \"%s\" in publication column list", |
| 581 |
colname)); |
| 582 |
|
| 583 |
if (TupleDescAttr(tupdesc, attnum - 1)->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL) |
| 584 |
ereport(ERROR, |
| 585 |
errcode(ERRCODE_INVALID_COLUMN_REFERENCE), |
| 586 |
errmsg("cannot use virtual generated column \"%s\" in publication column list", |
| 587 |
colname)); |
| 588 |
|
| 589 |
if (bms_is_member(attnum, set)) |
| 590 |
ereport(ERROR, |
| 591 |
errcode(ERRCODE_DUPLICATE_OBJECT), |
| 592 |
errmsg("duplicate column \"%s\" in publication column list", |
| 593 |
colname)); |
| 594 |
|
| 595 |
set = bms_add_member(set, attnum); |
| 596 |
} |
| 597 |
|
| 598 |
return set; |
| 599 |
} |
| 600 |
|
| 601 |
/* |
| 602 |
* Transform a column list (represented by an array Datum) to a bitmapset. |
| 603 |
* |
| 604 |
* If columns isn't NULL, add the column numbers to that set. |
| 605 |
* |
| 606 |
* If mcxt isn't NULL, build the bitmapset in that context. |
| 607 |
*/ |
| 608 |
Bitmapset * |
| 609 |
pub_collist_to_bitmapset(Bitmapset *columns, Datum pubcols, MemoryContext mcxt) |
| 610 |
{ |
| 611 |
Bitmapset *result = columns; |
| 612 |
ArrayType *arr; |
| 613 |
int nelems; |
| 614 |
int16 *elems; |
| 615 |
MemoryContext oldcxt = NULL; |
| 616 |
|
| 617 |
arr = DatumGetArrayTypeP(pubcols); |
| 618 |
nelems = ARR_DIMS(arr)[0]; |
| 619 |
elems = (int16 *) ARR_DATA_PTR(arr); |
| 620 |
|
| 621 |
/* If a memory context was specified, switch to it. */ |
| 622 |
if (mcxt) |
| 623 |
oldcxt = MemoryContextSwitchTo(mcxt); |
| 624 |
|
| 625 |
for (int i = 0; i < nelems; i++) |
| 626 |
result = bms_add_member(result, elems[i]); |
| 627 |
|
| 628 |
if (mcxt) |
| 629 |
MemoryContextSwitchTo(oldcxt); |
| 630 |
|
| 631 |
return result; |
| 632 |
} |
| 633 |
|
| 634 |
/* |
| 635 |
* Returns a bitmap representing the columns of the specified table. |
| 636 |
* |
| 637 |
* Generated columns are included if include_gencols_type is |
| 638 |
* PUBLISH_GENCOLS_STORED. |
| 639 |
*/ |
| 640 |
Bitmapset * |
| 641 |
pub_form_cols_map(Relation relation, PublishGencolsType include_gencols_type) |
| 642 |
{ |
| 643 |
Bitmapset *result = NULL; |
| 644 |
TupleDesc desc = RelationGetDescr(relation); |
| 645 |
|
| 646 |
for (int i = 0; i < desc->natts; i++) |
| 647 |
{ |
| 648 |
Form_pg_attribute att = TupleDescAttr(desc, i); |
| 649 |
|
| 650 |
if (att->attisdropped) |
| 651 |
continue; |
| 652 |
|
| 653 |
if (att->attgenerated) |
| 654 |
{ |
| 655 |
/* We only support replication of STORED generated cols. */ |
| 656 |
if (att->attgenerated != ATTRIBUTE_GENERATED_STORED) |
| 657 |
continue; |
| 658 |
|
| 659 |
/* User hasn't requested to replicate STORED generated cols. */ |
| 660 |
if (include_gencols_type != PUBLISH_GENCOLS_STORED) |
| 661 |
continue; |
| 662 |
} |
| 663 |
|
| 664 |
result = bms_add_member(result, att->attnum); |
| 665 |
} |
| 666 |
|
| 667 |
return result; |
| 668 |
} |
| 669 |
|
| 670 |
/* |
| 671 |
* Insert new publication / schema mapping. |
| 672 |
*/ |
| 673 |
ObjectAddress |
| 674 |
publication_add_schema(Oid pubid, Oid schemaid, bool if_not_exists) |
| 675 |
{ |
| 676 |
Relation rel; |
| 677 |
HeapTuple tup; |
| 678 |
Datum values[Natts_pg_publication_namespace]; |
| 679 |
bool nulls[Natts_pg_publication_namespace]; |
| 680 |
Oid psschid; |
| 681 |
Publication *pub = GetPublication(pubid); |
| 682 |
List *schemaRels = NIL; |
| 683 |
ObjectAddress myself, |
| 684 |
referenced; |
| 685 |
|
| 686 |
rel = table_open(PublicationNamespaceRelationId, RowExclusiveLock); |
| 687 |
|
| 688 |
/* |
| 689 |
* Check for duplicates. Note that this does not really prevent |
| 690 |
* duplicates, it's here just to provide nicer error message in common |
| 691 |
* case. The real protection is the unique key on the catalog. |
| 692 |
*/ |
| 693 |
if (SearchSysCacheExists2(PUBLICATIONNAMESPACEMAP, |
| 694 |
ObjectIdGetDatum(schemaid), |
| 695 |
ObjectIdGetDatum(pubid))) |
| 696 |
{ |
| 697 |
table_close(rel, RowExclusiveLock); |
| 698 |
|
| 699 |
if (if_not_exists) |
| 700 |
return InvalidObjectAddress; |
| 701 |
|
| 702 |
ereport(ERROR, |
| 703 |
(errcode(ERRCODE_DUPLICATE_OBJECT), |
| 704 |
errmsg("schema \"%s\" is already member of publication \"%s\"", |
| 705 |
get_namespace_name(schemaid), pub->name))); |
| 706 |
} |
| 707 |
|
| 708 |
check_publication_add_schema(schemaid); |
| 709 |
|
| 710 |
/* Form a tuple */ |
| 711 |
memset(values, 0, sizeof(values)); |
| 712 |
memset(nulls, false, sizeof(nulls)); |
| 713 |
|
| 714 |
psschid = GetNewOidWithIndex(rel, PublicationNamespaceObjectIndexId, |
| 715 |
Anum_pg_publication_namespace_oid); |
| 716 |
values[Anum_pg_publication_namespace_oid - 1] = ObjectIdGetDatum(psschid); |
| 717 |
values[Anum_pg_publication_namespace_pnpubid - 1] = |
| 718 |
ObjectIdGetDatum(pubid); |
| 719 |
values[Anum_pg_publication_namespace_pnnspid - 1] = |
| 720 |
ObjectIdGetDatum(schemaid); |
| 721 |
|
| 722 |
tup = heap_form_tuple(RelationGetDescr(rel), values, nulls); |
| 723 |
|
| 724 |
/* Insert tuple into catalog */ |
| 725 |
CatalogTupleInsert(rel, tup); |
| 726 |
heap_freetuple(tup); |
| 727 |
|
| 728 |
ObjectAddressSet(myself, PublicationNamespaceRelationId, psschid); |
| 729 |
|
| 730 |
/* Add dependency on the publication */ |
| 731 |
ObjectAddressSet(referenced, PublicationRelationId, pubid); |
| 732 |
recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO); |
| 733 |
|
| 734 |
/* Add dependency on the schema */ |
| 735 |
ObjectAddressSet(referenced, NamespaceRelationId, schemaid); |
| 736 |
recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO); |
| 737 |
|
| 738 |
/* Close the table */ |
| 739 |
table_close(rel, RowExclusiveLock); |
| 740 |
|
| 741 |
/* |
| 742 |
* Invalidate relcache so that publication info is rebuilt. See |
| 743 |
* publication_add_relation for why we need to consider all the |
| 744 |
* partitions. |
| 745 |
*/ |
| 746 |
schemaRels = GetSchemaPublicationRelations(schemaid, |
| 747 |
PUBLICATION_PART_ALL); |
| 748 |
InvalidatePublicationRels(schemaRels); |
| 749 |
|
| 750 |
return myself; |
| 751 |
} |
| 752 |
|
| 753 |
/* Gets list of publication oids for a relation */ |
| 754 |
List * |
| 755 |
GetRelationPublications(Oid relid) |
| 756 |
{ |
| 757 |
List *result = NIL; |
| 758 |
CatCList *pubrellist; |
| 759 |
int i; |
| 760 |
|
| 761 |
/* Find all publications associated with the relation. */ |
| 762 |
pubrellist = SearchSysCacheList1(PUBLICATIONRELMAP, |
| 763 |
ObjectIdGetDatum(relid)); |
| 764 |
for (i = 0; i < pubrellist->n_members; i++) |
| 765 |
{ |
| 766 |
HeapTuple tup = &pubrellist->members[i]->tuple; |
| 767 |
Oid pubid = ((Form_pg_publication_rel) GETSTRUCT(tup))->prpubid; |
| 768 |
|
| 769 |
result = lappend_oid(result, pubid); |
| 770 |
} |
| 771 |
|
| 772 |
ReleaseSysCacheList(pubrellist); |
| 773 |
|
| 774 |
return result; |
| 775 |
} |
| 776 |
|
| 777 |
/* |
| 778 |
* Gets list of relation oids for a publication. |
| 779 |
* |
| 780 |
* This should only be used FOR TABLE publications, the FOR ALL TABLES |
| 781 |
* should use GetAllTablesPublicationRelations(). |
| 782 |
*/ |
| 783 |
List * |
| 784 |
GetPublicationRelations(Oid pubid, PublicationPartOpt pub_partopt) |
| 785 |
{ |
| 786 |
List *result; |
| 787 |
Relation pubrelsrel; |
| 788 |
ScanKeyData scankey; |
| 789 |
SysScanDesc scan; |
| 790 |
HeapTuple tup; |
| 791 |
|
| 792 |
/* Find all publications associated with the relation. */ |
| 793 |
pubrelsrel = table_open(PublicationRelRelationId, AccessShareLock); |
| 794 |
|
| 795 |
ScanKeyInit(&scankey, |
| 796 |
Anum_pg_publication_rel_prpubid, |
| 797 |
BTEqualStrategyNumber, F_OIDEQ, |
| 798 |
ObjectIdGetDatum(pubid)); |
| 799 |
|
| 800 |
scan = systable_beginscan(pubrelsrel, PublicationRelPrpubidIndexId, |
| 801 |
true, NULL, 1, &scankey); |
| 802 |
|
| 803 |
result = NIL; |
| 804 |
while (HeapTupleIsValid(tup = systable_getnext(scan))) |
| 805 |
{ |
| 806 |
Form_pg_publication_rel pubrel; |
| 807 |
|
| 808 |
pubrel = (Form_pg_publication_rel) GETSTRUCT(tup); |
| 809 |
result = GetPubPartitionOptionRelations(result, pub_partopt, |
| 810 |
pubrel->prrelid); |
| 811 |
} |
| 812 |
|
| 813 |
systable_endscan(scan); |
| 814 |
table_close(pubrelsrel, AccessShareLock); |
| 815 |
|
| 816 |
/* Now sort and de-duplicate the result list */ |
| 817 |
list_sort(result, list_oid_cmp); |
| 818 |
list_deduplicate_oid(result); |
| 819 |
|
| 820 |
return result; |
| 821 |
} |
| 822 |
|
| 823 |
/* |
| 824 |
* Gets list of publication oids for publications marked as FOR ALL TABLES. |
| 825 |
*/ |
| 826 |
List * |
| 827 |
GetAllTablesPublications(void) |
| 828 |
{ |
| 829 |
List *result; |
| 830 |
Relation rel; |
| 831 |
ScanKeyData scankey; |
| 832 |
SysScanDesc scan; |
| 833 |
HeapTuple tup; |
| 834 |
|
| 835 |
/* Find all publications that are marked as for all tables. */ |
| 836 |
rel = table_open(PublicationRelationId, AccessShareLock); |
| 837 |
|
| 838 |
ScanKeyInit(&scankey, |
| 839 |
Anum_pg_publication_puballtables, |
| 840 |
BTEqualStrategyNumber, F_BOOLEQ, |
| 841 |
BoolGetDatum(true)); |
| 842 |
|
| 843 |
scan = systable_beginscan(rel, InvalidOid, false, |
| 844 |
NULL, 1, &scankey); |
| 845 |
|
| 846 |
result = NIL; |
| 847 |
while (HeapTupleIsValid(tup = systable_getnext(scan))) |
| 848 |
{ |
| 849 |
Oid oid = ((Form_pg_publication) GETSTRUCT(tup))->oid; |
| 850 |
|
| 851 |
result = lappend_oid(result, oid); |
| 852 |
} |
| 853 |
|
| 854 |
systable_endscan(scan); |
| 855 |
table_close(rel, AccessShareLock); |
| 856 |
|
| 857 |
return result; |
| 858 |
} |
| 859 |
|
| 860 |
/* |
| 861 |
* Gets list of all relation published by FOR ALL TABLES publication(s). |
| 862 |
* |
| 863 |
* If the publication publishes partition changes via their respective root |
| 864 |
* partitioned tables, we must exclude partitions in favor of including the |
| 865 |
* root partitioned tables. |
| 866 |
*/ |
| 867 |
List * |
| 868 |
GetAllTablesPublicationRelations(bool pubviaroot) |
| 869 |
{ |
| 870 |
Relation classRel; |
| 871 |
ScanKeyData key[1]; |
| 872 |
TableScanDesc scan; |
| 873 |
HeapTuple tuple; |
| 874 |
List *result = NIL; |
| 875 |
|
| 876 |
classRel = table_open(RelationRelationId, AccessShareLock); |
| 877 |
|
| 878 |
ScanKeyInit(&key[0], |
| 879 |
Anum_pg_class_relkind, |
| 880 |
BTEqualStrategyNumber, F_CHAREQ, |
| 881 |
CharGetDatum(RELKIND_RELATION)); |
| 882 |
|
| 883 |
scan = table_beginscan_catalog(classRel, 1, key); |
| 884 |
|
| 885 |
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL) |
| 886 |
{ |
| 887 |
Form_pg_class relForm = (Form_pg_class) GETSTRUCT(tuple); |
| 888 |
Oid relid = relForm->oid; |
| 889 |
|
| 890 |
if (is_publishable_class(relid, relForm) && |
| 891 |
!(relForm->relispartition && pubviaroot)) |
| 892 |
result = lappend_oid(result, relid); |
| 893 |
} |
| 894 |
|
| 895 |
table_endscan(scan); |
| 896 |
|
| 897 |
if (pubviaroot) |
| 898 |
{ |
| 899 |
ScanKeyInit(&key[0], |
| 900 |
Anum_pg_class_relkind, |
| 901 |
BTEqualStrategyNumber, F_CHAREQ, |
| 902 |
CharGetDatum(RELKIND_PARTITIONED_TABLE)); |
| 903 |
|
| 904 |
scan = table_beginscan_catalog(classRel, 1, key); |
| 905 |
|
| 906 |
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL) |
| 907 |
{ |
| 908 |
Form_pg_class relForm = (Form_pg_class) GETSTRUCT(tuple); |
| 909 |
Oid relid = relForm->oid; |
| 910 |
|
| 911 |
if (is_publishable_class(relid, relForm) && |
| 912 |
!relForm->relispartition) |
| 913 |
result = lappend_oid(result, relid); |
| 914 |
} |
| 915 |
|
| 916 |
table_endscan(scan); |
| 917 |
} |
| 918 |
|
| 919 |
table_close(classRel, AccessShareLock); |
| 920 |
return result; |
| 921 |
} |
| 922 |
|
| 923 |
/* |
| 924 |
* Gets the list of schema oids for a publication. |
| 925 |
* |
| 926 |
* This should only be used FOR TABLES IN SCHEMA publications. |
| 927 |
*/ |
| 928 |
List * |
| 929 |
GetPublicationSchemas(Oid pubid) |
| 930 |
{ |
| 931 |
List *result = NIL; |
| 932 |
Relation pubschsrel; |
| 933 |
ScanKeyData scankey; |
| 934 |
SysScanDesc scan; |
| 935 |
HeapTuple tup; |
| 936 |
|
| 937 |
/* Find all schemas associated with the publication */ |
| 938 |
pubschsrel = table_open(PublicationNamespaceRelationId, AccessShareLock); |
| 939 |
|
| 940 |
ScanKeyInit(&scankey, |
| 941 |
Anum_pg_publication_namespace_pnpubid, |
| 942 |
BTEqualStrategyNumber, F_OIDEQ, |
| 943 |
ObjectIdGetDatum(pubid)); |
| 944 |
|
| 945 |
scan = systable_beginscan(pubschsrel, |
| 946 |
PublicationNamespacePnnspidPnpubidIndexId, |
| 947 |
true, NULL, 1, &scankey); |
| 948 |
while (HeapTupleIsValid(tup = systable_getnext(scan))) |
| 949 |
{ |
| 950 |
Form_pg_publication_namespace pubsch; |
| 951 |
|
| 952 |
pubsch = (Form_pg_publication_namespace) GETSTRUCT(tup); |
| 953 |
|
| 954 |
result = lappend_oid(result, pubsch->pnnspid); |
| 955 |
} |
| 956 |
|
| 957 |
systable_endscan(scan); |
| 958 |
table_close(pubschsrel, AccessShareLock); |
| 959 |
|
| 960 |
return result; |
| 961 |
} |
| 962 |
|
| 963 |
/* |
| 964 |
* Gets the list of publication oids associated with a specified schema. |
| 965 |
*/ |
| 966 |
List * |
| 967 |
GetSchemaPublications(Oid schemaid) |
| 968 |
{ |
| 969 |
List *result = NIL; |
| 970 |
CatCList *pubschlist; |
| 971 |
int i; |
| 972 |
|
| 973 |
/* Find all publications associated with the schema */ |
| 974 |
pubschlist = SearchSysCacheList1(PUBLICATIONNAMESPACEMAP, |
| 975 |
ObjectIdGetDatum(schemaid)); |
| 976 |
for (i = 0; i < pubschlist->n_members; i++) |
| 977 |
{ |
| 978 |
HeapTuple tup = &pubschlist->members[i]->tuple; |
| 979 |
Oid pubid = ((Form_pg_publication_namespace) GETSTRUCT(tup))->pnpubid; |
| 980 |
|
| 981 |
result = lappend_oid(result, pubid); |
| 982 |
} |
| 983 |
|
| 984 |
ReleaseSysCacheList(pubschlist); |
| 985 |
|
| 986 |
return result; |
| 987 |
} |
| 988 |
|
| 989 |
/* |
| 990 |
* Get the list of publishable relation oids for a specified schema. |
| 991 |
*/ |
| 992 |
List * |
| 993 |
GetSchemaPublicationRelations(Oid schemaid, PublicationPartOpt pub_partopt) |
| 994 |
{ |
| 995 |
Relation classRel; |
| 996 |
ScanKeyData key[1]; |
| 997 |
TableScanDesc scan; |
| 998 |
HeapTuple tuple; |
| 999 |
List *result = NIL; |
| 1000 |
|
| 1001 |
Assert(OidIsValid(schemaid)); |
| 1002 |
|
| 1003 |
classRel = table_open(RelationRelationId, AccessShareLock); |
| 1004 |
|
| 1005 |
ScanKeyInit(&key[0], |
| 1006 |
Anum_pg_class_relnamespace, |
| 1007 |
BTEqualStrategyNumber, F_OIDEQ, |
| 1008 |
schemaid); |
| 1009 |
|
| 1010 |
/* get all the relations present in the specified schema */ |
| 1011 |
scan = table_beginscan_catalog(classRel, 1, key); |
| 1012 |
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL) |
| 1013 |
{ |
| 1014 |
Form_pg_class relForm = (Form_pg_class) GETSTRUCT(tuple); |
| 1015 |
Oid relid = relForm->oid; |
| 1016 |
char relkind; |
| 1017 |
|
| 1018 |
if (!is_publishable_class(relid, relForm)) |
| 1019 |
continue; |
| 1020 |
|
| 1021 |
relkind = get_rel_relkind(relid); |
| 1022 |
if (relkind == RELKIND_RELATION) |
| 1023 |
result = lappend_oid(result, relid); |
| 1024 |
else if (relkind == RELKIND_PARTITIONED_TABLE) |
| 1025 |
{ |
| 1026 |
List *partitionrels = NIL; |
| 1027 |
|
| 1028 |
/* |
| 1029 |
* It is quite possible that some of the partitions are in a |
| 1030 |
* different schema than the parent table, so we need to get such |
| 1031 |
* partitions separately. |
| 1032 |
*/ |
| 1033 |
partitionrels = GetPubPartitionOptionRelations(partitionrels, |
| 1034 |
pub_partopt, |
| 1035 |
relForm->oid); |
| 1036 |
result = list_concat_unique_oid(result, partitionrels); |
| 1037 |
} |
| 1038 |
} |
| 1039 |
|
| 1040 |
table_endscan(scan); |
| 1041 |
table_close(classRel, AccessShareLock); |
| 1042 |
return result; |
| 1043 |
} |
| 1044 |
|
| 1045 |
/* |
| 1046 |
* Gets the list of all relations published by FOR TABLES IN SCHEMA |
| 1047 |
* publication. |
| 1048 |
*/ |
| 1049 |
List * |
| 1050 |
GetAllSchemaPublicationRelations(Oid pubid, PublicationPartOpt pub_partopt) |
| 1051 |
{ |
| 1052 |
List *result = NIL; |
| 1053 |
List *pubschemalist = GetPublicationSchemas(pubid); |
| 1054 |
ListCell *cell; |
| 1055 |
|
| 1056 |
foreach(cell, pubschemalist) |
| 1057 |
{ |
| 1058 |
Oid schemaid = lfirst_oid(cell); |
| 1059 |
List *schemaRels = NIL; |
| 1060 |
|
| 1061 |
schemaRels = GetSchemaPublicationRelations(schemaid, pub_partopt); |
| 1062 |
result = list_concat(result, schemaRels); |
| 1063 |
} |
| 1064 |
|
| 1065 |
return result; |
| 1066 |
} |
| 1067 |
|
| 1068 |
/* |
| 1069 |
* Get publication using oid |
| 1070 |
* |
| 1071 |
* The Publication struct and its data are palloc'ed here. |
| 1072 |
*/ |
| 1073 |
Publication * |
| 1074 |
GetPublication(Oid pubid) |
| 1075 |
{ |
| 1076 |
HeapTuple tup; |
| 1077 |
Publication *pub; |
| 1078 |
Form_pg_publication pubform; |
| 1079 |
|
| 1080 |
tup = SearchSysCache1(PUBLICATIONOID, ObjectIdGetDatum(pubid)); |
| 1081 |
if (!HeapTupleIsValid(tup)) |
| 1082 |
elog(ERROR, "cache lookup failed for publication %u", pubid); |
| 1083 |
|
| 1084 |
pubform = (Form_pg_publication) GETSTRUCT(tup); |
| 1085 |
|
| 1086 |
pub = (Publication *) palloc(sizeof(Publication)); |
| 1087 |
pub->oid = pubid; |
| 1088 |
pub->name = pstrdup(NameStr(pubform->pubname)); |
| 1089 |
pub->alltables = pubform->puballtables; |
| 1090 |
pub->pubactions.pubinsert = pubform->pubinsert; |
| 1091 |
pub->pubactions.pubupdate = pubform->pubupdate; |
| 1092 |
pub->pubactions.pubdelete = pubform->pubdelete; |
| 1093 |
pub->pubactions.pubtruncate = pubform->pubtruncate; |
| 1094 |
pub->pubviaroot = pubform->pubviaroot; |
| 1095 |
pub->pubgencols_type = pubform->pubgencols; |
| 1096 |
|
| 1097 |
ReleaseSysCache(tup); |
| 1098 |
|
| 1099 |
return pub; |
| 1100 |
} |
| 1101 |
|
| 1102 |
/* |
| 1103 |
* Get Publication using name. |
| 1104 |
*/ |
| 1105 |
Publication * |
| 1106 |
GetPublicationByName(const char *pubname, bool missing_ok) |
| 1107 |
{ |
| 1108 |
Oid oid; |
| 1109 |
|
| 1110 |
oid = get_publication_oid(pubname, missing_ok); |
| 1111 |
|
| 1112 |
return OidIsValid(oid) ? GetPublication(oid) : NULL; |
| 1113 |
} |
| 1114 |
|
| 1115 |
/* |
| 1116 |
* Get information of the tables in the given publication array. |
| 1117 |
* |
| 1118 |
* Returns pubid, relid, column list, row filter for each table. |
| 1119 |
*/ |
| 1120 |
Datum |
| 1121 |
pg_get_publication_tables(PG_FUNCTION_ARGS) |
| 1122 |
{ |
| 1123 |
#define NUM_PUBLICATION_TABLES_ELEM 4 |
| 1124 |
|
| 1125 |
/* |
| 1126 |
* State carried across SRF calls. We track the index ourselves instead of |
| 1127 |
* using funcctx->call_cntr, so that concurrently dropped tables can be |
| 1128 |
* skipped without emitting a row. |
| 1129 |
*/ |
| 1130 |
typedef struct |
| 1131 |
{ |
| 1132 |
List *table_infos; /* list of published_rel */ |
| 1133 |
int curr_idx; /* current index into table_infos */ |
| 1134 |
} publication_tables_state; |
| 1135 |
|
| 1136 |
FuncCallContext *funcctx; |
| 1137 |
publication_tables_state *ptstate = NULL; |
| 1138 |
|
| 1139 |
/* stuff done only on the first call of the function */ |
| 1140 |
if (SRF_IS_FIRSTCALL()) |
| 1141 |
{ |
| 1142 |
TupleDesc tupdesc; |
| 1143 |
MemoryContext oldcontext; |
| 1144 |
List *table_infos = NIL; |
| 1145 |
ArrayType *arr; |
| 1146 |
Datum *elems; |
| 1147 |
int nelems, |
| 1148 |
i; |
| 1149 |
bool viaroot = false; |
| 1150 |
|
| 1151 |
/* create a function context for cross-call persistence */ |
| 1152 |
funcctx = SRF_FIRSTCALL_INIT(); |
| 1153 |
|
| 1154 |
/* switch to memory context appropriate for multiple function calls */ |
| 1155 |
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); |
| 1156 |
|
| 1157 |
/* |
| 1158 |
* Deconstruct the parameter into elements where each element is a |
| 1159 |
* publication name. |
| 1160 |
*/ |
| 1161 |
arr = PG_GETARG_ARRAYTYPE_P(0); |
| 1162 |
deconstruct_array_builtin(arr, TEXTOID, &elems, NULL, &nelems); |
| 1163 |
|
| 1164 |
/* Get Oids of tables from each publication. */ |
| 1165 |
for (i = 0; i < nelems; i++) |
| 1166 |
{ |
| 1167 |
Publication *pub_elem; |
| 1168 |
List *pub_elem_tables = NIL; |
| 1169 |
ListCell *lc; |
| 1170 |
|
| 1171 |
pub_elem = GetPublicationByName(TextDatumGetCString(elems[i]), false); |
| 1172 |
|
| 1173 |
/* |
| 1174 |
* Publications support partitioned tables. If |
| 1175 |
* publish_via_partition_root is false, all changes are replicated |
| 1176 |
* using leaf partition identity and schema, so we only need |
| 1177 |
* those. Otherwise, get the partitioned table itself. |
| 1178 |
*/ |
| 1179 |
if (pub_elem->alltables) |
| 1180 |
pub_elem_tables = GetAllTablesPublicationRelations(pub_elem->pubviaroot); |
| 1181 |
else |
| 1182 |
{ |
| 1183 |
List *relids, |
| 1184 |
*schemarelids; |
| 1185 |
|
| 1186 |
relids = GetPublicationRelations(pub_elem->oid, |
| 1187 |
pub_elem->pubviaroot ? |
| 1188 |
PUBLICATION_PART_ROOT : |
| 1189 |
PUBLICATION_PART_LEAF); |
| 1190 |
schemarelids = GetAllSchemaPublicationRelations(pub_elem->oid, |
| 1191 |
pub_elem->pubviaroot ? |
| 1192 |
PUBLICATION_PART_ROOT : |
| 1193 |
PUBLICATION_PART_LEAF); |
| 1194 |
pub_elem_tables = list_concat_unique_oid(relids, schemarelids); |
| 1195 |
} |
| 1196 |
|
| 1197 |
/* |
| 1198 |
* Record the published table and the corresponding publication so |
| 1199 |
* that we can get row filters and column lists later. |
| 1200 |
* |
| 1201 |
* When a table is published by multiple publications, to obtain |
| 1202 |
* all row filters and column lists, the structure related to this |
| 1203 |
* table will be recorded multiple times. |
| 1204 |
*/ |
| 1205 |
foreach(lc, pub_elem_tables) |
| 1206 |
{ |
| 1207 |
published_rel *table_info = (published_rel *) palloc(sizeof(published_rel)); |
| 1208 |
|
| 1209 |
table_info->relid = lfirst_oid(lc); |
| 1210 |
table_info->pubid = pub_elem->oid; |
| 1211 |
table_infos = lappend(table_infos, table_info); |
| 1212 |
} |
| 1213 |
|
| 1214 |
/* At least one publication is using publish_via_partition_root. */ |
| 1215 |
if (pub_elem->pubviaroot) |
| 1216 |
viaroot = true; |
| 1217 |
} |
| 1218 |
|
| 1219 |
/* |
| 1220 |
* If the publication publishes partition changes via their respective |
| 1221 |
* root partitioned tables, we must exclude partitions in favor of |
| 1222 |
* including the root partitioned tables. Otherwise, the function |
| 1223 |
* could return both the child and parent tables which could cause |
| 1224 |
* data of the child table to be double-published on the subscriber |
| 1225 |
* side. |
| 1226 |
*/ |
| 1227 |
if (viaroot) |
| 1228 |
filter_partitions(table_infos); |
| 1229 |
|
| 1230 |
/* Construct a tuple descriptor for the result rows. */ |
| 1231 |
tupdesc = CreateTemplateTupleDesc(NUM_PUBLICATION_TABLES_ELEM); |
| 1232 |
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "pubid", |
| 1233 |
OIDOID, -1, 0); |
| 1234 |
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "relid", |
| 1235 |
OIDOID, -1, 0); |
| 1236 |
TupleDescInitEntry(tupdesc, (AttrNumber) 3, "attrs", |
| 1237 |
INT2VECTOROID, -1, 0); |
| 1238 |
TupleDescInitEntry(tupdesc, (AttrNumber) 4, "qual", |
| 1239 |
PG_NODE_TREEOID, -1, 0); |
| 1240 |
|
| 1241 |
funcctx->tuple_desc = BlessTupleDesc(tupdesc); |
| 1242 |
|
| 1243 |
/* Store the state to be used across SRF calls. */ |
| 1244 |
ptstate = palloc_object(publication_tables_state); |
| 1245 |
ptstate->table_infos = table_infos; |
| 1246 |
ptstate->curr_idx = 0; |
| 1247 |
funcctx->user_fctx = ptstate; |
| 1248 |
|
| 1249 |
MemoryContextSwitchTo(oldcontext); |
| 1250 |
} |
| 1251 |
|
| 1252 |
/* stuff done on every call of the function */ |
| 1253 |
funcctx = SRF_PERCALL_SETUP(); |
| 1254 |
ptstate = (publication_tables_state *) funcctx->user_fctx; |
| 1255 |
|
| 1256 |
while (ptstate->curr_idx < list_length(ptstate->table_infos)) |
| 1257 |
{ |
| 1258 |
HeapTuple pubtuple = NULL; |
| 1259 |
HeapTuple rettuple; |
| 1260 |
Publication *pub; |
| 1261 |
published_rel *table_info = (published_rel *) list_nth(ptstate->table_infos, |
| 1262 |
ptstate->curr_idx); |
| 1263 |
Oid relid = table_info->relid; |
| 1264 |
Relation rel; |
| 1265 |
Oid schemaid; |
| 1266 |
Datum values[NUM_PUBLICATION_TABLES_ELEM] = {0}; |
| 1267 |
bool nulls[NUM_PUBLICATION_TABLES_ELEM] = {0}; |
| 1268 |
|
| 1269 |
/* Advance the index for the next call. */ |
| 1270 |
ptstate->curr_idx++; |
| 1271 |
|
| 1272 |
/* |
| 1273 |
* The table OIDs were collected earlier, so a table may have been |
| 1274 |
* dropped before we get here. try_table_open() returns NULL if it is |
| 1275 |
* already gone, in which case we skip it; such tables are simply |
| 1276 |
* absent from the result set, which is the expected point-in-time |
| 1277 |
* behavior. |
| 1278 |
*/ |
| 1279 |
rel = try_table_open(relid, AccessShareLock); |
| 1280 |
if (rel == NULL) |
| 1281 |
continue; |
| 1282 |
|
| 1283 |
/* |
| 1284 |
* Form tuple with appropriate data. |
| 1285 |
*/ |
| 1286 |
|
| 1287 |
pub = GetPublication(table_info->pubid); |
| 1288 |
|
| 1289 |
values[0] = ObjectIdGetDatum(pub->oid); |
| 1290 |
values[1] = ObjectIdGetDatum(relid); |
| 1291 |
|
| 1292 |
/* |
| 1293 |
* We don't consider row filters or column lists for FOR ALL TABLES or |
| 1294 |
* FOR TABLES IN SCHEMA publications. |
| 1295 |
*/ |
| 1296 |
schemaid = RelationGetNamespace(rel); |
| 1297 |
if (!pub->alltables && |
| 1298 |
!SearchSysCacheExists2(PUBLICATIONNAMESPACEMAP, |
| 1299 |
ObjectIdGetDatum(schemaid), |
| 1300 |
ObjectIdGetDatum(pub->oid))) |
| 1301 |
pubtuple = SearchSysCacheCopy2(PUBLICATIONRELMAP, |
| 1302 |
ObjectIdGetDatum(relid), |
| 1303 |
ObjectIdGetDatum(pub->oid)); |
| 1304 |
|
| 1305 |
if (HeapTupleIsValid(pubtuple)) |
| 1306 |
{ |
| 1307 |
/* Lookup the column list attribute. */ |
| 1308 |
values[2] = SysCacheGetAttr(PUBLICATIONRELMAP, pubtuple, |
| 1309 |
Anum_pg_publication_rel_prattrs, |
| 1310 |
&(nulls[2])); |
| 1311 |
|
| 1312 |
/* Null indicates no filter. */ |
| 1313 |
values[3] = SysCacheGetAttr(PUBLICATIONRELMAP, pubtuple, |
| 1314 |
Anum_pg_publication_rel_prqual, |
| 1315 |
&(nulls[3])); |
| 1316 |
} |
| 1317 |
else |
| 1318 |
{ |
| 1319 |
nulls[2] = true; |
| 1320 |
nulls[3] = true; |
| 1321 |
} |
| 1322 |
|
| 1323 |
/* Show all columns when the column list is not specified. */ |
| 1324 |
if (nulls[2]) |
| 1325 |
{ |
| 1326 |
int nattnums = 0; |
| 1327 |
int16 *attnums; |
| 1328 |
TupleDesc desc = RelationGetDescr(rel); |
| 1329 |
int i; |
| 1330 |
|
| 1331 |
attnums = (int16 *) palloc(desc->natts * sizeof(int16)); |
| 1332 |
|
| 1333 |
for (i = 0; i < desc->natts; i++) |
| 1334 |
{ |
| 1335 |
Form_pg_attribute att = TupleDescAttr(desc, i); |
| 1336 |
|
| 1337 |
if (att->attisdropped) |
| 1338 |
continue; |
| 1339 |
|
| 1340 |
if (att->attgenerated) |
| 1341 |
{ |
| 1342 |
/* We only support replication of STORED generated cols. */ |
| 1343 |
if (att->attgenerated != ATTRIBUTE_GENERATED_STORED) |
| 1344 |
continue; |
| 1345 |
|
| 1346 |
/* |
| 1347 |
* User hasn't requested to replicate STORED generated |
| 1348 |
* cols. |
| 1349 |
*/ |
| 1350 |
if (pub->pubgencols_type != PUBLISH_GENCOLS_STORED) |
| 1351 |
continue; |
| 1352 |
} |
| 1353 |
|
| 1354 |
attnums[nattnums++] = att->attnum; |
| 1355 |
} |
| 1356 |
|
| 1357 |
if (nattnums > 0) |
| 1358 |
{ |
| 1359 |
values[2] = PointerGetDatum(buildint2vector(attnums, nattnums)); |
| 1360 |
nulls[2] = false; |
| 1361 |
} |
| 1362 |
} |
| 1363 |
|
| 1364 |
table_close(rel, AccessShareLock); |
| 1365 |
|
| 1366 |
rettuple = heap_form_tuple(funcctx->tuple_desc, values, nulls); |
| 1367 |
|
| 1368 |
SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(rettuple)); |
| 1369 |
} |
| 1370 |
|
| 1371 |
SRF_RETURN_DONE(funcctx); |
| 1372 |
} |