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 RemovePublicationById (Oid pubid)
 
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 412 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().

413 {
414  Relation rel;
415  HeapTuple tup;
416  Form_pg_publication pubform;
417 
418  rel = table_open(PublicationRelationId, RowExclusiveLock);
419 
421  CStringGetDatum(stmt->pubname));
422 
423  if (!HeapTupleIsValid(tup))
424  ereport(ERROR,
425  (errcode(ERRCODE_UNDEFINED_OBJECT),
426  errmsg("publication \"%s\" does not exist",
427  stmt->pubname)));
428 
429  pubform = (Form_pg_publication) GETSTRUCT(tup);
430 
431  /* must be owner */
432  if (!pg_publication_ownercheck(pubform->oid, GetUserId()))
434  stmt->pubname);
435 
436  if (stmt->options)
437  AlterPublicationOptions(stmt, rel, tup);
438  else
439  AlterPublicationTables(stmt, rel, tup);
440 
441  /* Cleanup. */
442  heap_freetuple(tup);
444 }
static void AlterPublicationOptions(AlterPublicationStmt *stmt, Relation rel, HeapTuple tup)
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
Oid GetUserId(void)
Definition: miscinit.c:380
bool pg_publication_ownercheck(Oid pub_oid, Oid roleid)
Definition: aclchk.c:5291
int errcode(int sqlerrcode)
Definition: elog.c:608
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3352
#define ERROR
Definition: elog.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CStringGetDatum(X)
Definition: postgres.h:578
#define ereport(elevel, rest)
Definition: elog.h:141
static void AlterPublicationTables(AlterPublicationStmt *stmt, Relation rel, HeapTuple tup)
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:174
int errmsg(const char *fmt,...)
Definition: elog.c:822
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 716 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().

717 {
718  Oid subid;
719  HeapTuple tup;
720  Relation rel;
721  ObjectAddress address;
722  Form_pg_publication pubform;
723 
724  rel = table_open(PublicationRelationId, RowExclusiveLock);
725 
727 
728  if (!HeapTupleIsValid(tup))
729  ereport(ERROR,
730  (errcode(ERRCODE_UNDEFINED_OBJECT),
731  errmsg("publication \"%s\" does not exist", name)));
732 
733  pubform = (Form_pg_publication) GETSTRUCT(tup);
734  subid = pubform->oid;
735 
736  AlterPublicationOwner_internal(rel, tup, newOwnerId);
737 
738  ObjectAddressSet(address, PublicationRelationId, subid);
739 
740  heap_freetuple(tup);
741 
743 
744  return address;
745 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
int errcode(int sqlerrcode)
Definition: elog.c:608
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
#define ereport(elevel, rest)
Definition: elog.h:141
static void AlterPublicationOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
const char * name
Definition: encode.c:521
#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:822
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 751 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().

752 {
753  HeapTuple tup;
754  Relation rel;
755 
756  rel = table_open(PublicationRelationId, RowExclusiveLock);
757 
759 
760  if (!HeapTupleIsValid(tup))
761  ereport(ERROR,
762  (errcode(ERRCODE_UNDEFINED_OBJECT),
763  errmsg("publication with OID %u does not exist", subid)));
764 
765  AlterPublicationOwner_internal(rel, tup, newOwnerId);
766 
767  heap_freetuple(tup);
768 
770 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
int errcode(int sqlerrcode)
Definition: elog.c:608
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
#define ereport(elevel, rest)
Definition: elog.h:141
static void AlterPublicationOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:174
int errmsg(const char *fmt,...)
Definition: elog.c:822
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ CreatePublication()

ObjectAddress CreatePublication ( CreatePublicationStmt stmt)

Definition at line 136 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(), PublicationAddTables(), PUBLICATIONNAME, PublicationObjectIndexId, CreatePublicationStmt::pubname, recordDependencyOnOwner(), RelationGetDescr, RowExclusiveLock, superuser(), table_close(), table_open(), CreatePublicationStmt::tables, values, wal_level, WAL_LEVEL_LOGICAL, and WARNING.

Referenced by ProcessUtilitySlow().

137 {
138  Relation rel;
139  ObjectAddress myself;
140  Oid puboid;
141  bool nulls[Natts_pg_publication];
142  Datum values[Natts_pg_publication];
143  HeapTuple tup;
144  bool publish_given;
145  bool publish_insert;
146  bool publish_update;
147  bool publish_delete;
148  bool publish_truncate;
149  AclResult aclresult;
150 
151  /* must have CREATE privilege on database */
153  if (aclresult != ACLCHECK_OK)
154  aclcheck_error(aclresult, OBJECT_DATABASE,
156 
157  /* FOR ALL TABLES requires superuser */
158  if (stmt->for_all_tables && !superuser())
159  ereport(ERROR,
160  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
161  (errmsg("must be superuser to create FOR ALL TABLES publication"))));
162 
163  rel = table_open(PublicationRelationId, RowExclusiveLock);
164 
165  /* Check if name is used */
166  puboid = GetSysCacheOid1(PUBLICATIONNAME, Anum_pg_publication_oid,
167  CStringGetDatum(stmt->pubname));
168  if (OidIsValid(puboid))
169  {
170  ereport(ERROR,
172  errmsg("publication \"%s\" already exists",
173  stmt->pubname)));
174  }
175 
176  /* Form a tuple. */
177  memset(values, 0, sizeof(values));
178  memset(nulls, false, sizeof(nulls));
179 
180  values[Anum_pg_publication_pubname - 1] =
182  values[Anum_pg_publication_pubowner - 1] = ObjectIdGetDatum(GetUserId());
183 
185  &publish_given, &publish_insert,
186  &publish_update, &publish_delete,
187  &publish_truncate);
188 
190  Anum_pg_publication_oid);
191  values[Anum_pg_publication_oid - 1] = ObjectIdGetDatum(puboid);
192  values[Anum_pg_publication_puballtables - 1] =
194  values[Anum_pg_publication_pubinsert - 1] =
195  BoolGetDatum(publish_insert);
196  values[Anum_pg_publication_pubupdate - 1] =
197  BoolGetDatum(publish_update);
198  values[Anum_pg_publication_pubdelete - 1] =
199  BoolGetDatum(publish_delete);
200  values[Anum_pg_publication_pubtruncate - 1] =
201  BoolGetDatum(publish_truncate);
202 
203  tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
204 
205  /* Insert tuple into catalog. */
206  CatalogTupleInsert(rel, tup);
207  heap_freetuple(tup);
208 
209  recordDependencyOnOwner(PublicationRelationId, puboid, GetUserId());
210 
211  ObjectAddressSet(myself, PublicationRelationId, puboid);
212 
213  /* Make the changes visible. */
215 
216  if (stmt->tables)
217  {
218  List *rels;
219 
220  Assert(list_length(stmt->tables) > 0);
221 
222  rels = OpenTableList(stmt->tables);
223  PublicationAddTables(puboid, rels, true, NULL);
224  CloseTableList(rels);
225  }
226 
228 
229  InvokeObjectPostCreateHook(PublicationRelationId, puboid, 0);
230 
232  {
234  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
235  errmsg("wal_level is insufficient to publish logical changes"),
236  errhint("Set wal_level to logical before creating subscriptions.")));
237  }
238 
239  return myself;
240 }
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:322
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:133
int errhint(const char *fmt,...)
Definition: elog.c:1069
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:151
#define RelationGetDescr(relation)
Definition: rel.h:448
Oid GetUserId(void)
Definition: miscinit.c:380
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition: syscache.h:192
int wal_level
Definition: xlog.c:103
int errcode(int sqlerrcode)
Definition: elog.c:608
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:615
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:645
static void CloseTableList(List *rels)
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3352
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define PublicationObjectIndexId
Definition: indexing.h:349
#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 ereport(elevel, rest)
Definition: elog.h:141
#define WARNING
Definition: elog.h:40
AclResult
Definition: acl.h:177
uintptr_t Datum
Definition: postgres.h:367
void CommandCounterIncrement(void)
Definition: xact.c:1005
Oid MyDatabaseId
Definition: globals.c:85
#define BoolGetDatum(X)
Definition: postgres.h:402
AclResult pg_database_aclcheck(Oid db_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4641
#define Assert(condition)
Definition: c.h:739
static void parse_publication_options(List *options, bool *publish_given, bool *publish_insert, bool *publish_update, bool *publish_delete, bool *publish_truncate)
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:167
int errmsg(const char *fmt,...)
Definition: elog.c:822
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:31
Definition: pg_list.h:50
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:183
static List * OpenTableList(List *tables)

◆ RemovePublicationById()

void RemovePublicationById ( Oid  pubid)

Definition at line 450 of file publicationcmds.c.

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

Referenced by doDeletion().

451 {
452  Relation rel;
453  HeapTuple tup;
454 
455  rel = table_open(PublicationRelationId, RowExclusiveLock);
456 
458 
459  if (!HeapTupleIsValid(tup))
460  elog(ERROR, "cache lookup failed for publication %u", pubid);
461 
462  CatalogTupleDelete(rel, &tup->t_self);
463 
464  ReleaseSysCache(tup);
465 
467 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:269
#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
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:228
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ RemovePublicationRelById()

void RemovePublicationRelById ( Oid  proid)

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

474 {
475  Relation rel;
476  HeapTuple tup;
478 
479  rel = table_open(PublicationRelRelationId, RowExclusiveLock);
480 
482 
483  if (!HeapTupleIsValid(tup))
484  elog(ERROR, "cache lookup failed for publication table %u",
485  proid);
486 
487  pubrel = (Form_pg_publication_rel) GETSTRUCT(tup);
488 
489  /* Invalidate relcache so that publication info is rebuilt. */
490  CacheInvalidateRelcacheByRelid(pubrel->prrelid);
491 
492  CatalogTupleDelete(rel, &tup->t_self);
493 
494  ReleaseSysCache(tup);
495 
497 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:269
#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:1329
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:228
FormData_pg_publication_rel * Form_pg_publication_rel
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39