Skip to content

Commit 07f386e

Browse files
committed
Add access method names to \d[i|m|t]+ in psql
Listing a full set of relations with those psql meta-commands, without a matching pattern, has never showed the access method associated with each relation. This commit adds the access method of tables, indexes and matviews, masking it for relation kinds where it does not apply. Note that when HIDE_TABLEAM is enabled, the information does not show up. This is available when connecting to a backend version of at least 12, where table AMs have been introduced. Author: Georgios Kokolatos Reviewed-by: Vignesh C, Michael Paquier, Justin Pryzby Discussion: https://postgr.es/m/svaS1VTOEscES9CLKVTeKItjJP1EEJuBhTsA0ESOdlnbXeQSgycYwVlliL5zt8Jwcfo4ATYDXtEqsExxjkSkkhCSTCL8fnRgaCAJdr0unUg=@protonmail.com
1 parent 0176753 commit 07f386e

File tree

4 files changed

+108
-12
lines changed

4 files changed

+108
-12
lines changed

doc/src/sgml/ref/psql-ref.sgml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1183,7 +1183,9 @@ testdb=>
11831183
columns of the table are shown, as is the presence of OIDs in the
11841184
table, the view definition if the relation is a view, a non-default
11851185
<link linkend="sql-createtable-replica-identity">replica
1186-
identity</link> setting.
1186+
identity</link> setting and the
1187+
<link linkend="sql-create-access-method">access method</link> name
1188+
if the relation has an access method.
11871189
</para>
11881190

11891191
<para>

src/bin/psql/describe.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3678,7 +3678,7 @@ listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSys
36783678
PGresult *res;
36793679
printQueryOpt myopt = pset.popt;
36803680
int cols_so_far;
3681-
bool translate_columns[] = {false, false, true, false, false, false, false, false};
3681+
bool translate_columns[] = {false, false, true, false, false, false, false, false, false};
36823682

36833683
/* If tabtypes is empty, we default to \dtvmsE (but see also command.c) */
36843684
if (!(showTables || showIndexes || showViews || showMatViews || showSeq || showForeign))
@@ -3751,6 +3751,16 @@ listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSys
37513751
* to; this might change with future additions to the output columns.
37523752
*/
37533753

3754+
/*
3755+
* Access methods exist for tables, materialized views and indexes.
3756+
* This has been introduced in PostgreSQL 12 for tables.
3757+
*/
3758+
if (pset.sversion >= 120000 && !pset.hide_tableam &&
3759+
(showTables || showMatViews || showIndexes))
3760+
appendPQExpBuffer(&buf,
3761+
",\n am.amname as \"%s\"",
3762+
gettext_noop("Access Method"));
3763+
37543764
/*
37553765
* As of PostgreSQL 9.0, use pg_table_size() to show a more accurate
37563766
* size of a table, including FSM, VM and TOAST tables.
@@ -3772,6 +3782,12 @@ listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSys
37723782
appendPQExpBufferStr(&buf,
37733783
"\nFROM pg_catalog.pg_class c"
37743784
"\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace");
3785+
3786+
if (pset.sversion >= 120000 && !pset.hide_tableam &&
3787+
(showTables || showMatViews || showIndexes))
3788+
appendPQExpBufferStr(&buf,
3789+
"\n LEFT JOIN pg_catalog.pg_am am ON am.oid = c.relam");
3790+
37753791
if (showIndexes)
37763792
appendPQExpBufferStr(&buf,
37773793
"\n LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid"

src/test/regress/expected/psql.out

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2795,45 +2795,105 @@ Argument data types | numeric
27952795
Type | func
27962796

27972797
\pset tuples_only false
2798-
-- check conditional tableam display
2799-
-- Create a heap2 table am handler with heapam handler
2798+
-- check conditional am display
2799+
\pset expanded off
2800+
CREATE SCHEMA tableam_display;
2801+
CREATE ROLE regress_display_role;
2802+
ALTER SCHEMA tableam_display OWNER TO regress_display_role;
2803+
SET search_path TO tableam_display;
28002804
CREATE ACCESS METHOD heap_psql TYPE TABLE HANDLER heap_tableam_handler;
2805+
SET ROLE TO regress_display_role;
2806+
-- Use only relations with a physical size of zero.
28012807
CREATE TABLE tbl_heap_psql(f1 int, f2 char(100)) using heap_psql;
28022808
CREATE TABLE tbl_heap(f1 int, f2 char(100)) using heap;
2809+
CREATE VIEW view_heap_psql AS SELECT f1 from tbl_heap_psql;
2810+
CREATE MATERIALIZED VIEW mat_view_heap_psql USING heap_psql AS SELECT f1 from tbl_heap_psql;
28032811
\d+ tbl_heap_psql
2804-
Table "public.tbl_heap_psql"
2812+
Table "tableam_display.tbl_heap_psql"
28052813
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
28062814
--------+----------------+-----------+----------+---------+----------+--------------+-------------
28072815
f1 | integer | | | | plain | |
28082816
f2 | character(100) | | | | extended | |
28092817

28102818
\d+ tbl_heap
2811-
Table "public.tbl_heap"
2819+
Table "tableam_display.tbl_heap"
28122820
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
28132821
--------+----------------+-----------+----------+---------+----------+--------------+-------------
28142822
f1 | integer | | | | plain | |
28152823
f2 | character(100) | | | | extended | |
28162824

28172825
\set HIDE_TABLEAM off
28182826
\d+ tbl_heap_psql
2819-
Table "public.tbl_heap_psql"
2827+
Table "tableam_display.tbl_heap_psql"
28202828
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
28212829
--------+----------------+-----------+----------+---------+----------+--------------+-------------
28222830
f1 | integer | | | | plain | |
28232831
f2 | character(100) | | | | extended | |
28242832
Access method: heap_psql
28252833

28262834
\d+ tbl_heap
2827-
Table "public.tbl_heap"
2835+
Table "tableam_display.tbl_heap"
28282836
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
28292837
--------+----------------+-----------+----------+---------+----------+--------------+-------------
28302838
f1 | integer | | | | plain | |
28312839
f2 | character(100) | | | | extended | |
28322840
Access method: heap
28332841

2842+
-- AM is displayed for tables, indexes and materialized views.
2843+
\d+
2844+
List of relations
2845+
Schema | Name | Type | Owner | Persistence | Access Method | Size | Description
2846+
-----------------+--------------------+-------------------+----------------------+-------------+---------------+---------+-------------
2847+
tableam_display | mat_view_heap_psql | materialized view | regress_display_role | permanent | heap_psql | 0 bytes |
2848+
tableam_display | tbl_heap | table | regress_display_role | permanent | heap | 0 bytes |
2849+
tableam_display | tbl_heap_psql | table | regress_display_role | permanent | heap_psql | 0 bytes |
2850+
tableam_display | view_heap_psql | view | regress_display_role | permanent | | 0 bytes |
2851+
(4 rows)
2852+
2853+
\dt+
2854+
List of relations
2855+
Schema | Name | Type | Owner | Persistence | Access Method | Size | Description
2856+
-----------------+---------------+-------+----------------------+-------------+---------------+---------+-------------
2857+
tableam_display | tbl_heap | table | regress_display_role | permanent | heap | 0 bytes |
2858+
tableam_display | tbl_heap_psql | table | regress_display_role | permanent | heap_psql | 0 bytes |
2859+
(2 rows)
2860+
2861+
\dm+
2862+
List of relations
2863+
Schema | Name | Type | Owner | Persistence | Access Method | Size | Description
2864+
-----------------+--------------------+-------------------+----------------------+-------------+---------------+---------+-------------
2865+
tableam_display | mat_view_heap_psql | materialized view | regress_display_role | permanent | heap_psql | 0 bytes |
2866+
(1 row)
2867+
2868+
-- But not for views and sequences.
2869+
\dv+
2870+
List of relations
2871+
Schema | Name | Type | Owner | Persistence | Size | Description
2872+
-----------------+----------------+------+----------------------+-------------+---------+-------------
2873+
tableam_display | view_heap_psql | view | regress_display_role | permanent | 0 bytes |
2874+
(1 row)
2875+
28342876
\set HIDE_TABLEAM on
2835-
DROP TABLE tbl_heap, tbl_heap_psql;
2877+
\d+
2878+
List of relations
2879+
Schema | Name | Type | Owner | Persistence | Size | Description
2880+
-----------------+--------------------+-------------------+----------------------+-------------+---------+-------------
2881+
tableam_display | mat_view_heap_psql | materialized view | regress_display_role | permanent | 0 bytes |
2882+
tableam_display | tbl_heap | table | regress_display_role | permanent | 0 bytes |
2883+
tableam_display | tbl_heap_psql | table | regress_display_role | permanent | 0 bytes |
2884+
tableam_display | view_heap_psql | view | regress_display_role | permanent | 0 bytes |
2885+
(4 rows)
2886+
2887+
RESET ROLE;
2888+
RESET search_path;
2889+
DROP SCHEMA tableam_display CASCADE;
2890+
NOTICE: drop cascades to 4 other objects
2891+
DETAIL: drop cascades to table tableam_display.tbl_heap_psql
2892+
drop cascades to table tableam_display.tbl_heap
2893+
drop cascades to view tableam_display.view_heap_psql
2894+
drop cascades to materialized view tableam_display.mat_view_heap_psql
28362895
DROP ACCESS METHOD heap_psql;
2896+
DROP ROLE regress_display_role;
28372897
-- test numericlocale (as best we can without control of psql's locale)
28382898
\pset format aligned
28392899
\pset expanded off

src/test/regress/sql/psql.sql

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,20 +455,38 @@ select 1 where false;
455455
\df exp
456456
\pset tuples_only false
457457

458-
-- check conditional tableam display
458+
-- check conditional am display
459+
\pset expanded off
459460

460-
-- Create a heap2 table am handler with heapam handler
461+
CREATE SCHEMA tableam_display;
462+
CREATE ROLE regress_display_role;
463+
ALTER SCHEMA tableam_display OWNER TO regress_display_role;
464+
SET search_path TO tableam_display;
461465
CREATE ACCESS METHOD heap_psql TYPE TABLE HANDLER heap_tableam_handler;
466+
SET ROLE TO regress_display_role;
467+
-- Use only relations with a physical size of zero.
462468
CREATE TABLE tbl_heap_psql(f1 int, f2 char(100)) using heap_psql;
463469
CREATE TABLE tbl_heap(f1 int, f2 char(100)) using heap;
470+
CREATE VIEW view_heap_psql AS SELECT f1 from tbl_heap_psql;
471+
CREATE MATERIALIZED VIEW mat_view_heap_psql USING heap_psql AS SELECT f1 from tbl_heap_psql;
464472
\d+ tbl_heap_psql
465473
\d+ tbl_heap
466474
\set HIDE_TABLEAM off
467475
\d+ tbl_heap_psql
468476
\d+ tbl_heap
477+
-- AM is displayed for tables, indexes and materialized views.
478+
\d+
479+
\dt+
480+
\dm+
481+
-- But not for views and sequences.
482+
\dv+
469483
\set HIDE_TABLEAM on
470-
DROP TABLE tbl_heap, tbl_heap_psql;
484+
\d+
485+
RESET ROLE;
486+
RESET search_path;
487+
DROP SCHEMA tableam_display CASCADE;
471488
DROP ACCESS METHOD heap_psql;
489+
DROP ROLE regress_display_role;
472490

473491
-- test numericlocale (as best we can without control of psql's locale)
474492

0 commit comments

Comments
 (0)