Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hcBmK-0004z9-Ih for pgsql-bugs@arkaria.postgresql.org; Sat, 15 Jun 2019 16:43:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hcBmI-0003Qj-J8 for pgsql-bugs@arkaria.postgresql.org; Sat, 15 Jun 2019 16:43:42 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hcBmI-0003Qa-86 for pgsql-bugs@lists.postgresql.org; Sat, 15 Jun 2019 16:43:42 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hcBmB-0004QI-WD for pgsql-bugs@lists.postgresql.org; Sat, 15 Jun 2019 16:43:41 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x5FGhXV4022528; Sat, 15 Jun 2019 12:43:33 -0400 From: Tom Lane To: Alvaro Herrera cc: wondertx@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #15853: DROP TABLE CASCADE drops sequence that other tables depend on In-reply-to: <20190615161358.GA31948@alvherre.pgsql> References: <20190615161358.GA31948@alvherre.pgsql> Comments: In-reply-to Alvaro Herrera message dated "Sat, 15 Jun 2019 12:13:58 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <22526.1560617013.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sat, 15 Jun 2019 12:43:33 -0400 Message-ID: <22527.1560617013@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Alvaro Herrera 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 INCLUDI= NG >> 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=3D# create table src (f1 int generated always as identity); CREATE TABLE regression=3D# create table dest (like src including identity); CREATE TABLE regression=3D# \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=3D# insert into dest default values; INSERT 0 1 regression=3D# insert into dest default values; INSERT 0 1 regression=3D# table dest; f1 = ---- 1 2 (2 rows) regression=3D# drop table src; DROP TABLE regression=3D# insert into dest default values; INSERT 0 1 regression=3D# insert into dest default values; INSERT 0 1 regression=3D# table dest; f1 = ---- 1 2 3 4 (4 rows) regards, tom lane