pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Roger Tannous <roger77_lb@yahoo.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
To: D'Arcy J.M. Cain <darcy@druid.net>
Cc: Roger Tannous <roger77_lb@yahoo.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: A Table's Primary Key Listing
Date: Thu, 18 Aug 2005 13:39:09 -0700 (PDT)
Message-ID: <20050818203909.3979.qmail@web51906.mail.yahoo.com> (raw)
In-Reply-To: <10279.1124395722@sss.pgh.pa.us>

Hi, 

If you put pg_index.indkey in the select statement, you'd notice that it's
sometimes 1 ( it's when we have one PK field) and sometimes 1 2 ( for two
PK fields), etc.

So I tried to use a replace command like the following:

(just to add parentheses, replace the space by a comma to use the
resulting string in an IN statement)

select '(' || replace('1 2', " ", ",") || ')';

which yields: (1,2)

But the following query fails to execute!!
select replace(indkey, " ", ",") from pg_index; 

[
sub question: Did I miss quotes around elements? I mean should I enclose
every element originating from the indkey array with single quotes ? if
yes, so easy, no need to matter about it: so I should have tried the
following (which I didn't have time to do yet):

select '(\'' || replace(indkey, " ", "','") || '\')' from pg_index;

Another issue here too: Could double quotes here be the source of a
problem ? So I should have tested also this query:

select '(\'' || replace(indkey, ' ', '\',\'') || '\')' from pg_index;

I expect this query to work :) Let's hope so!!
]



So we can use the following WHERE statement: 
WHERE pg_attribute.attnum IN '(' || replace('1 2', " ", ",") || ')'

which should translate into: WHERE pg_attribute.attnum IN (1,2)


Finally, this WHERE statement:

WHERE pg_attribute.attnum IN
            '(\'' || replace(pg_index.indkey, " ", "','") || '\')'


[
Again, I should test:

WHERE pg_attribute.attnum IN
            '(\'' || replace(pg_index.indkey, ' ', '\',\'') || '\')'

]


I wish I had database access in the internet cafe I'm sending this message
from :) instead of just loading you with this bunch of questions.


Best Regards,
Roger Tannous.


--- Tom Lane <tgl@sss.pgh.pa.us> wrote:

> "D'Arcy J.M. Cain" <darcy@druid.net> writes:
> > That's a good question.  The following query does this in a very
> > unsatisfactory way.  Anyone know what the general solution would be?
> 
> > ...
> >     (
> >       pg_index.indkey[0]=pg_attribute.attnum OR
> >       pg_index.indkey[1]=pg_attribute.attnum OR
> >       pg_index.indkey[2]=pg_attribute.attnum OR
> >       pg_index.indkey[3]=pg_attribute.attnum OR
> >       pg_index.indkey[4]=pg_attribute.attnum OR
> >       pg_index.indkey[5]=pg_attribute.attnum OR
> >       pg_index.indkey[6]=pg_attribute.attnum OR
> >       pg_index.indkey[7]=pg_attribute.attnum OR
> >       pg_index.indkey[8]=pg_attribute.attnum OR
> >       pg_index.indkey[9]=pg_attribute.attnum
> >     )
> 
> In CVS tip you could replace this with "attnum = ANY (indkey)".
> Unfortunately, most array support doesn't work on int2vector in
> pre-8.1 releases, so I think you're kinda stuck with the above
> for now.
> 
> 			regards, tom lane
> 



		
____________________________________________________
Start your day with Yahoo! - make it your home page 
http://www.yahoo.com/r/hs 
 



view thread (13+ messages)  latest in thread

Message-ID: <20050818203909.3979.qmail@web51906.mail.yahoo.com>
Permalink:  ../20050818203909.3979.qmail@web51906.mail.yahoo.com/
Also on:    postgresql.org/message-id/20050818203909.3979.qmail@web51906.mail.yahoo.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: roger77_lb@yahoo.com, tgl@sss.pgh.pa.us, darcy@druid.net
  Subject: Re: A Table's Primary Key Listing
  In-Reply-To: <20050818203909.3979.qmail@web51906.mail.yahoo.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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