Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xBMP6-000000039eu-2ZUT for pgsql-hackers@arkaria.postgresql.org; Tue, 29 Sep 2026 01:13:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xBMP3-0000000C8Co-3VG2 for pgsql-hackers@arkaria.postgresql.org; Tue, 29 Sep 2026 01:13:05 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xBMP3-0000000C8Cg-1vqS for pgsql-hackers@lists.postgresql.org; Tue, 29 Sep 2026 01:13:05 +0000 Received: from mail-pj2-x14.google.com ([2607:f8b0:4864:39::14]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1xBMP0-00000001q1V-1bV2 for pgsql-hackers@postgresql.org; Tue, 29 Sep 2026 01:13:04 +0000 Received: by mail-pj2-x14.google.com with SMTP id d9443c01a7336-2df9754484aso17452535ad.0 for ; Mon, 28 Sep 2026 18:13:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790644379; x=1791249179; darn=postgresql.org; h=references:to:cc:in-reply-to:date:subject:mime-version:content-type :message-id:from:from:to:cc:subject:date:message-id:reply-to :content-type; bh=kWljIEy/KrA93uCL7Pt+n4uSH8u8JUjVYGTUlHYa9Jg=; b=OSYRdOGGs3onnzOA+HIgw08EwDsyz2ruVSL0NyK9WeNKsh+N84uFqCf8gew/Zhcptl SxyE3bNdqOa+5YX0K6HO7rIo19SCnQ0X+zVYTb/hMbKpRLprQclovleR9/7nKrTyWo01 Kb3nO+PXA4plSKIXRdBfnbFnJ4rgln++Nrwtqc7BN6AUMzohaeSNXsgp7PS3SgGR1t1t M91E89BtvKmBH+iKqtaxTvc8qcxCqwLd6T+XGEfQ3xW2Fu6VsVUHNweWl2pSo6YqXEm6 1oGNH6FeRS7nFMrbwlyJN5DEeGh/EtJYWQb0o1+SXPsBugk2dDOVeTX0lk9JAdvK2gMD wlug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790644379; x=1791249179; h=references:to:cc:in-reply-to:date:subject:mime-version:content-type :message-id:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=kWljIEy/KrA93uCL7Pt+n4uSH8u8JUjVYGTUlHYa9Jg=; b=1reEcYRHAo5O4M099b968PLu2sbAtXwzhzbMouwRBHQ7pY7/uvl39mXcjq02x0/pAH eTvtQVHfx3kDQX/9ujUw6z+KjGIEC5/mOzGpD04nLAqaXtKR2r9jpAuIedMrly5wEmyt Giq+aX/KbYnKj4WWH2kQOFctjmtPgyF6D3Z7MSzpWK87RlWllkFxSgtse6CJgoPkUg/f Ha1ym/RNpr0mrgPHKWQQBiAY2+2e0/8JwxFwSTpBOCahcZJoWzRVaY94O1elym1f98fl oPiz+DcPAN2CzJML/M+VI4jxOldiNJEjZSff22875wmVQw89R7l4hqXaj8VB8tlrtHXB 5KyQ== X-Gm-Message-State: AFq9FYJdjAjr5EukEXt/7M5T1mVrmeYClPXWXz4HfBc/QiygNVI8vph+ 4tm1hDSO6SSFZ8fNjo7uhRvEmuR4M6YeUnzMP/NZkzhDd6eIeIkRXmhZeI7F5A== X-Gm-Gg: AYBFou1AkeBZwBXX9b5a+dFFaHmFVRjd88CpddxCnfNNNy3InZ8VEUuxPK3ggYRgA1o Qeu4rQLvjGlWRl27AHhSKQ2b0OG7McqyKUrmAaxGEF2VIs5yxsStQhdyMEtStopRQqLC+LMdW4w gvsUrCTbbusoex/8gMXJaCDh2KRzrRp9E0wtvS2QsqRWI+JPW9fQP8pH3Th/TDoeHJ/OrILwqw1 zpCiwcrZ63v3hxK/bQTUacFPFWAaDcqK1W4T42RDPiKSVLOiSxdwq2YFdpQkHW8kqeA8TWXNLE4 uatBdYv3hr3ehT4zHxADuxq9AebFEBLzgMo7VMO9HXRJQ5qWqX8snMiHb2NeO9qjdGchJ3FdhD3 kUvbGnbboEKK4cKIX6XK0BNz4M95wlMR2d0dF3PBSKOZK5pSmFnPNrfoo7i6LkRlgSV3R6vJUcs ph6nDApNkLMbaeNG4G/2QgjSO9iA+0FGtGE/0LYrVs9NcoiTjcR+ipCpBxkfgtjIZYDdNAxKdvX 1iBcCE= X-Received: by 2002:a17:902:ce84:b0:2df:90d4:1185 with SMTP id d9443c01a7336-2df90d41ad1mr101265865ad.10.1790644378356; Mon, 28 Sep 2026 18:12:58 -0700 (PDT) Received: from smtpclient.apple ([185.135.79.161]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2df964d46b0sm45647005ad.15.2026.09.28.18.12.56 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 28 Sep 2026 18:12:57 -0700 (PDT) From: Chao Li Message-Id: Content-Type: multipart/mixed; boundary="Apple-Mail=_67E885E9-8230-4144-B126-FF9BBF515396" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.700.51.1.1\)) Subject: Re: pg_resetwal with replication slot (17.11) Date: Tue, 29 Sep 2026 09:12:22 +0800 In-Reply-To: Cc: pgsql-hackers@postgresql.org To: =?utf-8?Q?R=C4=B1dvan_Korkmaz?= References: X-Mailer: Apple Mail (2.3864.700.51.1.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_67E885E9-8230-4144-B126-FF9BBF515396 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Sep 21, 2026, at 20:21, R=C4=B1dvan Korkmaz = wrote: >=20 > Hi Dear Experts, > I hit a case seems odd. I wonder if I do something unexpected, or = something is here I can't see. >=20 > First, these are all on test environment. The version is PostgreSQL = 17.11, uses homebrew installation on MacOS. >=20 > I have a master - replica setup, both are on the same host. > master's > PGDATA =3D m > port =3D 15432 > rs =3D rep slot for streaming replication used by instance "r", = created on master (m instance) > max_wal_size =3D 4GB > min_wal_size =3D 2GB > wal_level =3D replica >=20 >=20 > replica's > PGDATA =3D r > port =3D 25432 > primary_conninfo =3D created by pg_basebackup > primary_slot_name =3D rs >=20 >=20 > Case: I have 16MB WAL files on master instance (so on replica). I want = to utilize 1GB WAL files. > Here are the steps I take. >=20 > 1. setup master - replica run on same host in respective directories = and on ports > 2. verify streaming replication works > 3. verify "rs" (replication slot), master ("m" instance), and replica = ("r" instance) have SAME WAL lsn > 4. stop master, keep replica online (simulation for actual case) = (pg_ctl-17 stop -D m) > 5. run "pg_resetwal-17 -D m --wal-segsize=3D1024" on master instance. > 6. start master instance, started, fine. Replica complains about = "ERROR: requested WAL segment 000000010000000000000000 has already been = removed", no worry. > 7. stop master again, cool, done. (last log lines: "checkpoint = complete", "database system is shut down") > 8. start master -> bamm, could not start. (pg_ctl-17 start -D m -l = m.log) >=20 > There is no read, write between after step 3 (after verification of = WAL lsns) >=20 > Final failure log (step 8): > 2026-09-21 14:47:45.525 +03 [22817] LOG: starting PostgreSQL 17.11 = (Homebrew) on aarch64-apple-darwin25.6.0, compiled by Apple clang = version 21.0.0 (clang-2100.1.1.101), 64-bit > 2026-09-21 14:47:45.525 +03 [22817] LOG: listening on IPv4 address = "127.0.0.1", port 15432 > 2026-09-21 14:47:45.525 +03 [22817] LOG: listening on Unix socket = "/tmp/.s.PGSQL.15432" > 2026-09-21 14:47:45.528 +03 [22820] LOG: database system was shut down = at 2026-09-21 14:46:42 +03 > 2026-09-21 14:47:45.528 +03 [22820] LOG: invalid checkpoint record > 2026-09-21 14:47:45.528 +03 [22820] PANIC: could not locate a valid = checkpoint record at 0/40000110 > 2026-09-21 14:47:45.528 +03 [22817] LOG: startup process (PID 22820) = was terminated by signal 6: Abort trap: 6 > 2026-09-21 14:47:45.528 +03 [22817] LOG: terminating any other active = server processes > 2026-09-21 14:47:45.529 +03 [22817] LOG: shutting down due to startup = process failure > 2026-09-21 14:47:45.529 +03 [22817] LOG: database system is shut down >=20 >=20 >=20 > After pg_resetwal, first start of master successful, but a second = start fails. > I guess this causes master to be lost. >=20 > I'm able to spot the issue:=20 > The issue is replication slot. If I would have removed replication = slot before second start (do it between 6 and 7), it succeeds. >=20 > Questions: > 1. Is this behavior is expected? > 2. Should replication slot case mentioned in PostgreSQL documents? (I = checked yet could not see) > 3. Am I doing something out of order, unexpected? > 4. Once I understood the case, I dropped replication slot and able to = start master. Now I want to copy m/global/pg_control to replica and = m/pg_wal to replica as well and complete wal segment size change. I = wonder if this way is documented or supported. I can say "it works" but = does not mean "supported or documented at all". >=20 > Thank you in advance. >=20 > Attachments:<1-master-replica-setup-info.txt><3-all-wal-lsn-same.txt> >=20 To make the issue easier to reproduce, I created the attached = repro_slot_wal_segsize.sh. It basically follows the procedure described = by R=C4=B1dvan. I added "sleep 1" before the second server stop so that = the log messages generated by that stop are easier to distinguish. I = also added some temporary logging to show the problem more explicitly. = See the attached temp_log.diff for those changes. Here are the server logs from my reproduction: ``` 2026-09-28 16:49:57.428 CST [76061] LOG: EVAN checkpoint cleanup after = decrement: segment 0 2026-09-28 16:49:57.428 CST [76061] LOG: EVAN RemoveOldXlogFiles: = remove through segment 0, boundary 000000000000000000000000, end segment = 1, recycle through segment 128 2026-09-28 16:49:58.786 CST [76148] LOG: EVAN restoring replication = slot "rs" from disk 2026-09-28 16:49:58.787 CST [76148] LOG: EVAN computed replication slot = minimum LSN 0/0151FA80 2026-09-28 16:49:58.882 CST [76155] LOG: EVAN KeepLogSeg entry: end = 0/400000E8, slot minimum 0/0151FA80, current segment 1, input segment 1 2026-09-28 16:49:58.882 CST [76155] LOG: EVAN KeepLogSeg mapped slot = minimum to segment 0 2026-09-28 16:49:58.882 CST [76155] LOG: EVAN KeepLogSeg exit: = candidate segment 0, output segment 0 ``` The following messages are generated by the second server stop, as shown = by their timestamps: ``` 2026-09-28 16:50:00.899 CST [76146] LOG: EVAN checkpoint cleanup before = KeepLogSeg: redo 0/400000E8, end 0/40000170, segment 1 2026-09-28 16:50:00.899 CST [76146] LOG: EVAN KeepLogSeg entry: end = 0/40000170, slot minimum 0/0151FA80, current segment 1, input segment 1 2026-09-28 16:50:00.899 CST [76146] LOG: EVAN KeepLogSeg mapped slot = minimum to segment 0 ``` Here is where the problem begins. The slot=E2=80=99s restart_lsn is = 0/0151FA80. After wal_segment_size has been changed to 1 GB, this call = in KeepLogSeg(): ``` XLByteToSeg(keep, segno, wal_segment_size); ``` maps that LSN to segment zero. The slot=E2=80=99s restart_lsn refers to = the old WAL history and is no longer meaningful after pg_resetwal has = replaced that history: ``` 2026-09-28 16:50:00.899 CST [76146] LOG: EVAN KeepLogSeg exit: = candidate segment 0, output segment 0 2026-09-28 16:50:00.899 CST [76146] LOG: EVAN checkpoint cleanup after = KeepLogSeg: segment 0 2026-09-28 16:50:00.899 CST [76146] LOG: EVAN checkpoint cleanup before = decrement: segment 0 2026-09-28 16:50:00.899 CST [76146] LOG: EVAN checkpoint cleanup after = decrement: segment 18446744073709551615 ``` CreateCheckPoint() then makes the problem worse by unconditionally = decrementing _logSegNo. This decrement is normally required because = RemoveOldXlogFiles() removes files whose segment numbers are less than = or equal to the supplied boundary. However, because _logSegNo is already = 0 and XLogSegNo is unsigned, the decrement wraps to UINT64_MAX: ``` 2026-09-28 16:50:00.899 CST [76146] LOG: EVAN RemoveOldXlogFiles: = remove through segment 18446744073709551615, boundary = 00000000FFFFFFFF00000003, end segment 1, recycle through segment 2 2026-09-28 16:50:00.899 CST [76146] LOG: EVAN RemoveOldXlogFiles: = candidate 000000010000000000000001, next segment 1, recycle through = segment 2 2026-09-28 16:50:00.899 CST [76146] LOG: EVAN RemoveOldXlogFiles: = removing WAL segment 000000010000000000000001 2026-09-28 16:50:00.899 CST [76146] LOG: EVAN RemoveXlogFile: candidate = 000000010000000000000001, next segment 1, recycle through segment 2 2026-09-28 16:50:00.899 CST [76146] LOG: EVAN InstallXLogFileSegment: = renaming pg_wal/000000010000000000000001 to = pg_wal/000000010000000000000002 as segment 2 ``` Consequently, RemoveOldXlogFiles() incorrectly recycles = 000000010000000000000001 as 000000010000000000000002. Segment 1 is no = longer available under its expected name, even though it contains the = checkpoint created after pg_resetwal. The next startup therefore cannot = locate the checkpoint referenced by pg_control. Running pg_resetwal again creates another checkpoint and allows the = server to start again. However, this is only a temporary recovery. If = the stale slot remains, a later checkpoint cleanup can reproduce the = same failure. Actually, the repro must be run with assertions disabled to reach the = WAL recycling. I initially reproduced it that way because I had disabled = assertions in my sandbox while load testing another patch. With = assertions enabled, the second server stop fails earlier: ``` TRAP: failed Assert("!(possible_causes & RS_INVAL_WAL_REMOVED) || = oldestSegno > 0"), File: "slot.c", Line: 2230, PID: 32858 0 postgres 0x0000000104d538a0 = ExceptionalCondition + 216 1 postgres 0x0000000104a3b664 = InvalidateObsoleteReplicationSlots + 148 2 postgres 0x0000000104546fb0 = CreateCheckPoint + 3656 3 postgres 0x00000001045456a0 ShutdownXLOG = + 424 4 postgres 0x00000001049c0328 = CheckpointerMain + 2276 5 postgres 0x00000001049c5b98 = postmaster_child_launch + 464 6 postgres 0x00000001049cabd4 = StartChildProcess + 308 7 postgres 0x00000001049c9ae8 = PostmasterMain + 6128 8 postgres 0x000000010483c158 main + 924 9 dyld 0x00000001827ac4e4 start + 6992 2026-09-28 14:05:39.589 CST [32855] LOG: checkpointer process (PID = 32858) was terminated by signal 6: Abort trap: 6 ``` This shows that passing segment 0 to = InvalidateObsoleteReplicationSlots() violates the function=E2=80=99s = existing precondition. Changing wal_segment_size caused the stale restart_lsn in this repro to = map to segment 0, which exposed the underflow. However, the real problem = is that pg_resetwal discards the previous WAL history while leaving = replication slot state unchanged. The stored restart_lsn values are no = longer valid in the new WAL history. Even when the primary-startup = failure does not occur, an existing standby cannot resume replication = from the discarded history. Its slot must be recreated, and the standby = must be rebuilt from a fresh base backup. How to fix? My first thought was to prevent changing wal_segment_size = when replication slots exist. In that case, users would have to drop all = replication slots before changing wal_segment_size, otherwise, = pg_resetwal would fail with a hint. On second thought, I don't think we need to add this complexity to = pg_resetwal. When pg_resetwal is used to recover a cluster with = corrupted WAL or a corrupted control file, the doc already instructs = users to immediately dump the data, run initdb, and restore into a new = cluster. Replication slots do not play a meaningful role in that = recovery procedure. The relevant special case is using --wal-segsize to change the WAL = segment size of an otherwise sound cluster without running initdb. In = that case, existing replication slots retain restart_lsn values = referring to the discarded WAL history. As this repro demonstrates, = increasing the segment size can make such a value map to incorrect = segment numbers and trigger the failure. Since pg_resetwal is not frequently used, this problem is limited to = this special use of --wal-segsize, and the server can be recovered by = running pg_resetwal again and then immediately dropping the stale slots, = I think enhancing the doc is sufficient. The doc for --wal-segsize = should tell users to drop all replication slots before changing the = segment size. It should also explain that existing standbys cannot = resume replication afterward and must be rebuilt from a new base backup. See the attached v1 patch for my proposed documentation change. To answer R=C4=B1dvan's questions: > Questions: > 1. Is this behavior is expected? No. > 2. Should replication slot case mentioned in PostgreSQL documents? (I = checked yet could not see) I think so. > 3. Am I doing something out of order, unexpected? For a planned WAL segment-size change on a sound cluster, the standby = should first be stopped and the old replication slot should be dropped = before running pg_resetwal. If the slot was not dropped, it may be possible to start the primary = once and drop the slot before the next checkpoint or shutdown. The = standby must be stopped first so that it does not reacquire the slot. If = startup has already failed, another pg_resetwal may allow one more = startup, but the stale slot must then be removed immediately. > 4. Once I understood the case, I dropped replication slot and able to = start master. Now I want to copy m/global/pg_control to replica and = m/pg_wal to replica as well and complete wal segment size change. I = wonder if this way is documented or supported. I can say "it works" but = does not mean "supported or documented at all=E2=80=9D. AFAIK, no. Copying only global/pg_control and pg_wal does not produce a = consistent standby and is not a supported procedure. After the = primary=E2=80=99s WAL history has been reset, the standby should be = recreated from a fresh base backup and attached using a newly created = replication slot. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/ --Apple-Mail=_67E885E9-8230-4144-B126-FF9BBF515396 Content-Disposition: attachment; filename=temp_log.diff Content-Type: application/octet-stream; x-unix-mode=0644; name="temp_log.diff" Content-Transfer-Encoding: 7bit diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index 9ec0be77ca0..6c812ce7dd6 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -3711,6 +3711,8 @@ InstallXLogFileSegment(XLogSegNo *segno, char *tmppath, } Assert(access(path, F_OK) != 0 && errno == ENOENT); + elog(LOG, "EVAN InstallXLogFileSegment: renaming %s to %s as segment " UINT64_FORMAT, + tmppath, path, (uint64) *segno); if (durable_rename(tmppath, path, LOG) != 0) { LWLockRelease(ControlFileLock); @@ -3993,6 +3995,9 @@ RemoveOldXlogFiles(XLogSegNo segno, XLogRecPtr lastredoptr, XLogRecPtr endptr, * InsertTimeLineID isn't set, so we can't use that.) */ XLogFileName(lastoff, 0, segno, wal_segment_size); + elog(LOG, "EVAN RemoveOldXlogFiles: remove through segment " UINT64_FORMAT ", boundary %s, end segment " UINT64_FORMAT ", recycle through segment " UINT64_FORMAT, + (uint64) segno, lastoff, (uint64) endlogSegNo, + (uint64) recycleSegNo); elog(DEBUG2, "attempting to remove WAL segments older than log file %s", lastoff); @@ -4019,8 +4024,11 @@ RemoveOldXlogFiles(XLogSegNo segno, XLogRecPtr lastredoptr, XLogRecPtr endptr, */ if (strcmp(xlde->d_name + 8, lastoff + 8) <= 0) { + elog(LOG, "EVAN RemoveOldXlogFiles: candidate %s, next segment " UINT64_FORMAT ", recycle through segment " UINT64_FORMAT, + xlde->d_name, (uint64) endlogSegNo, (uint64) recycleSegNo); if (XLogArchiveCheckDone(xlde->d_name)) { + elog(LOG, "EVAN RemoveOldXlogFiles: removing WAL segment %s", xlde->d_name); /* Update the last removed location in shared memory first */ UpdateLastRemovedPtr(xlde->d_name); @@ -4129,6 +4137,8 @@ RemoveXlogFile(const struct dirent *segment_de, const char *segname = segment_de->d_name; snprintf(path, MAXPGPATH, XLOGDIR "/%s", segname); + elog(LOG, "EVAN RemoveXlogFile: candidate %s, next segment " UINT64_FORMAT ", recycle through segment " UINT64_FORMAT, + segname, (uint64) *endlogSegNo, (uint64) recycleSegNo); /* * Before deleting the file, see if it can be recycled as a future log @@ -8184,7 +8194,12 @@ CreateCheckPoint(int flags) * prevent the disk holding the xlog from growing full. */ XLByteToSeg(RedoRecPtr, _logSegNo, wal_segment_size); + elog(LOG, "EVAN checkpoint cleanup before KeepLogSeg: redo %X/%08X, end %X/%08X, segment " UINT64_FORMAT, + LSN_FORMAT_ARGS(RedoRecPtr), LSN_FORMAT_ARGS(recptr), + (uint64) _logSegNo); KeepLogSeg(recptr, &_logSegNo); + elog(LOG, "EVAN checkpoint cleanup after KeepLogSeg: segment " UINT64_FORMAT, + (uint64) _logSegNo); if (InvalidateObsoleteReplicationSlots(RS_INVAL_WAL_REMOVED | RS_INVAL_IDLE_TIMEOUT, _logSegNo, InvalidOid, InvalidTransactionId)) @@ -8196,7 +8211,11 @@ CreateCheckPoint(int flags) XLByteToSeg(RedoRecPtr, _logSegNo, wal_segment_size); KeepLogSeg(recptr, &_logSegNo); } + elog(LOG, "EVAN checkpoint cleanup before decrement: segment " UINT64_FORMAT, + (uint64) _logSegNo); _logSegNo--; + elog(LOG, "EVAN checkpoint cleanup after decrement: segment " UINT64_FORMAT, + (uint64) _logSegNo); RemoveOldXlogFiles(_logSegNo, RedoRecPtr, recptr, checkPoint.ThisTimeLineID); @@ -8921,9 +8940,14 @@ KeepLogSeg(XLogRecPtr recptr, XLogSegNo *logSegNo) /* Calculate how many segments are kept by slots. */ keep = XLogGetReplicationSlotMinimumLSN(); + elog(LOG, "EVAN KeepLogSeg entry: end %X/%08X, slot minimum %X/%08X, current segment " UINT64_FORMAT ", input segment " UINT64_FORMAT, + LSN_FORMAT_ARGS(recptr), LSN_FORMAT_ARGS(keep), + (uint64) currSegNo, (uint64) *logSegNo); if (XLogRecPtrIsValid(keep) && keep < recptr) { XLByteToSeg(keep, segno, wal_segment_size); + elog(LOG, "EVAN KeepLogSeg mapped slot minimum to segment " UINT64_FORMAT, + (uint64) segno); /* * Account for max_slot_wal_keep_size to avoid keeping more than @@ -8976,6 +9000,9 @@ KeepLogSeg(XLogRecPtr recptr, XLogSegNo *logSegNo) /* don't delete WAL segments newer than the calculated segment */ if (segno < *logSegNo) *logSegNo = segno; + + elog(LOG, "EVAN KeepLogSeg exit: candidate segment " UINT64_FORMAT ", output segment " UINT64_FORMAT, + (uint64) segno, (uint64) *logSegNo); } /* diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c index 63ce6d27885..93e3580631f 100644 --- a/src/backend/replication/slot.c +++ b/src/backend/replication/slot.c @@ -1357,6 +1357,8 @@ ReplicationSlotsComputeRequiredLSN(void) } LWLockRelease(ReplicationSlotControlLock); + elog(LOG, "EVAN computed replication slot minimum LSN %X/%08X", + LSN_FORMAT_ARGS(min_required)); XLogSetReplicationSlotMinimumLSN(min_required); } @@ -2437,6 +2439,8 @@ StartupReplicationSlots(void) } /* looks like a slot in a normal state, restore */ + elog(LOG, "EVAN restoring replication slot \"%s\" from disk", + replication_de->d_name); RestoreSlotFromDisk(replication_de->d_name); } FreeDir(replication_dir); --Apple-Mail=_67E885E9-8230-4144-B126-FF9BBF515396 Content-Disposition: attachment; filename=v1-0001-Document-replication-slot-handling-with-pg_resetw.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="v1-0001-Document-replication-slot-handling-with-pg_resetw.patch" Content-Transfer-Encoding: quoted-printable =46rom=204d04db3fbd78b1ab36a91ac8fd04ad2904ee089f=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20"Chao=20Li=20(Evan)"=20=0A= Date:=20Tue,=2029=20Sep=202026=2008:49:48=20+0800=0ASubject:=20[PATCH=20= v1]=20Document=20replication=20slot=20handling=20with=20pg_resetwal=0A=20= --wal-segsize=0AMIME-Version:=201.0=0AContent-Type:=20text/plain;=20= charset=3DUTF-8=0AContent-Transfer-Encoding:=208bit=0A=0AChanging=20the=20= WAL=20segment=20size=20discards=20the=20previous=20WAL=20history,=20= making=0Aexisting=20replication=20slots=20unusable.=20Document=20that=20= replication=20slots=20must=0Abe=20dropped=20before=20changing=20the=20= segment=20size=20and=20recreated=20afterward,=20and=0Athat=20standby=20= servers=20must=20be=20rebuilt=20from=20new=20base=20backups.=0A=0A= Reported-by:=20R=C4=B1dvan=20Korkmaz=20=0A= Author:=20Chao=20Li=20=0A---=0A=20= doc/src/sgml/ref/pg_resetwal.sgml=20|=208=20++++++++=0A=201=20file=20= changed,=208=20insertions(+)=0A=0Adiff=20--git=20= a/doc/src/sgml/ref/pg_resetwal.sgml=20= b/doc/src/sgml/ref/pg_resetwal.sgml=0Aindex=20d34aa26413c..6d3eeaecf06=20= 100644=0A---=20a/doc/src/sgml/ref/pg_resetwal.sgml=0A+++=20= b/doc/src/sgml/ref/pg_resetwal.sgml=0A@@=20-399,6=20+399,14=20@@=20= PostgreSQL=20documentation=0A=20=20=20=20=20=20=20= re-initdb.=0A=20=20=20=20=20=20=0A=20=0A+=20=20= =20=20=20=0A+=20=20=20=20=20=20Before=20changing=20the=20WAL=20= segment=20size,=20drop=20all=20replication=20slots.=0A+=20=20=20=20=20=20= Existing=20standby=20servers=20cannot=20continue=20replication=20after=20= the=20reset=0A+=20=20=20=20=20=20because=20the=20previous=20WAL=20= history=20has=20been=20discarded.=20=20After=20the=20reset,=0A+=20=20=20=20= =20=20recreate=20the=20replication=20slots=20and=20rebuild=20the=20= standby=20servers=20from=20new=0A+=20=20=20=20=20=20base=20backups.=0A+=20= =20=20=20=20=0A+=0A=20=20=20=20=20=20=0A=20=20=20=20=20=20=20= =0A=20=20=20=20=20=20=20=20While=20pg_resetwal=20= will=20set=20the=20WAL=20starting=20address=0A--=20=0A2.50.1=20(Apple=20= Git-155)=0A=0A= --Apple-Mail=_67E885E9-8230-4144-B126-FF9BBF515396 Content-Disposition: attachment; filename=repro_slot_wal_segsize.sh Content-Type: application/octet-stream; x-unix-mode=0755; name="repro_slot_wal_segsize.sh" Content-Transfer-Encoding: 7bit #!/usr/bin/env bash set -euo pipefail script_dir=$(cd "$(dirname "$0")" && pwd) source_dir=$(cd "$script_dir/../../../.." && pwd) pg_bindir=${PG_BINDIR:-"/usr/local/pgsql/bin"} old_seg_mb=${OLD_WAL_SEG_MB:-16} new_seg_mb=${NEW_WAL_SEG_MB:-1024} server_port=${PGPORT:-35433} test_root=${TEST_ROOT:-$(mktemp -d /tmp/pg-resetwal-slot.XXXXXX)} data_dir="$test_root/data" server_log="$test_root/server.log" reset_log="$test_root/pg_resetwal.log" for program in initdb pg_ctl pg_resetwal psql; do if [[ ! -x "$pg_bindir/$program" ]]; then echo "missing executable: $pg_bindir/$program" >&2 exit 1 fi done min_wal_mb=$((new_seg_mb * 2)) max_wal_mb=$((new_seg_mb * 4)) server_options="-p $server_port -k $test_root -c min_wal_size=${min_wal_mb}MB -c max_wal_size=${max_wal_mb}MB -c log_min_messages=log" echo "test directory: $test_root" echo "binaries: $pg_bindir" echo "WAL segment resize: ${old_seg_mb}MB -> ${new_seg_mb}MB" "$pg_bindir/initdb" -D "$data_dir" --no-sync --no-data-checksums \ "--wal-segsize=$old_seg_mb" "$pg_bindir/pg_ctl" -D "$data_dir" -l "$server_log" \ -o "$server_options" start "$pg_bindir/psql" -h "$test_root" -p "$server_port" postgres \ -v ON_ERROR_STOP=1 -c \ "SELECT * FROM pg_create_physical_replication_slot('rs', true); CHECKPOINT; SELECT slot_name, restart_lsn, pg_current_wal_lsn() FROM pg_replication_slots;" "$pg_bindir/pg_ctl" -D "$data_dir" -m fast stop "$pg_bindir/pg_resetwal" -D "$data_dir" \ "--wal-segsize=$new_seg_mb" 2>&1 | tee "$reset_log" "$pg_bindir/pg_ctl" -D "$data_dir" -l "$server_log" \ -o "$server_options" start "$pg_bindir/psql" -h "$test_root" -p "$server_port" postgres \ -v ON_ERROR_STOP=1 -c \ "SELECT pg_current_wal_lsn(), slot_name, restart_lsn, pg_walfile_name(restart_lsn) FROM pg_replication_slots;" # Add a short delay to distinguish the logs about the server stop sleep 2 "$pg_bindir/pg_ctl" -D "$data_dir" -m fast stop echo "WAL files after the first post-reset shutdown:" find "$data_dir/pg_wal" -maxdepth 1 -type f -print | sort if "$pg_bindir/pg_ctl" -D "$data_dir" -l "$server_log" \ -o "$server_options" start; then echo "second post-reset start unexpectedly succeeded" >&2 "$pg_bindir/pg_ctl" -D "$data_dir" -m fast stop exit 1 fi echo echo "EVAN trace:" grep 'EVAN' "$server_log" || true echo echo "Final startup failure:" grep -E 'invalid checkpoint record|could not locate a valid checkpoint record' \ "$server_log" || true echo echo "reproduction completed; files retained in $test_root" --Apple-Mail=_67E885E9-8230-4144-B126-FF9BBF515396--