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 1redVq-00CVj5-FZ for pgsql-hackers@arkaria.postgresql.org; Mon, 26 Feb 2024 16:07:30 +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 1redVp-00EogM-0W for pgsql-hackers@arkaria.postgresql.org; Mon, 26 Feb 2024 16:07:29 +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 1redVo-00EogE-NT for pgsql-hackers@lists.postgresql.org; Mon, 26 Feb 2024 16:07:29 +0000 Received: from m16.mail.163.com ([117.135.210.3]) by makus.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1redVi-001Ead-8t for pgsql-hackers@lists.postgresql.org; Mon, 26 Feb 2024 16:07:26 +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=pqGZTQwFO+J0CLDj+drF9bz/rGpEAxsJppfXB9RZIZQ=; b=F0zcBt8xiz7q81ue2jAvfJzMd4mUJA1Oxd34yqZzec3TqwKs7i0+gjHTxbQq4U WW0k2ZalM9Zhu6Waf3r2DWLjibgpZtGiDvEjJpOW6SOSS2enhd8gomoG27ZlxBwU PKIGPx5JBfm5FpH1+HdFZq+lXw8w5adhPhtx2LqRGmChU= Received: from 8235eee8a2a0 (unknown [140.205.118.32]) by gzga-smtp-mta-g1-2 (Coremail) with SMTP id _____wDnr1Att9xlh1SwAA--.43881S3; Tue, 27 Feb 2024 00:07:10 +0800 (CST) 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> <8734tmg88e.fsf@163.com> <7f4c8673-4c5d-4dbf-b8e5-0ec11cd2fba0@enterprisedb.com> <87h6i2djf9.fsf@163.com> <87jzms0xza.fsf@163.com> <87frxf1gld.fsf@163.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, 26 Feb 2024 23:29:33 +0800 In-reply-to: Message-ID: <875xyb1a6q.fsf@163.com> MIME-Version: 1.0 Content-Type: text/plain X-CM-TRANSID: _____wDnr1Att9xlh1SwAA--.43881S3 X-Coremail-Antispam: 1Uf129KBjvJXoWxCryUKw17Xr1xXFyfJryxKrg_yoWrJF1rpr WYga4a9r4kuFnrZw1vy3W8Xrnav345JFW5ArnYgw1UZws8JrW0kF4ayw4jgFW3Cr18A390 vF4jqrW5uaykAa7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zRpVbQUUUUU= X-Originating-IP: [140.205.118.32] X-CM-SenderInfo: x2klx3xlid0iqsrtqiywtou0bp/xtbBzwGQU2V4H9eM2wAEsL List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 2/26/24 14:22, Andy Fan wrote: >> >>... >> >>> Also, toasted values >>> are not always being used immediately and as a whole, i.e. jsonb values are fully >>> detoasted (we're working on this right now) to extract the smallest value from >>> big json, and these values are not worth keeping in memory. For text values too, >>> we often do not need the whole value to be detoasted. >> >> I'm not sure how often this is true, espeically you metioned text data >> type. I can't image why people acquire a piece of text and how can we >> design a toasting system to fulfill such case without detoast the whole >> as the first step. But for the jsonb or array data type, I think it is >> more often. However if we design toasting like that, I'm not sure if it >> would slow down other user case. for example detoasting the whole piece >> use case. I am justing feeling both way has its user case, kind of heap >> store and columna store. >> > > Any substr/starts_with call benefits from this optimization, and such > calls don't seem that uncommon. I certainly can imagine people doing > this fairly often. This leads me to pay more attention to pg_detoast_datum_slice user case which I did overlook and caused some regression in this case. Thanks! As you said later: > Is there a way to identify cases that are likely to benefit from this > slicing, and disable the detoasting for them? We already disable it for > other cases, so maybe we can do this here too? I think the answer is yes, if our goal is to disable the whole toast for some speical calls like substr/starts_with. In the current patch, we have: /* * increase_level_for_pre_detoast * Check if the given Expr could detoast a Var directly, if yes, * increase the level and return true. otherwise return false; */ static inline void increase_level_for_pre_detoast(Node *node, intermediate_level_context *ctx) { /* The following nodes is impossible to detoast a Var directly. */ if (IsA(node, List) || IsA(node, TargetEntry) || IsA(node, NullTest)) { ctx->level_added = false; } else if (IsA(node, FuncExpr) && castNode(FuncExpr, node)->funcid == F_PG_COLUMN_COMPRESSION) { /* let's not detoast first so that pg_column_compression works. */ ctx->level_added = false; } while it works, but the blacklist looks not awesome. > In any case, it's a long-standing behavior / > optimization, and I don't think we can just dismiss it this quickly. I agree. So I said both have their user case. and when I say the *both*, I mean the other method is "partial detoast prototype", which Nikita has told me before. while I am sure it has many user case, I'm also feeling it is complex and have many questions in my mind, but I'd like to see the design before asking them. > Or maybe there's a way to do the detoasting lazily, and only keep the > detoasted value when it's requesting the whole value. Or perhaps even > better, remember what part we detoasted, and maybe it'll be enough for > future requests? > > I'm not sure how difficult would this be with the proposed approach, > which eagerly detoasts the value into tts_values. I think it'd be > easier to implement with the TOAST cache I briefly described ... I can understand the benefits of the TOAST cache, but the following issues looks not good to me IIUC: 1). we can't put the result to tts_values[*] since without the planner decision, we don't know if this will break small_tlist logic. But if we put it into the cache only, which means a cache-lookup as a overhead is unavoidable. 2). It is hard to decide which entry should be evicted without attaching it to the TupleTableSlot's life-cycle. then we can't grantee the entry we keep is the entry we will reuse soon? -- Best Regards Andy Fan