PostgreSQL Source Code git master
publicationcmds.h File Reference
Include dependency graph for publicationcmds.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define MAX_RELCACHE_INVAL_MSGS   4096
 

Functions

ObjectAddress CreatePublication (ParseState *pstate, CreatePublicationStmt *stmt)
 
void AlterPublication (ParseState *pstate, AlterPublicationStmt *stmt)
 
void RemovePublicationById (Oid pubid)
 
void RemovePublicationRelById (Oid proid)
 
void RemovePublicationSchemaById (Oid psoid)
 
ObjectAddress AlterPublicationOwner (const char *name, Oid newOwnerId)
 
void AlterPublicationOwner_oid (Oid pubid, Oid newOwnerId)
 
void InvalidatePublicationRels (List *relids)
 
bool pub_rf_contains_invalid_column (Oid pubid, Relation relation, List *ancestors, bool pubviaroot)
 
bool pub_contains_invalid_column (Oid pubid, Relation relation, List *ancestors, bool pubviaroot, char pubgencols_type, bool *invalid_column_list, bool *invalid_gen_col)
 
void InvalidatePubRelSyncCache (Oid pubid, bool puballtables)
 

Macro Definition Documentation

◆ MAX_RELCACHE_INVAL_MSGS

#define MAX_RELCACHE_INVAL_MSGS   4096

Definition at line 23 of file publicationcmds.h.

Function Documentation

◆ AlterPublication()

void AlterPublication ( ParseState pstate,
AlterPublicationStmt stmt 
)

Definition at line 1540 of file publicationcmds.c.

1541{
1542 Relation rel;
1543 HeapTuple tup;
1544 Form_pg_publication pubform;
1545
1546 rel = table_open(PublicationRelationId, RowExclusiveLock);
1547
1548 tup = SearchSysCacheCopy1(PUBLICATIONNAME,
1549 CStringGetDatum(stmt->pubname));
1550
1551 if (!HeapTupleIsValid(tup))
1552 ereport(ERROR,
1553 (errcode(ERRCODE_UNDEFINED_OBJECT),
1554 errmsg("publication \"%s\" does not exist",
1555 stmt->pubname)));
1556
1557 pubform = (Form_pg_publication) GETSTRUCT(tup);
1558
1559 /* must be owner */
1560 if (!object_ownercheck(PublicationRelationId, pubform->oid, GetUserId()))
1562 stmt->pubname);
1563
1564 if (stmt->options)
1565 AlterPublicationOptions(pstate, stmt, rel, tup);
1566 else
1567 {
1568 List *relations = NIL;
1569 List *schemaidlist = NIL;
1570 Oid pubid = pubform->oid;
1571
1572 ObjectsInPublicationToOids(stmt->pubobjects, pstate, &relations,
1573 &schemaidlist);
1574
1575 CheckAlterPublication(stmt, tup, relations, schemaidlist);
1576
1577 heap_freetuple(tup);
1578
1579 /* Lock the publication so nobody else can do anything with it. */
1580 LockDatabaseObject(PublicationRelationId, pubid, 0,
1582
1583 /*
1584 * It is possible that by the time we acquire the lock on publication,
1585 * concurrent DDL has removed it. We can test this by checking the
1586 * existence of publication. We get the tuple again to avoid the risk
1587 * of any publication option getting changed.
1588 */
1589 tup = SearchSysCacheCopy1(PUBLICATIONOID, ObjectIdGetDatum(pubid));
1590 if (!HeapTupleIsValid(tup))
1591 ereport(ERROR,
1592 errcode(ERRCODE_UNDEFINED_OBJECT),
1593 errmsg("publication \"%s\" does not exist",
1594 stmt->pubname));
1595
1596 AlterPublicationTables(stmt, tup, relations, pstate->p_sourcetext,
1597 schemaidlist != NIL);
1598 AlterPublicationSchemas(stmt, tup, schemaidlist);
1599 }
1600
1601 /* Cleanup. */
1602 heap_freetuple(tup);
1604}
@ ACLCHECK_NOT_OWNER
Definition: acl.h:185
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:2654
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Definition: aclchk.c:4090
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1435
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
#define stmt
Definition: indent_codes.h:59
void LockDatabaseObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1008
#define AccessExclusiveLock
Definition: lockdefs.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
Oid GetUserId(void)
Definition: miscinit.c:469
@ OBJECT_PUBLICATION
Definition: parsenodes.h:2382
#define NIL
Definition: pg_list.h:68
FormData_pg_publication * Form_pg_publication
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:380
unsigned int Oid
Definition: postgres_ext.h:32
static void AlterPublicationSchemas(AlterPublicationStmt *stmt, HeapTuple tup, List *schemaidlist)
static void ObjectsInPublicationToOids(List *pubobjspec_list, ParseState *pstate, List **rels, List **schemas)
static void CheckAlterPublication(AlterPublicationStmt *stmt, HeapTuple tup, List *tables, List *schemaidlist)
static void AlterPublicationOptions(ParseState *pstate, AlterPublicationStmt *stmt, Relation rel, HeapTuple tup)
static void AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, List *tables, const char *queryString, bool publish_schema)
Definition: pg_list.h:54
const char * p_sourcetext
Definition: parse_node.h:195
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:91
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40

References AccessExclusiveLock, aclcheck_error(), ACLCHECK_NOT_OWNER, AlterPublicationOptions(), AlterPublicationSchemas(), AlterPublicationTables(), CheckAlterPublication(), CStringGetDatum(), ereport, errcode(), errmsg(), ERROR, GETSTRUCT(), GetUserId(), heap_freetuple(), HeapTupleIsValid, LockDatabaseObject(), NIL, object_ownercheck(), OBJECT_PUBLICATION, ObjectIdGetDatum(), ObjectsInPublicationToOids(), ParseState::p_sourcetext, RowExclusiveLock, SearchSysCacheCopy1, stmt, table_close(), and table_open().

Referenced by ProcessUtilitySlow().

◆ AlterPublicationOwner()

ObjectAddress AlterPublicationOwner ( const char *  name,
Oid  newOwnerId 
)

Definition at line 2108 of file publicationcmds.c.

2109{
2110 Oid pubid;
2111 HeapTuple tup;
2112 Relation rel;
2113 ObjectAddress address;
2114 Form_pg_publication pubform;
2115
2116 rel = table_open(PublicationRelationId, RowExclusiveLock);
2117
2118 tup = SearchSysCacheCopy1(PUBLICATIONNAME, CStringGetDatum(name));
2119
2120 if (!HeapTupleIsValid(tup))
2121 ereport(ERROR,
2122 (errcode(ERRCODE_UNDEFINED_OBJECT),
2123 errmsg("publication \"%s\" does not exist", name)));
2124
2125 pubform = (Form_pg_publication) GETSTRUCT(tup);
2126 pubid = pubform->oid;
2127
2128 AlterPublicationOwner_internal(rel, tup, newOwnerId);
2129
2130 ObjectAddressSet(address, PublicationRelationId, pubid);
2131
2132 heap_freetuple(tup);
2133
2135
2136 return address;
2137}
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
static void AlterPublicationOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
const char * name

References AlterPublicationOwner_internal(), CStringGetDatum(), ereport, errcode(), errmsg(), ERROR, GETSTRUCT(), heap_freetuple(), HeapTupleIsValid, name, ObjectAddressSet, RowExclusiveLock, SearchSysCacheCopy1, table_close(), and table_open().

Referenced by ExecAlterOwnerStmt().

◆ AlterPublicationOwner_oid()

void AlterPublicationOwner_oid ( Oid  pubid,
Oid  newOwnerId 
)

Definition at line 2143 of file publicationcmds.c.

2144{
2145 HeapTuple tup;
2146 Relation rel;
2147
2148 rel = table_open(PublicationRelationId, RowExclusiveLock);
2149
2150 tup = SearchSysCacheCopy1(PUBLICATIONOID, ObjectIdGetDatum(pubid));
2151
2152 if (!HeapTupleIsValid(tup))
2153 ereport(ERROR,
2154 (errcode(ERRCODE_UNDEFINED_OBJECT),
2155 errmsg("publication with OID %u does not exist", pubid)));
2156
2157 AlterPublicationOwner_internal(rel, tup, newOwnerId);
2158
2159 heap_freetuple(tup);
2160
2162}

References AlterPublicationOwner_internal(), ereport, errcode(), errmsg(), ERROR, heap_freetuple(), HeapTupleIsValid, ObjectIdGetDatum(), RowExclusiveLock, SearchSysCacheCopy1, table_close(), and table_open().

Referenced by shdepReassignOwned_Owner().

◆ CreatePublication()

ObjectAddress CreatePublication ( ParseState pstate,
CreatePublicationStmt stmt 
)

Definition at line 831 of file publicationcmds.c.

832{
833 Relation rel;
834 ObjectAddress myself;
835 Oid puboid;
836 bool nulls[Natts_pg_publication];
837 Datum values[Natts_pg_publication];
838 HeapTuple tup;
839 bool publish_given;
840 PublicationActions pubactions;
841 bool publish_via_partition_root_given;
842 bool publish_via_partition_root;
843 bool publish_generated_columns_given;
844 char publish_generated_columns;
845 AclResult aclresult;
846 List *relations = NIL;
847 List *schemaidlist = NIL;
848
849 /* must have CREATE privilege on database */
850 aclresult = object_aclcheck(DatabaseRelationId, MyDatabaseId, GetUserId(), ACL_CREATE);
851 if (aclresult != ACLCHECK_OK)
854
855 /* FOR ALL TABLES and FOR ALL SEQUENCES requires superuser */
856 if (!superuser())
857 {
858 if (stmt->for_all_tables || stmt->for_all_sequences)
860 errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
861 errmsg("must be superuser to create a FOR ALL TABLES or ALL SEQUENCES publication"));
862 }
863
864 rel = table_open(PublicationRelationId, RowExclusiveLock);
865
866 /* Check if name is used */
867 puboid = GetSysCacheOid1(PUBLICATIONNAME, Anum_pg_publication_oid,
868 CStringGetDatum(stmt->pubname));
869 if (OidIsValid(puboid))
872 errmsg("publication \"%s\" already exists",
873 stmt->pubname)));
874
875 /* Form a tuple. */
876 memset(values, 0, sizeof(values));
877 memset(nulls, false, sizeof(nulls));
878
879 values[Anum_pg_publication_pubname - 1] =
881 values[Anum_pg_publication_pubowner - 1] = ObjectIdGetDatum(GetUserId());
882
884 stmt->options,
885 &publish_given, &pubactions,
886 &publish_via_partition_root_given,
887 &publish_via_partition_root,
888 &publish_generated_columns_given,
889 &publish_generated_columns);
890
891 if (stmt->for_all_sequences &&
892 (publish_given || publish_via_partition_root_given ||
893 publish_generated_columns_given))
895 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
896 errmsg("publication parameters are not applicable to sequence synchronization and will be ignored for sequences"));
897
898 puboid = GetNewOidWithIndex(rel, PublicationObjectIndexId,
899 Anum_pg_publication_oid);
900 values[Anum_pg_publication_oid - 1] = ObjectIdGetDatum(puboid);
901 values[Anum_pg_publication_puballtables - 1] =
902 BoolGetDatum(stmt->for_all_tables);
903 values[Anum_pg_publication_puballsequences - 1] =
904 BoolGetDatum(stmt->for_all_sequences);
905 values[Anum_pg_publication_pubinsert - 1] =
906 BoolGetDatum(pubactions.pubinsert);
907 values[Anum_pg_publication_pubupdate - 1] =
908 BoolGetDatum(pubactions.pubupdate);
909 values[Anum_pg_publication_pubdelete - 1] =
910 BoolGetDatum(pubactions.pubdelete);
911 values[Anum_pg_publication_pubtruncate - 1] =
912 BoolGetDatum(pubactions.pubtruncate);
913 values[Anum_pg_publication_pubviaroot - 1] =
914 BoolGetDatum(publish_via_partition_root);
915 values[Anum_pg_publication_pubgencols - 1] =
916 CharGetDatum(publish_generated_columns);
917
918 tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
919
920 /* Insert tuple into catalog. */
921 CatalogTupleInsert(rel, tup);
922 heap_freetuple(tup);
923
924 recordDependencyOnOwner(PublicationRelationId, puboid, GetUserId());
925
926 ObjectAddressSet(myself, PublicationRelationId, puboid);
927
928 /* Make the changes visible. */
930
931 /* Associate objects with the publication. */
932 if (stmt->for_all_tables)
933 {
934 /*
935 * Invalidate relcache so that publication info is rebuilt. Sequences
936 * publication doesn't require invalidation, as replica identity
937 * checks don't apply to them.
938 */
940 }
941 else if (!stmt->for_all_sequences)
942 {
943 ObjectsInPublicationToOids(stmt->pubobjects, pstate, &relations,
944 &schemaidlist);
945
946 /* FOR TABLES IN SCHEMA requires superuser */
947 if (schemaidlist != NIL && !superuser())
949 errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
950 errmsg("must be superuser to create FOR TABLES IN SCHEMA publication"));
951
952 if (relations != NIL)
953 {
954 List *rels;
955
956 rels = OpenTableList(relations);
958 publish_via_partition_root);
959
960 CheckPubRelationColumnList(stmt->pubname, rels,
961 schemaidlist != NIL,
962 publish_via_partition_root);
963
964 PublicationAddTables(puboid, rels, true, NULL);
965 CloseTableList(rels);
966 }
967
968 if (schemaidlist != NIL)
969 {
970 /*
971 * Schema lock is held until the publication is created to prevent
972 * concurrent schema deletion.
973 */
974 LockSchemaList(schemaidlist);
975 PublicationAddSchemas(puboid, schemaidlist, true, NULL);
976 }
977 }
978
980
981 InvokeObjectPostCreateHook(PublicationRelationId, puboid, 0);
982
983 /*
984 * We don't need this warning message when wal_level >= 'replica' since
985 * logical decoding is automatically enabled up on a logical slot
986 * creation.
987 */
990 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
991 errmsg("logical decoding must be enabled to publish logical changes"),
992 errhint("Before creating subscriptions, ensure that \"wal_level\" is set to \"replica\" or higher.")));
993
994 return myself;
995}
AclResult
Definition: acl.h:182
@ ACLCHECK_OK
Definition: acl.h:183
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
Definition: aclchk.c:3836
static Datum values[MAXATTR]
Definition: bootstrap.c:155
#define OidIsValid(objectId)
Definition: c.h:794
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:448
int errhint(const char *fmt,...)
Definition: elog.c:1330
#define WARNING
Definition: elog.h:36
#define NOTICE
Definition: elog.h:35
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:684
Oid MyDatabaseId
Definition: globals.c:94
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:233
void CacheInvalidateRelcacheAll(void)
Definition: inval.c:1658
char * get_database_name(Oid dbid)
Definition: lsyscache.c:1242
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:173
@ OBJECT_DATABASE
Definition: parsenodes.h:2361
#define ACL_CREATE
Definition: parsenodes.h:85
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:168
static Datum BoolGetDatum(bool X)
Definition: postgres.h:112
uint64_t Datum
Definition: postgres.h:70
static Datum CharGetDatum(char X)
Definition: postgres.h:132
static void PublicationAddSchemas(Oid pubid, List *schemas, bool if_not_exists, AlterPublicationStmt *stmt)
static void PublicationAddTables(Oid pubid, List *rels, bool if_not_exists, AlterPublicationStmt *stmt)
static void CloseTableList(List *rels)
static void TransformPubWhereClauses(List *tables, const char *queryString, bool pubviaroot)
static List * OpenTableList(List *tables)
static void parse_publication_options(ParseState *pstate, List *options, bool *publish_given, PublicationActions *pubactions, bool *publish_via_partition_root_given, bool *publish_via_partition_root, bool *publish_generated_columns_given, char *publish_generated_columns)
static void CheckPubRelationColumnList(char *pubname, List *tables, bool publish_schema, bool pubviaroot)
static void LockSchemaList(List *schemalist)
#define RelationGetDescr(relation)
Definition: rel.h:541
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:30
bool superuser(void)
Definition: superuser.c:46
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition: syscache.h:109
void CommandCounterIncrement(void)
Definition: xact.c:1101
int wal_level
Definition: xlog.c:134
@ WAL_LEVEL_REPLICA
Definition: xlog.h:76

References ACL_CREATE, aclcheck_error(), ACLCHECK_OK, BoolGetDatum(), CacheInvalidateRelcacheAll(), CatalogTupleInsert(), CharGetDatum(), CheckPubRelationColumnList(), CloseTableList(), CommandCounterIncrement(), CStringGetDatum(), DirectFunctionCall1, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errhint(), errmsg(), ERROR, get_database_name(), GetNewOidWithIndex(), GetSysCacheOid1, GetUserId(), heap_form_tuple(), heap_freetuple(), InvokeObjectPostCreateHook, LockSchemaList(), MyDatabaseId, namein(), NIL, NOTICE, object_aclcheck(), OBJECT_DATABASE, ObjectAddressSet, ObjectIdGetDatum(), ObjectsInPublicationToOids(), OidIsValid, OpenTableList(), ParseState::p_sourcetext, parse_publication_options(), PublicationActions::pubdelete, PublicationActions::pubinsert, PublicationAddSchemas(), PublicationAddTables(), PublicationActions::pubtruncate, PublicationActions::pubupdate, recordDependencyOnOwner(), RelationGetDescr, RowExclusiveLock, stmt, superuser(), table_close(), table_open(), TransformPubWhereClauses(), values, wal_level, WAL_LEVEL_REPLICA, and WARNING.

Referenced by ProcessUtilitySlow().

◆ InvalidatePublicationRels()

void InvalidatePublicationRels ( List relids)

Definition at line 1205 of file publicationcmds.c.

1206{
1207 /*
1208 * We don't want to send too many individual messages, at some point it's
1209 * cheaper to just reset whole relcache.
1210 */
1212 {
1213 ListCell *lc;
1214
1215 foreach(lc, relids)
1217 }
1218 else
1220}
void CacheInvalidateRelcacheByRelid(Oid relid)
Definition: inval.c:1691
static int list_length(const List *l)
Definition: pg_list.h:152
#define lfirst_oid(lc)
Definition: pg_list.h:174
#define MAX_RELCACHE_INVAL_MSGS

References CacheInvalidateRelcacheAll(), CacheInvalidateRelcacheByRelid(), lfirst_oid, list_length(), and MAX_RELCACHE_INVAL_MSGS.

Referenced by AlterPublicationOptions(), publication_add_relation(), publication_add_schema(), RemovePublicationRelById(), and RemovePublicationSchemaById().

◆ InvalidatePubRelSyncCache()

void InvalidatePubRelSyncCache ( Oid  pubid,
bool  puballtables 
)

Definition at line 505 of file publicationcmds.c.

506{
507 if (puballtables)
508 {
510 }
511 else
512 {
513 List *relids = NIL;
514 List *schemarelids = NIL;
515
516 /*
517 * For partitioned tables, we must invalidate all partitions and
518 * itself. WAL records for INSERT/UPDATE/DELETE specify leaf tables as
519 * a target. However, WAL records for TRUNCATE specify both a root and
520 * its leaves.
521 */
522 relids = GetPublicationRelations(pubid,
524 schemarelids = GetAllSchemaPublicationRelations(pubid,
526
527 relids = list_concat_unique_oid(relids, schemarelids);
528
529 /* Invalidate the relsyncache */
530 foreach_oid(relid, relids)
532 }
533
534 return;
535}
void CacheInvalidateRelSyncAll(void)
Definition: inval.c:1724
void CacheInvalidateRelSync(Oid relid)
Definition: inval.c:1712
List * list_concat_unique_oid(List *list1, const List *list2)
Definition: list.c:1469
#define foreach_oid(var, lst)
Definition: pg_list.h:471
List * GetAllSchemaPublicationRelations(Oid pubid, PublicationPartOpt pub_partopt)
List * GetPublicationRelations(Oid pubid, PublicationPartOpt pub_partopt)
@ PUBLICATION_PART_ALL

References CacheInvalidateRelSync(), CacheInvalidateRelSyncAll(), foreach_oid, GetAllSchemaPublicationRelations(), GetPublicationRelations(), list_concat_unique_oid(), NIL, and PUBLICATION_PART_ALL.

Referenced by AlterObjectRename_internal().

◆ pub_contains_invalid_column()

bool pub_contains_invalid_column ( Oid  pubid,
Relation  relation,
List ancestors,
bool  pubviaroot,
char  pubgencols_type,
bool *  invalid_column_list,
bool *  invalid_gen_col 
)

Definition at line 361 of file publicationcmds.c.

365{
366 Oid relid = RelationGetRelid(relation);
367 Oid publish_as_relid = RelationGetRelid(relation);
368 Bitmapset *idattrs;
369 Bitmapset *columns = NULL;
370 TupleDesc desc = RelationGetDescr(relation);
371 Publication *pub;
372 int x;
373
374 *invalid_column_list = false;
375 *invalid_gen_col = false;
376
377 /*
378 * For a partition, if pubviaroot is true, find the topmost ancestor that
379 * is published via this publication as we need to use its column list for
380 * the changes.
381 *
382 * Note that even though the column list used is for an ancestor, the
383 * REPLICA IDENTITY used will be for the actual child table.
384 */
385 if (pubviaroot && relation->rd_rel->relispartition)
386 {
387 publish_as_relid = GetTopMostAncestorInPublication(pubid, ancestors, NULL);
388
389 if (!OidIsValid(publish_as_relid))
390 publish_as_relid = relid;
391 }
392
393 /* Fetch the column list */
394 pub = GetPublication(pubid);
395 check_and_fetch_column_list(pub, publish_as_relid, NULL, &columns);
396
397 if (relation->rd_rel->relreplident == REPLICA_IDENTITY_FULL)
398 {
399 /* With REPLICA IDENTITY FULL, no column list is allowed. */
400 *invalid_column_list = (columns != NULL);
401
402 /*
403 * As we don't allow a column list with REPLICA IDENTITY FULL, the
404 * publish_generated_columns option must be set to stored if the table
405 * has any stored generated columns.
406 */
407 if (pubgencols_type != PUBLISH_GENCOLS_STORED &&
408 relation->rd_att->constr &&
410 *invalid_gen_col = true;
411
412 /*
413 * Virtual generated columns are currently not supported for logical
414 * replication at all.
415 */
416 if (relation->rd_att->constr &&
418 *invalid_gen_col = true;
419
420 if (*invalid_gen_col && *invalid_column_list)
421 return true;
422 }
423
424 /* Remember columns that are part of the REPLICA IDENTITY */
425 idattrs = RelationGetIndexAttrBitmap(relation,
427
428 /*
429 * Attnums in the bitmap returned by RelationGetIndexAttrBitmap are offset
430 * (to handle system columns the usual way), while column list does not
431 * use offset, so we can't do bms_is_subset(). Instead, we have to loop
432 * over the idattrs and check all of them are in the list.
433 */
434 x = -1;
435 while ((x = bms_next_member(idattrs, x)) >= 0)
436 {
438 Form_pg_attribute att = TupleDescAttr(desc, attnum - 1);
439
440 if (columns == NULL)
441 {
442 /*
443 * The publish_generated_columns option must be set to stored if
444 * the REPLICA IDENTITY contains any stored generated column.
445 */
446 if (att->attgenerated == ATTRIBUTE_GENERATED_STORED && pubgencols_type != PUBLISH_GENCOLS_STORED)
447 {
448 *invalid_gen_col = true;
449 break;
450 }
451
452 /*
453 * The equivalent setting for virtual generated columns does not
454 * exist yet.
455 */
456 if (att->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
457 {
458 *invalid_gen_col = true;
459 break;
460 }
461
462 /* Skip validating the column list since it is not defined */
463 continue;
464 }
465
466 /*
467 * If pubviaroot is true, we are validating the column list of the
468 * parent table, but the bitmap contains the replica identity
469 * information of the child table. The parent/child attnums may not
470 * match, so translate them to the parent - get the attname from the
471 * child, and look it up in the parent.
472 */
473 if (pubviaroot)
474 {
475 /* attribute name in the child table */
476 char *colname = get_attname(relid, attnum, false);
477
478 /*
479 * Determine the attnum for the attribute name in parent (we are
480 * using the column list defined on the parent).
481 */
482 attnum = get_attnum(publish_as_relid, colname);
483 }
484
485 /* replica identity column, not covered by the column list */
486 *invalid_column_list |= !bms_is_member(attnum, columns);
487
488 if (*invalid_column_list && *invalid_gen_col)
489 break;
490 }
491
492 bms_free(columns);
493 bms_free(idattrs);
494
495 return *invalid_column_list || *invalid_gen_col;
496}
int16 AttrNumber
Definition: attnum.h:21
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1305
void bms_free(Bitmapset *a)
Definition: bitmapset.c:239
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
int x
Definition: isn.c:75
AttrNumber get_attnum(Oid relid, const char *attname)
Definition: lsyscache.c:934
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition: lsyscache.c:903
int16 attnum
Definition: pg_attribute.h:74
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:202
Oid GetTopMostAncestorInPublication(Oid puboid, List *ancestors, int *ancestor_level)
Publication * GetPublication(Oid pubid)
bool check_and_fetch_column_list(Publication *pub, Oid relid, MemoryContext mcxt, Bitmapset **cols)
#define RelationGetRelid(relation)
Definition: rel.h:515
Bitmapset * RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind)
Definition: relcache.c:5298
@ INDEX_ATTR_BITMAP_IDENTITY_KEY
Definition: relcache.h:71
TupleDesc rd_att
Definition: rel.h:112
Form_pg_class rd_rel
Definition: rel.h:111
bool has_generated_virtual
Definition: tupdesc.h:47
bool has_generated_stored
Definition: tupdesc.h:46
TupleConstr * constr
Definition: tupdesc.h:141
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:160

References attnum, bms_free(), bms_is_member(), bms_next_member(), check_and_fetch_column_list(), TupleDescData::constr, FirstLowInvalidHeapAttributeNumber, get_attname(), get_attnum(), GetPublication(), GetTopMostAncestorInPublication(), TupleConstr::has_generated_stored, TupleConstr::has_generated_virtual, INDEX_ATTR_BITMAP_IDENTITY_KEY, OidIsValid, RelationData::rd_att, RelationData::rd_rel, RelationGetDescr, RelationGetIndexAttrBitmap(), RelationGetRelid, TupleDescAttr(), and x.

Referenced by RelationBuildPublicationDesc().

◆ pub_rf_contains_invalid_column()

bool pub_rf_contains_invalid_column ( Oid  pubid,
Relation  relation,
List ancestors,
bool  pubviaroot 
)

Definition at line 275 of file publicationcmds.c.

277{
278 HeapTuple rftuple;
279 Oid relid = RelationGetRelid(relation);
280 Oid publish_as_relid = RelationGetRelid(relation);
281 bool result = false;
282 Datum rfdatum;
283 bool rfisnull;
284
285 /*
286 * FULL means all columns are in the REPLICA IDENTITY, so all columns are
287 * allowed in the row filter and we can skip the validation.
288 */
289 if (relation->rd_rel->relreplident == REPLICA_IDENTITY_FULL)
290 return false;
291
292 /*
293 * For a partition, if pubviaroot is true, find the topmost ancestor that
294 * is published via this publication as we need to use its row filter
295 * expression to filter the partition's changes.
296 *
297 * Note that even though the row filter used is for an ancestor, the
298 * REPLICA IDENTITY used will be for the actual child table.
299 */
300 if (pubviaroot && relation->rd_rel->relispartition)
301 {
302 publish_as_relid
303 = GetTopMostAncestorInPublication(pubid, ancestors, NULL);
304
305 if (!OidIsValid(publish_as_relid))
306 publish_as_relid = relid;
307 }
308
309 rftuple = SearchSysCache2(PUBLICATIONRELMAP,
310 ObjectIdGetDatum(publish_as_relid),
311 ObjectIdGetDatum(pubid));
312
313 if (!HeapTupleIsValid(rftuple))
314 return false;
315
316 rfdatum = SysCacheGetAttr(PUBLICATIONRELMAP, rftuple,
317 Anum_pg_publication_rel_prqual,
318 &rfisnull);
319
320 if (!rfisnull)
321 {
322 rf_context context = {0};
323 Node *rfnode;
324 Bitmapset *bms = NULL;
325
326 context.pubviaroot = pubviaroot;
327 context.parentid = publish_as_relid;
328 context.relid = relid;
329
330 /* Remember columns that are part of the REPLICA IDENTITY */
331 bms = RelationGetIndexAttrBitmap(relation,
333
334 context.bms_replident = bms;
335 rfnode = stringToNode(TextDatumGetCString(rfdatum));
336 result = contain_invalid_rfcolumn_walker(rfnode, &context);
337 }
338
339 ReleaseSysCache(rftuple);
340
341 return result;
342}
#define TextDatumGetCString(d)
Definition: builtins.h:98
static bool contain_invalid_rfcolumn_walker(Node *node, rf_context *context)
void * stringToNode(const char *str)
Definition: read.c:90
Definition: nodes.h:135
Bitmapset * bms_replident
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:264
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:595
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:230

References rf_context::bms_replident, contain_invalid_rfcolumn_walker(), GetTopMostAncestorInPublication(), HeapTupleIsValid, INDEX_ATTR_BITMAP_IDENTITY_KEY, ObjectIdGetDatum(), OidIsValid, rf_context::parentid, rf_context::pubviaroot, RelationData::rd_rel, RelationGetIndexAttrBitmap(), RelationGetRelid, ReleaseSysCache(), rf_context::relid, SearchSysCache2(), stringToNode(), SysCacheGetAttr(), and TextDatumGetCString.

Referenced by RelationBuildPublicationDesc().

◆ RemovePublicationById()

void RemovePublicationById ( Oid  pubid)

Definition at line 1651 of file publicationcmds.c.

1652{
1653 Relation rel;
1654 HeapTuple tup;
1655 Form_pg_publication pubform;
1656
1657 rel = table_open(PublicationRelationId, RowExclusiveLock);
1658
1659 tup = SearchSysCache1(PUBLICATIONOID, ObjectIdGetDatum(pubid));
1660 if (!HeapTupleIsValid(tup))
1661 elog(ERROR, "cache lookup failed for publication %u", pubid);
1662
1663 pubform = (Form_pg_publication) GETSTRUCT(tup);
1664
1665 /* Invalidate relcache so that publication info is rebuilt. */
1666 if (pubform->puballtables)
1668
1669 CatalogTupleDelete(rel, &tup->t_self);
1670
1671 ReleaseSysCache(tup);
1672
1674}
#define elog(elevel,...)
Definition: elog.h:226
void CatalogTupleDelete(Relation heapRel, const ItemPointerData *tid)
Definition: indexing.c:365
ItemPointerData t_self
Definition: htup.h:65
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:220

References CacheInvalidateRelcacheAll(), CatalogTupleDelete(), elog, ERROR, GETSTRUCT(), HeapTupleIsValid, ObjectIdGetDatum(), ReleaseSysCache(), RowExclusiveLock, SearchSysCache1(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by doDeletion().

◆ RemovePublicationRelById()

void RemovePublicationRelById ( Oid  proid)

Definition at line 1610 of file publicationcmds.c.

1611{
1612 Relation rel;
1613 HeapTuple tup;
1615 List *relids = NIL;
1616
1617 rel = table_open(PublicationRelRelationId, RowExclusiveLock);
1618
1619 tup = SearchSysCache1(PUBLICATIONREL, ObjectIdGetDatum(proid));
1620
1621 if (!HeapTupleIsValid(tup))
1622 elog(ERROR, "cache lookup failed for publication table %u",
1623 proid);
1624
1625 pubrel = (Form_pg_publication_rel) GETSTRUCT(tup);
1626
1627 /*
1628 * Invalidate relcache so that publication info is rebuilt.
1629 *
1630 * For the partitioned tables, we must invalidate all partitions contained
1631 * in the respective partition hierarchies, not just the one explicitly
1632 * mentioned in the publication. This is required because we implicitly
1633 * publish the child tables when the parent table is published.
1634 */
1636 pubrel->prrelid);
1637
1639
1640 CatalogTupleDelete(rel, &tup->t_self);
1641
1642 ReleaseSysCache(tup);
1643
1645}
List * GetPubPartitionOptionRelations(List *result, PublicationPartOpt pub_partopt, Oid relid)
FormData_pg_publication_rel * Form_pg_publication_rel
void InvalidatePublicationRels(List *relids)

References CatalogTupleDelete(), elog, ERROR, GetPubPartitionOptionRelations(), GETSTRUCT(), HeapTupleIsValid, InvalidatePublicationRels(), NIL, ObjectIdGetDatum(), PUBLICATION_PART_ALL, ReleaseSysCache(), RowExclusiveLock, SearchSysCache1(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by doDeletion().

◆ RemovePublicationSchemaById()

void RemovePublicationSchemaById ( Oid  psoid)

Definition at line 1680 of file publicationcmds.c.

1681{
1682 Relation rel;
1683 HeapTuple tup;
1684 List *schemaRels = NIL;
1686
1687 rel = table_open(PublicationNamespaceRelationId, RowExclusiveLock);
1688
1689 tup = SearchSysCache1(PUBLICATIONNAMESPACE, ObjectIdGetDatum(psoid));
1690
1691 if (!HeapTupleIsValid(tup))
1692 elog(ERROR, "cache lookup failed for publication schema %u", psoid);
1693
1695
1696 /*
1697 * Invalidate relcache so that publication info is rebuilt. See
1698 * RemovePublicationRelById for why we need to consider all the
1699 * partitions.
1700 */
1701 schemaRels = GetSchemaPublicationRelations(pubsch->pnnspid,
1703 InvalidatePublicationRels(schemaRels);
1704
1705 CatalogTupleDelete(rel, &tup->t_self);
1706
1707 ReleaseSysCache(tup);
1708
1710}
List * GetSchemaPublicationRelations(Oid schemaid, PublicationPartOpt pub_partopt)
FormData_pg_publication_namespace * Form_pg_publication_namespace

References CatalogTupleDelete(), elog, ERROR, GetSchemaPublicationRelations(), GETSTRUCT(), HeapTupleIsValid, InvalidatePublicationRels(), NIL, ObjectIdGetDatum(), PUBLICATION_PART_ALL, ReleaseSysCache(), RowExclusiveLock, SearchSysCache1(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by doDeletion().