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.96) (envelope-from ) id 1wD17j-002WTd-2d for pgsql-hackers@arkaria.postgresql.org; Wed, 15 Apr 2026 14:21:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wD17e-00HLNK-30 for pgsql-hackers@arkaria.postgresql.org; Wed, 15 Apr 2026 14:21:43 +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.96) (envelope-from ) id 1wD12o-00GTZp-1x for pgsql-hackers@lists.postgresql.org; Wed, 15 Apr 2026 14:16:43 +0000 Received: from mail-pj1-x1029.google.com ([2607:f8b0:4864:20::1029]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wCxP4-00000001C6Y-3G7N for pgsql-hackers@lists.postgresql.org; Wed, 15 Apr 2026 10:23:28 +0000 Received: by mail-pj1-x1029.google.com with SMTP id 98e67ed59e1d1-35da2d35eccso4884842a91.0 for ; Wed, 15 Apr 2026 03:23:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776248604; x=1776853404; darn=lists.postgresql.org; h=to:date:message-id:subject:mime-version:from:from:to:cc:subject :date:message-id:reply-to; bh=3YGm2HvdkKqv7YEMSNFhgPmzkMV529EnRkxx7JltxsI=; b=cS8U1YZFoyRUumn1Mu8ZjYMZoiMEDu07o77jVYO+Di48x0jkqMN5XbpuhVCB0/BXl9 6f+QFZG8MZymGdstcO9Zo1eEJdVFl719QVyHEWNHQJQzkJj+bs1NYduM0zgxu2147kv0 mBDZ8IGC3Zfo7caOKxiZQyKHs6WF6XRPmjOYswo63ZYiK0TMb9J9e2NvHnfMxaLrYfIB 9ANHgGRy+Z1cYBnGtpyd5gUXNiFe+KURbXACxKo+teSD8mk+0T1NGB7TqrKiXb++oSTn g/FBN3HpcGFL2XBf5PgiKDsMEc2HHDXiFmx9inTyN4Qh96LsBdAwPxAZcH19iPKimjvk IsMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776248604; x=1776853404; h=to:date:message-id:subject:mime-version:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=3YGm2HvdkKqv7YEMSNFhgPmzkMV529EnRkxx7JltxsI=; b=OvErbWpVepwJiVgZnKp6heuZA5YeGovPGQGCbtSe8SDk2K/+tS84CjjRoa7bR1HUWG v0+bnaBuVtV1Wl8sJAJk3UXfjAQeP5IyVch/kGUN+pEVgKp43Cxd+lLLGRytEYr+SPgw luDMqnKrZUhrXu8jMqyRew1e3f58CqFpseObxuK92gayp9fDPIJOnnY2ayB6Y3EY5v0p h53oCZKLALKLTx+b5P29vsT8ipoKiO642g4+Bm7x1c09ylEoi/rLMC3lWZalv9IsAobv psDHUxV8EdVCFfWIUd3hmei6BU3INCpczScBa43kIz5nx+rA+w7jcplodsx/B86/qth8 1r/Q== X-Gm-Message-State: AOJu0Yxq2ujZqquW2nnxvNX2Oc3KKaRKitxOtebWTnYG5zsCS3WpolCw jk6sdSMj3B0H92riC5z5sbqDsAk7dqFOBu76RHwRgaVePA8IpspUWBO80m1qbyYSUvY= X-Gm-Gg: AeBDievdfJxnXBdDae4AME7wS/6v95nkZp5AqIuWB2gwlw9jfx7qhom4tzAjlHLcaV+ x8um6Y0e9vjteOhKzcNuMSF6JEnfCp/I3SRPZKlgrbfevfUthJasdEus5Li39TQlfumRBQe2zNj v0TN1YjDKXj6UhKxnXaeGqk+CHL+Aof4F8EONWnO1QsadQoVHQGRv82IWrpTS8RZtfyQ7l93FRb PN3fIqip8sBjqo7NzKsgaEawzYGuy66UVqK4HOPDW4DKNK/p3sM9jKrCM2KfBn1tlUwIeqV5nWd h6ltoeCalZDE7ew+PWPUHRQqWBTYxP9f3Y5AYdSXhR+CoObyOnZ4pJnbVxXPvM/hD9BV/AeBXLa skvO/TYj7wfoZrvNgtsKnUqLeCFevxfZNbeBmLYbNT++SUkiLM4oeqXYqwnD2Px2436TNkO8zcl gECpT/ociK3phrCu7h4IMDAaly85sUHk0ZplFT1ECsfg== X-Received: by 2002:a17:90b:2dc8:b0:35c:a8f:5c5f with SMTP id 98e67ed59e1d1-35e427708acmr22115480a91.8.1776248604131; Wed, 15 Apr 2026 03:23:24 -0700 (PDT) Received: from smtpclient.apple ([45.32.121.103]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-35fd20d0ec0sm2449186a91.10.2026.04.15.03.23.22 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 15 Apr 2026 03:23:23 -0700 (PDT) From: Chao Li Content-Type: multipart/mixed; boundary="Apple-Mail=_8E763899-1AE2-4FA4-AE46-2A71E6CD57A5" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.400.21\)) Subject: repack: fix uninitialized DecodingWorkerShared.initialized Message-Id: <9AFE5694-9420-40C3-842C-4A4473E1D02B@gmail.com> Date: Wed, 15 Apr 2026 18:22:44 +0800 To: Postgres hackers X-Mailer: Apple Mail (2.3864.400.21) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_8E763899-1AE2-4FA4-AE46-2A71E6CD57A5 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Hi, I have not reviewed the repack-related patches before. Recently, I = started trying to understand how repack works and trace through the = code. While tracing start_repack_decoding_worker(), I noticed something = suspicious: ``` seg =3D dsm_create(size, 0); shared =3D (DecodingWorkerShared *) dsm_segment_address(seg); shared->lsn_upto =3D InvalidXLogRecPtr; shared->done =3D false; SharedFileSetInit(&shared->sfs, seg); shared->last_exported =3D -1; SpinLockInit(&shared->mutex); shared->dbid =3D MyDatabaseId; ``` Here, the code creates a shared-memory segment and lets =E2=80=9Cshared" = point to that memory. It then initializes some fields of =E2=80=9Cshared".= However, later code reads shared->initialized, but this field was not = initialized: ``` for (;;) { bool initialized; SpinLockAcquire(&shared->mutex); initialized =3D shared->initialized; SpinLockRelease(&shared->mutex); if (initialized) break; ConditionVariableSleep(&shared->cv, = WAIT_EVENT_REPACK_WORKER_EXPORT); } ``` I checked the code of dsm_create(), and I did not see anything showing = that the created shared-memory segment is zeroed. This is actually just an eyeball finding. =46rom my tracing on my = MacBook, after shared =3D (DecodingWorkerShared *) = dsm_segment_address(seg);, the memory always seemed to be zeroed, so I = may have missed something. But given that the code explicitly = initializes several other fields, it seems better not to rely on that = implicitly. For the fix, since start_repack_decoding_worker() is not on a hot path, = I think it is fine to zero the whole shared struct explicitly, and then = initialize the non-zero fields afterwards. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/ --Apple-Mail=_8E763899-1AE2-4FA4-AE46-2A71E6CD57A5 Content-Disposition: attachment; filename=v1-0001-repack-zero-initialize-DecodingWorkerShared.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="v1-0001-repack-zero-initialize-DecodingWorkerShared.patch" Content-Transfer-Encoding: quoted-printable =46rom=20b9c22b95492bff66a511159ceffd37d6394cc5b1=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20"Chao=20Li=20(Evan)"=20=0A= Date:=20Wed,=2015=20Apr=202026=2016:46:20=20+0800=0ASubject:=20[PATCH=20= v1]=20repack:=20zero-initialize=20DecodingWorkerShared=0A=0AAuthor:=20= Chao=20Li=20=0AReviewed-by:=0ADiscussion:=20= https://postgr.es/m/=0A---=0A=20src/backend/commands/repack.c=20|=203=20= +--=0A=201=20file=20changed,=201=20insertion(+),=202=20deletions(-)=0A=0A= diff=20--git=20a/src/backend/commands/repack.c=20= b/src/backend/commands/repack.c=0Aindex=2058e3867246f..058b39a67ad=20= 100644=0A---=20a/src/backend/commands/repack.c=0A+++=20= b/src/backend/commands/repack.c=0A@@=20-3311,8=20+3311,7=20@@=20= start_repack_decoding_worker(Oid=20relid)=0A=20=09=09= BUFFERALIGN(REPACK_ERROR_QUEUE_SIZE);=0A=20=09seg=20=3D=20= dsm_create(size,=200);=0A=20=09shared=20=3D=20(DecodingWorkerShared=20*)=20= dsm_segment_address(seg);=0A-=09shared->lsn_upto=20=3D=20= InvalidXLogRecPtr;=0A-=09shared->done=20=3D=20false;=0A+=09= memset(shared,=200,=20sizeof(DecodingWorkerShared));=0A=20=09= SharedFileSetInit(&shared->sfs,=20seg);=0A=20=09shared->last_exported=20= =3D=20-1;=0A=20=09SpinLockInit(&shared->mutex);=0A--=20=0A2.50.1=20= (Apple=20Git-155)=0A=0A= --Apple-Mail=_8E763899-1AE2-4FA4-AE46-2A71E6CD57A5--