From 59d102e21c0261c7349fc5ad55026d0125972836 Mon Sep 17 00:00:00 2001
From: Georgios <gkokolatos@protonmail.com> 
Date: Mon, 17 Aug 2020 22:51:17 +0530
Subject: [PATCH v6] Include access method in listTables output

Author: Georgios <gkokolatos@protonmail.com> 
Discussion: https://www.postgresql.org/message-id/svaS1VTOEscES9CLKVTeKItjJP1EEJuBhTsA0ESOdlnbXeQSgycYwVlliL5zt8Jwcfo4ATYDXtEqsExxjkSkkhCSTCL8fnRgaCAJdr0unUg%3D%40protonmail.com
---
 doc/src/sgml/ref/psql-ref.sgml     |  4 +++-
 src/bin/psql/describe.c            | 14 ++++++++++-
 src/test/regress/expected/psql.out | 49 ++++++++++++++++++++++++++++++++++----
 src/test/regress/sql/psql.sql      | 17 ++++++++++++-
 4 files changed, 76 insertions(+), 8 deletions(-)

diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index fc16e6c..7667687 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -1183,7 +1183,9 @@ testdb=&gt;
         columns of the table are shown, as is the presence of OIDs in the
         table, the view definition if the relation is a view, a non-default
         <link linkend="sql-createtable-replica-identity">replica
-        identity</link> setting.
+        identity</link> setting, the access method name
+        <link linkend="sql-create-access-method"> if the relation has access
+        method.
         </para>
 
         <para>
diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index d81f157..ac6a93c 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -3678,7 +3678,7 @@ listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSys
 	PGresult   *res;
 	printQueryOpt myopt = pset.popt;
 	int			cols_so_far;
-	bool		translate_columns[] = {false, false, true, false, false, false, false, false};
+	bool		translate_columns[] = {false, false, true, false, false, false, false, false, false};
 
 	/* If tabtypes is empty, we default to \dtvmsE (but see also command.c) */
 	if (!(showTables || showIndexes || showViews || showMatViews || showSeq || showForeign))
@@ -3751,6 +3751,12 @@ listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSys
 		 * to; this might change with future additions to the output columns.
 		 */
 
+		if (pset.sversion >= 120000 && !pset.hide_tableam &&
+			(showTables || showMatViews || showIndexes))
+			appendPQExpBuffer(&buf,
+							  ",\n  am.amname as \"%s\"",
+							  gettext_noop("Access Method"));
+
 		/*
 		 * As of PostgreSQL 9.0, use pg_table_size() to show a more accurate
 		 * size of a table, including FSM, VM and TOAST tables.
@@ -3772,6 +3778,12 @@ listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSys
 	appendPQExpBufferStr(&buf,
 						 "\nFROM pg_catalog.pg_class c"
 						 "\n     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace");
+
+	if (pset.sversion >= 120000 && !pset.hide_tableam &&
+		(showTables || showMatViews || showIndexes))
+		appendPQExpBufferStr(&buf,
+						 "\n     LEFT JOIN pg_catalog.pg_am am ON am.oid = c.relam");
+
 	if (showIndexes)
 		appendPQExpBufferStr(&buf,
 							 "\n     LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid"
diff --git a/src/test/regress/expected/psql.out b/src/test/regress/expected/psql.out
index 555d464..e497adf 100644
--- a/src/test/regress/expected/psql.out
+++ b/src/test/regress/expected/psql.out
@@ -2796,19 +2796,24 @@ Type                | func
 
 \pset tuples_only false
 -- check conditional tableam display
--- Create a heap2 table am handler with heapam handler
+\pset expanded off
+CREATE SCHEMA conditional_tableam_display;
+CREATE ROLE	conditional_tableam_display_role;
+ALTER SCHEMA conditional_tableam_display OWNER TO conditional_tableam_display_role;
+SET search_path TO conditional_tableam_display;
 CREATE ACCESS METHOD heap_psql TYPE TABLE HANDLER heap_tableam_handler;
+SET role TO conditional_tableam_display_role;
 CREATE TABLE tbl_heap_psql(f1 int, f2 char(100)) using heap_psql;
 CREATE TABLE tbl_heap(f1 int, f2 char(100)) using heap;
 \d+ tbl_heap_psql
-                                   Table "public.tbl_heap_psql"
+                        Table "conditional_tableam_display.tbl_heap_psql"
  Column |      Type      | Collation | Nullable | Default | Storage  | Stats target | Description 
 --------+----------------+-----------+----------+---------+----------+--------------+-------------
  f1     | integer        |           |          |         | plain    |              | 
  f2     | character(100) |           |          |         | extended |              | 
 
 \d+ tbl_heap
-                                     Table "public.tbl_heap"
+                           Table "conditional_tableam_display.tbl_heap"
  Column |      Type      | Collation | Nullable | Default | Storage  | Stats target | Description 
 --------+----------------+-----------+----------+---------+----------+--------------+-------------
  f1     | integer        |           |          |         | plain    |              | 
@@ -2816,7 +2821,7 @@ CREATE TABLE tbl_heap(f1 int, f2 char(100)) using heap;
 
 \set HIDE_TABLEAM off
 \d+ tbl_heap_psql
-                                   Table "public.tbl_heap_psql"
+                        Table "conditional_tableam_display.tbl_heap_psql"
  Column |      Type      | Collation | Nullable | Default | Storage  | Stats target | Description 
 --------+----------------+-----------+----------+---------+----------+--------------+-------------
  f1     | integer        |           |          |         | plain    |              | 
@@ -2824,16 +2829,50 @@ CREATE TABLE tbl_heap(f1 int, f2 char(100)) using heap;
 Access method: heap_psql
 
 \d+ tbl_heap
-                                     Table "public.tbl_heap"
+                           Table "conditional_tableam_display.tbl_heap"
  Column |      Type      | Collation | Nullable | Default | Storage  | Stats target | Description 
 --------+----------------+-----------+----------+---------+----------+--------------+-------------
  f1     | integer        |           |          |         | plain    |              | 
  f2     | character(100) |           |          |         | extended |              | 
 Access method: heap
 
+\d+
+                                                              List of relations
+           Schema            |     Name      | Type  |              Owner               | Persistence | Access Method |  Size   | Description 
+-----------------------------+---------------+-------+----------------------------------+-------------+---------------+---------+-------------
+ conditional_tableam_display | tbl_heap      | table | conditional_tableam_display_role | permanent   | heap          | 0 bytes | 
+ conditional_tableam_display | tbl_heap_psql | table | conditional_tableam_display_role | permanent   | heap_psql     | 0 bytes | 
+(2 rows)
+
+-- access method column should not be displayed for sequences
+\ds+
+                        List of relations
+ Schema | Name | Type | Owner | Persistence | Size | Description 
+--------+------+------+-------+-------------+------+-------------
+(0 rows)
+
+-- access method column should not be displayed for views 
+\dv+
+                        List of relations
+ Schema | Name | Type | Owner | Persistence | Size | Description 
+--------+------+------+-------+-------------+------+-------------
+(0 rows)
+
 \set HIDE_TABLEAM on
+\d+
+                                                      List of relations
+           Schema            |     Name      | Type  |              Owner               | Persistence |  Size   | Description 
+-----------------------------+---------------+-------+----------------------------------+-------------+---------+-------------
+ conditional_tableam_display | tbl_heap      | table | conditional_tableam_display_role | permanent   | 0 bytes | 
+ conditional_tableam_display | tbl_heap_psql | table | conditional_tableam_display_role | permanent   | 0 bytes | 
+(2 rows)
+
+SET ROLE TO default;
 DROP TABLE tbl_heap, tbl_heap_psql;
 DROP ACCESS METHOD heap_psql;
+SET search_path TO default;
+DROP SCHEMA conditional_tableam_display;
+DROP ROLE conditional_tableam_display_role;
 -- test numericlocale (as best we can without control of psql's locale)
 \pset format aligned
 \pset expanded off
diff --git a/src/test/regress/sql/psql.sql b/src/test/regress/sql/psql.sql
index 5a16080..920652b 100644
--- a/src/test/regress/sql/psql.sql
+++ b/src/test/regress/sql/psql.sql
@@ -456,9 +456,14 @@ select 1 where false;
 \pset tuples_only false
 
 -- check conditional tableam display
+\pset expanded off
 
--- Create a heap2 table am handler with heapam handler
+CREATE SCHEMA conditional_tableam_display;
+CREATE ROLE	conditional_tableam_display_role;
+ALTER SCHEMA conditional_tableam_display OWNER TO conditional_tableam_display_role;
+SET search_path TO conditional_tableam_display;
 CREATE ACCESS METHOD heap_psql TYPE TABLE HANDLER heap_tableam_handler;
+SET role TO conditional_tableam_display_role;
 CREATE TABLE tbl_heap_psql(f1 int, f2 char(100)) using heap_psql;
 CREATE TABLE tbl_heap(f1 int, f2 char(100)) using heap;
 \d+ tbl_heap_psql
@@ -466,9 +471,19 @@ CREATE TABLE tbl_heap(f1 int, f2 char(100)) using heap;
 \set HIDE_TABLEAM off
 \d+ tbl_heap_psql
 \d+ tbl_heap
+\d+
+-- access method column should not be displayed for sequences
+\ds+
+-- access method column should not be displayed for views 
+\dv+
 \set HIDE_TABLEAM on
+\d+
+SET ROLE TO default;
 DROP TABLE tbl_heap, tbl_heap_psql;
 DROP ACCESS METHOD heap_psql;
+SET search_path TO default;
+DROP SCHEMA conditional_tableam_display;
+DROP ROLE conditional_tableam_display_role;
 
 -- test numericlocale (as best we can without control of psql's locale)
 
-- 
1.8.3.1

