Re: \d <Table> functionality in a query.

From: Joe Conway <mail(at)joeconway(dot)com>
To: Timothy Grant <tjg(at)craigelachie(dot)org>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: \d <Table> functionality in a query.
Date: 2002-12-03 03:44:10
Message-ID: 3DEC288A.3060001@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Timothy Grant wrote:
> Hi all,
>
> I'm sure it's a FAQ, and I discovered syscat.source, but was still not
> able to determine how to get a table description in a query.
>

Start psql with "-E" on the command line. Then you see all the internally
generated queries. This is using cvs, but it works in prior versions also:

$ psql -E regression
********* QUERY **********
BEGIN; SELECT usesuper FROM pg_catalog.pg_user WHERE usename = 'postgres'; COMMIT
**************************

Welcome to psql 7.4devel, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help on internal slash commands
\g or terminate with semicolon to execute query
\q to quit

regression=# \d foo
********* QUERY **********
SELECT c.oid,
n.nspname,
c.relname
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE pg_catalog.pg_table_is_visible(c.oid)
AND c.relname ~ '^foo$'
ORDER BY 2, 3;
**************************

********* QUERY **********
SELECT relhasindex, relkind, relchecks, reltriggers, relhasrules
FROM pg_catalog.pg_class WHERE oid = '822242'
**************************

********* QUERY **********
SELECT a.attname,
pg_catalog.format_type(a.atttypid, a.atttypmod),
a.attnotnull, a.atthasdef, a.attnum
FROM pg_catalog.pg_attribute a
WHERE a.attrelid = '822242' AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
**************************

********* QUERY **********
SELECT c2.relname, i.indisprimary, i.indisunique,
pg_catalog.pg_get_indexdef(i.indexrelid)
FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i
WHERE c.oid = '822242' AND c.oid = i.indrelid AND i.indexrelid = c2.oid
ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname
**************************

Table "public.foo"
Column | Type | Modifiers
--------+---------+-----------
f1 | integer | not null
f2 | text | not null
f3 | text[] |
Indexes: foo_pkey primary key btree (f1, f2)

HTH,

Joe

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Timothy Grant 2002-12-03 03:52:21 Re: \d <Table> functionality in a query.
Previous Message Justin Clift 2002-12-03 03:41:27 Re: PostgreSQL in Universities (Was: Re: 7.4 Wishlist)