pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #15853: DROP TABLE CASCADE drops sequence that other tables depend on
5+ messages / 5 participants
[nested] [flat]

* BUG #15853: DROP TABLE CASCADE drops sequence that other tables depend on
@ 2019-06-15 08:47  PG Bug reporting form <noreply@postgresql.org>
  0 siblings, 2 replies; 5+ messages in thread

From: PG Bug reporting form @ 2019-06-15 08:47 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: wondertx@gmail.com

The following bug has been logged on the website:

Bug reference:      15853
Logged by:          wondertx
Email address:      wondertx@gmail.com
PostgreSQL version: 11.3
Operating system:   Arch Linux
Description:        

The following SQL executed will drop the sequence `t_id_seq`:
CREATE TABLE t(id SERIAL, value INT NOT NULL);
CREATE TABLE t_bak  LIKE t INCLUDING DEFAULTS INCLUDING INDEXES INCLUDING
COMMENTS INCLUDING CONSTRAINTS);
DROP TABLE t CASCADE;
This will drop default value of column `value` in t_bak.
However, the following SQL will not drop the sequence:
CREATE TABLE t(id SERIAL, value INT NOT NULL);
DROP SEQUENCE t_id_seq;
CREATE SEQUENCE t_id_seq;
ALTER TABLE T ALTER COLUMN ID SET DEFAULT NEXTVAL('t_id_seq'::regclass);
CREATE TABLE t_bak  LIKE t INCLUDING DEFAULTS INCLUDING INDEXES INCLUDING
COMMENTS INCLUDING CONSTRAINTS);
DROP TABLE t CASCADE;



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

* Re: BUG #15853: DROP TABLE CASCADE drops sequence that other tables depend on
@ 2019-06-15 11:35  David G. Johnston <david.g.johnston@gmail.com>
  parent: PG Bug reporting form <noreply@postgresql.org>
  1 sibling, 0 replies; 5+ messages in thread

From: David G. Johnston @ 2019-06-15 11:35 UTC (permalink / raw)
  To: wondertx@gmail.com <wondertx@gmail.com>; pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>

On Saturday, June 15, 2019, PG Bug reporting form <noreply@postgresql.org>
wrote:

> The following bug has been logged on the website:
>
> Bug reference:      15853
> Logged by:          wondertx
> Email address:      wondertx@gmail.com
> PostgreSQL version: 11.3
> Operating system:   Arch Linux
> Description:
>
> The following SQL executed will drop the sequence `t_id_seq`:
> CREATE TABLE t(id SERIAL, value INT NOT NULL);
> CREATE TABLE t_bak  LIKE t INCLUDING DEFAULTS INCLUDING INDEXES INCLUDING
> COMMENTS INCLUDING CONSTRAINTS);
> DROP TABLE t CASCADE;
> This will drop default value of column `value` in t_bak.
> However, the following SQL will not drop the sequence:
> CREATE TABLE t(id SERIAL, value INT NOT NULL);
> DROP SEQUENCE t_id_seq;
> CREATE SEQUENCE t_id_seq;
> ALTER TABLE T ALTER COLUMN ID SET DEFAULT NEXTVAL('t_id_seq'::regclass);
> CREATE TABLE t_bak  LIKE t INCLUDING DEFAULTS INCLUDING INDEXES INCLUDING
> COMMENTS INCLUDING CONSTRAINTS);
> DROP TABLE t CASCADE;
>
>

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

* Re: BUG #15853: DROP TABLE CASCADE drops sequence that other tables depend on
@ 2019-06-15 16:13  Alvaro Herrera <alvherre@2ndquadrant.com>
  parent: PG Bug reporting form <noreply@postgresql.org>
  1 sibling, 1 reply; 5+ messages in thread

From: Alvaro Herrera @ 2019-06-15 16:13 UTC (permalink / raw)
  To: wondertx@gmail.com; pgsql-bugs@lists.postgresql.org

On 2019-Jun-15, PG Bug reporting form wrote:

> The following SQL executed will drop the sequence `t_id_seq`:
> CREATE TABLE t(id SERIAL, value INT NOT NULL);
> CREATE TABLE t_bak  LIKE t INCLUDING DEFAULTS INCLUDING INDEXES INCLUDING
> COMMENTS INCLUDING CONSTRAINTS);
> DROP TABLE t CASCADE;
> This will drop default value of column `value` in t_bak.

Yes.  The reason the sequence is dropped is that it is owned by the t.id
column, so when the column goes away, so does the sequence.  And this
cascades to that default value.

> However, the following SQL will not drop the sequence:
> CREATE TABLE t(id SERIAL, value INT NOT NULL);
> DROP SEQUENCE t_id_seq;
> CREATE SEQUENCE t_id_seq;
> ALTER TABLE T ALTER COLUMN ID SET DEFAULT NEXTVAL('t_id_seq'::regclass);
> CREATE TABLE t_bak  LIKE t INCLUDING DEFAULTS INCLUDING INDEXES INCLUDING
> COMMENTS INCLUDING CONSTRAINTS);
> DROP TABLE t CASCADE;

In this case, by dropping and recreating the sequence, you made it an
independent sequence.  Setting as the default for the column doesn't
make it owned by it.  If you throw
ALTER SEQUENCE t_id_set OWNED BY t.id;
in the mix, then it should work just like in the first case.
Conversely, you can make the first case behave like the second by doing
this
ALTER SEQUENCE t_id_set OWNED BY NONE;

In short, not a bug.

-- 
Álvaro Herrera                https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services





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

* Re: BUG #15853: DROP TABLE CASCADE drops sequence that other tables depend on
@ 2019-06-15 16:43  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Alvaro Herrera <alvherre@2ndquadrant.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Tom Lane @ 2019-06-15 16:43 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: wondertx@gmail.com; pgsql-bugs@lists.postgresql.org

Alvaro Herrera <alvherre@2ndquadrant.com> writes:
> On 2019-Jun-15, PG Bug reporting form wrote:
>> The following SQL executed will drop the sequence `t_id_seq`:
>> CREATE TABLE t(id SERIAL, value INT NOT NULL);
>> CREATE TABLE t_bak  LIKE t INCLUDING DEFAULTS INCLUDING INDEXES INCLUDING
>> COMMENTS INCLUDING CONSTRAINTS);
>> DROP TABLE t CASCADE;
>> This will drop default value of column `value` in t_bak.

> Yes.  The reason the sequence is dropped is that it is owned by the t.id
> column, so when the column goes away, so does the sequence.  And this
> cascades to that default value.

Yeah, not a bug.  The OP might find that generated-as-identity columns
work more to his liking than SERIAL does: copying them with LIKE creates
an independent new sequence.

regression=# create table src (f1 int generated always as identity);
CREATE TABLE
regression=# create table dest (like src including identity);
CREATE TABLE
regression=# \d+ dest
                                              Table "public.dest"
 Column |  Type   | Collation | Nullable |           Default            | Storag
e | Stats target | Description 
--------+---------+-----------+----------+------------------------------+-------
--+--------------+-------------
 f1     | integer |           | not null | generated always as identity | plain 
  |              | 
Access method: heap

regression=# insert into dest default values;
INSERT 0 1
regression=# insert into dest default values;
INSERT 0 1
regression=# table dest;
 f1 
----
  1
  2
(2 rows)

regression=# drop table src;
DROP TABLE
regression=# insert into dest default values;
INSERT 0 1
regression=# insert into dest default values;
INSERT 0 1
regression=# table dest;
 f1 
----
  1
  2
  3
  4
(4 rows)


			regards, tom lane





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

* Re: BUG #15853: DROP TABLE CASCADE drops sequence that other tables depend on
@ 2019-06-17 02:08  Tao Xu <wondertx@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 5+ messages in thread

From: Tao Xu @ 2019-06-17 02:08 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Alvaro Herrera <alvherre@2ndquadrant.com>; pgsql-bugs@lists.postgresql.org

Thank you all. The configurability of Postgre is splendid

On Sun, Jun 16, 2019 at 12:43 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:

> Alvaro Herrera <alvherre@2ndquadrant.com> writes:
> > On 2019-Jun-15, PG Bug reporting form wrote:
> >> The following SQL executed will drop the sequence `t_id_seq`:
> >> CREATE TABLE t(id SERIAL, value INT NOT NULL);
> >> CREATE TABLE t_bak  LIKE t INCLUDING DEFAULTS INCLUDING INDEXES
> INCLUDING
> >> COMMENTS INCLUDING CONSTRAINTS);
> >> DROP TABLE t CASCADE;
> >> This will drop default value of column `value` in t_bak.
>
> > Yes.  The reason the sequence is dropped is that it is owned by the t.id
> > column, so when the column goes away, so does the sequence.  And this
> > cascades to that default value.
>
> Yeah, not a bug.  The OP might find that generated-as-identity columns
> work more to his liking than SERIAL does: copying them with LIKE creates
> an independent new sequence.
>
> regression=# create table src (f1 int generated always as identity);
> CREATE TABLE
> regression=# create table dest (like src including identity);
> CREATE TABLE
> regression=# \d+ dest
>                                               Table "public.dest"
>  Column |  Type   | Collation | Nullable |           Default            |
> Storag
> e | Stats target | Description
>
> --------+---------+-----------+----------+------------------------------+-------
> --+--------------+-------------
>  f1     | integer |           | not null | generated always as identity |
> plain
>   |              |
> Access method: heap
>
> regression=# insert into dest default values;
> INSERT 0 1
> regression=# insert into dest default values;
> INSERT 0 1
> regression=# table dest;
>  f1
> ----
>   1
>   2
> (2 rows)
>
> regression=# drop table src;
> DROP TABLE
> regression=# insert into dest default values;
> INSERT 0 1
> regression=# insert into dest default values;
> INSERT 0 1
> regression=# table dest;
>  f1
> ----
>   1
>   2
>   3
>   4
> (4 rows)
>
>
>                         regards, tom lane
>

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


end of thread, other threads:[~2019-06-17 02:08 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-06-15 08:47 BUG #15853: DROP TABLE CASCADE drops sequence that other tables depend on PG Bug reporting form <noreply@postgresql.org>
2019-06-15 11:35 ` David G. Johnston <david.g.johnston@gmail.com>
2019-06-15 16:13 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2019-06-15 16:43   ` Tom Lane <tgl@sss.pgh.pa.us>
2019-06-17 02:08     ` Tao Xu <wondertx@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