FAQ
Hi.

I just ran into a problem when (/after) renaming a sequence.

It seems that psql does something different than DBD::Pg when finding
the default value of a column.

DBD::Pg seems to get it directly from pg_catalog.pg_attrdef(adsrc), but
apparantly 'ALTER SEQUENCE some_seq RENAME TO other_seq' doesn't update
that, and psql seems to look somewhere else.

Here is an example that hopefully illustrates the problem:

First I create a simple table and rename the sequence:

core@[local] test=# CREATE TABLE test (id serial, name text, value integer);
NOTICE: CREATE TABLE will create implicit sequence "test_id_seq" for serial column "test.id"
CREATE TABLE
core@[local] test=# \d test
Table "public.test"
Column | Type | Modifiers
--------+---------+---------------------------------------------------
id | integer | not null default nextval('test_id_seq'::regclass)
name | text |
value | integer |

core@[local] test=# ALTER SEQUENCE test_id_seq RENAME TO other_id_seq;
ALTER SEQUENCE
core@[local] test=# \d test
Table "public.test"
Column | Type | Modifiers
--------+---------+----------------------------------------------------
id | integer | not null default nextval('other_id_seq'::regclass)
name | text |
value | integer |

core@[local] test=#

This looks good, the default of the id column is automatically updated
with the new sequence name as expected.

For some reason pg_catalog.pg_attrdef(adsrc) isn't updated, though:

core@[local] test=# SELECT adrelid, adsrc FROM pg_catalog.pg_attrdef;
adrelid | adsrc
---------+----------------------------------
1466741 | nextval('test_id_seq'::regclass)
(1 row)

core@[local] test=#

When DBIx::Class asks DBD::Pg for the column_info, the COLUMN_DEF field
is filled in from the pg_attrdef:

$ perl -MDBI -e '$dbh=DBI->connect("dbi:Pg:dbname=test", "core", "core"); $info=$dbh->column_info(undef, undef, "test", "id")->fetchrow_hashref; print $info->{COLUMN_DEF} . "\n"'
nextval('test_id_seq'::regclass)
$

... and SELECT currval on 'test_id_seq' fails, as that isn't the name of
the sequence (any more).

My question is basically where to try and get it fixed:

* Should PostgreSQL update pg_attrdef as part of executing "ALTER
SEQUENCE .. RENAME TO .."?

* Should DBD::Pg get COLUMN_DEF from the same place that psql does? - or:

* Should DBIx::Class get the default value of a column from somewhere
else than COLUMN_DEF, if so, where?

Thanks :-)


Version information:

PostgreSQL 8.3.6
DBD::Pg 2.8.2
DBI 1.605
DBIx::Class 0.08010

Ubuntu 8.10 amd64


Best regards,

Adam

--
Adam Sjøgren
[email protected]

Search Discussions

  • Adam Sjøgren at Mar 24, 2009 at 2:54 pm
    On Fri, 20 Mar 2009 13:24:06 +0100, I wrote:

    [... DBD::Pg uses pg_catalog.pg_attrdef to find the default value of a
    column ...]
    My question is basically where to try and get it fixed:
    * Should PostgreSQL update pg_attrdef as part of executing "ALTER
    SEQUENCE .. RENAME TO .."?
    * Should DBD::Pg get COLUMN_DEF from the same place that psql does? - or:
    * Should DBIx::Class get the default value of a column from somewhere
    else than COLUMN_DEF, if so, where?
    Looking at the PostgreSQL documentation of pg_attrdef, it says:

    "The adsrc field is historical, and is best not used, because it does
    not track outside changes that might affect the representation of the
    default value. Reverse-compiling the adbin field (with pg_get_expr for
    example) is a better way to display the default value."

    - http://www.postgresql.org/docs/8.3/static/catalog-pg-attrdef.html

    So I guess that column_info in DBD::Pg should be updated to take that
    advice into account?

    Peeking at what psql does, I've put together the patch below (against
    trunk).

    I'm not sure how it will impact DBD::Pg running against older versions
    of PostgreSQL, though.

    When was pg_get_expr() created? Between 7.4 and 8.0 it seems¹, so maybe
    a conditional is necessary?


    Best regards,

    Adam

    ¹ http://www.postgresql.org/docs/7.4/static/catalog-pg-attrdef.html
    http://www.postgresql.org/docs/8.0/static/catalog-pg-attrdef.html

    --
    Adam Sjøgren
    [email protected]
  • Adam Sjøgren at Mar 25, 2009 at 9:45 am

    On Tue, 24 Mar 2009 15:54:21 +0100, I wrote:

    When was pg_get_expr() created? Between 7.4 and 8.0 it seems¹, so maybe
    a conditional is necessary?
    Attached is a patch that uses pg_get_expr() for 8.0+ and adsrc
    otherwise.


    Best regards,

    Adam

    --
    Adam Sjøgren
    [email protected]
  • Greg Sabino Mullane at Mar 26, 2009 at 3:37 pm
    Attached is a patch that uses pg_get_expr() for 8.0+ and adsrc otherwise.
    Applied, thanks!

    - --
    Greg Sabino Mullane [email protected]
    End Point Corporation
    PGP Key: 0x14964AC8 200903261136
    http://biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupdbd-pg @
categoriesperl
postedMar 20, '09 at 2:19p
activeMar 26, '09 at 3:37p
posts4
users2
websiteperl.org

People

Translate

site design / logo © 2023 Grokbase