Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ateZo-0003fk-8P for pgsql-hackers@arkaria.postgresql.org; Fri, 22 Apr 2016 17:09:08 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1ateZn-0007rC-RX for pgsql-hackers@arkaria.postgresql.org; Fri, 22 Apr 2016 17:09:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ateYR-0006Kw-ME for pgsql-hackers@postgresql.org; Fri, 22 Apr 2016 17:07:43 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ateYN-0000Jw-7X for pgsql-hackers@postgresql.org; Fri, 22 Apr 2016 17:07:43 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id B5F7425F07 for ; Fri, 22 Apr 2016 13:07:37 -0400 (EDT) Received: from frontend1 ([10.202.2.160]) by compute4.internal (MEProxy); Fri, 22 Apr 2016 13:07:37 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=IV9EScI05g2P/gg QPOJZuNvQLmc=; b=BE1J+mTphmCo4fWeZxBeeOMn1J5ZwJS7WwtEu9+wT0pWt4r g9RLLLcw450kVyU48FjOreC7U2DC+D4Dj/LIh1SZEUwAjExZmQ3p1WR2ogY3qydw rs4Io4DmqWetDpYUH7AR43LAhQRw77GJqmHUlII3cZFiHlExipNoO2F/TQ2k= X-Sasl-enc: wXGPr8QtVXfhluDE27+eaTNeFNOSP+6szu1kzbNdQVQf 1461344857 Received: from intern.anarazel.de (unknown [192.77.237.104]) by mail.messagingengine.com (Postfix) with ESMTPA id 4A602C00012; Fri, 22 Apr 2016 13:07:37 -0400 (EDT) Date: Fri, 22 Apr 2016 10:07:36 -0700 From: Andres Freund To: Abhijit Menon-Sen Cc: Robert Haas , Michael Paquier , Haribabu Kommi , Thom Brown , Fabien COELHO , pgsql-hackers@postgresql.org Subject: Re: [BUGS] Breakage with VACUUM ANALYSE + partitions Message-ID: <20160422170736.bjrsusb4ubdhdtl4@alap3.anarazel.de> References: <20160326123942.qnuwqg3yg2y7ae4p@alap3.anarazel.de> <20160411165054.r35o4fjicdg4avs7@alap3.anarazel.de> <20160411171742.n5xebjt7tcdy4sxf@alap3.anarazel.de> <20160414053929.GA29751@toroid.org> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="casgvl5vl3bj5dgc" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20160414053929.GA29751@toroid.org> X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org --casgvl5vl3bj5dgc Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit On 2016-04-14 11:09:29 +0530, Abhijit Menon-Sen wrote: > At 2016-04-12 09:00:57 -0400, robertmhaas@gmail.com wrote: > > > > On Mon, Apr 11, 2016 at 1:17 PM, Andres Freund wrote: > > > > > > 3) Actually handle the case of the last open segment not being > > > RELSEG_SIZE properly in _mdfd_getseg() - mdnblocks() does so. > > > > #3 seems like it's probably about 15 years overdue, so let's do that > > anyway. > > I don't have anything useful to contribute, I'm just trying to > understand this fix. > > _mdfd_getseg() looks like this: > > targetseg = blkno / ((BlockNumber) RELSEG_SIZE); > for (nextsegno = 1; nextsegno <= targetseg; nextsegno++) > { > Assert(nextsegno == v->mdfd_segno + 1); > > if (v->mdfd_chain == NULL) > { > /* > * Normally we will create new segments only if authorized by the > * caller (i.e., we are doing mdextend()). […] > */ > if (behavior == EXTENSION_CREATE || InRecovery) > { > if (_mdnblocks(reln, forknum, v) < RELSEG_SIZE) > /* mdextend */ > v->mdfd_chain = _mdfd_openseg(reln, forknum, +nextsegno, O_CREAT); > } > else > { > /* We won't create segment if not existent */ > v->mdfd_chain = _mdfd_openseg(reln, forknum, nextsegno, 0); > } > if (v->mdfd_chain == NULL) > /* return NULL or ERROR */ > } > v = v->mdfd_chain; > } > > Do I understand correctly that the case of the "last open segment" > (i.e., the one for which mdfd_chain == NULL) not being RELSEG_SIZE > (i.e., _mdnblocks(reln, forknum, v) < RELSEG_SIZE) should not call > _mfdf_openseg on nextsegno if behaviour is not EXTENSION_CREATE or > InRecovery? > > And that "We won't create segment if not existent" should happen, but > doesn't only because the next segment file wasn't removed earlier, so > we have to add an extra check for that case? > > In other words, is something like the following what's needed here, or > is there more to it? It's a bit more complicated than that, but that's the principle. Thanks for the patch, and sorry for my late response. See my attached version for a more fleshed out version of this. Looking at the code around this I found: * _mdfd_getseg(), in contrast to mdnblocks() doesn't check segment size, neither whether to continue with a too small, nor to error out with a too big segment * For, imo pretty bad, reasons in mdsync() we currently rely on EXTENSION_RETURN_NULL to extend the file in recovery, and to set errno to ENOENT. Brrr. * we currently FATAL if a segment is too big - I've copied that behaviour, but why not just ERROR out? The attached patch basically adds the segment size checks to _mdfd_getseg(), and doesn't perform extension, even in recovery, if EXTENSION_REALLY_RETURN_NULL is passed. This fixes the issue for me, both in the originally reported variant, and in some more rudely setup version (i.e. rm'ing files). I'm seing some weird behaviour (even with writeback flushing disabled) with smgr invals and recovery/standbys, but I don't want to dive into it before http://www.postgresql.org/message-id/CAD21AoDpZ6Xjg=gFrGPnSn4oTRRcwK1EBrWCq9OqOHuAcMMC=w@mail.gmail.com is addressed, it's too likely to be related. Greetings, Andres Freund --casgvl5vl3bj5dgc Content-Type: text/x-patch; charset=us-ascii Content-Disposition: attachment; filename="0001-Don-t-open-formally-non-existant-segments-in-_mdfd_g.patch"