Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id DC92BB5DDF1 for ; Mon, 30 May 2011 20:28:50 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 82905-01 for ; Mon, 30 May 2011 23:28:44 +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 E7693B5DDD5 for ; Mon, 30 May 2011 20:28:43 -0300 (ADT) Received: by mail.anarazel.de (Postfix, from userid 108) id 0BC04448006; Tue, 31 May 2011 01:28:42 +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 2E04A448009; Tue, 31 May 2011 01:28:39 +0200 (CEST) Received: by anarazel.de (Postfix, from userid 1000) id A3A771C0172; Tue, 31 May 2011 01:28:37 +0200 (CEST) From: Andres Freund To: pgsql-bugs@postgresql.org Subject: Re: Unlogged tables cannot be truncated twice Date: Tue, 31 May 2011 01:28:37 +0200 User-Agent: KMail/1.13.7 (Linux/2.6.39-rc6-andres-00108-gdf2cc6b; KDE/4.6.2; x86_64; ; ) Cc: Alvaro Herrera , Greg Sabino Mullane References: <20110530160043.GC3012@core.home> <1306790274-sup-6596@alvh.no-ip.org> In-Reply-To: <1306790274-sup-6596@alvh.no-ip.org> MIME-Version: 1.0 Content-Type: Text/Plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <201105310128.37288.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/226 X-Sequence-Number: 30365 On Monday, May 30, 2011 11:18:20 PM Alvaro Herrera wrote: > Excerpts from Greg Sabino Mullane's message of lun may 30 12:00:43 -0400 2011: > > Wow, this one took a bit to narrow down. Here's the failing case: > > > > # create unlogged table foo (a text); > > CREATE TABLE > > # begin; > > BEGIN > > #* truncate table foo; > > TRUNCATE TABLE > > #* truncate table foo; > > ERROR: could not create file "base/19131/19183_init": File exists > > > > Very reproducible. The column types matter: if the only column > > is an INT, for example, the problem does not occur. > > So 19183 is the toast table OID? Nope. Its any index. You can provoke it with: begin;create unlogged table foo (a int primary key);truncate foo;rollback; or begin;create unlogged table foo (a text);truncate foo;rollback; The problem is this tidbit from tablecmds.c's ExecuteTruncate: /* * Normally, we need a transaction-safe truncation here. However, if * the table was either created in the current (sub)transaction or has * a new relfilenode in the current (sub)transaction, then we can just * truncate it in-place, because a rollback would cause the whole * table or the current physical file to be thrown away anyway. */ if (rel->rd_createSubid == mySubid || rel->rd_newRelfilenodeSubid == mySubid) { /* Immediate, non-rollbackable truncation is OK */ heap_truncate_one_rel(rel); } in combination with index.c's index_build: /* * If this is an unlogged index, we need to write out an init fork for it. */ if (heapRelation->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED) { RegProcedure ambuildempty = indexRelation->rd_am->ambuildempty; RelationOpenSmgr(indexRelation); smgrcreate(indexRelation->rd_smgr, INIT_FORKNUM, false); OidFunctionCall1(ambuildempty, PointerGetDatum(indexRelation)); } Andres