pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Unlogged tables cannot be truncated twice
16+ messages / 5 participants
[nested] [flat]

* Unlogged tables cannot be truncated twice
@ 2011-05-30 16:00  Greg Sabino Mullane <greg@endpoint.com>
  0 siblings, 1 reply; 16+ messages in thread

From: Greg Sabino Mullane @ 2011-05-30 16:00 UTC (permalink / raw)
  To: pgsql-bugs

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.

-- 
Greg Sabino Mullane greg@endpoint.com
End Point Corporation
PGP Key: 0x14964AC8

^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Unlogged tables cannot be truncated twice
@ 2011-05-30 21:18  Alvaro Herrera <alvherre@commandprompt.com>
  parent: Greg Sabino Mullane <greg@endpoint.com>
  0 siblings, 1 reply; 16+ messages in thread

From: Alvaro Herrera @ 2011-05-30 21:18 UTC (permalink / raw)
  To: Greg Sabino Mullane <greg@endpoint.com>; +Cc: pgsql-bugs

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?

-- 
Álvaro Herrera <alvherre@commandprompt.com>
The PostgreSQL Company - Command Prompt, Inc.
PostgreSQL Replication, Consulting, Custom Development, 24x7 support



^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Unlogged tables cannot be truncated twice
@ 2011-05-30 23:28  Andres Freund <andres@anarazel.de>
  parent: Alvaro Herrera <alvherre@commandprompt.com>
  0 siblings, 1 reply; 16+ messages in thread

From: Andres Freund @ 2011-05-30 23:28 UTC (permalink / raw)
  To: pgsql-bugs; +Cc: Alvaro Herrera <alvherre@commandprompt.com>; Greg Sabino Mullane <greg@endpoint.com>

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



^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Unlogged tables cannot be truncated twice
@ 2011-05-30 23:56  Cédric Villemain <cedric.villemain.debian@gmail.com>
  parent: Andres Freund <andres@anarazel.de>
  0 siblings, 1 reply; 16+ messages in thread

From: Cédric Villemain @ 2011-05-30 23:56 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: pgsql-bugs; Alvaro Herrera <alvherre@commandprompt.com>; Greg Sabino Mullane <greg@endpoint.com>

2011/5/31 Andres Freund <andres@anarazel.de>:
> 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));
>        }
>

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().

It looks enough from early testing.

-- 
Cédric Villemain               2ndQuadrant
http://2ndQuadrant.fr/     PostgreSQL : Expertise, Formation et Support

Attachments:

fix_reindex_unlog.patch (text/x-patch, 543B) download diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index a0898e0..3564d52 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -1743,7 +1743,7 @@ index_build(Relation heapRelation, /* * If this is an unlogged index, we need to write out an init fork for it. */ - if (heapRelation->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED) + if (heapRelation->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED && !isreindex) { RegProcedure ambuildempty = indexRelation->rd_am->ambuildempty;
^ permalink raw reply [nested|flat] 16+ messages in thread

* Re: Unlogged tables cannot be truncated twice
@ 2011-05-31 00:14  Andres Freund <andres@anarazel.de>
  parent: Cédric Villemain <cedric.villemain.debian@gmail.com>
  0 siblings, 1 reply; 16+ messages in thread

From: Andres Freund @ 2011-05-31 00:14 UTC (permalink / raw)
  To: pgsql-bugs; +Cc: Cédric Villemain <cedric.villemain.debian@gmail.com>; Alvaro Herrera <alvherre@commandprompt.com>; Greg Sabino Mullane <greg@endpoint.com>

On Tuesday, May 31, 2011 01:56:05 AM Cédric 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().
Thats imo wrong because it will break a plain REINDEX?

I think one possible correct fix would be the attached:


Andres

Attachments:

0001-Fix-file-existing-errors-uppon-recreation-of-an-unlo.patch (text/x-patch, 1.6K) download From c22d067cfe233428631b52f7bed2d0013e7f4712 Mon Sep 17 00:00:00 2001 From: Andres Freund <andres@anarazel.de> 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 drop the relations _init fork in storage.c:RelationTruncate as the other forks are handled there as well. BEGIN;CREATE UNLOGGED TABLE foo (a int PRIMARY KEY);TRUNCATE foo; ROLLBACK; --- src/backend/catalog/storage.c | 6 ++++++ 1 files changed, 6 insertions(+), 0 deletions(-) diff --git a/src/backend/catalog/storage.c b/src/backend/catalog/storage.c index 57987be..7da63ba 100644 --- a/src/backend/catalog/storage.c +++ b/src/backend/catalog/storage.c @@ -252,6 +252,7 @@ RelationTruncate(Relation rel, BlockNumber nblocks) { bool fsm; bool vm; + bool initfork; /* Open it at the smgr level if not already done */ RelationOpenSmgr(rel); @@ -273,6 +274,11 @@ RelationTruncate(Relation rel, BlockNumber nblocks) if (vm) visibilitymap_truncate(rel, nblocks); + /* the init fork will be recreated in index_build */ + initfork = smgrexists(rel->rd_smgr, INIT_FORKNUM); + if (initfork) + smgrdounlink(rel->rd_smgr, INIT_FORKNUM, false); + /* * We WAL-log the truncation before actually truncating, which means * trouble if the truncation fails. If we then crash, the WAL replay -- 1.7.5.rc1.16.g9db1.dirty
^ permalink raw reply [nested|flat] 16+ messages in thread

* Re: Unlogged tables cannot be truncated twice
@ 2011-05-31 00:35  Andres Freund <andres@anarazel.de>
  parent: Andres Freund <andres@anarazel.de>
  0 siblings, 1 reply; 16+ messages in thread

From: Andres Freund @ 2011-05-31 00:35 UTC (permalink / raw)
  To: pgsql-bugs; +Cc: Cédric Villemain <cedric.villemain.debian@gmail.com>; Alvaro Herrera <alvherre@commandprompt.com>; Greg Sabino Mullane <greg@endpoint.com>

On Tuesday, May 31, 2011 02:14:00 AM Andres Freund wrote:
> On Tuesday, May 31, 2011 01:56:05 AM Cédric 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().
> 
> Thats imo wrong because it will break a plain REINDEX?
> 
> 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.

So I think it is the simpler version of simply checking the existance of the 
fork before creating is ok.

Andres

Attachments:

0001-Fix-file-existing-errors-uppon-recreation-of-an-unlo_v2.patch (text/x-patch, 1.9K) download From bd7240ab8b331c261d6a5215bee906f8c63f0019 Mon Sep 17 00:00:00 2001 From: Andres Freund <andres@anarazel.de> 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; --- src/backend/catalog/index.c | 3 ++- src/backend/catalog/storage.c | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index a0898e0..d833a09 100644 --- 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. */ - if (heapRelation->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED) + if (heapRelation->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED + && !smgrexists(indexRelation->rd_smgr, FSM_FORKNUM)) { RegProcedure ambuildempty = indexRelation->rd_am->ambuildempty; diff --git a/src/backend/catalog/storage.c b/src/backend/catalog/storage.c index 57987be..68a7b30 100644 --- a/src/backend/catalog/storage.c +++ b/src/backend/catalog/storage.c @@ -265,8 +265,6 @@ RelationTruncate(Relation rel, BlockNumber nblocks) /* Truncate the FSM first if it exists */ fsm = smgrexists(rel->rd_smgr, FSM_FORKNUM); - if (fsm) - FreeSpaceMapTruncateRel(rel, nblocks); /* Truncate the visibility map too if it exists. */ vm = smgrexists(rel->rd_smgr, VISIBILITYMAP_FORKNUM); -- 1.7.5.rc1.16.g9db1.dirty
^ permalink raw reply [nested|flat] 16+ messages in thread

* Re: Unlogged tables cannot be truncated twice
@ 2011-05-31 00:47  Andres Freund <andres@anarazel.de>
  parent: Andres Freund <andres@anarazel.de>
  0 siblings, 1 reply; 16+ messages in thread

From: Andres Freund @ 2011-05-31 00:47 UTC (permalink / raw)
  To: pgsql-bugs; +Cc: Cédric Villemain <cedric.villemain.debian@gmail.com>; Alvaro Herrera <alvherre@commandprompt.com>; Greg Sabino Mullane <greg@endpoint.com>

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édric 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().
> > 
> > Thats imo wrong because it will break a plain REINDEX?
> 
> > 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.
> 
> 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 
would not have been good on a real commit.

Argh.

Attachments: download all (2 files, .tar.gz)

0001-Fix-file-existing-errors-uppon-recreation-of-an-unlo_v3.patch (text/x-patch, 1.3K) download From 1dbef0a907ec5e87da61a7ffb505ba082f2e11f8 Mon Sep 17 00:00:00 2001 From: Andres Freund <andres@anarazel.de> 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; --- 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 --- 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. */ - if (heapRelation->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED) + if (heapRelation->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED + && !smgrexists(indexRelation->rd_smgr, FSM_FORKNUM)) { RegProcedure ambuildempty = indexRelation->rd_am->ambuildempty; -- 1.7.5.rc1.16.g9db1.dirty
0001-Fix-file-existing-errors-uppon-recreation-of-an-unlo_v3.patch (text/x-patch, 1.3K) download From 1dbef0a907ec5e87da61a7ffb505ba082f2e11f8 Mon Sep 17 00:00:00 2001 From: Andres Freund <andres@anarazel.de> 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; --- 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 --- 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. */ - if (heapRelation->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED) + if (heapRelation->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED + && !smgrexists(indexRelation->rd_smgr, FSM_FORKNUM)) { RegProcedure ambuildempty = indexRelation->rd_am->ambuildempty; -- 1.7.5.rc1.16.g9db1.dirty
^ permalink raw reply [nested|flat] 16+ messages in thread

* Re: Unlogged tables cannot be truncated twice
@ 2011-05-31 01:27  Alvaro Herrera <alvherre@commandprompt.com>
  parent: Andres Freund <andres@anarazel.de>
  0 siblings, 1 reply; 16+ messages in thread

From: Alvaro Herrera @ 2011-05-31 01:27 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: pgsql-bugs; Cédric Villemain <cedric.villemain.debian@gmail.com>; Greg Sabino Mullane <greg@endpoint.com>

Excerpts from Andres Freund's message of lun may 30 20:47:49 -0400 2011:
> 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édric 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().
> > > 
> > > Thats imo wrong because it will break a plain REINDEX?
> > 
> > > 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.
> > 
> > So I think it is the simpler version of simply checking the existance of
> > the fork before creating is ok.

Hmm, I wonder if what we should be doing here is observe isreindex in
index_build to avoid creating the init fork.  Doing smgr access at that
level seems wrong.

> Gna. gnargl. Coffe. Bed. ;)
> 
> There was an accidental hunk I added while removing some whitespace. That 
> would not have been good on a real commit.
> 
> Argh.

Hah :-)

-- 
Álvaro Herrera <alvherre@commandprompt.com>
The PostgreSQL Company - Command Prompt, Inc.
PostgreSQL Replication, Consulting, Custom Development, 24x7 support



^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Unlogged tables cannot be truncated twice
@ 2011-05-31 07:44  Andres Freund <andres@anarazel.de>
  parent: Alvaro Herrera <alvherre@commandprompt.com>
  0 siblings, 2 replies; 16+ messages in thread

From: Andres Freund @ 2011-05-31 07:44 UTC (permalink / raw)
  To: pgsql-bugs; +Cc: Alvaro Herrera <alvherre@commandprompt.com>; Cédric Villemain <cedric.villemain.debian@gmail.com>; Greg Sabino Mullane <greg@endpoint.com>

On Tuesday, May 31, 2011 03:27:22 Alvaro Herrera wrote:
> Excerpts from Andres Freund's message of lun may 30 20:47:49 -0400 2011:
> > 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édric 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().
> > > > 
> > > > Thats imo wrong because it will break a plain REINDEX?
> > > 
> > > > 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.
> > > 
> > > So I think it is the simpler version of simply checking the existance
> > > of the fork before creating is ok.
> 
> Hmm, I wonder if what we should be doing here is observe isreindex in
> index_build to avoid creating the init fork.  Doing smgr access at that
> level seems wrong.
isreindex doesn't contain the necessary informormation as its set doing a 
REINDEX even though a new relfilenode is created and thus the fork needs to be 
created.

It doesn't seem terribly clean do do the !smgrexists(), I aggree with you 
there. On the other hand we are calling smgrcreate() two lines down anyway. I 
personally don't realy like the placement of that piece of code very much. 
Doing it in index_build seems to be the wrong place. I don't think there 
really is a good place for it right now.

Andres



^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Unlogged tables cannot be truncated twice
@ 2011-05-31 11:04  Cédric Villemain <cedric.villemain.debian@gmail.com>
  parent: Andres Freund <andres@anarazel.de>
  1 sibling, 1 reply; 16+ messages in thread

From: Cédric Villemain @ 2011-05-31 11:04 UTC (permalink / raw)
  To: Robert Haas <robertmhaas@gmail.com>; +Cc: pgsql-bugs; Alvaro Herrera <alvherre@commandprompt.com>; Greg Sabino Mullane <greg@endpoint.com>; Andres Freund <andres@anarazel.de>

2011/5/31 Andres Freund <andres@anarazel.de>:
> On Tuesday, May 31, 2011 03:27:22 Alvaro Herrera wrote:
>> Excerpts from Andres Freund's message of lun may 30 20:47:49 -0400 2011:
>> > 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édric 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().
>> > > >
>> > > > Thats imo wrong because it will break a plain REINDEX?
>> > >
>> > > > 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.
>> > >
>> > > So I think it is the simpler version of simply checking the existance
>> > > of the fork before creating is ok.
>>
>> Hmm, I wonder if what we should be doing here is observe isreindex in
>> index_build to avoid creating the init fork.  Doing smgr access at that
>> level seems wrong.
> isreindex doesn't contain the necessary informormation as its set doing a
> REINDEX even though a new relfilenode is created and thus the fork needs to be
> created.
>
> It doesn't seem terribly clean do do the !smgrexists(), I aggree with you
> there. On the other hand we are calling smgrcreate() two lines down anyway. I
> personally don't realy like the placement of that piece of code very much.
> Doing it in index_build seems to be the wrong place. I don't think there
> really is a good place for it right now.

Robert, I wonder if all the logic with INIT_FORK is only to simulate
truncate on server restart ?
I failled to understand why not simply use truncate around the server
start (when it is in recovery).

The current large amount of code changed just to be able to flush
unlogged files on server start looks crazy, if the only point is a
possible  performance impact on server restart, can't we find
something more elegant ?

While here I also wonder why GiST unlogged are not supported ?!

-- 
Cédric Villemain               2ndQuadrant
http://2ndQuadrant.fr/     PostgreSQL : Expertise, Formation et Support



^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Unlogged tables cannot be truncated twice
@ 2011-06-01 17:12  Robert Haas <robertmhaas@gmail.com>
  parent: Cédric Villemain <cedric.villemain.debian@gmail.com>
  0 siblings, 1 reply; 16+ messages in thread

From: Robert Haas @ 2011-06-01 17:12 UTC (permalink / raw)
  To: Cédric Villemain <cedric.villemain.debian@gmail.com>; +Cc: pgsql-bugs; Alvaro Herrera <alvherre@commandprompt.com>; Greg Sabino Mullane <greg@endpoint.com>; Andres Freund <andres@anarazel.de>

On Tue, May 31, 2011 at 7:04 AM, Cédric Villemain
<cedric.villemain.debian@gmail.com> wrote:
> Robert, I wonder if all the logic with INIT_FORK is only to simulate
> truncate on server restart ?

That is correct.

> I failled to understand why not simply use truncate around the server
> start (when it is in recovery).

We need to do the equivalent of a truncate but without relying in any
way on the system catalogs.  The startup process is not bound to a
database, and starting a separate worker process for each database
would be both very complicated and very expensive.

Also note that while truncate is very simple for a table (just zero
out the file) it's not so simple for an index (a zero-length file is
an invalid index, not an empty one).

> While here I also wonder why GiST unlogged are not supported ?!

Because they use LSNs internally to guarantee proper synchronization,
which presumes that WAL records are being omitted.  Temporary GIST
indexes were broken too, for the same reason, but Heikki fixed that
using GetXLogRecPtrForTemp().  We could engineer a similar solution
for unlogged GIST indexes using a shared counter that is saved in
pg_control across clean shutdowns, but I think that's a 9.2 project.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company



^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Unlogged tables cannot be truncated twice
@ 2011-06-01 17:15  Robert Haas <robertmhaas@gmail.com>
  parent: Andres Freund <andres@anarazel.de>
  1 sibling, 1 reply; 16+ messages in thread

From: Robert Haas @ 2011-06-01 17:15 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: pgsql-bugs; Alvaro Herrera <alvherre@commandprompt.com>; Cédric Villemain <cedric.villemain.debian@gmail.com>; Greg Sabino Mullane <greg@endpoint.com>

2011/5/31 Andres Freund <andres@anarazel.de>:
> On Tuesday, May 31, 2011 03:27:22 Alvaro Herrera wrote:
>> Excerpts from Andres Freund's message of lun may 30 20:47:49 -0400 2011:
>> > 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édric 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().
>> > > >
>> > > > Thats imo wrong because it will break a plain REINDEX?
>> > >
>> > > > 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.
>> > >
>> > > So I think it is the simpler version of simply checking the existance
>> > > of the fork before creating is ok.
>>
>> Hmm, I wonder if what we should be doing here is observe isreindex in
>> index_build to avoid creating the init fork.  Doing smgr access at that
>> level seems wrong.
> isreindex doesn't contain the necessary informormation as its set doing a
> REINDEX even though a new relfilenode is created and thus the fork needs to be
> created.
>
> It doesn't seem terribly clean do do the !smgrexists(), I aggree with you
> there. On the other hand we are calling smgrcreate() two lines down anyway. I
> personally don't realy like the placement of that piece of code very much.
> Doing it in index_build seems to be the wrong place. I don't think there
> really is a good place for it right now.

I'm open to suggestions on how to rearrange this, but I think for
right now the approach you proposed upthread (add a smgrexists() test)
is probably the simplest way to fix this.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company



^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Unlogged tables cannot be truncated twice
@ 2011-06-01 18:43  Cédric Villemain <cedric.villemain.debian@gmail.com>
  parent: Robert Haas <robertmhaas@gmail.com>
  0 siblings, 0 replies; 16+ messages in thread

From: Cédric Villemain @ 2011-06-01 18:43 UTC (permalink / raw)
  To: Robert Haas <robertmhaas@gmail.com>; +Cc: pgsql-bugs; Alvaro Herrera <alvherre@commandprompt.com>; Greg Sabino Mullane <greg@endpoint.com>; Andres Freund <andres@anarazel.de>

2011/6/1 Robert Haas <robertmhaas@gmail.com>:
> On Tue, May 31, 2011 at 7:04 AM, Cédric Villemain
> <cedric.villemain.debian@gmail.com> wrote:
>> Robert, I wonder if all the logic with INIT_FORK is only to simulate
>> truncate on server restart ?
>
> That is correct.
>
>> I failled to understand why not simply use truncate around the server
>> start (when it is in recovery).
>
> We need to do the equivalent of a truncate but without relying in any
> way on the system catalogs.  The startup process is not bound to a
> database, and starting a separate worker process for each database
> would be both very complicated and very expensive.
>
> Also note that while truncate is very simple for a table (just zero
> out the file) it's not so simple for an index (a zero-length file is
> an invalid index, not an empty one).
>
>> While here I also wonder why GiST unlogged are not supported ?!
>
> Because they use LSNs internally to guarantee proper synchronization,
> which presumes that WAL records are being omitted.  Temporary GIST
> indexes were broken too, for the same reason, but Heikki fixed that
> using GetXLogRecPtrForTemp().  We could engineer a similar solution
> for unlogged GIST indexes using a shared counter that is saved in
> pg_control across clean shutdowns, but I think that's a 9.2 project.
>
> --
> Robert Haas
> EnterpriseDB: http://www.enterprisedb.com
> The Enterprise PostgreSQL Company
>

All right, things under a new light!
Thank you.


-- 
Cédric Villemain               2ndQuadrant
http://2ndQuadrant.fr/     PostgreSQL : Expertise, Formation et Support



^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Unlogged tables cannot be truncated twice
@ 2011-06-02 17:31  Robert Haas <robertmhaas@gmail.com>
  parent: Robert Haas <robertmhaas@gmail.com>
  0 siblings, 1 reply; 16+ messages in thread

From: Robert Haas @ 2011-06-02 17:31 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: pgsql-bugs; Alvaro Herrera <alvherre@commandprompt.com>; Cédric Villemain <cedric.villemain.debian@gmail.com>; Greg Sabino Mullane <greg@endpoint.com>

On Wed, Jun 1, 2011 at 1:15 PM, Robert Haas <robertmhaas@gmail.com> wrote:
> 2011/5/31 Andres Freund <andres@anarazel.de>:
>> On Tuesday, May 31, 2011 03:27:22 Alvaro Herrera wrote:
>>> Excerpts from Andres Freund's message of lun may 30 20:47:49 -0400 2011:
>>> > 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édric 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().
>>> > > >
>>> > > > Thats imo wrong because it will break a plain REINDEX?
>>> > >
>>> > > > 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.
>>> > >
>>> > > So I think it is the simpler version of simply checking the existance
>>> > > of the fork before creating is ok.
>>>
>>> Hmm, I wonder if what we should be doing here is observe isreindex in
>>> index_build to avoid creating the init fork.  Doing smgr access at that
>>> level seems wrong.
>> isreindex doesn't contain the necessary informormation as its set doing a
>> REINDEX even though a new relfilenode is created and thus the fork needs to be
>> created.
>>
>> It doesn't seem terribly clean do do the !smgrexists(), I aggree with you
>> there. On the other hand we are calling smgrcreate() two lines down anyway. I
>> personally don't realy like the placement of that piece of code very much.
>> Doing it in index_build seems to be the wrong place. I don't think there
>> really is a good place for it right now.
>
> I'm open to suggestions on how to rearrange this, but I think for
> right now the approach you proposed upthread (add a smgrexists() test)
> is probably the simplest way to fix this.

Done.  Your patch tested for FSM_FORKNUM instead of INIT_FORKNUM,
which seemed wrong, so I changed it.  I also added comments.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company



^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Unlogged tables cannot be truncated twice
@ 2011-06-02 18:52  Andres Freund <andres@anarazel.de>
  parent: Robert Haas <robertmhaas@gmail.com>
  0 siblings, 1 reply; 16+ messages in thread

From: Andres Freund @ 2011-06-02 18:52 UTC (permalink / raw)
  To: pgsql-bugs; +Cc: Robert Haas <robertmhaas@gmail.com>; Alvaro Herrera <alvherre@commandprompt.com>; Cédric Villemain <cedric.villemain.debian@gmail.com>; Greg Sabino Mullane <greg@endpoint.com>

On Thursday, June 02, 2011 07:31:33 PM Robert Haas wrote:
> On Wed, Jun 1, 2011 at 1:15 PM, Robert Haas <robertmhaas@gmail.com> wrote:
> > 2011/5/31 Andres Freund <andres@anarazel.de>:
> >> On Tuesday, May 31, 2011 03:27:22 Alvaro Herrera wrote:
> >>> Excerpts from Andres Freund's message of lun may 30 20:47:49 -0400 2011:
> >>> > 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édric 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().
> >>> > > > 
> >>> > > > Thats imo wrong because it will break a plain REINDEX?
> >>> > > 
> >>> > > > 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.
> >>> > > 
> >>> > > So I think it is the simpler version of simply checking the
> >>> > > existance of the fork before creating is ok.
> >>> 
> >>> Hmm, I wonder if what we should be doing here is observe isreindex in
> >>> index_build to avoid creating the init fork.  Doing smgr access at that
> >>> level seems wrong.
> >> 
> >> isreindex doesn't contain the necessary informormation as its set doing
> >> a REINDEX even though a new relfilenode is created and thus the fork
> >> needs to be created.
> >> 
> >> It doesn't seem terribly clean do do the !smgrexists(), I aggree with
> >> you there. On the other hand we are calling smgrcreate() two lines down
> >> anyway. I personally don't realy like the placement of that piece of
> >> code very much. Doing it in index_build seems to be the wrong place. I
> >> don't think there really is a good place for it right now.
> > 
> > I'm open to suggestions on how to rearrange this, but I think for
> > right now the approach you proposed upthread (add a smgrexists() test)
> > is probably the simplest way to fix this.
> 
> Done.  Your patch tested for FSM_FORKNUM instead of INIT_FORKNUM,
> which seemed wrong, so I changed it.  I also added comments.
Wow. I don't think I ever made so many stupid mistakes when doing a two line 
change. I abviously wasn't really awake that evening. As evidenced excessively 
in that thread ;)

Thanks,

Andres



^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Unlogged tables cannot be truncated twice
@ 2011-06-02 22:11  Robert Haas <robertmhaas@gmail.com>
  parent: Andres Freund <andres@anarazel.de>
  0 siblings, 0 replies; 16+ messages in thread

From: Robert Haas @ 2011-06-02 22:11 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: pgsql-bugs; Alvaro Herrera <alvherre@commandprompt.com>; Cédric Villemain <cedric.villemain.debian@gmail.com>; Greg Sabino Mullane <greg@endpoint.com>

On Thu, Jun 2, 2011 at 2:52 PM, Andres Freund <andres@anarazel.de> wrote:
> Wow. I don't think I ever made so many stupid mistakes when doing a two line
> change. I abviously wasn't really awake that evening. As evidenced excessively
> in that thread ;)

I think I once found two bugs in a one-line patch...

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company



^ permalink  raw  reply  [nested|flat] 16+ messages in thread


end of thread, other threads:[~2011-06-02 22:11 UTC | newest]

Thread overview: 16+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-05-30 16:00 Unlogged tables cannot be truncated twice Greg Sabino Mullane <greg@endpoint.com>
2011-05-30 21:18 ` Alvaro Herrera <alvherre@commandprompt.com>
2011-05-30 23:28   ` Andres Freund <andres@anarazel.de>
2011-05-30 23:56     ` Cédric Villemain <cedric.villemain.debian@gmail.com>
2011-05-31 00:14       ` Andres Freund <andres@anarazel.de>
2011-05-31 00:35         ` Andres Freund <andres@anarazel.de>
2011-05-31 00:47           ` Andres Freund <andres@anarazel.de>
2011-05-31 01:27             ` Alvaro Herrera <alvherre@commandprompt.com>
2011-05-31 07:44               ` Andres Freund <andres@anarazel.de>
2011-05-31 11:04                 ` Cédric Villemain <cedric.villemain.debian@gmail.com>
2011-06-01 17:12                   ` Robert Haas <robertmhaas@gmail.com>
2011-06-01 18:43                     ` Cédric Villemain <cedric.villemain.debian@gmail.com>
2011-06-01 17:15                 ` Robert Haas <robertmhaas@gmail.com>
2011-06-02 17:31                   ` Robert Haas <robertmhaas@gmail.com>
2011-06-02 18:52                     ` Andres Freund <andres@anarazel.de>
2011-06-02 22:11                       ` Robert Haas <robertmhaas@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox