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 1iFI2d-0005Wc-9t for pgsql-hackers@arkaria.postgresql.org; Tue, 01 Oct 2019 13:18:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iFI2c-00021e-6E for pgsql-hackers@arkaria.postgresql.org; Tue, 01 Oct 2019 13:18:10 +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 1iFI2b-00021O-Qj for pgsql-hackers@lists.postgresql.org; Tue, 01 Oct 2019 13:18:09 +0000 Received: from mail-wm1-x342.google.com ([2a00:1450:4864:20::342]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iFI2Y-0004cp-VO for pgsql-hackers@postgresql.org; Tue, 01 Oct 2019 13:18:08 +0000 Received: by mail-wm1-x342.google.com with SMTP id v17so3202979wml.4 for ; Tue, 01 Oct 2019 06:18:06 -0700 (PDT) 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:content-transfer-encoding:in-reply-to :user-agent; bh=CkXaF2L8zsuhA3Ed5KuVrV4SMAUknCZS0jtSVdRQUoQ=; b=QcirQxiu5eRMPyPVYTR0u227b7+jxBcYypxkelV57vgGtYlGhRTCJD6Gt2IrL36Tco INF9EPYJzAaSuIK+6xWtzsHyNChxUZudgzNFEFbDPOWaA4FeZU9nXOTvnYSnJHKbpxGq PojpQrcgW8goMsK4OstBXjGuEwm8HnFsKPvZs+KEq594AtCnj4afJ7AxPdxS+EHmHdP0 Rh7/HR8vK6XxyCIVb416KoCvnMc5Vj0DWbkFONYK/ZUiQ9+44D96YCZxNUe4Ibu4b5YP fVfnfM801c+kEZhLcocROGEJwCSC9WMPmPWs5wBDTBtNZjQK/w10uIzKyRuHKPvK5WaG 2Cwg== 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:content-transfer-encoding :in-reply-to:user-agent; bh=CkXaF2L8zsuhA3Ed5KuVrV4SMAUknCZS0jtSVdRQUoQ=; b=ecN1ZsVVA4fz2PYIMjost5jsw93Hu2aD1GkG94Z5Z94rNt8/hYSCIKkHTk91BfMVGx qGRgqUl0AmGLZ386pOEvBZaVdyrUsLS2nSPh+2axXNbPOGCki7FGcOyhZMJP4pqiuDrq I53CAwy4HjR6SaoJpgfuFvuN8nPjs6AWa8rd31nCXwud7F3W0yYNKdf1nOxmyvRZew4o CFgGLSJl8STtrzl27aAeZnGcnTWKs9AjJ26nGN233+v7FEMZyDNqT3e3KZDfZSEXhdiA iakCQaq71mbhQZYiZNGMNZLKSslFA2c26RlGqerHvrQQIWYhkh3zgb6EzRM7Tr6DGFoy f+zA== X-Gm-Message-State: APjAAAXT9JMNI4hJCFuH5WydS6ZInWlNuIdW8OmVM1hJenc0gZOqkNnJ FUNz2XvXtXJmJHdznfM/wZXnjA== X-Google-Smtp-Source: APXvYqyx4ZkIWazEkwDHs7APtiDsB9UrNJfLcmUcnTrCDtRkjFmaYVoEnY6tTIyIx6YQUxJRGO/0xA== X-Received: by 2002:a1c:7519:: with SMTP id o25mr3603384wmc.16.1569935885417; Tue, 01 Oct 2019 06:18:05 -0700 (PDT) Received: from localhost (ip-86-49-253-183.net.upcbroadband.cz. [86.49.253.183]) by smtp.gmail.com with ESMTPSA id z9sm26108660wrp.26.2019.10.01.06.18.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Oct 2019 06:18:04 -0700 (PDT) Date: Tue, 1 Oct 2019 15:18:03 +0200 From: Tomas Vondra To: Andrey Borodin Cc: Alvaro Herrera , Binguo Bao , Paul Ramsey , Pgsql Hackers Subject: Re: Optimize partial TOAST decompression Message-ID: <20191001131803.j6uin7nho7t6vxzy@development> References: <20190710144725.kaswinyw2bdgb6tz@development> <20190925203834.GA25060@alvherre.pgsql> <20190926230036.ubxzgkqsphbzmbdl@development> <20190930155607.5tnyzksijgm4imbg@development> <20190930172951.gdedvexnf4d2wv5e@development> <123EF56B-F8EC-4868-B49B-095795095E7A@yandex-team.ru> <20191001100805.wnnuj73kuzfwzs56@development> <20191001123420.uwzxtqr7qvleyxef@development> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20191001123420.uwzxtqr7qvleyxef@development> User-Agent: NeoMutt/20180716 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Tue, Oct 01, 2019 at 02:34:20PM +0200, Tomas Vondra wrote: >On Tue, Oct 01, 2019 at 12:08:05PM +0200, Tomas Vondra wrote: >>On Tue, Oct 01, 2019 at 11:20:39AM +0500, Andrey Borodin wrote: >>> >>> >>>>30 сент. 2019 г., в 22:29, Tomas Vondra написал(а): >>>> >>>>On Mon, Sep 30, 2019 at 09:20:22PM +0500, Andrey Borodin wrote: >>>>> >>>>> >>>>>>30 сент. 2019 г., в 20:56, Tomas Vondra написал(а): >>>>>> >>>>>>I mean this: >>>>>> >>>>>>/* >>>>>> * Use int64 to prevent overflow during calculation. >>>>>> */ >>>>>>compressed_size = (int32) ((int64) rawsize * 9 + 8) / 8; >>>>>> >>>>>>I'm not very familiar with pglz internals, but I'm a bit puzzled by >>>>>>this. My first instinct was to compare it to this: >>>>>> >>>>>>#define PGLZ_MAX_OUTPUT(_dlen) ((_dlen) + 4) >>>>>> >>>>>>but clearly that's a very different (much simpler) formula. So why >>>>>>shouldn't pglz_maximum_compressed_size simply use this macro? >>>> >>>>> >>>>>compressed_size accounts for possible increase of size during >>>>>compression. pglz can consume up to 1 control byte for each 8 bytes of >>>>>data in worst case. >>>> >>>>OK, but does that actually translate in to the formula? We essentially >>>>need to count 8-byte chunks in raw data, and multiply that by 9. Which >>>>gives us something like >>>> >>>>nchunks = ((rawsize + 7) / 8) * 9; >>>> >>>>which is not quite what the patch does. >>> >>>I'm afraid neither formula is correct, but all this is hair-splitting differences. >>> >> >>Sure. I just want to be sure the formula is safe and we won't end up >>using too low value in some corner case. >> >>>Your formula does not account for the fact that we may not need all bytes from last chunk. >>>Consider desired decompressed size of 3 bytes. We may need 1 control byte and 3 literals, 4 bytes total >>>But nchunks = 9. >>> >> >>OK, so essentially this means my formula works with whole chunks, i.e. >>if we happen to need just a part of a decompressed chunk, we still >>request enough data to decompress it whole. This way we may request up >>to 7 extra bytes, which seems fine. >> >>>Binguo's formula is appending 1 control bit per data byte and one extra >>>control byte. Consider size = 8 bytes. We need 1 control byte, 8 >>>literals, 9 total. But compressed_size = 10. >>> >>>Mathematically correct formula is compressed_size = (int32) ((int64) >>>rawsize * 9 + 7) / 8; Here we take one bit for each data byte, and 7 >>>control bits for overflow. >>> >>>But this equations make no big difference, each formula is safe. I'd >>>pick one which is easier to understand and document (IMO, its nchunks = >>>((rawsize + 7) / 8) * 9). >>> >> >>I'd use the *mathematically correct* formula, it doesn't seem to be any >>more complex, and the "one bit per byte, complete bytes" explanation >>seems quite understandable. >> > >Pushed. > >I've ended up using the *mathematically correct* formula, hopefully >with sufficient explanation why it's correct. I've also polished a >couple more comments, and pushed like that. > >Thanks to Binguo Bao for this improvement, and all the reviewers in this >thread. > Hmmm, this seems to trigger a failure on thorntail, which is a sparc64 machine (and it seems to pass on all x86 machines, so far). Per the backtrace, it seems to have failed like this: Core was generated by `postgres: parallel worker for PID 2341 '. Program terminated with signal SIGUSR1, User defined signal 1. #0 heap_tuple_untoast_attr_slice (attr=, sliceoffset=, slicelength=) at /home/nm/farm/sparc64_deb10_gcc_64_ubsan/HEAD/pgsql.build/../pgsql/src/backend/access/common/detoast.c:235 235 max_size = pglz_maximum_compressed_size(sliceoffset + slicelength, #0 heap_tuple_untoast_attr_slice (attr=, sliceoffset=, slicelength=) at /home/nm/farm/sparc64_deb10_gcc_64_ubsan/HEAD/pgsql.build/../pgsql/src/backend/access/common/detoast.c:235 #1 0x00000100003d4ae8 in ExecInterpExpr (state=0x10000d02298, econtext=0x10000d01510, isnull=0x7feffb2fd1f) at /home/nm/farm/sparc64_deb10_gcc_64_ubsan/HEAD/pgsql.build/../pgsql/src/backend/executor/execExprInterp.c:690 ... so likely on this line: max_size = pglz_maximum_compressed_size(sliceoffset + slicelength, TOAST_COMPRESS_SIZE(attr)); the offset+length is just intereger arithmetics, so I don't see why that would fail. So it has to be TOAST_COMPRESS_SIZE, which is defined like this: #define TOAST_COMPRESS_SIZE(ptr) ((int32) VARSIZE(ptr) - TOAST_COMPRESS_HDRSZ) I wonder if that's wrong, somehow ... Maybe it should use VARSIZE_ANY, but then how would it work on any platform and only fail on sparc64? regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services