Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ijnji-0002Qf-Fe for pgsql-hackers@arkaria.postgresql.org; Tue, 24 Dec 2019 17:12:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ijnjg-0000hz-0P for pgsql-hackers@arkaria.postgresql.org; Tue, 24 Dec 2019 17:12:44 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ijnjf-0000hs-HQ for pgsql-hackers@lists.postgresql.org; Tue, 24 Dec 2019 17:12:43 +0000 Received: from cyclops.postgrespro.ru ([93.174.131.138] helo=mail.postgrespro.ru) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ijnjX-0005Vz-FX for pgsql-hackers@postgresql.org; Tue, 24 Dec 2019 17:12:42 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id E01A021C4893; Tue, 24 Dec 2019 20:12:32 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=x tagged_above=-99 required=4 WHITELISTED tests=[] autolearn=unavailable Received: from [192.168.27.223] (gw.postgrespro.ru [93.174.131.141]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id ACFCE21C4883; Tue, 24 Dec 2019 20:12:32 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1577207552; bh=/SCUDGxJcXhA04Cs7W90H1vrQ+szvGsuobO6NlUT8bI=; h=From:Subject:To:Cc:Date; b=gmezx57AvLHN/Tqn8OfTIKOkdvt/sQsSvZKoYVYMfhtFe0nIsdU9nZYRo5k16m70J JvhMP3DLwLsGyAP0qTPim1xlVumCORQ+50hIXJLiYpkNB1SnN6q6ZXimN3Xu9gpuX5 4I7uEQyoXGw/cgz4beOEQD7qy8CCSCMS1vSmZ2Zw= From: Alexey Kondratov Subject: Physical replication slot advance is not persistent To: PostgreSQL Hackers Cc: Michael Paquier , Simon Riggs Message-ID: <059cc53a-8b14-653a-a24d-5f867503b0ee@postgrespro.ru> Date: Tue, 24 Dec 2019 20:12:32 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------884CF7FCE27D593ACF7B7188" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------884CF7FCE27D593ACF7B7188 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Hi Hackers, I have accidentally noticed that pg_replication_slot_advance only changes in-memory state of the slot when its type is physical. Its new value does not survive restart. Reproduction steps: 1) Create new slot and remember its restart_lsn SELECT pg_create_physical_replication_slot('slot1', true); SELECT * from pg_replication_slots; 2) Generate some dummy WAL CHECKPOINT; SELECT pg_switch_wal(); CHECKPOINT; SELECT pg_switch_wal(); 3) Advance slot to the value of pg_current_wal_insert_lsn() SELECT pg_replication_slot_advance('slot1', '0/160001A0'); 4) Check that restart_lsn has been updated SELECT * from pg_replication_slots; 5) Restart server and check restart_lsn again. It should be the same as in the step 1. I dig into the code and it happens because of this if statement:     /* Update the on disk state when lsn was updated. */     if (XLogRecPtrIsInvalid(endlsn))     {         ReplicationSlotMarkDirty();         ReplicationSlotsComputeRequiredXmin(false);         ReplicationSlotsComputeRequiredLSN();         ReplicationSlotSave();     } Actually, endlsn is always a valid LSN after the execution of replication slot advance guts. It works for logical slots only by chance, since there is an implicit ReplicationSlotMarkDirty() call inside LogicalConfirmReceivedLocation. Attached is a small patch, which fixes this bug. I have tried to stick to the same logic in this 'if (XLogRecPtrIsInvalid(endlsn))' and now pg_logical_replication_slot_advance and pg_physical_replication_slot_advance return InvalidXLogRecPtr if no-op. What do you think? Regards -- Alexey Kondratov Postgres Professional https://www.postgrespro.com Russian Postgres Company P.S. CCed Simon and Michael as they are the last who seriously touched pg_replication_slot_advance code. --------------884CF7FCE27D593ACF7B7188 Content-Type: text/x-patch; charset=UTF-8; name="v1-0001-Make-phsycal-replslot-advance-persistent.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="v1-0001-Make-phsycal-replslot-advance-persistent.patch" From 36d1fa2a89b3fb354a813354496df475ee11b62e Mon Sep 17 00:00:00 2001 From: Alexey Kondratov Date: Tue, 24 Dec 2019 18:21:50 +0300 Subject: [PATCH v1] Make phsycal replslot advance persistent --- src/backend/replication/slotfuncs.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/backend/replication/slotfuncs.c b/src/backend/replication/slotfuncs.c index 46e6dd4d12..826708d3f6 100644 --- a/src/backend/replication/slotfuncs.c +++ b/src/backend/replication/slotfuncs.c @@ -358,12 +358,14 @@ pg_get_replication_slots(PG_FUNCTION_ARGS) * The LSN position to move to is compared simply to the slot's restart_lsn, * knowing that any position older than that would be removed by successive * checkpoints. + * + * Returns InvalidXLogRecPtr if no-op. */ static XLogRecPtr pg_physical_replication_slot_advance(XLogRecPtr moveto) { XLogRecPtr startlsn = MyReplicationSlot->data.restart_lsn; - XLogRecPtr retlsn = startlsn; + XLogRecPtr retlsn = InvalidXLogRecPtr; if (startlsn < moveto) { @@ -386,6 +388,8 @@ pg_physical_replication_slot_advance(XLogRecPtr moveto) * because we need to digest WAL to advance restart_lsn allowing to recycle * WAL and removal of old catalog tuples. As decoding is done in fast_forward * mode, no changes are generated anyway. + * + * Returns InvalidXLogRecPtr if no-op. */ static XLogRecPtr pg_logical_replication_slot_advance(XLogRecPtr moveto) @@ -393,7 +397,7 @@ pg_logical_replication_slot_advance(XLogRecPtr moveto) LogicalDecodingContext *ctx; ResourceOwner old_resowner = CurrentResourceOwner; XLogRecPtr startlsn; - XLogRecPtr retlsn; + XLogRecPtr retlsn = InvalidXLogRecPtr; PG_TRY(); { @@ -414,9 +418,6 @@ pg_logical_replication_slot_advance(XLogRecPtr moveto) */ startlsn = MyReplicationSlot->data.restart_lsn; - /* Initialize our return value in case we don't do anything */ - retlsn = MyReplicationSlot->data.confirmed_flush; - /* invalidate non-timetravel entries */ InvalidateSystemCaches(); @@ -480,9 +481,9 @@ pg_logical_replication_slot_advance(XLogRecPtr moveto) * better than always losing the position even on clean restart. */ ReplicationSlotMarkDirty(); - } - retlsn = MyReplicationSlot->data.confirmed_flush; + retlsn = MyReplicationSlot->data.confirmed_flush; + } /* free context, call shutdown callback */ FreeDecodingContext(ctx); @@ -575,7 +576,7 @@ pg_replication_slot_advance(PG_FUNCTION_ARGS) nulls[0] = false; /* Update the on disk state when lsn was updated. */ - if (XLogRecPtrIsInvalid(endlsn)) + if (!XLogRecPtrIsInvalid(endlsn)) { ReplicationSlotMarkDirty(); ReplicationSlotsComputeRequiredXmin(false); -- 2.17.1 --------------884CF7FCE27D593ACF7B7188--