PostgreSQL Source Code git master
publicationcmds.c File Reference
Include dependency graph for publicationcmds.c:

Go to the source code of this file.

Data Structures

struct  rf_context
 

Typedefs

typedef struct rf_context rf_context
 

Functions

static ListOpenTableList (List *tables)
 
static void CloseTableList (List *rels)
 
static void LockSchemaList (List *schemalist)
 
static void PublicationAddTables (Oid pubid, List *rels, bool if_not_exists, AlterPublicationStmt *stmt)
 
static void PublicationDropTables (Oid pubid, List *rels, bool missing_ok)
 
static void PublicationAddSchemas (Oid pubid, List *schemas, bool if_not_exists, AlterPublicationStmt *stmt)
 
static void PublicationDropSchemas (Oid pubid, List *schemas, bool missing_ok)
 
static char defGetGeneratedColsOption (DefElem *def)
 
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 ObjectsInPublicationToOids (List *pubobjspec_list, ParseState *pstate, List **rels, List **schemas)
 
static bool contain_invalid_rfcolumn_walker (Node *node, rf_context *context)
 
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)
 
static bool contain_mutable_or_user_functions_checker (Oid func_id, void *context)
 
static bool check_simple_rowfilter_expr_walker (Node *node, ParseState *pstate)
 
static bool check_simple_rowfilter_expr (Node *node, ParseState *pstate)
 
static void TransformPubWhereClauses (List *tables, const char *queryString, bool pubviaroot)
 
static void CheckPubRelationColumnList (char *pubname, List *tables, bool publish_schema, bool pubviaroot)
 
ObjectAddress CreatePublication (ParseState *pstate, CreatePublicationStmt *stmt)
 
static void AlterPublicationOptions (ParseState *pstate, AlterPublicationStmt *stmt, Relation rel, HeapTuple tup)
 
void InvalidatePublicationRels (List *relids)
 
static void AlterPublicationTables (AlterPublicationStmt *stmt, HeapTuple tup, List *tables, const char *queryString, bool publish_schema)
 
static void AlterPublicationSchemas (AlterPublicationStmt *stmt, HeapTuple tup, List *schemaidlist)
 
static void CheckAlterPublication (AlterPublicationStmt *stmt, HeapTuple tup, List *tables, List *schemaidlist)
 
void AlterPublication (ParseState *pstate, AlterPublicationStmt *stmt)
 
void RemovePublicationRelById (Oid proid)
 
void RemovePublicationById (Oid pubid)
 
void RemovePublicationSchemaById (Oid psoid)
 
static void AlterPublicationOwner_internal (Relation rel, HeapTuple tup, Oid newOwnerId)
 
ObjectAddress AlterPublicationOwner (const char *name, Oid newOwnerId)
 
void AlterPublicationOwner_oid (Oid subid, Oid newOwnerId)
 

Typedef Documentation

◆ rf_context

typedef struct rf_context rf_context

Function Documentation

◆ AlterPublication()

void AlterPublication ( ParseState pstate,
AlterPublicationStmt stmt 
)

Definition at line 1418 of file publicationcmds.c.

1419{
1420 Relation rel;
1421 HeapTuple tup;
1422 Form_pg_publication pubform;
1423
1424 rel = table_open(PublicationRelationId, RowExclusiveLock);
1425
1426 tup = SearchSysCacheCopy1(PUBLICATIONNAME,
1427 CStringGetDatum(stmt->pubname));
1428
1429 if (!HeapTupleIsValid(tup))
1430 ereport(ERROR,
1431 (errcode(ERRCODE_UNDEFINED_OBJECT),
1432 errmsg("publication \"%s\" does not exist",
1433 stmt->pubname)));
1434
1435 pubform = (Form_pg_publication) GETSTRUCT(tup);
1436
1437 /* must be owner */
1438 if (!object_ownercheck(PublicationRelationId, pubform->oid, GetUserId()))
1440 stmt->pubname);
1441
1442 if (stmt->options)
1443 AlterPublicationOptions(pstate, stmt, rel, tup);
1444 else
1445 {
1446 List *relations = NIL;
1447 List *schemaidlist = NIL;
1448 Oid pubid = pubform->oid;
1449
1450 ObjectsInPublicationToOids(stmt->pubobjects, pstate, &relations,
1451 &schemaidlist);
1452
1453 CheckAlterPublication(stmt, tup, relations, schemaidlist);
1454
1455 heap_freetuple(tup);
1456
1457 /* Lock the publication so nobody else can do anything with it. */
1458 LockDatabaseObject(PublicationRelationId, pubid, 0,
1460
1461 /*
1462 * It is possible that by the time we acquire the lock on publication,
1463 * concurrent DDL has removed it. We can test this by checking the
1464 * existence of publication. We get the tuple again to avoid the risk
1465 * of any publication option getting changed.
1466 */
1467 tup = SearchSysCacheCopy1(PUBLICATIONOID, ObjectIdGetDatum(pubid));
1468 if (!HeapTupleIsValid(tup))
1469 ereport(ERROR,
1470 errcode(ERRCODE_UNDEFINED_OBJECT),
1471 errmsg("publication \"%s\" does not exist",
1472 stmt->pubname));
1473
1474 AlterPublicationTables(stmt, tup, relations, pstate->p_sourcetext,
1475 schemaidlist != NIL);
1476 AlterPublicationSchemas(stmt, tup, schemaidlist);
1477 }
1478
1479 /* Cleanup. */
1480 heap_freetuple(tup);
1482}
@ ACLCHECK_NOT_OWNER
Definition: acl.h:185
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:2622
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Definition: aclchk.c:4058
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
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:993
#define AccessExclusiveLock
Definition: lockdefs.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
Oid GetUserId(void)
Definition: miscinit.c:517
@ OBJECT_PUBLICATION
Definition: parsenodes.h:2342
#define NIL
Definition: pg_list.h:68
FormData_pg_publication * Form_pg_publication
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:355
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:209
#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().

◆ AlterPublicationOptions()

static void AlterPublicationOptions ( ParseState pstate,
AlterPublicationStmt stmt,
Relation  rel,
HeapTuple  tup 
)
static

Definition at line 916 of file publicationcmds.c.

918{
919 bool nulls[Natts_pg_publication];
920 bool replaces[Natts_pg_publication];
921 Datum values[Natts_pg_publication];
922 bool publish_given;
923 PublicationActions pubactions;
924 bool publish_via_partition_root_given;
925 bool publish_via_partition_root;
926 bool publish_generated_columns_given;
927 char publish_generated_columns;
928 ObjectAddress obj;
929 Form_pg_publication pubform;
930 List *root_relids = NIL;
931 ListCell *lc;
932
934 stmt->options,
935 &publish_given, &pubactions,
936 &publish_via_partition_root_given,
937 &publish_via_partition_root,
938 &publish_generated_columns_given,
939 &publish_generated_columns);
940
941 pubform = (Form_pg_publication) GETSTRUCT(tup);
942
943 /*
944 * If the publication doesn't publish changes via the root partitioned
945 * table, the partition's row filter and column list will be used. So
946 * disallow using WHERE clause and column lists on partitioned table in
947 * this case.
948 */
949 if (!pubform->puballtables && publish_via_partition_root_given &&
950 !publish_via_partition_root)
951 {
952 /*
953 * Lock the publication so nobody else can do anything with it. This
954 * prevents concurrent alter to add partitioned table(s) with WHERE
955 * clause(s) and/or column lists which we don't allow when not
956 * publishing via root.
957 */
958 LockDatabaseObject(PublicationRelationId, pubform->oid, 0,
960
961 root_relids = GetPublicationRelations(pubform->oid,
963
964 foreach(lc, root_relids)
965 {
966 Oid relid = lfirst_oid(lc);
967 HeapTuple rftuple;
968 char relkind;
969 char *relname;
970 bool has_rowfilter;
971 bool has_collist;
972
973 /*
974 * Beware: we don't have lock on the relations, so cope silently
975 * with the cache lookups returning NULL.
976 */
977
978 rftuple = SearchSysCache2(PUBLICATIONRELMAP,
979 ObjectIdGetDatum(relid),
980 ObjectIdGetDatum(pubform->oid));
981 if (!HeapTupleIsValid(rftuple))
982 continue;
983 has_rowfilter = !heap_attisnull(rftuple, Anum_pg_publication_rel_prqual, NULL);
984 has_collist = !heap_attisnull(rftuple, Anum_pg_publication_rel_prattrs, NULL);
985 if (!has_rowfilter && !has_collist)
986 {
987 ReleaseSysCache(rftuple);
988 continue;
989 }
990
991 relkind = get_rel_relkind(relid);
992 if (relkind != RELKIND_PARTITIONED_TABLE)
993 {
994 ReleaseSysCache(rftuple);
995 continue;
996 }
997 relname = get_rel_name(relid);
998 if (relname == NULL) /* table concurrently dropped */
999 {
1000 ReleaseSysCache(rftuple);
1001 continue;
1002 }
1003
1004 if (has_rowfilter)
1005 ereport(ERROR,
1006 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1007 errmsg("cannot set parameter \"%s\" to false for publication \"%s\"",
1008 "publish_via_partition_root",
1009 stmt->pubname),
1010 errdetail("The publication contains a WHERE clause for partitioned table \"%s\", which is not allowed when \"%s\" is false.",
1011 relname, "publish_via_partition_root")));
1012 Assert(has_collist);
1013 ereport(ERROR,
1014 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1015 errmsg("cannot set parameter \"%s\" to false for publication \"%s\"",
1016 "publish_via_partition_root",
1017 stmt->pubname),
1018 errdetail("The publication contains a column list for partitioned table \"%s\", which is not allowed when \"%s\" is false.",
1019 relname, "publish_via_partition_root")));
1020 }
1021 }
1022
1023 /* Everything ok, form a new tuple. */
1024 memset(values, 0, sizeof(values));
1025 memset(nulls, false, sizeof(nulls));
1026 memset(replaces, false, sizeof(replaces));
1027
1028 if (publish_given)
1029 {
1030 values[Anum_pg_publication_pubinsert - 1] = BoolGetDatum(pubactions.pubinsert);
1031 replaces[Anum_pg_publication_pubinsert - 1] = true;
1032
1033 values[Anum_pg_publication_pubupdate - 1] = BoolGetDatum(pubactions.pubupdate);
1034 replaces[Anum_pg_publication_pubupdate - 1] = true;
1035
1036 values[Anum_pg_publication_pubdelete - 1] = BoolGetDatum(pubactions.pubdelete);
1037 replaces[Anum_pg_publication_pubdelete - 1] = true;
1038
1039 values[Anum_pg_publication_pubtruncate - 1] = BoolGetDatum(pubactions.pubtruncate);
1040 replaces[Anum_pg_publication_pubtruncate - 1] = true;
1041 }
1042
1043 if (publish_via_partition_root_given)
1044 {
1045 values[Anum_pg_publication_pubviaroot - 1] = BoolGetDatum(publish_via_partition_root);
1046 replaces[Anum_pg_publication_pubviaroot - 1] = true;
1047 }
1048
1049 if (publish_generated_columns_given)
1050 {
1051 values[Anum_pg_publication_pubgencols - 1] = CharGetDatum(publish_generated_columns);
1052 replaces[Anum_pg_publication_pubgencols - 1] = true;
1053 }
1054
1055 tup = heap_modify_tuple(tup, RelationGetDescr(rel), values, nulls,
1056 replaces);
1057
1058 /* Update the catalog. */
1059 CatalogTupleUpdate(rel, &tup->t_self, tup);
1060
1062
1063 pubform = (Form_pg_publication) GETSTRUCT(tup);
1064
1065 /* Invalidate the relcache. */
1066 if (pubform->puballtables)
1067 {
1069 }
1070 else
1071 {
1072 List *relids = NIL;
1073 List *schemarelids = NIL;
1074
1075 /*
1076 * For any partitioned tables contained in the publication, we must
1077 * invalidate all partitions contained in the respective partition
1078 * trees, not just those explicitly mentioned in the publication.
1079 */
1080 if (root_relids == NIL)
1081 relids = GetPublicationRelations(pubform->oid,
1083 else
1084 {
1085 /*
1086 * We already got tables explicitly mentioned in the publication.
1087 * Now get all partitions for the partitioned table in the list.
1088 */
1089 foreach(lc, root_relids)
1090 relids = GetPubPartitionOptionRelations(relids,
1092 lfirst_oid(lc));
1093 }
1094
1095 schemarelids = GetAllSchemaPublicationRelations(pubform->oid,
1097 relids = list_concat_unique_oid(relids, schemarelids);
1098
1100 }
1101
1102 ObjectAddressSet(obj, PublicationRelationId, pubform->oid);
1104 (Node *) stmt);
1105
1106 InvokeObjectPostAlterHook(PublicationRelationId, pubform->oid, 0);
1107}
static Datum values[MAXATTR]
Definition: bootstrap.c:151
#define Assert(condition)
Definition: c.h:815
int errdetail(const char *fmt,...)
Definition: elog.c:1203
void EventTriggerCollectSimpleCommand(ObjectAddress address, ObjectAddress secondaryObject, Node *parsetree)
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
Definition: heaptuple.c:1210
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:456
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:313
void CacheInvalidateRelcacheAll(void)
Definition: inval.c:1579
List * list_concat_unique_oid(List *list1, const List *list2)
Definition: list.c:1469
#define AccessShareLock
Definition: lockdefs.h:36
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1955
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:2030
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:197
const ObjectAddress InvalidObjectAddress
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
NameData relname
Definition: pg_class.h:38
#define lfirst_oid(lc)
Definition: pg_list.h:174
List * GetPubPartitionOptionRelations(List *result, PublicationPartOpt pub_partopt, Oid relid)
List * GetAllSchemaPublicationRelations(Oid pubid, PublicationPartOpt pub_partopt)
List * GetPublicationRelations(Oid pubid, PublicationPartOpt pub_partopt)
@ PUBLICATION_PART_ROOT
@ PUBLICATION_PART_ALL
uintptr_t Datum
Definition: postgres.h:69
static Datum BoolGetDatum(bool X)
Definition: postgres.h:107
static Datum CharGetDatum(char X)
Definition: postgres.h:127
void InvalidatePublicationRels(List *relids)
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)
#define RelationGetDescr(relation)
Definition: rel.h:532
ItemPointerData t_self
Definition: htup.h:65
Definition: nodes.h:129
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:232
void CommandCounterIncrement(void)
Definition: xact.c:1099

References AccessShareLock, Assert, BoolGetDatum(), CacheInvalidateRelcacheAll(), CatalogTupleUpdate(), CharGetDatum(), CommandCounterIncrement(), ereport, errcode(), errdetail(), errmsg(), ERROR, EventTriggerCollectSimpleCommand(), get_rel_name(), get_rel_relkind(), GetAllSchemaPublicationRelations(), GetPublicationRelations(), GetPubPartitionOptionRelations(), GETSTRUCT(), heap_attisnull(), heap_modify_tuple(), HeapTupleIsValid, InvalidatePublicationRels(), InvalidObjectAddress, InvokeObjectPostAlterHook, lfirst_oid, list_concat_unique_oid(), LockDatabaseObject(), NIL, ObjectAddressSet, ObjectIdGetDatum(), parse_publication_options(), PublicationActions::pubdelete, PublicationActions::pubinsert, PUBLICATION_PART_ALL, PUBLICATION_PART_ROOT, PublicationActions::pubtruncate, PublicationActions::pubupdate, RelationGetDescr, ReleaseSysCache(), relname, SearchSysCache2(), stmt, HeapTupleData::t_self, and values.

Referenced by AlterPublication().

◆ AlterPublicationOwner()

ObjectAddress AlterPublicationOwner ( const char *  name,
Oid  newOwnerId 
)

Definition at line 1993 of file publicationcmds.c.

1994{
1995 Oid subid;
1996 HeapTuple tup;
1997 Relation rel;
1998 ObjectAddress address;
1999 Form_pg_publication pubform;
2000
2001 rel = table_open(PublicationRelationId, RowExclusiveLock);
2002
2003 tup = SearchSysCacheCopy1(PUBLICATIONNAME, CStringGetDatum(name));
2004
2005 if (!HeapTupleIsValid(tup))
2006 ereport(ERROR,
2007 (errcode(ERRCODE_UNDEFINED_OBJECT),
2008 errmsg("publication \"%s\" does not exist", name)));
2009
2010 pubform = (Form_pg_publication) GETSTRUCT(tup);
2011 subid = pubform->oid;
2012
2013 AlterPublicationOwner_internal(rel, tup, newOwnerId);
2014
2015 ObjectAddressSet(address, PublicationRelationId, subid);
2016
2017 heap_freetuple(tup);
2018
2020
2021 return address;
2022}
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_internal()

static void AlterPublicationOwner_internal ( Relation  rel,
HeapTuple  tup,
Oid  newOwnerId 
)
static

Definition at line 1935 of file publicationcmds.c.

1936{
1938
1939 form = (Form_pg_publication) GETSTRUCT(tup);
1940
1941 if (form->pubowner == newOwnerId)
1942 return;
1943
1944 if (!superuser())
1945 {
1946 AclResult aclresult;
1947
1948 /* Must be owner */
1949 if (!object_ownercheck(PublicationRelationId, form->oid, GetUserId()))
1951 NameStr(form->pubname));
1952
1953 /* Must be able to become new owner */
1954 check_can_set_role(GetUserId(), newOwnerId);
1955
1956 /* New owner must have CREATE privilege on database */
1957 aclresult = object_aclcheck(DatabaseRelationId, MyDatabaseId, newOwnerId, ACL_CREATE);
1958 if (aclresult != ACLCHECK_OK)
1961
1962 if (form->puballtables && !superuser_arg(newOwnerId))
1963 ereport(ERROR,
1964 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1965 errmsg("permission denied to change owner of publication \"%s\"",
1966 NameStr(form->pubname)),
1967 errhint("The owner of a FOR ALL TABLES publication must be a superuser.")));
1968
1969 if (!superuser_arg(newOwnerId) && is_schema_publication(form->oid))
1970 ereport(ERROR,
1971 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1972 errmsg("permission denied to change owner of publication \"%s\"",
1973 NameStr(form->pubname)),
1974 errhint("The owner of a FOR TABLES IN SCHEMA publication must be a superuser.")));
1975 }
1976
1977 form->pubowner = newOwnerId;
1978 CatalogTupleUpdate(rel, &tup->t_self, tup);
1979
1980 /* Update owner dependency reference */
1981 changeDependencyOnOwner(PublicationRelationId,
1982 form->oid,
1983 newOwnerId);
1984
1985 InvokeObjectPostAlterHook(PublicationRelationId,
1986 form->oid, 0);
1987}
void check_can_set_role(Oid member, Oid role)
Definition: acl.c:5325
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:3804
#define NameStr(name)
Definition: c.h:703
char * get_database_name(Oid dbid)
Definition: dbcommands.c:3187
int errhint(const char *fmt,...)
Definition: elog.c:1317
Oid MyDatabaseId
Definition: globals.c:93
@ OBJECT_DATABASE
Definition: parsenodes.h:2321
#define ACL_CREATE
Definition: parsenodes.h:85
bool is_schema_publication(Oid pubid)
void changeDependencyOnOwner(Oid classId, Oid objectId, Oid newOwnerId)
Definition: pg_shdepend.c:316
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
bool superuser(void)
Definition: superuser.c:46

References ACL_CREATE, aclcheck_error(), ACLCHECK_NOT_OWNER, ACLCHECK_OK, CatalogTupleUpdate(), changeDependencyOnOwner(), check_can_set_role(), ereport, errcode(), errhint(), errmsg(), ERROR, get_database_name(), GETSTRUCT(), GetUserId(), InvokeObjectPostAlterHook, is_schema_publication(), MyDatabaseId, NameStr, object_aclcheck(), OBJECT_DATABASE, object_ownercheck(), OBJECT_PUBLICATION, superuser(), superuser_arg(), and HeapTupleData::t_self.

Referenced by AlterPublicationOwner(), and AlterPublicationOwner_oid().

◆ AlterPublicationOwner_oid()

void AlterPublicationOwner_oid ( Oid  subid,
Oid  newOwnerId 
)

Definition at line 2028 of file publicationcmds.c.

2029{
2030 HeapTuple tup;
2031 Relation rel;
2032
2033 rel = table_open(PublicationRelationId, RowExclusiveLock);
2034
2035 tup = SearchSysCacheCopy1(PUBLICATIONOID, ObjectIdGetDatum(subid));
2036
2037 if (!HeapTupleIsValid(tup))
2038 ereport(ERROR,
2039 (errcode(ERRCODE_UNDEFINED_OBJECT),
2040 errmsg("publication with OID %u does not exist", subid)));
2041
2042 AlterPublicationOwner_internal(rel, tup, newOwnerId);
2043
2044 heap_freetuple(tup);
2045
2047}

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

Referenced by shdepReassignOwned_Owner().

◆ AlterPublicationSchemas()

static void AlterPublicationSchemas ( AlterPublicationStmt stmt,
HeapTuple  tup,
List schemaidlist 
)
static

Definition at line 1296 of file publicationcmds.c.

1298{
1300
1301 /*
1302 * Nothing to do if no objects, except in SET: for that it is quite
1303 * possible that user has not specified any schemas in which case we need
1304 * to remove all the existing schemas.
1305 */
1306 if (!schemaidlist && stmt->action != AP_SetObjects)
1307 return;
1308
1309 /*
1310 * Schema lock is held until the publication is altered to prevent
1311 * concurrent schema deletion.
1312 */
1313 LockSchemaList(schemaidlist);
1314 if (stmt->action == AP_AddObjects)
1315 {
1316 ListCell *lc;
1317 List *reloids;
1318
1319 reloids = GetPublicationRelations(pubform->oid, PUBLICATION_PART_ROOT);
1320
1321 foreach(lc, reloids)
1322 {
1323 HeapTuple coltuple;
1324
1325 coltuple = SearchSysCache2(PUBLICATIONRELMAP,
1327 ObjectIdGetDatum(pubform->oid));
1328
1329 if (!HeapTupleIsValid(coltuple))
1330 continue;
1331
1332 /*
1333 * Disallow adding schema if column list is already part of the
1334 * publication. See CheckPubRelationColumnList.
1335 */
1336 if (!heap_attisnull(coltuple, Anum_pg_publication_rel_prattrs, NULL))
1337 ereport(ERROR,
1338 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1339 errmsg("cannot add schema to publication \"%s\"",
1340 stmt->pubname),
1341 errdetail("Schemas cannot be added if any tables that specify a column list are already part of the publication."));
1342
1343 ReleaseSysCache(coltuple);
1344 }
1345
1346 PublicationAddSchemas(pubform->oid, schemaidlist, false, stmt);
1347 }
1348 else if (stmt->action == AP_DropObjects)
1349 PublicationDropSchemas(pubform->oid, schemaidlist, false);
1350 else /* AP_SetObjects */
1351 {
1352 List *oldschemaids = GetPublicationSchemas(pubform->oid);
1353 List *delschemas = NIL;
1354
1355 /* Identify which schemas should be dropped */
1356 delschemas = list_difference_oid(oldschemaids, schemaidlist);
1357
1358 /*
1359 * Schema lock is held until the publication is altered to prevent
1360 * concurrent schema deletion.
1361 */
1362 LockSchemaList(delschemas);
1363
1364 /* And drop them */
1365 PublicationDropSchemas(pubform->oid, delschemas, true);
1366
1367 /*
1368 * Don't bother calculating the difference for adding, we'll catch and
1369 * skip existing ones when doing catalog update.
1370 */
1371 PublicationAddSchemas(pubform->oid, schemaidlist, true, stmt);
1372 }
1373}
List * list_difference_oid(const List *list1, const List *list2)
Definition: list.c:1313
@ AP_DropObjects
Definition: parsenodes.h:4247
@ AP_SetObjects
Definition: parsenodes.h:4248
@ AP_AddObjects
Definition: parsenodes.h:4246
List * GetPublicationSchemas(Oid pubid)
static void PublicationAddSchemas(Oid pubid, List *schemas, bool if_not_exists, AlterPublicationStmt *stmt)
static void PublicationDropSchemas(Oid pubid, List *schemas, bool missing_ok)
static void LockSchemaList(List *schemalist)

References AP_AddObjects, AP_DropObjects, AP_SetObjects, ereport, errcode(), errdetail(), errmsg(), ERROR, GetPublicationRelations(), GetPublicationSchemas(), GETSTRUCT(), heap_attisnull(), HeapTupleIsValid, lfirst_oid, list_difference_oid(), LockSchemaList(), NIL, ObjectIdGetDatum(), PUBLICATION_PART_ROOT, PublicationAddSchemas(), PublicationDropSchemas(), ReleaseSysCache(), SearchSysCache2(), and stmt.

Referenced by AlterPublication().

◆ AlterPublicationTables()

static void AlterPublicationTables ( AlterPublicationStmt stmt,
HeapTuple  tup,
List tables,
const char *  queryString,
bool  publish_schema 
)
static

Definition at line 1134 of file publicationcmds.c.

1137{
1138 List *rels = NIL;
1140 Oid pubid = pubform->oid;
1141
1142 /*
1143 * Nothing to do if no objects, except in SET: for that it is quite
1144 * possible that user has not specified any tables in which case we need
1145 * to remove all the existing tables.
1146 */
1147 if (!tables && stmt->action != AP_SetObjects)
1148 return;
1149
1150 rels = OpenTableList(tables);
1151
1152 if (stmt->action == AP_AddObjects)
1153 {
1154 TransformPubWhereClauses(rels, queryString, pubform->pubviaroot);
1155
1156 publish_schema |= is_schema_publication(pubid);
1157
1158 CheckPubRelationColumnList(stmt->pubname, rels, publish_schema,
1159 pubform->pubviaroot);
1160
1161 PublicationAddTables(pubid, rels, false, stmt);
1162 }
1163 else if (stmt->action == AP_DropObjects)
1164 PublicationDropTables(pubid, rels, false);
1165 else /* AP_SetObjects */
1166 {
1167 List *oldrelids = GetPublicationRelations(pubid,
1169 List *delrels = NIL;
1170 ListCell *oldlc;
1171
1172 TransformPubWhereClauses(rels, queryString, pubform->pubviaroot);
1173
1174 CheckPubRelationColumnList(stmt->pubname, rels, publish_schema,
1175 pubform->pubviaroot);
1176
1177 /*
1178 * To recreate the relation list for the publication, look for
1179 * existing relations that do not need to be dropped.
1180 */
1181 foreach(oldlc, oldrelids)
1182 {
1183 Oid oldrelid = lfirst_oid(oldlc);
1184 ListCell *newlc;
1185 PublicationRelInfo *oldrel;
1186 bool found = false;
1187 HeapTuple rftuple;
1188 Node *oldrelwhereclause = NULL;
1189 Bitmapset *oldcolumns = NULL;
1190
1191 /* look up the cache for the old relmap */
1192 rftuple = SearchSysCache2(PUBLICATIONRELMAP,
1193 ObjectIdGetDatum(oldrelid),
1194 ObjectIdGetDatum(pubid));
1195
1196 /*
1197 * See if the existing relation currently has a WHERE clause or a
1198 * column list. We need to compare those too.
1199 */
1200 if (HeapTupleIsValid(rftuple))
1201 {
1202 bool isnull = true;
1203 Datum whereClauseDatum;
1204 Datum columnListDatum;
1205
1206 /* Load the WHERE clause for this table. */
1207 whereClauseDatum = SysCacheGetAttr(PUBLICATIONRELMAP, rftuple,
1208 Anum_pg_publication_rel_prqual,
1209 &isnull);
1210 if (!isnull)
1211 oldrelwhereclause = stringToNode(TextDatumGetCString(whereClauseDatum));
1212
1213 /* Transform the int2vector column list to a bitmap. */
1214 columnListDatum = SysCacheGetAttr(PUBLICATIONRELMAP, rftuple,
1215 Anum_pg_publication_rel_prattrs,
1216 &isnull);
1217
1218 if (!isnull)
1219 oldcolumns = pub_collist_to_bitmapset(NULL, columnListDatum, NULL);
1220
1221 ReleaseSysCache(rftuple);
1222 }
1223
1224 foreach(newlc, rels)
1225 {
1226 PublicationRelInfo *newpubrel;
1227 Oid newrelid;
1228 Bitmapset *newcolumns = NULL;
1229
1230 newpubrel = (PublicationRelInfo *) lfirst(newlc);
1231 newrelid = RelationGetRelid(newpubrel->relation);
1232
1233 /*
1234 * Validate the column list. If the column list or WHERE
1235 * clause changes, then the validation done here will be
1236 * duplicated inside PublicationAddTables(). The validation
1237 * is cheap enough that that seems harmless.
1238 */
1239 newcolumns = pub_collist_validate(newpubrel->relation,
1240 newpubrel->columns);
1241
1242 /*
1243 * Check if any of the new set of relations matches with the
1244 * existing relations in the publication. Additionally, if the
1245 * relation has an associated WHERE clause, check the WHERE
1246 * expressions also match. Same for the column list. Drop the
1247 * rest.
1248 */
1249 if (newrelid == oldrelid)
1250 {
1251 if (equal(oldrelwhereclause, newpubrel->whereClause) &&
1252 bms_equal(oldcolumns, newcolumns))
1253 {
1254 found = true;
1255 break;
1256 }
1257 }
1258 }
1259
1260 /*
1261 * Add the non-matched relations to a list so that they can be
1262 * dropped.
1263 */
1264 if (!found)
1265 {
1266 oldrel = palloc(sizeof(PublicationRelInfo));
1267 oldrel->whereClause = NULL;
1268 oldrel->columns = NIL;
1269 oldrel->relation = table_open(oldrelid,
1271 delrels = lappend(delrels, oldrel);
1272 }
1273 }
1274
1275 /* And drop them. */
1276 PublicationDropTables(pubid, delrels, true);
1277
1278 /*
1279 * Don't bother calculating the difference for adding, we'll catch and
1280 * skip existing ones when doing catalog update.
1281 */
1282 PublicationAddTables(pubid, rels, true, stmt);
1283
1284 CloseTableList(delrels);
1285 }
1286
1287 CloseTableList(rels);
1288}
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:142
#define TextDatumGetCString(d)
Definition: builtins.h:98
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:223
List * lappend(List *list, void *datum)
Definition: list.c:339
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
void * palloc(Size size)
Definition: mcxt.c:1317
#define lfirst(lc)
Definition: pg_list.h:172
Bitmapset * pub_collist_validate(Relation targetrel, List *columns)
Bitmapset * pub_collist_to_bitmapset(Bitmapset *columns, Datum pubcols, MemoryContext mcxt)
static void PublicationAddTables(Oid pubid, List *rels, bool if_not_exists, AlterPublicationStmt *stmt)
static void CloseTableList(List *rels)
static void PublicationDropTables(Oid pubid, List *rels, bool missing_ok)
static void TransformPubWhereClauses(List *tables, const char *queryString, bool pubviaroot)
static List * OpenTableList(List *tables)
static void CheckPubRelationColumnList(char *pubname, List *tables, bool publish_schema, bool pubviaroot)
void * stringToNode(const char *str)
Definition: read.c:90
#define RelationGetRelid(relation)
Definition: rel.h:506
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:600

References AP_AddObjects, AP_DropObjects, AP_SetObjects, bms_equal(), CheckPubRelationColumnList(), CloseTableList(), PublicationRelInfo::columns, equal(), GetPublicationRelations(), GETSTRUCT(), HeapTupleIsValid, is_schema_publication(), lappend(), lfirst, lfirst_oid, NIL, ObjectIdGetDatum(), OpenTableList(), palloc(), pub_collist_to_bitmapset(), pub_collist_validate(), PUBLICATION_PART_ROOT, PublicationAddTables(), PublicationDropTables(), PublicationRelInfo::relation, RelationGetRelid, ReleaseSysCache(), SearchSysCache2(), ShareUpdateExclusiveLock, stmt, stringToNode(), SysCacheGetAttr(), table_open(), TextDatumGetCString, TransformPubWhereClauses(), and PublicationRelInfo::whereClause.

Referenced by AlterPublication().

◆ check_simple_rowfilter_expr()

static bool check_simple_rowfilter_expr ( Node node,
ParseState pstate 
)
static

Definition at line 629 of file publicationcmds.c.

630{
631 return check_simple_rowfilter_expr_walker(node, pstate);
632}
static bool check_simple_rowfilter_expr_walker(Node *node, ParseState *pstate)

References check_simple_rowfilter_expr_walker().

Referenced by TransformPubWhereClauses().

◆ check_simple_rowfilter_expr_walker()

static bool check_simple_rowfilter_expr_walker ( Node node,
ParseState pstate 
)
static

Definition at line 522 of file publicationcmds.c.

523{
524 char *errdetail_msg = NULL;
525
526 if (node == NULL)
527 return false;
528
529 switch (nodeTag(node))
530 {
531 case T_Var:
532 /* System columns are not allowed. */
533 if (((Var *) node)->varattno < InvalidAttrNumber)
534 errdetail_msg = _("System columns are not allowed.");
535 break;
536 case T_OpExpr:
537 case T_DistinctExpr:
538 case T_NullIfExpr:
539 /* OK, except user-defined operators are not allowed. */
540 if (((OpExpr *) node)->opno >= FirstNormalObjectId)
541 errdetail_msg = _("User-defined operators are not allowed.");
542 break;
543 case T_ScalarArrayOpExpr:
544 /* OK, except user-defined operators are not allowed. */
545 if (((ScalarArrayOpExpr *) node)->opno >= FirstNormalObjectId)
546 errdetail_msg = _("User-defined operators are not allowed.");
547
548 /*
549 * We don't need to check the hashfuncid and negfuncid of
550 * ScalarArrayOpExpr as those functions are only built for a
551 * subquery.
552 */
553 break;
554 case T_RowCompareExpr:
555 {
556 ListCell *opid;
557
558 /* OK, except user-defined operators are not allowed. */
559 foreach(opid, ((RowCompareExpr *) node)->opnos)
560 {
561 if (lfirst_oid(opid) >= FirstNormalObjectId)
562 {
563 errdetail_msg = _("User-defined operators are not allowed.");
564 break;
565 }
566 }
567 }
568 break;
569 case T_Const:
570 case T_FuncExpr:
571 case T_BoolExpr:
572 case T_RelabelType:
573 case T_CollateExpr:
574 case T_CaseExpr:
575 case T_CaseTestExpr:
576 case T_ArrayExpr:
577 case T_RowExpr:
578 case T_CoalesceExpr:
579 case T_MinMaxExpr:
580 case T_XmlExpr:
581 case T_NullTest:
582 case T_BooleanTest:
583 case T_List:
584 /* OK, supported */
585 break;
586 default:
587 errdetail_msg = _("Only columns, constants, built-in operators, built-in data types, built-in collations, and immutable built-in functions are allowed.");
588 break;
589 }
590
591 /*
592 * For all the supported nodes, if we haven't already found a problem,
593 * check the types, functions, and collations used in it. We check List
594 * by walking through each element.
595 */
596 if (!errdetail_msg && !IsA(node, List))
597 {
598 if (exprType(node) >= FirstNormalObjectId)
599 errdetail_msg = _("User-defined types are not allowed.");
601 pstate))
602 errdetail_msg = _("User-defined or built-in mutable functions are not allowed.");
603 else if (exprCollation(node) >= FirstNormalObjectId ||
605 errdetail_msg = _("User-defined collations are not allowed.");
606 }
607
608 /*
609 * If we found a problem in this node, throw error now. Otherwise keep
610 * going.
611 */
612 if (errdetail_msg)
614 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
615 errmsg("invalid publication WHERE expression"),
616 errdetail_internal("%s", errdetail_msg),
617 parser_errposition(pstate, exprLocation(node))));
618
620 pstate);
621}
#define InvalidAttrNumber
Definition: attnum.h:23
int errdetail_internal(const char *fmt,...)
Definition: elog.c:1230
#define _(x)
Definition: elog.c:90
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
Oid exprInputCollation(const Node *expr)
Definition: nodeFuncs.c:1076
bool check_functions_in_node(Node *node, check_function_callback checker, void *context)
Definition: nodeFuncs.c:1910
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:821
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1388
#define expression_tree_walker(n, w, c)
Definition: nodeFuncs.h:153
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
#define nodeTag(nodeptr)
Definition: nodes.h:133
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:106
static bool contain_mutable_or_user_functions_checker(Oid func_id, void *context)
Definition: primnodes.h:262
#define FirstNormalObjectId
Definition: transam.h:197

References _, check_functions_in_node(), check_simple_rowfilter_expr_walker(), contain_mutable_or_user_functions_checker(), ereport, errcode(), errdetail_internal(), errmsg(), ERROR, exprCollation(), expression_tree_walker, exprInputCollation(), exprLocation(), exprType(), FirstNormalObjectId, InvalidAttrNumber, IsA, lfirst_oid, nodeTag, and parser_errposition().

Referenced by check_simple_rowfilter_expr(), and check_simple_rowfilter_expr_walker().

◆ CheckAlterPublication()

static void CheckAlterPublication ( AlterPublicationStmt stmt,
HeapTuple  tup,
List tables,
List schemaidlist 
)
static

Definition at line 1380 of file publicationcmds.c.

1382{
1384
1385 if ((stmt->action == AP_AddObjects || stmt->action == AP_SetObjects) &&
1386 schemaidlist && !superuser())
1387 ereport(ERROR,
1388 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1389 errmsg("must be superuser to add or set schemas")));
1390
1391 /*
1392 * Check that user is allowed to manipulate the publication tables in
1393 * schema
1394 */
1395 if (schemaidlist && pubform->puballtables)
1396 ereport(ERROR,
1397 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1398 errmsg("publication \"%s\" is defined as FOR ALL TABLES",
1399 NameStr(pubform->pubname)),
1400 errdetail("Schemas cannot be added to or dropped from FOR ALL TABLES publications.")));
1401
1402 /* Check that user is allowed to manipulate the publication tables. */
1403 if (tables && pubform->puballtables)
1404 ereport(ERROR,
1405 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1406 errmsg("publication \"%s\" is defined as FOR ALL TABLES",
1407 NameStr(pubform->pubname)),
1408 errdetail("Tables cannot be added to or dropped from FOR ALL TABLES publications.")));
1409}

References AP_AddObjects, AP_SetObjects, ereport, errcode(), errdetail(), errmsg(), ERROR, GETSTRUCT(), NameStr, stmt, and superuser().

Referenced by AlterPublication().

◆ CheckPubRelationColumnList()

static void CheckPubRelationColumnList ( char *  pubname,
List tables,
bool  publish_schema,
bool  pubviaroot 
)
static

Definition at line 716 of file publicationcmds.c.

718{
719 ListCell *lc;
720
721 foreach(lc, tables)
722 {
724
725 if (pri->columns == NIL)
726 continue;
727
728 /*
729 * Disallow specifying column list if any schema is in the
730 * publication.
731 *
732 * XXX We could instead just forbid the case when the publication
733 * tries to publish the table with a column list and a schema for that
734 * table. However, if we do that then we need a restriction during
735 * ALTER TABLE ... SET SCHEMA to prevent such a case which doesn't
736 * seem to be a good idea.
737 */
738 if (publish_schema)
740 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
741 errmsg("cannot use column list for relation \"%s.%s\" in publication \"%s\"",
743 RelationGetRelationName(pri->relation), pubname),
744 errdetail("Column lists cannot be specified in publications containing FOR TABLES IN SCHEMA elements."));
745
746 /*
747 * If the publication doesn't publish changes via the root partitioned
748 * table, the partition's column list will be used. So disallow using
749 * a column list on the partitioned table in this case.
750 */
751 if (!pubviaroot &&
752 pri->relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
754 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
755 errmsg("cannot use column list for relation \"%s.%s\" in publication \"%s\"",
757 RelationGetRelationName(pri->relation), pubname),
758 errdetail("Column lists cannot be specified for partitioned tables when %s is false.",
759 "publish_via_partition_root")));
760 }
761}
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3393
#define RelationGetRelationName(relation)
Definition: rel.h:540
#define RelationGetNamespace(relation)
Definition: rel.h:547
Form_pg_class rd_rel
Definition: rel.h:111

References PublicationRelInfo::columns, ereport, errcode(), errdetail(), errmsg(), ERROR, get_namespace_name(), lfirst, NIL, RelationData::rd_rel, PublicationRelInfo::relation, RelationGetNamespace, and RelationGetRelationName.

Referenced by AlterPublicationTables(), and CreatePublication().

◆ CloseTableList()

static void CloseTableList ( List rels)
static

Definition at line 1746 of file publicationcmds.c.

1747{
1748 ListCell *lc;
1749
1750 foreach(lc, rels)
1751 {
1752 PublicationRelInfo *pub_rel;
1753
1754 pub_rel = (PublicationRelInfo *) lfirst(lc);
1755 table_close(pub_rel->relation, NoLock);
1756 }
1757
1758 list_free_deep(rels);
1759}
void list_free_deep(List *list)
Definition: list.c:1560
#define NoLock
Definition: lockdefs.h:34

References lfirst, list_free_deep(), NoLock, PublicationRelInfo::relation, and table_close().

Referenced by AlterPublicationTables(), and CreatePublication().

◆ contain_invalid_rfcolumn_walker()

static bool contain_invalid_rfcolumn_walker ( Node node,
rf_context context 
)
static

Definition at line 231 of file publicationcmds.c.

232{
233 if (node == NULL)
234 return false;
235
236 if (IsA(node, Var))
237 {
238 Var *var = (Var *) node;
240
241 /*
242 * If pubviaroot is true, we are validating the row filter of the
243 * parent table, but the bitmap contains the replica identity
244 * information of the child table. So, get the column number of the
245 * child table as parent and child column order could be different.
246 */
247 if (context->pubviaroot)
248 {
249 char *colname = get_attname(context->parentid, attnum, false);
250
251 attnum = get_attnum(context->relid, colname);
252 }
253
255 context->bms_replident))
256 return true;
257 }
258
260 context);
261}
int16 AttrNumber
Definition: attnum.h:21
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
AttrNumber get_attnum(Oid relid, const char *attname)
Definition: lsyscache.c:859
char * get_attname(Oid relid, AttrNumber attnum, bool missing_ok)
Definition: lsyscache.c:828
int16 attnum
Definition: pg_attribute.h:74
static bool contain_invalid_rfcolumn_walker(Node *node, rf_context *context)
AttrNumber varattno
Definition: primnodes.h:274
Bitmapset * bms_replident
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27

References attnum, bms_is_member(), rf_context::bms_replident, contain_invalid_rfcolumn_walker(), expression_tree_walker, FirstLowInvalidHeapAttributeNumber, get_attname(), get_attnum(), IsA, rf_context::parentid, rf_context::pubviaroot, rf_context::relid, and Var::varattno.

Referenced by contain_invalid_rfcolumn_walker(), and pub_rf_contains_invalid_column().

◆ contain_mutable_or_user_functions_checker()

static bool contain_mutable_or_user_functions_checker ( Oid  func_id,
void *  context 
)
static

Definition at line 477 of file publicationcmds.c.

478{
479 return (func_volatile(func_id) != PROVOLATILE_IMMUTABLE ||
480 func_id >= FirstNormalObjectId);
481}
char func_volatile(Oid funcid)
Definition: lsyscache.c:1807

References FirstNormalObjectId, and func_volatile().

Referenced by check_simple_rowfilter_expr_walker().

◆ CreatePublication()

ObjectAddress CreatePublication ( ParseState pstate,
CreatePublicationStmt stmt 
)

Definition at line 767 of file publicationcmds.c.

768{
769 Relation rel;
770 ObjectAddress myself;
771 Oid puboid;
772 bool nulls[Natts_pg_publication];
773 Datum values[Natts_pg_publication];
774 HeapTuple tup;
775 bool publish_given;
776 PublicationActions pubactions;
777 bool publish_via_partition_root_given;
778 bool publish_via_partition_root;
779 bool publish_generated_columns_given;
780 char publish_generated_columns;
781 AclResult aclresult;
782 List *relations = NIL;
783 List *schemaidlist = NIL;
784
785 /* must have CREATE privilege on database */
786 aclresult = object_aclcheck(DatabaseRelationId, MyDatabaseId, GetUserId(), ACL_CREATE);
787 if (aclresult != ACLCHECK_OK)
790
791 /* FOR ALL TABLES requires superuser */
792 if (stmt->for_all_tables && !superuser())
794 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
795 errmsg("must be superuser to create FOR ALL TABLES publication")));
796
797 rel = table_open(PublicationRelationId, RowExclusiveLock);
798
799 /* Check if name is used */
800 puboid = GetSysCacheOid1(PUBLICATIONNAME, Anum_pg_publication_oid,
801 CStringGetDatum(stmt->pubname));
802 if (OidIsValid(puboid))
805 errmsg("publication \"%s\" already exists",
806 stmt->pubname)));
807
808 /* Form a tuple. */
809 memset(values, 0, sizeof(values));
810 memset(nulls, false, sizeof(nulls));
811
812 values[Anum_pg_publication_pubname - 1] =
814 values[Anum_pg_publication_pubowner - 1] = ObjectIdGetDatum(GetUserId());
815
817 stmt->options,
818 &publish_given, &pubactions,
819 &publish_via_partition_root_given,
820 &publish_via_partition_root,
821 &publish_generated_columns_given,
822 &publish_generated_columns);
823
824 puboid = GetNewOidWithIndex(rel, PublicationObjectIndexId,
825 Anum_pg_publication_oid);
826 values[Anum_pg_publication_oid - 1] = ObjectIdGetDatum(puboid);
827 values[Anum_pg_publication_puballtables - 1] =
828 BoolGetDatum(stmt->for_all_tables);
829 values[Anum_pg_publication_pubinsert - 1] =
830 BoolGetDatum(pubactions.pubinsert);
831 values[Anum_pg_publication_pubupdate - 1] =
832 BoolGetDatum(pubactions.pubupdate);
833 values[Anum_pg_publication_pubdelete - 1] =
834 BoolGetDatum(pubactions.pubdelete);
835 values[Anum_pg_publication_pubtruncate - 1] =
836 BoolGetDatum(pubactions.pubtruncate);
837 values[Anum_pg_publication_pubviaroot - 1] =
838 BoolGetDatum(publish_via_partition_root);
839 values[Anum_pg_publication_pubgencols - 1] =
840 CharGetDatum(publish_generated_columns);
841
842 tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
843
844 /* Insert tuple into catalog. */
845 CatalogTupleInsert(rel, tup);
846 heap_freetuple(tup);
847
848 recordDependencyOnOwner(PublicationRelationId, puboid, GetUserId());
849
850 ObjectAddressSet(myself, PublicationRelationId, puboid);
851
852 /* Make the changes visible. */
854
855 /* Associate objects with the publication. */
856 if (stmt->for_all_tables)
857 {
858 /* Invalidate relcache so that publication info is rebuilt. */
860 }
861 else
862 {
863 ObjectsInPublicationToOids(stmt->pubobjects, pstate, &relations,
864 &schemaidlist);
865
866 /* FOR TABLES IN SCHEMA requires superuser */
867 if (schemaidlist != NIL && !superuser())
869 errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
870 errmsg("must be superuser to create FOR TABLES IN SCHEMA publication"));
871
872 if (relations != NIL)
873 {
874 List *rels;
875
876 rels = OpenTableList(relations);
878 publish_via_partition_root);
879
880 CheckPubRelationColumnList(stmt->pubname, rels,
881 schemaidlist != NIL,
882 publish_via_partition_root);
883
884 PublicationAddTables(puboid, rels, true, NULL);
885 CloseTableList(rels);
886 }
887
888 if (schemaidlist != NIL)
889 {
890 /*
891 * Schema lock is held until the publication is created to prevent
892 * concurrent schema deletion.
893 */
894 LockSchemaList(schemaidlist);
895 PublicationAddSchemas(puboid, schemaidlist, true, NULL);
896 }
897 }
898
900
901 InvokeObjectPostCreateHook(PublicationRelationId, puboid, 0);
902
905 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
906 errmsg("\"wal_level\" is insufficient to publish logical changes"),
907 errhint("Set \"wal_level\" to \"logical\" before creating subscriptions.")));
908
909 return myself;
910}
#define OidIsValid(objectId)
Definition: c.h:732
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:419
#define WARNING
Definition: elog.h:36
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:641
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
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:173
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:168
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:30
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition: syscache.h:109
int wal_level
Definition: xlog.c:131
@ WAL_LEVEL_LOGICAL
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, 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_LOGICAL, and WARNING.

Referenced by ProcessUtilitySlow().

◆ defGetGeneratedColsOption()

static char defGetGeneratedColsOption ( DefElem def)
static

Definition at line 2054 of file publicationcmds.c.

2055{
2056 char *sval;
2057
2058 /*
2059 * If no parameter value given, assume "stored" is meant.
2060 */
2061 if (!def->arg)
2062 return PUBLISH_GENCOLS_STORED;
2063
2064 sval = defGetString(def);
2065
2066 if (pg_strcasecmp(sval, "none") == 0)
2067 return PUBLISH_GENCOLS_NONE;
2068 if (pg_strcasecmp(sval, "stored") == 0)
2069 return PUBLISH_GENCOLS_STORED;
2070
2071 ereport(ERROR,
2072 errcode(ERRCODE_SYNTAX_ERROR),
2073 errmsg("%s requires a \"none\" or \"stored\" value",
2074 def->defname));
2075
2076 return PUBLISH_GENCOLS_NONE; /* keep compiler quiet */
2077}
char * defGetString(DefElem *def)
Definition: define.c:35
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
char * defname
Definition: parsenodes.h:826
Node * arg
Definition: parsenodes.h:827

References DefElem::arg, defGetString(), DefElem::defname, ereport, errcode(), errmsg(), ERROR, and pg_strcasecmp().

Referenced by parse_publication_options().

◆ InvalidatePublicationRels()

void InvalidatePublicationRels ( List relids)

Definition at line 1113 of file publicationcmds.c.

1114{
1115 /*
1116 * We don't want to send too many individual messages, at some point it's
1117 * cheaper to just reset whole relcache.
1118 */
1120 {
1121 ListCell *lc;
1122
1123 foreach(lc, relids)
1125 }
1126 else
1128}
void CacheInvalidateRelcacheByRelid(Oid relid)
Definition: inval.c:1612
static int list_length(const List *l)
Definition: pg_list.h:152
#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().

◆ LockSchemaList()

static void LockSchemaList ( List schemalist)
static

Definition at line 1766 of file publicationcmds.c.

1767{
1768 ListCell *lc;
1769
1770 foreach(lc, schemalist)
1771 {
1772 Oid schemaid = lfirst_oid(lc);
1773
1774 /* Allow query cancel in case this takes a long time */
1776 LockDatabaseObject(NamespaceRelationId, schemaid, 0, AccessShareLock);
1777
1778 /*
1779 * It is possible that by the time we acquire the lock on schema,
1780 * concurrent DDL has removed it. We can test this by checking the
1781 * existence of schema.
1782 */
1783 if (!SearchSysCacheExists1(NAMESPACEOID, ObjectIdGetDatum(schemaid)))
1784 ereport(ERROR,
1785 errcode(ERRCODE_UNDEFINED_SCHEMA),
1786 errmsg("schema with OID %u does not exist", schemaid));
1787 }
1788}
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:100

References AccessShareLock, CHECK_FOR_INTERRUPTS, ereport, errcode(), errmsg(), ERROR, lfirst_oid, LockDatabaseObject(), ObjectIdGetDatum(), and SearchSysCacheExists1.

Referenced by AlterPublicationSchemas(), and CreatePublication().

◆ ObjectsInPublicationToOids()

static void ObjectsInPublicationToOids ( List pubobjspec_list,
ParseState pstate,
List **  rels,
List **  schemas 
)
static

Definition at line 178 of file publicationcmds.c.

180{
181 ListCell *cell;
182 PublicationObjSpec *pubobj;
183
184 if (!pubobjspec_list)
185 return;
186
187 foreach(cell, pubobjspec_list)
188 {
189 Oid schemaid;
190 List *search_path;
191
192 pubobj = (PublicationObjSpec *) lfirst(cell);
193
194 switch (pubobj->pubobjtype)
195 {
197 *rels = lappend(*rels, pubobj->pubtable);
198 break;
200 schemaid = get_namespace_oid(pubobj->name, false);
201
202 /* Filter out duplicates if user specifies "sch1, sch1" */
203 *schemas = list_append_unique_oid(*schemas, schemaid);
204 break;
206 search_path = fetch_search_path(false);
207 if (search_path == NIL) /* nothing valid in search_path? */
209 errcode(ERRCODE_UNDEFINED_SCHEMA),
210 errmsg("no schema has been selected for CURRENT_SCHEMA"));
211
212 schemaid = linitial_oid(search_path);
213 list_free(search_path);
214
215 /* Filter out duplicates if user specifies "sch1, sch1" */
216 *schemas = list_append_unique_oid(*schemas, schemaid);
217 break;
218 default:
219 /* shouldn't happen */
220 elog(ERROR, "invalid publication object type %d", pubobj->pubobjtype);
221 break;
222 }
223 }
224}
#define elog(elevel,...)
Definition: elog.h:225
List * list_append_unique_oid(List *list, Oid datum)
Definition: list.c:1380
void list_free(List *list)
Definition: list.c:1546
List * fetch_search_path(bool includeImplicit)
Definition: namespace.c:4819
Oid get_namespace_oid(const char *nspname, bool missing_ok)
Definition: namespace.c:3535
@ PUBLICATIONOBJ_TABLES_IN_CUR_SCHEMA
Definition: parsenodes.h:4221
@ PUBLICATIONOBJ_TABLES_IN_SCHEMA
Definition: parsenodes.h:4220
@ PUBLICATIONOBJ_TABLE
Definition: parsenodes.h:4219
#define linitial_oid(l)
Definition: pg_list.h:180
PublicationObjSpecType pubobjtype
Definition: parsenodes.h:4229
PublicationTable * pubtable
Definition: parsenodes.h:4231

References elog, ereport, errcode(), errmsg(), ERROR, fetch_search_path(), get_namespace_oid(), lappend(), lfirst, linitial_oid, list_append_unique_oid(), list_free(), PublicationObjSpec::name, NIL, PUBLICATIONOBJ_TABLE, PUBLICATIONOBJ_TABLES_IN_CUR_SCHEMA, PUBLICATIONOBJ_TABLES_IN_SCHEMA, PublicationObjSpec::pubobjtype, and PublicationObjSpec::pubtable.

Referenced by AlterPublication(), and CreatePublication().

◆ OpenTableList()

static List * OpenTableList ( List tables)
static

Definition at line 1596 of file publicationcmds.c.

1597{
1598 List *relids = NIL;
1599 List *rels = NIL;
1600 ListCell *lc;
1601 List *relids_with_rf = NIL;
1602 List *relids_with_collist = NIL;
1603
1604 /*
1605 * Open, share-lock, and check all the explicitly-specified relations
1606 */
1607 foreach(lc, tables)
1608 {
1610 bool recurse = t->relation->inh;
1611 Relation rel;
1612 Oid myrelid;
1613 PublicationRelInfo *pub_rel;
1614
1615 /* Allow query cancel in case this takes a long time */
1617
1619 myrelid = RelationGetRelid(rel);
1620
1621 /*
1622 * Filter out duplicates if user specifies "foo, foo".
1623 *
1624 * Note that this algorithm is known to not be very efficient (O(N^2))
1625 * but given that it only works on list of tables given to us by user
1626 * it's deemed acceptable.
1627 */
1628 if (list_member_oid(relids, myrelid))
1629 {
1630 /* Disallow duplicate tables if there are any with row filters. */
1631 if (t->whereClause || list_member_oid(relids_with_rf, myrelid))
1632 ereport(ERROR,
1634 errmsg("conflicting or redundant WHERE clauses for table \"%s\"",
1636
1637 /* Disallow duplicate tables if there are any with column lists. */
1638 if (t->columns || list_member_oid(relids_with_collist, myrelid))
1639 ereport(ERROR,
1641 errmsg("conflicting or redundant column lists for table \"%s\"",
1643
1645 continue;
1646 }
1647
1648 pub_rel = palloc(sizeof(PublicationRelInfo));
1649 pub_rel->relation = rel;
1650 pub_rel->whereClause = t->whereClause;
1651 pub_rel->columns = t->columns;
1652 rels = lappend(rels, pub_rel);
1653 relids = lappend_oid(relids, myrelid);
1654
1655 if (t->whereClause)
1656 relids_with_rf = lappend_oid(relids_with_rf, myrelid);
1657
1658 if (t->columns)
1659 relids_with_collist = lappend_oid(relids_with_collist, myrelid);
1660
1661 /*
1662 * Add children of this rel, if requested, so that they too are added
1663 * to the publication. A partitioned table can't have any inheritance
1664 * children other than its partitions, which need not be explicitly
1665 * added to the publication.
1666 */
1667 if (recurse && rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
1668 {
1669 List *children;
1670 ListCell *child;
1671
1673 NULL);
1674
1675 foreach(child, children)
1676 {
1677 Oid childrelid = lfirst_oid(child);
1678
1679 /* Allow query cancel in case this takes a long time */
1681
1682 /*
1683 * Skip duplicates if user specified both parent and child
1684 * tables.
1685 */
1686 if (list_member_oid(relids, childrelid))
1687 {
1688 /*
1689 * We don't allow to specify row filter for both parent
1690 * and child table at the same time as it is not very
1691 * clear which one should be given preference.
1692 */
1693 if (childrelid != myrelid &&
1694 (t->whereClause || list_member_oid(relids_with_rf, childrelid)))
1695 ereport(ERROR,
1697 errmsg("conflicting or redundant WHERE clauses for table \"%s\"",
1699
1700 /*
1701 * We don't allow to specify column list for both parent
1702 * and child table at the same time as it is not very
1703 * clear which one should be given preference.
1704 */
1705 if (childrelid != myrelid &&
1706 (t->columns || list_member_oid(relids_with_collist, childrelid)))
1707 ereport(ERROR,
1709 errmsg("conflicting or redundant column lists for table \"%s\"",
1711
1712 continue;
1713 }
1714
1715 /* find_all_inheritors already got lock */
1716 rel = table_open(childrelid, NoLock);
1717 pub_rel = palloc(sizeof(PublicationRelInfo));
1718 pub_rel->relation = rel;
1719 /* child inherits WHERE clause from parent */
1720 pub_rel->whereClause = t->whereClause;
1721
1722 /* child inherits column list from parent */
1723 pub_rel->columns = t->columns;
1724 rels = lappend(rels, pub_rel);
1725 relids = lappend_oid(relids, childrelid);
1726
1727 if (t->whereClause)
1728 relids_with_rf = lappend_oid(relids_with_rf, childrelid);
1729
1730 if (t->columns)
1731 relids_with_collist = lappend_oid(relids_with_collist, childrelid);
1732 }
1733 }
1734 }
1735
1736 list_free(relids);
1737 list_free(relids_with_rf);
1738
1739 return rels;
1740}
List * lappend_oid(List *list, Oid datum)
Definition: list.c:375
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:722
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
Definition: pg_inherits.c:255
#define lfirst_node(type, lc)
Definition: pg_list.h:176
RangeVar * relation
Definition: parsenodes.h:4209
bool inh
Definition: primnodes.h:86
Relation table_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: table.c:83

References CHECK_FOR_INTERRUPTS, PublicationRelInfo::columns, PublicationTable::columns, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, find_all_inheritors(), RangeVar::inh, lappend(), lappend_oid(), lfirst_node, lfirst_oid, list_free(), list_member_oid(), NIL, NoLock, palloc(), RelationData::rd_rel, PublicationRelInfo::relation, PublicationTable::relation, RelationGetRelationName, RelationGetRelid, ShareUpdateExclusiveLock, table_close(), table_open(), table_openrv(), PublicationRelInfo::whereClause, and PublicationTable::whereClause.

Referenced by AlterPublicationTables(), and CreatePublication().

◆ parse_publication_options()

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

Definition at line 77 of file publicationcmds.c.

85{
86 ListCell *lc;
87
88 *publish_given = false;
89 *publish_via_partition_root_given = false;
90 *publish_generated_columns_given = false;
91
92 /* defaults */
93 pubactions->pubinsert = true;
94 pubactions->pubupdate = true;
95 pubactions->pubdelete = true;
96 pubactions->pubtruncate = true;
97 *publish_via_partition_root = false;
98 *publish_generated_columns = PUBLISH_GENCOLS_NONE;
99
100 /* Parse options */
101 foreach(lc, options)
102 {
103 DefElem *defel = (DefElem *) lfirst(lc);
104
105 if (strcmp(defel->defname, "publish") == 0)
106 {
107 char *publish;
108 List *publish_list;
109 ListCell *lc2;
110
111 if (*publish_given)
112 errorConflictingDefElem(defel, pstate);
113
114 /*
115 * If publish option was given only the explicitly listed actions
116 * should be published.
117 */
118 pubactions->pubinsert = false;
119 pubactions->pubupdate = false;
120 pubactions->pubdelete = false;
121 pubactions->pubtruncate = false;
122
123 *publish_given = true;
124 publish = defGetString(defel);
125
126 if (!SplitIdentifierString(publish, ',', &publish_list))
128 (errcode(ERRCODE_SYNTAX_ERROR),
129 errmsg("invalid list syntax in parameter \"%s\"",
130 "publish")));
131
132 /* Process the option list. */
133 foreach(lc2, publish_list)
134 {
135 char *publish_opt = (char *) lfirst(lc2);
136
137 if (strcmp(publish_opt, "insert") == 0)
138 pubactions->pubinsert = true;
139 else if (strcmp(publish_opt, "update") == 0)
140 pubactions->pubupdate = true;
141 else if (strcmp(publish_opt, "delete") == 0)
142 pubactions->pubdelete = true;
143 else if (strcmp(publish_opt, "truncate") == 0)
144 pubactions->pubtruncate = true;
145 else
147 (errcode(ERRCODE_SYNTAX_ERROR),
148 errmsg("unrecognized value for publication option \"%s\": \"%s\"",
149 "publish", publish_opt)));
150 }
151 }
152 else if (strcmp(defel->defname, "publish_via_partition_root") == 0)
153 {
154 if (*publish_via_partition_root_given)
155 errorConflictingDefElem(defel, pstate);
156 *publish_via_partition_root_given = true;
157 *publish_via_partition_root = defGetBoolean(defel);
158 }
159 else if (strcmp(defel->defname, "publish_generated_columns") == 0)
160 {
161 if (*publish_generated_columns_given)
162 errorConflictingDefElem(defel, pstate);
163 *publish_generated_columns_given = true;
164 *publish_generated_columns = defGetGeneratedColsOption(defel);
165 }
166 else
168 (errcode(ERRCODE_SYNTAX_ERROR),
169 errmsg("unrecognized publication parameter: \"%s\"", defel->defname)));
170 }
171}
bool defGetBoolean(DefElem *def)
Definition: define.c:94
void errorConflictingDefElem(DefElem *defel, ParseState *pstate)
Definition: define.c:371
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:76
static char defGetGeneratedColsOption(DefElem *def)
bool SplitIdentifierString(char *rawstring, char separator, List **namelist)
Definition: varlena.c:3432

References defGetBoolean(), defGetGeneratedColsOption(), defGetString(), DefElem::defname, ereport, errcode(), errmsg(), ERROR, errorConflictingDefElem(), if(), lfirst, PublicationActions::pubdelete, PublicationActions::pubinsert, PublicationActions::pubtruncate, PublicationActions::pubupdate, and SplitIdentifierString().

Referenced by AlterPublicationOptions(), and CreatePublication().

◆ 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 356 of file publicationcmds.c.

360{
361 Oid relid = RelationGetRelid(relation);
362 Oid publish_as_relid = RelationGetRelid(relation);
363 Bitmapset *idattrs;
364 Bitmapset *columns = NULL;
365 TupleDesc desc = RelationGetDescr(relation);
366 Publication *pub;
367 int x;
368
369 *invalid_column_list = false;
370 *invalid_gen_col = false;
371
372 /*
373 * For a partition, if pubviaroot is true, find the topmost ancestor that
374 * is published via this publication as we need to use its column list for
375 * the changes.
376 *
377 * Note that even though the column list used is for an ancestor, the
378 * REPLICA IDENTITY used will be for the actual child table.
379 */
380 if (pubviaroot && relation->rd_rel->relispartition)
381 {
382 publish_as_relid = GetTopMostAncestorInPublication(pubid, ancestors, NULL);
383
384 if (!OidIsValid(publish_as_relid))
385 publish_as_relid = relid;
386 }
387
388 /* Fetch the column list */
389 pub = GetPublication(pubid);
390 check_and_fetch_column_list(pub, publish_as_relid, NULL, &columns);
391
392 if (relation->rd_rel->relreplident == REPLICA_IDENTITY_FULL)
393 {
394 /* With REPLICA IDENTITY FULL, no column list is allowed. */
395 *invalid_column_list = (columns != NULL);
396
397 /*
398 * As we don't allow a column list with REPLICA IDENTITY FULL, the
399 * publish_generated_columns option must be set to stored if the table
400 * has any stored generated columns.
401 */
402 if (pubgencols_type != PUBLISH_GENCOLS_STORED &&
403 relation->rd_att->constr &&
405 *invalid_gen_col = true;
406
407 if (*invalid_gen_col && *invalid_column_list)
408 return true;
409 }
410
411 /* Remember columns that are part of the REPLICA IDENTITY */
412 idattrs = RelationGetIndexAttrBitmap(relation,
414
415 /*
416 * Attnums in the bitmap returned by RelationGetIndexAttrBitmap are offset
417 * (to handle system columns the usual way), while column list does not
418 * use offset, so we can't do bms_is_subset(). Instead, we have to loop
419 * over the idattrs and check all of them are in the list.
420 */
421 x = -1;
422 while ((x = bms_next_member(idattrs, x)) >= 0)
423 {
425 Form_pg_attribute att = TupleDescAttr(desc, attnum - 1);
426
427 if (columns == NULL)
428 {
429 /*
430 * The publish_generated_columns option must be set to stored if
431 * the REPLICA IDENTITY contains any stored generated column.
432 */
433 if (pubgencols_type != PUBLISH_GENCOLS_STORED && att->attgenerated)
434 {
435 *invalid_gen_col = true;
436 break;
437 }
438
439 /* Skip validating the column list since it is not defined */
440 continue;
441 }
442
443 /*
444 * If pubviaroot is true, we are validating the column list of the
445 * parent table, but the bitmap contains the replica identity
446 * information of the child table. The parent/child attnums may not
447 * match, so translate them to the parent - get the attname from the
448 * child, and look it up in the parent.
449 */
450 if (pubviaroot)
451 {
452 /* attribute name in the child table */
453 char *colname = get_attname(relid, attnum, false);
454
455 /*
456 * Determine the attnum for the attribute name in parent (we are
457 * using the column list defined on the parent).
458 */
459 attnum = get_attnum(publish_as_relid, colname);
460 }
461
462 /* replica identity column, not covered by the column list */
463 *invalid_column_list |= !bms_is_member(attnum, columns);
464
465 if (*invalid_column_list && *invalid_gen_col)
466 break;
467 }
468
469 bms_free(columns);
470 bms_free(idattrs);
471
472 return *invalid_column_list || *invalid_gen_col;
473}
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306
void bms_free(Bitmapset *a)
Definition: bitmapset.c:239
int x
Definition: isn.c:70
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
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)
Bitmapset * RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind)
Definition: relcache.c:5223
@ INDEX_ATTR_BITMAP_IDENTITY_KEY
Definition: relcache.h:63
TupleDesc rd_att
Definition: rel.h:112
bool has_generated_stored
Definition: tupdesc.h:46
TupleConstr * constr
Definition: tupdesc.h:134
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:153

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, 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 270 of file publicationcmds.c.

272{
273 HeapTuple rftuple;
274 Oid relid = RelationGetRelid(relation);
275 Oid publish_as_relid = RelationGetRelid(relation);
276 bool result = false;
277 Datum rfdatum;
278 bool rfisnull;
279
280 /*
281 * FULL means all columns are in the REPLICA IDENTITY, so all columns are
282 * allowed in the row filter and we can skip the validation.
283 */
284 if (relation->rd_rel->relreplident == REPLICA_IDENTITY_FULL)
285 return false;
286
287 /*
288 * For a partition, if pubviaroot is true, find the topmost ancestor that
289 * is published via this publication as we need to use its row filter
290 * expression to filter the partition's changes.
291 *
292 * Note that even though the row filter used is for an ancestor, the
293 * REPLICA IDENTITY used will be for the actual child table.
294 */
295 if (pubviaroot && relation->rd_rel->relispartition)
296 {
297 publish_as_relid
298 = GetTopMostAncestorInPublication(pubid, ancestors, NULL);
299
300 if (!OidIsValid(publish_as_relid))
301 publish_as_relid = relid;
302 }
303
304 rftuple = SearchSysCache2(PUBLICATIONRELMAP,
305 ObjectIdGetDatum(publish_as_relid),
306 ObjectIdGetDatum(pubid));
307
308 if (!HeapTupleIsValid(rftuple))
309 return false;
310
311 rfdatum = SysCacheGetAttr(PUBLICATIONRELMAP, rftuple,
312 Anum_pg_publication_rel_prqual,
313 &rfisnull);
314
315 if (!rfisnull)
316 {
317 rf_context context = {0};
318 Node *rfnode;
319 Bitmapset *bms = NULL;
320
321 context.pubviaroot = pubviaroot;
322 context.parentid = publish_as_relid;
323 context.relid = relid;
324
325 /* Remember columns that are part of the REPLICA IDENTITY */
326 bms = RelationGetIndexAttrBitmap(relation,
328
329 context.bms_replident = bms;
330 rfnode = stringToNode(TextDatumGetCString(rfdatum));
331 result = contain_invalid_rfcolumn_walker(rfnode, &context);
332 }
333
334 ReleaseSysCache(rftuple);
335
336 return result;
337}

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().

◆ PublicationAddSchemas()

static void PublicationAddSchemas ( Oid  pubid,
List schemas,
bool  if_not_exists,
AlterPublicationStmt stmt 
)
static

Definition at line 1873 of file publicationcmds.c.

1875{
1876 ListCell *lc;
1877
1878 Assert(!stmt || !stmt->for_all_tables);
1879
1880 foreach(lc, schemas)
1881 {
1882 Oid schemaid = lfirst_oid(lc);
1883 ObjectAddress obj;
1884
1885 obj = publication_add_schema(pubid, schemaid, if_not_exists);
1886 if (stmt)
1887 {
1889 (Node *) stmt);
1890
1891 InvokeObjectPostCreateHook(PublicationNamespaceRelationId,
1892 obj.objectId, 0);
1893 }
1894 }
1895}
ObjectAddress publication_add_schema(Oid pubid, Oid schemaid, bool if_not_exists)

References Assert, EventTriggerCollectSimpleCommand(), InvalidObjectAddress, InvokeObjectPostCreateHook, lfirst_oid, ObjectAddress::objectId, publication_add_schema(), and stmt.

Referenced by AlterPublicationSchemas(), and CreatePublication().

◆ PublicationAddTables()

static void PublicationAddTables ( Oid  pubid,
List rels,
bool  if_not_exists,
AlterPublicationStmt stmt 
)
static

Definition at line 1794 of file publicationcmds.c.

1796{
1797 ListCell *lc;
1798
1799 Assert(!stmt || !stmt->for_all_tables);
1800
1801 foreach(lc, rels)
1802 {
1803 PublicationRelInfo *pub_rel = (PublicationRelInfo *) lfirst(lc);
1804 Relation rel = pub_rel->relation;
1805 ObjectAddress obj;
1806
1807 /* Must be owner of the table or superuser. */
1808 if (!object_ownercheck(RelationRelationId, RelationGetRelid(rel), GetUserId()))
1811
1812 obj = publication_add_relation(pubid, pub_rel, if_not_exists);
1813 if (stmt)
1814 {
1816 (Node *) stmt);
1817
1818 InvokeObjectPostCreateHook(PublicationRelRelationId,
1819 obj.objectId, 0);
1820 }
1821 }
1822}
ObjectType get_relkind_objtype(char relkind)
ObjectAddress publication_add_relation(Oid pubid, PublicationRelInfo *pri, bool if_not_exists)

References aclcheck_error(), ACLCHECK_NOT_OWNER, Assert, EventTriggerCollectSimpleCommand(), get_relkind_objtype(), GetUserId(), InvalidObjectAddress, InvokeObjectPostCreateHook, lfirst, object_ownercheck(), ObjectAddress::objectId, publication_add_relation(), RelationData::rd_rel, PublicationRelInfo::relation, RelationGetRelationName, RelationGetRelid, and stmt.

Referenced by AlterPublicationTables(), and CreatePublication().

◆ PublicationDropSchemas()

static void PublicationDropSchemas ( Oid  pubid,
List schemas,
bool  missing_ok 
)
static

Definition at line 1901 of file publicationcmds.c.

1902{
1903 ObjectAddress obj;
1904 ListCell *lc;
1905 Oid psid;
1906
1907 foreach(lc, schemas)
1908 {
1909 Oid schemaid = lfirst_oid(lc);
1910
1911 psid = GetSysCacheOid2(PUBLICATIONNAMESPACEMAP,
1912 Anum_pg_publication_namespace_oid,
1913 ObjectIdGetDatum(schemaid),
1914 ObjectIdGetDatum(pubid));
1915 if (!OidIsValid(psid))
1916 {
1917 if (missing_ok)
1918 continue;
1919
1920 ereport(ERROR,
1921 (errcode(ERRCODE_UNDEFINED_OBJECT),
1922 errmsg("tables from schema \"%s\" are not part of the publication",
1923 get_namespace_name(schemaid))));
1924 }
1925
1926 ObjectAddressSet(obj, PublicationNamespaceRelationId, psid);
1927 performDeletion(&obj, DROP_CASCADE, 0);
1928 }
1929}
void performDeletion(const ObjectAddress *object, DropBehavior behavior, int flags)
Definition: dependency.c:273
@ DROP_CASCADE
Definition: parsenodes.h:2386
#define GetSysCacheOid2(cacheId, oidcol, key1, key2)
Definition: syscache.h:111

References DROP_CASCADE, ereport, errcode(), errmsg(), ERROR, get_namespace_name(), GetSysCacheOid2, lfirst_oid, ObjectAddressSet, ObjectIdGetDatum(), OidIsValid, and performDeletion().

Referenced by AlterPublicationSchemas().

◆ PublicationDropTables()

static void PublicationDropTables ( Oid  pubid,
List rels,
bool  missing_ok 
)
static

Definition at line 1828 of file publicationcmds.c.

1829{
1830 ObjectAddress obj;
1831 ListCell *lc;
1832 Oid prid;
1833
1834 foreach(lc, rels)
1835 {
1837 Relation rel = pubrel->relation;
1838 Oid relid = RelationGetRelid(rel);
1839
1840 if (pubrel->columns)
1841 ereport(ERROR,
1842 errcode(ERRCODE_SYNTAX_ERROR),
1843 errmsg("column list must not be specified in ALTER PUBLICATION ... DROP"));
1844
1845 prid = GetSysCacheOid2(PUBLICATIONRELMAP, Anum_pg_publication_rel_oid,
1846 ObjectIdGetDatum(relid),
1847 ObjectIdGetDatum(pubid));
1848 if (!OidIsValid(prid))
1849 {
1850 if (missing_ok)
1851 continue;
1852
1853 ereport(ERROR,
1854 (errcode(ERRCODE_UNDEFINED_OBJECT),
1855 errmsg("relation \"%s\" is not part of the publication",
1857 }
1858
1859 if (pubrel->whereClause)
1860 ereport(ERROR,
1861 (errcode(ERRCODE_SYNTAX_ERROR),
1862 errmsg("cannot use a WHERE clause when removing a table from a publication")));
1863
1864 ObjectAddressSet(obj, PublicationRelRelationId, prid);
1865 performDeletion(&obj, DROP_CASCADE, 0);
1866 }
1867}

References PublicationRelInfo::columns, DROP_CASCADE, ereport, errcode(), errmsg(), ERROR, GetSysCacheOid2, lfirst, ObjectAddressSet, ObjectIdGetDatum(), OidIsValid, performDeletion(), PublicationRelInfo::relation, RelationGetRelationName, RelationGetRelid, and PublicationRelInfo::whereClause.

Referenced by AlterPublicationTables().

◆ RemovePublicationById()

void RemovePublicationById ( Oid  pubid)

Definition at line 1529 of file publicationcmds.c.

1530{
1531 Relation rel;
1532 HeapTuple tup;
1533 Form_pg_publication pubform;
1534
1535 rel = table_open(PublicationRelationId, RowExclusiveLock);
1536
1537 tup = SearchSysCache1(PUBLICATIONOID, ObjectIdGetDatum(pubid));
1538 if (!HeapTupleIsValid(tup))
1539 elog(ERROR, "cache lookup failed for publication %u", pubid);
1540
1541 pubform = (Form_pg_publication) GETSTRUCT(tup);
1542
1543 /* Invalidate relcache so that publication info is rebuilt. */
1544 if (pubform->puballtables)
1546
1547 CatalogTupleDelete(rel, &tup->t_self);
1548
1549 ReleaseSysCache(tup);
1550
1552}
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:365
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221

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 1488 of file publicationcmds.c.

1489{
1490 Relation rel;
1491 HeapTuple tup;
1493 List *relids = NIL;
1494
1495 rel = table_open(PublicationRelRelationId, RowExclusiveLock);
1496
1497 tup = SearchSysCache1(PUBLICATIONREL, ObjectIdGetDatum(proid));
1498
1499 if (!HeapTupleIsValid(tup))
1500 elog(ERROR, "cache lookup failed for publication table %u",
1501 proid);
1502
1503 pubrel = (Form_pg_publication_rel) GETSTRUCT(tup);
1504
1505 /*
1506 * Invalidate relcache so that publication info is rebuilt.
1507 *
1508 * For the partitioned tables, we must invalidate all partitions contained
1509 * in the respective partition hierarchies, not just the one explicitly
1510 * mentioned in the publication. This is required because we implicitly
1511 * publish the child tables when the parent table is published.
1512 */
1514 pubrel->prrelid);
1515
1517
1518 CatalogTupleDelete(rel, &tup->t_self);
1519
1520 ReleaseSysCache(tup);
1521
1523}
FormData_pg_publication_rel * Form_pg_publication_rel

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 1558 of file publicationcmds.c.

1559{
1560 Relation rel;
1561 HeapTuple tup;
1562 List *schemaRels = NIL;
1564
1565 rel = table_open(PublicationNamespaceRelationId, RowExclusiveLock);
1566
1567 tup = SearchSysCache1(PUBLICATIONNAMESPACE, ObjectIdGetDatum(psoid));
1568
1569 if (!HeapTupleIsValid(tup))
1570 elog(ERROR, "cache lookup failed for publication schema %u", psoid);
1571
1573
1574 /*
1575 * Invalidate relcache so that publication info is rebuilt. See
1576 * RemovePublicationRelById for why we need to consider all the
1577 * partitions.
1578 */
1579 schemaRels = GetSchemaPublicationRelations(pubsch->pnnspid,
1581 InvalidatePublicationRels(schemaRels);
1582
1583 CatalogTupleDelete(rel, &tup->t_self);
1584
1585 ReleaseSysCache(tup);
1586
1588}
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().

◆ TransformPubWhereClauses()

static void TransformPubWhereClauses ( List tables,
const char *  queryString,
bool  pubviaroot 
)
static

Definition at line 644 of file publicationcmds.c.

646{
647 ListCell *lc;
648
649 foreach(lc, tables)
650 {
651 ParseNamespaceItem *nsitem;
652 Node *whereclause = NULL;
653 ParseState *pstate;
655
656 if (pri->whereClause == NULL)
657 continue;
658
659 /*
660 * If the publication doesn't publish changes via the root partitioned
661 * table, the partition's row filter will be used. So disallow using
662 * WHERE clause on partitioned table in this case.
663 */
664 if (!pubviaroot &&
665 pri->relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
667 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
668 errmsg("cannot use publication WHERE clause for relation \"%s\"",
670 errdetail("WHERE clause cannot be used for a partitioned table when %s is false.",
671 "publish_via_partition_root")));
672
673 /*
674 * A fresh pstate is required so that we only have "this" table in its
675 * rangetable
676 */
677 pstate = make_parsestate(NULL);
678 pstate->p_sourcetext = queryString;
679 nsitem = addRangeTableEntryForRelation(pstate, pri->relation,
680 AccessShareLock, NULL,
681 false, false);
682 addNSItemToQuery(pstate, nsitem, false, true, true);
683
684 whereclause = transformWhereClause(pstate,
687 "PUBLICATION WHERE");
688
689 /* Fix up collation information */
690 assign_expr_collations(pstate, whereclause);
691
692 /*
693 * We allow only simple expressions in row filters. See
694 * check_simple_rowfilter_expr_walker.
695 */
696 check_simple_rowfilter_expr(whereclause, pstate);
697
698 free_parsestate(pstate);
699
700 pri->whereClause = whereclause;
701 }
702}
#define copyObject(obj)
Definition: nodes.h:224
Node * transformWhereClause(ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName)
void assign_expr_collations(ParseState *pstate, Node *expr)
void free_parsestate(ParseState *pstate)
Definition: parse_node.c:72
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:39
@ EXPR_KIND_WHERE
Definition: parse_node.h:46
ParseNamespaceItem * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, int lockmode, Alias *alias, bool inh, bool inFromCl)
void addNSItemToQuery(ParseState *pstate, ParseNamespaceItem *nsitem, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
static bool check_simple_rowfilter_expr(Node *node, ParseState *pstate)

References AccessShareLock, addNSItemToQuery(), addRangeTableEntryForRelation(), assign_expr_collations(), check_simple_rowfilter_expr(), copyObject, ereport, errcode(), errdetail(), errmsg(), ERROR, EXPR_KIND_WHERE, free_parsestate(), lfirst, make_parsestate(), ParseState::p_sourcetext, RelationData::rd_rel, PublicationRelInfo::relation, RelationGetRelationName, transformWhereClause(), and PublicationRelInfo::whereClause.

Referenced by AlterPublicationTables(), and CreatePublication().