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)
 
ObjectAddress AlterPublicationOwner (const char *name, Oid newOwnerId)
 
void AlterPublicationOwner_oid (Oid pubid, Oid newOwnerId)
 
void InvalidatePublicationRels (List *relids)
 

Macro Definition Documentation

◆ MAX_RELCACHE_INVAL_MSGS

#define MAX_RELCACHE_INVAL_MSGS   4096

Definition at line 23 of file publicationcmds.h.

Referenced by InvalidatePublicationRels().

Function Documentation

◆ AlterPublication()

void AlterPublication ( ParseState pstate,
AlterPublicationStmt stmt 
)

Definition at line 450 of file publicationcmds.c.

References aclcheck_error(), ACLCHECK_NOT_OWNER, AlterPublicationOptions(), AlterPublicationTables(), CStringGetDatum, ereport, errcode(), errmsg(), ERROR, GETSTRUCT, GetUserId(), heap_freetuple(), HeapTupleIsValid, OBJECT_PUBLICATION, AlterPublicationStmt::options, pg_publication_ownercheck(), PUBLICATIONNAME, AlterPublicationStmt::pubname, RowExclusiveLock, SearchSysCacheCopy1, table_close(), and table_open().

Referenced by ProcessUtilitySlow().

451 {
452  Relation rel;
453  HeapTuple tup;
454  Form_pg_publication pubform;
455 
456  rel = table_open(PublicationRelationId, RowExclusiveLock);
457 
459  CStringGetDatum(stmt->pubname));
460 
461  if (!HeapTupleIsValid(tup))
462  ereport(ERROR,
463  (errcode(ERRCODE_UNDEFINED_OBJECT),
464  errmsg("publication \"%s\" does not exist",
465  stmt->pubname)));
466 
467  pubform = (Form_pg_publication) GETSTRUCT(tup);
468 
469  /* must be owner */
470  if (!pg_publication_ownercheck(pubform->oid, GetUserId()))
472  stmt->pubname);
473 
474  if (stmt->options)
475  AlterPublicationOptions(pstate, stmt, rel, tup);
476  else
477  AlterPublicationTables(stmt, rel, tup);
478 
479  /* Cleanup. */
480  heap_freetuple(tup);
482 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
Oid GetUserId(void)
Definition: miscinit.c:495
static void AlterPublicationOptions(ParseState *pstate, AlterPublicationStmt *stmt, Relation rel, HeapTuple tup)
bool pg_publication_ownercheck(Oid pub_oid, Oid roleid)
Definition: aclchk.c:5356
int errcode(int sqlerrcode)
Definition: elog.c:698
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3308
#define ERROR
Definition: elog.h:46
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CStringGetDatum(X)
Definition: postgres.h:622
static void AlterPublicationTables(AlterPublicationStmt *stmt, Relation rel, HeapTuple tup)
#define ereport(elevel,...)
Definition: elog.h:157
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:175
int errmsg(const char *fmt,...)
Definition: elog.c:909
FormData_pg_publication * Form_pg_publication
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ AlterPublicationOwner()

ObjectAddress AlterPublicationOwner ( const char *  name,
Oid  newOwnerId 
)

Definition at line 785 of file publicationcmds.c.

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

Referenced by ExecAlterOwnerStmt().

786 {
787  Oid subid;
788  HeapTuple tup;
789  Relation rel;
790  ObjectAddress address;
791  Form_pg_publication pubform;
792 
793  rel = table_open(PublicationRelationId, RowExclusiveLock);
794 
796 
797  if (!HeapTupleIsValid(tup))
798  ereport(ERROR,
799  (errcode(ERRCODE_UNDEFINED_OBJECT),
800  errmsg("publication \"%s\" does not exist", name)));
801 
802  pubform = (Form_pg_publication) GETSTRUCT(tup);
803  subid = pubform->oid;
804 
805  AlterPublicationOwner_internal(rel, tup, newOwnerId);
806 
807  ObjectAddressSet(address, PublicationRelationId, subid);
808 
809  heap_freetuple(tup);
810 
812 
813  return address;
814 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
int errcode(int sqlerrcode)
Definition: elog.c:698
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:46
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CStringGetDatum(X)
Definition: postgres.h:622
static void AlterPublicationOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
#define ereport(elevel,...)
Definition: elog.h:157
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
const char * name
Definition: encode.c:561
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:175
int errmsg(const char *fmt,...)
Definition: elog.c:909
FormData_pg_publication * Form_pg_publication
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ AlterPublicationOwner_oid()

void AlterPublicationOwner_oid ( Oid  pubid,
Oid  newOwnerId 
)

Definition at line 820 of file publicationcmds.c.

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

Referenced by shdepReassignOwned().

821 {
822  HeapTuple tup;
823  Relation rel;
824 
825  rel = table_open(PublicationRelationId, RowExclusiveLock);
826 
828 
829  if (!HeapTupleIsValid(tup))
830  ereport(ERROR,
831  (errcode(ERRCODE_UNDEFINED_OBJECT),
832  errmsg("publication with OID %u does not exist", subid)));
833 
834  AlterPublicationOwner_internal(rel, tup, newOwnerId);
835 
836  heap_freetuple(tup);
837 
839 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
int errcode(int sqlerrcode)
Definition: elog.c:698
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define RowExclusiveLock
Definition: lockdefs.h:38
static void AlterPublicationOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
#define ereport(elevel,...)
Definition: elog.h:157
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:175
int errmsg(const char *fmt,...)
Definition: elog.c:909
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ CreatePublication()

ObjectAddress CreatePublication ( ParseState pstate,
CreatePublicationStmt stmt 
)

Definition at line 142 of file publicationcmds.c.

References ACL_CREATE, aclcheck_error(), ACLCHECK_OK, Assert, BoolGetDatum, CacheInvalidateRelcacheAll(), CatalogTupleInsert(), CloseTableList(), CommandCounterIncrement(), CStringGetDatum, DirectFunctionCall1, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errhint(), errmsg(), ERROR, CreatePublicationStmt::for_all_tables, get_database_name(), GetNewOidWithIndex(), GetSysCacheOid1, GetUserId(), heap_form_tuple(), heap_freetuple(), InvokeObjectPostCreateHook, list_length(), MyDatabaseId, namein(), OBJECT_DATABASE, ObjectAddressSet, ObjectIdGetDatum, OidIsValid, OpenTableList(), CreatePublicationStmt::options, parse_publication_options(), pg_database_aclcheck(), PublicationActions::pubdelete, PublicationActions::pubinsert, PublicationAddTables(), PUBLICATIONNAME, CreatePublicationStmt::pubname, PublicationActions::pubtruncate, PublicationActions::pubupdate, recordDependencyOnOwner(), RelationGetDescr, RowExclusiveLock, superuser(), table_close(), table_open(), CreatePublicationStmt::tables, values, wal_level, WAL_LEVEL_LOGICAL, and WARNING.

Referenced by ProcessUtilitySlow().

143 {
144  Relation rel;
145  ObjectAddress myself;
146  Oid puboid;
147  bool nulls[Natts_pg_publication];
148  Datum values[Natts_pg_publication];
149  HeapTuple tup;
150  bool publish_given;
151  PublicationActions pubactions;
152  bool publish_via_partition_root_given;
153  bool publish_via_partition_root;
154  AclResult aclresult;
155 
156  /* must have CREATE privilege on database */
158  if (aclresult != ACLCHECK_OK)
159  aclcheck_error(aclresult, OBJECT_DATABASE,
161 
162  /* FOR ALL TABLES requires superuser */
163  if (stmt->for_all_tables && !superuser())
164  ereport(ERROR,
165  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
166  errmsg("must be superuser to create FOR ALL TABLES publication")));
167 
168  rel = table_open(PublicationRelationId, RowExclusiveLock);
169 
170  /* Check if name is used */
171  puboid = GetSysCacheOid1(PUBLICATIONNAME, Anum_pg_publication_oid,
172  CStringGetDatum(stmt->pubname));
173  if (OidIsValid(puboid))
174  {
175  ereport(ERROR,
177  errmsg("publication \"%s\" already exists",
178  stmt->pubname)));
179  }
180 
181  /* Form a tuple. */
182  memset(values, 0, sizeof(values));
183  memset(nulls, false, sizeof(nulls));
184 
185  values[Anum_pg_publication_pubname - 1] =
187  values[Anum_pg_publication_pubowner - 1] = ObjectIdGetDatum(GetUserId());
188 
190  stmt->options,
191  &publish_given, &pubactions,
192  &publish_via_partition_root_given,
193  &publish_via_partition_root);
194 
195  puboid = GetNewOidWithIndex(rel, PublicationObjectIndexId,
196  Anum_pg_publication_oid);
197  values[Anum_pg_publication_oid - 1] = ObjectIdGetDatum(puboid);
198  values[Anum_pg_publication_puballtables - 1] =
200  values[Anum_pg_publication_pubinsert - 1] =
201  BoolGetDatum(pubactions.pubinsert);
202  values[Anum_pg_publication_pubupdate - 1] =
203  BoolGetDatum(pubactions.pubupdate);
204  values[Anum_pg_publication_pubdelete - 1] =
205  BoolGetDatum(pubactions.pubdelete);
206  values[Anum_pg_publication_pubtruncate - 1] =
207  BoolGetDatum(pubactions.pubtruncate);
208  values[Anum_pg_publication_pubviaroot - 1] =
209  BoolGetDatum(publish_via_partition_root);
210 
211  tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
212 
213  /* Insert tuple into catalog. */
214  CatalogTupleInsert(rel, tup);
215  heap_freetuple(tup);
216 
217  recordDependencyOnOwner(PublicationRelationId, puboid, GetUserId());
218 
219  ObjectAddressSet(myself, PublicationRelationId, puboid);
220 
221  /* Make the changes visible. */
223 
224  if (stmt->tables)
225  {
226  List *rels;
227 
228  Assert(list_length(stmt->tables) > 0);
229 
230  rels = OpenTableList(stmt->tables);
231  PublicationAddTables(puboid, rels, true, NULL);
232  CloseTableList(rels);
233  }
234  else if (stmt->for_all_tables)
235  {
236  /* Invalidate relcache so that publication info is rebuilt. */
238  }
239 
241 
242  InvokeObjectPostCreateHook(PublicationRelationId, puboid, 0);
243 
245  {
247  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
248  errmsg("wal_level is insufficient to publish logical changes"),
249  errhint("Set wal_level to logical before creating subscriptions.")));
250  }
251 
252  return myself;
253 }
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:381
static void PublicationAddTables(Oid pubid, List *rels, bool if_not_exists, AlterPublicationStmt *stmt)
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
int errhint(const char *fmt,...)
Definition: elog.c:1156
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:151
#define RelationGetDescr(relation)
Definition: rel.h:503
Oid GetUserId(void)
Definition: miscinit.c:495
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition: syscache.h:193
int wal_level
Definition: xlog.c:108
int errcode(int sqlerrcode)
Definition: elog.c:698
bool superuser(void)
Definition: superuser.c:46
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:626
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:163
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
static void CloseTableList(List *rels)
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3308
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define ACL_CREATE
Definition: parsenodes.h:92
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2113
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CStringGetDatum(X)
Definition: postgres.h:622
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)
#define WARNING
Definition: elog.h:40
void CacheInvalidateRelcacheAll(void)
Definition: inval.c:1366
AclResult
Definition: acl.h:177
uintptr_t Datum
Definition: postgres.h:411
void CommandCounterIncrement(void)
Definition: xact.c:1021
Oid MyDatabaseId
Definition: globals.c:88
#define BoolGetDatum(X)
Definition: postgres.h:446
#define ereport(elevel,...)
Definition: elog.h:157
AclResult pg_database_aclcheck(Oid db_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4706
#define Assert(condition)
Definition: c.h:804
static int list_length(const List *l)
Definition: pg_list.h:149
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
static Datum values[MAXATTR]
Definition: bootstrap.c:156
int errmsg(const char *fmt,...)
Definition: elog.c:909
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
Definition: pg_list.h:50
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:221
static List * OpenTableList(List *tables)

◆ InvalidatePublicationRels()

void InvalidatePublicationRels ( List relids)

Definition at line 343 of file publicationcmds.c.

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

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

344 {
345  /*
346  * We don't want to send too many individual messages, at some point it's
347  * cheaper to just reset whole relcache.
348  */
349  if (list_length(relids) < MAX_RELCACHE_INVAL_MSGS)
350  {
351  ListCell *lc;
352 
353  foreach(lc, relids)
355  }
356  else
358 }
void CacheInvalidateRelcacheByRelid(Oid relid)
Definition: inval.c:1401
void CacheInvalidateRelcacheAll(void)
Definition: inval.c:1366
static int list_length(const List *l)
Definition: pg_list.h:149
#define MAX_RELCACHE_INVAL_MSGS
#define lfirst_oid(lc)
Definition: pg_list.h:171

◆ RemovePublicationById()

void RemovePublicationById ( Oid  pubid)

Definition at line 529 of file publicationcmds.c.

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

Referenced by doDeletion().

530 {
531  Relation rel;
532  HeapTuple tup;
533  Form_pg_publication pubform;
534 
535  rel = table_open(PublicationRelationId, RowExclusiveLock);
536 
538  if (!HeapTupleIsValid(tup))
539  elog(ERROR, "cache lookup failed for publication %u", pubid);
540 
541  pubform = (Form_pg_publication)GETSTRUCT(tup);
542 
543  /* Invalidate relcache so that publication info is rebuilt. */
544  if (pubform->puballtables)
546 
547  CatalogTupleDelete(rel, &tup->t_self);
548 
549  ReleaseSysCache(tup);
550 
552 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:350
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void CacheInvalidateRelcacheAll(void)
Definition: inval.c:1366
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define elog(elevel,...)
Definition: elog.h:232
FormData_pg_publication * Form_pg_publication
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ RemovePublicationRelById()

void RemovePublicationRelById ( Oid  proid)

Definition at line 488 of file publicationcmds.c.

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

Referenced by doDeletion().

489 {
490  Relation rel;
491  HeapTuple tup;
493  List *relids = NIL;
494 
495  rel = table_open(PublicationRelRelationId, RowExclusiveLock);
496 
498 
499  if (!HeapTupleIsValid(tup))
500  elog(ERROR, "cache lookup failed for publication table %u",
501  proid);
502 
503  pubrel = (Form_pg_publication_rel) GETSTRUCT(tup);
504 
505  /*
506  * Invalidate relcache so that publication info is rebuilt.
507  *
508  * For the partitioned tables, we must invalidate all partitions contained
509  * in the respective partition hierarchies, not just the one explicitly
510  * mentioned in the publication. This is required because we implicitly
511  * publish the child tables when the parent table is published.
512  */
514  pubrel->prrelid);
515 
517 
518  CatalogTupleDelete(rel, &tup->t_self);
519 
520  ReleaseSysCache(tup);
521 
523 }
#define NIL
Definition: pg_list.h:65
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:350
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
List * GetPubPartitionOptionRelations(List *result, PublicationPartOpt pub_partopt, Oid relid)
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define elog(elevel,...)
Definition: elog.h:232
FormData_pg_publication_rel * Form_pg_publication_rel
void InvalidatePublicationRels(List *relids)
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50