Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iVEkC-0001Q2-O5 for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Nov 2019 13:01:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iVEkB-0000RW-JG for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Nov 2019 13:01:03 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iVEkB-0000QQ-1j for pgsql-hackers@lists.postgresql.org; Thu, 14 Nov 2019 13:01:03 +0000 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iVEk7-0001wa-Qy for pgsql-hackers@postgresql.org; Thu, 14 Nov 2019 13:01:01 +0000 Received: by mail-wm1-x344.google.com with SMTP id l17so5582393wmh.0 for ; Thu, 14 Nov 2019 05:00:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=lY0aiG/Mf+blTJ9TeOfdIeVUH8pzoKkPKW6DgaKI64w=; b=Kbtc+dBTdGQRjeVVf+UabsZwb72CW3i9iYZ+skCbZrdKu59urh+5+3Fa52Utt3vQNB 5GjuhRPw5Ob6TlrW3xRKdrP6HIQpvOT7r4rS3SAN5F5Fsq3fCNvp28fottt/ItEVRR2D YQBobn30FLh1ONtMesQ13e3M//FwhUh1W7ay7ld5zno6QfiGaSpDWod6QnPzjt1M6kvl dHDzOY2tL35TFxB5hUQCge50gv5gjXxJDHFQta49di0aAXPxbdh0lkyEZeB/zhuxOTh/ al69ERtJpwv4w43E7Ik8RSNXkMXq/aVOrvNzLixc//lL5GXuMjZKyHA6f0fllb1MshQV aZRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=lY0aiG/Mf+blTJ9TeOfdIeVUH8pzoKkPKW6DgaKI64w=; b=H/BuZzG8F2wHOh0jWCdqS5mLh9vmgsoQVjXPYRN+uK0sZgJkAk8Ixy1lAXsb4wdMIZ LgBB7nGDM+fBURMZYlTpuI6J+jFDpwWRWFEINg5VTJF/bUUy61LKoYEluqAxd/PClA9b 7sMlNF5QggiVGNhgCNgH1aUxltWzX8fS9bE1kXqyCTjwgjAzGKAzOvbCMBHl8M6wlnP9 07JuFIeD/oMjMYmua46Y021muvLFYesYbt7a3UmcBSj+f54L/32OB9LHFaNIt1oEzgR2 rOiACM883vf6tWhml7cB+76E4DPgEM93qKVhCVrTkKxadINmhO7FF7odqoXBFPvVrO8K vYgg== X-Gm-Message-State: APjAAAVmBqn1oN2vXoN0iMTD2evjzIm76Jd1zM9lFZ6QasYcjjzPlB12 GiP/gZD80zv2c6dDneo5UbY4Dw== X-Google-Smtp-Source: APXvYqzXo3fHKifDDkCtrVjrbURvRLBvM3w7BRTD4hflBZgp8j+N4m/oZWxB4kG5Op429tfwoyhz0w== X-Received: by 2002:a1c:9e58:: with SMTP id h85mr8151689wme.77.1573736458278; Thu, 14 Nov 2019 05:00:58 -0800 (PST) Received: from localhost (static-84-42-175-93.net.upcbroadband.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id a2sm3368315wrt.79.2019.11.14.05.00.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Nov 2019 05:00:57 -0800 (PST) Date: Thu, 14 Nov 2019 14:00:55 +0100 From: Tomas Vondra To: Rushabh Lathia Cc: Tom Lane , Andrey Borodin , Alvaro Herrera , Binguo Bao , Paul Ramsey , Pgsql Hackers Subject: Re: Optimize partial TOAST decompression Message-ID: <20191114130055.dkrjcveigeviil7n@development> References: <20190930172951.gdedvexnf4d2wv5e@development> <123EF56B-F8EC-4868-B49B-095795095E7A@yandex-team.ru> <20191001100805.wnnuj73kuzfwzs56@development> <20191001123420.uwzxtqr7qvleyxef@development> <20191001131803.j6uin7nho7t6vxzy@development> <4411.1569939037@sss.pgh.pa.us> <20191001145758.rofbwvguwxwpdxyb@development> <2093.1569944075@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="h5nvyo6telceqdoh" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --h5nvyo6telceqdoh Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline On Thu, Nov 14, 2019 at 03:27:42PM +0530, Rushabh Lathia wrote: >Today I noticed strange behaviour, consider the following test: > >postgres@126111=#create table foo ( a text ); >CREATE TABLE >postgres@126111=#insert into foo values ( repeat('PostgreSQL is the >world''s best database and leading by an Open Source Community.', 8000)); >INSERT 0 1 > >postgres@126111=#select substring(a from 639921 for 81) from foo; > substring >----------- > >(1 row) > Hmmm. I think the issue is heap_tuple_untoast_attr_slice is using the wrong way to determine compressed size in the VARATT_IS_EXTERNAL_ONDISK branch. It does this max_size = pglz_maximum_compressed_size(sliceoffset + slicelength, TOAST_COMPRESS_SIZE(attr)); But for the example you've posted TOAST_COMPRESS_SIZE(attr) returns 10, which is obviously bogus because the TOAST table contains ~75kB of data. I think it should be doing this instead: max_size = pglz_maximum_compressed_size(sliceoffset + slicelength, toast_pointer.va_extsize); At least that fixes it for me. I wonder if this actually explains the crashes 540f3168091 was supposed to fix, but it just masked them instead. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --h5nvyo6telceqdoh Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="detoasting.patch" diff --git a/src/backend/access/common/detoast.c b/src/backend/access/common/detoast.c index 47a03fa98b..b796406239 100644 --- a/src/backend/access/common/detoast.c +++ b/src/backend/access/common/detoast.c @@ -233,7 +233,7 @@ heap_tuple_untoast_attr_slice(struct varlena *attr, * of a given length (after decompression). */ max_size = pglz_maximum_compressed_size(sliceoffset + slicelength, - TOAST_COMPRESS_SIZE(attr)); + toast_pointer.va_extsize); /* * Fetch enough compressed slices (compressed marker will get set --h5nvyo6telceqdoh--