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 1iFHMM-00046c-Eg for pgsql-hackers@arkaria.postgresql.org; Tue, 01 Oct 2019 12:34:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iFHMJ-0006k5-IB for pgsql-hackers@arkaria.postgresql.org; Tue, 01 Oct 2019 12:34:27 +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 1iFHMJ-0006js-2I for pgsql-hackers@lists.postgresql.org; Tue, 01 Oct 2019 12:34:27 +0000 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iFHMG-0004Gu-8f for pgsql-hackers@postgresql.org; Tue, 01 Oct 2019 12:34:25 +0000 Received: by mail-wr1-x444.google.com with SMTP id h7so15307629wrw.8 for ; Tue, 01 Oct 2019 05:34:23 -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=CXtxW5cev/pXf0kjpub45oGHGYSzTJ/UndQYEYjpDho=; b=zXk3/jKuOxUJSkGBTdvRlq2c9/I0z5e2mDf1k0J4f516xONBtL5IDy+ug2pux6Hify DpFe0pGop+bEFEoZpjgo+pkZchcDwW3O/8hYb/N6f6NxK//icVRaF7DYAbxfqFodNcsD OiEJlwjtHOMaGcYiBbbKviYGaVuVrjNyPMJDeT2RF6HDlhVddWHwecKo/RmA4fSKiiKR 1P5Na56v9LkG/gJPoDMWABWqqEI1shmV/RRNlPQPFud3jQL7sxzYhKkkVNDOY5l8USsR dx1E4it6nnUARt4+D0IRejq00x0wem0MjZQSatO2Cv1Ct1A0c0FbGY3IpCa01B0A2Wzc zU7A== 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=CXtxW5cev/pXf0kjpub45oGHGYSzTJ/UndQYEYjpDho=; b=rCQGECekS+QuhT+hCF6D9SuxGA9h1UN6+zPzH4VDLDy2ECBHMIMeRzDEhaibQHeTpd DG+qtSDjopVByp9hrzCaJqeAduB3vaKaCdrKrbBXsNujB6WCOQB8jVFUXDRNsr2I/r8G r3ec9JxZvzry+C1doiSVynvus3p6Jakgzdz4xmBoHAOKfpatiXgOkcflyao3/Lq9PvAt xcabol1EL399KB+8VA0hp/CrCedkDBYX0bhuODssnp7/cXQGkppHuZTGE5QZXOPH+4UG Xeqo/ybiYbSi6GT+PLh6Epqf55g08ZJuvp8rtizLubjGmHserl3hKLChrQXpmQDfgGIN BzPA== X-Gm-Message-State: APjAAAUXSVA5rXnjnfOe467NMXAhTV6qDnwQB+VblxWPezpvUx+7E1AC A94BanZaaPUs2aeRAHwpys2qIA== X-Google-Smtp-Source: APXvYqx2giy2lG/UC4dAuoJslkj8YlBm+tnC8C52gBXRTLEaVU/MhiVUXbe8yTc/Q0t+QnM7DGQ0hQ== X-Received: by 2002:adf:dc03:: with SMTP id t3mr19535538wri.27.1569933262635; Tue, 01 Oct 2019 05:34:22 -0700 (PDT) Received: from localhost (ip-86-49-253-183.net.upcbroadband.cz. [86.49.253.183]) by smtp.gmail.com with ESMTPSA id a18sm24112656wrh.25.2019.10.01.05.34.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Oct 2019 05:34:21 -0700 (PDT) Date: Tue, 1 Oct 2019 14:34:20 +0200 From: Tomas Vondra To: Andrey Borodin Cc: Alvaro Herrera , Binguo Bao , Paul Ramsey , Pgsql Hackers Subject: Re: Optimize partial TOAST decompression Message-ID: <20191001123420.uwzxtqr7qvleyxef@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> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20191001100805.wnnuj73kuzfwzs56@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 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. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services