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 1tuvv2-00EVLG-4x for pgsql-hackers@arkaria.postgresql.org; Wed, 19 Mar 2025 16:05:24 +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 1tuvv0-007CFT-0M for pgsql-hackers@arkaria.postgresql.org; Wed, 19 Mar 2025 16:05:22 +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 1tuvuz-007CFK-G7 for pgsql-hackers@lists.postgresql.org; Wed, 19 Mar 2025 16:05:21 +0000 Received: from mail.postgrespro.ru ([93.174.132.70]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tuvuw-003j1J-0M for pgsql-hackers@lists.postgresql.org; Wed, 19 Mar 2025 16:05:20 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1742400314; bh=HvDvTkeFnIFl0v8eCFVaRzyPxFN7TQmDJI6NyFEY5Ao=; h=Message-ID:Date:User-Agent:Subject:To:Cc:References:From: In-Reply-To:From; b=U1e6pv3NfGAnt9jjemjqkhFS68GUWlS7rU3mh7+z/fRcPVS1RoMx1u+kt8EYPy3KA 9+0l0C5K8OIXBaDBWNSzvZK7DA8ZI+UefKqHNjVznlB19ZAGKLJ8udlAOVl3ASoOka 4wkg9jVT5Th2XwQrz8+m1oUY2GIFAUFEjNOpgUNmVDk6j9HAJa0Te9MeymSNL0VdWd 5k6yOzjhDLn3llJ9RL4nDCXlcYfjLx2wGhyjeQv9+CNuxWdayNEhrqFjnTzIZTintc CE4R0u6G2kOpDi8CcoAl2qdxS8QuYp5852LmKWq0jRyFqH5e+fzH0oPXWNbrw9oMUs T8KAFI0BXthGQ== Received: from [172.30.50.50] (unknown [93.174.131.138]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (Client did not present a certificate) (Authenticated sender: y.sokolov@postgrespro.ru) by mail.postgrespro.ru (Postfix/465) with ESMTPSA id 5EE216024B; Wed, 19 Mar 2025 19:05:14 +0300 (MSK) Message-ID: <0be607a5-20f6-4894-bb22-412e3c784bc2@postgrespro.ru> Date: Wed, 19 Mar 2025 19:05:13 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Optimize truncation logic to reduce AccessExclusive lock impact To: David Rowley , Stepan Neretin Cc: PostgreSQL Hackers References: Content-Language: en-US From: Yura Sokolov In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-KSMG-AntiPhishing: NotDetected X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.1.0.7854, bases: 2025/03/19 09:15:00 #27801794 X-KSMG-AntiVirus-Status: NotDetected, skipped X-KSMG-LinksScanning: not scanned, disabled by settings X-KSMG-Message-Action: skipped X-KSMG-Rule-ID: 1 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 19.03.2025 05:09, David Rowley wrote: > On Tue, 18 Mar 2025 at 19:04, Stepan Neretin wrote: >> We propose modifying the truncation condition in should_attempt_truncation to avoid unnecessary full buffer scans. The new formula ensures we only attempt truncation when we can free at least 3% of the relation size + 2 pages. This change prevents excessive truncation attempts on small tables while reducing the impact on larger relations. Previously, small tables could theoretically be truncated to a single page, but with this change, they may remain around 3 pages instead. We don't see this as a significant issue. > > I'm just following this code through. Looking at > DropRelationBuffers(), d6ad34f34 added an optimisation so that we can > skip scanning all of shared buffers while in recovery (where > smgrnblocks_cached() can return a non-InvalidBlockNumber value), which > the following code might decide to lookup the buffers one-by-one > rather than scanning the entire buffer pool: > > if (BlockNumberIsValid(nBlocksToInvalidate) && > nBlocksToInvalidate < BUF_DROP_FULL_SCAN_THRESHOLD) > { > for (j = 0; j < nforks; j++) > FindAndDropRelationBuffers(rlocator.locator, forkNum[j], > nForkBlock[j], firstDelBlock[j]); > return; > } > > While I don't know this code too well, from a quick look, it seem to > me that since DropRelationBuffers() is being called from > smgrtruncate() in this case, smgrtruncate() has access to old_nblocks > so it knows the old size of the relation. Couldn't we do something > like have another version of DropRelationBuffers() which accepts a > parameter for the old number of blocks and uses that instead of > calling smgrnblocks_cached()? That would allow the > FindAndDropRelationBuffers() optimisation to be used in cases where > the buffers to truncate is small or shared buffers is large. Yes, it will work for smaller relations. But it doesn't fix issue with large relations: - imho, there is no big need to truncate 2TB table to release every 8MB of free space, but it happens now. So, probably we should do both: - improve truncate of smaller relations - reduce frequency of truncation larger relations. I believe, suggested patch is acceptable in its form and will not harm after optimization for small relations will be implemented. (Disclaimer: I'm the author of the patch.) > As for the patch being proposed, it looks like you're maybe just > showing the answer to the question left in the comment below is "Yes". > > /* > * Space/time tradeoff parameters: do these need to be user-tunable? Well, the patch changes formula as well. It is harder to implement user-tunable formula. ------- regards Yura Sokolov