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 1x0231-004PUq-2w for pgsql-hackers@arkaria.postgresql.org; Fri, 28 Aug 2026 19:15:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1x0230-008yxc-2S for pgsql-hackers@arkaria.postgresql.org; Fri, 28 Aug 2026 19:15:30 +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 1x0230-008yxA-1H for pgsql-hackers@lists.postgresql.org; Fri, 28 Aug 2026 19:15:30 +0000 Received: from mail-wr1-x431.google.com ([2a00:1450:4864:20::431]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1x022x-00000001lXM-3ftu for pgsql-hackers@postgresql.org; Fri, 28 Aug 2026 19:15:30 +0000 Received: by mail-wr1-x431.google.com with SMTP id ffacd0b85a97d-482f2ee53e7so582220f8f.1 for ; Fri, 28 Aug 2026 12:15:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1787944526; x=1788549326; darn=postgresql.org; h=message-id:date:content-type:mime-version:comments:references :in-reply-to:subject:cc:to:from:from:to:cc:subject:date:message-id :reply-to:content-type; bh=yK9Cxf+a7l2CQyzKaP9HjmtRuK6WpW1YGNxZA3yGtTU=; b=NYlY792auah5SA69ke25NBQT/G5EqmIxV+ErPkKYGARuokp+Kl9K8Xi0wY99aj8wO5 Te6pdm2CO00JZMZTXnifQ5UoGd4kKQMdU8gcHhlWwBn1JTx1E1z7k5zsXxxT/foZpR0x eCjC3eYXZi4aFeZDQHJSP7fDimTAzk67HBXvKrxIc3lYAEK2iHHBXGpgMIlgp/Ybxyw6 7HOCHoy0HHFfv/pq6s2iZudh9oado9Gj5YMaqFjHR8Ve/NOi+ffU8i46Y/wSo+KIpVz5 KOfkfv7W0Mtc5UPcOx8dA337L2ztCyiSZoy7+nR7nAfRMm5pmzbfVhUPiI2FteOyCBYF uPYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787944526; x=1788549326; h=message-id:date:content-type:mime-version:comments:references :in-reply-to:subject:cc:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to:content-type; bh=yK9Cxf+a7l2CQyzKaP9HjmtRuK6WpW1YGNxZA3yGtTU=; b=gP7fsLl4o3HnG2dP2Navi5ZYrHY4DYH0fGxpe9cwrdpRZIiCcjnnCGAekq3JrRXLhI LsBbUcbx4eEMkNk2rLzbfRRBHNZWnImg/3SQ/hUPq98cSZ7Cro58Hq7JV5Dnib9whVb7 HURVQNzFgC4YIpkr/iw50xrU+AFKXixn2r+YgLM8Qtp9LzWZUOcagTRWmalO5IqEBUFK tSAz4N8xqymPBkb6Eg0wjP60VQ5jpmEOko6j3WrlrBkSFxynOJHveuzRiclzmsS4GeS3 ZqyLerl3z95sX0AkP/ugV4M8FxgJiqm7x805Up07I9Ebk7TfZUqx6465So1fZjlVnL9y xqdg== X-Forwarded-Encrypted: i=1; AKwUvByylal1Zx6CQrkqjL1PPJB3BEhDvH9S0V+DnU7E9zOV8Lihojk8R9uGa+mfxQ0Y9RKpVU14dDc0HXP6AmVB@postgresql.org X-Gm-Message-State: AFuF++lzyRJ8lJZmw8Bz6ivd8kTCikoPixRG27sL1XzC2LuwAlO4Rt/k ccyocgCK1Nr5kFLPugTnXzjMVDerCNgiGxwM4VHOijtt7t9ab05Fzho5i9DylvoIfWJLevykKhs M3vYnGws= X-Gm-Gg: AYBFou1x0q3S5EoUovM0PpqBacgG7K68GvwIazX2SA3EIssg+2Jo9y2CEYpgmmi5wAe aL96obpdsggVmZHDhygANlTVEhtBAgwy5Ee/T5h+Gj2Xq0v8aXYngF5wMilu8ApjMbSIDec30Dk etNPiUgoZQpSKJ1jVc54vFrlqNmNLrq1TR+8s1z9bOXsAtyu4MT5OqQPcmfUCmWWJSlxqrimOK4 MW2NmjNKYtgzZOCWkrMboDl+Sr4CNWIYRRLzvlqDgwfXc9PzRXyOmmr1sFK5KMZAHm9AG0NX4XH oc2PAzaU16jT+6XKF17iXhCrVPJUKIcRCEXfilOnHqnqhUXd6zHVMo4IFPFydXa4/Mgpv9ACVm0 kT3YxQb/ZKQRpYvHC2Up0+Fs/VWVfhMYQEY9Nl3YFaXiNe/rLvMLh9I+26Lm2vYaeuabHIOm7ZL mARs4GAmqrDL5hI+Mz20LUg8396fdlpgTUeFbumLmQd/+6opPivU/Vc5kaZyp4m8iyJeEk2ZVzq Q== X-Received: by 2002:a5d:5f49:0:b0:483:ca4a:530c with SMTP id ffacd0b85a97d-483ca4a5654mr154397f8f.18.1787944526465; Fri, 28 Aug 2026 12:15:26 -0700 (PDT) Received: from localhost (109-81-170-190.rct.o2.cz. [109.81.170.190]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-482fbb20784sm5822484f8f.18.2026.08.28.12.15.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Aug 2026 12:15:26 -0700 (PDT) From: Antonin Houska To: Nathan Bossart cc: Fujii Masao , pgsql-hackers@postgresql.org, alvherre@kurilemu.de Subject: Re: REPACK (ANALYZE) within transaction block segfaults In-reply-to: References: Comments: In-reply-to Nathan Bossart message dated "Thu, 27 Aug 2026 14:39:35 -0500." X-Mailer: MH-E 8.6+git; nmh 1.8; GNU Emacs 28.3 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Date: Fri, 28 Aug 2026 21:15:25 +0200 Message-ID: <49398.1787944525@localhost> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Nathan Bossart wrote: > On Fri, Aug 28, 2026 at 01:09:59AM +0900, Fujii Masao wrote: > > On Thu, Aug 27, 2026 at 10:56=E2=80=AFPM Nathan Bossart > > wrote: > >> Presumably we need to handle transaction blocks a bit like how vacuum() > >> does. Or maybe even prevent REPACK (ANALYZE) within a transaction blo= ck. > >=20 > > I looked into this a bit. I think the problem is not ordinary > > transaction blocks themselves, but non-top-level execution, such as the > > DO block in the reproducer. > >=20 > > So the attached patch rejects only non-top-level REPACK (ANALYZE) > > commands. >=20 > Hm. Couldn't we do something like the in_outer_xact/use_own_xacts stuff = in > vacuum() to get it working instead? I think there are just two different concepts (for historical reasons?): vacuum_rel() expects no active transaction on entry, while cluster_rel() handles transaction boundaries on its own. Since REPACK (ANALYZE) is effectively VACUUM (FULL, ANALYZE), I'd prefer the same behavior, i.e. prohibiting execution both in a transaction block and i= n a function: postgres=3D# BEGIN; VACUUM (FULL, ANALYZE) t; END; BEGIN ERROR: VACUUM cannot run inside a transaction block ROLLBACK postgres=3D# DO $$ BEGIN EXECUTE 'VACUUM (FULL, ANALYZE) t'; END $$; ERROR: VACUUM cannot be executed from a function or procedure CONTEXT: SQL statement "VACUUM (FULL, ANALYZE) t" PL/pgSQL function inline_code_block line 1 at EXECUTE postgres=3D# BEGIN; REPACK (ANALYZE) t; END; BEGIN ERROR: REPACK (ANALYZE) cannot run inside a transaction block ROLLBACK postgres=3D# DO $$ BEGIN EXECUTE 'REPACK (ANALYZE) t'; END $$; ERROR: REPACK (ANALYZE) cannot be executed from a function or procedure CONTEXT: SQL statement "REPACK (ANALYZE) t" PL/pgSQL function inline_code_block line 1 at EXECUTE The attached patch does that. --=20 Antonin Houska Web: https://www.cybertec-postgresql.com --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Do-not-allow-REPACK-ANALYZE-in-function-and-in-trans.patch From b577bc4b61474b65a80b5ab8b5333c6ccd751e81 Mon Sep 17 00:00:00 2001 From: Antonin Houska Date: Fri, 28 Aug 2026 19:53:26 +0200 Subject: [PATCH] Do not allow REPACK (ANALYZE) in function and in transaction block. If REPACK (ANALYZE) is called from a pl/pgsql function, cluster_rel() might start a new transaction while SPI session is in progress. Use PreventInTransactionBlock() to avoid that. That function also raises error if REPACK (ANALYZE) is called in a transaction block, but that's fine: VACUUM (FULL, ANALYZE) - a synonym of REPACK (ANALYZE) - also raises ERROR in that case. --- src/backend/commands/repack.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c index 477c86b2ba6..81877029199 100644 --- a/src/backend/commands/repack.c +++ b/src/backend/commands/repack.c @@ -314,6 +314,20 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel) */ PreventInTransactionBlock(isTopLevel, "REPACK (CONCURRENTLY)"); } + else if ((params.options & CLUOPT_ANALYZE) != 0) + { + /* + * Technically, transaction block is not a problem for REPACK + * (ANALYZE), but if it's called from a pl/pgsql function, + * cluster_rel() might start a new transaction while SPI session is in + * progress. Make sure ERROR is raised instead. + * + * This way we also prohibit execution in a transaction block, but + * that's just consistent with VACUUM (FULL, ANALYZE), which is a + * synonym for REPACK (ANALYZE). + */ + PreventInTransactionBlock(isTopLevel, "REPACK (ANALYZE)"); + } /* * If a single relation is specified, process it and we're done ... unless -- 2.52.0 --=-=-=--