Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1duJKe-00082k-Ah for pgsql-general@arkaria.postgresql.org; Tue, 19 Sep 2017 14:17:00 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1duJKd-0002tu-Ta for pgsql-general@arkaria.postgresql.org; Tue, 19 Sep 2017 14:16:59 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1duJIA-0004G1-Ea for pgsql-general@postgresql.org; Tue, 19 Sep 2017 14:14:26 +0000 Received: from host3.dynacom.ondsl.gr ([62.103.35.211] helo=smadev.internal.net) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1duJI2-0007gK-SD for pgsql-general@postgresql.org; Tue, 19 Sep 2017 14:14:25 +0000 Received: from smadev.internal.net (smadev [10.9.200.131]) by smadev.internal.net (8.15.2/8.15.2) with ESMTP id v8JEEGwV079294 for ; Tue, 19 Sep 2017 17:14:16 +0300 (EEST) (envelope-from achill@matrix.gatewaynet.com) Subject: Re: JDBC: logical replication and LSN feedback To: pgsql-general@postgresql.org References: From: Achilleas Mantzios Message-ID: <254cb682-98e1-6966-48b7-1c931c5bd12e@matrix.gatewaynet.com> Date: Tue, 19 Sep 2017 17:14:16 +0300 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-general Precedence: bulk Sender: pgsql-general-owner@postgresql.org 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