From c29abc8b6f5334ac2f7046a33b233776ead12395 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sun, 17 Apr 2011 18:09:22 -0400 Subject: [PATCH] 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. --- src/bin/psql/describe.c | 34 +++++---- src/test/regress/expected/alter_table.out | 96 ++++++++++++------------ src/test/regress/expected/collate.linux.utf8.out | 6 +- 3 files changed, 71 insertions(+), 65 deletions(-) diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index bab67174a2..e01fb7bdeb 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -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) diff --git a/src/test/regress/expected/alter_table.out b/src/test/regress/expected/alter_table.out index 315b915c13..5b1223be6a 100644 --- a/src/test/regress/expected/alter_table.out +++ b/src/test/regress/expected/alter_table.out @@ -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" diff --git a/src/test/regress/expected/collate.linux.utf8.out b/src/test/regress/expected/collate.linux.utf8.out index f225b48730..9813b6847c 100644 --- a/src/test/regress/expected/collate.linux.utf8.out +++ b/src/test/regress/expected/collate.linux.utf8.out @@ -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; -- 2.11.0