Querying database for table pk - better way?

From: Josh Trutwin <josh(at)trutwins(dot)homeip(dot)net>
To: "Postgres General" <pgsql-general(at)postgresql(dot)org>
Subject: Querying database for table pk - better way?
Date: 2007-09-05 21:17:22
Message-ID: 20070905161722.7e83494d@sinkhole.intrcomm.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I have a php application that needs to query the PK of a table - I'm
currently using this from the information_schema views:

SELECT column_name
FROM information_schema.table_constraints tc
INNER JOIN information_schema.constraint_column_usage ccu
ON tc.constraint_name = ccu.constraint_name
AND tc.constraint_schema = ccu.constraint_schema
WHERE constraint_type = 'PRIMARY KEY'
AND tc.table_name = '$table'
AND tc.table_schema = '$schema'

This is an extremely slow query tho.

The top/bottom of my EXPLAIN ANALYZE (8.1.9):

Nested Loop (cost=22.20..342.24 rows=1 width=64) (actual
time=68985.008..68988.784 rows=1 loops=1)
Join Filter: ("inner".oid = "outer".relnamespace)
-> Nested Loop (cost=22.20..341.15 rows=1 width=68) (actual
time=68984.922..68988.694 rows=1 loops=1)

<snip about 60 rows>

Total runtime: 68989.637 ms

I can put the full explain output on the web if it's useful.

Curious if there is a better/cheaper way to get the data I'm looking
for though?

Thanks,

Josh

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Rodrigo De León 2007-09-05 21:38:08 Re: Querying database for table pk - better way?
Previous Message Scott Marlowe 2007-09-05 20:53:42 Re: psql hanging