Commit c29abc8b authored by Tom Lane's avatar Tom Lane

Fix assorted infelicities in collation handling in psql's describe.c.

In \d, be more careful to print collation only if it's not the default for
the column's data type.  Avoid assuming that the name "default" is magic.

Fix \d on a composite type so that it will print per-column collations.
It's no longer the case that a composite type cannot have modifiers.
(In consequence, the expected outputs for composite-type regression tests
change.)

Fix \dD so that it will print collation for a domain, again only if it's
not the same as the base type's collation.
parent 2d461712
...@@ -1287,11 +1287,12 @@ describeOneTableDetails(const char *schemaname, ...@@ -1287,11 +1287,12 @@ describeOneTableDetails(const char *schemaname,
"\n (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)" "\n (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)"
"\n FROM pg_catalog.pg_attrdef d" "\n FROM pg_catalog.pg_attrdef d"
"\n WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef)," "\n WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),"
"\n a.attnotnull, a.attnum"); "\n a.attnotnull, a.attnum,");
if (pset.sversion >= 90100) if (pset.sversion >= 90100)
appendPQExpBuffer(&buf, ",\n (SELECT collname FROM pg_collation WHERE oid = a.attcollation AND collname <> 'default') AS attcollation"); appendPQExpBuffer(&buf, "\n (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t\n"
" WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) AS attcollation");
else else
appendPQExpBuffer(&buf, ",\n NULL AS attcollation"); appendPQExpBuffer(&buf, "\n NULL AS attcollation");
if (tableinfo.relkind == 'i') if (tableinfo.relkind == 'i')
appendPQExpBuffer(&buf, ",\n pg_catalog.pg_get_indexdef(a.attrelid, a.attnum, TRUE) AS indexdef"); appendPQExpBuffer(&buf, ",\n pg_catalog.pg_get_indexdef(a.attrelid, a.attnum, TRUE) AS indexdef");
if (verbose) if (verbose)
...@@ -1362,7 +1363,7 @@ describeOneTableDetails(const char *schemaname, ...@@ -1362,7 +1363,7 @@ describeOneTableDetails(const char *schemaname,
cols = 2; cols = 2;
if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' || if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
tableinfo.relkind == 'f') tableinfo.relkind == 'f' || tableinfo.relkind == 'c')
{ {
show_modifiers = true; show_modifiers = true;
headers[cols++] = gettext_noop("Modifiers"); headers[cols++] = gettext_noop("Modifiers");
...@@ -2697,22 +2698,27 @@ listDomains(const char *pattern, bool showSystem) ...@@ -2697,22 +2698,27 @@ listDomains(const char *pattern, bool showSystem)
printfPQExpBuffer(&buf, printfPQExpBuffer(&buf,
"SELECT n.nspname as \"%s\",\n" "SELECT n.nspname as \"%s\",\n"
" t.typname as \"%s\",\n" " t.typname as \"%s\",\n"
" pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n" " pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n"
" CASE WHEN t.typnotnull AND t.typdefault IS NOT NULL THEN 'not null default '||t.typdefault\n" " TRIM(LEADING\n",
" WHEN t.typnotnull AND t.typdefault IS NULL THEN 'not null'\n" gettext_noop("Schema"),
" WHEN NOT t.typnotnull AND t.typdefault IS NOT NULL THEN 'default '||t.typdefault\n" gettext_noop("Name"),
" ELSE ''\n" gettext_noop("Type"));
" END as \"%s\",\n" if (pset.sversion >= 90100)
appendPQExpBuffer(&buf,
" COALESCE((SELECT ' collate ' || c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type bt\n"
" WHERE c.oid = t.typcollation AND bt.oid = t.typbasetype AND t.typcollation <> bt.typcollation), '') ||\n");
appendPQExpBuffer(&buf,
" CASE WHEN t.typnotnull THEN ' not null' ELSE '' END ||\n"
" CASE WHEN t.typdefault IS NOT NULL THEN ' default ' || t.typdefault ELSE '' END\n"
" ) as \"%s\",\n",
gettext_noop("Modifier"));
appendPQExpBuffer(&buf,
" pg_catalog.array_to_string(ARRAY(\n" " pg_catalog.array_to_string(ARRAY(\n"
" SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n" " SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n"
" ), ' ') as \"%s\"\n" " ), ' ') as \"%s\"\n"
"FROM pg_catalog.pg_type t\n" "FROM pg_catalog.pg_type t\n"
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n" " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n"
"WHERE t.typtype = 'd'\n", "WHERE t.typtype = 'd'\n",
gettext_noop("Schema"),
gettext_noop("Name"),
gettext_noop("Type"),
gettext_noop("Modifier"),
gettext_noop("Check")); gettext_noop("Check"));
if (!showSystem && !pattern) if (!showSystem && !pattern)
......
...@@ -1779,44 +1779,44 @@ drop cascades to text search dictionary dict ...@@ -1779,44 +1779,44 @@ drop cascades to text search dictionary dict
CREATE TYPE test_type AS (a int); CREATE TYPE test_type AS (a int);
\d test_type \d test_type
Composite type "public.test_type" Composite type "public.test_type"
Column | Type Column | Type | Modifiers
--------+--------- --------+---------+-----------
a | integer a | integer |
ALTER TYPE nosuchtype ADD ATTRIBUTE b text; -- fails ALTER TYPE nosuchtype ADD ATTRIBUTE b text; -- fails
ERROR: relation "nosuchtype" does not exist ERROR: relation "nosuchtype" does not exist
ALTER TYPE test_type ADD ATTRIBUTE b text; ALTER TYPE test_type ADD ATTRIBUTE b text;
\d test_type \d test_type
Composite type "public.test_type" Composite type "public.test_type"
Column | Type Column | Type | Modifiers
--------+--------- --------+---------+-----------
a | integer a | integer |
b | text b | text |
ALTER TYPE test_type ADD ATTRIBUTE b text; -- fails ALTER TYPE test_type ADD ATTRIBUTE b text; -- fails
ERROR: column "b" of relation "test_type" already exists ERROR: column "b" of relation "test_type" already exists
ALTER TYPE test_type ALTER ATTRIBUTE b SET DATA TYPE varchar; ALTER TYPE test_type ALTER ATTRIBUTE b SET DATA TYPE varchar;
\d test_type \d test_type
Composite type "public.test_type" Composite type "public.test_type"
Column | Type Column | Type | Modifiers
--------+------------------- --------+-------------------+-----------
a | integer a | integer |
b | character varying b | character varying |
ALTER TYPE test_type ALTER ATTRIBUTE b SET DATA TYPE integer; ALTER TYPE test_type ALTER ATTRIBUTE b SET DATA TYPE integer;
\d test_type \d test_type
Composite type "public.test_type" Composite type "public.test_type"
Column | Type Column | Type | Modifiers
--------+--------- --------+---------+-----------
a | integer a | integer |
b | integer b | integer |
ALTER TYPE test_type DROP ATTRIBUTE b; ALTER TYPE test_type DROP ATTRIBUTE b;
\d test_type \d test_type
Composite type "public.test_type" Composite type "public.test_type"
Column | Type Column | Type | Modifiers
--------+--------- --------+---------+-----------
a | integer a | integer |
ALTER TYPE test_type DROP ATTRIBUTE c; -- fails ALTER TYPE test_type DROP ATTRIBUTE c; -- fails
ERROR: column "c" of relation "test_type" does not exist ERROR: column "c" of relation "test_type" does not exist
...@@ -1825,18 +1825,18 @@ NOTICE: column "c" of relation "test_type" does not exist, skipping ...@@ -1825,18 +1825,18 @@ NOTICE: column "c" of relation "test_type" does not exist, skipping
ALTER TYPE test_type DROP ATTRIBUTE a, ADD ATTRIBUTE d boolean; ALTER TYPE test_type DROP ATTRIBUTE a, ADD ATTRIBUTE d boolean;
\d test_type \d test_type
Composite type "public.test_type" Composite type "public.test_type"
Column | Type Column | Type | Modifiers
--------+--------- --------+---------+-----------
d | boolean d | boolean |
ALTER TYPE test_type RENAME ATTRIBUTE a TO aa; ALTER TYPE test_type RENAME ATTRIBUTE a TO aa;
ERROR: column "a" does not exist ERROR: column "a" does not exist
ALTER TYPE test_type RENAME ATTRIBUTE d TO dd; ALTER TYPE test_type RENAME ATTRIBUTE d TO dd;
\d test_type \d test_type
Composite type "public.test_type" Composite type "public.test_type"
Column | Type Column | Type | Modifiers
--------+--------- --------+---------+-----------
dd | boolean dd | boolean |
DROP TYPE test_type; DROP TYPE test_type;
CREATE TYPE test_type1 AS (a int, b text); CREATE TYPE test_type1 AS (a int, b text);
...@@ -1847,10 +1847,10 @@ CREATE TYPE test_type2 AS (a int, b text); ...@@ -1847,10 +1847,10 @@ CREATE TYPE test_type2 AS (a int, b text);
CREATE TABLE test_tbl2 OF test_type2; CREATE TABLE test_tbl2 OF test_type2;
\d test_type2 \d test_type2
Composite type "public.test_type2" Composite type "public.test_type2"
Column | Type Column | Type | Modifiers
--------+--------- --------+---------+-----------
a | integer a | integer |
b | text b | text |
\d test_tbl2 \d test_tbl2
Table "public.test_tbl2" Table "public.test_tbl2"
...@@ -1866,11 +1866,11 @@ HINT: Use ALTER ... CASCADE to alter the typed tables too. ...@@ -1866,11 +1866,11 @@ HINT: Use ALTER ... CASCADE to alter the typed tables too.
ALTER TYPE test_type2 ADD ATTRIBUTE c text CASCADE; ALTER TYPE test_type2 ADD ATTRIBUTE c text CASCADE;
\d test_type2 \d test_type2
Composite type "public.test_type2" Composite type "public.test_type2"
Column | Type Column | Type | Modifiers
--------+--------- --------+---------+-----------
a | integer a | integer |
b | text b | text |
c | text c | text |
\d test_tbl2 \d test_tbl2
Table "public.test_tbl2" Table "public.test_tbl2"
...@@ -1886,12 +1886,12 @@ ERROR: cannot alter type "test_type2" because it is the type of a typed table ...@@ -1886,12 +1886,12 @@ ERROR: cannot alter type "test_type2" because it is the type of a typed table
HINT: Use ALTER ... CASCADE to alter the typed tables too. HINT: Use ALTER ... CASCADE to alter the typed tables too.
ALTER TYPE test_type2 ALTER ATTRIBUTE b TYPE varchar CASCADE; ALTER TYPE test_type2 ALTER ATTRIBUTE b TYPE varchar CASCADE;
\d test_type2 \d test_type2
Composite type "public.test_type2" Composite type "public.test_type2"
Column | Type Column | Type | Modifiers
--------+------------------- --------+-------------------+-----------
a | integer a | integer |
b | character varying b | character varying |
c | text c | text |
\d test_tbl2 \d test_tbl2
Table "public.test_tbl2" Table "public.test_tbl2"
...@@ -1908,10 +1908,10 @@ HINT: Use ALTER ... CASCADE to alter the typed tables too. ...@@ -1908,10 +1908,10 @@ HINT: Use ALTER ... CASCADE to alter the typed tables too.
ALTER TYPE test_type2 DROP ATTRIBUTE b CASCADE; ALTER TYPE test_type2 DROP ATTRIBUTE b CASCADE;
\d test_type2 \d test_type2
Composite type "public.test_type2" Composite type "public.test_type2"
Column | Type Column | Type | Modifiers
--------+--------- --------+---------+-----------
a | integer a | integer |
c | text c | text |
\d test_tbl2 \d test_tbl2
Table "public.test_tbl2" Table "public.test_tbl2"
...@@ -1927,10 +1927,10 @@ HINT: Use ALTER ... CASCADE to alter the typed tables too. ...@@ -1927,10 +1927,10 @@ HINT: Use ALTER ... CASCADE to alter the typed tables too.
ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa CASCADE; ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa CASCADE;
\d test_type2 \d test_type2
Composite type "public.test_type2" Composite type "public.test_type2"
Column | Type Column | Type | Modifiers
--------+--------- --------+---------+-----------
aa | integer aa | integer |
c | text c | text |
\d test_tbl2 \d test_tbl2
Table "public.test_tbl2" Table "public.test_tbl2"
......
...@@ -1041,9 +1041,9 @@ Table "public.collate_dep_test1" ...@@ -1041,9 +1041,9 @@ Table "public.collate_dep_test1"
\d collate_dep_test2 \d collate_dep_test2
Composite type "public.collate_dep_test2" Composite type "public.collate_dep_test2"
Column | Type Column | Type | Modifiers
--------+--------- --------+---------+-----------
x | integer x | integer |
DROP TABLE collate_dep_test1, collate_dep_test4t; DROP TABLE collate_dep_test1, collate_dep_test4t;
DROP TYPE collate_dep_test2; DROP TYPE collate_dep_test2;
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