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 1rcUee-000P8N-9G for pgsql-hackers@arkaria.postgresql.org; Tue, 20 Feb 2024 18:15:44 +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 1rcUeb-0016Kj-Sa for pgsql-hackers@arkaria.postgresql.org; Tue, 20 Feb 2024 18:15:42 +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.94.2) (envelope-from ) id 1rcUeb-0016Kb-HD for pgsql-hackers@lists.postgresql.org; Tue, 20 Feb 2024 18:15:42 +0000 Received: from mail-ed1-x536.google.com ([2a00:1450:4864:20::536]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rcUeY-0002jc-2d for pgsql-hackers@lists.postgresql.org; Tue, 20 Feb 2024 18:15:40 +0000 Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-564d95d64e1so864891a12.3 for ; Tue, 20 Feb 2024 10:15:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1708452936; x=1709057736; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=+hRkTgpQ6iPgt4IdXUmKUkFxanGGWXOyvrQ9JW3B/p4=; b=L27ajnRmlwfIoHq5wiYAWyNNTYFWhDejl+z09Bx5FtaStA8VuMNk6XAkcbOPqDqp/e ITxEM/pw2qy/evCJgSjaek7t2vJekok5AsO21t+VkW7C2tbHTRxZZwhJmgYCb8UVTGos z78HOtguwJhb6G/v950yNtk/HLCZwZXQlcHiFgL4zCuFY4+bjI8Mpfy7YFACaENioPtv cPlG6khmvZk15safiIA7YRZ9QhyQZwf3PsWJhBIof2oKN2ij+3ASSbNOCBOCpVuvQlrR Oed4nHd/wp6ZIYflvzC9IuksniOCTZ7gvG56mRCnyKGe/sCkzXFxL3U79Iqi7tVRq78Z eABw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708452936; x=1709057736; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=+hRkTgpQ6iPgt4IdXUmKUkFxanGGWXOyvrQ9JW3B/p4=; b=UrV7NGQKS75+UiEnOtUrVLfi2ldVmIff2cMRN+IT9DfJurPiy8ake890YfpMQLiKPH Q2tFEUPni+DUUDafIu9dzB9Z+LjA+1a7FXEojx7ogTU3x9VtITbIDTZoXdbaGAtrnQEW 1YB24Hholt+lC5DKyEOnREK1cfgmag5/Xz/bJ7kOiCYV9GRZP2t560hIUtW9Wse+OhIs uZUm8BwRP4GtTrvJJcg0Ypv3D1qDmsKOJT8xAxJ9bwSP6UUM7WzD18MAdX/gNsCSfEu7 vOs5JkgD1/ALGJGLhj6yYLoCHrQvbxGFY7xTMX4HlJBP1hIuPiUAsZUAQ8PrDs/wdQoK bIXQ== X-Forwarded-Encrypted: i=1; AJvYcCU1bgteQtKduV8HCHLRSLbfbohziBRzmRkBcnXIajlJY/6i7yRyAqXJaFkuMaXvJNa2q5MOyLvg/Cz08IiIrg2nCv/v5LETgvJvG6QP6oyCxtwO X-Gm-Message-State: AOJu0YzF7mA0YheW+Q+EPMgpHRynaCsCtLKaSQ5JwLbHA9FQC5J/7jaj LL1z/VAZvhMKe3y1oArNwIYgPbRf+DLXaf7e6eISIzuYjbTUOFBgilR1mHXeDA== X-Google-Smtp-Source: AGHT+IHI8Eugc3nPNKolXS8OaWaRuIFc7QrjY/hzFkP41PRcFFvThFNQNii+vXGYW0CMQmy2iY9L2Q== X-Received: by 2002:a05:6402:270b:b0:564:ca35:b146 with SMTP id y11-20020a056402270b00b00564ca35b146mr2845086edd.8.1708452936538; Tue, 20 Feb 2024 10:15:36 -0800 (PST) Received: from [10.137.0.18] (ip-86-49-229-30.bb.vodafone.cz. [86.49.229.30]) by smtp.gmail.com with ESMTPSA id bf2-20020a0564021a4200b00562149c7bf4sm3833467edb.48.2024.02.20.10.15.35 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 20 Feb 2024 10:15:36 -0800 (PST) Message-ID: Date: Tue, 20 Feb 2024 19:15:34 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Shared detoast Datum proposal Content-Language: en-US To: Andy Fan Cc: Michael Zhilin , Peter Smith , vignesh C , Matthias van de Meent , Nikita Malakhov , pgsql-hackers@lists.postgresql.org References: <87il4jrk1l.fsf@163.com> <87h6jxw41k.fsf@163.com> <87sf3andy2.fsf@163.com> <87wmskm9qu.fsf@163.com> <87v87kvboq.fsf@163.com> <71321d83-391b-473b-aaf6-618c7f682b96@postgrespro.ru> <87r0i7vos3.fsf@163.com> <874je3hcwn.fsf@163.com> From: Tomas Vondra In-Reply-To: <874je3hcwn.fsf@163.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, I took a quick look on this thread/patch today, so let me share a couple initial thoughts. I may not have a particularly coherent/consistent opinion on the patch or what would be a better way to do this yet, but perhaps it'll start a discussion ... The goal of the patch (as I understand it) is essentially to cache detoasted values, so that the value does not need to be detoasted repeatedly in different parts of the plan. I think that's perfectly sensible and worthwhile goal - detoasting is not cheap, and complex plans may easily spend a lot of time on it. That being said, the approach seems somewhat invasive, and touching parts I wouldn't have expected to need a change to implement this. For example, I certainly would not have guessed the patch to need changes in createplan.c or setrefs.c. Perhaps it really needs to do these things, but neither the thread nor the comments are very enlightening as for why it's needed :-( In many cases I can guess, but I'm not sure my guess is correct. And comments in code generally describe what's happening locally / next line, not the bigger picture & why it's happening. IIUC we walk the plan to decide which Vars should be detoasted (and cached) once, and which cases should not do that because it'd inflate the amount of data we need to keep in a Sort, Hash etc. Not sure if there's a better way to do this - it depends on what happens in the upper parts of the plan, so we can't decide while building the paths. But maybe we could decide this while transforming the paths into a plan? (I realize the JIT thread nearby needs to do something like that in create_plan, and in that one I suggested maybe walking the plan would be a better approach, so I may be contradicting myself a little bit.). In any case, set_plan_forbid_pre_detoast_vars_recurse should probably explain the overall strategy / reasoning in a bit more detail. Maybe it's somewhere in this thread, but that's not great for reviewers. Similar for the setrefs.c changes. It seems a bit suspicious to piggy back the new code into fix_scan_expr/fix_scan_list and similar code. Those functions have a pretty clearly defined purpose, not sure we want to also extend them to also deal with this new thing. (FWIW I'd 100%% did it this way if I hacked on a PoC of this, to make it work. But I'm not sure it's the right solution.) I don't know what to thing about the Bitset - maybe it's necessary, but how would I know? I don't have any way to measure the benefits, because the 0002 patch uses it right away. I think it should be done the other way around, i.e. the patch should introduce the main feature first (using the traditional Bitmapset), and then add Bitset on top of that. That way we could easily measure the impact and see if it's useful. On the whole, my biggest concern is memory usage & leaks. It's not difficult to already have problems with large detoasted values, and if we start keeping more of them, that may get worse. Or at least that's my intuition - it can't really get better by keeping the values longer, right? The other thing is the risk of leaks (in the sense of keeping detoasted values longer than expected). I see the values are allocated in tts_mcxt, and maybe that's the right solution - not sure. FWIW while looking at the patch, I couldn't help but to think about expanded datums. There's similarity in what these two features do - keep detoasted values for a while, so that we don't need to do the expensive processing if we access them repeatedly. Of course, expanded datums are not meant to be long-lived, while "shared detoasted values" are meant to exist (potentially) for the query duration. But maybe there's something we could learn from expanded datums? For example how the varlena pointer is leveraged to point to the expanded object. For example, what if we add a "TOAST cache" as a query-level hash table, and modify the detoasting to first check the hash table (with the TOAST pointer as a key)? It'd be fairly trivial to enforce a memory limit on the hash table, evict values from it, etc. And it wouldn't require any of the createplan/setrefs changes, I think ... regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company