From 00b5c7896ffce14ca62f92670b485ba2e9ccba61 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter_e@gmx.net>
Date: Mon, 12 Dec 2016 12:00:00 -0500
Subject: [PATCH 6/8] fixup! Define logical replication protocol and output
 plugin

---
 doc/src/sgml/protocol.sgml              | 92 ++++++++++++++-------------------
 src/backend/replication/logical/proto.c | 12 ++---
 src/include/replication/pgoutput.h      |  2 +-
 3 files changed, 43 insertions(+), 63 deletions(-)

diff --git a/doc/src/sgml/protocol.sgml b/doc/src/sgml/protocol.sgml
index 4026687220..368d86c126 100644
--- a/doc/src/sgml/protocol.sgml
+++ b/doc/src/sgml/protocol.sgml
@@ -2126,7 +2126,7 @@ <title>Streaming Replication Protocol</title>
  <title>Logical Streaming Replication Protocol</title>
 
  <para>
-  This section describes the logical replication protocol which is the message
+  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.
@@ -2134,14 +2134,14 @@ <title>Logical Streaming Replication Protocol</title>
 
  <para>
   The logical streaming replication protocol builds on the primitives of
-  physical streaming replication protocol.
+  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
+   The logical replication <literal>START_REPLICATION</literal> command
    accepts following parameters:
 
    <variablelist>
@@ -2164,7 +2164,7 @@ <title>Logical Streaming Replication Parameters</title>
      <listitem>
       <para>
        Comma separated list of publication names for which to subscribe
-       (receive changes). The invidividual publication names are treated
+       (receive changes). The individual publication names are treated
        as standard objects names and can be quoted the same as needed.
       </para>
      </listitem>
@@ -2179,7 +2179,7 @@ <title>Logical Replication Protocol Messages</title>
 
   <para>
    The individual protocol messages are discussed in the following
-   sub-sections. Individual messages are describer in
+   subsections. Individual messages are describer in
    <xref linkend="protocol-logicalrep-message-formats"> section.
   </para>
 
@@ -2208,36 +2208,31 @@ <title>Logical Replication Protocol Message Flow</title>
 
   <para>
    The logical replication protocol sends individual transactions one by one.
-   This means that all messages between two Begin and Commit messages all
+   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) and in case of cascaded setup it can also contain Origin
+   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 replication node in the scope of logical
+   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 responsibility to handle this as
+   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 arbitrary relation id, which can be mapped to
-   an id in the Relation messages. The Relation describe the schema of the
+   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's the first time we send DML message for given relation in
+   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>
-
-  <para>
-  </para>
-
  </sect2>
-
 </sect1>
 
 <sect1 id="protocol-message-types">
@@ -5272,10 +5267,10 @@ <title>Logical Replication Message Formats</title>
 
 <para>
 This section describes the detailed format of each logical replication message.
-These messages are returned either by replication slot SQL interface or by
-WalSender. In case of WalSender they are encapsulated inside the replication
+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">
-section and generaly obey same message flow as physical replication.
+and generally obey same message flow as physical replication.
 </para>
 
 <variablelist>
@@ -5420,17 +5415,6 @@ <title>Logical Replication Message Formats</title>
 </varlistentry>
 <varlistentry>
 <term>
-        Int8
-</term>
-<listitem>
-<para>
-                Length of the origin name (including the NULL-termination
-                character).
-</para>
-</listitem>
-</varlistentry>
-<varlistentry>
-<term>
         String
 </term>
 <listitem>
@@ -5444,7 +5428,7 @@ <title>Logical Replication Message Formats</title>
 </para>
 
 <para>
-  Note that there can be mutiple Origin messages inside single transaction.
+  Note that there can be multiple Origin messages inside a single transaction.
 </para>
 
 </listitem>
@@ -5464,7 +5448,7 @@ <title>Logical Replication Message Formats</title>
 </term>
 <listitem>
 <para>
-                Identifies the message as an relation message.
+                Identifies the message as a relation message.
 </para>
 </listitem>
 </varlistentry>
@@ -5474,7 +5458,7 @@ <title>Logical Replication Message Formats</title>
 </term>
 <listitem>
 <para>
-                Id of the relation.
+                ID of the relation.
 </para>
 </listitem>
 </varlistentry>
@@ -5484,7 +5468,7 @@ <title>Logical Replication Message Formats</title>
 </term>
 <listitem>
 <para>
-                Namespace (empty string for pg_catalog).
+                Namespace (empty string for <literal>pg_catalog</literal>).
 </para>
 </listitem>
 </varlistentry>
@@ -5506,7 +5490,7 @@ <title>Logical Replication Message Formats</title>
 <listitem>
 <para>
                 Replica identity setting for the relation (same as
-                relreplident in pg_class).
+                <structfield>relreplident</structfield> in <structname>pg_class</structname>).
 </para>
 </listitem>
 </varlistentry>
@@ -5575,7 +5559,7 @@ <title>Logical Replication Message Formats</title>
 </term>
 <listitem>
 <para>
-                Id of the relation corresponding to the id in the relation
+                ID of the relation corresponding to the ID in the relation
                 message.
 </para>
 </listitem>
@@ -5593,11 +5577,11 @@ <title>Logical Replication Message Formats</title>
 
 <varlistentry>
 <term>
-        TableData
+        TupleData
 </term>
 <listitem>
 <para>
-                TableData message part representing the contents of new tuple.
+                TupleData message part representing the contents of new tuple.
 </para>
 </listitem>
 </varlistentry>
@@ -5631,7 +5615,7 @@ <title>Logical Replication Message Formats</title>
 </term>
 <listitem>
 <para>
-                Id of the relation corresponding to the id in the relation
+                ID of the relation corresponding to the ID in the relation
                 message.
 </para>
 </listitem>
@@ -5643,9 +5627,9 @@ <title>Logical Replication Message Formats</title>
 </term>
 <listitem>
 <para>
-                Identifies the following TupleData sub message as a key.
-                This field is optional and is only present if table in which
-                the update changed the REPLICA IDENTITY index.
+                Identifies the following TupleData submessage as a key.
+                This field is optional and is only present if
+                the update changed the REPLICA IDENTITY index. XXX???
 </para>
 </listitem>
 </varlistentry>
@@ -5656,7 +5640,7 @@ <title>Logical Replication Message Formats</title>
 </term>
 <listitem>
 <para>
-                Identifies the following TupleData message as a old tuple.
+                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>
@@ -5665,11 +5649,11 @@ <title>Logical Replication Message Formats</title>
 
 <varlistentry>
 <term>
-        TableData
+        TupleData
 </term>
 <listitem>
 <para>
-                TableData message part representing the contents of old tuple
+                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>
@@ -5689,11 +5673,11 @@ <title>Logical Replication Message Formats</title>
 
 <varlistentry>
 <term>
-        TableData
+        TupleData
 </term>
 <listitem>
 <para>
-                TableData message part representing the contents of a new tuple.
+                TupleData message part representing the contents of a new tuple.
 </para>
 </listitem>
 </varlistentry>
@@ -5702,7 +5686,7 @@ <title>Logical Replication Message Formats</title>
 </para>
 
 <para>
-    The Update message may contain either 'K' message part or 'O' message part
+    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>
 
@@ -5733,7 +5717,7 @@ <title>Logical Replication Message Formats</title>
 </term>
 <listitem>
 <para>
-                Id of the relation corresponding to the id in the relation
+                ID of the relation corresponding to the ID in the relation
                 message.
 </para>
 </listitem>
@@ -5745,7 +5729,7 @@ <title>Logical Replication Message Formats</title>
 </term>
 <listitem>
 <para>
-                Identifies the following TupleData sub message as a key.
+                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>
@@ -5767,11 +5751,11 @@ <title>Logical Replication Message Formats</title>
 
 <varlistentry>
 <term>
-        TableData
+        TupleData
 </term>
 <listitem>
 <para>
-                TableData message part representing the contents of old tuple
+                TupleData message part representing the contents of the old tuple
                 or primary key, depending on the previous field.
 </para>
 </listitem>
@@ -5780,7 +5764,7 @@ <title>Logical Replication Message Formats</title>
 </para>
 
 <para>
-    The Delete message may contain either 'K' message part or 'O' message,
+    The Delete message may contain either a 'K' message part or an 'O' message part,
     but never both of them.
 </para>
 
diff --git a/src/backend/replication/logical/proto.c b/src/backend/replication/logical/proto.c
index 56790c859e..9a2a168b40 100644
--- a/src/backend/replication/logical/proto.c
+++ b/src/backend/replication/logical/proto.c
@@ -200,7 +200,6 @@ logicalrep_write_origin(StringInfo out, const char *origin,
 	pq_sendstring(out, origin);
 }
 
-
 /*
  * Read ORIGIN from the output stream.
  */
@@ -214,7 +213,6 @@ logicalrep_read_origin(StringInfo in, XLogRecPtr *origin_lsn)
 	return pstrdup(pq_getmsgstring(in));
 }
 
-
 /*
  * Write INSERT to the output stream.
  */
@@ -487,7 +485,7 @@ logicalrep_write_tuple(StringInfo out, Relation rel, HeapTuple tuple)
 	}
 	pq_sendint(out, nliveatts, 2);
 
-	/* try to allocate enough memory from the get go */
+	/* try to allocate enough memory from the get-go */
 	enlargeStringInfo(out, tuple->t_len +
 					  nliveatts * (1 + 4));
 
@@ -499,7 +497,7 @@ logicalrep_write_tuple(StringInfo out, Relation rel, HeapTuple tuple)
 		HeapTuple	typtup;
 		Form_pg_type typclass;
 		Form_pg_attribute att = desc->attrs[i];
-		char   	   *outputstr;
+		char	   *outputstr;
 		int			len;
 
 		/* skip dropped columns */
@@ -584,9 +582,8 @@ logicalrep_read_tuple(StringInfo in, LogicalRepTupleData *tuple)
 	}
 }
 
-
 /*
- * Write relation attributes to the outputstream.
+ * Write relation attributes to the stream.
  */
 static void
 logicalrep_write_attrs(StringInfo out, Relation rel)
@@ -644,9 +641,8 @@ logicalrep_write_attrs(StringInfo out, Relation rel)
 	bms_free(idattrs);
 }
 
-
 /*
- * Read relation attribute names from the outputstream.
+ * Read relation attribute names from the stream.
  */
 static void
 logicalrep_read_attrs(StringInfo in, LogicalRepRelation *rel)
diff --git a/src/include/replication/pgoutput.h b/src/include/replication/pgoutput.h
index 7083cd7e76..c20451d1f2 100644
--- a/src/include/replication/pgoutput.h
+++ b/src/include/replication/pgoutput.h
@@ -16,7 +16,7 @@
 
 typedef struct PGOutputData
 {
-	MemoryContext	context;			/* pricate memory context for transient
+	MemoryContext	context;			/* private memory context for transient
 										 * allocations */
 
 	/* client info */
-- 
2.11.0

