Commit 90371a24 authored by Tom Lane's avatar Tom Lane

Improve psql's \d command to show whether index columns are key columns.

This is essential information when looking at an index that has
"included" columns.  Per discussion, follow the style used in \dC
and some other places: column header is "Key?" and values are "yes"
or "no" (all translatable).

While at it, revise describeOneTableDetails to be a bit more maintainable:
avoid hard-wired column numbers and multiple repetitions of what needs
to be identical test logic.  This also results in the emitted catalog
query corresponding more closely to what we print, which should be a
benefit to users of ECHO_HIDDEN mode, and perhaps a bit faster too
(the old logic sometimes asked for values it would not print, even
ones that are fairly expensive to get).

Discussion: https://postgr.es/m/21724.1531943735@sss.pgh.pa.us
parent 028e3da2
This diff is collapsed.
...@@ -112,11 +112,11 @@ HINT: Alter statistics on table column instead. ...@@ -112,11 +112,11 @@ HINT: Alter statistics on table column instead.
ALTER INDEX attmp_idx ALTER COLUMN 2 SET STATISTICS 1000; ALTER INDEX attmp_idx ALTER COLUMN 2 SET STATISTICS 1000;
\d+ attmp_idx \d+ attmp_idx
Index "public.attmp_idx" Index "public.attmp_idx"
Column | Type | Definition | Storage | Stats target Column | Type | Key? | Definition | Storage | Stats target
--------+------------------+------------+---------+-------------- --------+------------------+------+------------+---------+--------------
a | integer | a | plain | a | integer | yes | a | plain |
expr | double precision | (d + e) | plain | 1000 expr | double precision | yes | (d + e) | plain | 1000
b | cstring | b | plain | b | cstring | yes | b | plain |
btree, for table "public.attmp" btree, for table "public.attmp"
ALTER INDEX attmp_idx ALTER COLUMN 3 SET STATISTICS 1000; ALTER INDEX attmp_idx ALTER COLUMN 3 SET STATISTICS 1000;
......
...@@ -2363,9 +2363,9 @@ CREATE INDEX gin_relopts_test ON array_index_op_test USING gin (i) ...@@ -2363,9 +2363,9 @@ CREATE INDEX gin_relopts_test ON array_index_op_test USING gin (i)
WITH (FASTUPDATE=on, GIN_PENDING_LIST_LIMIT=128); WITH (FASTUPDATE=on, GIN_PENDING_LIST_LIMIT=128);
\d+ gin_relopts_test \d+ gin_relopts_test
Index "public.gin_relopts_test" Index "public.gin_relopts_test"
Column | Type | Definition | Storage | Stats target Column | Type | Key? | Definition | Storage | Stats target
--------+---------+------------+---------+-------------- --------+---------+------+------------+---------+--------------
i | integer | i | plain | i | integer | yes | i | plain |
gin, for table "public.array_index_op_test" gin, for table "public.array_index_op_test"
Options: fastupdate=on, gin_pending_list_limit=128 Options: fastupdate=on, gin_pending_list_limit=128
...@@ -2583,10 +2583,10 @@ Indexes: ...@@ -2583,10 +2583,10 @@ Indexes:
\d cwi_uniq_idx \d cwi_uniq_idx
Index "public.cwi_uniq_idx" Index "public.cwi_uniq_idx"
Column | Type | Definition Column | Type | Key? | Definition
--------+-----------------------+------------ --------+-----------------------+------+------------
a | integer | a a | integer | yes | a
b | character varying(10) | b b | character varying(10) | yes | b
primary key, btree, for table "public.cwi_test" primary key, btree, for table "public.cwi_test"
CREATE UNIQUE INDEX cwi_uniq2_idx ON cwi_test(b , a); CREATE UNIQUE INDEX cwi_uniq2_idx ON cwi_test(b , a);
...@@ -2606,10 +2606,10 @@ Indexes: ...@@ -2606,10 +2606,10 @@ Indexes:
\d cwi_replaced_pkey \d cwi_replaced_pkey
Index "public.cwi_replaced_pkey" Index "public.cwi_replaced_pkey"
Column | Type | Definition Column | Type | Key? | Definition
--------+-----------------------+------------ --------+-----------------------+------+------------
b | character varying(10) | b b | character varying(10) | yes | b
a | integer | a a | integer | yes | a
primary key, btree, for table "public.cwi_test" primary key, btree, for table "public.cwi_test"
DROP INDEX cwi_replaced_pkey; -- Should fail; a constraint depends on it DROP INDEX cwi_replaced_pkey; -- Should fail; a constraint depends on it
......
...@@ -20,13 +20,13 @@ WHERE i.indrelid = 'tbl_include_reg'::regclass ORDER BY c.relname; ...@@ -20,13 +20,13 @@ WHERE i.indrelid = 'tbl_include_reg'::regclass ORDER BY c.relname;
(2 rows) (2 rows)
\d tbl_include_reg_idx \d tbl_include_reg_idx
Index "public.tbl_include_reg_idx" Index "public.tbl_include_reg_idx"
Column | Type | Definition Column | Type | Key? | Definition
--------+---------+------------ --------+---------+------+------------
c1 | integer | c1 c1 | integer | yes | c1
c2 | integer | c2 c2 | integer | yes | c2
c3 | integer | c3 c3 | integer | no | c3
c4 | box | c4 c4 | box | no | c4
btree, for table "public.tbl_include_reg" btree, for table "public.tbl_include_reg"
-- Unique index and unique constraint -- Unique index and unique constraint
......
...@@ -67,17 +67,17 @@ INSERT INTO testschema.test_default_tab VALUES (1); ...@@ -67,17 +67,17 @@ INSERT INTO testschema.test_default_tab VALUES (1);
CREATE INDEX test_index1 on testschema.test_default_tab (id); CREATE INDEX test_index1 on testschema.test_default_tab (id);
CREATE INDEX test_index2 on testschema.test_default_tab (id) TABLESPACE regress_tblspace; CREATE INDEX test_index2 on testschema.test_default_tab (id) TABLESPACE regress_tblspace;
\d testschema.test_index1 \d testschema.test_index1
Index "testschema.test_index1" Index "testschema.test_index1"
Column | Type | Definition Column | Type | Key? | Definition
--------+--------+------------ --------+--------+------+------------
id | bigint | id id | bigint | yes | id
btree, for table "testschema.test_default_tab" btree, for table "testschema.test_default_tab"
\d testschema.test_index2 \d testschema.test_index2
Index "testschema.test_index2" Index "testschema.test_index2"
Column | Type | Definition Column | Type | Key? | Definition
--------+--------+------------ --------+--------+------+------------
id | bigint | id id | bigint | yes | id
btree, for table "testschema.test_default_tab" btree, for table "testschema.test_default_tab"
Tablespace: "regress_tblspace" Tablespace: "regress_tblspace"
...@@ -86,17 +86,17 @@ SET default_tablespace TO regress_tblspace; ...@@ -86,17 +86,17 @@ SET default_tablespace TO regress_tblspace;
-- tablespace should not change if no rewrite -- tablespace should not change if no rewrite
ALTER TABLE testschema.test_default_tab ALTER id TYPE bigint; ALTER TABLE testschema.test_default_tab ALTER id TYPE bigint;
\d testschema.test_index1 \d testschema.test_index1
Index "testschema.test_index1" Index "testschema.test_index1"
Column | Type | Definition Column | Type | Key? | Definition
--------+--------+------------ --------+--------+------+------------
id | bigint | id id | bigint | yes | id
btree, for table "testschema.test_default_tab" btree, for table "testschema.test_default_tab"
\d testschema.test_index2 \d testschema.test_index2
Index "testschema.test_index2" Index "testschema.test_index2"
Column | Type | Definition Column | Type | Key? | Definition
--------+--------+------------ --------+--------+------+------------
id | bigint | id id | bigint | yes | id
btree, for table "testschema.test_default_tab" btree, for table "testschema.test_default_tab"
Tablespace: "regress_tblspace" Tablespace: "regress_tblspace"
...@@ -109,17 +109,17 @@ SELECT * FROM testschema.test_default_tab; ...@@ -109,17 +109,17 @@ SELECT * FROM testschema.test_default_tab;
-- tablespace should not change even if there is an index rewrite -- tablespace should not change even if there is an index rewrite
ALTER TABLE testschema.test_default_tab ALTER id TYPE int; ALTER TABLE testschema.test_default_tab ALTER id TYPE int;
\d testschema.test_index1 \d testschema.test_index1
Index "testschema.test_index1" Index "testschema.test_index1"
Column | Type | Definition Column | Type | Key? | Definition
--------+---------+------------ --------+---------+------+------------
id | integer | id id | integer | yes | id
btree, for table "testschema.test_default_tab" btree, for table "testschema.test_default_tab"
\d testschema.test_index2 \d testschema.test_index2
Index "testschema.test_index2" Index "testschema.test_index2"
Column | Type | Definition Column | Type | Key? | Definition
--------+---------+------------ --------+---------+------+------------
id | integer | id id | integer | yes | id
btree, for table "testschema.test_default_tab" btree, for table "testschema.test_default_tab"
Tablespace: "regress_tblspace" Tablespace: "regress_tblspace"
...@@ -134,34 +134,34 @@ SET default_tablespace TO ''; ...@@ -134,34 +134,34 @@ SET default_tablespace TO '';
-- tablespace should not change if no rewrite -- tablespace should not change if no rewrite
ALTER TABLE testschema.test_default_tab ALTER id TYPE int; ALTER TABLE testschema.test_default_tab ALTER id TYPE int;
\d testschema.test_index1 \d testschema.test_index1
Index "testschema.test_index1" Index "testschema.test_index1"
Column | Type | Definition Column | Type | Key? | Definition
--------+---------+------------ --------+---------+------+------------
id | integer | id id | integer | yes | id
btree, for table "testschema.test_default_tab" btree, for table "testschema.test_default_tab"
\d testschema.test_index2 \d testschema.test_index2
Index "testschema.test_index2" Index "testschema.test_index2"
Column | Type | Definition Column | Type | Key? | Definition
--------+---------+------------ --------+---------+------+------------
id | integer | id id | integer | yes | id
btree, for table "testschema.test_default_tab" btree, for table "testschema.test_default_tab"
Tablespace: "regress_tblspace" Tablespace: "regress_tblspace"
-- tablespace should not change even if there is an index rewrite -- tablespace should not change even if there is an index rewrite
ALTER TABLE testschema.test_default_tab ALTER id TYPE bigint; ALTER TABLE testschema.test_default_tab ALTER id TYPE bigint;
\d testschema.test_index1 \d testschema.test_index1
Index "testschema.test_index1" Index "testschema.test_index1"
Column | Type | Definition Column | Type | Key? | Definition
--------+--------+------------ --------+--------+------+------------
id | bigint | id id | bigint | yes | id
btree, for table "testschema.test_default_tab" btree, for table "testschema.test_default_tab"
\d testschema.test_index2 \d testschema.test_index2
Index "testschema.test_index2" Index "testschema.test_index2"
Column | Type | Definition Column | Type | Key? | Definition
--------+--------+------------ --------+--------+------+------------
id | bigint | id id | bigint | yes | id
btree, for table "testschema.test_default_tab" btree, for table "testschema.test_default_tab"
Tablespace: "regress_tblspace" Tablespace: "regress_tblspace"
...@@ -174,18 +174,18 @@ ALTER TABLE testschema.test_tab ADD CONSTRAINT test_tab_unique UNIQUE (id); ...@@ -174,18 +174,18 @@ ALTER TABLE testschema.test_tab ADD CONSTRAINT test_tab_unique UNIQUE (id);
SET default_tablespace TO ''; SET default_tablespace TO '';
ALTER TABLE testschema.test_tab ADD CONSTRAINT test_tab_pkey PRIMARY KEY (id); ALTER TABLE testschema.test_tab ADD CONSTRAINT test_tab_pkey PRIMARY KEY (id);
\d testschema.test_tab_unique \d testschema.test_tab_unique
Index "testschema.test_tab_unique" Index "testschema.test_tab_unique"
Column | Type | Definition Column | Type | Key? | Definition
--------+---------+------------ --------+---------+------+------------
id | integer | id id | integer | yes | id
unique, btree, for table "testschema.test_tab" unique, btree, for table "testschema.test_tab"
Tablespace: "regress_tblspace" Tablespace: "regress_tblspace"
\d testschema.test_tab_pkey \d testschema.test_tab_pkey
Index "testschema.test_tab_pkey" Index "testschema.test_tab_pkey"
Column | Type | Definition Column | Type | Key? | Definition
--------+---------+------------ --------+---------+------+------------
id | integer | id id | integer | yes | id
primary key, btree, for table "testschema.test_tab" primary key, btree, for table "testschema.test_tab"
SELECT * FROM testschema.test_tab; SELECT * FROM testschema.test_tab;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment