Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id B6321B5DF68 for ; Mon, 30 May 2011 21:48:01 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 65924-03 for ; Tue, 31 May 2011 00:47:54 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.anarazel.de (mail.anarazel.de [217.115.131.40]) by mail.postgresql.org (Postfix) with ESMTP id A349EB5DEED for ; Mon, 30 May 2011 21:47:54 -0300 (ADT) Received: by mail.anarazel.de (Postfix, from userid 108) id 45A7C448004; Tue, 31 May 2011 02:47:54 +0200 (CEST) Received: from anarazel.de (f053042007.adsl.alicedsl.de [78.53.42.7]) (Authenticated sender: andres@anarazel.de) by mail.anarazel.de (Postfix) with ESMTPSA id F18DF448003; Tue, 31 May 2011 02:47:50 +0200 (CEST) Received: by anarazel.de (Postfix, from userid 1000) id 4E9831C0172; Tue, 31 May 2011 02:47:50 +0200 (CEST) From: Andres Freund To: pgsql-bugs@postgresql.org Subject: Re: Unlogged tables cannot be truncated twice Date: Tue, 31 May 2011 02:47:49 +0200 User-Agent: KMail/1.13.7 (Linux/2.6.39-rc6-andres-00108-gdf2cc6b; KDE/4.6.2; x86_64; ; ) Cc: =?iso-8859-1?q?C=E9dric_Villemain?= , Alvaro Herrera , Greg Sabino Mullane References: <20110530160043.GC3012@core.home> <201105310214.00791.andres@anarazel.de> <201105310235.58484.andres@anarazel.de> In-Reply-To: <201105310235.58484.andres@anarazel.de> MIME-Version: 1.0 Content-Type: Multipart/Mixed; boundary="Boundary-00=_1qD5N2Tg2wtuofq" Message-Id: <201105310247.50055.andres@anarazel.de> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201105/230 X-Sequence-Number: 30369 --Boundary-00=_1qD5N2Tg2wtuofq Content-Type: Text/Plain; charset="iso-8859-15" Content-Transfer-Encoding: quoted-printable On Tuesday, May 31, 2011 02:35:58 AM Andres Freund wrote: > On Tuesday, May 31, 2011 02:14:00 AM Andres Freund wrote: > > On Tuesday, May 31, 2011 01:56:05 AM C=E9dric Villemain wrote: > > > I remove my own explanations as we conclude on the same thing. > > > Attached is the fix by adding a (!reindex) in the index.c if(). > >=20 > > Thats imo wrong because it will break a plain REINDEX? >=20 > > I think one possible correct fix would be the attached: > My version was wrong as well because it did not observe RelationTruncate= 's > nblocks argument. That function is used to "shorten" the relation in > vacuum. So dropping the init fork there is not a good idea. >=20 > So I think it is the simpler version of simply checking the existance of > the fork before creating is ok. Gna. gnargl. Coffe. Bed. ;) There was an accidental hunk I added while removing some whitespace. That=20 would not have been good on a real commit. Argh. --Boundary-00=_1qD5N2Tg2wtuofq Content-Type: text/x-patch; charset="UTF-8"; name="0001-Fix-file-existing-errors-uppon-recreation-of-an-unlo_v3.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="0001-Fix-file-existing-errors-uppon-recreation-of-an-unlo_v3.patch" =46rom 1dbef0a907ec5e87da61a7ffb505ba082f2e11f8 Mon Sep 17 00:00:00 2001 =46rom: Andres Freund Date: Tue, 31 May 2011 02:02:19 +0200 Subject: [PATCH] Fix file existing errors uppon recreation of an unlogged tables indexes _init fork The error could only occur if a relation got truncated that got a new relfilenode in the same transaction before because in that case heap_truncate_one_rel gets called instead of setting a new relfilenode for all relevant relations. To fix that simply check for the existance of the fork before recreating it. To reproduce: BEGIN;CREATE UNLOGGED TABLE foo (a int PRIMARY KEY);TRUNCATE foo; ROLLBACK; =2D-- src/backend/catalog/index.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-) diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index a0898e0..d833a09 100644 =2D-- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -1743,7 +1743,8 @@ index_build(Relation heapRelation, /* * If this is an unlogged index, we need to write out an init fork for it. */ =2D if (heapRelation->rd_rel->relpersistence =3D=3D RELPERSISTENCE_UNLOGGED) + if (heapRelation->rd_rel->relpersistence =3D=3D RELPERSISTENCE_UNLOGGED + && !smgrexists(indexRelation->rd_smgr, FSM_FORKNUM)) { RegProcedure ambuildempty =3D indexRelation->rd_am->ambuildempty; =20 =2D-=20 1.7.5.rc1.16.g9db1.dirty --Boundary-00=_1qD5N2Tg2wtuofq Content-Type: text/x-patch; charset="UTF-8"; name="0001-Fix-file-existing-errors-uppon-recreation-of-an-unlo_v3.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="0001-Fix-file-existing-errors-uppon-recreation-of-an-unlo_v3.patch" =46rom 1dbef0a907ec5e87da61a7ffb505ba082f2e11f8 Mon Sep 17 00:00:00 2001 =46rom: Andres Freund Date: Tue, 31 May 2011 02:02:19 +0200 Subject: [PATCH] Fix file existing errors uppon recreation of an unlogged tables indexes _init fork The error could only occur if a relation got truncated that got a new relfilenode in the same transaction before because in that case heap_truncate_one_rel gets called instead of setting a new relfilenode for all relevant relations. To fix that simply check for the existance of the fork before recreating it. To reproduce: BEGIN;CREATE UNLOGGED TABLE foo (a int PRIMARY KEY);TRUNCATE foo; ROLLBACK; =2D-- src/backend/catalog/index.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-) diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index a0898e0..d833a09 100644 =2D-- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -1743,7 +1743,8 @@ index_build(Relation heapRelation, /* * If this is an unlogged index, we need to write out an init fork for it. */ =2D if (heapRelation->rd_rel->relpersistence =3D=3D RELPERSISTENCE_UNLOGGED) + if (heapRelation->rd_rel->relpersistence =3D=3D RELPERSISTENCE_UNLOGGED + && !smgrexists(indexRelation->rd_smgr, FSM_FORKNUM)) { RegProcedure ambuildempty =3D indexRelation->rd_am->ambuildempty; =20 =2D-=20 1.7.5.rc1.16.g9db1.dirty --Boundary-00=_1qD5N2Tg2wtuofq--