pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #14827: "ALTER TABLE... IF NOT EXISTS...ADD.. BIGSERIAL" leaves extra sequences
8+ messages / 5 participants
[nested] [flat]
* BUG #14827: "ALTER TABLE... IF NOT EXISTS...ADD.. BIGSERIAL" leaves extra sequences
@ 2017-09-25 08:45 hvisage@gmail.com
0 siblings, 1 reply; 8+ messages in thread
From: hvisage@gmail.com @ 2017-09-25 08:45 UTC (permalink / raw)
To: pgsql-bugs; +Cc: hvisage@gmail.com
The following bug has been logged on the website:
Bug reference: 14827
Logged by: Hendrik Visage
Email address: hvisage@gmail.com
PostgreSQL version: 9.6.5
Operating system: Debian 9
Description:
Goodday,
With an alter table to add an UID field if it doesn't exist, the process
creates a sequence, but when the column does exist, the created sequence,
that isn't used, is just left there, and subsequent runs keeps adding extra
sequences.
postgres@tracsdbhvt01:~$ cat test-serial.sql
create database test;
\c test
create table test_serial ( teststring varchar(5));
alter table test_serial add column if not exists uid BIGSERIAL;
alter table test_serial add column if not exists uid BIGSERIAL;
\d
\d test_serial
postgres@tracsdbhvt01:~$ psql -p 5433 < test-serial.sql
CREATE DATABASE
You are now connected to database "test" as user "postgres".
CREATE TABLE
ALTER TABLE
NOTICE: column "uid" of relation "test_serial" already exists, skipping
ALTER TABLE
List of relations
Schema | Name | Type | Owner
--------+----------------------+----------+----------
public | test_serial | table | postgres
public | test_serial_uid_seq | sequence | postgres
public | test_serial_uid_seq1 | sequence | postgres
(3 rows)
Table "public.test_serial"
Column | Type | Modifiers
------------+----------------------+-----------------------------------------------------------
teststring | character varying(5) |
uid | bigint | not null default
nextval('test_serial_uid_seq'::regclass)
postgres@tracsdbhvt01:~$
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: BUG #14827: "ALTER TABLE... IF NOT EXISTS...ADD.. BIGSERIAL" leaves extra sequences
@ 2017-09-26 13:04 Fabrízio de Royes Mello <fabriziomello@gmail.com>
parent: hvisage@gmail.com
0 siblings, 1 reply; 8+ messages in thread
From: Fabrízio de Royes Mello @ 2017-09-26 13:04 UTC (permalink / raw)
To: hvisage@gmail.com; +Cc: pgsql-bugs
On Mon, Sep 25, 2017 at 5:45 AM, <hvisage@gmail.com> wrote:
>
> The following bug has been logged on the website:
>
> Bug reference: 14827
> Logged by: Hendrik Visage
> Email address: hvisage@gmail.com
> PostgreSQL version: 9.6.5
> Operating system: Debian 9
> Description:
>
> Goodday,
> With an alter table to add an UID field if it doesn't exist, the process
> creates a sequence, but when the column does exist, the created sequence,
> that isn't used, is just left there, and subsequent runs keeps adding
extra
> sequences.
>
> postgres@tracsdbhvt01:~$ cat test-serial.sql
> create database test;
> \c test
> create table test_serial ( teststring varchar(5));
> alter table test_serial add column if not exists uid BIGSERIAL;
> alter table test_serial add column if not exists uid BIGSERIAL;
> \d
> \d test_serial
> postgres@tracsdbhvt01:~$ psql -p 5433 < test-serial.sql
> CREATE DATABASE
> You are now connected to database "test" as user "postgres".
> CREATE TABLE
> ALTER TABLE
> NOTICE: column "uid" of relation "test_serial" already exists, skipping
> ALTER TABLE
> List of relations
> Schema | Name | Type | Owner
> --------+----------------------+----------+----------
> public | test_serial | table | postgres
> public | test_serial_uid_seq | sequence | postgres
> public | test_serial_uid_seq1 | sequence | postgres
> (3 rows)
>
> Table "public.test_serial"
> Column | Type | Modifiers
>
------------+----------------------+-----------------------------------------------------------
> teststring | character varying(5) |
> uid | bigint | not null default
> nextval('test_serial_uid_seq'::regclass)
>
> postgres@tracsdbhvt01:~$
>
>
That's awful... I'll take a look into it.
--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL
>> Timbira: http://www.timbira.com.br
>> Blog: http://fabriziomello.github.io
>> Linkedin: http://br.linkedin.com/in/fabriziomello
>> Twitter: http://twitter.com/fabriziomello
>> Github: http://github.com/fabriziomello
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: BUG #14827: "ALTER TABLE... IF NOT EXISTS...ADD.. BIGSERIAL" leaves extra sequences
@ 2017-09-26 19:30 Fabrízio de Royes Mello <fabriziomello@gmail.com>
parent: Fabrízio de Royes Mello <fabriziomello@gmail.com>
0 siblings, 1 reply; 8+ messages in thread
From: Fabrízio de Royes Mello @ 2017-09-26 19:30 UTC (permalink / raw)
To: hvisage@gmail.com; +Cc: pgsql-bugs
On Tue, Sep 26, 2017 at 10:04 AM, Fabrízio de Royes Mello <
fabriziomello@gmail.com> wrote:
>
> On Mon, Sep 25, 2017 at 5:45 AM, <hvisage@gmail.com> wrote:
> >
> > The following bug has been logged on the website:
> >
> > Bug reference: 14827
> > Logged by: Hendrik Visage
> > Email address: hvisage@gmail.com
> > PostgreSQL version: 9.6.5
> > Operating system: Debian 9
> > Description:
> >
> > Goodday,
> > With an alter table to add an UID field if it doesn't exist, the
process
> > creates a sequence, but when the column does exist, the created
sequence,
> > that isn't used, is just left there, and subsequent runs keeps adding
extra
> > sequences.
> >
> > postgres@tracsdbhvt01:~$ cat test-serial.sql
> > create database test;
> > \c test
> > create table test_serial ( teststring varchar(5));
> > alter table test_serial add column if not exists uid BIGSERIAL;
> > alter table test_serial add column if not exists uid BIGSERIAL;
> > \d
> > \d test_serial
> > postgres@tracsdbhvt01:~$ psql -p 5433 < test-serial.sql
> > CREATE DATABASE
> > You are now connected to database "test" as user "postgres".
> > CREATE TABLE
> > ALTER TABLE
> > NOTICE: column "uid" of relation "test_serial" already exists, skipping
> > ALTER TABLE
> > List of relations
> > Schema | Name | Type | Owner
> > --------+----------------------+----------+----------
> > public | test_serial | table | postgres
> > public | test_serial_uid_seq | sequence | postgres
> > public | test_serial_uid_seq1 | sequence | postgres
> > (3 rows)
> >
> > Table "public.test_serial"
> > Column | Type | Modifiers
> >
------------+----------------------+-----------------------------------------------------------
> > teststring | character varying(5) |
> > uid | bigint | not null default
> > nextval('test_serial_uid_seq'::regclass)
> >
> > postgres@tracsdbhvt01:~$
> >
> >
>
> That's awful... I'll take a look into it.
>
I didn't came with better solution, but for now what I did is inside
transformaAlterTableStmt when calling transformColumnDefinition now we pass
down "AlterTableStmt->missing_ok" to check and skip CREATE SEQUENCE
statements when use SERIAL pseudo-types.
It's not an elegant solution because during ATExecAddColumn we check it
again by calling check_for_column_name_collision... Ideas are very welcome?
Att,
--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL
>> Timbira: http://www.timbira.com.br
>> Blog: http://fabriziomello.github.io
>> Linkedin: http://br.linkedin.com/in/fabriziomello
>> Twitter: http://twitter.com/fabriziomello
>> Github: http://github.com/fabriziomello
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
fix_bug_14827_v1.patch (text/x-patch, 6.2K) download
diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c
index 27e568f..6aa7f87 100644
--- a/src/backend/parser/parse_utilcmd.c
+++ b/src/backend/parser/parse_utilcmd.c
@@ -110,7 +110,7 @@ typedef struct
static void transformColumnDefinition(CreateStmtContext *cxt,
- ColumnDef *column);
+ ColumnDef *column, bool if_not_exists);
static void transformTableConstraint(CreateStmtContext *cxt,
Constraint *constraint);
static void transformTableLikeClause(CreateStmtContext *cxt,
@@ -138,7 +138,8 @@ static void transformPartitionCmd(CreateStmtContext *cxt, PartitionCmd *cmd);
static void validateInfiniteBounds(ParseState *pstate, List *blist);
static Const *transformPartitionBoundValue(ParseState *pstate, A_Const *con,
const char *colName, Oid colType, int32 colTypmod);
-
+static bool skip_serial_add_column_if_not_exists(Relation rel, const char *colname,
+ bool if_not_exists);
/*
* transformCreateStmt -
@@ -277,7 +278,7 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString)
switch (nodeTag(element))
{
case T_ColumnDef:
- transformColumnDefinition(&cxt, (ColumnDef *) element);
+ transformColumnDefinition(&cxt, (ColumnDef *) element, false);
break;
case T_Constraint:
@@ -500,7 +501,7 @@ generateSerialExtraStmts(CreateStmtContext *cxt, ColumnDef *column,
* Also used in ALTER TABLE ADD COLUMN
*/
static void
-transformColumnDefinition(CreateStmtContext *cxt, ColumnDef *column)
+transformColumnDefinition(CreateStmtContext *cxt, ColumnDef *column, bool if_not_exists)
{
bool is_serial;
bool saw_nullable;
@@ -557,8 +558,8 @@ transformColumnDefinition(CreateStmtContext *cxt, ColumnDef *column)
if (column->typeName)
transformColumnType(cxt, column);
- /* Special actions for SERIAL pseudo-types */
- if (is_serial)
+ /* Special actions for SERIAL pseudo-types, skip when IF NOT EXISTS is used */
+ if (is_serial && !skip_serial_add_column_if_not_exists(cxt->rel, column->colname, if_not_exists))
{
char *snamespace;
char *sname;
@@ -2714,7 +2715,7 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
{
ColumnDef *def = castNode(ColumnDef, cmd->def);
- transformColumnDefinition(&cxt, def);
+ transformColumnDefinition(&cxt, def, cmd->missing_ok);
/*
* If the column has a non-null default, we can't skip
@@ -3560,3 +3561,29 @@ transformPartitionBoundValue(ParseState *pstate, A_Const *con,
return (Const *) value;
}
+
+/*
+ * Skip SEQUENCE step when if_not_exists and column already exists
+ */
+static bool
+skip_serial_add_column_if_not_exists(Relation rel, const char *colname,
+ bool if_not_exists)
+{
+ HeapTuple attTuple;
+
+ if (!RelationIsValid(rel))
+ return false;
+
+ attTuple = SearchSysCache2(ATTNAME,
+ ObjectIdGetDatum(RelationGetRelid(rel)),
+ PointerGetDatum(colname));
+ if (!HeapTupleIsValid(attTuple))
+ return false;
+
+ ReleaseSysCache(attTuple);
+
+ if (if_not_exists)
+ return true;
+
+ return false;
+}
diff --git a/src/test/regress/expected/alter_table.out b/src/test/regress/expected/alter_table.out
index 0478a8a..b759ce0 100644
--- a/src/test/regress/expected/alter_table.out
+++ b/src/test/regress/expected/alter_table.out
@@ -3121,6 +3121,48 @@ NOTICE: column "c3" of relation "test_add_column" already exists, skipping
c3 | integer | | |
c4 | integer | | |
+ALTER TABLE test_add_column
+ ADD COLUMN c5 serial;
+\d test_add_column
+ Table "public.test_add_column"
+ Column | Type | Collation | Nullable | Default
+--------+---------+-----------+----------+---------------------------------------------
+ c1 | integer | | |
+ c2 | integer | | |
+ c3 | integer | | |
+ c4 | integer | | |
+ c5 | integer | | not null | nextval('test_add_column_c5_seq'::regclass)
+
+\ds test_add_column*
+ List of relations
+ Schema | Name | Type | Owner
+--------+------------------------+----------+----------
+ public | test_add_column_c5_seq | sequence | fabrizio
+(1 row)
+
+ALTER TABLE test_add_column
+ ADD COLUMN c5 serial; -- fail because c5 already exists
+ERROR: column "c5" of relation "test_add_column" already exists
+ALTER TABLE test_add_column
+ ADD COLUMN IF NOT EXISTS c5 serial; -- skipping because c5 already exists
+NOTICE: column "c5" of relation "test_add_column" already exists, skipping
+\d test_add_column
+ Table "public.test_add_column"
+ Column | Type | Collation | Nullable | Default
+--------+---------+-----------+----------+---------------------------------------------
+ c1 | integer | | |
+ c2 | integer | | |
+ c3 | integer | | |
+ c4 | integer | | |
+ c5 | integer | | not null | nextval('test_add_column_c5_seq'::regclass)
+
+\ds test_add_column*
+ List of relations
+ Schema | Name | Type | Owner
+--------+------------------------+----------+----------
+ public | test_add_column_c5_seq | sequence | fabrizio
+(1 row)
+
DROP TABLE test_add_column;
-- unsupported constraint types for partitioned tables
CREATE TABLE partitioned (
diff --git a/src/test/regress/sql/alter_table.sql b/src/test/regress/sql/alter_table.sql
index 37cca72..15ce995 100644
--- a/src/test/regress/sql/alter_table.sql
+++ b/src/test/regress/sql/alter_table.sql
@@ -1965,6 +1965,16 @@ ALTER TABLE test_add_column
ADD COLUMN IF NOT EXISTS c3 integer, -- skipping because c3 already exists
ADD COLUMN c4 integer;
\d test_add_column
+ALTER TABLE test_add_column
+ ADD COLUMN c5 serial;
+\d test_add_column
+\ds test_add_column*
+ALTER TABLE test_add_column
+ ADD COLUMN c5 serial; -- fail because c5 already exists
+ALTER TABLE test_add_column
+ ADD COLUMN IF NOT EXISTS c5 serial; -- skipping because c5 already exists
+\d test_add_column
+\ds test_add_column*
DROP TABLE test_add_column;
-- unsupported constraint types for partitioned tables
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: BUG #14827: "ALTER TABLE... IF NOT EXISTS...ADD.. BIGSERIAL" leaves extra sequences
@ 2017-09-26 19:57 Tom Lane <tgl@sss.pgh.pa.us>
parent: Fabrízio de Royes Mello <fabriziomello@gmail.com>
0 siblings, 1 reply; 8+ messages in thread
From: Tom Lane @ 2017-09-26 19:57 UTC (permalink / raw)
To: fabriziomello@gmail.com; +Cc: hvisage@gmail.com; pgsql-bugs
=?UTF-8?Q?Fabr=C3=ADzio_de_Royes_Mello?= <fabriziomello@gmail.com> writes:
> I didn't came with better solution, but for now what I did is inside
> transformaAlterTableStmt when calling transformColumnDefinition now we pass
> down "AlterTableStmt->missing_ok" to check and skip CREATE SEQUENCE
> statements when use SERIAL pseudo-types.
> It's not an elegant solution because during ATExecAddColumn we check it
> again by calling check_for_column_name_collision... Ideas are very welcome?
I do not think this is a solution at all. It doesn't address the
fundamental problem that we decide whether to make a serial sequence
before determining whether we're going to make a column default
depending on it. What it does do is introduce a different set of failure
conditions, basically race conditions around the discrepancy between
parse-time and execution-time state.
I don't feel like this is exactly a "must fix" problem, and it certainly
isn't one that we should fix by introducing different oddities of
behavior.
We could maybe fix things by arranging to create the sequence only after
we've made the column successfully, but that would be messy. I'm also
not clear on how to document it. The documentation right now is quite
clear, and accurate, about what SERIAL does:
https://www.postgresql.org/docs/devel/static/datatype-numeric.html#datatype-serial
This patch falsifies that, and so would any other conditional creation
of the sequence.
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: BUG #14827: "ALTER TABLE... IF NOT EXISTS...ADD.. BIGSERIAL" leaves extra sequences
@ 2017-09-26 20:42 Fabrízio de Royes Mello <fabriziomello@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 8+ messages in thread
From: Fabrízio de Royes Mello @ 2017-09-26 20:42 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Hendrik Visage <hvisage@gmail.com>; pgsql-bugs
On Tue, Sep 26, 2017 at 4:57 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> =?UTF-8?Q?Fabr=C3=ADzio_de_Royes_Mello?= <fabriziomello@gmail.com> writes:
> > I didn't came with better solution, but for now what I did is inside
> > transformaAlterTableStmt when calling transformColumnDefinition now we
pass
> > down "AlterTableStmt->missing_ok" to check and skip CREATE SEQUENCE
> > statements when use SERIAL pseudo-types.
>
> > It's not an elegant solution because during ATExecAddColumn we check it
> > again by calling check_for_column_name_collision... Ideas are very
welcome?
>
> I do not think this is a solution at all. It doesn't address the
> fundamental problem that we decide whether to make a serial sequence
> before determining whether we're going to make a column default
> depending on it.
I tried to address only the ALTER TABLE ... ADD COLUMN IF NOT EXISTS
statement, and do not touch CREATE TABLE statements...
For example when we add a new SERIAL column to a relation:
ALTER TABLE foo ADD COLUMN bar SERIAL;
What I understood is actually PostgreSQL will convert it to:
1. CREATE SEQUENCE foo_bar_seq;
2. ALTER TABLE foo ADD COLUMN bar INTEGER DEFAULT nextval('foo_bar_seq');
3. ALTER SEQUENCE foo_bar_seq OWNER BY foo.bar;
And what I tried to implement is skip step 1 and 3... and fo step 2 skip
the DEFAULT constraint
> What it does do is introduce a different set of failure
> conditions, basically race conditions around the discrepancy between
> parse-time and execution-time state.
>
I didn't understand what you mean here...
> I don't feel like this is exactly a "must fix" problem, and it certainly
> isn't one that we should fix by introducing different oddities of
> behavior.
>
When I see the code I felt the same... :-(
> We could maybe fix things by arranging to create the sequence only after
> we've made the column successfully, but that would be messy.
If we do that we should add more steps to execution queue...
> I'm also
> not clear on how to document it. The documentation right now is quite
> clear, and accurate, about what SERIAL does:
>
https://www.postgresql.org/docs/devel/static/datatype-numeric.html#datatype-serial
> This patch falsifies that, and so would any other conditional creation
> of the sequence.
>
This patch doesn't falsifies that, because will act just when IF NOT EXISTS
is used...
Regards,
--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL
>> Timbira: http://www.timbira.com.br
>> Blog: http://fabriziomello.github.io
>> Linkedin: http://br.linkedin.com/in/fabriziomello
>> Twitter: http://twitter.com/fabriziomello
>> Github: http://github.com/fabriziomello
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: BUG #14827: "ALTER TABLE... IF NOT EXISTS...ADD.. BIGSERIAL" leaves extra sequences
@ 2017-09-26 21:07 David G. Johnston <david.g.johnston@gmail.com>
parent: Fabrízio de Royes Mello <fabriziomello@gmail.com>
0 siblings, 1 reply; 8+ messages in thread
From: David G. Johnston @ 2017-09-26 21:07 UTC (permalink / raw)
To: fabriziomello@gmail.com <fabriziomello@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Hendrik Visage <hvisage@gmail.com>; pgsql-bugs
On Tue, Sep 26, 2017 at 1:42 PM, Fabrízio de Royes Mello <
fabriziomello@gmail.com> wrote:
> On Tue, Sep 26, 2017 at 4:57 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> I tried to address only the ALTER TABLE ... ADD COLUMN IF NOT EXISTS
> statement, and do not touch CREATE TABLE statements...
>
And since our docs don't explain the equivalence in terms of ALTER TABLE
we are not falsifying anything.
For example when we add a new SERIAL column to a relation:
>
> ALTER TABLE foo ADD COLUMN bar SERIAL;
>
> What I understood is actually PostgreSQL will convert it to:
>
> 1. CREATE SEQUENCE foo_bar_seq;
> 2. ALTER TABLE foo ADD COLUMN bar INTEGER DEFAULT nextval('foo_bar_seq');
> 3. ALTER SEQUENCE foo_bar_seq OWNER BY foo.bar;
>
> [...]
> > I don't feel like this is exactly a "must fix" problem, and it certainly
> > isn't one that we should fix by introducing different oddities of
> > behavior.
> >
>
> When I see the code I felt the same... :-(
>
Agreed, but it seems worthwhile to consider making it work as the OP
expected.
> > I'm also
> > not clear on how to document it. The documentation right now is quite
> > clear, and accurate, about what SERIAL does:
> > https://www.postgresql.org/docs/devel/static/datatype-
> numeric.html#datatype-serial
> > This patch falsifies that, and so would any other conditional creation
> > of the sequence.
> >
>
> This patch doesn't falsifies that, because will act just when IF NOT
> EXISTS is used...
>
>
And we already deviate for ALTER TABLE by not strictly adhering to the
specified format: tablename_colname_seq; instead we allow for appending "N"
to the end of the name if necessary to make the sequence name unique.
It seems like we'd want to invoke:
CREATE SEQUENCE IF NOT EXISTS tablename_colname_seq
If the corresponding add column is likewise IF NOT EXISTS.
If we detect the column was newly created maybe then also issue a RESET
SEQUENCE just in case we picked up a left-over? This feels both cleaner
and more dangerous than just inspecting everything first and deciding how
to proceed on both fronts.
David J.
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: BUG #14827: "ALTER TABLE... IF NOT EXISTS...ADD.. BIGSERIAL" leaves extra sequences
@ 2017-09-26 21:23 Fabrízio de Royes Mello <fabriziomello@gmail.com>
parent: David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 1 reply; 8+ messages in thread
From: Fabrízio de Royes Mello @ 2017-09-26 21:23 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Hendrik Visage <hvisage@gmail.com>; pgsql-bugs
On Tue, Sep 26, 2017 at 6:07 PM, David G. Johnston <
david.g.johnston@gmail.com> wrote:
>
> On Tue, Sep 26, 2017 at 1:42 PM, Fabrízio de Royes Mello <
fabriziomello@gmail.com> wrote:
>>
>> On Tue, Sep 26, 2017 at 4:57 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>>
>> I tried to address only the ALTER TABLE ... ADD COLUMN IF NOT EXISTS
statement, and do not touch CREATE TABLE statements...
>
> And since our docs don't explain the equivalence in terms of ALTER TABLE
we are not falsifying anything.
>
+1
>> For example when we add a new SERIAL column to a relation:
>>
>> ALTER TABLE foo ADD COLUMN bar SERIAL;
>>
>> What I understood is actually PostgreSQL will convert it to:
>>
>> 1. CREATE SEQUENCE foo_bar_seq;
>> 2. ALTER TABLE foo ADD COLUMN bar INTEGER DEFAULT nextval('foo_bar_seq');
>> 3. ALTER SEQUENCE foo_bar_seq OWNER BY foo.bar;
>>
> [...]
>
>>
>> > I don't feel like this is exactly a "must fix" problem, and it
certainly
>> > isn't one that we should fix by introducing different oddities of
>> > behavior.
>> >
>>
>> When I see the code I felt the same... :-(
>
> Agreed, but it seems worthwhile to consider making it work as the OP
expected.
>
Sure... but the way things happen in code is not easy how to figure out a
good elegant solution.
>> > I'm also
>> > not clear on how to document it. The documentation right now is quite
>> > clear, and accurate, about what SERIAL does:
>> >
https://www.postgresql.org/docs/devel/static/datatype-numeric.html#datatype-serial
>> > This patch falsifies that, and so would any other conditional creation
>> > of the sequence.
>> >
>>
>> This patch doesn't falsifies that, because will act just when IF NOT
EXISTS is used...
>>
>
> And we already deviate for ALTER TABLE by not strictly adhering to the
specified format: tablename_colname_seq; instead we allow for appending "N"
to the end of the name if necessary to make the sequence name unique.
>
> It seems like we'd want to invoke:
>
> CREATE SEQUENCE IF NOT EXISTS tablename_colname_seq
>
> If the corresponding add column is likewise IF NOT EXISTS.
>
> If we detect the column was newly created maybe then also issue a RESET
SEQUENCE just in case we picked up a left-over? This feels both cleaner
and more dangerous than just inspecting everything first and deciding how
to proceed on both fronts.
>
Seems a good plan... but I don't agree with RESET SEQUENCE... maybe just
CREATE SEQUENCE IF NOT EXISTS when provide IF NOT EXISTS on ALTER TABLE ADD
COLUMN is enough...
Regards,
--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL
>> Timbira: http://www.timbira.com.br
>> Blog: http://fabriziomello.github.io
>> Linkedin: http://br.linkedin.com/in/fabriziomello
>> Twitter: http://twitter.com/fabriziomello
>> Github: http://github.com/fabriziomello
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: BUG #14827: "ALTER TABLE... IF NOT EXISTS...ADD.. BIGSERIAL" leaves extra sequences
@ 2017-09-27 05:14 Michael Paquier <michael.paquier@gmail.com>
parent: Fabrízio de Royes Mello <fabriziomello@gmail.com>
0 siblings, 0 replies; 8+ messages in thread
From: Michael Paquier @ 2017-09-27 05:14 UTC (permalink / raw)
To: Fabrízio Mello <fabriziomello@gmail.com>; +Cc: David G. Johnston <david.g.johnston@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; Hendrik Visage <hvisage@gmail.com>; pgsql-bugs
On Wed, Sep 27, 2017 at 6:23 AM, Fabrízio de Royes Mello
<fabriziomello@gmail.com> wrote:
> Seems a good plan... but I don't agree with RESET SEQUENCE... maybe just
> CREATE SEQUENCE IF NOT EXISTS when provide IF NOT EXISTS on ALTER TABLE ADD
> COLUMN is enough...
Anything like that is not completely hole-proof either. Let's not
forget that the sequence used with a default expression is not tracked
with its name, so if the sequence created after the serial definition
is renamed, and an INE is used on the given column, then you would
still create a sequence. Even worse, we need to be careful about not
linking the newly-created sequence instead of the one currently used.
In my opinion, the current behavior is more predictible. I think that
we should just document that IFE can leave behind sequences, and live
with that.
--
Michael
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 8+ messages in thread
end of thread, other threads:[~2017-09-27 05:14 UTC | newest]
Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2017-09-25 08:45 BUG #14827: "ALTER TABLE... IF NOT EXISTS...ADD.. BIGSERIAL" leaves extra sequences hvisage@gmail.com
2017-09-26 13:04 ` Fabrízio de Royes Mello <fabriziomello@gmail.com>
2017-09-26 19:30 ` Fabrízio de Royes Mello <fabriziomello@gmail.com>
2017-09-26 19:57 ` Tom Lane <tgl@sss.pgh.pa.us>
2017-09-26 20:42 ` Fabrízio de Royes Mello <fabriziomello@gmail.com>
2017-09-26 21:07 ` David G. Johnston <david.g.johnston@gmail.com>
2017-09-26 21:23 ` Fabrízio de Royes Mello <fabriziomello@gmail.com>
2017-09-27 05:14 ` Michael Paquier <michael.paquier@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