Re: pg_attribute.attnum - wrong column ordinal?

Lists: pgsql-generalpgsql-hackers
From: Konstantin Izmailov <pgfizm(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: pg_attribute.attnum - wrong column ordinal?
Date: 2009-11-25 01:03:03
Message-ID: 72746b5e0911241703q1c5b9323gcd70bff83b2e7be0@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general pgsql-hackers

Today I was contacted by a Microsoft (!) developer Kamil who was working on
issues in Linked Servers to PostgreSQL. He brought the following scenario:
if a column is dropped then ordinal positions of remaining columns are
reported incorrectly.

Here is test scenario:
1) create a table in PGAdmin:

create table ms_tst
(
col1 varchar(50),
col2 varchar(50)
);

2) Add col3 and drop col2:

alter table ms_tst add column col3 varchar(50);
alter table ms_tst drop column col2;

3) Use the following query to retrieve columns information (this query is
generated by the OLEDB provider):

select * from
(select T.schemaname as "TABLE_SCHEMA", T.tablename as "TABLE_NAME",
A.attname as "COLUMN_NAME", A.attnum as "ORDINAL_POSITION",
A.atthasdef as "COLUMN_HASDEFAULT", A.atttypid as "DATA_TYPE",
TY.typname as "TYPNAME", A.attnotnull as "NOT_NULL",
A.attlen as "FIELD_LENGTH", A.atttypmod as "FIELD_MOD", D.adsrc as
"COLUMN_DEFAULT"
from pg_attribute A
inner join pg_class C on (A.attrelid=C.oid)
inner join pg_tables T on (C.relname=T.tablename)
inner join pg_namespace NS on (NS.oid=C.relnamespace and
NS.nspname=T.schemaname)
inner join pg_type TY on (TY.oid=A.atttypid)
left outer join pg_attrdef D on (D.adrelid=C.oid and D.adnum=A.attnum)
where A.attnum>0 and A.attisdropped='f'
union select T.schemaname as "TABLE_SCHEMA", T.viewname as "TABLE_NAME",
A.attname as "COLUMN_NAME", A.attnum as "ORDINAL_POSITION",
A.atthasdef as "COLUMN_HASDEFAULT", A.atttypid as "DATA_TYPE",
TY.typname as "TYPNAME", A.attnotnull as "NOT_NULL",
A.attlen as "FIELD_LENGTH", A.atttypmod as "FIELD_MOD", D.adsrc as
"COLUMN_DEFAULT"
from pg_attribute A
inner join pg_class C on (A.attrelid=C.oid)
inner join pg_views T on (C.relname=T.viewname)
inner join pg_namespace NS on (NS.oid=C.relnamespace and
NS.nspname=T.schemaname)
inner join pg_type TY on (TY.oid=A.atttypid)
left outer join pg_attrdef D on (D.adrelid=C.oid and D.adnum=A.attnum)
where A.attnum>0 and A.attisdropped='f') s
where "TABLE_SCHEMA"='public' and "TABLE_NAME"='ms_tst'
order by "TABLE_SCHEMA", "TABLE_NAME", "ORDINAL_POSITION"

4) Note that ORDINAL_POSITIONs of col1 and col3 are 1 and 3 (expected 1 and
2).

My question: can pg_attribute.attnum be used to determine the sequential
ordinal positions of columns in a table? What is a right way to get the
ordinal numbers?

Please help!

Konstantin


From: Greg Stark <gsstark(at)mit(dot)edu>
To: Konstantin Izmailov <pgfizm(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: pg_attribute.attnum - wrong column ordinal?
Date: 2009-11-25 01:30:28
Message-ID: 407d949e0911241730j52ca3099oe7bce71adf1318e1@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general pgsql-hackers

On Wed, Nov 25, 2009 at 1:03 AM, Konstantin Izmailov <pgfizm(at)gmail(dot)com> wrote:

> My question: can pg_attribute.attnum be used to determine the sequential
> ordinal positions of columns in a table? What is a right way to get the
> ordinal numbers?

You could use something like:

row_number() over (partition by T.schemaname,T.viewname order by
attnum) as "ORDINAL_POSITION"

If you just stick this in there in place of attnum it'll cause an
extra sort. It should be possible with enough clever rearranging of
the query to do the whole query with a single sort since that's the
same sort order that the results are ordered in.

Incidentally you probably want UNION ALL rather than UNION in the
original query.

--
greg


From: Konstantin Izmailov <pgfizm(at)gmail(dot)com>
To: Greg Stark <gsstark(at)mit(dot)edu>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: pg_attribute.attnum - wrong column ordinal?
Date: 2009-11-25 02:00:20
Message-ID: 72746b5e0911241800k49a769a2yf9d9f59a43861667@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general pgsql-hackers

Greg,
this is brilliant - thank you very much!

Is "partition by" compatible to PostgreSQL 8.0/8.2? I could not find
compatibility information. It works fine with PG 8.3/8.4 and Greenplum 3.3
thou.

Konstantin

On Tue, Nov 24, 2009 at 6:30 PM, Greg Stark <gsstark(at)mit(dot)edu> wrote:

> On Wed, Nov 25, 2009 at 1:03 AM, Konstantin Izmailov <pgfizm(at)gmail(dot)com>
> wrote:
>
> > My question: can pg_attribute.attnum be used to determine the sequential
> > ordinal positions of columns in a table? What is a right way to get the
> > ordinal numbers?
>
> You could use something like:
>
> row_number() over (partition by T.schemaname,T.viewname order by
> attnum) as "ORDINAL_POSITION"
>
> If you just stick this in there in place of attnum it'll cause an
> extra sort. It should be possible with enough clever rearranging of
> the query to do the whole query with a single sort since that's the
> same sort order that the results are ordered in.
>
> Incidentally you probably want UNION ALL rather than UNION in the
> original query.
>
> --
> greg
>


From: Greg Stark <gsstark(at)mit(dot)edu>
To: Konstantin Izmailov <pgfizm(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: pg_attribute.attnum - wrong column ordinal?
Date: 2009-11-25 03:04:02
Message-ID: 407d949e0911241904t5c11b373n9af9acdd370b460@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general pgsql-hackers

On Wed, Nov 25, 2009 at 2:00 AM, Konstantin Izmailov <pgfizm(at)gmail(dot)com> wrote:
> Greg,
> this is brilliant - thank you very much!
>
> Is "partition by" compatible to PostgreSQL 8.0/8.2? I could not find
> compatibility information. It works fine with PG 8.3/8.4 and Greenplum 3.3
> thou.

It's 8.4 only.

You could also just generate them on the client since the ordering the
query requests is the right order for it.

--
greg


From: Konstantin Izmailov <pgfizm(at)gmail(dot)com>
To: Greg Stark <gsstark(at)mit(dot)edu>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: pg_attribute.attnum - wrong column ordinal?
Date: 2009-11-25 04:29:16
Message-ID: 72746b5e0911242029q1af87603wa0e1fb21e63ceb10@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general pgsql-hackers

Oops, the server that I thought was 8.3 was recently upgraded to 8.4.

I'm going to generate the ordinals on the client then. The nature of the
query is that it can request columns information for all or several tables
as well. The ordinals generation algorithm can reset counter to 1 each time
the table name changes.

Thank you tons!

On Tue, Nov 24, 2009 at 8:04 PM, Greg Stark <gsstark(at)mit(dot)edu> wrote:

> On Wed, Nov 25, 2009 at 2:00 AM, Konstantin Izmailov <pgfizm(at)gmail(dot)com>
> wrote:
> > Greg,
> > this is brilliant - thank you very much!
> >
> > Is "partition by" compatible to PostgreSQL 8.0/8.2? I could not find
> > compatibility information. It works fine with PG 8.3/8.4 and Greenplum
> 3.3
> > thou.
>
> It's 8.4 only.
>
> You could also just generate them on the client since the ordering the
> query requests is the right order for it.
>
> --
> greg
>


From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Greg Stark <gsstark(at)mit(dot)edu>
Cc: Konstantin Izmailov <pgfizm(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: pg_attribute.attnum - wrong column ordinal?
Date: 2009-12-03 13:42:39
Message-ID: 20091203134239.GB5059@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general pgsql-hackers

Greg Stark escribió:
> On Wed, Nov 25, 2009 at 1:03 AM, Konstantin Izmailov <pgfizm(at)gmail(dot)com> wrote:
>
> > My question: can pg_attribute.attnum be used to determine the sequential
> > ordinal positions of columns in a table? What is a right way to get the
> > ordinal numbers?
>
> You could use something like:
>
> row_number() over (partition by T.schemaname,T.viewname order by
> attnum) as "ORDINAL_POSITION"

Should we recast the attributes and columns views in information_schema?
I notice they still use attnum.

--
Alvaro Herrera http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: Greg Stark <gsstark(at)mit(dot)edu>, Konstantin Izmailov <pgfizm(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: pg_attribute.attnum - wrong column ordinal?
Date: 2009-12-03 15:09:17
Message-ID: 8576.1259852957@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general pgsql-hackers

Alvaro Herrera <alvherre(at)commandprompt(dot)com> writes:
> Should we recast the attributes and columns views in information_schema?
> I notice they still use attnum.

I'd vote against it, at least until we have something better than a
row_number solution. That would create another huge performance penalty
on views that are already ungodly slow.

When and if we get around to separating physical from logical column
position, the issue might go away "for free".

regards, tom lane


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Alvaro Herrera <alvherre(at)commandprompt(dot)com>, Greg Stark <gsstark(at)mit(dot)edu>, Konstantin Izmailov <pgfizm(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: pg_attribute.attnum - wrong column ordinal?
Date: 2009-12-05 14:46:34
Message-ID: 1260024394.20505.0.camel@vanquo.pezone.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general pgsql-hackers

On tor, 2009-12-03 at 10:09 -0500, Tom Lane wrote:
> Alvaro Herrera <alvherre(at)commandprompt(dot)com> writes:
> > Should we recast the attributes and columns views in information_schema?
> > I notice they still use attnum.
>
> I'd vote against it, at least until we have something better than a
> row_number solution. That would create another huge performance penalty
> on views that are already ungodly slow.

Should be easy to test the performance impact of this, since the limit
for columns per table is 1600.