pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Achilleas Mantzios <achill@matrix.gatewaynet.com>
To: pgsql-general@postgresql.org
Subject: Re: JDBC: logical replication and LSN feedback
Date: Tue, 19 Sep 2017 17:14:16 +0300
Message-ID: <254cb682-98e1-6966-48b7-1c931c5bd12e@matrix.gatewaynet.com> (raw)
In-Reply-To: <trinity-3e0e6193-3a88-442e-9b4f-1366a43c68f3-1505828251806@3c-app-mailcom-bs15>
References: <trinity-3e0e6193-3a88-442e-9b4f-1366a43c68f3-1505828251806@3c-app-mailcom-bs15>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-general>

On 19/09/2017 16:37, Yason TR wrote:
> Hi all,
>
> I am developing an application which connects to a logical replication slot, to consume the WAL events. These WAL events are then forwarded to a MQ broker.
>
> The heart of the code can be seen as:
>
> while (true) {
>      Connection connection = null;
>      PGReplicationStream stream = null;
>
>      try {
>          connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/db", properties);
>          stream = connection.unwrap(PGConnection.class).getReplicationAPI().replicationStream().logical().withSlotName("slot").start();
>
>          while (true) {
>              final ByteBuffer buffer = stream.read();
>
>              // ... MQ logic here ... omitted ...
>
>              stream.setAppliedLSN(stream.getLastReceiveLSN());
>              stream.setFlushedLSN(stream.getLastReceiveLSN());
>          }
>      } catch (final SQLException e) {
>          // ... log exception ... omitted ...
>      } finally {
>          // ... close stream and connection ... omitted ...
>      }
> }
>
> I notice some behavior which I cannot explain and would like to understand so I can alter my code:
>
> - When I restart the application, I notice that the application is retrieving the last event from the previous run again. The result is that this event is sent twice to the MQ broker after a restart of the application. Why is that? Isn't calling `setAppliedLSN(stream.getLastReceiveLSN())` and/or `setFlushedLSN(stream.getLastReceiveLSN())` enough to acknowledge an event, so it will removed from the WAL log and it will not be resent?
>
> - When receiving an event, the corresponding LSN from that event (which is sent in the payload) is not the same as the result of `stream.getLastReceivedLSN()`. Why is that? Which one should I use? Maybe this is correlated to my first question.
>
> - What is the difference between `setAppliedLSN(LSN)` and `setFlushedLSN(LSN)`? The Javadocs are not really helpful here.

The stages of a wal location generally go like : sent -> write -> flush -> replay , at least in terms of physical replication.
I guess applied=replayed ?

Note that from the docs : https://jdbc.postgresql.org/documentation/head/replication.html#logical-replication
it says :
"
In the event that replication has been restarted, it's will start from last successfully processed LSN that was sent via feedback to database.
"

>
> FYI, I also asked this question on https://stackoverflow.com/questions/46301578/postgres-jdbc-logical-replication-lsn-feedback.
>
> Thanks a lot and kind regards,
>
> Yason TR
>
>

-- 
Achilleas Mantzios
IT DEV Lead
IT DEPT
Dynacom Tankers Mgmt



-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



view thread (9+ messages)  latest in thread

Message-ID: <254cb682-98e1-6966-48b7-1c931c5bd12e@matrix.gatewaynet.com>
Permalink:  ../254cb682-98e1-6966-48b7-1c931c5bd12e@matrix.gatewaynet.com/
Also on:    postgresql.org/message-id/254cb682-98e1-6966-48b7-1c931c5bd12e@matrix.gatewaynet.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: achill@matrix.gatewaynet.com, pgsql-general@postgresql.org
  Subject: Re: JDBC: logical replication and LSN feedback
  In-Reply-To: <254cb682-98e1-6966-48b7-1c931c5bd12e@matrix.gatewaynet.com>
  References: <trinity-3e0e6193-3a88-442e-9b4f-1366a43c68f3-1505828251806@3c-app-mailcom-bs15> <254cb682-98e1-6966-48b7-1c931c5bd12e@matrix.gatewaynet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox