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 1tC1nt-009CZo-4U for pgsql-hackers@arkaria.postgresql.org; Fri, 15 Nov 2024 19:16: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 1tC1np-00BWG4-NS for pgsql-hackers@arkaria.postgresql.org; Fri, 15 Nov 2024 19:16: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 1tC1np-00BWFw-BP for pgsql-hackers@lists.postgresql.org; Fri, 15 Nov 2024 19:16:22 +0000 Received: from meesny.iki.fi ([195.140.195.201]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tC1nm-0024Mh-GJ for pgsql-hackers@postgresql.org; Fri, 15 Nov 2024 19:16:20 +0000 Received: from [192.168.1.110] (dsl-hkibng22-50ddb7-241.dhcp.inet.fi [80.221.183.241]) (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) (No client certificate requested) (Authenticated sender: hlinnaka) by meesny.iki.fi (Postfix) with ESMTPSA id 4Xqmvx4wDyzySZ; Fri, 15 Nov 2024 21:16:13 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=meesny; t=1731698174; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7wW/lCGQ1PL6YiLfe5+w9YrEAC66RMSZenkD034gNkg=; b=r6vw7w2HoDaKlKJFNAxJwYAK0XoIvzxP8RPYhejI4L3lmz4FKyOHW69lzrdQ7SGXZ4rrK2 3cWttmCGquF6uSYDCkceUIAdRunS39fgj+SRjGmvP6AtIFK/Er2jHlSYcIbKBAPBF4YP1K cbHV9MxOS46f/C8ibjXR0UNYD5pxgRE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=meesny; t=1731698174; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7wW/lCGQ1PL6YiLfe5+w9YrEAC66RMSZenkD034gNkg=; b=xWu6Qn06aN2T62Lcy81/HjVIZy08vQ8TlMpzdJ1XkVzIXBYGygGT5uvULP6l3tGkP8eHwe YlSgKjQpa284kXsWKCIayNIyX7wyMQ6gUeXkJkA3BXeqVuLjiczPjxXBhgw+k8C0F75wL1 7jVfOYdGc32WAfrTJ+VJFU9xPiYp3t0= ARC-Seal: i=1; s=meesny; d=iki.fi; t=1731698174; a=rsa-sha256; cv=none; b=sgShnYsQ2aL8I/y5K2d9NeTGpt8ofasKREX2BNpS5KFtO/iQ5kOgSKT8qRGXD9KGR+xUl3 foPClcz2fMOAiBoH//Gg9OzNYU976QDprdnsDq0NdSRTupvSlXgcQbSsc4elKqVSSZnTpU KaOxyISKTNGLnbSH3dYQVsWgVtr4rVU= ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi Message-ID: <39f9fe48-4db9-4daa-b4c5-c6f46ac92597@iki.fi> Date: Fri, 15 Nov 2024 21:16:13 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: CSN snapshots in hot standby From: Heikki Linnakangas To: Andres Freund Cc: "Andrey M. Borodin" , Kirill Reshke , pgsql-hackers References: <08da26cc-95ef-4c0e-9573-8b930f80ce27@iki.fi> <53C9CD5A-4403-4FF0-9F00-8B8A00F922A7@yandex-team.ru> <7cd97248-fd04-4fb2-9f7f-096c1d7660e8@iki.fi> Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 29/10/2024 18:33, Heikki Linnakangas wrote: > I added two tests to the test suite: >                                 master     patched > insert-all-different-xids:     0.00027    0.00019 s / iteration > insert-all-different-subxids:  0.00023    0.00020 s / iteration > > insert-all-different-xids: Open 1000 connections, insert one row in > each, and leave the transactions open. In the replica, select all the rows > > insert-all-different-subxids: The same, but with 1 transaction with 1000 > subxids. > > The point of these new tests is to test the scenario where the cache > doesn't help and just adds overhead, because each XID is looked up only > once. Seems to be fine. Surprisingly good actually; I'll do some more > profiling on that to understand why it's even faster than 'master'. Ok, I did some profiling and it makes sense: In the insert-all-different-xids test on 'master', we spend about 60& of CPU time in XidInMVCCSnapshot(), doing pg_lfind32() over the subxip array. We should probably sort the array and use a binary search if it's large or something... With these patches, instead of the pg_lfind32() over subxip array, we perform one CSN SLRU lookup instead, and the page is cached. There's locking overhead etc. with that, but it's still cheaper than the pg_lfind32(). In the insert-all-different-subxids test on 'master', the subxip array is overflowed, so we call SubTransGetTopmostTransaction() on each XID. That's performs two pg_subtrans lookups for each XID, first for the subxid, then for the parent. With these patches, we perform just one SLRU lookup, in pg_csnlog, which is faster. > Now the downside of this new cache: Since it has no size limit, if you > keep looking up different XIDs, it will keep growing until it holds all > the XIDs between the snapshot's xmin and xmax. That can take a lot of > memory in the worst case. Radix tree is pretty memory efficient, but > holding, say 1 billion XIDs would probably take something like 500 MB of > RAM (the radix tree stores 64-bit words with 2 bits per XID, plus the > radix tree nodes). That's per snapshot, so if you have a lot of 60& > connections, maybe even with multiple snapshots each, that can add up. > > I'm inclined to accept that memory usage. If we wanted to limit the size > of the cache, would need to choose a policy on how to truncate it > (delete random nodes?), what the limit should be etc. But I think it'd > be rare to hit those cases in practice. If you have a one billion XID > old transaction running in the primary, you probably have bigger > problems already. I'd love to hear some thoughts on this caching behavior. Is it acceptable to let the cache grow, potentially to very large sizes in the worst cases? Or do we need to make it more complicated and implement some eviction policy? -- Heikki Linnakangas Neon (https://neon.tech)