Re: invisible dependencies on a table?

From: Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com>
To: Tim Uckun <timuckun(at)gmail(dot)com>, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: invisible dependencies on a table?
Date: 2013-12-13 15:30:27
Message-ID: 52AB2813.8070605@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 12/12/2013 08:24 PM, Tim Uckun wrote:
> I have a table foo. It has a serial column called "id". I execute the
> following statement
>
> ALTER TABLE table_name RENAME TO archived_table_name;
> CREATE TABLE table_name (LIKE archived_table_name INCLUDING
> DEFAULTS INCLUDING CONSTRAINTS INCLUDING INDEXES);
>
> ..... Archieve the table here...
>
> DROP TABLE arhived_table_name
>
> This doesn't work because the archived table name has a dependency on
> the sequence created by the serial field. So I try to remove that
> dependency by doing this.
>
> alter table "archived_table_name" alter column id drop default;
> ALTER TABLE"archived_table_name" DROP CONSTRAINT systemevents_pkey;
>
> So by now there should not be a dependency on the sequence but I still
> can't drop the table and and pgadmin tells me it's still depending on
> the sequence.
>
> When I look at the table definition it doesn't seem to have any
> reference to the sequence at all.
>
> How can I drop this table and leave the sequence alone? Obviously the
> newly created table needs it.

In addition to what David said here is another option, create the
original table with a non-dependent sequence:

test=> CREATE SEQUENCE shared_seq;
CREATE SEQUENCE

test=> create table seq_test(id integer default nextval('shared_seq'),
fld varchar);
CREATE TABLE

test=> ALTER TABLE seq_test RENAME to archived_seq_test;
ALTER TABLE

test=> CREATE TABLE seq_test (LIKE archived_seq_test INCLUDING DEFAULTS
INCLUDING CONSTRAINTS INCLUDING INDEXES);
CREATE TABLE

test=> DROP TABLE archived_seq_test;
DROP TABLE

When you use the serial type it creates a dependency on the serial and
as David pointed out you can do the same thing with ALTER SEQUENCE.
However as shown above there is no requirement that a sequence be
dependent. It is at its core a 'table' that is a number generator.

>
> Thanks.

--
Adrian Klaver
adrian(dot)klaver(at)gmail(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message David Johnston 2013-12-13 15:42:30 Re: Grouping, Aggregate, Min, Max
Previous Message Spiros Ioannou 2013-12-13 15:19:00 Re: While only running pg_dump, postgresql performs writes inside base/ directory. How can we stop this?