Re: [HACKERS] TRUNCATE TABLE with IDENTITY

Lists: pgsql-hackerspgsql-patches
From: Simon Riggs <simon(at)2ndquadrant(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: TRUNCATE TABLE with IDENTITY
Date: 2008-03-25 14:44:58
Message-ID: 1206456298.4285.924.camel@ebony.site
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

SQL200n specifies a new qualifier on a TRUNCATE command

TRUNCATE TABLE foo
[ CONTINUE IDENTITY | RESTART IDENTITY ]

CONTINUE IDENTITY is the default and does nothing, like now.

RESTART IDENTITY will reset the SERIAL sequences back to the original
start value.

Seems like a % project for the TODO list

--
Simon Riggs
2ndQuadrant http://www.2ndQuadrant.com

PostgreSQL UK 2008 Conference: http://www.postgresql.org.uk


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Simon Riggs <simon(at)2ndquadrant(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: TRUNCATE TABLE with IDENTITY
Date: 2008-03-25 15:48:48
Message-ID: 21634.1206460128@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Simon Riggs <simon(at)2ndquadrant(dot)com> writes:
> SQL200n specifies a new qualifier on a TRUNCATE command
> TRUNCATE TABLE foo
> [ CONTINUE IDENTITY | RESTART IDENTITY ]

> CONTINUE IDENTITY is the default and does nothing, like now.

> RESTART IDENTITY will reset the SERIAL sequences back to the original
> start value.

> Seems like a % project for the TODO list

Seems like copying syntax from a *draft* standard is a bit premature,
especially when the amount of functionality added is nil.

regards, tom lane


From: Simon Riggs <simon(at)2ndquadrant(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: TRUNCATE TABLE with IDENTITY
Date: 2008-03-25 16:02:25
Message-ID: 1206460945.4285.955.camel@ebony.site
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

On Tue, 2008-03-25 at 11:48 -0400, Tom Lane wrote:
> Simon Riggs <simon(at)2ndquadrant(dot)com> writes:
> > SQL200n specifies a new qualifier on a TRUNCATE command
> > TRUNCATE TABLE foo
> > [ CONTINUE IDENTITY | RESTART IDENTITY ]
>
> > CONTINUE IDENTITY is the default and does nothing, like now.
>
> > RESTART IDENTITY will reset the SERIAL sequences back to the original
> > start value.
>
> > Seems like a % project for the TODO list
>
> Seems like copying syntax from a *draft* standard is a bit premature,
> especially when the amount of functionality added is nil.

It's at the final yes-or-no vote stage. Seems unlikely to be "no" to me,
and it would be good to be seen to be proactive on standards support.

The added functionality in this case isn't nil.

--
Simon Riggs
2ndQuadrant http://www.2ndQuadrant.com

PostgreSQL UK 2008 Conference: http://www.postgresql.org.uk


From: Steve Crawford <scrawford(at)pinpointresearch(dot)com>
To: Simon Riggs <simon(at)2ndquadrant(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: TRUNCATE TABLE with IDENTITY
Date: 2008-03-25 16:08:40
Message-ID: 47E92388.2000404@pinpointresearch.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Simon Riggs wrote:
> RESTART IDENTITY will reset the SERIAL sequences back to the original
> start value.
>
Assuming this feature were to be added....

In cases where the same sequence has been used across multiple tables,
what will be the appropriate response when a user attempts to TRUNCATE
one of those tables with RESTART IDENTITY?

Cheers,
Steve


From: Simon Riggs <simon(at)2ndquadrant(dot)com>
To: Steve Crawford <scrawford(at)pinpointresearch(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: TRUNCATE TABLE with IDENTITY
Date: 2008-03-25 16:18:50
Message-ID: 1206461930.4285.964.camel@ebony.site
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

On Tue, 2008-03-25 at 09:08 -0700, Steve Crawford wrote:
> Simon Riggs wrote:
> > RESTART IDENTITY will reset the SERIAL sequences back to the original
> > start value.
> >
> Assuming this feature were to be added....
>
> In cases where the same sequence has been used across multiple tables,
> what will be the appropriate response when a user attempts to TRUNCATE
> one of those tables with RESTART IDENTITY?

Well, I'm suggesting it as a TODO item, based on the standard. It would
be for whoever took this up to unravel that.

Since that's a weak answer, I'd say it should only reset sequences that
have been placed there automatically through the use of SERIAL or
BIGSERIAL datatypes.

--
Simon Riggs
2ndQuadrant http://www.2ndQuadrant.com

PostgreSQL UK 2008 Conference: http://www.postgresql.org.uk


From: Zoltan Boszormenyi <zb(at)cybertec(dot)at>
To: Simon Riggs <simon(at)2ndquadrant(dot)com>
Cc: Steve Crawford <scrawford(at)pinpointresearch(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: TRUNCATE TABLE with IDENTITY
Date: 2008-03-25 16:40:49
Message-ID: 47E92B11.4040006@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Simon Riggs írta:
> On Tue, 2008-03-25 at 09:08 -0700, Steve Crawford wrote:
>
>> Simon Riggs wrote:
>>
>>> RESTART IDENTITY will reset the SERIAL sequences back to the original
>>> start value.
>>>
>>>
>> Assuming this feature were to be added....
>>
>> In cases where the same sequence has been used across multiple tables,
>> what will be the appropriate response when a user attempts to TRUNCATE
>> one of those tables with RESTART IDENTITY?
>>
>
> Well, I'm suggesting it as a TODO item, based on the standard. It would
> be for whoever took this up to unravel that.
>
> Since that's a weak answer, I'd say it should only reset sequences that
> have been placed there automatically through the use of SERIAL or
> BIGSERIAL datatypes.
>

All of them? PostgreSQL allow multiple SERIALs to be present,
the standard allows only one IDENTITY column in a table.
And what about this case below?

CREATE TABLE t1 (id1 serial, ...);
ALTER SEQUENCE seq_t1_id1 RESTART WITH 5432 CYCLE;

or the equivalent

CREATE SEQUENCE seq_t1_id1 START WITH 5432 CYCLE;
CREATE TABLE t1 (id1 serial, ...);
ALTER SEQUENCE seq_t1_id1 OWNED BY t1.id1;

PostgreSQL doesn't keep the START WITH information.
But it should to perform a "restart" on the sequence,
using the minval in this case wouldn't be correct.

--
----------------------------------
Zoltán Böszörményi
Cybertec Schönig & Schönig GmbH
http://www.postgresql.at/


From: Zoltan Boszormenyi <zb(at)cybertec(dot)at>
To: Simon Riggs <simon(at)2ndquadrant(dot)com>
Cc: Steve Crawford <scrawford(at)pinpointresearch(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: TRUNCATE TABLE with IDENTITY
Date: 2008-03-25 16:42:33
Message-ID: 47E92B79.4020203@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Zoltan Boszormenyi írta:
> Simon Riggs írta:
>> On Tue, 2008-03-25 at 09:08 -0700, Steve Crawford wrote:
>>
>>> Simon Riggs wrote:
>>>
>>>> RESTART IDENTITY will reset the SERIAL sequences back to the original
>>>> start value.
>>>>
>>> Assuming this feature were to be added....
>>>
>>> In cases where the same sequence has been used across multiple
>>> tables, what will be the appropriate response when a user attempts
>>> to TRUNCATE one of those tables with RESTART IDENTITY?
>>>
>>
>> Well, I'm suggesting it as a TODO item, based on the standard. It would
>> be for whoever took this up to unravel that.
>>
>> Since that's a weak answer, I'd say it should only reset sequences that
>> have been placed there automatically through the use of SERIAL or
>> BIGSERIAL datatypes.
>>
>
> All of them? PostgreSQL allow multiple SERIALs to be present,
> the standard allows only one IDENTITY column in a table.
> And what about this case below?
>
> CREATE TABLE t1 (id1 serial, ...);
> ALTER SEQUENCE seq_t1_id1 RESTART WITH 5432 CYCLE;
>
> or the equivalent
>
> CREATE SEQUENCE seq_t1_id1 START WITH 5432 CYCLE;
> CREATE TABLE t1 (id1 serial, ...);

of course
CREATE TABLE t1 (id1 integer, ...);

> ALTER SEQUENCE seq_t1_id1 OWNED BY t1.id1;
>
> PostgreSQL doesn't keep the START WITH information.
> But it should to perform a "restart" on the sequence,
> using the minval in this case wouldn't be correct.
>

--
----------------------------------
Zoltán Böszörményi
Cybertec Schönig & Schönig GmbH
http://www.postgresql.at/


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Zoltan Boszormenyi <zb(at)cybertec(dot)at>
Cc: Simon Riggs <simon(at)2ndquadrant(dot)com>, Steve Crawford <scrawford(at)pinpointresearch(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: TRUNCATE TABLE with IDENTITY
Date: 2008-03-25 16:43:36
Message-ID: 200803251643.m2PGhaE24146@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Zoltan Boszormenyi wrote:
> All of them? PostgreSQL allow multiple SERIALs to be present,
> the standard allows only one IDENTITY column in a table.
> And what about this case below?
>
> CREATE TABLE t1 (id1 serial, ...);
> ALTER SEQUENCE seq_t1_id1 RESTART WITH 5432 CYCLE;
>
> or the equivalent
>
> CREATE SEQUENCE seq_t1_id1 START WITH 5432 CYCLE;
> CREATE TABLE t1 (id1 serial, ...);
> ALTER SEQUENCE seq_t1_id1 OWNED BY t1.id1;
>
> PostgreSQL doesn't keep the START WITH information.
> But it should to perform a "restart" on the sequence,
> using the minval in this case wouldn't be correct.

I do think we need to wait for the standard to be accepted before adding
them to the TODO list as standard-compliant additions, especially
because no one is asking for the syntax yet.

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://postgres.enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +


From: Gregory Stark <stark(at)enterprisedb(dot)com>
To: "Simon Riggs" <simon(at)2ndquadrant(dot)com>
Cc: <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: TRUNCATE TABLE with IDENTITY
Date: 2008-03-25 16:45:45
Message-ID: 87r6dyn45i.fsf@oxford.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

"Simon Riggs" <simon(at)2ndquadrant(dot)com> writes:

> SQL200n specifies a new qualifier on a TRUNCATE command
>
> TRUNCATE TABLE foo
> [ CONTINUE IDENTITY | RESTART IDENTITY ]
>
> CONTINUE IDENTITY is the default and does nothing, like now.
>
> RESTART IDENTITY will reset the SERIAL sequences back to the original
> start value.
>
> Seems like a % project for the TODO list

I think we need SQL standard IDENTITY columns before we can consider adding
SQL standard CONTINUE IDENTITY or RESTART IDENTITY clauses.

The reason the last attempt to add them petered out was precisely because they
*don't* exactly line up with the semantics of sequences so I don't imagine
attempting to shoehorn sequences into these clauses is likely to pan out.

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com
Ask me about EnterpriseDB's RemoteDBA services!


From: Decibel! <decibel(at)decibel(dot)org>
To: Zoltan Boszormenyi <zb(at)cybertec(dot)at>
Cc: Simon Riggs <simon(at)2ndquadrant(dot)com>, Steve Crawford <scrawford(at)pinpointresearch(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: TRUNCATE TABLE with IDENTITY
Date: 2008-04-02 22:53:10
Message-ID: 207EDE13-F000-4090-8FC6-77560736B60D@decibel.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

On Mar 25, 2008, at 11:40 AM, Zoltan Boszormenyi wrote:
> All of them? PostgreSQL allow multiple SERIALs to be present,
> the standard allows only one IDENTITY column in a table.
> And what about this case below?
>
> CREATE TABLE t1 (id1 serial, ...);
> ALTER SEQUENCE seq_t1_id1 RESTART WITH 5432 CYCLE;
>
> or the equivalent
>
> CREATE SEQUENCE seq_t1_id1 START WITH 5432 CYCLE;
> CREATE TABLE t1 (id1 serial, ...);
> ALTER SEQUENCE seq_t1_id1 OWNED BY t1.id1;
>
> PostgreSQL doesn't keep the START WITH information.
> But it should to perform a "restart" on the sequence,
> using the minval in this case wouldn't be correct.

I think you misunderstand what ALTER SEQUENCE RESTART does; it only
changes the current value of the sequence.
--
Decibel!, aka Jim C. Nasby, Database Architect decibel(at)decibel(dot)org
Give your computer some brain candy! www.distributed.net Team #1828


From: Zoltan Boszormenyi <zb(at)cybertec(dot)at>
To: "Decibel!" <decibel(at)decibel(dot)org>
Cc: Simon Riggs <simon(at)2ndquadrant(dot)com>, Steve Crawford <scrawford(at)pinpointresearch(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: TRUNCATE TABLE with IDENTITY
Date: 2008-04-03 05:52:25
Message-ID: 47F47099.7020806@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Decibel! írta:
> On Mar 25, 2008, at 11:40 AM, Zoltan Boszormenyi wrote:
>> All of them? PostgreSQL allow multiple SERIALs to be present,
>> the standard allows only one IDENTITY column in a table.
>> And what about this case below?
>>
>> CREATE TABLE t1 (id1 serial, ...);
>> ALTER SEQUENCE seq_t1_id1 RESTART WITH 5432 CYCLE;
>>
>> or the equivalent
>>
>> CREATE SEQUENCE seq_t1_id1 START WITH 5432 CYCLE;
>> CREATE TABLE t1 (id1 serial, ...);
>> ALTER SEQUENCE seq_t1_id1 OWNED BY t1.id1;
>>
>> PostgreSQL doesn't keep the START WITH information.
>> But it should to perform a "restart" on the sequence,
>> using the minval in this case wouldn't be correct.
>
>
> I think you misunderstand what ALTER SEQUENCE RESTART does; it only
> changes the current value of the sequence.

I didn't misunderstood, I know that. I quoted both
because (currently) CREATE SEQUENCE ... START WITH does the same.

zozo=> create sequence seq1 start with 327;
CREATE SEQUENCE
zozo=> select * from seq1;
sequence_name | last_value | increment_by | max_value |
min_value | cache_value | log_cnt | is_cycled | is_called
---------------+------------+--------------+---------------------+-----------+-------------+---------+-----------+-----------
seq1 | 327 | 1 | 9223372036854775807
| 1 | 1 | 1 | f | f
(1 row)

Note the difference between "min_value" and "last_value".
Using the standard syntax of

CREATE TABLE (
id integer IDENTITY GENERATED ALWAYS AS (START WITH 327),
...
);

and assuming you use the existing sequence infrastructure
there's a problem with TRUNCATE ... RESTART IDENTITY;
Where is the info in the sequence to provide restarting with
the _original_ start value?

--
----------------------------------
Zoltán Böszörményi
Cybertec Schönig & Schönig GmbH
http://www.postgresql.at/


From: Decibel! <decibel(at)decibel(dot)org>
To: Zoltan Boszormenyi <zb(at)cybertec(dot)at>
Cc: Simon Riggs <simon(at)2ndquadrant(dot)com>, Steve Crawford <scrawford(at)pinpointresearch(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: TRUNCATE TABLE with IDENTITY
Date: 2008-04-07 18:26:52
Message-ID: 7C3D84AC-E918-4D42-95E7-4AECBF0A3C52@decibel.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

On Apr 3, 2008, at 12:52 AM, Zoltan Boszormenyi wrote:
> Where is the info in the sequence to provide restarting with
> the _original_ start value?

There isn't any. If you want the sequence to start at some magic
value, adjust the minimum value.
--
Decibel!, aka Jim C. Nasby, Database Architect decibel(at)decibel(dot)org
Give your computer some brain candy! www.distributed.net Team #1828


From: Zoltan Boszormenyi <zb(at)cybertec(dot)at>
To: "Decibel!" <decibel(at)decibel(dot)org>
Cc: Simon Riggs <simon(at)2ndquadrant(dot)com>, Steve Crawford <scrawford(at)pinpointresearch(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: TRUNCATE TABLE with IDENTITY
Date: 2008-04-07 19:37:40
Message-ID: 47FA7804.3080705@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Decibel! írta:
> On Apr 3, 2008, at 12:52 AM, Zoltan Boszormenyi wrote:
>> Where is the info in the sequence to provide restarting with
>> the _original_ start value?
>
> There isn't any. If you want the sequence to start at some magic
> value, adjust the minimum value.

There's the START WITH option for IDENTITY columns and this below
is paragraph 8 under General rules of 14.10 <truncate table statement>
in 6WD2_02_Foundation_2007-12.pdf (page 902):

8) If RESTART IDENTITY is specified and the table descriptor of T
includes a column descriptor IDCD of
an identity column, then:
a) Let CN be the column name included in IDCD and let SV be the start
value included in IDCD.
b) The following <alter table statement> is effectively executed
without further Access Rule checking:
ALTER TABLE TN ALTER COLUMN CN RESTART WITH SV

This says that the original start value is used, not the minimum value.
IDENTITY has the same options as CREATE SEQUENCE. In fact the
"identity column specification" links to "11.63 <sequence generator
definition>"
when it comes to IDENTITY sequence options. And surprise, surprise,
"11.64 <alter sequence generator statement>" now defines
ALTER SEQUENCE sn RESTART [WITH newvalue]
where omitting the "WITH newval" part also uses the original start value.

Best regards,
Zoltán Böszörményi

--
----------------------------------
Zoltán Böszörményi
Cybertec Schönig & Schönig GmbH
http://www.postgresql.at/


From: Zoltan Boszormenyi <zb(at)cybertec(dot)at>
To: List pgsql-patches <pgsql-patches(at)postgresql(dot)org>
Cc: "Decibel!" <decibel(at)decibel(dot)org>, Simon Riggs <simon(at)2ndquadrant(dot)com>, Steve Crawford <scrawford(at)pinpointresearch(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [HACKERS] TRUNCATE TABLE with IDENTITY
Date: 2008-04-08 09:09:39
Message-ID: 47FB3653.1080307@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Zoltan Boszormenyi írta:
> Decibel! írta:
>> On Apr 3, 2008, at 12:52 AM, Zoltan Boszormenyi wrote:
>>> Where is the info in the sequence to provide restarting with
>>> the _original_ start value?
>>
>> There isn't any. If you want the sequence to start at some magic
>> value, adjust the minimum value.
>
> There's the START WITH option for IDENTITY columns and this below
> is paragraph 8 under General rules of 14.10 <truncate table statement>
> in 6WD2_02_Foundation_2007-12.pdf (page 902):
>
> 8) If RESTART IDENTITY is specified and the table descriptor of T
> includes a column descriptor IDCD of
> an identity column, then:
> a) Let CN be the column name included in IDCD and let SV be the
> start value included in IDCD.
> b) The following <alter table statement> is effectively executed
> without further Access Rule checking:
> ALTER TABLE TN ALTER COLUMN CN RESTART WITH SV
>
> This says that the original start value is used, not the minimum value.
> IDENTITY has the same options as CREATE SEQUENCE. In fact the
> "identity column specification" links to "11.63 <sequence generator
> definition>"
> when it comes to IDENTITY sequence options. And surprise, surprise,
> "11.64 <alter sequence generator statement>" now defines
> ALTER SEQUENCE sn RESTART [WITH newvalue]
> where omitting the "WITH newval" part also uses the original start value.
>
> Best regards,
> Zoltán Böszörményi

Attached patch implements the extension found in the current SQL200n draft,
implementing stored start value and supporting ALTER SEQUENCE seq RESTART;
Some error check are also added to prohibit CREATE SEQUENCE ... RESTART ...
and ALTER SEQUENCE ... START ...

Best regards,
Zoltán Böszörményi

--
----------------------------------
Zoltán Böszörményi
Cybertec Schönig & Schönig GmbH
http://www.postgresql.at/

Attachment Content-Type Size
sql2008-compliant-seq.patch text/x-patch 10.6 KB

From: Zoltan Boszormenyi <zb(at)cybertec(dot)at>
To: List pgsql-patches <pgsql-patches(at)postgresql(dot)org>
Cc: "Decibel!" <decibel(at)decibel(dot)org>, Simon Riggs <simon(at)2ndquadrant(dot)com>, Steve Crawford <scrawford(at)pinpointresearch(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [HACKERS] TRUNCATE TABLE with IDENTITY
Date: 2008-04-21 20:42:30
Message-ID: 480CFC36.30407@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Zoltan Boszormenyi írta:
> Zoltan Boszormenyi írta:
>> Decibel! írta:
>>> On Apr 3, 2008, at 12:52 AM, Zoltan Boszormenyi wrote:
>>>> Where is the info in the sequence to provide restarting with
>>>> the _original_ start value?
>>>
>>> There isn't any. If you want the sequence to start at some magic
>>> value, adjust the minimum value.
>>
>> There's the START WITH option for IDENTITY columns and this below
>> is paragraph 8 under General rules of 14.10 <truncate table statement>
>> in 6WD2_02_Foundation_2007-12.pdf (page 902):
>>
>> 8) If RESTART IDENTITY is specified and the table descriptor of T
>> includes a column descriptor IDCD of
>> an identity column, then:
>> a) Let CN be the column name included in IDCD and let SV be the
>> start value included in IDCD.
>> b) The following <alter table statement> is effectively executed
>> without further Access Rule checking:
>> ALTER TABLE TN ALTER COLUMN CN RESTART WITH SV
>>
>> This says that the original start value is used, not the minimum value.
>> IDENTITY has the same options as CREATE SEQUENCE. In fact the
>> "identity column specification" links to "11.63 <sequence generator
>> definition>"
>> when it comes to IDENTITY sequence options. And surprise, surprise,
>> "11.64 <alter sequence generator statement>" now defines
>> ALTER SEQUENCE sn RESTART [WITH newvalue]
>> where omitting the "WITH newval" part also uses the original start
>> value.
>>
>> Best regards,
>> Zoltán Böszörményi
>
> Attached patch implements the extension found in the current SQL200n
> draft,
> implementing stored start value and supporting ALTER SEQUENCE seq
> RESTART;
> Some error check are also added to prohibit CREATE SEQUENCE ...
> RESTART ...
> and ALTER SEQUENCE ... START ...
>
> Best regards,
> Zoltán Böszörményi

Updated patch implements TRUNCATE ... RESTART IDENTITY
which restarts all owned sequences for the truncated table(s).
Regression tests updated, documentation added. pg_dump was
also extended to output original[1] START value for creating SEQUENCEs.

[1] For 8.3 and below I could only guesstimate it as MINVALUE for ascending
and MAXVALUE for descending sequences.

Best regards,
Zoltán Böszörményi

--
----------------------------------
Zoltán Böszörményi
Cybertec Schönig & Schönig GmbH
http://www.postgresql.at/

Attachment Content-Type Size
sql2008-compliant-seq-v2.patch.gz application/x-tar 8.3 KB

From: Zoltan Boszormenyi <zb(at)cybertec(dot)at>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: "Decibel!" <decibel(at)decibel(dot)org>, Simon Riggs <simon(at)2ndquadrant(dot)com>, Steve Crawford <scrawford(at)pinpointresearch(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: TRUNCATE TABLE with IDENTITY
Date: 2008-04-22 15:19:11
Message-ID: 480E01EF.5020503@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Hi,

Zoltan Boszormenyi írta:
> Updated patch implements TRUNCATE ... RESTART IDENTITY
> which restarts all owned sequences for the truncated table(s).
> Regression tests updated, documentation added. pg_dump was
> also extended to output original[1] START value for creating SEQUENCEs.
>
> [1] For 8.3 and below I could only guesstimate it as MINVALUE for
> ascending
> and MAXVALUE for descending sequences.
>
> Best regards,
> Zoltán Böszörményi

I just saw this on the CommitFest:May page:

"alvherre says: I'm not sure if this is the same patch in the previous
entry, or a different feature"

I wanted to clarify, the second patch contains two features.
1. stored start value for sequences, ALTER SEQUENCE ... RESTART;
2. (builds on 1.) TRUNCATE ... RESTART IDENTITY;

Best regards,
Zoltán Böszörményi

--
----------------------------------
Zoltán Böszörményi
Cybertec Schönig & Schönig GmbH
http://www.postgresql.at/


From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Zoltan Boszormenyi <zb(at)cybertec(dot)at>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: TRUNCATE TABLE with IDENTITY
Date: 2008-04-22 16:01:02
Message-ID: 20080422160102.GC6912@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Zoltan Boszormenyi wrote:

> I just saw this on the CommitFest:May page:
>
> "alvherre says: I'm not sure if this is the same patch in the previous
> entry, or a different feature"
>
> I wanted to clarify, the second patch contains two features.
> 1. stored start value for sequences, ALTER SEQUENCE ... RESTART;
> 2. (builds on 1.) TRUNCATE ... RESTART IDENTITY;

Does this mean that the first patch can be removed? Please do so in
that case, and remove my comment too. And perhaps the description of
the patch will need a little fixing, too.

--
Alvaro Herrera http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Zoltan Boszormenyi <zb(at)cybertec(dot)at>
Cc: List pgsql-patches <pgsql-patches(at)postgresql(dot)org>, "Decibel!" <decibel(at)decibel(dot)org>, Simon Riggs <simon(at)2ndquadrant(dot)com>, Steve Crawford <scrawford(at)pinpointresearch(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [HACKERS] TRUNCATE TABLE with IDENTITY
Date: 2008-05-16 23:41:15
Message-ID: 3919.1210981275@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Zoltan Boszormenyi <zb(at)cybertec(dot)at> writes:
>> Attached patch implements the extension found in the current SQL200n draft,
>> implementing stored start value and supporting ALTER SEQUENCE seq RESTART;

> Updated patch implements TRUNCATE ... RESTART IDENTITY
> which restarts all owned sequences for the truncated table(s).

Applied with corrections. Most notably, since ALTER SEQUENCE RESTART
is nontransactional like most other ALTER SEQUENCE operations, I
rearranged things to try to ensure that foreseeable failures like
deadlock and lack of permissions would be detected before TRUNCATE
starts to issue any RESTART commands.

One interesting point here is that the patch as submitted allowed
ALTER SEQUENCE MINVALUE/MAXVALUE to be used to set a sequence range
that the original START value was outside of. This would result in
a failure at ALTER SEQUENCE RESTART. Since, as stated above, we
really don't want that happening during TRUNCATE, I adjusted the
patch to make such an ALTER SEQUENCE fail. This is at least potentially
an incompatible change: command sequences that used to be legal could
now fail. I doubt it's very likely to bite anyone in practice, though.

regards, tom lane


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Zoltan Boszormenyi <zb(at)cybertec(dot)at>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [PATCHES] TRUNCATE TABLE with IDENTITY
Date: 2008-05-17 00:15:37
Message-ID: 4346.1210983337@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

I wrote:
> One interesting point here is that the patch as submitted allowed
> ALTER SEQUENCE MINVALUE/MAXVALUE to be used to set a sequence range
> that the original START value was outside of. This would result in
> a failure at ALTER SEQUENCE RESTART. Since, as stated above, we
> really don't want that happening during TRUNCATE, I adjusted the
> patch to make such an ALTER SEQUENCE fail. This is at least potentially
> an incompatible change: command sequences that used to be legal could
> now fail. I doubt it's very likely to bite anyone in practice, though.

It occurs to me that we could define
ALTER SEQUENCE s START WITH x
(which is syntactically legal, but rejected by sequence.c at the moment)
as updating the stored start_value and thus affecting what future
ALTER SEQUENCE RESTART commands will do. Right now there is simply
no way to change start_value after sequence creation, which is pretty
strange considering we let you change every other sequence parameter.
It would also provide a way out for anyone who does want to change the
minval/maxval as sketched above.

I think this is about a ten-line change as far as the code goes...
any objections?

regards, tom lane


From: Neil Conway <neilc(at)samurai(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Zoltan Boszormenyi <zb(at)cybertec(dot)at>, List pgsql-patches <pgsql-patches(at)postgresql(dot)org>, Decibel! <decibel(at)decibel(dot)org>, Simon Riggs <simon(at)2ndquadrant(dot)com>, Steve Crawford <scrawford(at)pinpointresearch(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [HACKERS] TRUNCATE TABLE with IDENTITY
Date: 2008-05-17 01:35:52
Message-ID: 1210988152.10381.12.camel@goldbach
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

On Fri, 2008-05-16 at 19:41 -0400, Tom Lane wrote:
> Applied with corrections. Most notably, since ALTER SEQUENCE RESTART
> is nontransactional like most other ALTER SEQUENCE operations, I
> rearranged things to try to ensure that foreseeable failures like
> deadlock and lack of permissions would be detected before TRUNCATE
> starts to issue any RESTART commands.

Ugh. The fact that the RESTART IDENTITY part of TRUNCATE is
non-transactional is a pretty unsightly wort. I would also quarrel with
your addition to the docs that suggests this is only an issue "in
practice" if TRUNCATE RESTART IDENTITY is used in a transaction block:
unpredictable failures (such as OOM or query cancellation) can certainly
occur in practice, and would be very disruptive (e.g. if the sequence
values are stored into a column with a UNIQUE constraint, it would break
all inserting transactions until the DBA intervenes).

I wonder if it would be possible to make the sequence operations
performed by TRUNCATE transactional: while the TRUNCATE remains
uncommitted, it should be okay to block concurrent access to the
sequence.

-Neil


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Neil Conway <neilc(at)samurai(dot)com>
Cc: Zoltan Boszormenyi <zb(at)cybertec(dot)at>, List pgsql-patches <pgsql-patches(at)postgresql(dot)org>, Decibel! <decibel(at)decibel(dot)org>, Simon Riggs <simon(at)2ndquadrant(dot)com>, Steve Crawford <scrawford(at)pinpointresearch(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [HACKERS] TRUNCATE TABLE with IDENTITY
Date: 2008-05-17 01:50:01
Message-ID: 17769.1210989001@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Neil Conway <neilc(at)samurai(dot)com> writes:
> Ugh. The fact that the RESTART IDENTITY part of TRUNCATE is
> non-transactional is a pretty unsightly wort.

Actually, I agree. Shall we just revert that feature? The ALTER
SEQUENCE part of this patch is clean and useful, but I'm less than
enamored of the TRUNCATE part.

regards, tom lane


From: Simon Riggs <simon(at)2ndquadrant(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Neil Conway <neilc(at)samurai(dot)com>, Zoltan Boszormenyi <zb(at)cybertec(dot)at>, List pgsql-patches <pgsql-patches(at)postgresql(dot)org>, Decibel! <decibel(at)decibel(dot)org>, Steve Crawford <scrawford(at)pinpointresearch(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [HACKERS] TRUNCATE TABLE with IDENTITY
Date: 2008-05-17 08:58:10
Message-ID: 1211014690.18922.136.camel@ebony.site
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches


On Fri, 2008-05-16 at 21:50 -0400, Tom Lane wrote:
> Neil Conway <neilc(at)samurai(dot)com> writes:
> > Ugh. The fact that the RESTART IDENTITY part of TRUNCATE is
> > non-transactional is a pretty unsightly wort.
>
> Actually, I agree. Shall we just revert that feature? The ALTER
> SEQUENCE part of this patch is clean and useful, but I'm less than
> enamored of the TRUNCATE part.

Perhaps, but we should also take into account that TRUNCATE is not and
never will be MVCC compliant, so its not something you'd expect to run
except as a maintenance action. If we do keep it, I would suggest that
we add a WARNING so that the effects are clearly recorded.

--
Simon Riggs www.2ndQuadrant.com
PostgreSQL Training, Services and Support


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Simon Riggs <simon(at)2ndquadrant(dot)com>
Cc: Neil Conway <neilc(at)samurai(dot)com>, Zoltan Boszormenyi <zb(at)cybertec(dot)at>, List pgsql-patches <pgsql-patches(at)postgresql(dot)org>, Decibel! <decibel(at)decibel(dot)org>, Steve Crawford <scrawford(at)pinpointresearch(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [HACKERS] TRUNCATE TABLE with IDENTITY
Date: 2008-05-17 16:04:55
Message-ID: 26734.1211040295@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Simon Riggs <simon(at)2ndquadrant(dot)com> writes:
> On Fri, 2008-05-16 at 21:50 -0400, Tom Lane wrote:
>> Actually, I agree. Shall we just revert that feature?

> Perhaps, but we should also take into account that TRUNCATE is not and
> never will be MVCC compliant, so its not something you'd expect to run
> except as a maintenance action.

Good point. I had a couple of further thoughts this morning:

1. The case Neil is worried about is something like

BEGIN;
TRUNCATE TABLE foo RESTART IDENTITY;
COPY foo FROM ...;
COMMIT;

If the COPY fails partway through, the old table contents are restored,
but the sequences are not. However removing RESTART IDENTITY will not
remove the hazard, because there is no difference between this and

BEGIN;
TRUNCATE TABLE foo;
SELECT setval('foo_id', 1);
COPY foo FROM ...;
COMMIT;

other than the latter adding a little extra chance for pilot error in
resetting the wrong sequence. So if we revert the patch we haven't
accomplished much except to take away an opportunity to point out the
risk. I vote for leaving the patch in and rewriting the <warning>
to point out this risk.

2. I had first dismissed Neil's idea of transactional sequence updates
as impossible, but on second look it could be done. Suppose RESTART
IDENTITY does this for each sequence;

* obtain AccessExclusiveLock;
* assign a new relfilenode;
* insert a sequence row with all parameters copied except
last_value copies start_value;
* hold AccessExclusiveLock till commit.

IOW just like truncate-and-reload, but for a sequence. Within the
current backend, subsequent operations see the new sequence values.
If the transaction rolls back, the old sequence relfilenode is still
there and untouched.

It's slightly annoying to need to lock out other backends' nextval
operations, but for the use-case of TRUNCATE this doesn't seem
like it's really much of a problem.

I'm not sure if it'd be worth exposing this behavior as a separate
user-visible command (CREATE OR REPLACE SEQUENCE, maybe?), but it seems
worth doing to make TRUNCATE-and-reload less of a foot gun.

So what I think we should do is leave the patch there, revise the
warning per Neil's complaint, and add a TODO item to reimplement RESTART
IDENTITY transactionally.

regards, tom lane


From: Simon Riggs <simon(at)2ndquadrant(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Neil Conway <neilc(at)samurai(dot)com>, Zoltan Boszormenyi <zb(at)cybertec(dot)at>, List pgsql-patches <pgsql-patches(at)postgresql(dot)org>, Decibel! <decibel(at)decibel(dot)org>, Steve Crawford <scrawford(at)pinpointresearch(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [HACKERS] TRUNCATE TABLE with IDENTITY
Date: 2008-05-17 16:23:58
Message-ID: 1211041438.18922.166.camel@ebony.site
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches


On Sat, 2008-05-17 at 12:04 -0400, Tom Lane wrote:

> So what I think we should do is leave the patch there, revise the
> warning per Neil's complaint, and add a TODO item to reimplement
> RESTART IDENTITY transactionally.

Sounds good.

--
Simon Riggs www.2ndQuadrant.com
PostgreSQL Training, Services and Support


From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Simon Riggs <simon(at)2ndquadrant(dot)com>, Neil Conway <neilc(at)samurai(dot)com>, Zoltan Boszormenyi <zb(at)cybertec(dot)at>, List pgsql-patches <pgsql-patches(at)postgresql(dot)org>, Decibel! <decibel(at)decibel(dot)org>, Steve Crawford <scrawford(at)pinpointresearch(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [HACKERS] TRUNCATE TABLE with IDENTITY
Date: 2008-06-08 00:30:50
Message-ID: 20080608003050.GU16502@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Tom Lane wrote:

> 2. I had first dismissed Neil's idea of transactional sequence updates
> as impossible, but on second look it could be done. Suppose RESTART
> IDENTITY does this for each sequence;
>
> * obtain AccessExclusiveLock;
> * assign a new relfilenode;
> * insert a sequence row with all parameters copied except
> last_value copies start_value;
> * hold AccessExclusiveLock till commit.

Hmm, this kills the idea of moving sequence data to a single
non-transactional catalog :-(

> So what I think we should do is leave the patch there, revise the
> warning per Neil's complaint, and add a TODO item to reimplement RESTART
> IDENTITY transactionally.

I think the TODO item did not make it, but the docs do seem updated.

--
Alvaro Herrera http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: Simon Riggs <simon(at)2ndquadrant(dot)com>, Neil Conway <neilc(at)samurai(dot)com>, Zoltan Boszormenyi <zb(at)cybertec(dot)at>, List pgsql-patches <pgsql-patches(at)postgresql(dot)org>, Decibel! <decibel(at)decibel(dot)org>, Steve Crawford <scrawford(at)pinpointresearch(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [HACKERS] TRUNCATE TABLE with IDENTITY
Date: 2008-06-08 16:26:06
Message-ID: 4909.1212942366@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Alvaro Herrera <alvherre(at)commandprompt(dot)com> writes:
> Tom Lane wrote:
>> 2. I had first dismissed Neil's idea of transactional sequence updates
>> as impossible, but on second look it could be done. Suppose RESTART
>> IDENTITY does this for each sequence;
>>
>> * obtain AccessExclusiveLock;
>> * assign a new relfilenode;
>> * insert a sequence row with all parameters copied except
>> last_value copies start_value;
>> * hold AccessExclusiveLock till commit.

> Hmm, this kills the idea of moving sequence data to a single
> non-transactional catalog :-(

Well, there are a number of holes in our ideas of how to do that anyway.
But offhand I don't see why we couldn't distinguish regular heap_update
from update_in_place on single rows within a catalog.

regards, tom lane


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Simon Riggs <simon(at)2ndquadrant(dot)com>, Neil Conway <neilc(at)samurai(dot)com>, Zoltan Boszormenyi <zb(at)cybertec(dot)at>, List pgsql-patches <pgsql-patches(at)postgresql(dot)org>, "Decibel!" <decibel(at)decibel(dot)org>, Steve Crawford <scrawford(at)pinpointresearch(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [PATCHES] TRUNCATE TABLE with IDENTITY
Date: 2008-07-01 18:09:28
Message-ID: 200807011809.m61I9SR02927@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers pgsql-patches

Alvaro Herrera wrote:
> Tom Lane wrote:
>
> > 2. I had first dismissed Neil's idea of transactional sequence updates
> > as impossible, but on second look it could be done. Suppose RESTART
> > IDENTITY does this for each sequence;
> >
> > * obtain AccessExclusiveLock;
> > * assign a new relfilenode;
> > * insert a sequence row with all parameters copied except
> > last_value copies start_value;
> > * hold AccessExclusiveLock till commit.
>
> Hmm, this kills the idea of moving sequence data to a single
> non-transactional catalog :-(
>
> > So what I think we should do is leave the patch there, revise the
> > warning per Neil's complaint, and add a TODO item to reimplement RESTART
> > IDENTITY transactionally.
>
> I think the TODO item did not make it, but the docs do seem updated.

Done:

* Fix TRUNCATE ... RESTART IDENTITY so its affect on sequences is rolled
back on transaction abort

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +