psql \d for wide tables / pattern for individual columns

Started by Justin Pryzbyalmost 7 years ago3 messageshackers
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

won't retrysuccessCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t41506
psql -h localhost -U postgres

Built from patchset v1 (message #1), July 28, 2026 at 03:30 AM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t41506_1 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t41506_1 && git checkout t41506_1

Patchset v1 (message #1) is on t41506_1

Jump to latest
#1Justin Pryzby
pryzby@telsasoft.com

We have some very wide tables (historically, up to 1600 columns ; this is
improved now, but sometimes still several hundred, with numerous pages output
to psql pager). Is is reasonable to suggest adding a psql command to show a
table's definition, without all the columns listed?

Or limit display to matching columns ? That's more general than the above
functionality, if "empty string" is taken to mean "show no columns", like \d
table "" or \d table *id or \d table ????

Attached minimal patch for the latter.

postgres=# \d pg_attribute ""
Table "pg_catalog.pg_attribute"
Column | Type | Collation | Nullable | Default
--------+------+-----------+----------+---------
Indexes:
"pg_attribute_relid_attnam_index" UNIQUE, btree (attrelid, attname)
"pg_attribute_relid_attnum_index" UNIQUE, btree (attrelid, attnum)

postgres=# \d pg_attribute "attn*|attrel*"
Table "pg_catalog.pg_attribute"
Column | Type | Collation | Nullable | Default
------------+----------+-----------+----------+---------
attrelid | oid | | not null |
attname | name | | not null |
attnum | smallint | | not null |
attndims | integer | | not null |
attnotnull | boolean | | not null |
Indexes:
"pg_attribute_relid_attnam_index" UNIQUE, btree (attrelid, attname)
"pg_attribute_relid_attnum_index" UNIQUE, btree (attrelid, attnum)

postgres=# \d pg_attribute ??????
Table "pg_catalog.pg_attribute"
Column | Type | Collation | Nullable | Default
--------+-----------+-----------+----------+---------
attlen | smallint | | not null |
attnum | smallint | | not null |
attacl | aclitem[] | | |
Indexes:
"pg_attribute_relid_attnam_index" UNIQUE, btree (attrelid, attname)
"pg_attribute_relid_attnum_index" UNIQUE, btree (attrelid, attnum)

postgres=# \d pg_attribute *id
Table "pg_catalog.pg_attribute"
Column | Type | Collation | Nullable | Default
----------+------+-----------+----------+---------
attrelid | oid | | not null |
atttypid | oid | | not null |
Indexes:
"pg_attribute_relid_attnam_index" UNIQUE, btree (attrelid, attname)
"pg_attribute_relid_attnum_index" UNIQUE, btree (attrelid, attnum)

Attachments:

t41506_1
v1-0001-psql-Allow-filtering-columns-shown-by-d.patchtext/x-diff; charset=us-asciiDownload+22-14
In reply to: Justin Pryzby (#1)
Re: psql \d for wide tables / pattern for individual columns

Em dom., 10 de nov. de 2019 às 18:29, Justin Pryzby
<pryzby@telsasoft.com> escreveu:

We have some very wide tables (historically, up to 1600 columns ; this is
improved now, but sometimes still several hundred, with numerous pages output
to psql pager). Is is reasonable to suggest adding a psql command to show a
table's definition, without all the columns listed?

It seems a good idea. However, I'm afraid adding a second argument
could limit our capabilities to match/suppress other table properties
in the future. For example, I think psql might have a way to omit
indexes, FKs, partitions, some column properties, or even show GRANTs
for that table. I don't have a concrete plan at the moment but maybe
someone else already thought about it.

Or limit display to matching columns ? That's more general than the above
functionality, if "empty string" is taken to mean "show no columns", like \d
table "" or \d table *id or \d table ????

Attached minimal patch for the latter.

postgres=# \d pg_attribute ""
Table "pg_catalog.pg_attribute"
Column | Type | Collation | Nullable | Default
--------+------+-----------+----------+---------
Indexes:
"pg_attribute_relid_attnam_index" UNIQUE, btree (attrelid, attname)
"pg_attribute_relid_attnum_index" UNIQUE, btree (attrelid, attnum)

The problem with your proposal is that I can't differentiate a
complete output from another suppress-some-columns output if you don't
provide the meta-command. I think you should explicitly show that some
columns were suppressed (something like "... suppressed columns..."
after the list of matched columns). If you don't, it could lead to
confusion while reporting table description.

--
Euler Taveira Timbira -
http://www.timbira.com.br/
PostgreSQL: Consultoria, Desenvolvimento, Suporte 24x7 e Treinamento

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Euler Taveira de Oliveira (#2)
Re: psql \d for wide tables / pattern for individual columns

Euler Taveira <euler@timbira.com.br> writes:

Em dom., 10 de nov. de 2019 às 18:29, Justin Pryzby
<pryzby@telsasoft.com> escreveu:

We have some very wide tables (historically, up to 1600 columns ; this is
improved now, but sometimes still several hundred, with numerous pages output
to psql pager). Is is reasonable to suggest adding a psql command to show a
table's definition, without all the columns listed?

It seems a good idea. However, I'm afraid adding a second argument
could limit our capabilities to match/suppress other table properties
in the future.

Yeah, that was my immediate reaction to the proposed syntax as well.
I think we'd better make sure that we aren't foreclosing future
extensions of \d.

Maybe a reasonable idea is to expect that any additional arguments
are in "keyword=value" style, so that the immediate need could be
met with

\d mytable columns=<pattern>

It might already be worthwhile to allow both positive and negative
patterns, so also

\d mytable exclude_columns=<pattern>

The problem with your proposal is that I can't differentiate a
complete output from another suppress-some-columns output if you don't
provide the meta-command. I think you should explicitly show that some
columns were suppressed (something like "... suppressed columns..."
after the list of matched columns). If you don't, it could lead to
confusion while reporting table description.

Hm ... "N columns suppressed" might sometimes be useful, but I'm afraid
it would take an extra query to get it, and I'm not sure it's worth it.
I think someone who's using these options would already know perfectly
well what they're hiding. We don't expect, say, "\dt my*" to tell you
how many tables it didn't list.

regards, tom lane