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 1hBgXm-0000LP-3k for pgsql-hackers@arkaria.postgresql.org; Wed, 03 Apr 2019 14:07:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hBgXk-0000Zz-Oo for pgsql-hackers@arkaria.postgresql.org; Wed, 03 Apr 2019 14:07:08 +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 1hBgXk-0000Zm-CW for pgsql-hackers@lists.postgresql.org; Wed, 03 Apr 2019 14:07:08 +0000 Received: from mail-lf1-f65.google.com ([209.85.167.65]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hBgXg-0002OC-SD for pgsql-hackers@postgresql.org; Wed, 03 Apr 2019 14:07:07 +0000 Received: by mail-lf1-f65.google.com with SMTP id a28so11814262lfo.7 for ; Wed, 03 Apr 2019 07:07:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=iTQJG96psY3sKb4FdntfTilh4LBdkDNn1k2+Eydy+uc=; b=U2+HzfFdGvNv1wSqcgI939TBGV12bPg/kzEED+gNPV0pMnE42whd+0V/3HnyNY+R6H VBBGIF3uLkkcgAuVkVBYUlKXyPyhI+J2GK6qtBV69c4U05SR4MpwcSb2tOMS+rm2uW4C y3CE924/mbYrZbbGCPj4PhTparxdrdwJgHp8w1OV6u/pxuTt//jzySwPuRKQHhaJOnkH qR+cSexjVZC2LUrUD9DCDMlzB+pk3blfG8Lhmkmecvrfis2WfAkGxk5mHxlATsMOSVC1 D5PNPA4OlLtZeNz0kpiJqXk5x6ns0vWAcK1YjvI0yuszTu+pbFDW0PIxmRDDOjUP3uZg AxsQ== X-Gm-Message-State: APjAAAVx3Quu3xQmyCRLI7EUmO70o/uvjymp4TbUqkySk3xNH586pQ2g 2K4eP0qQeovTLhmiPRABmHIrH4x9 X-Google-Smtp-Source: APXvYqwlavP99xtSvzw8mvoByv2h9H4QB6R9WtXf9y5OfPnkMNFIucWzKml+L8OHfqShqKjWW3fGnQ== X-Received: by 2002:ac2:495c:: with SMTP id o28mr1152647lfi.16.1554300422977; Wed, 03 Apr 2019 07:07:02 -0700 (PDT) Received: from [192.168.1.10] (dsl-hkibng32-54fbf8-78.dhcp.inet.fi. [84.251.248.78]) by smtp.gmail.com with ESMTPSA id 15sm3003602lft.88.2019.04.03.07.07.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 03 Apr 2019 07:07:01 -0700 (PDT) Subject: Re: Reduce amount of WAL generated by CREATE INDEX for gist, gin and sp-gist To: Andrey Lepikhov Cc: David Steele , a.lubennikova@postgrespro.ru, Michael Paquier , Dmitry Dolgov <9erthalion6@gmail.com>, PostgreSQL-development References: <18f0b868-6888-27eb-22cc-982709c3d0f7@postgrespro.ru> <1cdb9c52-e8fb-124b-3949-d3be8a6a7451@postgrespro.ru> <20190204050413.GI29064@paquier.xyz> <4030cc62-c86d-52c6-dc06-087ed0125244@pgmasters.net> From: Heikki Linnakangas Message-ID: <090fb3cb-1ca4-e173-ecf7-47d41ebac620@iki.fi> Date: Wed, 3 Apr 2019 17:07:00 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.5.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 02/04/2019 08:58, Andrey Lepikhov wrote: > On 25/03/2019 15:21, Heikki Linnakangas wrote: >> I had another quick look. >> >> I still think using the "generic xlog AM" for this is a wrong level of >> abstraction, and we should use the XLOG_FPI records for this directly. >> We can extend XLOG_FPI so that it can store multiple pages in a single >> record, if it doesn't already handle it. >> >> Another counter-point to using the generic xlog record is that you're >> currently doing unnecessary two memcpy's of all pages in the index, in >> GenericXLogRegisterBuffer() and GenericXLogFinish(). That's not free. >> >> I guess the generic_log_relation() function can stay where it is, but it >> should use XLogRegisterBuffer() and XLogInsert() directly. > > Patch set v.3 uses XLOG_FPI records directly. Thanks! Committed, with some changes: * I moved the log_relation() function to xlog.c, so that it sits beside log_newpage_*() functions. I renamed it to log_newpage_range(), and changed the argument so that the caller provides the beginning and end block ranges. I added a 'page_std' flag, instead of just assuming that all pages use the standard page layout. All of the callers pass page_std=true at the moment, but seems better to be explicit about it. I made those changes because I felt that the function was too narrowly tailored for the current callers. The assumption about standard page layout, and also the fact that it only logged the main fork. It's more flexible now, for any future AMs that might not be exactly like that. It feels like it's at the same level of abstraction now as the other log_newpage_*() functions. Even if we never need the flexibility, I think making the 'page_std' and 'forknum' arguments explicit is good, to draw attention to those details, for anyone calling the function. * I fixed the REDO code. It was trivially broken, it only restored the first page in each FPI WAL record. * Using "fake" unlogged LSNs for GiST index build seemed fishy. I could not convince myself that it was safe in all corner cases. In a recently initdb'd cluster, it's theoretically possible that the fake LSN counter overtakes the real LSN value, and that could lead to strange behavior. For example, how would the buffer manager behave, if there was a dirty page in the buffer cache with an LSN value that's greater than the current WAL flush pointer? I think you'd get "ERROR: xlog flush request %X/%X is not satisfied --- flushed only to %X/%X". I changed that so that we use LSN 1 for all pages during index build. That's smaller than any real or fake LSN. Or actually, the fake LSN counter used to start at 1 - I bumped that up to 1000, so now it's safely smaller. Could've used 0 instead, but there's an assertion in gist scan code that didn't like that. > As a benchmark I use the script (test.sql in attachment) which show WAL > size increment during index build. In the table below you can see the > influence of the patch on WAL growth. > > Results > ======= > AM | master | patch | > GIN | 347 MB | 66 MB | > GiST | 157 MB | 43 MB | > SP-GiST | 119 MB | 38 MB | Nice! - Heikki