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.
This commit is contained in:
Tom Lane 2011-04-17 18:09:22 -04:00
parent 2d4617126f
commit c29abc8b6f
3 changed files with 71 additions and 65 deletions

View File

@ -1287,11 +1287,12 @@ describeOneTableDetails(const char *schemaname,
"\n (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)"
"\n FROM pg_catalog.pg_attrdef d"
"\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)
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
appendPQExpBuffer(&buf, ",\n NULL AS attcollation");
appendPQExpBuffer(&buf, "\n NULL AS attcollation");
if (tableinfo.relkind == 'i')
appendPQExpBuffer(&buf, ",\n pg_catalog.pg_get_indexdef(a.attrelid, a.attnum, TRUE) AS indexdef");
if (verbose)
@ -1362,7 +1363,7 @@ describeOneTableDetails(const char *schemaname,
cols = 2;
if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
tableinfo.relkind == 'f')
tableinfo.relkind == 'f' || tableinfo.relkind == 'c')
{
show_modifiers = true;
headers[cols++] = gettext_noop("Modifiers");
@ -2697,22 +2698,27 @@ listDomains(const char *pattern, bool showSystem)
printfPQExpBuffer(&buf,
"SELECT n.nspname as \"%s\",\n"
" t.typname 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"
" WHEN t.typnotnull AND t.typdefault IS NULL THEN 'not null'\n"
" WHEN NOT t.typnotnull AND t.typdefault IS NOT NULL THEN 'default '||t.typdefault\n"
" ELSE ''\n"
" END as \"%s\",\n"
" pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n"
" TRIM(LEADING\n",
gettext_noop("Schema"),
gettext_noop("Name"),
gettext_noop("Type"));
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"
" SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n"
" ), ' ') as \"%s\"\n"
"FROM pg_catalog.pg_type t\n"
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n"
"WHERE t.typtype = 'd'\n",
gettext_noop("Schema"),
gettext_noop("Name"),
gettext_noop("Type"),
gettext_noop("Modifier"),
gettext_noop("Check"));
if (!showSystem && !pattern)

View File

@ -1779,44 +1779,44 @@ drop cascades to text search dictionary dict
CREATE TYPE test_type AS (a int);
\d test_type
Composite type "public.test_type"
Column | Type
--------+---------
a | integer
Column | Type | Modifiers
--------+---------+-----------
a | integer |
ALTER TYPE nosuchtype ADD ATTRIBUTE b text; -- fails
ERROR: relation "nosuchtype" does not exist
ALTER TYPE test_type ADD ATTRIBUTE b text;
\d test_type
Composite type "public.test_type"
Column | Type
--------+---------
a | integer
b | text
Column | Type | Modifiers
--------+---------+-----------
a | integer |
b | text |
ALTER TYPE test_type ADD ATTRIBUTE b text; -- fails
ERROR: column "b" of relation "test_type" already exists
ALTER TYPE test_type ALTER ATTRIBUTE b SET DATA TYPE varchar;
\d test_type
Composite type "public.test_type"
Column | Type
--------+-------------------
a | integer
b | character varying
Composite type "public.test_type"
Column | Type | Modifiers
--------+-------------------+-----------
a | integer |
b | character varying |
ALTER TYPE test_type ALTER ATTRIBUTE b SET DATA TYPE integer;
\d test_type
Composite type "public.test_type"
Column | Type
--------+---------
a | integer
b | integer
Column | Type | Modifiers
--------+---------+-----------
a | integer |
b | integer |
ALTER TYPE test_type DROP ATTRIBUTE b;
\d test_type
Composite type "public.test_type"
Column | Type
--------+---------
a | integer
Column | Type | Modifiers
--------+---------+-----------
a | integer |
ALTER TYPE test_type DROP ATTRIBUTE c; -- fails
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
ALTER TYPE test_type DROP ATTRIBUTE a, ADD ATTRIBUTE d boolean;
\d test_type
Composite type "public.test_type"
Column | Type
--------+---------
d | boolean
Column | Type | Modifiers
--------+---------+-----------
d | boolean |
ALTER TYPE test_type RENAME ATTRIBUTE a TO aa;
ERROR: column "a" does not exist
ALTER TYPE test_type RENAME ATTRIBUTE d TO dd;
\d test_type
Composite type "public.test_type"
Column | Type
--------+---------
dd | boolean
Column | Type | Modifiers
--------+---------+-----------
dd | boolean |
DROP TYPE test_type;
CREATE TYPE test_type1 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;
\d test_type2
Composite type "public.test_type2"
Column | Type
--------+---------
a | integer
b | text
Column | Type | Modifiers
--------+---------+-----------
a | integer |
b | text |
\d test_tbl2
Table "public.test_tbl2"
@ -1866,11 +1866,11 @@ HINT: Use ALTER ... CASCADE to alter the typed tables too.
ALTER TYPE test_type2 ADD ATTRIBUTE c text CASCADE;
\d test_type2
Composite type "public.test_type2"
Column | Type
--------+---------
a | integer
b | text
c | text
Column | Type | Modifiers
--------+---------+-----------
a | integer |
b | text |
c | text |
\d 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
HINT: Use ALTER ... CASCADE to alter the typed tables too.
ALTER TYPE test_type2 ALTER ATTRIBUTE b TYPE varchar CASCADE;
\d test_type2
Composite type "public.test_type2"
Column | Type
--------+-------------------
a | integer
b | character varying
c | text
Composite type "public.test_type2"
Column | Type | Modifiers
--------+-------------------+-----------
a | integer |
b | character varying |
c | text |
\d test_tbl2
Table "public.test_tbl2"
@ -1908,10 +1908,10 @@ HINT: Use ALTER ... CASCADE to alter the typed tables too.
ALTER TYPE test_type2 DROP ATTRIBUTE b CASCADE;
\d test_type2
Composite type "public.test_type2"
Column | Type
--------+---------
a | integer
c | text
Column | Type | Modifiers
--------+---------+-----------
a | integer |
c | text |
\d test_tbl2
Table "public.test_tbl2"
@ -1927,10 +1927,10 @@ HINT: Use ALTER ... CASCADE to alter the typed tables too.
ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa CASCADE;
\d test_type2
Composite type "public.test_type2"
Column | Type
--------+---------
aa | integer
c | text
Column | Type | Modifiers
--------+---------+-----------
aa | integer |
c | text |
\d test_tbl2
Table "public.test_tbl2"

View File

@ -1041,9 +1041,9 @@ Table "public.collate_dep_test1"
\d collate_dep_test2
Composite type "public.collate_dep_test2"
Column | Type
--------+---------
x | integer
Column | Type | Modifiers
--------+---------+-----------
x | integer |
DROP TABLE collate_dep_test1, collate_dep_test4t;
DROP TYPE collate_dep_test2;