Logical replication
authorPeter Eisentraut <peter_e@gmx.net>
Thu, 19 Jan 2017 17:00:00 +0000 (12:00 -0500)
committerPeter Eisentraut <peter_e@gmx.net>
Fri, 20 Jan 2017 14:04:49 +0000 (09:04 -0500)
- Add PUBLICATION catalogs and DDL
- Add SUBSCRIPTION catalog and DDL
- Define logical replication protocol and output plugin
- Add logical replication workers

From: Petr Jelinek <petr@2ndquadrant.com>
Reviewed-by: Steve Singer <steve@ssinger.info>
Reviewed-by: Andres Freund <andres@anarazel.de>
Reviewed-by: Erik Rijkers <er@xs4all.nl>
Reviewed-by: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
119 files changed:
doc/src/sgml/catalogs.sgml
doc/src/sgml/config.sgml
doc/src/sgml/filelist.sgml
doc/src/sgml/func.sgml
doc/src/sgml/logical-replication.sgml [new file with mode: 0644]
doc/src/sgml/monitoring.sgml
doc/src/sgml/postgres.sgml
doc/src/sgml/protocol.sgml
doc/src/sgml/ref/allfiles.sgml
doc/src/sgml/ref/alter_publication.sgml [new file with mode: 0644]
doc/src/sgml/ref/alter_subscription.sgml [new file with mode: 0644]
doc/src/sgml/ref/create_publication.sgml [new file with mode: 0644]
doc/src/sgml/ref/create_subscription.sgml [new file with mode: 0644]
doc/src/sgml/ref/drop_publication.sgml [new file with mode: 0644]
doc/src/sgml/ref/drop_subscription.sgml [new file with mode: 0644]
doc/src/sgml/ref/pg_dump.sgml
doc/src/sgml/ref/psql-ref.sgml
doc/src/sgml/reference.sgml
src/Makefile
src/backend/access/transam/xact.c
src/backend/catalog/Makefile
src/backend/catalog/aclchk.c
src/backend/catalog/catalog.c
src/backend/catalog/dependency.c
src/backend/catalog/objectaddress.c
src/backend/catalog/pg_publication.c [new file with mode: 0644]
src/backend/catalog/pg_shdepend.c
src/backend/catalog/pg_subscription.c [new file with mode: 0644]
src/backend/catalog/system_views.sql
src/backend/commands/Makefile
src/backend/commands/alter.c
src/backend/commands/dbcommands.c
src/backend/commands/define.c
src/backend/commands/dropcmds.c
src/backend/commands/event_trigger.c
src/backend/commands/publicationcmds.c [new file with mode: 0644]
src/backend/commands/subscriptioncmds.c [new file with mode: 0644]
src/backend/commands/tablecmds.c
src/backend/executor/Makefile
src/backend/executor/execMain.c
src/backend/executor/execReplication.c [new file with mode: 0644]
src/backend/nodes/copyfuncs.c
src/backend/nodes/equalfuncs.c
src/backend/parser/gram.y
src/backend/postmaster/bgworker.c
src/backend/postmaster/pgstat.c
src/backend/postmaster/postmaster.c
src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
src/backend/replication/logical/Makefile
src/backend/replication/logical/launcher.c [new file with mode: 0644]
src/backend/replication/logical/proto.c [new file with mode: 0644]
src/backend/replication/logical/relation.c [new file with mode: 0644]
src/backend/replication/logical/worker.c [new file with mode: 0644]
src/backend/replication/pgoutput/Makefile [new file with mode: 0644]
src/backend/replication/pgoutput/pgoutput.c [new file with mode: 0644]
src/backend/replication/walreceiver.c
src/backend/storage/ipc/ipci.c
src/backend/storage/lmgr/lwlocknames.txt
src/backend/tcop/utility.c
src/backend/utils/cache/inval.c
src/backend/utils/cache/relcache.c
src/backend/utils/cache/syscache.c
src/backend/utils/misc/guc.c
src/backend/utils/misc/postgresql.conf.sample
src/bin/pg_dump/common.c
src/bin/pg_dump/pg_backup.h
src/bin/pg_dump/pg_backup_archiver.c
src/bin/pg_dump/pg_dump.c
src/bin/pg_dump/pg_dump.h
src/bin/pg_dump/pg_dump_sort.c
src/bin/pg_dump/pg_restore.c
src/bin/pg_dump/t/002_pg_dump.pl
src/bin/psql/command.c
src/bin/psql/describe.c
src/bin/psql/describe.h
src/bin/psql/help.c
src/bin/psql/tab-complete.c
src/include/catalog/dependency.h
src/include/catalog/indexing.h
src/include/catalog/pg_proc.h
src/include/catalog/pg_publication.h [new file with mode: 0644]
src/include/catalog/pg_publication_rel.h [new file with mode: 0644]
src/include/catalog/pg_subscription.h [new file with mode: 0644]
src/include/commands/defrem.h
src/include/commands/publicationcmds.h [new file with mode: 0644]
src/include/commands/subscriptioncmds.h [new file with mode: 0644]
src/include/executor/executor.h
src/include/nodes/nodes.h
src/include/nodes/parsenodes.h
src/include/parser/kwlist.h
src/include/pgstat.h
src/include/replication/logicallauncher.h [new file with mode: 0644]
src/include/replication/logicalproto.h [new file with mode: 0644]
src/include/replication/logicalrelation.h [new file with mode: 0644]
src/include/replication/logicalworker.h [new file with mode: 0644]
src/include/replication/pgoutput.h [new file with mode: 0644]
src/include/replication/walreceiver.h
src/include/replication/worker_internal.h [new file with mode: 0644]
src/include/storage/sinval.h
src/include/utils/acl.h
src/include/utils/inval.h
src/include/utils/rel.h
src/include/utils/relcache.h
src/include/utils/syscache.h
src/test/Makefile
src/test/perl/PostgresNode.pm
src/test/regress/expected/publication.out [new file with mode: 0644]
src/test/regress/expected/rules.out
src/test/regress/expected/sanity_check.out
src/test/regress/expected/subscription.out [new file with mode: 0644]
src/test/regress/parallel_schedule
src/test/regress/serial_schedule
src/test/regress/sql/publication.sql [new file with mode: 0644]
src/test/regress/sql/subscription.sql [new file with mode: 0644]
src/test/subscription/.gitignore [new file with mode: 0644]
src/test/subscription/Makefile [new file with mode: 0644]
src/test/subscription/README [new file with mode: 0644]
src/test/subscription/t/001_rep_changes.pl [new file with mode: 0644]
src/test/subscription/t/002_types.pl [new file with mode: 0644]

index 493050618df25c7037bdf0c48769fd0d3d75b78f..7c758a5081a2a5662032e0edd4348ea816e36fed 100644 (file)
       <entry>functions and procedures</entry>
      </row>
 
+     <row>
+      <entry><link linkend="catalog-pg-publication"><structname>pg_publication</structname></link></entry>
+      <entry>publications for logical replication</entry>
+     </row>
+
+     <row>
+      <entry><link linkend="catalog-pg-publication-rel"><structname>pg_publication_rel</structname></link></entry>
+      <entry>relation to publication mapping</entry>
+     </row>
+
      <row>
       <entry><link linkend="catalog-pg-range"><structname>pg_range</structname></link></entry>
       <entry>information about range types</entry>
       <entry>planner statistics</entry>
      </row>
 
+     <row>
+      <entry><link linkend="catalog-pg-subscription"><structname>pg_subscription</structname></link></entry>
+      <entry>logical replication subscriptions</entry>
+     </row>
+
      <row>
       <entry><link linkend="catalog-pg-tablespace"><structname>pg_tablespace</structname></link></entry>
       <entry>tablespaces within this database cluster</entry>
 
  </sect1>
 
+ <sect1 id="catalog-pg-publication">
+  <title><structname>pg_publication</structname></title>
+
+  <indexterm zone="catalog-pg-publication">
+   <primary>pg_publication</primary>
+  </indexterm>
+
+  <para>
+   The catalog <structname>pg_publication</structname> contains all
+   publications created in the database.  For more on publications see
+   <xref linkend="logical-replication-publication">.
+  </para>
+
+  <table>
+   <title><structname>pg_publication</structname> Columns</title>
+
+   <tgroup cols="4">
+    <thead>
+     <row>
+      <entry>Name</entry>
+      <entry>Type</entry>
+      <entry>References</entry>
+      <entry>Description</entry>
+     </row>
+    </thead>
+
+    <tbody>
+     <row>
+      <entry><structfield>oid</structfield></entry>
+      <entry><type>oid</type></entry>
+      <entry></entry>
+      <entry>Row identifier (hidden attribute; must be explicitly selected)</entry>
+     </row>
+
+     <row>
+      <entry><structfield>pubname</structfield></entry>
+      <entry><type>Name</type></entry>
+      <entry></entry>
+      <entry>Name of the publication</entry>
+     </row>
+
+     <row>
+      <entry><structfield>pubowner</structfield></entry>
+      <entry><type>oid</type></entry>
+      <entry><literal><link linkend="catalog-pg-authid"><structname>pg_authid</structname></link>.oid</literal></entry>
+      <entry>Owner of the publication</entry>
+     </row>
+
+     <row>
+      <entry><structfield>puballtables</structfield></entry>
+      <entry><type>bool</type></entry>
+      <entry></entry>
+      <entry>If true, this publication automatically includes all tables
+       in the database, including any that will be created in the future.
+      </entry>
+     </row>
+
+     <row>
+      <entry><structfield>pubinsert</structfield></entry>
+      <entry><type>bool</type></entry>
+      <entry></entry>
+      <entry>If true, <command>INSERT</command> operations are replicated for
+       tables in the publication.</entry>
+     </row>
+
+     <row>
+      <entry><structfield>pubupdate</structfield></entry>
+      <entry><type>bool</type></entry>
+      <entry></entry>
+      <entry>If true, <command>UPDATE</command> operations are replicated for
+       tables in the publication.</entry>
+     </row>
+
+     <row>
+      <entry><structfield>pubdelete</structfield></entry>
+      <entry><type>bool</type></entry>
+      <entry></entry>
+      <entry>If true, <command>DELETE</command> operations are replicated for
+       tables in the publication.</entry>
+     </row>
+    </tbody>
+   </tgroup>
+  </table>
+ </sect1>
+
+ <sect1 id="catalog-pg-publication-rel">
+  <title><structname>pg_publication_rel</structname></title>
+
+  <indexterm zone="catalog-pg-publication-rel">
+   <primary>pg_publication_rel</primary>
+  </indexterm>
+
+  <para>
+   The catalog <structname>pg_publication_rel</structname> contains the
+   mapping between relations and publications in the database.  This is a
+   many-to-many mapping.  See also <xref linkend="view-pg-publication-tables">
+   for a more user-friendly view of this information.
+  </para>
+
+  <table>
+   <title><structname>pg_publication_rel</structname> Columns</title>
+
+   <tgroup cols="4">
+    <thead>
+     <row>
+      <entry>Name</entry>
+      <entry>Type</entry>
+      <entry>References</entry>
+      <entry>Description</entry>
+     </row>
+    </thead>
+
+    <tbody>
+     <row>
+      <entry><structfield>prpubid</structfield></entry>
+      <entry><type>oid</type></entry>
+      <entry><literal><link linkend="catalog-pg-publication"><structname>pg_publication</structname></link>.oid</literal></entry>
+      <entry>Reference to publication</entry>
+     </row>
+
+     <row>
+      <entry><structfield>prrelid</structfield></entry>
+      <entry><type>oid</type></entry>
+      <entry><literal><link linkend="catalog-pg-class"><structname>pg_class</structname></link>.oid</literal></entry>
+      <entry>Reference to relation</entry>
+     </row>
+    </tbody>
+   </tgroup>
+  </table>
+ </sect1>
+
  <sect1 id="catalog-pg-range">
   <title><structname>pg_range</structname></title>
 
 
  </sect1>
 
+ <sect1 id="catalog-pg-subscription">
+  <title><structname>pg_subscription</structname></title>
+
+  <indexterm zone="catalog-pg-subscription">
+   <primary>pg_subscription</primary>
+  </indexterm>
+
+  <para>
+   The catalog <structname>pg_subscription</structname> contains all existing
+   logical replication subscriptions.  For more information about logical
+   replication see <xref linkend="logical-replication">.
+  </para>
+
+  <para>
+   Unlike most system catalogs, <structname>pg_subscription</structname> is
+   shared across all databases of a cluster: There is only one copy
+   of <structname>pg_subscription</structname> per cluster, not one per
+   database.
+  </para>
+
+  <para>
+   Access to this catalog is restricted from normal users.  Normal users can
+   use the view <xref linkend="pg-stat-subscription"> to get some information
+   about subscriptions.
+  </para>
+
+  <table>
+   <title><structname>pg_subscription</structname> Columns</title>
+
+   <tgroup cols="4">
+    <thead>
+     <row>
+      <entry>Name</entry>
+      <entry>Type</entry>
+      <entry>References</entry>
+      <entry>Description</entry>
+     </row>
+    </thead>
+
+    <tbody>
+     <row>
+      <entry><structfield>oid</structfield></entry>
+      <entry><type>oid</type></entry>
+      <entry></entry>
+      <entry>Row identifier (hidden attribute; must be explicitly selected)</entry>
+     </row>
+
+     <row>
+      <entry><structfield>subdbid</structfield></entry>
+      <entry><type>oid</type></entry>
+      <entry><literal><link linkend="catalog-pg-database"><structname>pg_database</structname></link>.oid</literal></entry>
+      <entry>OID of the database which the subscription resides in</entry>
+     </row>
+
+     <row>
+      <entry><structfield>subname</structfield></entry>
+      <entry><type>name</type></entry>
+      <entry></entry>
+      <entry>Name of the subscription</entry>
+     </row>
+
+     <row>
+      <entry><structfield>subowner</structfield></entry>
+      <entry><type>oid</type></entry>
+      <entry><literal><link linkend="catalog-pg-authid"><structname>pg_authid</structname></link>.oid</literal></entry>
+      <entry>Owner of the subscription</entry>
+     </row>
+
+     <row>
+      <entry><structfield>subenabled</structfield></entry>
+      <entry><type>bool</type></entry>
+      <entry></entry>
+      <entry>If true, the subscription is enabled and should be replicating.</entry>
+     </row>
+
+     <row>
+      <entry><structfield>subconninfo</structfield></entry>
+      <entry><type>text</type></entry>
+      <entry></entry>
+      <entry>Connection string to the upstream database</entry>
+     </row>
+
+     <row>
+      <entry><structfield>subslotname</structfield></entry>
+      <entry><type>name</type></entry>
+      <entry></entry>
+      <entry>Name of the replication slot in the upstream database. Also used
+       for local replication origin name.</entry>
+     </row>
+
+     <row>
+      <entry><structfield>subpublications</structfield></entry>
+      <entry><type>text[]</type></entry>
+      <entry></entry>
+      <entry>Array of subscribed publication names. These reference the
+       publications on the publisher server. For more on publications
+       see <xref linkend="logical-replication-publication">.
+      </entry>
+     </row>
+    </tbody>
+   </tgroup>
+  </table>
+ </sect1>
 
  <sect1 id="catalog-pg-tablespace">
   <title><structname>pg_tablespace</structname></title>
       <entry>prepared transactions</entry>
      </row>
 
+     <row>
+      <entry><link linkend="view-pg-publication-tables"><structname>pg_publication_tables</structname></link></entry>
+      <entry>publications and their associated tables</entry>
+     </row>
+
      <row>
       <entry><link linkend="view-pg-replication-origin-status"><structname>pg_replication_origin_status</structname></link></entry>
       <entry>information about replication origins, including replication progress</entry>
@@ -8871,6 +9125,61 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx
 
  </sect1>
 
+ <sect1 id="view-pg-publication-tables">
+  <title><structname>pg_publication_tables</structname></title>
+
+  <indexterm zone="view-pg-publication-tables">
+   <primary>pg_publication_tables</primary>
+  </indexterm>
+
+  <para>
+   The view <structname>pg_publication_tables</structname> provides
+   information about the mapping between publications and the tables they
+   contain.  Unlike the underlying
+   catalog <structname>pg_publication_rel</structname>, this view expands
+   publications defined as <literal>FOR ALL TABLES</literal>, so for such
+   publications there will be a row for each eligible table.
+  </para>
+
+  <table>
+   <title><structname>pg_publication_tables</structname> Columns</title>
+
+   <tgroup cols="4">
+    <thead>
+     <row>
+      <entry>Name</entry>
+      <entry>Type</entry>
+      <entry>References</entry>
+      <entry>Description</entry>
+     </row>
+    </thead>
+
+    <tbody>
+     <row>
+      <entry><structfield>pubname</structfield></entry>
+      <entry><type>name</type></entry>
+      <entry><literal><link linkend="catalog-pg-publication"><structname>pg_publication</structname></link>.pubname</literal></entry>
+      <entry>Name of publication</entry>
+     </row>
+
+     <row>
+      <entry><structfield>schemaname</structfield></entry>
+      <entry><type>name</type></entry>
+      <entry><literal><link linkend="catalog-pg-namespace"><structname>pg_namespace</structname></link>.nspname</literal></entry>
+      <entry>Name of schema containing table</entry>
+     </row>
+
+     <row>
+      <entry><structfield>tablename</structfield></entry>
+      <entry><type>name</type></entry>
+      <entry><literal><link linkend="catalog-pg-class"><structname>pg_class</structname></link>.relname</literal></entry>
+      <entry>Name of table</entry>
+     </row>
+    </tbody>
+   </tgroup>
+  </table>
+ </sect1>
+
   <sect1 id="view-pg-replication-origin-status">
   <title><structname>pg_replication_origin_status</structname></title>
 
index 07afa3c77a78ef740646b9aa7c7c3fe4a6f1b508..fb5d6473efee9cb8f916cae32b917fb7872be7ae 100644 (file)
@@ -3411,6 +3411,47 @@ ANY <replaceable class="parameter">num_sync</replaceable> ( <replaceable class="
 
      </variablelist>
     </sect2>
+
+    <sect2 id="runtime-config-replication-subscriber">
+     <title>Subscribers</title>
+
+     <para>
+      These settings control the behavior of a logical replication subscriber.
+      Their values on the publisher are irrelevant.
+     </para>
+
+     <para>
+      Note that <varname>wal_receiver_timeout</varname> and
+      <varname>wal_retrieve_retry_interval</varname> configuration parameters
+      affect the logical replication workers as well.
+     </para>
+
+     <variablelist>
+
+     <varlistentry id="guc-max-logical-replication-workers" xreflabel="max_logical_replication_workers">
+      <term><varname>max_logical_replication_workers</varname> (<type>int</type>)
+      <indexterm>
+       <primary><varname>max_logical_replication_workers</> configuration parameter</primary>
+      </indexterm>
+      </term>
+      <listitem>
+       <para>
+        Specifies maximum number of logical replication workers. This includes
+        both apply workers and table synchronization workers.
+       </para>
+       <para>
+        Logical replication workers are taken from the pool defined by
+        <varname>max_worker_processes</varname>.
+       </para>
+       <para>
+        The default value is 4.
+       </para>
+      </listitem>
+     </varlistentry>
+
+     </variablelist>
+    </sect2>
+
    </sect1>
 
    <sect1 id="runtime-config-query">
index 69649a7da4bbcf87aff2cfc4de44394dbf68460d..2624c627dcb3d36228cc40d2073ca55f7c8a9cf9 100644 (file)
@@ -50,6 +50,7 @@
 <!ENTITY config        SYSTEM "config.sgml">
 <!ENTITY user-manag    SYSTEM "user-manag.sgml">
 <!ENTITY wal           SYSTEM "wal.sgml">
+<!ENTITY logical-replication    SYSTEM "logical-replication.sgml">
 
 <!-- programmer's guide -->
 <!ENTITY bgworker   SYSTEM "bgworker.sgml">
index 2504a466e6d01712cf83cdb2079b56277934c271..b214218791e435194f6666f68e46f26a799e196c 100644 (file)
@@ -18762,7 +18762,7 @@ postgres=# SELECT * FROM pg_xlogfile_name_offset(pg_stop_backup());
       </row>
 
       <row>
-       <entry>
+       <entry id="pg-replication-origin-advance">
         <indexterm>
          <primary>pg_replication_origin_advance</primary>
         </indexterm>
diff --git a/doc/src/sgml/logical-replication.sgml b/doc/src/sgml/logical-replication.sgml
new file mode 100644 (file)
index 0000000..9312c0c
--- /dev/null
@@ -0,0 +1,396 @@
+<!-- doc/src/sgml/logical-replication.sgml -->
+
+<chapter id="logical-replication">
+ <title>Logical Replication</title>
+
+ <para>
+  Logical replication is a method of replicating data objects and their
+  changes, based upon their replication identity (usually a primary key).  We
+  use the term logical in contrast to physical replication, which uses exact
+  block addresses and byte-by-byte replication.  PostgreSQL supports both
+  mechanisms concurrently, see <xref linkend="high-availability">.  Logical
+  replication allows fine-grained control over both data replication and
+  security.
+ </para>
+
+ <para>
+  Logical replication uses a <firstterm>publish</firstterm>
+  and <firstterm>subscribe</firstterm> model with one or
+  more <firstterm>subscribers</firstterm> subscribing to one or more
+  <firstterm>publications</firstterm> on a <firstterm>publisher</firstterm>
+  node.  Subscribers pull data from the publications they subscribe to and may
+  subsequently re-publish data to allow cascading replication or more complex
+  configurations.
+ </para>
+
+ <para>
+  Logical replication sends the changes on the publisher to the subscriber as
+  they occur in real-time.  The subscriber applies the data in the same order
+  as the publisher so that transactional consistency is guaranteed for
+  publications within a single subscription.  This method of data replication
+  is sometimes referred to as transactional replication.
+ </para>
+
+ <para>
+  The typical use-cases for logical replication are:
+
+  <itemizedlist>
+   <listitem>
+    <para>
+     Sending incremental changes in a single database or a subset of a
+     database to subscribers as they occur.
+    </para>
+   </listitem>
+
+   <listitem>
+    <para>
+     Firing triggers for individual changes as they are incoming to
+     subscriber.
+    </para>
+   </listitem>
+
+   <listitem>
+    <para>
+     Consolidating multiple databases into a single one (for example for
+     analytical purposes).
+    </para>
+   </listitem>
+
+   <listitem>
+    <para>
+     Replicating between different major versions of PostgreSQL.
+    </para>
+   </listitem>
+
+   <listitem>
+    <para>
+     Giving access to replicated data to different groups of users.
+    </para>
+   </listitem>
+
+   <listitem>
+    <para>
+     Sharing a subset of the database between multiple databases.
+    </para>
+   </listitem>
+  </itemizedlist>
+ </para>
+
+ <para>
+  The subscriber database behaves in the same way as any other PostgreSQL
+  instance and can be used as a publisher for other databases by defining its
+  own publications.  When the subscriber is treated as read-only by
+  application, there will be no conflicts from a single subscription.  On the
+  other hand, if there are other writes done either by application or other
+  subscribers to the same set of tables conflicts can arise.
+ </para>
+
+ <sect1 id="logical-replication-publication">
+  <title>Publication</title>
+
+  <para>
+   A <firstterm>publication</firstterm> object can be defined on any physical
+   replication master.  The node where a publication is defined is referred to
+   as <firstterm>publisher</firstterm>.  A publication is a set of changes
+   generated from a group of tables, and might also be described as a change
+   set or replication set.  Each publication exists in only one database.
+  </para>
+
+  <para>
+   Publications are different from schemas and do not affect how the table is
+   accessed.  Each table can be added to multiple publications if needed.
+   Publications may currently only contain tables.  Objects must be added
+   explicitly, except when a publication is created for <literal>ALL
+   TABLES</literal>.
+  </para>
+
+  <para>
+   Publications can choose to limit the changes they produce to show
+   any combination of <command>INSERT</command>, <command>UPDATE</command>, and
+   <command>DELETE</command> in a similar way to the way triggers are fired by
+   particular event types.  If a table without a <literal>REPLICA
+   IDENTITY</literal> is added to a publication that
+   replicates <command>UPDATE</command> or <command>DELETE</command>
+   operations then subsequent <command>UPDATE</command>
+   or <command>DELETE</command> operations will fail on the publisher.
+  </para>
+
+  <para>
+   Every publication can have multiple subscribers.
+  </para>
+
+  <para>
+   A publication is created using the <xref linkend="sql-createpublication">
+   command and may be later altered or dropped using corresponding commands.
+  </para>
+
+  <para>
+   The individual tables can be added and removed dynamically using
+   <xref linkend="sql-alterpublication">.  Both the <literal>ADD
+   TABLE</literal> and <literal>DROP TABLE</literal> operations are
+   transactional; so the table will start or stop replicating at the correct
+   snapshot once the transaction has committed.
+  </para>
+ </sect1>
+
+ <sect1 id="logical-replication-subscription">
+  <title>Subscription</title>
+
+  <para>
+   A <firstterm>subscription</firstterm> is the downstream side of logical
+   replication.  The node where a subscription is defined is referred to as
+   the <firstterm>subscriber</firstterm>.  Subscription defines the connection
+   to another database and set of publications (one or more) to which it wants
+   to be subscribed.
+  </para>
+
+  <para>
+   The subscriber database behaves in the same way as any other PostgreSQL
+   instance and can be used as a publisher for other databases by defining its
+   own publications.
+  </para>
+
+  <para>
+   A subscriber node may have multiple subscriptions if desired.  It is
+   possible to define multiple subscriptions between a single
+   publisher-subscriber pair, in which case extra care must be taken to ensure
+   that the subscribed publication objects don't overlap.
+  </para>
+
+  <para>
+   Each subscription will receive changes via one replication slot (see
+   <xref linkend="streaming-replication-slots">).
+  </para>
+
+  <para>
+   Subscriptions are not dumped by <command>pg_dump</command> by default but
+   can be requested using the command-line
+   option <option>--subscriptions</option>.
+  </para>
+
+  <para>
+   The subscription is added using <xref linkend="sql-createsubscription"> and
+   can be stopped/resumed at any time using the
+   <xref linkend="sql-altersubscription"> command and removed using
+   <xref linkend="sql-dropsubscription">.
+  </para>
+
+  <para>
+   When a subscription is dropped and recreated, the synchronization
+   information is lost.  This means that the data has to be resynchronized
+   afterwards.
+  </para>
+
+  <para>
+   The schema definitions are not replicated and the published tables must
+   exist on the subsriber for replication to work.  Only regular tables may be
+   the target of replication.  For example, you can't replicate to a view.
+  </para>
+
+  <para>
+   The tables are matched between the publisher and the subscriber using the
+   fully qualified table name.  Replication to differently-named tables on the
+   subscriber is not supported.
+  </para>
+
+  <para>
+   Columns of a table are also matched by name.  A different order of columns
+   in the target table is allowed, but the column types have to match.
+  </para>
+ </sect1>
+
+ <sect1 id="logical-replication-conflicts">
+  <title>Conflicts</title>
+
+  <para>
+   The logical replication behaves similarly to normal DML operations in that
+   the data will be updated even if it was changed locally on the subscriber
+   node.  If the incoming data violates any constraints the replication will
+   stop.  This is referred to as a <firstterm>conflict</firstterm>.  When
+   replicating <command>UPDATE</command> or <command>DELETE</command>
+   operations, missing data will not produce a conflict and such operations
+   will simply be skipped.
+  </para>
+
+  <para>
+   A conflict will produce an error and will stop the replication; it must be
+   resolved manually by the user.  Details about the conflict can be found in
+   the subscriber's server log.
+  </para>
+
+  <para>
+   The resolution can be done either by changing data on the subscriber so
+   that it does not conflict with the incoming change or by skipping the
+   transaction that conflicts with the existing data.  The transaction can be
+   skipped by calling the <link linkend="pg-replication-origin-advance">
+   <function>pg_replication_origin_advance()</function></link> function with
+   a <parameter>node_name</parameter> corresponding to the subscription name.
+   The current position of origins can be seen in the
+   <link linkend="view-pg-replication-origin-status">
+   <structname>pg_replication_origin_status</structname></link> system view.
+  </para>
+ </sect1>
+
+ <sect1 id="logical-replication-architecture">
+  <title>Architecture</title>
+
+  <para>
+   Logical replication starts by copying a snapshot of the data on the
+   publisher database.  Once that is done, changes on the publisher are sent
+   to the subscriber as they occur in real time.  The subscriber applies data
+   in the order in which commits were made on the publisher so that
+   transactional consistency is guaranteed for the publications within any
+   single subscription.
+  </para>
+
+  <para>
+   Logical replication is built with an architecture similar to physical
+   streaming replication (see <xref linkend="streaming-replication">).  It is
+   implemented by <quote>walsender</quote> and the <quote>apply</quote>
+   processes.  The walsender starts logical decoding (described
+   in <xref linkend="logicaldecoding">) of the WAL and loads the standard
+   logical decoding plugin (pgoutput).  The plugin transforms the changes read
+   from WAL to the logical replication protocol
+   (see <xref linkend="protocol-logical-replication">) and filters the data
+   according to the publication specification.  The data is then continuously
+   transferred using the streaming replication protocol to the apply worker,
+   which maps the data to local tables and applies the individual changes as
+   they are received in exact transactional order.
+  </para>
+
+  <para>
+   The apply process on the subscriber database always runs with
+   <varname>session_replication_role</varname> set
+   to <literal>replica</literal>, which produces the usual effects on triggers
+   and constraints.
+  </para>
+ </sect1>
+
+  <sect1 id="logical-replication-monitoring">
+  <title>Monitoring</title>
+
+  <para>
+   Because logical replication is based on similar architecture as
+   <link linkend="streaming-replication">physical streaming replication</link>
+   the monitoring on a publication node is very similar to monitoring of
+   physical replication master
+   (see <xref linkend="streaming-replication-monitoring">).
+  </para>
+
+  <para>
+   The monitoring information about subscription is visible in
+   <link linkend="pg-stat-subscription"><literal>pg_stat_subscription</literal></link>.
+   This view contains one row for every subscription worker.  A subscription
+   can have zero or more active subscription workers depending on its state.
+  </para>
+
+  <para>
+   Normally, there is a single apply process running for an enabled
+   subscription.  A disabled subscription or a crashed subscription will have
+   zero rows in this view.
+  </para>
+ </sect1>
+
+ <sect1 id="logical-replication-security">
+  <title>Security</title>
+
+  <para>
+   Logical replication connections occur in the same way as physical streaming
+   replication.  It requires access to be specifically given using
+   <filename>pg_hba.conf</filename>.  The role used for the replication
+   connection must have the <literal>REPLICATION</literal> attribute.  This
+   gives a role access to both logical and physical replication.
+  </para>
+
+  <para>
+   To create a publication, the user must have the <literal>CREATE</literal>
+   privilege in the database.
+  </para>
+
+  <para>
+   To create a subscription, the user must be a superuser.
+  </para>
+
+  <para>
+   The subscription apply process will run in the local database with the
+   privileges of a superuser.
+  </para>
+
+  <para>
+   Privileges are only checked once at the start of a replication connection.
+   They are not re-checked as each change record is read from the publisher,
+   nor are they re-checked for each change when applied.
+  </para>
+ </sect1>
+
+ <sect1 id="logical-replication-config">
+  <title>Configuration Settings</title>
+
+  <para>
+   Logical replication requires several configuration options to be set.
+  </para>
+
+  <para>
+   On the publisher side, <varname>wal_level</varname> must be set to
+   <literal>logical</literal>, and <varname>max_replication_slots</varname>
+   has to be set to at least the number of subscriptions expected to connect.
+   And <varname>max_wal_senders</varname> should be set to at least the same
+   as <varname>max_replication_slots</varname> plus the number of physical replicas
+   that are connected at the same time.
+  </para>
+
+  <para>
+   The subscriber also requires the <varname>max_replication_slots</varname>
+   to be set.  In this case it should be set to at least the number of
+   subscriptions that will be added to the subscriber.
+   <varname>max_logical_replication_workers</varname> has to be set to at
+   least the number of subscriptions.  Additionally the
+   <varname>max_worker_processes</varname> may need to be adjusted to
+   accommodate for replication workers, at least
+   (<varname>max_logical_replication_workers</varname>
+   + <literal>1</literal>).  Note that some extensions and parallel queries
+   also take worker slots from <varname>max_worker_processes</varname>.
+  </para>
+ </sect1>
+
+ <sect1 id="logical-replication-quick-setup">
+  <title>Quick Setup</title>
+
+  <para>
+   First set the configuration options in <filename>postgresql.conf</filename>:
+<programlisting>
+wal_level = logical
+</programlisting>
+   The other required settings have default values that are sufficient for a
+   basic setup.
+  </para>
+
+  <para>
+   <filename>pg_hba.conf</filename> needs to be adjusted to allow replication
+   (the values here depend on your actual network configuration and user you
+   want to use for connecting):
+<programlisting>
+host    replication     repuser     0.0.0.0/0       md5
+</programlisting>
+  </para>
+
+  <para>
+   Then on the publisher database:
+<programlisting>
+CREATE PUBLICATION mypub FOR TABLE users, departments;
+</programlisting>
+  </para>
+
+  <para>
+   And on the subscriber database:
+<programlisting>
+CREATE SUBSCRIPTION mysub CONNECTION 'dbname=foo host=bar user=repuser' PUBLICATION mypub;
+</programlisting>
+  </para>
+
+  <para>
+   The above will start the replication process of changes to
+   <literal>users</literal> and <literal>departments</literal> tables.
+  </para>
+ </sect1>
+</chapter>
index 1545f03656c94f76ec2cb5c4b703300f8da88666..01fad3870f678350017d204687cdba457af3937f 100644 (file)
@@ -308,6 +308,14 @@ postgres   27093  0.0  0.0  30096  2752 ?        Ss   11:34   0:00 postgres: ser
       </entry>
      </row>
 
+     <row>
+      <entry><structname>pg_stat_subscription</><indexterm><primary>pg_stat_subscription</primary></indexterm></entry>
+      <entry>At least one row per subscription, showing information about
+       the subscription workers.
+       See <xref linkend="pg-stat-subscription"> for details.
+      </entry>
+     </row>
+
      <row>
       <entry><structname>pg_stat_ssl</><indexterm><primary>pg_stat_ssl</primary></indexterm></entry>
       <entry>One row per connection (regular and replication), showing information about
@@ -1545,6 +1553,72 @@ SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event i
    connected server.
   </para>
 
+  <table id="pg-stat-subscription" xreflabel="pg_stat_subscription">
+   <title><structname>pg_stat_subscription</structname> View</title>
+   <tgroup cols="3">
+    <thead>
+    <row>
+      <entry>Column</entry>
+      <entry>Type</entry>
+      <entry>Description</entry>
+     </row>
+    </thead>
+
+   <tbody>
+    <row>
+     <entry><structfield>subid</></entry>
+     <entry><type>oid</></entry>
+     <entry>OID of the subscription</entry>
+    </row>
+    <row>
+     <entry><structfield>subname</></entry>
+     <entry><type>text</></entry>
+     <entry>Name of the subscription</entry>
+    </row>
+    <row>
+     <entry><structfield>pid</></entry>
+     <entry><type>integer</></entry>
+     <entry>Process ID of the subscription worker process</entry>
+    </row>
+    <row>
+     <entry><structfield>received_lsn</></entry>
+     <entry><type>pg_lsn</></entry>
+     <entry>Last transaction log position received, the initial value of
+      this field being 0</entry>
+    </row>
+    <row>
+     <entry><structfield>last_msg_send_time</></entry>
+     <entry><type>timestamp with time zone</></entry>
+     <entry>Send time of last message received from origin WAL sender</entry>
+    </row>
+    <row>
+     <entry><structfield>last_msg_receipt_time</></entry>
+     <entry><type>timestamp with time zone</></entry>
+     <entry>Receipt time of last message received from origin WAL sender
+     </entry>
+    </row>
+    <row>
+     <entry><structfield>latest_end_lsn</></entry>
+     <entry><type>pg_lsn</></entry>
+     <entry>Last transaction log position reported to origin WAL sender
+     </entry>
+    </row>
+    <row>
+     <entry><structfield>latest_end_time</></entry>
+     <entry><type>timestamp with time zone</></entry>
+     <entry>Time of last transaction log position reported to origin WAL
+      sender</entry>
+    </row>
+   </tbody>
+   </tgroup>
+  </table>
+
+  <para>
+   The <structname>pg_stat_subscription</structname> view will contain one
+   row per subscription for main worker (with null PID if the worker is
+   not running).
+  </para>
+
   <table id="pg-stat-ssl-view" xreflabel="pg_stat_ssl">
    <title><structname>pg_stat_ssl</structname> View</title>
    <tgroup cols="3">
index 9143917c490b753e832bb301923b8e2f691c5ea7..4e169d1b18961c98b55ac023374ebed737f3e62b 100644 (file)
   &monitoring;
   &diskusage;
   &wal;
+  &logical-replication;
   &regress;
 
  </part>
index 9ba147cae5e7917e06fdc11ad92ae905ce17fee2..5f89db5857021d6dba0dcf460f2d850a96ed9813 100644 (file)
@@ -2122,6 +2122,119 @@ The commands accepted in walsender mode are:
 
 </sect1>
 
+<sect1 id="protocol-logical-replication">
+ <title>Logical Streaming Replication Protocol</title>
+
+ <para>
+  This section describes the logical replication protocol, which is the message
+  flow started by the <literal>START_REPLICATION</literal>
+  <literal>SLOT</literal> <replaceable class="parameter">slot_name</>
+  <literal>LOGICAL</literal> replication command.
+ </para>
+
+ <para>
+  The logical streaming replication protocol builds on the primitives of
+  the physical streaming replication protocol.
+ </para>
+
+ <sect2 id="protocol-logical-replication-params">
+  <title>Logical Streaming Replication Parameters</title>
+
+  <para>
+   The logical replication <literal>START_REPLICATION</literal> command
+   accepts following parameters:
+
+   <variablelist>
+    <varlistentry>
+     <term>
+      proto_version
+     </term>
+     <listitem>
+      <para>
+       Protocol version. Currently only version <literal>1</literal> is
+       supported.
+      </para>
+     </listitem>
+    </varlistentry>
+
+    <varlistentry>
+     <term>
+      publication_names
+     </term>
+     <listitem>
+      <para>
+       Comma separated list of publication names for which to subscribe
+       (receive changes). The individual publication names are treated
+       as standard objects names and can be quoted the same as needed.
+      </para>
+     </listitem>
+    </varlistentry>
+   </variablelist>
+
+  </para>
+ </sect2>
+
+ <sect2 id="protocol-logical-messages">
+  <title>Logical Replication Protocol Messages</title>
+
+  <para>
+   The individual protocol messages are discussed in the following
+   subsections. Individual messages are describer in
+   <xref linkend="protocol-logicalrep-message-formats"> section.
+  </para>
+
+  <para>
+   All top-level protocol messages begin with a message type byte.
+   While represented in code as a character, this is a signed byte with no
+   associated encoding.
+  </para>
+
+  <para>
+   Since the streaming replication protocol supplies a message length there
+   is no need for top-level protocol messages to embed a length in their
+   header.
+  </para>
+
+ </sect2>
+
+ <sect2 id="protocol-logical-messages-flow">
+  <title>Logical Replication Protocol Message Flow</title>
+
+  <para>
+   With the exception of the <literal>START_REPLICATION</literal> command and
+   the replay progress messages, all information flows only from the backend
+   to the frontend.
+  </para>
+
+  <para>
+   The logical replication protocol sends individual transactions one by one.
+   This means that all messages between a pair of Begin and Commit messages
+   belong to the same transaction.
+  </para>
+
+  <para>
+   Every sent transaction contains zero or more DML messages (Insert,
+   Update, Delete). In case of a cascaded setup it can also contain Origin
+   messages. The origin message indicated that the transaction originated on
+   different replication node. Since a replication node in the scope of logical
+   replication protocol can be pretty much anything, the only identifier
+   is the origin name. It's downstream's responsibility to handle this as
+   needed (if needed). The Origin message is always sent before any DML
+   messages in the transaction.
+  </para>
+
+  <para>
+   Every DML message contains an arbitrary relation ID, which can be mapped to
+   an ID in the Relation messages. The Relation messages describe the schema of the
+   given relation. The Relation message is sent for a given relation either
+   because it is the first time we send a DML message for given relation in the
+   current session or because the relation definition has changed since the
+   last Relation message was sent for it. The protocol assumes that the client
+   is capable of caching the metadata for as many relations as needed.
+  </para>
+ </sect2>
+</sect1>
+
 <sect1 id="protocol-message-types">
 <title>Message Data Types</title>
 
@@ -5149,6 +5262,614 @@ not line breaks.
 
 </sect1>
 
+<sect1 id="protocol-logicalrep-message-formats">
+<title>Logical Replication Message Formats</title>
+
+<para>
+This section describes the detailed format of each logical replication message.
+These messages are returned either by the replication slot SQL interface or are
+sent by a walsender. In case of a walsender they are encapsulated inside the replication
+protocol WAL messages as described in <xref linkend="protocol-replication">
+and generally obey same message flow as physical replication.
+</para>
+
+<variablelist>
+
+<varlistentry>
+<term>
+Begin
+</term>
+<listitem>
+<para>
+
+<variablelist>
+<varlistentry>
+<term>
+        Byte1('B')
+</term>
+<listitem>
+<para>
+                Identifies the message as a begin message.
+</para>
+</listitem>
+</varlistentry>
+<varlistentry>
+<term>
+        Int64
+</term>
+<listitem>
+<para>
+                The final LSN of the transaction.
+</para>
+</listitem>
+</varlistentry>
+<varlistentry>
+<term>
+        Int64
+</term>
+<listitem>
+<para>
+                Commit timestamp of the transaction. The value is in number
+                of microseconds since PostgreSQL epoch (2000-01-01).
+</para>
+</listitem>
+</varlistentry>
+<varlistentry>
+<term>
+        Int32
+</term>
+<listitem>
+<para>
+                Xid of the transaction.
+</para>
+</listitem>
+</varlistentry>
+
+</variablelist>
+</para>
+</listitem>
+</varlistentry>
+
+<varlistentry>
+<term>
+Commit
+</term>
+<listitem>
+<para>
+
+<variablelist>
+<varlistentry>
+<term>
+        Byte1('C')
+</term>
+<listitem>
+<para>
+                Identifies the message as a commit message.
+</para>
+</listitem>
+</varlistentry>
+<varlistentry>
+<term>
+        Int64
+</term>
+<listitem>
+<para>
+                The LSN of the commit.
+</para>
+</listitem>
+</varlistentry>
+<varlistentry>
+<term>
+        Int64
+</term>
+<listitem>
+<para>
+                The end LSN of the transaction.
+</para>
+</listitem>
+</varlistentry>
+<varlistentry>
+<term>
+        Int64
+</term>
+<listitem>
+<para>
+                Commit timestamp of the transaction. The value is in number
+                of microseconds since PostgreSQL epoch (2000-01-01).
+</para>
+</listitem>
+</varlistentry>
+
+</variablelist>
+</para>
+</listitem>
+</varlistentry>
+
+<varlistentry>
+<term>
+Origin
+</term>
+<listitem>
+<para>
+
+<variablelist>
+<varlistentry>
+<term>
+        Byte1('O')
+</term>
+<listitem>
+<para>
+                Identifies the message as an origin message.
+</para>
+</listitem>
+</varlistentry>
+<varlistentry>
+<term>
+        Int64
+</term>
+<listitem>
+<para>
+                The LSN of the commit on the origin server.
+</para>
+</listitem>
+</varlistentry>
+<varlistentry>
+<term>
+        String
+</term>
+<listitem>
+<para>
+                Name of the origin.
+</para>
+</listitem>
+</varlistentry>
+
+</variablelist>
+</para>
+
+<para>
+  Note that there can be multiple Origin messages inside a single transaction.
+</para>
+
+</listitem>
+</varlistentry>
+
+<varlistentry>
+<term>
+Relation
+</term>
+<listitem>
+<para>
+
+<variablelist>
+<varlistentry>
+<term>
+        Byte1('R')
+</term>
+<listitem>
+<para>
+                Identifies the message as a relation message.
+</para>
+</listitem>
+</varlistentry>
+<varlistentry>
+<term>
+        Int32
+</term>
+<listitem>
+<para>
+                ID of the relation.
+</para>
+</listitem>
+</varlistentry>
+<varlistentry>
+<term>
+        String
+</term>
+<listitem>
+<para>
+                Namespace (empty string for <literal>pg_catalog</literal>).
+</para>
+</listitem>
+</varlistentry>
+<varlistentry>
+<term>
+        String
+</term>
+<listitem>
+<para>
+                Relation name.
+</para>
+</listitem>
+</varlistentry>
+
+<varlistentry>
+<term>
+        Int8
+</term>
+<listitem>
+<para>
+                Replica identity setting for the relation (same as
+                <structfield>relreplident</structfield> in <structname>pg_class</structname>).
+</para>
+</listitem>
+</varlistentry>
+
+<varlistentry>
+<term>
+        Int16
+</term>
+<listitem>
+<para>
+                Number of columns.
+</para>
+</listitem>
+</varlistentry>
+</variablelist>
+        Next, the following message part appears for each column:
+<variablelist>
+<varlistentry>
+<term>
+        Int8
+</term>
+<listitem>
+<para>
+                Flags for the column. Currently can be either 0 for no flags
+                or 1 which marks the column as part of the key.
+</para>
+</listitem>
+</varlistentry>
+<varlistentry>
+<term>
+        String
+</term>
+<listitem>
+<para>
+                Name of the column.
+</para>
+</listitem>
+</varlistentry>
+
+</variablelist>
+</para>
+</listitem>
+</varlistentry>
+
+<varlistentry>
+<term>
+Insert
+</term>
+<listitem>
+<para>
+
+<variablelist>
+<varlistentry>
+<term>
+        Byte1('I')
+</term>
+<listitem>
+<para>
+                Identifies the message as an insert message.
+</para>
+</listitem>
+</varlistentry>
+<varlistentry>
+<term>
+        Int32
+</term>
+<listitem>
+<para>
+                ID of the relation corresponding to the ID in the relation
+                message.
+</para>
+</listitem>
+</varlistentry>
+<varlistentry>
+<term>
+        Byte1('N')
+</term>
+<listitem>
+<para>
+                Identifies the following TupleData message as a new tuple.
+</para>
+</listitem>
+</varlistentry>
+
+<varlistentry>
+<term>
+        TupleData
+</term>
+<listitem>
+<para>
+                TupleData message part representing the contents of new tuple.
+</para>
+</listitem>
+</varlistentry>
+
+</variablelist>
+</para>
+</listitem>
+</varlistentry>
+
+<varlistentry>
+<term>
+Update
+</term>
+<listitem>
+<para>
+
+<variablelist>
+<varlistentry>
+<term>
+        Byte1('U')
+</term>
+<listitem>
+<para>
+                Identifies the message as an update message.
+</para>
+</listitem>
+</varlistentry>
+<varlistentry>
+<term>
+        Int32
+</term>
+<listitem>
+<para>
+                ID of the relation corresponding to the ID in the relation
+                message.
+</para>
+</listitem>
+</varlistentry>
+
+<varlistentry>
+<term>
+        Byte1('K')
+</term>
+<listitem>
+<para>
+                Identifies the following TupleData submessage as a key.
+                This field is optional and is only present if
+                the update changed data in any of the column(s) that are
+                part of the REPLICA IDENTITY index.
+</para>
+</listitem>
+</varlistentry>
+
+<varlistentry>
+<term>
+        Byte1('O')
+</term>
+<listitem>
+<para>
+                Identifies the following TupleData submessage as an old tuple.
+                This field is optional and is only present if table in which
+                the update happened has REPLICA IDENTITY set to FULL.
+</para>
+</listitem>
+</varlistentry>
+
+<varlistentry>
+<term>
+        TupleData
+</term>
+<listitem>
+<para>
+                TupleData message part representing the contents of the old tuple
+                or primary key. Only present if the previous 'O' or 'K' part
+                is present.
+</para>
+</listitem>
+</varlistentry>
+
+<varlistentry>
+<term>
+        Byte1('N')
+</term>
+<listitem>
+<para>
+                Identifies the following TupleData message as a new tuple.
+</para>
+</listitem>
+</varlistentry>
+
+<varlistentry>
+<term>
+        TupleData
+</term>
+<listitem>
+<para>
+                TupleData message part representing the contents of a new tuple.
+</para>
+</listitem>
+</varlistentry>
+
+</variablelist>
+</para>
+
+<para>
+    The Update message may contain either a 'K' message part or an 'O' message part
+    or neither of them, but never both of them.
+</para>
+
+</listitem>
+</varlistentry>
+
+<varlistentry>
+<term>
+Delete
+</term>
+<listitem>
+<para>
+
+<variablelist>
+<varlistentry>
+<term>
+        Byte1('D')
+</term>
+<listitem>
+<para>
+                Identifies the message as a delete message.
+</para>
+</listitem>
+</varlistentry>
+<varlistentry>
+<term>
+        Int32
+</term>
+<listitem>
+<para>
+                ID of the relation corresponding to the ID in the relation
+                message.
+</para>
+</listitem>
+</varlistentry>
+
+<varlistentry>
+<term>
+        Byte1('K')
+</term>
+<listitem>
+<para>
+                Identifies the following TupleData submessage as a key.
+                This field is present if the table in which the delete has
+                happened uses an index as REPLICA IDENTITY.
+</para>
+</listitem>
+</varlistentry>
+
+<varlistentry>
+<term>
+        Byte1('O')
+</term>
+<listitem>
+<para>
+                Identifies the following TupleData message as a old tuple.
+                This field is is present if the table in which the delete has
+                happened has REPLICA IDENTITY set to FULL.
+</para>
+</listitem>
+</varlistentry>
+
+<varlistentry>
+<term>
+        TupleData
+</term>
+<listitem>
+<para>
+                TupleData message part representing the contents of the old tuple
+                or primary key, depending on the previous field.
+</para>
+</listitem>
+</varlistentry>
+</variablelist>
+</para>
+
+<para>
+    The Delete message may contain either a 'K' message part or an 'O' message part,
+    but never both of them.
+</para>
+
+</listitem>
+</varlistentry>
+
+</variablelist>
+
+<para>
+
+Following message parts that are shared by above messages.
+
+</para>
+
+<variablelist>
+
+<varlistentry>
+<term>
+TupleData
+</term>
+<listitem>
+<para>
+
+<variablelist>
+<varlistentry>
+<term>
+        Int16
+</term>
+<listitem>
+<para>
+                Number of columns.
+</para>
+</listitem>
+</varlistentry>
+</variablelist>
+        Next, one of the following submessages appears for each column:
+<variablelist>
+<varlistentry>
+<term>
+        Byte1('n')
+</term>
+<listitem>
+<para>
+                Idenfifies the data as NULL value.
+</para>
+</listitem>
+</varlistentry>
+</variablelist>
+        Or
+<variablelist>
+<varlistentry>
+<term>
+        Byte1('u')
+</term>
+<listitem>
+<para>
+                Idenfifies unchanged TOASTed value (the actual value is not
+                sent).
+</para>
+</listitem>
+</varlistentry>
+</variablelist>
+        Or
+<variablelist>
+<varlistentry>
+<term>
+        Byte1('t')
+</term>
+<listitem>
+<para>
+                Idenfifies the data as text formatted value.
+</para>
+</listitem>
+</varlistentry>
+<varlistentry>
+<term>
+        Int32
+</term>
+<listitem>
+<para>
+                Length of the column value.
+</para>
+</listitem>
+</varlistentry>
+<varlistentry>
+<term>
+        String
+</term>
+<listitem>
+<para>
+                The text value.
+</para>
+</listitem>
+</varlistentry>
+
+</variablelist>
+</para>
+</listitem>
+</varlistentry>
+
+</variablelist>
+
+</sect1>
+
 <sect1 id="protocol-changes">
 <title>Summary of Changes since Protocol 2.0</title>
 
index 77667bdebd1e68028658a0274da3ad32e89b4def..0d09f81ccc75a732953742e2766d6a93fde9e0ea 100644 (file)
@@ -26,11 +26,13 @@ Complete list of usable sgml source files in this directory.
 <!ENTITY alterOperatorClass SYSTEM "alter_opclass.sgml">
 <!ENTITY alterOperatorFamily SYSTEM "alter_opfamily.sgml">
 <!ENTITY alterPolicy        SYSTEM "alter_policy.sgml">
+<!ENTITY alterPublication   SYSTEM "alter_publication.sgml">
 <!ENTITY alterRole          SYSTEM "alter_role.sgml">
 <!ENTITY alterRule          SYSTEM "alter_rule.sgml">
 <!ENTITY alterSchema        SYSTEM "alter_schema.sgml">
 <!ENTITY alterServer        SYSTEM "alter_server.sgml">
 <!ENTITY alterSequence      SYSTEM "alter_sequence.sgml">
+<!ENTITY alterSubscription  SYSTEM "alter_subscription.sgml">
 <!ENTITY alterSystem        SYSTEM "alter_system.sgml">
 <!ENTITY alterTable         SYSTEM "alter_table.sgml">
 <!ENTITY alterTableSpace    SYSTEM "alter_tablespace.sgml">
@@ -72,11 +74,13 @@ Complete list of usable sgml source files in this directory.
 <!ENTITY createOperatorClass SYSTEM "create_opclass.sgml">
 <!ENTITY createOperatorFamily SYSTEM "create_opfamily.sgml">
 <!ENTITY createPolicy       SYSTEM "create_policy.sgml">
+<!ENTITY createPublication  SYSTEM "create_publication.sgml">
 <!ENTITY createRole         SYSTEM "create_role.sgml">
 <!ENTITY createRule         SYSTEM "create_rule.sgml">
 <!ENTITY createSchema       SYSTEM "create_schema.sgml">
 <!ENTITY createSequence     SYSTEM "create_sequence.sgml">
 <!ENTITY createServer       SYSTEM "create_server.sgml">
+<!ENTITY createSubscription SYSTEM "create_subscription.sgml">
 <!ENTITY createTable        SYSTEM "create_table.sgml">
 <!ENTITY createTableAs      SYSTEM "create_table_as.sgml">
 <!ENTITY createTableSpace   SYSTEM "create_tablespace.sgml">
@@ -116,11 +120,13 @@ Complete list of usable sgml source files in this directory.
 <!ENTITY dropOperatorFamily  SYSTEM "drop_opfamily.sgml">
 <!ENTITY dropOwned          SYSTEM "drop_owned.sgml">
 <!ENTITY dropPolicy         SYSTEM "drop_policy.sgml">
+<!ENTITY dropPublication    SYSTEM "drop_publication.sgml">
 <!ENTITY dropRole           SYSTEM "drop_role.sgml">
 <!ENTITY dropRule           SYSTEM "drop_rule.sgml">
 <!ENTITY dropSchema         SYSTEM "drop_schema.sgml">
 <!ENTITY dropSequence       SYSTEM "drop_sequence.sgml">
 <!ENTITY dropServer         SYSTEM "drop_server.sgml">
+<!ENTITY dropSubscription   SYSTEM "drop_subscription.sgml">
 <!ENTITY dropTable          SYSTEM "drop_table.sgml">
 <!ENTITY dropTableSpace     SYSTEM "drop_tablespace.sgml">
 <!ENTITY dropTransform      SYSTEM "drop_transform.sgml">
diff --git a/doc/src/sgml/ref/alter_publication.sgml b/doc/src/sgml/ref/alter_publication.sgml
new file mode 100644 (file)
index 0000000..47d83b8
--- /dev/null
@@ -0,0 +1,139 @@
+<!--
+doc/src/sgml/ref/alter_publication.sgml
+PostgreSQL documentation
+-->
+
+<refentry id="SQL-ALTERPUBLICATION">
+ <indexterm zone="sql-alterpublication">
+  <primary>ALTER PUBLICATION</primary>
+ </indexterm>
+
+ <refmeta>
+  <refentrytitle>ALTER PUBLICATION</refentrytitle>
+  <manvolnum>7</manvolnum>
+  <refmiscinfo>SQL - Language Statements</refmiscinfo>
+ </refmeta>
+
+ <refnamediv>
+  <refname>ALTER PUBLICATION</refname>
+  <refpurpose>change the definition of a publication</refpurpose>
+ </refnamediv>
+
+ <refsynopsisdiv>
+<synopsis>
+ALTER PUBLICATION <replaceable class="PARAMETER">name</replaceable> WITH ( <replaceable class="PARAMETER">option</replaceable> [, ... ] )
+
+<phrase>where <replaceable class="PARAMETER">option</replaceable> can be:</phrase>
+
+      PUBLISH INSERT | NOPUBLISH INSERT
+    | PUBLISH UPDATE | NOPUBLISH UPDATE
+    | PUBLISH DELETE | NOPUBLISH DELETE
+
+ALTER PUBLICATION <replaceable class="PARAMETER">name</replaceable> OWNER TO { <replaceable>new_owner</replaceable> | CURRENT_USER | SESSION_USER }
+ALTER PUBLICATION <replaceable class="PARAMETER">name</replaceable> ADD TABLE <replaceable class="PARAMETER">table_name</replaceable> [, ...]
+ALTER PUBLICATION <replaceable class="PARAMETER">name</replaceable> SET TABLE <replaceable class="PARAMETER">table_name</replaceable> [, ...]
+ALTER PUBLICATION <replaceable class="PARAMETER">name</replaceable> DROP TABLE <replaceable class="PARAMETER">table_name</replaceable> [, ...]
+</synopsis>
+ </refsynopsisdiv>
+
+ <refsect1>
+  <title>Description</title>
+
+  <para>
+   The first variant of this command listed in the synopsis can change
+   all of the publication properties specified in
+   <xref linkend="sql-createpublication">.  Properties not mentioned in the
+   command retain their previous settings.  Database superusers can change any
+   of these settings for any role.
+  </para>
+
+  <para>
+   To alter the owner, you must also be a direct or indirect member of the
+   new owning role. The new owner has to be a superuser
+  </para>
+
+  <para>
+   The other variants of this command deal with the table membership of the
+   publication.  The <literal>SET TABLE</literal> clause will replace the
+   list of tables in the publication with the specified one.
+   The <literal>ADD TABLE</literal> and
+   <literal>DROP TABLE</literal> will add and remove one or more tables from
+   the publication.
+  </para>
+ </refsect1>
+
+ <refsect1>
+  <title>Parameters</title>
+
+  <variablelist>
+   <varlistentry>
+    <term><replaceable class="parameter">name</replaceable></term>
+    <listitem>
+     <para>
+      The name of an existing publication whose definition is to be altered.
+     </para>
+    </listitem>
+   </varlistentry>
+
+   <varlistentry>
+    <term><literal>PUBLISH INSERT</literal></term>
+    <term><literal>NOPUBLISH INSERT</literal></term>
+    <term><literal>PUBLISH UPDATE</literal></term>
+    <term><literal>NOPUBLISH UPDATE</literal></term>
+    <term><literal>PUBLISH DELETE</literal></term>
+    <term><literal>NOPUBLISH DELETE</literal></term>
+    <listitem>
+     <para>
+      These clauses alter properties originally set by
+      <xref linkend="SQL-CREATEPUBLICATION">.  See there for more information.
+     </para>
+    </listitem>
+   </varlistentry>
+
+   <varlistentry>
+    <term><replaceable class="parameter">table_name</replaceable></term>
+    <listitem>
+     <para>
+      Name of an existing table.
+     </para>
+    </listitem>
+   </varlistentry>
+  </variablelist>
+ </refsect1>
+
+ <refsect1>
+  <title>Examples</title>
+
+  <para>
+   Change the publication to not publish inserts:
+<programlisting>
+ALTER PUBLICATION noinsert WITH (NOPUBLISH INSERT);
+</programlisting>
+  </para>
+
+  <para>
+   Add some tables to the publication:
+<programlisting>
+ALTER PUBLICATION mypublication ADD TABLE users, departments;
+</programlisting>
+  </para>
+ </refsect1>
+
+ <refsect1>
+  <title>Compatibility</title>
+
+  <para>
+   <command>ALTER PUBLICATION</command> is a <productname>PostgreSQL</>
+   extension.
+  </para>
+ </refsect1>
+
+ <refsect1>
+  <title>See Also</title>
+
+  <simplelist type="inline">
+   <member><xref linkend="sql-createpublication"></member>
+   <member><xref linkend="sql-droppublication"></member>
+  </simplelist>
+ </refsect1>
+</refentry>
diff --git a/doc/src/sgml/ref/alter_subscription.sgml b/doc/src/sgml/ref/alter_subscription.sgml
new file mode 100644 (file)
index 0000000..032ecbb
--- /dev/null
@@ -0,0 +1,139 @@
+<!--
+doc/src/sgml/ref/alter_subscription.sgml
+PostgreSQL documentation
+-->
+
+<refentry id="SQL-ALTERSUBSCRIPTION">
+ <indexterm zone="sql-altersubscription">
+  <primary>ALTER SUBSCRIPTION</primary>
+ </indexterm>
+
+ <refmeta>
+  <refentrytitle>ALTER SUBSCRIPTION</refentrytitle>
+  <manvolnum>7</manvolnum>
+  <refmiscinfo>SQL - Language Statements</refmiscinfo>
+ </refmeta>
+
+ <refnamediv>
+  <refname>ALTER SUBSCRIPTION</refname>
+  <refpurpose>change the definition of a subscription</refpurpose>
+ </refnamediv>
+
+ <refsynopsisdiv>
+<synopsis>
+ALTER SUBSCRIPTION <replaceable class="PARAMETER">name</replaceable> WITH ( <replaceable class="PARAMETER">option</replaceable> [, ... ] ) ]
+
+<phrase>where <replaceable class="PARAMETER">option</replaceable> can be:</phrase>
+
+  SLOT NAME = slot_name
+
+ALTER SUBSCRIPTION <replaceable class="PARAMETER">name</replaceable> OWNER TO { <replaceable>new_owner</replaceable> | CURRENT_USER | SESSION_USER }
+ALTER SUBSCRIPTION <replaceable class="PARAMETER">name</replaceable> CONNECTION 'conninfo'
+ALTER SUBSCRIPTION <replaceable class="PARAMETER">name</replaceable> SET PUBLICATION publication_name [, ...]
+ALTER SUBSCRIPTION <replaceable class="PARAMETER">name</replaceable> ENABLE
+ALTER SUBSCRIPTION <replaceable class="PARAMETER">name</replaceable> DISABLE
+</synopsis>
+ </refsynopsisdiv>
+
+ <refsect1>
+  <title>Description</title>
+
+  <para>
+   <command>ALTER SUBSCRIPTION</command> can change most of the subscription
+   properties that can be specified
+   in <xref linkend="sql-createsubscription">.
+  </para>
+
+  <para>
+   To alter the owner, you must also be a direct or indirect member of the
+   new owning role. The new owner has to be a superuser
+  </para>
+ </refsect1>
+
+ <refsect1>
+  <title>Parameters</title>
+
+  <variablelist>
+   <varlistentry>
+    <term><replaceable class="parameter">name</replaceable></term>
+    <listitem>
+     <para>
+      The name of a subscription whose properties are to be altered.
+     </para>
+    </listitem>
+   </varlistentry>
+
+   <varlistentry>
+    <term><literal>CONNECTION '<replaceable class="parameter">conninfo</replaceable>'</literal></term>
+    <term><literal>SET PUBLICATION <replaceable class="parameter">publication_name</replaceable></literal></term>
+    <term><literal>SLOT NAME = <replaceable class="parameter">slot_name</replaceable></literal></term>
+    <listitem>
+     <para>
+      These clauses alter properties originally set by
+      <xref linkend="SQL-CREATESUBSCRIPTION">.  See there for more
+      information.
+     </para>
+    </listitem>
+   </varlistentry>
+
+   <varlistentry>
+    <term><literal>ENABLE</literal></term>
+    <listitem>
+     <para>
+      Enables the previously disabled subscription, starting the logical
+      replication worker at the end of transaction.
+     </para>
+    </listitem>
+   </varlistentry>
+
+   <varlistentry>
+    <term><literal>DISABLE</literal></term>
+    <listitem>
+     <para>
+      Disables the running subscription, stopping the logical replication
+      worker at the end of transaction.
+     </para>
+    </listitem>
+   </varlistentry>
+  </variablelist>
+ </refsect1>
+
+ <refsect1>
+  <title>Examples</title>
+
+  <para>
+   Change the publication subscribed by a subscription to
+   <literal>insert_only</literal>:
+<programlisting>
+ALTER SUBSCRIPTION mysub SET PUBLICATION insert_only;
+</programlisting>
+  </para>
+
+  <para>
+   Disable (stop) the subscription:
+<programlisting>
+ALTER SUBSCRIPTION mysub DISABLE;
+</programlisting>
+  </para>
+ </refsect1>
+
+ <refsect1>
+  <title>Compatibility</title>
+
+  <para>
+   <command>ALTER SUBSCRIPTION</command> is a <productname>PostgreSQL</>
+   extension.
+  </para>
+ </refsect1>
+
+ <refsect1>
+  <title>See Also</title>
+
+  <simplelist type="inline">
+   <member><xref linkend="sql-createsubscription"></member>
+   <member><xref linkend="sql-dropsubscription"></member>
+   <member><xref linkend="sql-createpublication"></member>
+   <member><xref linkend="sql-alterpublication"></member>
+  </simplelist>
+ </refsect1>
+</refentry>
diff --git a/doc/src/sgml/ref/create_publication.sgml b/doc/src/sgml/ref/create_publication.sgml
new file mode 100644 (file)
index 0000000..995f2bc
--- /dev/null
@@ -0,0 +1,206 @@
+<!--
+doc/src/sgml/ref/create_publication.sgml
+PostgreSQL documentation
+-->
+
+<refentry id="SQL-CREATEPUBLICATION">
+ <indexterm zone="sql-createpublication">
+  <primary>CREATE PUBLICATION</primary>
+ </indexterm>
+
+ <refmeta>
+  <refentrytitle>CREATE PUBLICATION</refentrytitle>
+  <manvolnum>7</manvolnum>
+  <refmiscinfo>SQL - Language Statements</refmiscinfo>
+ </refmeta>
+
+ <refnamediv>
+  <refname>CREATE PUBLICATION</refname>
+  <refpurpose>define a new publication</refpurpose>
+ </refnamediv>
+
+ <refsynopsisdiv>
+<synopsis>
+CREATE PUBLICATION <replaceable class="parameter">name</replaceable>
+    [ FOR TABLE <replaceable class="parameter">table_name</replaceable> [, ...]
+      | FOR ALL TABLES ]
+    [ WITH ( <replaceable class="parameter">option</replaceable> [, ... ] ) ]
+
+<phrase>where <replaceable class="parameter">option</replaceable> can be:</phrase>
+
+      PUBLISH INSERT | NOPUBLISH INSERT
+    | PUBLISH UPDATE | NOPUBLISH UPDATE
+    | PUBLISH DELETE | NOPUBLISH DELETE
+</synopsis>
+ </refsynopsisdiv>
+
+ <refsect1>
+  <title>Description</title>
+
+  <para>
+   <command>CREATE PUBLICATION</command> adds a new publication
+   into the current database.  The publication name must be distinct from
+   the name of any existing publication in the current database.
+  </para>
+
+  <para>
+   A publication is essentially a group of tables whose data changes are
+   intended to be replicated through logical replication.  See
+   <xref linkend="logical-replication-publication"> for details about how
+   publications fit into the logical replication setup.
+   </para>
+ </refsect1>
+
+ <refsect1>
+  <title>Parameters</title>
+
+  <variablelist>
+   <varlistentry>
+    <term><replaceable class="parameter">name</replaceable></term>
+    <listitem>
+     <para>
+      The name of the new publication.
+     </para>
+    </listitem>
+   </varlistentry>
+
+   <varlistentry>
+    <term><literal>FOR TABLE</literal></term>
+    <listitem>
+     <para>
+      Specifies a list of tables to add to the publication.
+     </para>
+    </listitem>
+   </varlistentry>
+
+   <varlistentry>
+    <term><literal>FOR ALL TABLES</literal></term>
+    <listitem>
+     <para>
+      Marks the publication as one that replicates changes for all tables in
+      the database, including tables created in the future.
+     </para>
+    </listitem>
+   </varlistentry>
+
+   <varlistentry>
+    <term><literal>PUBLISH INSERT</literal></term>
+    <term><literal>NOPUBLISH INSERT</literal></term>
+    <listitem>
+     <para>
+      These clauses determine whether the new publication will send
+      the <command>INSERT</command> operations to the subscribers.
+      <literal>PUBLISH INSERT</literal> is the default.
+     </para>
+    </listitem>
+   </varlistentry>
+
+   <varlistentry>
+    <term><literal>PUBLISH UPDATE</literal></term>
+    <term><literal>NOPUBLISH UPDATE</literal></term>
+    <listitem>
+     <para>
+      These clauses determine whether the new publication will send
+      the <command>UPDATE</command> operations to the subscribers.
+      <literal>PUBLISH UPDATE</literal> is the default.
+     </para>
+    </listitem>
+   </varlistentry>
+
+   <varlistentry>
+    <term><literal>PUBLISH DELETE</literal></term>
+    <term><literal>NOPUBLISH DELETE</literal></term>
+    <listitem>
+     <para>
+      These clauses determine whether the new publication will send
+      the <command>DELETE</command> operations to the subscribers.
+      <literal>PUBLISH DELETE</literal> is the default.
+     </para>
+    </listitem>
+   </varlistentry>
+
+  </variablelist>
+ </refsect1>
+
+ <refsect1>
+  <title>Notes</title>
+
+  <para>
+   If neither <literal>FOR TABLE</literal> nor <literal>FOR ALL
+   TABLES</literal> is specified, then the publication starts out with an
+   empty set of tables.  That is useful if tables are to be added later.
+  </para>
+
+  <para>
+   The creation of a publication does not start replication.  It only defines
+   a grouping and filtering logic for future subscribers.
+  </para>
+
+  <para>
+   To create a publication, the invoking user must have the
+   <literal>CREATE</> privilege for the current database.
+   (Of course, superusers bypass this check.)
+  </para>
+
+  <para>
+   To add a table to a publication, the invoking user must have
+   <command>SELECT</command> privilege on given table.  The
+   <command>FOR ALL TABLES</command> clause requires superuser.
+  </para>
+
+  <para>
+   The tables added to a publication that publishes <command>UPDATE</command>
+   and/or <command>DELETE</command> operations must have
+   <literal>REPLICA IDENTITY</> defined.  Otherwise those operations will be
+   disallowed on those tables.
+  </para>
+
+  <para>
+   For an <command>INSERT ... ON CONFLICT</> command, the publication will
+   publish the operation that actually results from the command.  So depending
+   of the outcome, it may be published as either <command>INSERT</command> or
+   <command>UPDATE</command>, or it may not be published at all.
+  </para>
+
+  <para>
+   <command>TRUNCATE</command> and other <acronym>DDL</acronym> operations
+   are not published.
+  </para>
+ </refsect1>
+
+ <refsect1>
+  <title>Examples</title>
+
+  <para>
+   Create a simple publication that just publishes all DML for tables in it:
+<programlisting>
+CREATE PUBLICATION mypublication;
+</programlisting>
+  </para>
+
+  <para>
+   Create an insert-only publication:
+<programlisting>
+CREATE PUBLICATION insert_only WITH (NOPUBLISH UPDATE, NOPUBLISH DELETE);
+</programlisting>
+  </para>
+ </refsect1>
+
+ <refsect1>
+  <title>Compatibility</title>
+
+  <para>
+   <command>CREATE PUBLICATION</command> is a <productname>PostgreSQL</>
+   extension.
+  </para>
+ </refsect1>
+
+ <refsect1>
+  <title>See Also</title>
+
+  <simplelist type="inline">
+   <member><xref linkend="sql-alterpublication"></member>
+   <member><xref linkend="sql-droppublication"></member>
+  </simplelist>
+ </refsect1>
+</refentry>
diff --git a/doc/src/sgml/ref/create_subscription.sgml b/doc/src/sgml/ref/create_subscription.sgml
new file mode 100644 (file)
index 0000000..40d08b3
--- /dev/null
@@ -0,0 +1,176 @@
+<!--
+doc/src/sgml/ref/create_subscription.sgml
+PostgreSQL documentation
+-->
+
+<refentry id="SQL-CREATESUBSCRIPTION">
+ <indexterm zone="sql-createsubscription">
+  <primary>CREATE SUBSCRIPTION</primary>
+ </indexterm>
+
+ <refmeta>
+  <refentrytitle>CREATE SUBSCRIPTION</refentrytitle>
+  <manvolnum>7</manvolnum>
+  <refmiscinfo>SQL - Language Statements</refmiscinfo>
+ </refmeta>
+
+ <refnamediv>
+  <refname>CREATE SUBSCRIPTION</refname>
+  <refpurpose>define a new subscription</refpurpose>
+ </refnamediv>
+
+ <refsynopsisdiv>
+<synopsis>
+CREATE SUBSCRIPTION <replaceable class="PARAMETER">subscription_name</replaceable> CONNECTION 'conninfo' PUBLICATION { publication_name [, ...] } [ WITH ( <replaceable class="PARAMETER">option</replaceable> [, ... ] ) ]
+
+<phrase>where <replaceable class="PARAMETER">option</replaceable> can be:</phrase>
+
+    | ENABLED | DISABLED
+    | CREATE SLOT | NOCREATE SLOT
+    | SLOT NAME = slot_name
+</synopsis>
+ </refsynopsisdiv>
+
+ <refsect1>
+  <title>Description</title>
+
+  <para>
+   <command>CREATE SUBSCRIPTION</command> adds a new subscription for a
+   current database.  The subscription name must be distinct from the name of
+   any existing subscription in the database.
+  </para>
+
+  <para>
+   The subscription represents a replication connection to the publisher.  As
+   such this command does not only add definitions in the local catalogs but
+   also creates a replication slot on the publisher.
+  </para>
+
+  <para>
+   A logical replication worker will be started to replicate data for the new
+   subscription at the commit of the transaction where this command is run.
+  </para>
+
+  <para>
+   Additional info about subscriptions and logical replication as a whole
+   can is available at <xref linkend="logical-replication-subscription"> and
+   <xref linkend="logical-replication">.
+  </para>
+
+ </refsect1>
+
+ <refsect1>
+  <title>Parameters</title>
+
+  <variablelist>
+   <varlistentry>
+    <term><replaceable class="parameter">subscription_name</replaceable></term>
+    <listitem>
+     <para>
+      The name of the new subscription.
+     </para>
+    </listitem>
+   </varlistentry>
+
+   <varlistentry>
+    <term><literal>CONNECTION '<replaceable class="parameter">conninfo</replaceable>'</literal></term>
+    <listitem>
+     <para>
+      The connection string to the publisher.
+     </para>
+    </listitem>
+   </varlistentry>
+
+   <varlistentry>
+    <term><literal>PUBLICATION <replaceable class="parameter">publication_name</replaceable></literal></term>
+    <listitem>
+     <para>
+      Name(s) of the publications on the publisher to subscribe to.
+     </para>
+    </listitem>
+   </varlistentry>
+
+   <varlistentry>
+    <term><literal>ENABLED</literal></term>
+    <term><literal>DISABLED</literal></term>
+    <listitem>
+     <para>
+      Specifies whether the subscription should be actively replicating or
+      if it should be just setup but not started yet.  Note that the
+      replication slot as described above is created in either case.
+      <literal>ENABLED</literal> is the default.
+     </para>
+    </listitem>
+   </varlistentry>
+
+   <varlistentry>
+    <term><literal>CREATE SLOT</literal></term>
+    <term><literal>NOCREATE SLOT</literal></term>
+    <listitem>
+     <para>
+      Specifies whether the command should create the replication slot on the
+      publisher. <literal>CREATE SLOT</literal> is the default.
+     </para>
+    </listitem>
+   </varlistentry>
+
+   <varlistentry>
+    <term><literal>SLOT NAME = <replaceable class="parameter">slot_name</replaceable></literal></term>
+    <listitem>
+     <para>
+      Name of the replication slot to use. The default behavior is to use
+      <literal>subscription_name</> for slot name.
+     </para>
+    </listitem>
+   </varlistentry>
+  </variablelist>
+ </refsect1>
+
+ <refsect1>
+  <title>Examples</title>
+
+  <para>
+   Create a subscription to a remote server that replicates tables in
+   the publications <literal>mypubclication</literal> and
+   <literal>insert_only</literal> and starts replicating immediately on
+   commit:
+<programlisting>
+CREATE SUBSCRIPTION mysub
+         CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb password=foopass'
+        PUBLICATION mypublication, insert_only;
+</programlisting>
+  </para>
+
+  <para>
+   Create a subscription to a remote server that replicates tables in
+   the <literal>insert_only</literal> publication and does not start replicating
+   until enabled at a later time.
+<programlisting>
+CREATE SUBSCRIPTION mysub
+         CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb password=foopass'
+        PUBLICATION insert_only
+               WITH (DISABLED);
+</programlisting>
+  </para>
+ </refsect1>
+
+ <refsect1>
+  <title>Compatibility</title>
+
+  <para>
+   <command>CREATE SUBSCRIPTION</command> is a <productname>PostgreSQL</>
+   extension.
+  </para>
+ </refsect1>
+
+ <refsect1>
+  <title>See Also</title>
+
+  <simplelist type="inline">
+   <member><xref linkend="sql-altersubscription"></member>
+   <member><xref linkend="sql-dropsubscription"></member>
+   <member><xref linkend="sql-createpublication"></member>
+   <member><xref linkend="sql-alterpublication"></member>
+  </simplelist>
+ </refsect1>
+</refentry>
diff --git a/doc/src/sgml/ref/drop_publication.sgml b/doc/src/sgml/ref/drop_publication.sgml
new file mode 100644 (file)
index 0000000..1a1be57
--- /dev/null
@@ -0,0 +1,107 @@
+<!--
+doc/src/sgml/ref/drop_publication.sgml
+PostgreSQL documentation
+-->
+
+<refentry id="SQL-DROPPUBLICATION">
+ <indexterm zone="sql-droppublication">
+  <primary>DROP PUBLICATION</primary>
+ </indexterm>
+
+ <refmeta>
+  <refentrytitle>DROP PUBLICATION</refentrytitle>
+  <manvolnum>7</manvolnum>
+  <refmiscinfo>SQL - Language Statements</refmiscinfo>
+ </refmeta>
+
+ <refnamediv>
+  <refname>DROP PUBLICATION</refname>
+  <refpurpose>remove a publication</refpurpose>
+ </refnamediv>
+
+ <refsynopsisdiv>
+<synopsis>
+DROP PUBLICATION [ IF EXISTS ] <replaceable class="PARAMETER">name</replaceable> [, ...] [ CASCADE | RESTRICT ]
+</synopsis>
+ </refsynopsisdiv>
+
+ <refsect1>
+  <title>Description</title>
+
+  <para>
+   <command>DROP PUBLICATION</command> removes an existing publication from
+   the database.
+  </para>
+
+  <para>
+   A publication can only be dropped by its owner or a superuser.
+  </para>
+ </refsect1>
+
+ <refsect1>
+  <title>Parameters</title>
+
+  <variablelist>
+   <varlistentry>
+    <term><literal>IF EXISTS</literal></term>
+    <listitem>
+     <para>
+      Do not throw an error if the extension does not exist. A notice is issued
+      in this case.
+     </para>
+    </listitem>
+   </varlistentry>
+
+   <varlistentry>
+    <term><replaceable class="parameter">name</replaceable></term>
+    <listitem>
+     <para>
+      The name of an existing publication.
+     </para>
+    </listitem>
+   </varlistentry>
+
+   <varlistentry>
+    <term><literal>CASCADE</literal></term>
+    <term><literal>RESTRICT</literal></term>
+
+    <listitem>
+     <para>
+      These key words do not have any effect, since there are no dependencies
+      on publications.
+     </para>
+    </listitem>
+   </varlistentry>
+  </variablelist>
+ </refsect1>
+
+ <refsect1>
+  <title>Examples</title>
+
+  <para>
+   Drop a publication:
+<programlisting>
+DROP PUBLICATION mypublication;
+</programlisting>
+  </para>
+
+ </refsect1>
+
+ <refsect1>
+  <title>Compatibility</title>
+
+  <para>
+   <command>DROP PUBLICATION</command> is a <productname>PostgreSQL</>
+   extension.
+  </para>
+ </refsect1>
+
+ <refsect1>
+  <title>See Also</title>
+
+  <simplelist type="inline">
+   <member><xref linkend="sql-createpublication"></member>
+   <member><xref linkend="sql-alterpublication"></member>
+  </simplelist>
+ </refsect1>
+</refentry>
diff --git a/doc/src/sgml/ref/drop_subscription.sgml b/doc/src/sgml/ref/drop_subscription.sgml
new file mode 100644 (file)
index 0000000..9f2fb93
--- /dev/null
@@ -0,0 +1,110 @@
+<!--
+doc/src/sgml/ref/drop_subscription.sgml
+PostgreSQL documentation
+-->
+
+<refentry id="SQL-DROPSUBSCRIPTION">
+ <indexterm zone="sql-dropsubscription">
+  <primary>DROP SUBSCRIPTION</primary>
+ </indexterm>
+
+ <refmeta>
+  <refentrytitle>DROP SUBSCRIPTION</refentrytitle>
+  <manvolnum>7</manvolnum>
+  <refmiscinfo>SQL - Language Statements</refmiscinfo>
+ </refmeta>
+
+ <refnamediv>
+  <refname>DROP SUBSCRIPTION</refname>
+  <refpurpose>remove a subscription</refpurpose>
+ </refnamediv>
+
+ <refsynopsisdiv>
+<synopsis>
+DROP SUBSCRIPTION [ IF EXISTS ] <replaceable class="parameter">name</replaceable> [ <replaceable class="parameter">DROP SLOT</replaceable> | <replaceable class="parameter">NODROP SLOT</replaceable> ]
+</synopsis>
+ </refsynopsisdiv>
+
+ <refsect1>
+  <title>Description</title>
+
+  <para>
+   <command>DROP SUBSCRIPTION</command> removes a subscription from the
+   database cluster.
+  </para>
+
+  <para>
+   A subscription can only be dropped by a superuser.
+  </para>
+
+  <para>
+   The replication worker associated with the subscription will not stop until
+   after the transaction that issued this command has committed.
+  </para>
+ </refsect1>
+
+ <refsect1>
+  <title>Parameters</title>
+
+  <variablelist>
+   <varlistentry>
+    <term><replaceable class="parameter">name</replaceable></term>
+    <listitem>
+     <para>
+      The name of a subscription to be dropped.
+     </para>
+    </listitem>
+   </varlistentry>
+
+   <varlistentry>
+    <term><replaceable class="parameter">DROP SLOT</replaceable></term>
+    <term><replaceable class="parameter">NODROP SLOT</replaceable></term>
+    <listitem>
+     <para>
+      Specifies whether to drop the replication slot on the publisher.  The
+      default is
+      <literal>DROP SLOT</literal>.
+     </para>
+
+     <para>
+      If the publisher is not reachable when the subscription is to be
+      dropped, then it is useful to specify <literal>NODROP SLOT</literal>.
+      But the replication slot on the publisher will then have to be removed
+      manually.
+     </para>
+    </listitem>
+   </varlistentry>
+
+  </variablelist>
+ </refsect1>
+
+ <refsect1>
+  <title>Examples</title>
+
+  <para>
+   Drop a subscription:
+<programlisting>
+DROP SUBSCRIPTION mysub;
+</programlisting>
+  </para>
+
+ </refsect1>
+
+ <refsect1>
+  <title>Compatibility</title>
+
+  <para>
+   <command>DROP SUBSCRIPTION</command> is a <productname>PostgreSQL</>
+   extension.
+  </para>
+ </refsect1>
+
+ <refsect1>
+  <title>See Also</title>
+
+  <simplelist type="inline">
+   <member><xref linkend="sql-createsubscription"></member>
+   <member><xref linkend="sql-altersubscription"></member>
+  </simplelist>
+ </refsect1>
+</refentry>
index b70e7d57e95725622ed7124eaddb89b705bf04c3..a1e03c481d1107891c4d80654fa697fb80920976 100644 (file)
@@ -755,6 +755,15 @@ PostgreSQL documentation
       </listitem>
      </varlistentry>
 
+     <varlistentry>
+      <term><option>--include-subscriptions</option></term>
+      <listitem>
+       <para>
+        Include logical replication subscriptions in the dump.
+       </para>
+      </listitem>
+     </varlistentry>
+
      <varlistentry>
       <term><option>--inserts</option></term>
       <listitem>
@@ -789,6 +798,18 @@ PostgreSQL documentation
       </listitem>
      </varlistentry>
 
+     <varlistentry>
+      <term><option>--no-create-subscription-slots</option></term>
+      <listitem>
+       <para>
+        When dumping logical replication subscriptions,
+        generate <command>CREATE SUBSCRIPTION</command> commands that do not
+        create the remote replication slot.  That way, the dump can be
+        restored without requiring network access to the remote servers.
+       </para>
+      </listitem>
+     </varlistentry>
+
      <varlistentry>
       <term><option>--no-security-labels</option></term>
       <listitem>
index 991573121bec6c68fe25be5e185eb2509239155d..640fe12bbf638dc76f996f5ab26ea3eeae9ad9ba 100644 (file)
@@ -1600,6 +1600,34 @@ testdb=&gt;
         </listitem>
       </varlistentry>
 
+      <varlistentry>
+        <term><literal>\dRp[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
+        <listitem>
+        <para>
+        Lists replication publications.
+        If <replaceable class="parameter">pattern</replaceable> is
+        specified, only those publications whose names match the pattern are
+        listed.
+        If <literal>+</literal> is appended to the command name, the tables
+        associated with each publication are shown as well.
+        </para>
+        </listitem>
+      </varlistentry>
+
+      <varlistentry>
+        <term><literal>\dRs[+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
+        <listitem>
+        <para>
+        Lists replication subscriptions.
+        If <replaceable class="parameter">pattern</replaceable> is
+        specified, only those subscriptions whose names match the pattern are
+        listed.
+        If <literal>+</literal> is appended to the command name, additional
+        properties of the subscriptions are shown.
+        </para>
+        </listitem>
+      </varlistentry>
+
       <varlistentry>
         <term><literal>\dT[S+] [ <link linkend="APP-PSQL-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
         <listitem>
index 8acdff1393fd2d1460f5e707c8ada4840bdf8620..34007d3508def2de4405460abcaa3211df16f1b1 100644 (file)
    &alterOperatorClass;
    &alterOperatorFamily;
    &alterPolicy;
+   &alterPublication;
    &alterRole;
    &alterRule;
    &alterSchema;
    &alterSequence;
    &alterServer;
+   &alterSubscription;
    &alterSystem;
    &alterTable;
    &alterTableSpace;
    &createOperatorClass;
    &createOperatorFamily;
    &createPolicy;
+   &createPublication;
    &createRole;
    &createRule;
    &createSchema;
    &createSequence;
    &createServer;
+   &createSubscription;
    &createTable;
    &createTableAs;
    &createTableSpace;
    &dropOperatorFamily;
    &dropOwned;
    &dropPolicy;
+   &dropPublication;
    &dropRole;
    &dropRule;
    &dropSchema;
    &dropSequence;
    &dropServer;
+   &dropSubscription;
    &dropTable;
    &dropTableSpace;
    &dropTSConfig;
index 977f80b469bd6553f9db0f3b3a4e254e687bcbbf..b490c071382958e8f390872b99b302fe91c08e0b 100644 (file)
@@ -22,6 +22,7 @@ SUBDIRS = \
        include \
        interfaces \
        backend/replication/libpqwalreceiver \
+       backend/replication/pgoutput \
        fe_utils \
        bin \
        pl \
index f5346f024efbe80e56c76f0d2b114c05439e31da..f6f136da3ab3fff5a296f2f41d8c23487fb68ab9 100644 (file)
@@ -42,6 +42,7 @@
 #include "miscadmin.h"
 #include "pgstat.h"
 #include "replication/logical.h"
+#include "replication/logicallauncher.h"
 #include "replication/origin.h"
 #include "replication/syncrep.h"
 #include "replication/walsender.h"
@@ -2135,6 +2136,7 @@ CommitTransaction(void)
        AtEOXact_HashTables(true);
        AtEOXact_PgStat(true);
        AtEOXact_Snapshot(true);
+       AtCommit_ApplyLauncher();
        pgstat_report_xact_timestamp(0);
 
        CurrentResourceOwner = NULL;
index cd38c8ab3ff444a7406583267f8643e7760d81a4..31368585d212c8472891b41edc8e45f632326b4f 100644 (file)
@@ -14,8 +14,9 @@ OBJS = catalog.o dependency.o heap.o index.o indexing.o namespace.o aclchk.o \
        objectaccess.o objectaddress.o partition.o pg_aggregate.o pg_collation.o \
        pg_constraint.o pg_conversion.o \
        pg_depend.o pg_enum.o pg_inherits.o pg_largeobject.o pg_namespace.o \
-       pg_operator.o pg_proc.o pg_range.o pg_db_role_setting.o pg_shdepend.o \
-       pg_type.o storage.o toasting.o
+       pg_operator.o pg_proc.o pg_publication.o pg_range.o \
+          pg_db_role_setting.o pg_shdepend.o pg_subscription.o pg_type.o \
+          storage.o toasting.o
 
 BKIFILES = postgres.bki postgres.description postgres.shdescription
 
@@ -42,7 +43,7 @@ POSTGRES_BKI_SRCS = $(addprefix $(top_srcdir)/src/include/catalog/,\
        pg_foreign_table.h pg_policy.h pg_replication_origin.h \
        pg_default_acl.h pg_init_privs.h pg_seclabel.h pg_shseclabel.h \
        pg_collation.h pg_partitioned_table.h pg_range.h pg_transform.h \
-       pg_sequence.h \
+       pg_sequence.h pg_publication.h pg_publication_rel.h pg_subscription.h \
        toasting.h indexing.h \
     )
 
index 640632784c1f5044f6dd384c8631f6fb23423f6c..a96bf692dfdde8e6d5c7d77c9ac61d469fe22345 100644 (file)
@@ -45,6 +45,7 @@
 #include "catalog/pg_operator.h"
 #include "catalog/pg_opfamily.h"
 #include "catalog/pg_proc.h"
+#include "catalog/pg_subscription.h"
 #include "catalog/pg_tablespace.h"
 #include "catalog/pg_type.h"
 #include "catalog/pg_ts_config.h"
@@ -3390,6 +3391,10 @@ static const char *const not_owner_msg[MAX_ACL_KIND] =
        gettext_noop("must be owner of event trigger %s"),
        /* ACL_KIND_EXTENSION */
        gettext_noop("must be owner of extension %s"),
+       /* ACL_KIND_PUBLICATION */
+       gettext_noop("must be owner of publication %s"),
+       /* ACL_KIND_SUBSCRIPTION */
+       gettext_noop("must be owner of subscription %s"),
 };
 
 
@@ -5071,6 +5076,58 @@ pg_extension_ownercheck(Oid ext_oid, Oid roleid)
        return has_privs_of_role(roleid, ownerId);
 }
 
+/*
+ * Ownership check for an publication (specified by OID).
+ */
+bool
+pg_publication_ownercheck(Oid pub_oid, Oid roleid)
+{
+       HeapTuple       tuple;
+       Oid                     ownerId;
+
+       /* Superusers bypass all permission checking. */
+       if (superuser_arg(roleid))
+               return true;
+
+       tuple = SearchSysCache1(PUBLICATIONOID, ObjectIdGetDatum(pub_oid));
+       if (!HeapTupleIsValid(tuple))
+               ereport(ERROR,
+                               (errcode(ERRCODE_UNDEFINED_OBJECT),
+                                errmsg("publication with OID %u does not exist", pub_oid)));
+
+       ownerId = ((Form_pg_publication) GETSTRUCT(tuple))->pubowner;
+
+       ReleaseSysCache(tuple);
+
+       return has_privs_of_role(roleid, ownerId);
+}
+
+/*
+ * Ownership check for an subscription (specified by OID).
+ */
+bool
+pg_subscription_ownercheck(Oid sub_oid, Oid roleid)
+{
+       HeapTuple       tuple;
+       Oid                     ownerId;
+
+       /* Superusers bypass all permission checking. */
+       if (superuser_arg(roleid))
+               return true;
+
+       tuple = SearchSysCache1(SUBSCRIPTIONOID, ObjectIdGetDatum(sub_oid));
+       if (!HeapTupleIsValid(tuple))
+               ereport(ERROR,
+                               (errcode(ERRCODE_UNDEFINED_OBJECT),
+                                errmsg("subscription with OID %u does not exist", sub_oid)));
+
+       ownerId = ((Form_pg_subscription) GETSTRUCT(tuple))->subowner;
+
+       ReleaseSysCache(tuple);
+
+       return has_privs_of_role(roleid, ownerId);
+}
+
 /*
  * Check whether specified role has CREATEROLE privilege (or is a superuser)
  *
index c3b4d298ce29c25ea3dd100b1d3466db2ca28537..11ee536726ce08bf22b87bc8a087e2f73869e821 100644 (file)
@@ -36,6 +36,7 @@
 #include "catalog/pg_shdepend.h"
 #include "catalog/pg_shdescription.h"
 #include "catalog/pg_shseclabel.h"
+#include "catalog/pg_subscription.h"
 #include "catalog/pg_tablespace.h"
 #include "catalog/toasting.h"
 #include "miscadmin.h"
@@ -227,7 +228,8 @@ IsSharedRelation(Oid relationId)
                relationId == SharedSecLabelRelationId ||
                relationId == TableSpaceRelationId ||
                relationId == DbRoleSettingRelationId ||
-               relationId == ReplicationOriginRelationId)
+               relationId == ReplicationOriginRelationId ||
+               relationId == SubscriptionRelationId)
                return true;
        /* These are their indexes (see indexing.h) */
        if (relationId == AuthIdRolnameIndexId ||
@@ -245,7 +247,9 @@ IsSharedRelation(Oid relationId)
                relationId == TablespaceNameIndexId ||
                relationId == DbRoleSettingDatidRolidIndexId ||
                relationId == ReplicationOriginIdentIndex ||
-               relationId == ReplicationOriginNameIndex)
+               relationId == ReplicationOriginNameIndex ||
+               relationId == SubscriptionObjectIndexId ||
+               relationId == SubscriptionNameIndexId)
                return true;
        /* These are their toast tables and toast indexes (see toasting.h) */
        if (relationId == PgShdescriptionToastTable ||
index 359719e45047ac5ebcffa19aac47ec8fa4d0c7c0..1c43af6effb91aba6a64e3c5ad2f8af91b3bdf82 100644 (file)
 #include "catalog/pg_opfamily.h"
 #include "catalog/pg_policy.h"
 #include "catalog/pg_proc.h"
+#include "catalog/pg_publication.h"
+#include "catalog/pg_publication_rel.h"
 #include "catalog/pg_rewrite.h"
+#include "catalog/pg_subscription.h"
 #include "catalog/pg_tablespace.h"
 #include "catalog/pg_transform.h"
 #include "catalog/pg_trigger.h"
@@ -64,6 +67,7 @@
 #include "commands/extension.h"
 #include "commands/policy.h"
 #include "commands/proclang.h"
+#include "commands/publicationcmds.h"
 #include "commands/schemacmds.h"
 #include "commands/seclabel.h"
 #include "commands/sequence.h"
@@ -164,6 +168,9 @@ static const Oid object_classes[] = {
        ExtensionRelationId,            /* OCLASS_EXTENSION */
        EventTriggerRelationId,         /* OCLASS_EVENT_TRIGGER */
        PolicyRelationId,                       /* OCLASS_POLICY */
+       PublicationRelationId,          /* OCLASS_PUBLICATION */
+       PublicationRelRelationId,       /* OCLASS_PUBLICATION_REL */
+       SubscriptionRelationId,         /* OCLASS_SUBSCRIPTION */
        TransformRelationId                     /* OCLASS_TRANSFORM */
 };
 
@@ -1244,6 +1251,14 @@ doDeletion(const ObjectAddress *object, int flags)
                        RemovePolicyById(object->objectId);
                        break;
 
+               case OCLASS_PUBLICATION:
+                       RemovePublicationById(object->objectId);
+                       break;
+
+               case OCLASS_PUBLICATION_REL:
+                       RemovePublicationRelById(object->objectId);
+                       break;
+
                case OCLASS_TRANSFORM:
                        DropTransformById(object->objectId);
                        break;
@@ -2404,6 +2419,15 @@ getObjectClass(const ObjectAddress *object)
                case PolicyRelationId:
                        return OCLASS_POLICY;
 
+               case PublicationRelationId:
+                       return OCLASS_PUBLICATION;
+
+               case PublicationRelRelationId:
+                       return OCLASS_PUBLICATION_REL;
+
+               case SubscriptionRelationId:
+                       return OCLASS_SUBSCRIPTION;
+
                case TransformRelationId:
                        return OCLASS_TRANSFORM;
        }
index 2b1808b0f92651ca5f3165e830cd35e542d0b7f5..44d14ae2b18a97e79b4ef38bdb53cfb48c0ff297 100644 (file)
 #include "catalog/pg_operator.h"
 #include "catalog/pg_proc.h"
 #include "catalog/pg_policy.h"
+#include "catalog/pg_publication.h"
+#include "catalog/pg_publication_rel.h"
 #include "catalog/pg_rewrite.h"
+#include "catalog/pg_subscription.h"
 #include "catalog/pg_tablespace.h"
 #include "catalog/pg_transform.h"
 #include "catalog/pg_trigger.h"
@@ -450,6 +453,30 @@ static const ObjectPropertyType ObjectProperty[] =
                Anum_pg_type_typacl,
                ACL_KIND_TYPE,
                true
+       },
+       {
+               PublicationRelationId,
+               PublicationObjectIndexId,
+               PUBLICATIONOID,
+               PUBLICATIONNAME,
+               Anum_pg_publication_pubname,
+               InvalidAttrNumber,
+               Anum_pg_publication_pubowner,
+               InvalidAttrNumber,
+               -1,
+               true
+       },
+       {
+               SubscriptionRelationId,
+               SubscriptionObjectIndexId,
+               SUBSCRIPTIONOID,
+               SUBSCRIPTIONNAME,
+               Anum_pg_subscription_subname,
+               InvalidAttrNumber,
+               Anum_pg_subscription_subowner,
+               InvalidAttrNumber,
+               -1,
+               true
        }
 };
 
@@ -653,6 +680,18 @@ static const struct object_type_map
        {
                "policy", OBJECT_POLICY
        },
+       /* OCLASS_PUBLICATION */
+       {
+               "publication", OBJECT_PUBLICATION
+       },
+       /* OCLASS_PUBLICATION_REL */
+       {
+               "publication relation", OBJECT_PUBLICATION_REL
+       },
+       /* OCLASS_SUBSCRIPTION */
+       {
+               "subscription", OBJECT_SUBSCRIPTION
+       },
        /* OCLASS_TRANSFORM */
        {
                "transform", OBJECT_TRANSFORM
@@ -688,6 +727,9 @@ static ObjectAddress get_object_address_opf_member(ObjectType objtype,
 
 static ObjectAddress get_object_address_usermapping(List *objname,
                                                           List *objargs, bool missing_ok);
+static ObjectAddress get_object_address_publication_rel(List *objname,
+                                                                  List *objargs, Relation *relation,
+                                                                  bool missing_ok);
 static ObjectAddress get_object_address_defacl(List *objname, List *objargs,
                                                  bool missing_ok);
 static const ObjectPropertyType *get_object_property_data(Oid class_id);
@@ -812,6 +854,8 @@ get_object_address(ObjectType objtype, List *objname, List *objargs,
                        case OBJECT_FOREIGN_SERVER:
                        case OBJECT_EVENT_TRIGGER:
                        case OBJECT_ACCESS_METHOD:
+                       case OBJECT_PUBLICATION:
+                       case OBJECT_SUBSCRIPTION:
                                address = get_object_address_unqualified(objtype,
                                                                                                                 objname, missing_ok);
                                break;
@@ -926,6 +970,10 @@ get_object_address(ObjectType objtype, List *objname, List *objargs,
                                address = get_object_address_usermapping(objname, objargs,
                                                                                                                 missing_ok);
                                break;
+                       case OBJECT_PUBLICATION_REL:
+                               address = get_object_address_publication_rel(objname, objargs,
+                                                                                                                        &relation,
+                                                                                                                        missing_ok);
                        case OBJECT_DEFACL:
                                address = get_object_address_defacl(objname, objargs,
                                                                                                        missing_ok);
@@ -1091,6 +1139,12 @@ get_object_address_unqualified(ObjectType objtype,
                        case OBJECT_EVENT_TRIGGER:
                                msg = gettext_noop("event trigger name cannot be qualified");
                                break;
+                       case OBJECT_PUBLICATION:
+                               msg = gettext_noop("publication name cannot be qualified");
+                               break;
+                       case OBJECT_SUBSCRIPTION:
+                               msg = gettext_noop("subscription name cannot be qualified");
+                               break;
                        default:
                                elog(ERROR, "unrecognized objtype: %d", (int) objtype);
                                msg = NULL;             /* placate compiler */
@@ -1156,6 +1210,16 @@ get_object_address_unqualified(ObjectType objtype,
                        address.objectId = get_event_trigger_oid(name, missing_ok);
                        address.objectSubId = 0;
                        break;
+               case OBJECT_PUBLICATION:
+                       address.classId = PublicationRelationId;
+                       address.objectId = get_publication_oid(name, missing_ok);
+                       address.objectSubId = 0;
+                       break;
+               case OBJECT_SUBSCRIPTION:
+                       address.classId = SubscriptionRelationId;
+                       address.objectId = get_subscription_oid(name, missing_ok);
+                       address.objectSubId = 0;
+                       break;
                default:
                        elog(ERROR, "unrecognized objtype: %d", (int) objtype);
                        /* placate compiler, which doesn't know elog won't return */
@@ -1743,6 +1807,51 @@ get_object_address_usermapping(List *objname, List *objargs, bool missing_ok)
        return address;
 }
 
+/*
+ * Find the ObjectAddress for a publication relation.  The objname parameter
+ * is the relation name; objargs contains the publication name.
+ */
+static ObjectAddress
+get_object_address_publication_rel(List *objname, List *objargs,
+                                                                  Relation *relation, bool missing_ok)
+{
+       ObjectAddress address;
+       char       *pubname;
+       Publication *pub;
+
+       ObjectAddressSet(address, PublicationRelRelationId, InvalidOid);
+
+       *relation = relation_openrv_extended(makeRangeVarFromNameList(objname),
+                                                                                AccessShareLock, missing_ok);
+       if (!relation)
+               return address;
+
+       /* fetch publication name from input list */
+       pubname = strVal(linitial(objargs));
+
+       /* Now look up the pg_publication tuple */
+       pub = GetPublicationByName(pubname, missing_ok);
+       if (!pub)
+               return address;
+
+       /* Find the publication relation mapping in syscache. */
+       address.objectId =
+               GetSysCacheOid2(PUBLICATIONRELMAP,
+                                               ObjectIdGetDatum(RelationGetRelid(*relation)),
+                                               ObjectIdGetDatum(pub->oid));
+       if (!OidIsValid(address.objectId))
+       {
+               if (!missing_ok)
+                       ereport(ERROR,
+                                       (errcode(ERRCODE_UNDEFINED_OBJECT),
+                                        errmsg("publication relation \"%s\" in publication \"%s\" does not exist",
+                                                       RelationGetRelationName(*relation), pubname)));
+               return address;
+       }
+
+       return address;
+}
+
 /*
  * Find the ObjectAddress for a default ACL.
  */
@@ -2002,6 +2111,7 @@ pg_get_object_address(PG_FUNCTION_ARGS)
                case OBJECT_DOMCONSTRAINT:
                case OBJECT_CAST:
                case OBJECT_USER_MAPPING:
+               case OBJECT_PUBLICATION_REL:
                case OBJECT_DEFACL:
                case OBJECT_TRANSFORM:
                        if (list_length(args) != 1)
@@ -2183,6 +2293,16 @@ check_object_ownership(Oid roleid, ObjectType objtype, ObjectAddress address,
                                                                        format_type_be(targettypeid))));
                        }
                        break;
+               case OBJECT_PUBLICATION:
+                       if (!pg_publication_ownercheck(address.objectId, roleid))
+                               aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PUBLICATION,
+                                                          NameListToString(objname));
+                       break;
+               case OBJECT_SUBSCRIPTION:
+                       if (!pg_subscription_ownercheck(address.objectId, roleid))
+                               aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_SUBSCRIPTION,
+                                                          NameListToString(objname));
+                       break;
                case OBJECT_TRANSFORM:
                        {
                                TypeName   *typename = (TypeName *) linitial(objname);
@@ -3191,6 +3311,41 @@ getObjectDescription(const ObjectAddress *object)
                                break;
                        }
 
+               case OCLASS_PUBLICATION:
+                       {
+                               appendStringInfo(&buffer, _("publication %s"),
+                                                                get_publication_name(object->objectId));
+                               break;
+                       }
+
+               case OCLASS_PUBLICATION_REL:
+                       {
+                               HeapTuple       tup;
+                               char       *pubname;
+                               Form_pg_publication_rel prform;
+
+                               tup = SearchSysCache1(PUBLICATIONREL,
+                                                                         ObjectIdGetDatum(object->objectId));
+                               if (!HeapTupleIsValid(tup))
+                                       elog(ERROR, "cache lookup failed for publication table %u",
+                                                object->objectId);
+
+                               prform = (Form_pg_publication_rel) GETSTRUCT(tup);
+                               pubname = get_publication_name(prform->prpubid);
+
+                               appendStringInfo(&buffer, _("publication table %s in publication %s"),
+                                                                get_rel_name(prform->prrelid), pubname);
+                               ReleaseSysCache(tup);
+                               break;
+                       }
+
+               case OCLASS_SUBSCRIPTION:
+                       {
+                               appendStringInfo(&buffer, _("subscription %s"),
+                                                                get_subscription_name(object->objectId));
+                               break;
+                       }
+
                default:
                        appendStringInfo(&buffer, "unrecognized object %u %u %d",
                                                         object->classId,
@@ -3677,6 +3832,18 @@ getObjectTypeDescription(const ObjectAddress *object)
                        appendStringInfoString(&buffer, "access method");
                        break;
 
+               case OCLASS_PUBLICATION:
+                       appendStringInfoString(&buffer, "publication");
+                       break;
+
+               case OCLASS_PUBLICATION_REL:
+                       appendStringInfoString(&buffer, "publication table");
+                       break;
+
+               case OCLASS_SUBSCRIPTION:
+                       appendStringInfoString(&buffer, "subscription");
+                       break;
+
                default:
                        appendStringInfo(&buffer, "unrecognized %u", object->classId);
                        break;
@@ -4648,6 +4815,58 @@ getObjectIdentityParts(const ObjectAddress *object,
                        }
                        break;
 
+               case OCLASS_PUBLICATION:
+                       {
+                               char       *pubname;
+
+                               pubname = get_publication_name(object->objectId);
+                               appendStringInfoString(&buffer,
+                                                                          quote_identifier(pubname));
+                               if (objname)
+                                       *objname = list_make1(pubname);
+                               break;
+                       }
+
+               case OCLASS_PUBLICATION_REL:
+                       {
+                               HeapTuple       tup;
+                               char       *pubname;
+                               Form_pg_publication_rel prform;
+
+                               tup = SearchSysCache1(PUBLICATIONREL,
+                                                                         ObjectIdGetDatum(object->objectId));
+                               if (!HeapTupleIsValid(tup))
+                                       elog(ERROR, "cache lookup failed for publication table %u",
+                                                object->objectId);
+
+                               prform = (Form_pg_publication_rel) GETSTRUCT(tup);
+                               pubname = get_publication_name(prform->prpubid);
+
+                               appendStringInfo(&buffer, _("publication table %s in publication %s"),
+                                                                get_rel_name(prform->prrelid), pubname);
+
+                               if (objname)
+                               {
+                                       getRelationIdentity(&buffer, prform->prrelid, objname);
+                                       *objargs = list_make1(pubname);
+                               }
+
+                               ReleaseSysCache(tup);
+                               break;
+                       }
+
+               case OCLASS_SUBSCRIPTION:
+                       {
+                               char       *subname;
+
+                               subname = get_subscription_name(object->objectId);
+                               appendStringInfoString(&buffer,
+                                                                          quote_identifier(subname));
+                               if (objname)
+                                       *objname = list_make1(subname);
+                               break;
+                       }
+
                default:
                        appendStringInfo(&buffer, "unrecognized object %u %u %d",
                                                         object->classId,
diff --git a/src/backend/catalog/pg_publication.c b/src/backend/catalog/pg_publication.c
new file mode 100644 (file)
index 0000000..576b7fa
--- /dev/null
@@ -0,0 +1,457 @@
+/*-------------------------------------------------------------------------
+ *
+ * pg_publication.c
+ *             publication C API manipulation
+ *
+ * Copyright (c) 2016, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ *             pg_publication.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+#include "funcapi.h"
+#include "miscadmin.h"
+
+#include "access/genam.h"
+#include "access/hash.h"
+#include "access/heapam.h"
+#include "access/htup_details.h"
+#include "access/xact.h"
+
+#include "catalog/catalog.h"
+#include "catalog/dependency.h"
+#include "catalog/index.h"
+#include "catalog/indexing.h"
+#include "catalog/namespace.h"
+#include "catalog/objectaccess.h"
+#include "catalog/objectaddress.h"
+#include "catalog/pg_type.h"
+#include "catalog/pg_publication.h"
+#include "catalog/pg_publication_rel.h"
+
+#include "utils/array.h"
+#include "utils/builtins.h"
+#include "utils/catcache.h"
+#include "utils/fmgroids.h"
+#include "utils/inval.h"
+#include "utils/lsyscache.h"
+#include "utils/rel.h"
+#include "utils/syscache.h"
+
+/*
+ * Check if relation can be in given publication and throws appropriate
+ * error if not.
+ */
+static void
+check_publication_add_relation(Relation targetrel)
+{
+       /* Must be table */
+       if (RelationGetForm(targetrel)->relkind != RELKIND_RELATION)
+               ereport(ERROR,
+                               (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                                errmsg("\"%s\" is not a table",
+                                               RelationGetRelationName(targetrel)),
+                                errdetail("Only tables can be added to publications.")));
+
+       /* Can't be system table */
+       if (IsCatalogRelation(targetrel))
+               ereport(ERROR,
+                               (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                                errmsg("\"%s\" is a system table",
+                                               RelationGetRelationName(targetrel)),
+                                errdetail("System tables cannot be added to publications.")));
+
+       /* UNLOGGED and TEMP relations cannot be part of publication. */
+       if (!RelationNeedsWAL(targetrel))
+               ereport(ERROR,
+                               (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                                errmsg("table \"%s\" cannot be replicated",
+                                               RelationGetRelationName(targetrel)),
+                                errdetail("Temporary and unlogged relations cannot be replicated.")));
+}
+
+/*
+ * Returns if relation represented by oid and Form_pg_class entry
+ * is publishable.
+ *
+ * Does same checks as the above, but does not need relation to be opened
+ * and also does not throw errors.
+ */
+static bool
+is_publishable_class(Oid relid, Form_pg_class reltuple)
+{
+       return reltuple->relkind == RELKIND_RELATION &&
+               !IsCatalogClass(relid, reltuple) &&
+               reltuple->relpersistence == RELPERSISTENCE_PERMANENT &&
+               /*
+                * Also exclude any tables created as part of initdb. This mainly
+                * affects the preinstalled information_schema.
+                * Note that IsCatalogClass() only checks for these inside pg_catalog
+                * and toast schemas.
+                */
+               relid >= FirstNormalObjectId;
+}
+
+/*
+ * Insert new publication / relation mapping.
+ */
+ObjectAddress
+publication_add_relation(Oid pubid, Relation targetrel,
+                                                bool if_not_exists)
+{
+       Relation        rel;
+       HeapTuple       tup;
+       Datum           values[Natts_pg_publication_rel];
+       bool            nulls[Natts_pg_publication_rel];
+       Oid                     relid = RelationGetRelid(targetrel);
+       Oid                     prrelid;
+       Publication *pub = GetPublication(pubid);
+       ObjectAddress   myself,
+                                       referenced;
+
+       rel = heap_open(PublicationRelRelationId, RowExclusiveLock);
+
+       /*
+        * Check for duplicates. Note that this does not really prevent
+        * duplicates, it's here just to provide nicer error message in common
+        * case. The real protection is the unique key on the catalog.
+        */
+       if (SearchSysCacheExists2(PUBLICATIONRELMAP, ObjectIdGetDatum(relid),
+                                                         ObjectIdGetDatum(pubid)))
+       {
+               heap_close(rel, RowExclusiveLock);
+
+               if (if_not_exists)
+                       return InvalidObjectAddress;
+
+               ereport(ERROR,
+                               (errcode(ERRCODE_DUPLICATE_OBJECT),
+                                errmsg("relation \"%s\" is already member of publication \"%s\"",
+                                               RelationGetRelationName(targetrel), pub->name)));
+       }
+
+       check_publication_add_relation(targetrel);
+
+       /* Form a tuple. */
+       memset(values, 0, sizeof(values));
+       memset(nulls, false, sizeof(nulls));
+
+       values[Anum_pg_publication_rel_prpubid - 1] =
+               ObjectIdGetDatum(pubid);
+       values[Anum_pg_publication_rel_prrelid - 1] =
+               ObjectIdGetDatum(relid);
+
+       tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
+
+       /* Insert tuple into catalog. */
+       prrelid = simple_heap_insert(rel, tup);
+       CatalogUpdateIndexes(rel, tup);
+       heap_freetuple(tup);
+
+       ObjectAddressSet(myself, PublicationRelRelationId, prrelid);
+
+       /* Add dependency on the publication */
+       ObjectAddressSet(referenced, PublicationRelationId, pubid);
+       recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
+
+       /* Add dependency on the relation */
+       ObjectAddressSet(referenced, RelationRelationId, relid);
+       recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
+
+       /* Close the table. */
+       heap_close(rel, RowExclusiveLock);
+
+       /* Invalidate relcache so that publication info is rebuilt. */
+       CacheInvalidateRelcache(targetrel);
+
+       return myself;
+}
+
+
+/*
+ * Gets list of publication oids for a relation oid.
+ */
+List *
+GetRelationPublications(Oid relid)
+{
+       List               *result = NIL;
+       CatCList           *pubrellist;
+       int                             i;
+
+       /* Find all publications associated with the relation. */
+       pubrellist = SearchSysCacheList1(PUBLICATIONRELMAP,
+                                                                        ObjectIdGetDatum(relid));
+       for (i = 0; i < pubrellist->n_members; i++)
+       {
+               HeapTuple       tup = &pubrellist->members[i]->tuple;
+               Oid                     pubid = ((Form_pg_publication_rel) GETSTRUCT(tup))->prpubid;
+
+               result = lappend_oid(result, pubid);
+       }
+
+       ReleaseSysCacheList(pubrellist);
+
+       return result;
+}
+
+/*
+ * Gets list of relation oids for a publication.
+ *
+ * This should only be used for normal publications, the FOR ALL TABLES
+ * should use GetAllTablesPublicationRelations().
+ */
+List *
+GetPublicationRelations(Oid pubid)
+{
+       List               *result;
+       Relation                pubrelsrel;
+       ScanKeyData             scankey;
+       SysScanDesc             scan;
+       HeapTuple               tup;
+
+       /* Find all publications associated with the relation. */
+       pubrelsrel = heap_open(PublicationRelRelationId, AccessShareLock);
+
+       ScanKeyInit(&scankey,
+                               Anum_pg_publication_rel_prpubid,
+                               BTEqualStrategyNumber, F_OIDEQ,
+                               ObjectIdGetDatum(pubid));
+
+       scan = systable_beginscan(pubrelsrel, PublicationRelMapIndexId, true,
+                                                         NULL, 1, &scankey);
+
+       result = NIL;
+       while (HeapTupleIsValid(tup = systable_getnext(scan)))
+       {
+               Form_pg_publication_rel         pubrel;
+
+               pubrel = (Form_pg_publication_rel) GETSTRUCT(tup);
+
+               result = lappend_oid(result, pubrel->prrelid);
+       }
+
+       systable_endscan(scan);
+       heap_close(pubrelsrel, AccessShareLock);
+
+       return result;
+}
+
+/*
+ * Gets list of publication oids for publications marked as FOR ALL TABLES.
+ */
+List *
+GetAllTablesPublications(void)
+{
+       List               *result;
+       Relation                rel;
+       ScanKeyData             scankey;
+       SysScanDesc             scan;
+       HeapTuple               tup;
+
+       /* Find all publications that are marked as for all tables. */
+       rel = heap_open(PublicationRelationId, AccessShareLock);
+
+       ScanKeyInit(&scankey,
+                               Anum_pg_publication_puballtables,
+                               BTEqualStrategyNumber, F_BOOLEQ,
+                               BoolGetDatum(true));
+
+       scan = systable_beginscan(rel, InvalidOid, false,
+                                                         NULL, 1, &scankey);
+
+       result = NIL;
+       while (HeapTupleIsValid(tup = systable_getnext(scan)))
+               result = lappend_oid(result, HeapTupleGetOid(tup));
+
+       systable_endscan(scan);
+       heap_close(rel, AccessShareLock);
+
+       return result;
+}
+
+/*
+ * Gets list of all relation published by FOR ALL TABLES publication(s).
+ */
+List *
+GetAllTablesPublicationRelations(void)
+{
+       Relation        classRel;
+       ScanKeyData key[1];
+       HeapScanDesc scan;
+       HeapTuple       tuple;
+       List       *result = NIL;
+
+       classRel = heap_open(RelationRelationId, AccessShareLock);
+
+       ScanKeyInit(&key[0],
+                               Anum_pg_class_relkind,
+                               BTEqualStrategyNumber, F_CHAREQ,
+                               CharGetDatum(RELKIND_RELATION));
+
+       scan = heap_beginscan_catalog(classRel, 1, key);
+
+       while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
+       {
+               Oid                             relid = HeapTupleGetOid(tuple);
+               Form_pg_class   relForm = (Form_pg_class) GETSTRUCT(tuple);
+
+               if (is_publishable_class(relid, relForm))
+                       result = lappend_oid(result, relid);
+       }
+
+       heap_endscan(scan);
+       heap_close(classRel, AccessShareLock);
+
+       return result;
+}
+
+/*
+ * Get publication using oid
+ *
+ * The Publication struct and it's data are palloced here.
+ */
+Publication *
+GetPublication(Oid pubid)
+{
+       HeapTuple               tup;
+       Publication        *pub;
+       Form_pg_publication     pubform;
+
+       tup = SearchSysCache1(PUBLICATIONOID, ObjectIdGetDatum(pubid));
+
+       if (!HeapTupleIsValid(tup))
+               elog(ERROR, "cache lookup failed for publication %u", pubid);
+
+       pubform = (Form_pg_publication) GETSTRUCT(tup);
+
+       pub = (Publication *) palloc(sizeof(Publication));
+       pub->oid = pubid;
+       pub->name = pstrdup(NameStr(pubform->pubname));
+       pub->alltables = pubform->puballtables;
+       pub->pubactions.pubinsert = pubform->pubinsert;
+       pub->pubactions.pubupdate = pubform->pubupdate;
+       pub->pubactions.pubdelete = pubform->pubdelete;
+
+       ReleaseSysCache(tup);
+
+       return pub;
+}
+
+
+/*
+ * Get Publication using name.
+ */
+Publication *
+GetPublicationByName(const char *pubname, bool missing_ok)
+{
+       Oid                     oid;
+
+       oid = GetSysCacheOid1(PUBLICATIONNAME, CStringGetDatum(pubname));
+       if (!OidIsValid(oid))
+       {
+               if (missing_ok)
+                       return NULL;
+
+               ereport(ERROR,
+                               (errcode(ERRCODE_UNDEFINED_OBJECT),
+                                errmsg("publication \"%s\" does not exist", pubname)));
+       }
+
+       return GetPublication(oid);
+}
+
+/*
+ * get_publication_oid - given a publication name, look up the OID
+ *
+ * If missing_ok is false, throw an error if name not found.  If true, just
+ * return InvalidOid.
+ */
+Oid
+get_publication_oid(const char *pubname, bool missing_ok)
+{
+       Oid                     oid;
+
+       oid = GetSysCacheOid1(PUBLICATIONNAME, CStringGetDatum(pubname));
+       if (!OidIsValid(oid) && !missing_ok)
+               ereport(ERROR,
+                               (errcode(ERRCODE_UNDEFINED_OBJECT),
+                                errmsg("publication \"%s\" does not exist", pubname)));
+       return oid;
+}
+
+/*
+ * get_publication_name - given a publication Oid, look up the name
+ */
+char *
+get_publication_name(Oid pubid)
+{
+       HeapTuple               tup;
+       char               *pubname;
+       Form_pg_publication     pubform;
+
+       tup = SearchSysCache1(PUBLICATIONOID, ObjectIdGetDatum(pubid));
+
+       if (!HeapTupleIsValid(tup))
+               elog(ERROR, "cache lookup failed for publication %u", pubid);
+
+       pubform = (Form_pg_publication) GETSTRUCT(tup);
+       pubname = pstrdup(NameStr(pubform->pubname));
+
+       ReleaseSysCache(tup);
+
+       return pubname;
+}
+
+/*
+ * Returns Oids of tables in a publication.
+ */
+Datum
+pg_get_publication_tables(PG_FUNCTION_ARGS)
+{
+       FuncCallContext *funcctx;
+       char               *pubname = text_to_cstring(PG_GETARG_TEXT_PP(0));
+       Publication        *publication;
+       List               *tables;
+       ListCell          **lcp;
+
+       /* stuff done only on the first call of the function */
+       if (SRF_IS_FIRSTCALL())
+       {
+               MemoryContext oldcontext;
+
+               /* create a function context for cross-call persistence */
+               funcctx = SRF_FIRSTCALL_INIT();
+
+               /* switch to memory context appropriate for multiple function calls */
+               oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
+
+               publication = GetPublicationByName(pubname, false);
+               if (publication->alltables)
+                       tables = GetAllTablesPublicationRelations();
+               else
+                       tables = GetPublicationRelations(publication->oid);
+               lcp = (ListCell **) palloc(sizeof(ListCell *));
+               *lcp = list_head(tables);
+               funcctx->user_fctx = (void *) lcp;
+
+               MemoryContextSwitchTo(oldcontext);
+       }
+
+       /* stuff done on every call of the function */
+       funcctx = SRF_PERCALL_SETUP();
+       lcp = (ListCell **) funcctx->user_fctx;
+
+       while (*lcp != NULL)
+       {
+               Oid             relid = lfirst_oid(*lcp);
+
+               *lcp = lnext(*lcp);
+               SRF_RETURN_NEXT(funcctx, ObjectIdGetDatum(relid));
+       }
+
+       SRF_RETURN_DONE(funcctx);
+}
index fb39a01841a06497a900c1846c1d710354211d66..60ed957655e8e86d9c7ba9d69e8d9faf056ddbf1 100644 (file)
@@ -39,6 +39,7 @@
 #include "catalog/pg_opfamily.h"
 #include "catalog/pg_proc.h"
 #include "catalog/pg_shdepend.h"
+#include "catalog/pg_subscription.h"
 #include "catalog/pg_tablespace.h"
 #include "catalog/pg_ts_config.h"
 #include "catalog/pg_ts_dict.h"
@@ -53,7 +54,9 @@
 #include "commands/extension.h"
 #include "commands/policy.h"
 #include "commands/proclang.h"
+#include "commands/publicationcmds.h"
 #include "commands/schemacmds.h"
+#include "commands/subscriptioncmds.h"
 #include "commands/tablecmds.h"
 #include "commands/typecmds.h"
 #include "storage/lmgr.h"
@@ -1406,6 +1409,14 @@ shdepReassignOwned(List *roleids, Oid newrole)
                                        AlterEventTriggerOwner_oid(sdepForm->objid, newrole);
                                        break;
 
+                               case PublicationRelationId:
+                                       AlterPublicationOwner_oid(sdepForm->objid, newrole);
+                                       break;
+
+                               case SubscriptionRelationId:
+                                       AlterSubscriptionOwner_oid(sdepForm->objid, newrole);
+                                       break;
+
                                        /* Generic alter owner cases */
                                case CollationRelationId:
                                case ConversionRelationId:
diff --git a/src/backend/catalog/pg_subscription.c b/src/backend/catalog/pg_subscription.c
new file mode 100644 (file)
index 0000000..c358ef6
--- /dev/null
@@ -0,0 +1,207 @@
+/*-------------------------------------------------------------------------
+ *
+ * pg_subscription.c
+ *             replication subscriptions
+ *
+ * Copyright (c) 2016, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ *             src/backend/catalog/pg_subscription.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+#include "miscadmin.h"
+
+#include "access/genam.h"
+#include "access/heapam.h"
+#include "access/htup_details.h"
+
+#include "catalog/pg_type.h"
+#include "catalog/pg_subscription.h"
+
+#include "nodes/makefuncs.h"
+
+#include "utils/array.h"
+#include "utils/builtins.h"
+#include "utils/fmgroids.h"
+#include "utils/syscache.h"
+
+
+static List *textarray_to_stringlist(ArrayType *textarray);
+
+/*
+ * Fetch the subscription from the syscache.
+ */
+Subscription *
+GetSubscription(Oid subid, bool missing_ok)
+{
+       HeapTuple               tup;
+       Subscription   *sub;
+       Form_pg_subscription    subform;
+       Datum                   datum;
+       bool                    isnull;
+
+       tup = SearchSysCache1(SUBSCRIPTIONOID, ObjectIdGetDatum(subid));
+
+       if (!HeapTupleIsValid(tup))
+       {
+               if (missing_ok)
+                       return NULL;
+
+               elog(ERROR, "cache lookup failed for subscription %u", subid);
+       }
+
+       subform = (Form_pg_subscription) GETSTRUCT(tup);
+
+       sub = (Subscription *) palloc(sizeof(Subscription));
+       sub->oid = subid;
+       sub->dbid = subform->subdbid;
+       sub->name = pstrdup(NameStr(subform->subname));
+       sub->owner = subform->subowner;
+       sub->enabled = subform->subenabled;
+
+       /* Get conninfo */
+       datum = SysCacheGetAttr(SUBSCRIPTIONOID,
+                                                       tup,
+                                                       Anum_pg_subscription_subconninfo,
+                                                       &isnull);
+       Assert(!isnull);
+       sub->conninfo = pstrdup(TextDatumGetCString(datum));
+
+       /* Get slotname */
+       datum = SysCacheGetAttr(SUBSCRIPTIONOID,
+                                                       tup,
+                                                       Anum_pg_subscription_subslotname,
+                                                       &isnull);
+       Assert(!isnull);
+       sub->slotname = pstrdup(NameStr(*DatumGetName(datum)));
+
+       /* Get publications */
+       datum = SysCacheGetAttr(SUBSCRIPTIONOID,
+                                                       tup,
+                                                       Anum_pg_subscription_subpublications,
+                                                       &isnull);
+       Assert(!isnull);
+       sub->publications = textarray_to_stringlist(DatumGetArrayTypeP(datum));
+
+       ReleaseSysCache(tup);
+
+       return sub;
+}
+
+/*
+ * Return number of subscriptions defined in given database.
+ * Used by dropdb() to check if database can indeed be dropped.
+ */
+int
+CountDBSubscriptions(Oid dbid)
+{
+       int                             nsubs = 0;
+       Relation                rel;
+       ScanKeyData             scankey;
+       SysScanDesc             scan;
+       HeapTuple               tup;
+
+       rel = heap_open(SubscriptionRelationId, RowExclusiveLock);
+
+       ScanKeyInit(&scankey,
+                               Anum_pg_subscription_subdbid,
+                               BTEqualStrategyNumber, F_OIDEQ,
+                               ObjectIdGetDatum(dbid));
+
+       scan = systable_beginscan(rel, InvalidOid, false,
+                                                         NULL, 1, &scankey);
+
+       while (HeapTupleIsValid(tup = systable_getnext(scan)))
+               nsubs++;
+
+       systable_endscan(scan);
+
+       heap_close(rel, NoLock);
+
+       return nsubs;
+}
+
+/*
+ * Free memory allocated by subscription struct.
+ */
+void
+FreeSubscription(Subscription *sub)
+{
+       pfree(sub->name);
+       pfree(sub->conninfo);
+       pfree(sub->slotname);
+       list_free_deep(sub->publications);
+       pfree(sub);
+}
+
+/*
+ * get_subscription_oid - given a subscription name, look up the OID
+ *
+ * If missing_ok is false, throw an error if name not found.  If true, just
+ * return InvalidOid.
+ */
+Oid
+get_subscription_oid(const char *subname, bool missing_ok)
+{
+       Oid                     oid;
+
+       oid = GetSysCacheOid2(SUBSCRIPTIONNAME, MyDatabaseId,
+                                                 CStringGetDatum(subname));
+       if (!OidIsValid(oid) && !missing_ok)
+               ereport(ERROR,
+                               (errcode(ERRCODE_UNDEFINED_OBJECT),
+                                errmsg("subscription \"%s\" does not exist", subname)));
+       return oid;
+}
+
+/*
+ * get_subscription_name - given a subscription OID, look up the name
+ */
+char *
+get_subscription_name(Oid subid)
+{
+       HeapTuple               tup;
+       char               *subname;
+       Form_pg_subscription subform;
+
+       tup = SearchSysCache1(SUBSCRIPTIONOID, ObjectIdGetDatum(subid));
+
+       if (!HeapTupleIsValid(tup))
+               elog(ERROR, "cache lookup failed for subscription %u", subid);
+
+       subform = (Form_pg_subscription) GETSTRUCT(tup);
+       subname = pstrdup(NameStr(subform->subname));
+
+       ReleaseSysCache(tup);
+
+       return subname;
+}
+
+/*
+ * Convert text array to list of strings.
+ *
+ * Note: the resulting list of strings is pallocated here.
+ */
+static List *
+textarray_to_stringlist(ArrayType *textarray)
+{
+       Datum              *elems;
+       int                             nelems, i;
+       List               *res = NIL;
+
+       deconstruct_array(textarray,
+                                         TEXTOID, -1, false, 'i',
+                                         &elems, NULL, &nelems);
+
+       if (nelems == 0)
+               return NIL;
+
+       for (i = 0; i < nelems; i++)
+               res = lappend(res, makeString(pstrdup(TextDatumGetCString(elems[i]))));
+
+       return res;
+}
index 07f291b7cdb4f43af2966e54663e05f3a3d7f4cb..4dfedf89b611dff4a6a58600100fbdf38f48174e 100644 (file)
@@ -248,6 +248,15 @@ CREATE VIEW pg_stats WITH (security_barrier) AS
 
 REVOKE ALL on pg_statistic FROM public;
 
+CREATE VIEW pg_publication_tables AS
+    SELECT
+        P.pubname AS pubname,
+        N.nspname AS schemaname,
+        C.relname AS tablename
+    FROM pg_publication P, pg_class C
+         JOIN pg_namespace N ON (N.oid = C.relnamespace)
+    WHERE C.oid IN (SELECT relid FROM pg_get_publication_tables(P.pubname));
+
 CREATE VIEW pg_locks AS
     SELECT * FROM pg_lock_status() AS L;
 
@@ -708,6 +717,20 @@ CREATE VIEW pg_stat_wal_receiver AS
     FROM pg_stat_get_wal_receiver() s
     WHERE s.pid IS NOT NULL;
 
+CREATE VIEW pg_stat_subscription AS
+    SELECT
+            su.oid AS subid,
+            su.subname,
+            st.pid,
+            st.received_lsn,
+            st.last_msg_send_time,
+            st.last_msg_receipt_time,
+            st.latest_end_lsn,
+            st.latest_end_time
+    FROM pg_subscription su
+            LEFT JOIN pg_stat_get_subscription(NULL) st
+                      ON (st.subid = su.oid);
+
 CREATE VIEW pg_stat_ssl AS
     SELECT
             S.pid,
@@ -866,6 +889,8 @@ CREATE VIEW pg_replication_origin_status AS
 
 REVOKE ALL ON pg_replication_origin_status FROM public;
 
+REVOKE ALL ON pg_subscription FROM public;
+
 --
 -- We have a few function definitions in here, too.
 -- At some point there might be enough to justify breaking them out into
index 6b3742c0a0847196b7696ee5cdb9698cd611ed86..e0fab38cbe19af855403be312d5495165d9f0533 100644 (file)
@@ -17,9 +17,9 @@ OBJS = amcmds.o aggregatecmds.o alter.o analyze.o async.o cluster.o comment.o \
        dbcommands.o define.o discard.o dropcmds.o \
        event_trigger.o explain.o extension.o foreigncmds.o functioncmds.o \
        indexcmds.o lockcmds.o matview.o operatorcmds.o opclasscmds.o \
-       policy.o portalcmds.o prepare.o proclang.o \
-       schemacmds.o seclabel.o sequence.o tablecmds.o tablespace.o trigger.o \
-       tsearchcmds.o typecmds.o user.o vacuum.o vacuumlazy.o \
-       variable.o view.o
+       policy.o portalcmds.o prepare.o proclang.o publicationcmds.o \
+       schemacmds.o seclabel.o sequence.o subscriptioncmds.o tablecmds.o \
+       tablespace.o trigger.o tsearchcmds.o typecmds.o user.o vacuum.o \
+       vacuumlazy.o variable.o view.o
 
 include $(top_srcdir)/src/backend/common.mk
index 8b6f42090969bdec199b5a161bd728ae831232ef..768fcc82ddce8949ca7728f390d19f16becdd9a1 100644 (file)
@@ -45,7 +45,9 @@
 #include "commands/extension.h"
 #include "commands/policy.h"
 #include "commands/proclang.h"
+#include "commands/publicationcmds.h"
 #include "commands/schemacmds.h"
+#include "commands/subscriptioncmds.h"
 #include "commands/tablecmds.h"
 #include "commands/tablespace.h"
 #include "commands/trigger.h"
@@ -770,6 +772,14 @@ ExecAlterOwnerStmt(AlterOwnerStmt *stmt)
                        return AlterEventTriggerOwner(strVal(linitial(stmt->object)),
                                                                                  newowner);
 
+               case OBJECT_PUBLICATION:
+                       return AlterPublicationOwner(strVal(linitial(stmt->object)),
+                                                                                newowner);
+
+               case OBJECT_SUBSCRIPTION:
+                       return AlterSubscriptionOwner(strVal(linitial(stmt->object)),
+                                                                                 newowner);
+
                        /* Generic cases */
                case OBJECT_AGGREGATE:
                case OBJECT_COLLATION:
index 2833f3e846965d1c2c80df83304ab9e54ea72a01..6ad8fd77b109bb0c7450945dd4c442b8bccc7543 100644 (file)
@@ -37,6 +37,7 @@
 #include "catalog/pg_authid.h"
 #include "catalog/pg_database.h"
 #include "catalog/pg_db_role_setting.h"
+#include "catalog/pg_subscription.h"
 #include "catalog/pg_tablespace.h"
 #include "commands/comment.h"
 #include "commands/dbcommands.h"
@@ -790,6 +791,7 @@ dropdb(const char *dbname, bool missing_ok)
        int                     npreparedxacts;
        int                     nslots,
                                nslots_active;
+       int                     nsubscriptions;
 
        /*
         * Look up the target database's OID, and get exclusive lock on it. We
@@ -874,6 +876,21 @@ dropdb(const char *dbname, bool missing_ok)
                                                dbname),
                                 errdetail_busy_db(notherbackends, npreparedxacts)));
 
+       /*
+        * Check if there are subscriptions defined in the target database.
+        *
+        * We can't drop them automatically because they might be holding
+        * resources in other databases/instances.
+        */
+       if ((nsubscriptions = CountDBSubscriptions(db_id)) > 0)
+               ereport(ERROR,
+                               (errcode(ERRCODE_OBJECT_IN_USE),
+                                errmsg("database \"%s\" is being used by logical replication subscription",
+                                               dbname),
+                                errdetail_plural("There is %d subscription.",
+                                                                 "There are %d subscriptions.",
+                                                                 nsubscriptions, nsubscriptions)));
+
        /*
         * Remove the database's tuple from pg_database.
         */
index 714b5252c738d0cc682a1ca1324e4b40cefeb488..8da924517b9a69fd43ec06297e6d1829e1ea04c3 100644 (file)
@@ -319,3 +319,31 @@ defGetTypeLength(DefElem *def)
                                        def->defname, defGetString(def))));
        return 0;                                       /* keep compiler quiet */
 }
+
+/*
+ * Extract a list of string values (otherwise uninterpreted) from a DefElem.
+ */
+List *
+defGetStringList(DefElem *def)
+{
+       ListCell   *cell;
+
+       if (def->arg == NULL)
+               ereport(ERROR,
+                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                errmsg("%s requires a parameter",
+                                               def->defname)));
+       if (nodeTag(def->arg) != T_List)
+               elog(ERROR, "unrecognized node type: %d", (int) nodeTag(def->arg));
+
+       foreach(cell, (List *)def->arg)
+       {
+               Node       *str = (Node *) lfirst(cell);
+
+               if (!IsA(str, String))
+                       elog(ERROR, "unexpected node type in name list: %d",
+                                (int) nodeTag(str));
+       }
+
+       return (List *) def->arg;
+}
index 96436c06897dc147172882c649cf6dad63509c53..8cfbcf43f799cfd9ca6be362f4288b764bc7185a 100644 (file)
@@ -441,6 +441,10 @@ does_not_exist_skipping(ObjectType objtype, List *objname, List *objargs)
                                }
                        }
                        break;
+               case OBJECT_PUBLICATION:
+                       msg = gettext_noop("publication \"%s\" does not exist, skipping");
+                       name = NameListToString(objname);
+                       break;
                default:
                        elog(ERROR, "unrecognized object type: %d", (int) objtype);
                        break;
index c0061e195eb34f668add57293c952d835eb1f01e..812553736151211bd2db56d6964d237bb7238e01 100644 (file)
@@ -106,11 +106,13 @@ static event_trigger_support_data event_trigger_support[] = {
        {"OPERATOR CLASS", true},
        {"OPERATOR FAMILY", true},
        {"POLICY", true},
+       {"PUBLICATION", true},
        {"ROLE", false},
        {"RULE", true},
        {"SCHEMA", true},
        {"SEQUENCE", true},
        {"SERVER", true},
+       {"SUBSCRIPTION", true},
        {"TABLE", true},
        {"TABLESPACE", false},
        {"TRANSFORM", true},
@@ -1103,9 +1105,12 @@ EventTriggerSupportsObjectType(ObjectType obtype)
                case OBJECT_OPERATOR:
                case OBJECT_OPFAMILY:
                case OBJECT_POLICY:
+               case OBJECT_PUBLICATION:
+               case OBJECT_PUBLICATION_REL:
                case OBJECT_RULE:
                case OBJECT_SCHEMA:
                case OBJECT_SEQUENCE:
+               case OBJECT_SUBSCRIPTION:
                case OBJECT_TABCONSTRAINT:
                case OBJECT_TABLE:
                case OBJECT_TRANSFORM:
@@ -1168,6 +1173,9 @@ EventTriggerSupportsObjectClass(ObjectClass objclass)
                case OCLASS_EXTENSION:
                case OCLASS_POLICY:
                case OCLASS_AM:
+               case OCLASS_PUBLICATION:
+               case OCLASS_PUBLICATION_REL:
+               case OCLASS_SUBSCRIPTION:
                        return true;
        }
 
diff --git a/src/backend/commands/publicationcmds.c b/src/backend/commands/publicationcmds.c
new file mode 100644 (file)
index 0000000..21e523d
--- /dev/null
@@ -0,0 +1,754 @@
+/*-------------------------------------------------------------------------
+ *
+ * publicationcmds.c
+ *             publication manipulation
+ *
+ * Copyright (c) 2016, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ *             publicationcmds.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+#include "funcapi.h"
+#include "miscadmin.h"
+
+#include "access/genam.h"
+#include "access/hash.h"
+#include "access/heapam.h"
+#include "access/htup_details.h"
+#include "access/xact.h"
+
+#include "catalog/catalog.h"
+#include "catalog/indexing.h"
+#include "catalog/namespace.h"
+#include "catalog/objectaccess.h"
+#include "catalog/objectaddress.h"
+#include "catalog/pg_inherits_fn.h"
+#include "catalog/pg_type.h"
+#include "catalog/pg_publication.h"
+#include "catalog/pg_publication_rel.h"
+
+#include "commands/dbcommands.h"
+#include "commands/defrem.h"
+#include "commands/event_trigger.h"
+#include "commands/publicationcmds.h"
+
+#include "utils/array.h"
+#include "utils/builtins.h"
+#include "utils/catcache.h"
+#include "utils/fmgroids.h"
+#include "utils/inval.h"
+#include "utils/lsyscache.h"
+#include "utils/rel.h"
+#include "utils/syscache.h"
+
+/* Same as MAXNUMMESSAGES in sinvaladt.c */
+#define MAX_RELCACHE_INVAL_MSGS 4096
+
+static List *OpenTableList(List *tables);
+static void CloseTableList(List *rels);
+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
+parse_publication_options(List *options,
+                                                 bool *publish_insert_given,
+                                                 bool *publish_insert,
+                                                 bool *publish_update_given,
+                                                 bool *publish_update,
+                                                 bool *publish_delete_given,
+                                                 bool *publish_delete)
+{
+       ListCell   *lc;
+
+       *publish_insert_given = false;
+       *publish_update_given = false;
+       *publish_delete_given = false;
+
+       /* Defaults are true */
+       *publish_insert = true;
+       *publish_update = true;
+       *publish_delete = true;
+
+       /* Parse options */
+       foreach (lc, options)
+       {
+               DefElem    *defel = (DefElem *) lfirst(lc);
+
+               if (strcmp(defel->defname, "publish insert") == 0)
+               {
+                       if (*publish_insert_given)
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                errmsg("conflicting or redundant options")));
+
+                       *publish_insert_given = true;
+                       *publish_insert = defGetBoolean(defel);
+               }
+               else if (strcmp(defel->defname, "nopublish insert") == 0)
+               {
+                       if (*publish_insert_given)
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                errmsg("conflicting or redundant options")));
+
+                       *publish_insert_given = true;
+                       *publish_insert = !defGetBoolean(defel);
+               }
+               else if (strcmp(defel->defname, "publish update") == 0)
+               {
+                       if (*publish_update_given)
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                errmsg("conflicting or redundant options")));
+
+                       *publish_update_given = true;
+                       *publish_update = defGetBoolean(defel);
+               }
+               else if (strcmp(defel->defname, "nopublish update") == 0)
+               {
+                       if (*publish_update_given)
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                errmsg("conflicting or redundant options")));
+
+                       *publish_update_given = true;
+                       *publish_update = !defGetBoolean(defel);
+               }
+               else if (strcmp(defel->defname, "publish delete") == 0)
+               {
+                       if (*publish_delete_given)
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                errmsg("conflicting or redundant options")));
+
+                       *publish_delete_given = true;
+                       *publish_delete = defGetBoolean(defel);
+               }
+               else if (strcmp(defel->defname, "nopublish delete") == 0)
+               {
+                       if (*publish_delete_given)
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                errmsg("conflicting or redundant options")));
+
+                       *publish_delete_given = true;
+                       *publish_delete = !defGetBoolean(defel);
+               }
+               else
+                       elog(ERROR, "unrecognized option: %s", defel->defname);
+       }
+}
+
+/*
+ * Create new publication.
+ */
+ObjectAddress
+CreatePublication(CreatePublicationStmt *stmt)
+{
+       Relation        rel;
+       ObjectAddress myself;
+       Oid                     puboid;
+       bool            nulls[Natts_pg_publication];
+       Datum           values[Natts_pg_publication];
+       HeapTuple       tup;
+       bool            publish_insert_given;
+       bool            publish_update_given;
+       bool            publish_delete_given;
+       bool            publish_insert;
+       bool            publish_update;
+       bool            publish_delete;
+       AclResult       aclresult;
+
+       /* must have CREATE privilege on database */
+       aclresult = pg_database_aclcheck(MyDatabaseId, GetUserId(), ACL_CREATE);
+       if (aclresult != ACLCHECK_OK)
+               aclcheck_error(aclresult, ACL_KIND_DATABASE,
+                                          get_database_name(MyDatabaseId));
+
+       /* FOR ALL TABLES requires superuser */
+       if (stmt->for_all_tables && !superuser())
+               ereport(ERROR,
+                               (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+                                (errmsg("must be superuser to create FOR ALL TABLES publication"))));
+
+       rel = heap_open(PublicationRelationId, RowExclusiveLock);
+
+       /* Check if name is used */
+       puboid = GetSysCacheOid1(PUBLICATIONNAME, CStringGetDatum(stmt->pubname));
+       if (OidIsValid(puboid))
+       {
+               ereport(ERROR,
+                               (errcode(ERRCODE_DUPLICATE_OBJECT),
+                                errmsg("publication \"%s\" already exists",
+                                               stmt->pubname)));
+       }
+
+       /* Form a tuple. */
+       memset(values, 0, sizeof(values));
+       memset(nulls, false, sizeof(nulls));
+
+       values[Anum_pg_publication_pubname - 1] =
+               DirectFunctionCall1(namein, CStringGetDatum(stmt->pubname));
+       values[Anum_pg_publication_pubowner - 1] = ObjectIdGetDatum(GetUserId());
+
+       parse_publication_options(stmt->options,
+                                                         &publish_insert_given, &publish_insert,
+                                                         &publish_update_given, &publish_update,
+                                                         &publish_delete_given, &publish_delete);
+
+       values[Anum_pg_publication_puballtables - 1] =
+               BoolGetDatum(stmt->for_all_tables);
+       values[Anum_pg_publication_pubinsert - 1] =
+               BoolGetDatum(publish_insert);
+       values[Anum_pg_publication_pubupdate - 1] =
+               BoolGetDatum(publish_update);
+       values[Anum_pg_publication_pubdelete - 1] =
+               BoolGetDatum(publish_delete);
+
+       tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
+
+       /* Insert tuple into catalog. */
+       puboid = simple_heap_insert(rel, tup);
+       CatalogUpdateIndexes(rel, tup);
+       heap_freetuple(tup);
+
+       ObjectAddressSet(myself, PublicationRelationId, puboid);
+
+       /* Make the changes visible. */
+       CommandCounterIncrement();
+
+       if (stmt->tables)
+       {
+               List       *rels;
+
+               Assert(list_length(stmt->tables) > 0);
+
+               rels = OpenTableList(stmt->tables);
+               PublicationAddTables(puboid, rels, true, NULL);
+               CloseTableList(rels);
+       }
+
+       heap_close(rel, RowExclusiveLock);
+
+       InvokeObjectPostCreateHook(PublicationRelationId, puboid, 0);
+
+       return myself;
+}
+
+/*
+ * Change options of a publication.
+ */
+static void
+AlterPublicationOptions(AlterPublicationStmt *stmt, Relation rel,
+                                          HeapTuple tup)
+{
+       bool            nulls[Natts_pg_publication];
+       bool            replaces[Natts_pg_publication];
+       Datum           values[Natts_pg_publication];
+       bool            publish_insert_given;
+       bool            publish_update_given;
+       bool            publish_delete_given;
+       bool            publish_insert;
+       bool            publish_update;
+       bool            publish_delete;
+       ObjectAddress           obj;
+
+       parse_publication_options(stmt->options,
+                                                         &publish_insert_given, &publish_insert,
+                                                         &publish_update_given, &publish_update,
+                                                         &publish_delete_given, &publish_delete);
+
+       /* Everything ok, form a new tuple. */
+       memset(values, 0, sizeof(values));
+       memset(nulls, false, sizeof(nulls));
+       memset(replaces, false, sizeof(replaces));
+
+       if (publish_insert_given)
+       {
+               values[Anum_pg_publication_pubinsert - 1] =
+                       BoolGetDatum(publish_insert);
+               replaces[Anum_pg_publication_pubinsert - 1] = true;
+       }
+       if (publish_update_given)
+       {
+               values[Anum_pg_publication_pubupdate - 1] =
+                       BoolGetDatum(publish_update);
+               replaces[Anum_pg_publication_pubupdate - 1] = true;
+       }
+       if (publish_delete_given)
+       {
+               values[Anum_pg_publication_pubdelete - 1] =
+                       BoolGetDatum(publish_delete);
+               replaces[Anum_pg_publication_pubdelete - 1] = true;
+       }
+
+       tup = heap_modify_tuple(tup, RelationGetDescr(rel), values, nulls,
+                                                       replaces);
+
+       /* Update the catalog. */
+       simple_heap_update(rel, &tup->t_self, tup);
+       CatalogUpdateIndexes(rel, tup);
+
+       CommandCounterIncrement();
+
+       /* Invalidate the relcache. */
+       if (((Form_pg_publication) GETSTRUCT(tup))->puballtables)
+       {
+               CacheInvalidateRelcacheAll();
+       }
+       else
+       {
+               List    *relids = GetPublicationRelations(HeapTupleGetOid(tup));
+
+               /*
+                * We don't want to send too many individual messages, at some point
+                * it's cheaper to just reset whole relcache.
+                */
+               if (list_length(relids) < MAX_RELCACHE_INVAL_MSGS)
+               {
+                       ListCell *lc;
+
+                       foreach (lc, relids)
+                       {
+                               Oid     relid = lfirst_oid(lc);
+
+                               CacheInvalidateRelcacheByRelid(relid);
+                       }
+               }
+               else
+                       CacheInvalidateRelcacheAll();
+       }
+
+       ObjectAddressSet(obj, PublicationRelationId, HeapTupleGetOid(tup));
+       EventTriggerCollectSimpleCommand(obj, InvalidObjectAddress,
+                                                                        (Node *) stmt);
+
+       InvokeObjectPostAlterHook(PublicationRelationId, HeapTupleGetOid(tup), 0);
+}
+
+/*
+ * Add or remove table to/from publication.
+ */
+static void
+AlterPublicationTables(AlterPublicationStmt *stmt, Relation rel,
+                                          HeapTuple tup)
+{
+       Oid                     pubid = HeapTupleGetOid(tup);
+       List       *rels = NIL;
+       Form_pg_publication pubform = (Form_pg_publication) GETSTRUCT(tup);
+
+       /* Check that user is allowed to manipulate the publication tables. */
+       if (pubform->puballtables)
+               ereport(ERROR,
+                               (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+                                errmsg("publication \"%s\" is defined as FOR ALL TABLES",
+                                               NameStr(pubform->pubname)),
+                                errdetail("Tables cannot be added to or dropped from FOR ALL TABLES publications.")));
+
+       Assert(list_length(stmt->tables) > 0);
+
+       rels = OpenTableList(stmt->tables);
+
+       if (stmt->tableAction == DEFELEM_ADD)
+               PublicationAddTables(pubid, rels, false, stmt);
+       else if (stmt->tableAction == DEFELEM_DROP)
+               PublicationDropTables(pubid, rels, false);
+       else /* DEFELEM_SET */
+       {
+               List       *oldrelids = GetPublicationRelations(pubid);
+               List       *delrels = NIL;
+               ListCell   *oldlc;
+
+               /* Calculate which relations to drop. */
+               foreach(oldlc, oldrelids)
+               {
+                       Oid                     oldrelid = lfirst_oid(oldlc);
+                       ListCell   *newlc;
+                       bool            found = false;
+
+                       foreach(newlc, rels)
+                       {
+                               Relation        newrel = (Relation) lfirst(newlc);
+
+                               if (RelationGetRelid(newrel) == oldrelid)
+                               {
+                                       found = true;
+                                       break;
+                               }
+                       }
+
+                       if (!found)
+                       {
+                               Relation        oldrel = heap_open(oldrelid,
+                                                                                          ShareUpdateExclusiveLock);
+                               delrels = lappend(delrels, oldrel);
+                       }
+               }
+
+               /* And drop them. */
+               PublicationDropTables(pubid, delrels, true);
+
+               /*
+                * Don't bother calculating the difference for adding, we'll catch
+                * and skip existing ones when doing catalog update.
+                */
+               PublicationAddTables(pubid, rels, true, stmt);
+
+               CloseTableList(delrels);
+       }
+
+       CloseTableList(rels);
+}
+
+/*
+ * Alter the existing publication.
+ *
+ * This is dispatcher function for AlterPublicationOptions and
+ * AlterPublicationTables.
+ */
+void
+AlterPublication(AlterPublicationStmt *stmt)
+{
+       Relation                rel;
+       HeapTuple               tup;
+
+       rel = heap_open(PublicationRelationId, RowExclusiveLock);
+
+       tup = SearchSysCacheCopy1(PUBLICATIONNAME,
+                                                         CStringGetDatum(stmt->pubname));
+
+       if (!HeapTupleIsValid(tup))
+               ereport(ERROR,
+                               (errcode(ERRCODE_UNDEFINED_OBJECT),
+                                errmsg("publication \"%s\" does not exist",
+                                               stmt->pubname)));
+
+       /* must be owner */
+       if (!pg_publication_ownercheck(HeapTupleGetOid(tup), GetUserId()))
+               aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PUBLICATION,
+                                          stmt->pubname);
+
+       if (stmt->options)
+               AlterPublicationOptions(stmt, rel, tup);
+       else
+               AlterPublicationTables(stmt, rel, tup);
+
+       /* Cleanup. */
+       heap_freetuple(tup);
+       heap_close(rel, RowExclusiveLock);
+}
+
+/*
+ * Drop publication by OID
+ */
+void
+RemovePublicationById(Oid pubid)
+{
+       Relation        rel;
+       HeapTuple       tup;
+
+       rel = heap_open(PublicationRelationId, RowExclusiveLock);
+
+       tup = SearchSysCache1(PUBLICATIONOID, ObjectIdGetDatum(pubid));
+
+       if (!HeapTupleIsValid(tup))
+               elog(ERROR, "cache lookup failed for publication %u", pubid);
+
+       simple_heap_delete(rel, &tup->t_self);
+
+       ReleaseSysCache(tup);
+
+       heap_close(rel, RowExclusiveLock);
+}
+
+/*
+ * Remove relation from publication by mapping OID.
+ */
+void
+RemovePublicationRelById(Oid proid)
+{
+       Relation        rel;
+       HeapTuple       tup;
+       Form_pg_publication_rel         pubrel;
+
+       rel = heap_open(PublicationRelRelationId, RowExclusiveLock);
+
+       tup = SearchSysCache1(PUBLICATIONREL, ObjectIdGetDatum(proid));
+
+       if (!HeapTupleIsValid(tup))
+               elog(ERROR, "cache lookup failed for publication table %u",
+                        proid);
+
+
+       pubrel = (Form_pg_publication_rel) GETSTRUCT(tup);
+
+       /* Invalidate relcache so that publication info is rebuilt. */
+       CacheInvalidateRelcacheByRelid(pubrel->prrelid);
+
+       simple_heap_delete(rel, &tup->t_self);
+
+       ReleaseSysCache(tup);
+
+       heap_close(rel, RowExclusiveLock);
+}
+
+/*
+ * Open relations based om provided by RangeVar list.
+ * The returned tables are locked in ShareUpdateExclusiveLock mode.
+ */
+static List *
+OpenTableList(List *tables)
+{
+       List       *relids = NIL;
+       List       *rels = NIL;
+       ListCell   *lc;
+
+       /*
+        * Open, share-lock, and check all the explicitly-specified relations
+        */
+       foreach(lc, tables)
+       {
+               RangeVar   *rv = lfirst(lc);
+               Relation        rel;
+               bool            recurse = rv->inh;
+               Oid                     myrelid;
+
+               CHECK_FOR_INTERRUPTS();
+
+               rel = heap_openrv(rv, ShareUpdateExclusiveLock);
+               myrelid = RelationGetRelid(rel);
+               /*
+                * filter out duplicates when user specifies "foo, foo"
+                * Note that this algrithm is know to not be very effective (O(N^2))
+                * but given that it only works on list of tables given to us by user
+                * it's deemed acceptable.
+                */
+               if (list_member_oid(relids, myrelid))
+               {
+                       heap_close(rel, ShareUpdateExclusiveLock);
+                       continue;
+               }
+               rels = lappend(rels, rel);
+               relids = lappend_oid(relids, myrelid);
+
+               if (recurse)
+               {
+                       ListCell   *child;
+                       List       *children;
+
+                       children = find_all_inheritors(myrelid, ShareUpdateExclusiveLock,
+                                                                                  NULL);
+
+                       foreach(child, children)
+                       {
+                               Oid                     childrelid = lfirst_oid(child);
+
+                               if (list_member_oid(relids, childrelid))
+                                       continue;
+
+                               /*
+                                * Skip duplicates if user specified both parent and child
+                                * tables.
+                                */
+                               if (list_member_oid(relids, childrelid))
+                               {
+                                       heap_close(rel, ShareUpdateExclusiveLock);
+                                       continue;
+                               }
+
+                               /* find_all_inheritors already got lock */
+                               rel = heap_open(childrelid, NoLock);
+                               rels = lappend(rels, rel);
+                               relids = lappend_oid(relids, childrelid);
+                       }
+               }
+       }
+
+       list_free(relids);
+
+       return rels;
+}
+
+/*
+ * Close all relations in the list.
+ */
+static void
+CloseTableList(List *rels)
+{
+       ListCell   *lc;
+
+       foreach(lc, rels)
+       {
+               Relation        rel = (Relation) lfirst(lc);
+
+               heap_close(rel, NoLock);
+       }
+}
+
+/*
+ * Add listed tables to the publication.
+ */
+static void
+PublicationAddTables(Oid pubid, List *rels, bool if_not_exists,
+                                        AlterPublicationStmt *stmt)
+{
+       ListCell           *lc;
+
+       Assert(!stmt || !stmt->for_all_tables);
+
+       foreach(lc, rels)
+       {
+               Relation        rel = (Relation) lfirst(lc);
+               ObjectAddress   obj;
+
+               /* Must be owner of the table or superuser. */
+               if (!pg_class_ownercheck(RelationGetRelid(rel), GetUserId()))
+                       aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CLASS,
+                                                  RelationGetRelationName(rel));
+
+               obj = publication_add_relation(pubid, rel, if_not_exists);
+               if (stmt)
+               {
+                       EventTriggerCollectSimpleCommand(obj, InvalidObjectAddress,
+                                                                                        (Node *) stmt);
+
+                       InvokeObjectPostCreateHook(PublicationRelRelationId,
+                                                                          obj.objectId, 0);
+               }
+       }
+}
+
+/*
+ * Remove listed tables from the publication.
+ */
+static void
+PublicationDropTables(Oid pubid, List *rels, bool missing_ok)
+{
+       ObjectAddress   obj;
+       ListCell           *lc;
+       Oid                             prid;
+
+       foreach(lc, rels)
+       {
+               Relation        rel = (Relation) lfirst(lc);
+               Oid                     relid = RelationGetRelid(rel);
+
+               prid = GetSysCacheOid2(PUBLICATIONRELMAP, ObjectIdGetDatum(relid),
+                                                          ObjectIdGetDatum(pubid));
+               if (!OidIsValid(prid))
+               {
+                       if (missing_ok)
+                               continue;
+
+                       ereport(ERROR,
+                                       (errcode(ERRCODE_UNDEFINED_OBJECT),
+                                        errmsg("relation \"%s\" is not part of the publication",
+                                                       RelationGetRelationName(rel))));
+               }
+
+               ObjectAddressSet(obj, PublicationRelRelationId, prid);
+               performDeletion(&obj, DROP_CASCADE, 0);
+       }
+}
+
+/*
+ * Internal workhorse for changing a publication owner
+ */
+       static void
+AlterPublicationOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
+{
+       Form_pg_publication form;
+
+       form = (Form_pg_publication) GETSTRUCT(tup);
+
+       if (form->pubowner == newOwnerId)
+               return;
+
+       if (!pg_publication_ownercheck(HeapTupleGetOid(tup), GetUserId()))
+               aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PUBLICATION,
+                                          NameStr(form->pubname));
+
+       /* New owner must be a superuser */
+       if (!superuser_arg(newOwnerId))
+               ereport(ERROR,
+                               (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+                                errmsg("permission denied to change owner of publication \"%s\"",
+                                               NameStr(form->pubname)),
+                                errhint("The owner of a publication must be a superuser.")));
+
+       form->pubowner = newOwnerId;
+       simple_heap_update(rel, &tup->t_self, tup);
+       CatalogUpdateIndexes(rel, tup);
+
+       /* Update owner dependency reference */
+       changeDependencyOnOwner(PublicationRelationId,
+                                                       HeapTupleGetOid(tup),
+                                                       newOwnerId);
+
+       InvokeObjectPostAlterHook(PublicationRelationId,
+                                                         HeapTupleGetOid(tup), 0);
+}
+
+/*
+ * Change publication owner -- by name
+ */
+ObjectAddress
+AlterPublicationOwner(const char *name, Oid newOwnerId)
+{
+       Oid                     subid;
+       HeapTuple       tup;
+       Relation        rel;
+       ObjectAddress address;
+
+       rel = heap_open(PublicationRelationId, RowExclusiveLock);
+
+       tup = SearchSysCacheCopy1(PUBLICATIONNAME, CStringGetDatum(name));
+
+       if (!HeapTupleIsValid(tup))
+               ereport(ERROR,
+                               (errcode(ERRCODE_UNDEFINED_OBJECT),
+                                errmsg("publication \"%s\" does not exist", name)));
+
+       subid = HeapTupleGetOid(tup);
+
+       AlterPublicationOwner_internal(rel, tup, newOwnerId);
+
+       ObjectAddressSet(address, PublicationRelationId, subid);
+
+       heap_freetuple(tup);
+
+       heap_close(rel, RowExclusiveLock);
+
+       return address;
+}
+
+/*
+ * Change publication owner -- by OID
+ */
+void
+AlterPublicationOwner_oid(Oid subid, Oid newOwnerId)
+{
+       HeapTuple       tup;
+       Relation        rel;
+
+       rel = heap_open(PublicationRelationId, RowExclusiveLock);
+
+       tup = SearchSysCacheCopy1(PUBLICATIONOID, ObjectIdGetDatum(subid));
+
+       if (!HeapTupleIsValid(tup))
+               ereport(ERROR,
+                               (errcode(ERRCODE_UNDEFINED_OBJECT),
+                                errmsg("publication with OID %u does not exist", subid)));
+
+       AlterPublicationOwner_internal(rel, tup, newOwnerId);
+
+       heap_freetuple(tup);
+
+       heap_close(rel, RowExclusiveLock);
+}
diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c
new file mode 100644 (file)
index 0000000..1448ee3
--- /dev/null
@@ -0,0 +1,643 @@
+/*-------------------------------------------------------------------------
+ *
+ * subscriptioncmds.c
+ *             subscription catalog manipulation functions
+ *
+ * Copyright (c) 2015, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ *             subscriptioncmds.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+#include "miscadmin.h"
+
+#include "access/heapam.h"
+#include "access/htup_details.h"
+
+#include "catalog/indexing.h"
+#include "catalog/objectaccess.h"
+#include "catalog/objectaddress.h"
+#include "catalog/pg_type.h"
+#include "catalog/pg_subscription.h"
+
+#include "commands/defrem.h"
+#include "commands/event_trigger.h"
+#include "commands/subscriptioncmds.h"
+
+#include "replication/logicallauncher.h"
+#include "replication/origin.h"
+#include "replication/walreceiver.h"
+#include "replication/worker_internal.h"
+
+#include "storage/lmgr.h"
+
+#include "utils/builtins.h"
+#include "utils/memutils.h"
+#include "utils/syscache.h"
+
+/*
+ * Common option parsing function for CREATE and ALTER SUBSCRIPTION commands.
+ *
+ * Since not all options can be specified in both commands, this function
+ * will report an error on options if the target output pointer is NULL to
+ * accomodate that.
+ */
+static void
+parse_subscription_options(List *options, char **conninfo,
+                                                  List **publications, bool *enabled_given,
+                                                  bool *enabled, bool *create_slot, char **slot_name)
+{
+       ListCell   *lc;
+       bool            create_slot_given = false;
+
+       if (conninfo)
+               *conninfo = NULL;
+       if (publications)
+               *publications = NIL;
+       if (enabled)
+       {
+               *enabled_given = false;
+               *enabled = true;
+       }
+       if (create_slot)
+               *create_slot = true;
+       if (slot_name)
+               *slot_name = NULL;
+
+       /* Parse options */
+       foreach (lc, options)
+       {
+               DefElem    *defel = (DefElem *) lfirst(lc);
+
+               if (strcmp(defel->defname, "conninfo") == 0 && conninfo)
+               {
+                       if (*conninfo)
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                errmsg("conflicting or redundant options")));
+
+                       *conninfo = defGetString(defel);
+               }
+               else if (strcmp(defel->defname, "publication") == 0 && publications)
+               {
+                       if (*publications)
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                errmsg("conflicting or redundant options")));
+
+                       *publications = defGetStringList(defel);
+               }
+               else if (strcmp(defel->defname, "enabled") == 0 && enabled)
+               {
+                       if (*enabled_given)
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                errmsg("conflicting or redundant options")));
+
+                       *enabled_given = true;
+                       *enabled = defGetBoolean(defel);
+               }
+               else if (strcmp(defel->defname, "disabled") == 0 && enabled)
+               {
+                       if (*enabled_given)
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                errmsg("conflicting or redundant options")));
+
+                       *enabled_given = true;
+                       *enabled = !defGetBoolean(defel);
+               }
+               else if (strcmp(defel->defname, "create slot") == 0 && create_slot)
+               {
+                       if (create_slot_given)
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                errmsg("conflicting or redundant options")));
+
+                       create_slot_given = true;
+                       *create_slot = defGetBoolean(defel);
+               }
+               else if (strcmp(defel->defname, "nocreate slot") == 0 && create_slot)
+               {
+                       if (create_slot_given)
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                errmsg("conflicting or redundant options")));
+
+                       create_slot_given = true;
+                       *create_slot = !defGetBoolean(defel);
+               }
+               else if (strcmp(defel->defname, "slot name") == 0 && slot_name)
+               {
+                       if (*slot_name)
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                errmsg("conflicting or redundant options")));
+
+                       *slot_name = defGetString(defel);
+               }
+               else
+                       elog(ERROR, "unrecognized option: %s", defel->defname);
+       }
+}
+
+/*
+ * Auxiliary function to return a text array out of a list of String nodes.
+ */
+static Datum
+publicationListToArray(List *publist)
+{
+       ArrayType  *arr;
+       Datum      *datums;
+       int                     j = 0;
+       ListCell   *cell;
+       MemoryContext memcxt;
+       MemoryContext oldcxt;
+
+       /* Create memory context for temporary allocations. */
+       memcxt = AllocSetContextCreate(CurrentMemoryContext,
+                                                                  "publicationListToArray to array",
+                                                                  ALLOCSET_DEFAULT_MINSIZE,
+                                                                  ALLOCSET_DEFAULT_INITSIZE,
+                                                                  ALLOCSET_DEFAULT_MAXSIZE);
+       oldcxt = MemoryContextSwitchTo(memcxt);
+
+       datums = palloc(sizeof(text *) * list_length(publist));
+       foreach(cell, publist)
+       {
+               char       *name = strVal(lfirst(cell));
+               ListCell   *pcell;
+
+               /* Check for duplicates. */
+               foreach(pcell, publist)
+               {
+                       char       *pname = strVal(lfirst(cell));
+
+                       if (name == pname)
+                               break;
+
+                       if (strcmp(name, pname) == 0)
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                errmsg("publication name \"%s\" used more than once",
+                                                               pname)));
+               }
+
+               datums[j++] = CStringGetTextDatum(name);
+       }
+
+       MemoryContextSwitchTo(oldcxt);
+
+       arr = construct_array(datums, list_length(publist),
+                                                 TEXTOID, -1, false, 'i');
+       MemoryContextDelete(memcxt);
+
+       return PointerGetDatum(arr);
+}
+
+/*
+ * Create new subscription.
+ */
+ObjectAddress
+CreateSubscription(CreateSubscriptionStmt *stmt)
+{
+       Relation        rel;
+       ObjectAddress myself;
+       Oid                     subid;
+       bool            nulls[Natts_pg_subscription];
+       Datum           values[Natts_pg_subscription];
+       HeapTuple       tup;
+       bool            enabled_given;
+       bool            enabled;
+       char       *conninfo;
+       char       *slotname;
+       char            originname[NAMEDATALEN];
+       bool            create_slot;
+       List       *publications;
+
+       if (!superuser())
+               ereport(ERROR,
+                               (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+                                (errmsg("must be superuser to create subscriptions"))));
+
+       rel = heap_open(SubscriptionRelationId, RowExclusiveLock);
+
+       /* Check if name is used */
+       subid = GetSysCacheOid2(SUBSCRIPTIONNAME, MyDatabaseId,
+                                                       CStringGetDatum(stmt->subname));
+       if (OidIsValid(subid))
+       {
+               ereport(ERROR,
+                               (errcode(ERRCODE_DUPLICATE_OBJECT),
+                                errmsg("subscription \"%s\" already exists",
+                                               stmt->subname)));
+       }
+
+       /*
+        * Parse and check options.
+        * Connection and publication should not be specified here.
+        */
+       parse_subscription_options(stmt->options, NULL, NULL,
+                                                          &enabled_given, &enabled,
+                                                          &create_slot, &slotname);
+       if (slotname == NULL)
+               slotname = stmt->subname;
+
+       conninfo = stmt->conninfo;
+       publications = stmt->publication;
+
+       /* Load the library providing us libpq calls. */
+       load_file("libpqwalreceiver", false);
+
+       /* Check the connection info string. */
+       walrcv_check_conninfo(conninfo);
+
+       /* Everything ok, form a new tuple. */
+       memset(values, 0, sizeof(values));
+       memset(nulls, false, sizeof(nulls));
+
+       values[Anum_pg_subscription_subdbid - 1] = ObjectIdGetDatum(MyDatabaseId);
+       values[Anum_pg_subscription_subname - 1] =
+               DirectFunctionCall1(namein, CStringGetDatum(stmt->subname));
+       values[Anum_pg_subscription_subowner - 1] = ObjectIdGetDatum(GetUserId());
+       values[Anum_pg_subscription_subenabled - 1] = BoolGetDatum(enabled);
+       values[Anum_pg_subscription_subconninfo - 1] =
+               CStringGetTextDatum(conninfo);
+       values[Anum_pg_subscription_subslotname - 1] =
+               DirectFunctionCall1(namein, CStringGetDatum(slotname));
+       values[Anum_pg_subscription_subpublications - 1] =
+                publicationListToArray(publications);
+
+       tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
+
+       /* Insert tuple into catalog. */
+       subid = simple_heap_insert(rel, tup);
+       CatalogUpdateIndexes(rel, tup);
+       heap_freetuple(tup);
+
+       snprintf(originname, sizeof(originname), "pg_%u", subid);
+       replorigin_create(originname);
+
+       /*
+        * If requested, create the replication slot on remote side for our
+        * newly created subscription.
+        */
+       if (create_slot)
+       {
+               XLogRecPtr                      lsn;
+               char                       *err;
+               WalReceiverConn    *wrconn;
+
+               /* Try to connect to the publisher. */
+               wrconn = walrcv_connect(conninfo, true, stmt->subname, &err);
+               if (!wrconn)
+                       ereport(ERROR,
+                                       (errmsg("could not connect to the publisher: %s", err)));
+
+               walrcv_create_slot(wrconn, slotname, false, &lsn);
+               ereport(NOTICE,
+                               (errmsg("created replication slot \"%s\" on publisher",
+                                               slotname)));
+
+               /* And we are done with the remote side. */
+               walrcv_disconnect(wrconn);
+       }
+
+       heap_close(rel, RowExclusiveLock);
+
+       ApplyLauncherWakeupAtCommit();
+
+       ObjectAddressSet(myself, SubscriptionRelationId, subid);
+
+       InvokeObjectPostCreateHook(SubscriptionRelationId, subid, 0);
+
+       return myself;
+}
+
+/*
+ * Alter the existing subscription.
+ */
+ObjectAddress
+AlterSubscription(AlterSubscriptionStmt *stmt)
+{
+       Relation        rel;
+       ObjectAddress myself;
+       bool            nulls[Natts_pg_subscription];
+       bool            replaces[Natts_pg_subscription];
+       Datum           values[Natts_pg_subscription];
+       HeapTuple       tup;
+       Oid                     subid;
+       bool            enabled_given;
+       bool            enabled;
+       char       *conninfo;
+       char       *slot_name;
+       List       *publications;
+
+       rel = heap_open(SubscriptionRelationId, RowExclusiveLock);
+
+       /* Fetch the existing tuple. */
+       tup = SearchSysCacheCopy2(SUBSCRIPTIONNAME, MyDatabaseId,
+                                                         CStringGetDatum(stmt->subname));
+
+       if (!HeapTupleIsValid(tup))
+               ereport(ERROR,
+                               (errcode(ERRCODE_UNDEFINED_OBJECT),
+                                errmsg("subscription \"%s\" does not exist",
+                                               stmt->subname)));
+
+       /* must be owner */
+       if (!pg_subscription_ownercheck(HeapTupleGetOid(tup), GetUserId()))
+               aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_SUBSCRIPTION,
+                                          stmt->subname);
+
+       subid = HeapTupleGetOid(tup);
+
+       /* Parse options. */
+       parse_subscription_options(stmt->options, &conninfo, &publications,
+                                                          &enabled_given, &enabled,
+                                                          NULL, &slot_name);
+
+       /* Form a new tuple. */
+       memset(values, 0, sizeof(values));
+       memset(nulls, false, sizeof(nulls));
+       memset(replaces, false, sizeof(replaces));
+
+       if (enabled_given)
+       {
+               values[Anum_pg_subscription_subenabled - 1] = BoolGetDatum(enabled);
+               replaces[Anum_pg_subscription_subenabled - 1] = true;
+       }
+       if (conninfo)
+       {
+               values[Anum_pg_subscription_subconninfo - 1] =
+                       CStringGetTextDatum(conninfo);
+               replaces[Anum_pg_subscription_subconninfo - 1] = true;
+       }
+       if (slot_name)
+       {
+               values[Anum_pg_subscription_subslotname - 1] =
+                       DirectFunctionCall1(namein, CStringGetDatum(slot_name));
+               replaces[Anum_pg_subscription_subslotname - 1] = true;
+       }
+       if (publications != NIL)
+       {
+               values[Anum_pg_subscription_subpublications - 1] =
+                        publicationListToArray(publications);
+               replaces[Anum_pg_subscription_subpublications - 1] = true;
+       }
+
+       tup = heap_modify_tuple(tup, RelationGetDescr(rel), values, nulls,
+                                                       replaces);
+
+       /* Update the catalog. */
+       simple_heap_update(rel, &tup->t_self, tup);
+       CatalogUpdateIndexes(rel, tup);
+
+       ObjectAddressSet(myself, SubscriptionRelationId, subid);
+
+       /* Cleanup. */
+       heap_freetuple(tup);
+       heap_close(rel, RowExclusiveLock);
+
+       InvokeObjectPostAlterHook(SubscriptionRelationId, subid, 0);
+
+       return myself;
+}
+
+/*
+ * Drop a subscription
+ */
+void
+DropSubscription(DropSubscriptionStmt *stmt)
+{
+       Relation        rel;
+       ObjectAddress myself;
+       HeapTuple       tup;
+       Oid                     subid;
+       Datum           datum;
+       bool            isnull;
+       char       *subname;
+       char       *conninfo;
+       char       *slotname;
+       char            originname[NAMEDATALEN];
+       char       *err = NULL;
+       RepOriginId     originid;
+       WalReceiverConn    *wrconn = NULL;
+       StringInfoData          cmd;
+
+       rel = heap_open(SubscriptionRelationId, RowExclusiveLock);
+
+       tup = SearchSysCache2(SUBSCRIPTIONNAME, MyDatabaseId,
+                                                 CStringGetDatum(stmt->subname));
+
+       if (!HeapTupleIsValid(tup))
+       {
+               heap_close(rel, NoLock);
+
+               if (!stmt->missing_ok)
+                       ereport(ERROR,
+                                       (errcode(ERRCODE_UNDEFINED_OBJECT),
+                                        errmsg("subscription \"%s\" does not exist",
+                                                       stmt->subname)));
+               else
+                       ereport(NOTICE,
+                                       (errmsg("subscription \"%s\" does not exist, skipping",
+                                                       stmt->subname)));
+
+               return;
+       }
+
+       subid = HeapTupleGetOid(tup);
+
+       /* must be owner */
+       if (!pg_subscription_ownercheck(subid, GetUserId()))
+               aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_SUBSCRIPTION,
+                                          stmt->subname);
+
+       /* DROP hook for the subscription being removed */
+       InvokeObjectDropHook(SubscriptionRelationId, subid, 0);
+
+       /*
+        * Lock the subscription so noboby else can do anything with it
+        * (including the replication workers).
+        */
+       LockSharedObject(SubscriptionRelationId, subid, 0, AccessExclusiveLock);
+
+       /* Get subname */
+       datum = SysCacheGetAttr(SUBSCRIPTIONOID, tup,
+                                                       Anum_pg_subscription_subname, &isnull);
+       Assert(!isnull);
+       subname = pstrdup(NameStr(*DatumGetName(datum)));
+
+       /* Get conninfo */
+       datum = SysCacheGetAttr(SUBSCRIPTIONOID, tup,
+                                                       Anum_pg_subscription_subconninfo, &isnull);
+       Assert(!isnull);
+       conninfo = pstrdup(TextDatumGetCString(datum));
+
+       /* Get slotname */
+       datum = SysCacheGetAttr(SUBSCRIPTIONOID, tup,
+                                                       Anum_pg_subscription_subslotname, &isnull);
+       Assert(!isnull);
+       slotname = pstrdup(NameStr(*DatumGetName(datum)));
+
+       ObjectAddressSet(myself, SubscriptionRelationId, subid);
+       EventTriggerSQLDropAddObject(&myself, true, true);
+
+       /* Remove the tuple from catalog. */
+       simple_heap_delete(rel, &tup->t_self);
+
+       ReleaseSysCache(tup);
+
+       /* Protect against launcher restarting the worker. */
+       LWLockAcquire(LogicalRepLauncherLock, LW_EXCLUSIVE);
+
+       /* Kill the apply worker so that the slot becomes accessible. */
+       logicalrep_worker_stop(subid);
+
+       /* Remove the origin tracking if exists. */
+       snprintf(originname, sizeof(originname), "pg_%u", subid);
+       originid = replorigin_by_name(originname, true);
+       if (originid != InvalidRepOriginId)
+               replorigin_drop(originid);
+
+       /* If the user asked to not drop the slot, we are done mow.*/
+       if (!stmt->drop_slot)
+       {
+               heap_close(rel, NoLock);
+               return;
+       }
+
+       /*
+        * Otherwise drop the replication slot at the publisher node using
+        * the replication connection.
+        */
+       load_file("libpqwalreceiver", false);
+
+       initStringInfo(&cmd);
+       appendStringInfo(&cmd, "DROP_REPLICATION_SLOT \"%s\"", slotname);
+
+       wrconn = walrcv_connect(conninfo, true, subname, &err);
+       if (wrconn == NULL)
+               ereport(ERROR,
+                               (errmsg("could not connect to publisher when attempting to "
+                                               "drop the replication slot \"%s\"", slotname),
+                                errdetail("The error was: %s", err)));
+
+       if (!walrcv_command(wrconn, cmd.data, &err))
+               ereport(ERROR,
+                               (errmsg("count not drop the replication slot \"%s\" on publisher",
+                                               slotname),
+                                errdetail("The error was: %s", err)));
+       else
+               ereport(NOTICE,
+                               (errmsg("dropped replication slot \"%s\" on publisher",
+                                               slotname)));
+
+       walrcv_disconnect(wrconn);
+
+       pfree(cmd.data);
+
+       heap_close(rel, NoLock);
+}
+
+/*
+ * Internal workhorse for changing a subscription owner
+ */
+static void
+AlterSubscriptionOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId)
+{
+       Form_pg_subscription form;
+
+       form = (Form_pg_subscription) GETSTRUCT(tup);
+
+       if (form->subowner == newOwnerId)
+               return;
+
+       if (!pg_subscription_ownercheck(HeapTupleGetOid(tup), GetUserId()))
+               aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_SUBSCRIPTION,
+                                          NameStr(form->subname));
+
+       /* New owner must be a superuser */
+       if (!superuser_arg(newOwnerId))
+               ereport(ERROR,
+                               (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+                 errmsg("permission denied to change owner of subscription \"%s\"",
+                                NameStr(form->subname)),
+                        errhint("The owner of an subscription must be a superuser.")));
+
+       form->subowner = newOwnerId;
+       simple_heap_update(rel, &tup->t_self, tup);
+       CatalogUpdateIndexes(rel, tup);
+
+       /* Update owner dependency reference */
+       changeDependencyOnOwner(SubscriptionRelationId,
+                                                       HeapTupleGetOid(tup),
+                                                       newOwnerId);
+
+       InvokeObjectPostAlterHook(SubscriptionRelationId,
+                                                         HeapTupleGetOid(tup), 0);
+}
+
+/*
+ * Change subscription owner -- by name
+ */
+ObjectAddress
+AlterSubscriptionOwner(const char *name, Oid newOwnerId)
+{
+       Oid                     subid;
+       HeapTuple       tup;
+       Relation        rel;
+       ObjectAddress address;
+
+       rel = heap_open(SubscriptionRelationId, RowExclusiveLock);
+
+       tup = SearchSysCacheCopy2(SUBSCRIPTIONNAME, MyDatabaseId,
+                                                         CStringGetDatum(name));
+
+       if (!HeapTupleIsValid(tup))
+               ereport(ERROR,
+                               (errcode(ERRCODE_UNDEFINED_OBJECT),
+                                errmsg("subscription \"%s\" does not exist", name)));
+
+       subid = HeapTupleGetOid(tup);
+
+       AlterSubscriptionOwner_internal(rel, tup, newOwnerId);
+
+       ObjectAddressSet(address, SubscriptionRelationId, subid);
+
+       heap_freetuple(tup);
+
+       heap_close(rel, RowExclusiveLock);
+
+       return address;
+}
+
+/*
+ * Change subscription owner -- by OID
+ */
+void
+AlterSubscriptionOwner_oid(Oid subid, Oid newOwnerId)
+{
+       HeapTuple       tup;
+       Relation        rel;
+
+       rel = heap_open(SubscriptionRelationId, RowExclusiveLock);
+
+       tup = SearchSysCacheCopy1(SUBSCRIPTIONOID, ObjectIdGetDatum(subid));
+
+       if (!HeapTupleIsValid(tup))
+               ereport(ERROR,
+                               (errcode(ERRCODE_UNDEFINED_OBJECT),
+                                errmsg("subscription with OID %u does not exist", subid)));
+
+       AlterSubscriptionOwner_internal(rel, tup, newOwnerId);
+
+       heap_freetuple(tup);
+
+       heap_close(rel, RowExclusiveLock);
+}
index 6ed2a3dc4d12f493e281b55bdc35a65124b77624..c4b0011bdd61e3e0589fb35a559090e4f7342987 100644 (file)
@@ -12055,6 +12055,18 @@ ATPrepChangePersistence(Relation rel, bool toLogged)
                        break;
        }
 
+       /*
+        * Check that the table is not part any publication when changing to
+        * UNLOGGED as UNLOGGED tables can't be published.
+        */
+       if (!toLogged &&
+               list_length(GetRelationPublications(RelationGetRelid(rel))) > 0)
+               ereport(ERROR,
+                               (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+                                errmsg("cannot change table \"%s\" to unlogged because it is part of a publication",
+                                               RelationGetRelationName(rel)),
+                                errdetail("Unlogged relations cannot be replicated.")));
+
        /*
         * Check existing foreign key constraints to preserve the invariant that
         * permanent tables cannot reference unlogged ones.  Self-referencing
index c51415830ae2ee82ebb6d247daa5456430281cc2..2a2b7eb9bd94c141eb721e7bdc5dfffe4fe2e240 100644 (file)
@@ -14,7 +14,7 @@ include $(top_builddir)/src/Makefile.global
 
 OBJS = execAmi.o execCurrent.o execGrouping.o execIndexing.o execJunk.o \
        execMain.o execParallel.o execProcnode.o execQual.o \
-       execScan.o execTuples.o \
+       execReplication.o execScan.o execTuples.o \
        execUtils.o functions.o instrument.o nodeAppend.o nodeAgg.o \
        nodeBitmapAnd.o nodeBitmapOr.o \
        nodeBitmapHeapscan.o nodeBitmapIndexscan.o \
index e6edcc06c2393e58d6eb8c36339974e43c5940fb..0bc146ca47df06bfbcab8fc1e0c7c5e3b0da94af 100644 (file)
@@ -43,6 +43,7 @@
 #include "access/xact.h"
 #include "catalog/namespace.h"
 #include "catalog/partition.h"
+#include "catalog/pg_publication.h"
 #include "commands/matview.h"
 #include "commands/trigger.h"
 #include "executor/execdebug.h"
@@ -1024,7 +1025,7 @@ CheckValidResultRel(Relation resultRel, CmdType operation)
        {
                case RELKIND_RELATION:
                case RELKIND_PARTITIONED_TABLE:
-                       /* OK */
+                       CheckCmdReplicaIdentity(resultRel, operation);
                        break;
                case RELKIND_SEQUENCE:
                        ereport(ERROR,
diff --git a/src/backend/executor/execReplication.c b/src/backend/executor/execReplication.c
new file mode 100644 (file)
index 0000000..a18ae51
--- /dev/null
@@ -0,0 +1,553 @@
+/*-------------------------------------------------------------------------
+ *
+ * execReplication.c
+ *       miscellaneous executor routines for logical replication
+ *
+ * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * IDENTIFICATION
+ *       src/backend/executor/execReplication.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+#include "access/relscan.h"
+#include "access/transam.h"
+#include "access/xact.h"
+#include "commands/trigger.h"
+#include "executor/executor.h"
+#include "nodes/nodeFuncs.h"
+#include "parser/parse_relation.h"
+#include "parser/parsetree.h"
+#include "storage/bufmgr.h"
+#include "storage/lmgr.h"
+#include "utils/datum.h"
+#include "utils/lsyscache.h"
+#include "utils/memutils.h"
+#include "utils/rel.h"
+#include "utils/snapmgr.h"
+#include "utils/syscache.h"
+#include "utils/tqual.h"
+
+
+/*
+ * Setup a ScanKey for a search in the relation 'rel' for a tuple 'key' that
+ * is setup to match 'rel' (*NOT* idxrel!).
+ *
+ * Returns whether any column contains NULLs.
+ *
+ * This is not generic routine, it expects the idxrel to be replication
+ * identity of a rel and meet all limitations associated with that.
+ */
+static bool
+build_replindex_scan_key(ScanKey skey, Relation rel, Relation idxrel,
+                                                TupleTableSlot *searchslot)
+{
+       int                     attoff;
+       bool            isnull;
+       Datum           indclassDatum;
+       oidvector  *opclass;
+       int2vector *indkey = &idxrel->rd_index->indkey;
+       bool            hasnulls = false;
+
+       Assert(RelationGetReplicaIndex(rel) == RelationGetRelid(idxrel));
+
+       indclassDatum = SysCacheGetAttr(INDEXRELID, idxrel->rd_indextuple,
+                                                                       Anum_pg_index_indclass, &isnull);
+       Assert(!isnull);
+       opclass = (oidvector *) DatumGetPointer(indclassDatum);
+
+       /* Build scankey for every attribute in the index. */
+       for (attoff = 0; attoff < RelationGetNumberOfAttributes(idxrel); attoff++)
+       {
+               Oid                     operator;
+               Oid                     opfamily;
+               RegProcedure regop;
+               int                     pkattno = attoff + 1;
+               int                     mainattno = indkey->values[attoff];
+               Oid                     optype = get_opclass_input_type(opclass->values[attoff]);
+
+               /*
+                * Load the operator info.  We need this to get the equality operator
+                * function for the scan key.
+                */
+               opfamily = get_opclass_family(opclass->values[attoff]);
+
+               operator = get_opfamily_member(opfamily, optype,
+                                                                          optype,
+                                                                          BTEqualStrategyNumber);
+
+               if (!OidIsValid(operator))
+                       elog(ERROR, "could not find member %d(%u,%u) of opfamily %u",
+                                BTEqualStrategyNumber, optype, optype, opfamily);
+
+               regop = get_opcode(operator);
+
+               /* Initialize the scankey. */
+               ScanKeyInit(&skey[attoff],
+                                       pkattno,
+                                       BTEqualStrategyNumber,
+                                       regop,
+                                       searchslot->tts_values[mainattno - 1]);
+
+               /* Check for null value. */
+               if (searchslot->tts_isnull[mainattno - 1])
+               {
+                       hasnulls = true;
+                       skey[attoff].sk_flags |= SK_ISNULL;
+               }
+       }
+
+       return hasnulls;
+}
+
+/*
+ * Search the relation 'rel' for tuple using the index.
+ *
+ * If a matching tuple is found, lock it with lockmode, fill the slot with its
+ * contents, and return true.  Return false otherwise.
+ */
+bool
+RelationFindReplTupleByIndex(Relation rel, Oid idxoid,
+                                                        LockTupleMode lockmode,
+                                                        TupleTableSlot *searchslot,
+                                                        TupleTableSlot *outslot)
+{
+       HeapTuple               scantuple;
+       ScanKeyData             skey[INDEX_MAX_KEYS];
+       IndexScanDesc   scan;
+       SnapshotData    snap;
+       TransactionId   xwait;
+       Relation                idxrel;
+       bool                    found;
+
+       /* Open the index.*/
+       idxrel = index_open(idxoid, RowExclusiveLock);
+
+       /* Start an index scan. */
+       InitDirtySnapshot(snap);
+       scan = index_beginscan(rel, idxrel, &snap,
+                                                  RelationGetNumberOfAttributes(idxrel),
+                                                  0);
+
+       /* Build scan key. */
+       build_replindex_scan_key(skey, rel, idxrel, searchslot);
+
+retry:
+       found = false;
+
+       index_rescan(scan, skey, RelationGetNumberOfAttributes(idxrel), NULL, 0);
+
+       /* Try to find the tuple */
+       if ((scantuple = index_getnext(scan, ForwardScanDirection)) != NULL)
+       {
+               found = true;
+               ExecStoreTuple(scantuple, outslot, InvalidBuffer, false);
+               ExecMaterializeSlot(outslot);
+
+               xwait = TransactionIdIsValid(snap.xmin) ?
+                       snap.xmin : snap.xmax;
+
+               /*
+                * If the tuple is locked, wait for locking transaction to finish
+                * and retry.
+                */
+               if (TransactionIdIsValid(xwait))
+               {
+                       XactLockTableWait(xwait, NULL, NULL, XLTW_None);
+                       goto retry;
+               }
+       }
+
+       /* Found tuple, try to lock it in the lockmode. */
+       if (found)
+       {
+               Buffer buf;
+               HeapUpdateFailureData hufd;
+               HTSU_Result res;
+               HeapTupleData locktup;
+
+               ItemPointerCopy(&outslot->tts_tuple->t_self, &locktup.t_self);
+
+               PushActiveSnapshot(GetLatestSnapshot());
+
+               res = heap_lock_tuple(rel, &locktup, GetCurrentCommandId(false),
+                                                         lockmode,
+                                                         false /* wait */,
+                                                         false /* don't follow updates */,
+                                                         &buf, &hufd);
+               /* the tuple slot already has the buffer pinned */
+               ReleaseBuffer(buf);
+
+               PopActiveSnapshot();
+
+               switch (res)
+               {
+                       case HeapTupleMayBeUpdated:
+                               break;
+                       case HeapTupleUpdated:
+                               /* XXX: Improve handling here */
+                               ereport(LOG,
+                                               (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
+                                                errmsg("concurrent update, retrying")));
+                               goto retry;
+                       case HeapTupleInvisible:
+                               elog(ERROR, "attempted to lock invisible tuple");
+                       default:
+                               elog(ERROR, "unexpected heap_lock_tuple status: %u", res);
+                               break;
+               }
+       }
+
+       index_endscan(scan);
+
+       /* Don't release lock until commit. */
+       index_close(idxrel, NoLock);
+
+       return found;
+}
+
+/*
+ * Compare the tuple and slot and check if they have equal values.
+ *
+ * We use binary datum comparison which might return false negatives but
+ * that's the best we can do here as there may be multiple notions of
+ * equality for the data types and table columns don't specify which one
+ * to use.
+ */
+static bool
+tuple_equals_slot(TupleDesc    desc, HeapTuple tup, TupleTableSlot *slot)
+{
+       Datum           values[MaxTupleAttributeNumber];
+       bool            isnull[MaxTupleAttributeNumber];
+       int                     attrnum;
+       Form_pg_attribute att;
+
+       heap_deform_tuple(tup, desc, values, isnull);
+
+       /* Check equality of the attributes. */
+       for (attrnum = 0; attrnum < desc->natts; attrnum++)
+       {
+               /*
+                * If one value is NULL and other is not, then they are certainly not
+                * equal
+                */
+               if (isnull[attrnum] != slot->tts_isnull[attrnum])
+                       return false;
+
+               /*
+                * If both are NULL, they can be considered equal.
+                */
+               if (isnull[attrnum])
+                       continue;
+
+               att = desc->attrs[attrnum];
+               if (!datumIsEqual(values[attrnum], slot->tts_values[attrnum],
+                                                 att->attbyval, att->attlen))
+                       return false;
+       }
+
+       return true;
+}
+
+/*
+ * Search the relation 'rel' for tuple using the sequential scan.
+ *
+ * If a matching tuple is found, lock it with lockmode, fill the slot with its
+ * contents, and return true.  Return false otherwise.
+ *
+ * Note that this stops on the first matching tuple.
+ *
+ * This can obviously be quite slow on tables that have more than few rows.
+ */
+bool
+RelationFindReplTupleSeq(Relation rel, LockTupleMode lockmode,
+                                                TupleTableSlot *searchslot, TupleTableSlot *outslot)
+{
+       HeapTuple               scantuple;
+       HeapScanDesc    scan;
+       SnapshotData    snap;
+       TransactionId   xwait;
+       bool                    found;
+       TupleDesc               desc = RelationGetDescr(rel);
+
+       Assert(equalTupleDescs(desc, outslot->tts_tupleDescriptor));
+
+       /* Start an index scan. */
+       InitDirtySnapshot(snap);
+       scan = heap_beginscan(rel, &snap, 0, NULL);
+
+retry:
+       found = false;
+
+       heap_rescan(scan, NULL);
+
+       /* Try to find the tuple */
+       while ((scantuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
+       {
+               if (!tuple_equals_slot(desc, scantuple, searchslot))
+                       continue;
+
+               found = true;
+               ExecStoreTuple(scantuple, outslot, InvalidBuffer, false);
+               ExecMaterializeSlot(outslot);
+
+               xwait = TransactionIdIsValid(snap.xmin) ?
+                       snap.xmin : snap.xmax;
+
+               /*
+                * If the tuple is locked, wait for locking transaction to finish
+                * and retry.
+                */
+               if (TransactionIdIsValid(xwait))
+               {
+                       XactLockTableWait(xwait, NULL, NULL, XLTW_None);
+                       goto retry;
+               }
+       }
+
+       /* Found tuple, try to lock it in the lockmode. */
+       if (found)
+       {
+               Buffer buf;
+               HeapUpdateFailureData hufd;
+               HTSU_Result res;
+               HeapTupleData locktup;
+
+               ItemPointerCopy(&outslot->tts_tuple->t_self, &locktup.t_self);
+
+               PushActiveSnapshot(GetLatestSnapshot());
+
+               res = heap_lock_tuple(rel, &locktup, GetCurrentCommandId(false),
+                                                         lockmode,
+                                                         false /* wait */,
+                                                         false /* don't follow updates */,
+                                                         &buf, &hufd);
+               /* the tuple slot already has the buffer pinned */
+               ReleaseBuffer(buf);
+
+               PopActiveSnapshot();
+
+               switch (res)
+               {
+                       case HeapTupleMayBeUpdated:
+                               break;
+                       case HeapTupleUpdated:
+                               /* XXX: Improve handling here */
+                               ereport(LOG,
+                                               (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
+                                                errmsg("concurrent update, retrying")));
+                               goto retry;
+                       case HeapTupleInvisible:
+                               elog(ERROR, "attempted to lock invisible tuple");
+                       default:
+                               elog(ERROR, "unexpected heap_lock_tuple status: %u", res);
+                               break;
+               }
+       }
+
+       heap_endscan(scan);
+
+       return found;
+}
+
+/*
+ * Insert tuple represented in the slot to the relation, update the indexes,
+ * and execute any constraints and per-row triggers.
+ *
+ * Caller is responsible for opening the indexes.
+ */
+void
+ExecSimpleRelationInsert(EState *estate, TupleTableSlot *slot)
+{
+       bool                    skip_tuple = false;
+       HeapTuple               tuple;
+       ResultRelInfo  *resultRelInfo = estate->es_result_relation_info;
+       Relation                rel = resultRelInfo->ri_RelationDesc;
+
+       /* For now we support only tables. */
+       Assert(rel->rd_rel->relkind == RELKIND_RELATION);
+
+       CheckCmdReplicaIdentity(rel, CMD_INSERT);
+
+       /* BEFORE ROW INSERT Triggers */
+       if (resultRelInfo->ri_TrigDesc &&
+               resultRelInfo->ri_TrigDesc->trig_insert_before_row)
+       {
+               slot = ExecBRInsertTriggers(estate, resultRelInfo, slot);
+
+               if (slot == NULL)       /* "do nothing" */
+                       skip_tuple = true;
+       }
+
+       if (!skip_tuple)
+       {
+               List       *recheckIndexes = NIL;
+
+               /* Check the constraints of the tuple */
+               if (rel->rd_att->constr)
+                       ExecConstraints(resultRelInfo, slot, slot, estate);
+
+               /* Store the slot into tuple that we can insett. */
+               tuple = ExecMaterializeSlot(slot);
+
+               /* OK, store the tuple and create index entries for it */
+               simple_heap_insert(rel, tuple);
+
+               if (resultRelInfo->ri_NumIndices > 0)
+                       recheckIndexes = ExecInsertIndexTuples(slot, &(tuple->t_self),
+                                                                                                  estate, false, NULL,
+                                                                                                  NIL);
+
+               /* AFTER ROW INSERT Triggers */
+               ExecARInsertTriggers(estate, resultRelInfo, tuple,
+                                                        recheckIndexes);
+
+               list_free(recheckIndexes);
+       }
+}
+
+/*
+ * Find the searchslot tuple and update it with data in the slot,
+ * update the indexes, and execute any constraints and per-row triggers.
+ *
+ * Caller is responsible for opening the indexes.
+ */
+void
+ExecSimpleRelationUpdate(EState *estate, EPQState *epqstate,
+                                                TupleTableSlot *searchslot, TupleTableSlot *slot)
+{
+       bool                    skip_tuple = false;
+       HeapTuple               tuple;
+       ResultRelInfo  *resultRelInfo = estate->es_result_relation_info;
+       Relation                rel = resultRelInfo->ri_RelationDesc;
+
+       /* For now we support only tables. */
+       Assert(rel->rd_rel->relkind == RELKIND_RELATION);
+
+       CheckCmdReplicaIdentity(rel, CMD_UPDATE);
+
+       /* BEFORE ROW INSERT Triggers */
+       if (resultRelInfo->ri_TrigDesc &&
+               resultRelInfo->ri_TrigDesc->trig_update_before_row)
+       {
+               slot = ExecBRUpdateTriggers(estate, epqstate, resultRelInfo,
+                                                                       &searchslot->tts_tuple->t_self,
+                                                                       NULL, slot);
+
+               if (slot == NULL)       /* "do nothing" */
+                       skip_tuple = true;
+       }
+
+       if (!skip_tuple)
+       {
+               List       *recheckIndexes = NIL;
+
+               /* Check the constraints of the tuple */
+               if (rel->rd_att->constr)
+                       ExecConstraints(resultRelInfo, slot, slot, estate);
+
+               /* Store the slot into tuple that we can write. */
+               tuple = ExecMaterializeSlot(slot);
+
+               /* OK, update the tuple and index entries for it */
+               simple_heap_update(rel, &searchslot->tts_tuple->t_self,
+                                                  slot->tts_tuple);
+
+               if (resultRelInfo->ri_NumIndices > 0 &&
+                       !HeapTupleIsHeapOnly(slot->tts_tuple))
+                       recheckIndexes = ExecInsertIndexTuples(slot, &(tuple->t_self),
+                                                                                                  estate, false, NULL,
+                                                                                                  NIL);
+
+               /* AFTER ROW UPDATE Triggers */
+               ExecARUpdateTriggers(estate, resultRelInfo,
+                                                        &searchslot->tts_tuple->t_self,
+                                                        NULL, tuple, recheckIndexes);
+
+               list_free(recheckIndexes);
+       }
+}
+
+/*
+ * Find the searchslot tuple and delete it, and execute any constraints
+ * and per-row triggers.
+ *
+ * Caller is responsible for opening the indexes.
+ */
+void
+ExecSimpleRelationDelete(EState *estate, EPQState *epqstate,
+                                                TupleTableSlot *searchslot)
+{
+       bool                    skip_tuple = false;
+       ResultRelInfo  *resultRelInfo = estate->es_result_relation_info;
+       Relation                rel = resultRelInfo->ri_RelationDesc;
+
+       /* For now we support only tables. */
+       Assert(rel->rd_rel->relkind == RELKIND_RELATION);
+
+       CheckCmdReplicaIdentity(rel, CMD_DELETE);
+
+       /* BEFORE ROW INSERT Triggers */
+       if (resultRelInfo->ri_TrigDesc &&
+               resultRelInfo->ri_TrigDesc->trig_update_before_row)
+       {
+               skip_tuple = !ExecBRDeleteTriggers(estate, epqstate, resultRelInfo,
+                                                                                  &searchslot->tts_tuple->t_self,
+                                                                                  NULL);
+       }
+
+       if (!skip_tuple)
+       {
+               List       *recheckIndexes = NIL;
+
+               /* OK, delete the tuple */
+               simple_heap_delete(rel, &searchslot->tts_tuple->t_self);
+
+               /* AFTER ROW DELETE Triggers */
+               ExecARDeleteTriggers(estate, resultRelInfo,
+                                                        &searchslot->tts_tuple->t_self, NULL);
+
+               list_free(recheckIndexes);
+       }
+}
+
+/*
+ * Check if command can be executed with current replica identity.
+ */
+void
+CheckCmdReplicaIdentity(Relation rel, CmdType cmd)
+{
+       PublicationActions *pubactions;
+
+       /* We only need to do checks for UPDATE and DELETE. */
+       if (cmd != CMD_UPDATE && cmd != CMD_DELETE)
+               return;
+
+       /* If relation has replica identity we are always good. */
+       if (rel->rd_rel->relreplident == REPLICA_IDENTITY_FULL ||
+               OidIsValid(RelationGetReplicaIndex(rel)))
+               return;
+
+       /*
+        * This is either UPDATE OR DELETE and there is no replica identity.
+        *
+        * Check if the table publishes UPDATES or DELETES.
+        */
+       pubactions = GetRelationPublicationActions(rel);
+       if (cmd == CMD_UPDATE && pubactions->pubupdate)
+               ereport(ERROR,
+                               (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+                                errmsg("cannot update table \"%s\" because it does not have replica identity and publishes updates",
+                                               RelationGetRelationName(rel)),
+                                errhint("To enable updating the table, set REPLICA IDENTITY using ALTER TABLE.")));
+       else if (cmd == CMD_DELETE && pubactions->pubdelete)
+               ereport(ERROR,
+                               (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+                                errmsg("cannot delete from table \"%s\" because it does not have replica identity and publishes deletes",
+                                               RelationGetRelationName(rel)),
+                                errhint("To enable deleting from the table, set REPLICA IDENTITY using ALTER TABLE.")));
+}
index f871e9d4bbf5b8c38c909551d868de735c01124d..30d733e57a002d23f45ccf653b0bca3962991f6a 100644 (file)
@@ -4286,6 +4286,69 @@ _copyPartitionCmd(const PartitionCmd *from)
        return newnode;
 }
 
+static CreatePublicationStmt *
+_copyCreatePublicationStmt(const CreatePublicationStmt *from)
+{
+       CreatePublicationStmt *newnode = makeNode(CreatePublicationStmt);
+
+       COPY_STRING_FIELD(pubname);
+       COPY_NODE_FIELD(options);
+       COPY_NODE_FIELD(tables);
+       COPY_SCALAR_FIELD(for_all_tables);
+
+       return newnode;
+}
+
+static AlterPublicationStmt *
+_copyAlterPublicationStmt(const AlterPublicationStmt *from)
+{
+       AlterPublicationStmt *newnode = makeNode(AlterPublicationStmt);
+
+       COPY_STRING_FIELD(pubname);
+       COPY_NODE_FIELD(options);
+       COPY_NODE_FIELD(tables);
+       COPY_SCALAR_FIELD(for_all_tables);
+       COPY_SCALAR_FIELD(tableAction);
+
+       return newnode;
+}
+
+static CreateSubscriptionStmt *
+_copyCreateSubscriptionStmt(const CreateSubscriptionStmt *from)
+{
+       CreateSubscriptionStmt *newnode = makeNode(CreateSubscriptionStmt);
+
+       COPY_STRING_FIELD(subname);
+       COPY_STRING_FIELD(conninfo);
+       COPY_NODE_FIELD(publication);
+       COPY_NODE_FIELD(options);
+
+       return newnode;
+}
+
+static AlterSubscriptionStmt *
+_copyAlterSubscriptionStmt(const AlterSubscriptionStmt *from)
+{
+       AlterSubscriptionStmt *newnode = makeNode(AlterSubscriptionStmt);
+
+       COPY_STRING_FIELD(subname);
+       COPY_NODE_FIELD(options);
+
+       return newnode;
+}
+
+static DropSubscriptionStmt *
+_copyDropSubscriptionStmt(const DropSubscriptionStmt *from)
+{
+       DropSubscriptionStmt *newnode = makeNode(DropSubscriptionStmt);
+
+       COPY_STRING_FIELD(subname);
+       COPY_SCALAR_FIELD(drop_slot);
+       COPY_SCALAR_FIELD(missing_ok);
+
+       return newnode;
+}
+
 /* ****************************************************************
  *                                     pg_list.h copy functions
  * ****************************************************************
@@ -5086,6 +5149,21 @@ copyObject(const void *from)
                case T_AlterPolicyStmt:
                        retval = _copyAlterPolicyStmt(from);
                        break;
+               case T_CreatePublicationStmt:
+                       retval = _copyCreatePublicationStmt(from);
+                       break;
+               case T_AlterPublicationStmt:
+                       retval = _copyAlterPublicationStmt(from);
+                       break;
+               case T_CreateSubscriptionStmt:
+                       retval = _copyCreateSubscriptionStmt(from);
+                       break;
+               case T_AlterSubscriptionStmt:
+                       retval = _copyAlterSubscriptionStmt(from);
+                       break;
+               case T_DropSubscriptionStmt:
+                       retval = _copyDropSubscriptionStmt(from);
+                       break;
                case T_A_Expr:
                        retval = _copyAExpr(from);
                        break;
index 78ed3c773e5a7aba7199a078e2451904fa51d32f..55c73b729262012cd66bbd2ef8db77aa7f4c45dc 100644 (file)
@@ -2134,6 +2134,64 @@ _equalAlterTSConfigurationStmt(const AlterTSConfigurationStmt *a,
        return true;
 }
 
+static bool
+_equalCreatePublicationStmt(const CreatePublicationStmt *a,
+                                                       const CreatePublicationStmt *b)
+{
+       COMPARE_STRING_FIELD(pubname);
+       COMPARE_NODE_FIELD(options);
+       COMPARE_NODE_FIELD(tables);
+       COMPARE_SCALAR_FIELD(for_all_tables);
+
+       return true;
+}
+
+static bool
+_equalAlterPublicationStmt(const AlterPublicationStmt *a,
+                                                  const AlterPublicationStmt *b)
+{
+       COMPARE_STRING_FIELD(pubname);
+       COMPARE_NODE_FIELD(options);
+       COMPARE_NODE_FIELD(tables);
+       COMPARE_SCALAR_FIELD(for_all_tables);
+       COMPARE_SCALAR_FIELD(tableAction);
+
+       return true;
+}
+
+static bool
+_equalCreateSubscriptionStmt(const CreateSubscriptionStmt *a,
+                                                        const CreateSubscriptionStmt *b)
+{
+       COMPARE_STRING_FIELD(subname);
+       COMPARE_STRING_FIELD(conninfo);
+       COMPARE_NODE_FIELD(publication);
+       COMPARE_NODE_FIELD(options);
+
+       return true;
+}
+
+static bool
+_equalAlterSubscriptionStmt(const AlterSubscriptionStmt *a,
+                                                       const AlterSubscriptionStmt *b)
+{
+       COMPARE_STRING_FIELD(subname);
+       COMPARE_NODE_FIELD(options);
+
+       return true;
+}
+
+static bool
+_equalDropSubscriptionStmt(const DropSubscriptionStmt *a,
+                                                  const DropSubscriptionStmt *b)
+{
+       COMPARE_STRING_FIELD(subname);
+       COMPARE_SCALAR_FIELD(drop_slot);
+       COMPARE_SCALAR_FIELD(missing_ok);
+
+       return true;
+}
+
 static bool
 _equalCreatePolicyStmt(const CreatePolicyStmt *a, const CreatePolicyStmt *b)
 {
@@ -3349,6 +3407,21 @@ equal(const void *a, const void *b)
                case T_AlterPolicyStmt:
                        retval = _equalAlterPolicyStmt(a, b);
                        break;
+               case T_CreatePublicationStmt:
+                       retval = _equalCreatePublicationStmt(a, b);
+                       break;
+               case T_AlterPublicationStmt:
+                       retval = _equalAlterPublicationStmt(a, b);
+                       break;
+               case T_CreateSubscriptionStmt:
+                       retval = _equalCreateSubscriptionStmt(a, b);
+                       break;
+               case T_AlterSubscriptionStmt:
+                       retval = _equalAlterSubscriptionStmt(a, b);
+                       break;
+               case T_DropSubscriptionStmt:
+                       retval = _equalDropSubscriptionStmt(a, b);
+                       break;
                case T_A_Expr:
                        retval = _equalAExpr(a, b);
                        break;
index e61ba06efe59088f358337fae9c5e85f3de58eef..a8e35fecccdcc06d717bb8bbeb2b15b35bd40f55 100644 (file)
@@ -280,6 +280,8 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
                DropOwnedStmt ReassignOwnedStmt
                AlterTSConfigurationStmt AlterTSDictionaryStmt
                CreateMatViewStmt RefreshMatViewStmt CreateAmStmt
+               CreatePublicationStmt AlterPublicationStmt
+               CreateSubscriptionStmt AlterSubscriptionStmt DropSubscriptionStmt
 
 %type <node>   select_no_parens select_with_parens select_clause
                                simple_select values_clause
@@ -334,6 +336,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
                                database_name access_method_clause access_method attr_name
                                name cursor_name file_name
                                index_name opt_index_name cluster_index_specification
+                               def_key
 
 %type <list>   func_name handler_name qual_Op qual_all_Op subquery_Op
                                opt_class opt_inline_handler opt_validator validator_clause
@@ -391,10 +394,13 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
                                relation_expr_list dostmt_opt_list
                                transform_element_list transform_type_list
                                TriggerTransitions TriggerReferencing
+                               publication_name_list
 
 %type <list>   group_by_list
 %type <node>   group_by_item empty_grouping_set rollup_clause cube_clause
 %type <node>   grouping_sets_clause
+%type <node>   opt_publication_for_tables publication_for_tables
+%type <value>  publication_name_item
 
 %type <list>   opt_fdw_options fdw_options
 %type <defelt> fdw_option
@@ -407,7 +413,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
 %type <fun_param_mode> arg_class
 %type <typnam> func_return func_type
 
-%type <boolean>  opt_trusted opt_restart_seqs
+%type <boolean>  opt_trusted opt_restart_seqs opt_drop_slot
 %type <ival>    OptTemp
 %type <ival>    OptNoLog
 %type <oncommit> OnCommitOption
@@ -647,7 +653,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
 
        PARALLEL PARSER PARTIAL PARTITION PASSING PASSWORD PLACING PLANS POLICY
        POSITION PRECEDING PRECISION PRESERVE PREPARE PREPARED PRIMARY
-       PRIOR PRIVILEGES PROCEDURAL PROCEDURE PROGRAM
+       PRIOR PRIVILEGES PROCEDURAL PROCEDURE PROGRAM PUBLICATION
 
        QUOTE
 
@@ -658,9 +664,9 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
 
        SAVEPOINT SCHEMA SCROLL SEARCH SECOND_P SECURITY SELECT SEQUENCE SEQUENCES
        SERIALIZABLE SERVER SESSION SESSION_USER SET SETS SETOF SHARE SHOW
-       SIMILAR SIMPLE SKIP SMALLINT SNAPSHOT SOME SQL_P STABLE STANDALONE_P START
-       STATEMENT STATISTICS STDIN STDOUT STORAGE STRICT_P STRIP_P SUBSTRING
-       SYMMETRIC SYSID SYSTEM_P
+       SIMILAR SIMPLE SKIP SLOT SMALLINT SNAPSHOT SOME SQL_P STABLE STANDALONE_P
+       START STATEMENT STATISTICS STDIN STDOUT STORAGE STRICT_P STRIP_P
+       SUBSCRIPTION SUBSTRING SYMMETRIC SYSID SYSTEM_P
 
        TABLE TABLES TABLESAMPLE TABLESPACE TEMP TEMPLATE TEMPORARY TEXT_P THEN
        TIME TIMESTAMP TO TRAILING TRANSACTION TRANSFORM TREAT TRIGGER TRIM TRUE_P
@@ -822,8 +828,10 @@ stmt :
                        | AlterTableStmt
                        | AlterTblSpcStmt
                        | AlterCompositeTypeStmt
+                       | AlterPublicationStmt
                        | AlterRoleSetStmt
                        | AlterRoleStmt
+                       | AlterSubscriptionStmt
                        | AlterTSConfigurationStmt
                        | AlterTSDictionaryStmt
                        | AlterUserMappingStmt
@@ -851,12 +859,14 @@ stmt :
                        | CreateMatViewStmt
                        | CreateOpClassStmt
                        | CreateOpFamilyStmt
+                       | CreatePublicationStmt
                        | AlterOpFamilyStmt
                        | CreatePolicyStmt
                        | CreatePLangStmt
                        | CreateSchemaStmt
                        | CreateSeqStmt
                        | CreateStmt
+                       | CreateSubscriptionStmt
                        | CreateTableSpaceStmt
                        | CreateTransformStmt
                        | CreateTrigStmt
@@ -883,6 +893,7 @@ stmt :
                        | DropPLangStmt
                        | DropRuleStmt
                        | DropStmt
+                       | DropSubscriptionStmt
                        | DropTableSpaceStmt
                        | DropTransformStmt
                        | DropTrigStmt
@@ -5613,16 +5624,21 @@ def_list:       def_elem                                                &n