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.94.2) (envelope-from ) id 1uSLu4-006TZj-O4 for pgsql-hackers@arkaria.postgresql.org; Thu, 19 Jun 2025 20:30:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1uSLu2-00EdSG-QX for pgsql-hackers@arkaria.postgresql.org; Thu, 19 Jun 2025 20:30:31 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uSLu2-00EdS8-H3 for pgsql-hackers@lists.postgresql.org; Thu, 19 Jun 2025 20:30:31 +0000 Received: from mail-il1-x130.google.com ([2607:f8b0:4864:20::130]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uSLu1-002wmv-1E for pgsql-hackers@postgresql.org; Thu, 19 Jun 2025 20:30:30 +0000 Received: by mail-il1-x130.google.com with SMTP id e9e14a558f8ab-3de18fdeab0so12658105ab.3 for ; Thu, 19 Jun 2025 13:30:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750365028; x=1750969828; darn=postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=XqLYh86ibEOlINZKaC7LeRd4Cm5P1Vq3aS80hMCNtrY=; b=HiQWPYTWQ29nWjRr5ru3cGD+sow/kKB/eSITooPZC+gPY72Wx9loR4Naizu6Aq8+nN Vz/UNSgv/IRe0THx0iXTo+lvT1RoJcVWiRpbWH41ko9l09p/IS4glx2j3umwIi0IjUMG rU5/uqSjlOdNToKXghI31PEJSL0c+m2Vo9FofYCdVjqq37ir1rvNPvEeb0AbTXgsEyAp 0qU6ILOC285Tts2rmoh0Rd7KxFlPpPw1QiTYEnwvasMlPzb2F0dDkjcNoslu3DMwfU7h yYl/N83N+TgWBL3wuip7xSEQe906zOxXHDS6oTQ6ocSpV2QcgVysxF2MAINnpC1jwQ++ +/Gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750365028; x=1750969828; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=XqLYh86ibEOlINZKaC7LeRd4Cm5P1Vq3aS80hMCNtrY=; b=SSDw4xBLMCZO9VAsvI9RSwoZDuCcTqCC3BKygpASS4dMi/uh+Gr36dNGQLui4A83fD tT3i376pXrhU+fY0xPSRJ9g0QE+Mk0idAQ3ttXND+WDLcqghTzf92CwnUR8Qvem3CP6t leDAUmwDGFZ0TY3OgtlVBcjkP0Ea79vHV7NlXNDzuZIA5aqmmV/m/xHxjnPehLKFS09/ cKoEmPUKK9x7VMhWo/3a4/Es/+wppCA755x/gsKA88LEZ9QllZvrg/hddEYlajdL97fG DqI1/Cugo1wF4w3F5kIOgob/c2KfGX88sKJE+sCadfargel7WbB31MaWdui++0Jmd86y qgsg== X-Gm-Message-State: AOJu0YzVsatJyVL3AHlqe/qcobFsOQQFyv5psuYfx/jJjZ2VS7kYeEQ+ DkvY+GV7PyXFQwj+CDmFcYVVDrJcQqEx9Ty8zcyOkUF4cax0ZZ8BW9p4b8VISw== X-Gm-Gg: ASbGncv7KMk7W+l1c+2CkWUuDiAjSeWBLsNyAIlCLHrth2uKnQRetjF3ouJHyttPk3u /z1TZpatVJoXeHl6m9XMEXlODzM26uk0pFwBnxKrmrwFi4MjoxMIAc/WcBRMSKqQv1RWGTHHqzI A3ymlVD2CJjEBEXUK0DxO0vNUoBROip47ajIynlBBBOap8x6Eh2XgXTFqoKTX2zOa7QczSJeDf8 B9nV2Odr1MmfBS7DUN6IXTIbhqBaGb8bf2WuFyAeid3AnVv9hvpCf3uVsM/P1d5QKFBGyU0IIts Fz13414v+xIYAul9LkmmdMaszmEnfsrHI2zALxam4DS5sWRsLkgJt3Agzh+ZPBPk8tQjXQ5jJCL L/Ad+R6NHnJj1mKQdPaGQmM+b1SoxlSVX5tO3iB86LcxqE47tGlaK X-Google-Smtp-Source: AGHT+IFxeQInAKhe9n2tOmZCXL2aCNTk4tZvWvRx7QlcPz6XrCYvXSSr4hZyuaSFbE0Oa4BggyfL5w== X-Received: by 2002:a05:6e02:2502:b0:3dc:7d57:30a8 with SMTP id e9e14a558f8ab-3de38caae81mr2654525ab.10.1750365028599; Thu, 19 Jun 2025 13:30:28 -0700 (PDT) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id e9e14a558f8ab-3de376272ffsm2124535ab.22.2025.06.19.13.30.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Jun 2025 13:30:27 -0700 (PDT) Date: Thu, 19 Jun 2025 15:30:26 -0500 From: Nathan Bossart To: shihao zhong Cc: PostgreSQL-development Subject: Re: Fixes inconsistent behavior in vacuum when it processes multiple relations Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="kspI2SpvKnsc4o9Q" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --kspI2SpvKnsc4o9Q Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, Jun 18, 2025 at 02:48:16PM -0400, shihao zhong wrote: >>> That leads me to think (1) might be the better option, although I'm not too >>> wild about the subtlety of the fix. > > Thanks for your feedback. New patch is attached. I also updated the > signature of do_analyze_rel for the same reason. After thinking about this some more, I'm wondering if it would be better to pursue option (2) because it's a little less invasive (which is important because this will need to be back-patched). In any case, we have a similar problem when recursing to the TOAST table, which can be fixed by copying the params at the top of vacuum_rel(). While testing out the attached patch, I noticed a couple of other interesting problems in this area [0]. [0] https://postgr.es/m/aFRxC1W_kZU9OjJ9%40nathan -- nathan --kspI2SpvKnsc4o9Q Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=vacuum_params_v3.patch diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c index 33a33bf6b1c..a43f090ee17 100644 --- a/src/backend/commands/vacuum.c +++ b/src/backend/commands/vacuum.c @@ -634,7 +634,15 @@ vacuum(List *relations, VacuumParams *params, BufferAccessStrategy bstrategy, if (params->options & VACOPT_VACUUM) { - if (!vacuum_rel(vrel->oid, vrel->relation, params, bstrategy)) + VacuumParams params_copy; + + /* + * vacuum_rel() scribbles on the parameters, so give it a copy + * to avoid affecting other relations. + */ + memcpy(¶ms_copy, params, sizeof(VacuumParams)); + + if (!vacuum_rel(vrel->oid, vrel->relation, ¶ms_copy, bstrategy)) continue; } @@ -2008,9 +2016,16 @@ vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params, Oid save_userid; int save_sec_context; int save_nestlevel; + VacuumParams toast_vacuum_params; Assert(params != NULL); + /* + * This function scribbles on the parameters, so make a copy early to + * avoid affecting the TOAST table (if we do end up recursing to it). + */ + memcpy(&toast_vacuum_params, params, sizeof(VacuumParams)); + /* Begin a transaction for vacuuming this relation */ StartTransactionCommand(); @@ -2299,15 +2314,12 @@ vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params, */ if (toast_relid != InvalidOid) { - VacuumParams toast_vacuum_params; - /* * Force VACOPT_PROCESS_MAIN so vacuum_rel() processes it. Likewise, * set toast_parent so that the privilege checks are done on the main * relation. NB: This is only safe to do because we hold a session * lock on the main relation that prevents concurrent deletion. */ - memcpy(&toast_vacuum_params, params, sizeof(VacuumParams)); toast_vacuum_params.options |= VACOPT_PROCESS_MAIN; toast_vacuum_params.toast_parent = relid; --kspI2SpvKnsc4o9Q--