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 1rh7Ht-009gl0-2g for pgsql-hackers@arkaria.postgresql.org; Mon, 04 Mar 2024 12:19:21 +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 1rh7Hr-007PMO-24 for pgsql-hackers@arkaria.postgresql.org; Mon, 04 Mar 2024 12:19:19 +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 1rh7Hq-007PMF-Oz for pgsql-hackers@lists.postgresql.org; Mon, 04 Mar 2024 12:19:19 +0000 Received: from m15.mail.163.com ([45.254.50.220]) by makus.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rh7Hg-002nLf-Lo for pgsql-hackers@lists.postgresql.org; Mon, 04 Mar 2024 12:19:17 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-ID:MIME-Version: Content-Type; bh=JkICKzAQdqkZe2Ak8Rsykqm9ZU/RjuSf/ssADrJSEBs=; b=LgLu6lMboXqHxvNV/iXjr9mzDwpXoIq4tz5QwDZddvbTeX+YlxvbT7uvbip0fB KPzox6EIit1pL5ZfjSn7keJzIJiXU2IC/q9Xj7KtrvaG0Fp+OlEQIom9tSJ3MqSl xQiuIWEPf2F7o28GT9pfQal8xtQVKipfkpRVvBh71OfE8= Received: from 8235eee8a2a0 (unknown [140.205.118.136]) by gzga-smtp-mta-g0-0 (Coremail) with SMTP id _____wDnr54kvOVlx3AxAQ--.29962S3; Mon, 04 Mar 2024 20:18:45 +0800 (CST) References: <87il4jrk1l.fsf@163.com> <87v87kvboq.fsf@163.com> <71321d83-391b-473b-aaf6-618c7f682b96@postgrespro.ru> <87r0i7vos3.fsf@163.com> <874je3hcwn.fsf@163.com> <8734tmg88e.fsf@163.com> <7f4c8673-4c5d-4dbf-b8e5-0ec11cd2fba0@enterprisedb.com> <87h6i2djf9.fsf@163.com> <87jzms0xza.fsf@163.com> <87frxf1gld.fsf@163.com> <871q8z19ux.fsf@163.com> <87msrgdq93.fsf@163.com> <87frx7et1j.fsf@163.com> <87v862dbht.fsf@163.com> <6718759c-2dac-48e4-bf18-282de4d82204@enterprisedb.com> User-agent: mu4e 1.10.7; emacs 29.1 From: Andy Fan To: Tomas Vondra Cc: Nikita Malakhov , Michael Zhilin , Peter Smith , vignesh C , Matthias van de Meent , pgsql-hackers@lists.postgresql.org Subject: Re: Shared detoast Datum proposal Date: Mon, 04 Mar 2024 20:13:20 +0800 In-reply-to: <6718759c-2dac-48e4-bf18-282de4d82204@enterprisedb.com> Message-ID: <87a5nechqz.fsf@163.com> MIME-Version: 1.0 Content-Type: text/plain X-CM-TRANSID: _____wDnr54kvOVlx3AxAQ--.29962S3 X-Coremail-Antispam: 1Uf129KBjvJXoW7AFyUKryfCry7Wr1UZryUtrb_yoW8Kw4fpr WfJFy29r4DArn5twn2g3Wvyw1fC395Kry3Grn8J34xuFZ0kryIkrWjgF409rWqgr48ZryY qF4jqw4DWa45AFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zRpwZ3UUUUU= X-Originating-IP: [140.205.118.136] X-CM-SenderInfo: x2klx3xlid0iqsrtqiywtou0bp/1tbiQBeXU2VOBuze0gAAse List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Tomas Vondra writes: >>> But I'm a bit surprised the patch needs to pass a >>> MemoryContext to so many places as a function argument - that seems to >>> go against how we work with memory contexts. Doubly so because it seems >>> to only ever pass CurrentMemoryContext anyway. So what's that about? >> >> I think you are talking about the argument like this: >> >> /* ---------- >> - * detoast_attr - >> + * detoast_attr_ext - >> * >> * Public entry point to get back a toasted value from compression >> * or external storage. The result is always non-extended varlena form. >> * >> + * ctx: The memory context which the final value belongs to. >> + * >> * Note some callers assume that if the input is an EXTERNAL or COMPRESSED >> * datum, the result will be a pfree'able chunk. >> * ---------- >> */ >> >> +extern struct varlena * >> +detoast_attr_ext(struct varlena *attr, MemoryContext ctx) >> >> This is mainly because 'detoast_attr' will apply more memory than the >> "final detoast datum" , for example the code to scan toast relation >> needs some memory as well, and what I want is just keeping the memory >> for the final detoast datum so that other memory can be released sooner, >> so I added the function argument for that. >> > > What exactly does detoast_attr allocate in order to scan toast relation? > Does that happen in master, or just with the patch? It is in the current master, for example the TupleTableSlot creation needed by scanning the toast relation needs a memory allocating. > If with master, I > suggest to ignore that / treat that as a separate issue and leave it for > a different patch. OK, I can make it as a seperate commit in the next version. > In any case, the custom is to allocate results in the context that is > set in CurrentMemoryContext at the moment of the call, and if there's > substantial amount of allocations that we want to free soon, we either > do that by explicit pfree() calls, or create a small temporary context > in the function (but that's more expensive). > > I don't think we should invent a new convention where the context is > passed as an argument, unless absolutely necessary. Hmm, in this case, if we don't add the new argument, we have to get the detoast datum in Context-1 and copy it to the desired memory context, which is the thing I want to avoid. I think we have a same decision to make on the TOAST cache method as well. -- Best Regards Andy Fan