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.

Functions

ObjectAddress CreatePublication (CreatePublicationStmt *stmt)
 
void AlterPublication (AlterPublicationStmt *stmt)
 
void RemovePublicationRelById (Oid proid)
 
ObjectAddress AlterPublicationOwner (const char *name, Oid newOwnerId)
 
void AlterPublicationOwner_oid (Oid pubid, Oid newOwnerId)
 

Function Documentation

◆ AlterPublication()

void AlterPublication ( AlterPublicationStmt stmt)

Definition at line 437 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().

438 {
439  Relation rel;
440  HeapTuple tup;
441  Form_pg_publication pubform;
442 
443  rel = table_open(PublicationRelationId, RowExclusiveLock);
444 
446  CStringGetDatum(stmt->pubname));
447 
448  if (!HeapTupleIsValid(tup))
449  ereport(ERROR,
450  (errcode(ERRCODE_UNDEFINED_OBJECT),
451  errmsg("publication \"%s\" does not exist",
452  stmt->pubname)));
453 
454  pubform = (Form_pg_publication) GETSTRUCT(tup);
455 
456  /* must be owner */
457  if (!pg_publication_ownercheck(pubform->oid, GetUserId()))
459  stmt->pubname);
460 
461  if (stmt->options)
462  AlterPublicationOptions(stmt, rel, tup);
463  else
464  AlterPublicationTables(stmt, rel, tup);
465 
466  /* Cleanup. */
467  heap_freetuple(tup);
469 }
static void AlterPublicationOptions(AlterPublicationStmt *stmt, Relation rel, HeapTuple tup)
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
Oid GetUserId(void)
Definition: miscinit.c:476
bool pg_publication_ownercheck(Oid pub_oid, Oid roleid)
Definition: aclchk.c:5225
int errcode(int sqlerrcode)
Definition: elog.c:610
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3294
#define ERROR
Definition: elog.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CStringGetDatum(X)
Definition: postgres.h:578
static void AlterPublicationTables(AlterPublicationStmt *stmt, Relation rel, HeapTuple tup)
#define ereport(elevel,...)
Definition: elog.h:144
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:174
int errmsg(const char *fmt,...)
Definition: elog.c:824
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 724 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().

725 {
726  Oid subid;
727  HeapTuple tup;
728  Relation rel;
729  ObjectAddress address;
730  Form_pg_publication pubform;
731 
732  rel = table_open(PublicationRelationId, RowExclusiveLock);
733 
735 
736  if (!HeapTupleIsValid(tup))
737  ereport(ERROR,
738  (errcode(ERRCODE_UNDEFINED_OBJECT),
739  errmsg("publication \"%s\" does not exist", name)));
740 
741  pubform = (Form_pg_publication) GETSTRUCT(tup);
742  subid = pubform->oid;
743 
744  AlterPublicationOwner_internal(rel, tup, newOwnerId);
745 
746  ObjectAddressSet(address, PublicationRelationId, subid);
747 
748  heap_freetuple(tup);
749 
751 
752  return address;
753 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
int errcode(int sqlerrcode)
Definition: elog.c:610
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CStringGetDatum(X)
Definition: postgres.h:578
static void AlterPublicationOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
#define ereport(elevel,...)
Definition: elog.h:144
#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:174
int errmsg(const char *fmt,...)
Definition: elog.c:824
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 759 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().

760 {
761  HeapTuple tup;
762  Relation rel;
763 
764  rel = table_open(PublicationRelationId, RowExclusiveLock);
765 
767 
768  if (!HeapTupleIsValid(tup))
769  ereport(ERROR,
770  (errcode(ERRCODE_UNDEFINED_OBJECT),
771  errmsg("publication with OID %u does not exist", subid)));
772 
773  AlterPublicationOwner_internal(rel, tup, newOwnerId);
774 
775  heap_freetuple(tup);
776 
778 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
int errcode(int sqlerrcode)
Definition: elog.c:610
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
static void AlterPublicationOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
#define ereport(elevel,...)
Definition: elog.h:144
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:174
int errmsg(const char *fmt,...)
Definition: elog.c:824
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ CreatePublication()

ObjectAddress CreatePublication ( CreatePublicationStmt stmt)

Definition at line 148 of file publicationcmds.c.

References ACL_CREATE, aclcheck_error(), ACLCHECK_OK, Assert, BoolGetDatum, 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, PublicationObjectIndexId, 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().

149 {
150  Relation rel;
151  ObjectAddress myself;
152  Oid puboid;
153  bool nulls[Natts_pg_publication];
154  Datum values[Natts_pg_publication];
155  HeapTuple tup;
156  bool publish_given;
157  PublicationActions pubactions;
158  bool publish_via_partition_root_given;
159  bool publish_via_partition_root;
160  AclResult aclresult;
161 
162  /* must have CREATE privilege on database */
164  if (aclresult != ACLCHECK_OK)
165  aclcheck_error(aclresult, OBJECT_DATABASE,
167 
168  /* FOR ALL TABLES requires superuser */
169  if (stmt->for_all_tables && !superuser())
170  ereport(ERROR,
171  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
172  errmsg("must be superuser to create FOR ALL TABLES publication")));
173 
174  rel = table_open(PublicationRelationId, RowExclusiveLock);
175 
176  /* Check if name is used */
177  puboid = GetSysCacheOid1(PUBLICATIONNAME, Anum_pg_publication_oid,
178  CStringGetDatum(stmt->pubname));
179  if (OidIsValid(puboid))
180  {
181  ereport(ERROR,
183  errmsg("publication \"%s\" already exists",
184  stmt->pubname)));
185  }
186 
187  /* Form a tuple. */
188  memset(values, 0, sizeof(values));
189  memset(nulls, false, sizeof(nulls));
190 
191  values[Anum_pg_publication_pubname - 1] =
193  values[Anum_pg_publication_pubowner - 1] = ObjectIdGetDatum(GetUserId());
194 
196  &publish_given, &pubactions,
197  &publish_via_partition_root_given,
198  &publish_via_partition_root);
199 
201  Anum_pg_publication_oid);
202  values[Anum_pg_publication_oid - 1] = ObjectIdGetDatum(puboid);
203  values[Anum_pg_publication_puballtables - 1] =
205  values[Anum_pg_publication_pubinsert - 1] =
206  BoolGetDatum(pubactions.pubinsert);
207  values[Anum_pg_publication_pubupdate - 1] =
208  BoolGetDatum(pubactions.pubupdate);
209  values[Anum_pg_publication_pubdelete - 1] =
210  BoolGetDatum(pubactions.pubdelete);
211  values[Anum_pg_publication_pubtruncate - 1] =
212  BoolGetDatum(pubactions.pubtruncate);
213  values[Anum_pg_publication_pubviaroot - 1] =
214  BoolGetDatum(publish_via_partition_root);
215 
216  tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
217 
218  /* Insert tuple into catalog. */
219  CatalogTupleInsert(rel, tup);
220  heap_freetuple(tup);
221 
222  recordDependencyOnOwner(PublicationRelationId, puboid, GetUserId());
223 
224  ObjectAddressSet(myself, PublicationRelationId, puboid);
225 
226  /* Make the changes visible. */
228 
229  if (stmt->tables)
230  {
231  List *rels;
232 
233  Assert(list_length(stmt->tables) > 0);
234 
235  rels = OpenTableList(stmt->tables);
236  PublicationAddTables(puboid, rels, true, NULL);
237  CloseTableList(rels);
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:317
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:1071
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:151
#define RelationGetDescr(relation)
Definition: rel.h:482
Oid GetUserId(void)
Definition: miscinit.c:476
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition: syscache.h:192
int wal_level
Definition: xlog.c:107
int errcode(int sqlerrcode)
Definition: elog.c:610
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:624
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:164
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:651
static void CloseTableList(List *rels)
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3294
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define PublicationObjectIndexId
Definition: indexing.h:357
#define ACL_CREATE
Definition: parsenodes.h:84
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2155
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CStringGetDatum(X)
Definition: postgres.h:578
#define WARNING
Definition: elog.h:40
AclResult
Definition: acl.h:177
uintptr_t Datum
Definition: postgres.h:367
void CommandCounterIncrement(void)
Definition: xact.c:1021
Oid MyDatabaseId
Definition: globals.c:85
#define BoolGetDatum(X)
Definition: postgres.h:402
#define ereport(elevel,...)
Definition: elog.h:144
AclResult pg_database_aclcheck(Oid db_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4575
#define Assert(condition)
Definition: c.h:745
static int list_length(const List *l)
Definition: pg_list.h:169
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
static Datum values[MAXATTR]
Definition: bootstrap.c:165
static void parse_publication_options(List *options, bool *publish_given, PublicationActions *pubactions, bool *publish_via_partition_root_given, bool *publish_via_partition_root)
int errmsg(const char *fmt,...)
Definition: elog.c:824
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)

◆ RemovePublicationRelById()

void RemovePublicationRelById ( Oid  proid)

Definition at line 475 of file publicationcmds.c.

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

Referenced by doDeletion().

476 {
477  Relation rel;
478  HeapTuple tup;
480 
481  rel = table_open(PublicationRelRelationId, RowExclusiveLock);
482 
484 
485  if (!HeapTupleIsValid(tup))
486  elog(ERROR, "cache lookup failed for publication table %u",
487  proid);
488 
489  pubrel = (Form_pg_publication_rel) GETSTRUCT(tup);
490 
491  /* Invalidate relcache so that publication info is rebuilt. */
492  CacheInvalidateRelcacheByRelid(pubrel->prrelid);
493 
494  CatalogTupleDelete(rel, &tup->t_self);
495 
496  ReleaseSysCache(tup);
497 
499 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:350
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
void CacheInvalidateRelcacheByRelid(Oid relid)
Definition: inval.c:1337
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define elog(elevel,...)
Definition: elog.h:214
FormData_pg_publication_rel * Form_pg_publication_rel
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39