Обсуждение: \d functionality in a query. Поиск
Список
Период
Сортировка
functionality in a query.
От
Timothy Grant
Дата:
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.

I want my code to be able to get column names and column types.

Thanks.

--
Stand Fast,
    tjg.

Timothy Grant
www.craigelachie.org

Вложения
functionality in a query.
От
Joe Conway
Дата:
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




functionality in a query.
От
Timothy Grant
Дата:
Joe,

Thank you so very much! That is gonna be hugely helpful!

Wow! Slightly more involved queries than I expected! Anyway that's a
huge help.

On Mon, 2002-12-02 at 19:44, Joe Conway wrote:
> 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
>
>
>
>
--
Stand Fast,
    tjg.

Timothy Grant
www.craigelachie.org

Вложения