Skip to content

Commit c29abc8

Browse files
committed
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.
1 parent 2d46171 commit c29abc8

File tree

3 files changed

+71
-65
lines changed

3 files changed

+71
-65
lines changed

src/bin/psql/describe.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,11 +1287,12 @@ describeOneTableDetails(const char *schemaname,
12871287
"\n (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)"
12881288
"\n FROM pg_catalog.pg_attrdef d"
12891289
"\n WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),"
1290-
"\n a.attnotnull, a.attnum");
1290+
"\n a.attnotnull, a.attnum,");
12911291
if (pset.sversion >= 90100)
1292-
appendPQExpBuffer(&buf, ",\n (SELECT collname FROM pg_collation WHERE oid = a.attcollation AND collname <> 'default') AS attcollation");
1292+
appendPQExpBuffer(&buf, "\n (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t\n"
1293+
" WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) AS attcollation");
12931294
else
1294-
appendPQExpBuffer(&buf, ",\n NULL AS attcollation");
1295+
appendPQExpBuffer(&buf, "\n NULL AS attcollation");
12951296
if (tableinfo.relkind == 'i')
12961297
appendPQExpBuffer(&buf, ",\n pg_catalog.pg_get_indexdef(a.attrelid, a.attnum, TRUE) AS indexdef");
12971298
if (verbose)
@@ -1362,7 +1363,7 @@ describeOneTableDetails(const char *schemaname,
13621363
cols = 2;
13631364

13641365
if (tableinfo.relkind == 'r' || tableinfo.relkind == 'v' ||
1365-
tableinfo.relkind == 'f')
1366+
tableinfo.relkind == 'f' || tableinfo.relkind == 'c')
13661367
{
13671368
show_modifiers = true;
13681369
headers[cols++] = gettext_noop("Modifiers");
@@ -2697,22 +2698,27 @@ listDomains(const char *pattern, bool showSystem)
26972698
printfPQExpBuffer(&buf,
26982699
"SELECT n.nspname as \"%s\",\n"
26992700
" t.typname as \"%s\",\n"
2700-
" pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n"
2701-
" CASE WHEN t.typnotnull AND t.typdefault IS NOT NULL THEN 'not null default '||t.typdefault\n"
2702-
" WHEN t.typnotnull AND t.typdefault IS NULL THEN 'not null'\n"
2703-
" WHEN NOT t.typnotnull AND t.typdefault IS NOT NULL THEN 'default '||t.typdefault\n"
2704-
" ELSE ''\n"
2705-
" END as \"%s\",\n"
2701+
" pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n"
2702+
" TRIM(LEADING\n",
2703+
gettext_noop("Schema"),
2704+
gettext_noop("Name"),
2705+
gettext_noop("Type"));
2706+
if (pset.sversion >= 90100)
2707+
appendPQExpBuffer(&buf,
2708+
" COALESCE((SELECT ' collate ' || c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type bt\n"
2709+
" WHERE c.oid = t.typcollation AND bt.oid = t.typbasetype AND t.typcollation <> bt.typcollation), '') ||\n");
2710+
appendPQExpBuffer(&buf,
2711+
" CASE WHEN t.typnotnull THEN ' not null' ELSE '' END ||\n"
2712+
" CASE WHEN t.typdefault IS NOT NULL THEN ' default ' || t.typdefault ELSE '' END\n"
2713+
" ) as \"%s\",\n",
2714+
gettext_noop("Modifier"));
2715+
appendPQExpBuffer(&buf,
27062716
" pg_catalog.array_to_string(ARRAY(\n"
27072717
" SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n"
27082718
" ), ' ') as \"%s\"\n"
27092719
"FROM pg_catalog.pg_type t\n"
27102720
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n"
27112721
"WHERE t.typtype = 'd'\n",
2712-
gettext_noop("Schema"),
2713-
gettext_noop("Name"),
2714-
gettext_noop("Type"),
2715-
gettext_noop("Modifier"),
27162722
gettext_noop("Check"));
27172723

27182724
if (!showSystem && !pattern)

src/test/regress/expected/alter_table.out

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,44 +1779,44 @@ drop cascades to text search dictionary dict
17791779
CREATE TYPE test_type AS (a int);
17801780
\d test_type
17811781
Composite type "public.test_type"
1782-
Column | Type
1783-
--------+---------
1784-
a | integer
1782+
Column | Type | Modifiers
1783+
--------+---------+-----------
1784+
a | integer |
17851785

17861786
ALTER TYPE nosuchtype ADD ATTRIBUTE b text; -- fails
17871787
ERROR: relation "nosuchtype" does not exist
17881788
ALTER TYPE test_type ADD ATTRIBUTE b text;
17891789
\d test_type
17901790
Composite type "public.test_type"
1791-
Column | Type
1792-
--------+---------
1793-
a | integer
1794-
b | text
1791+
Column | Type | Modifiers
1792+
--------+---------+-----------
1793+
a | integer |
1794+
b | text |
17951795

17961796
ALTER TYPE test_type ADD ATTRIBUTE b text; -- fails
17971797
ERROR: column "b" of relation "test_type" already exists
17981798
ALTER TYPE test_type ALTER ATTRIBUTE b SET DATA TYPE varchar;
17991799
\d test_type
1800-
Composite type "public.test_type"
1801-
Column | Type
1802-
--------+-------------------
1803-
a | integer
1804-
b | character varying
1800+
Composite type "public.test_type"
1801+
Column | Type | Modifiers
1802+
--------+-------------------+-----------
1803+
a | integer |
1804+
b | character varying |
18051805

18061806
ALTER TYPE test_type ALTER ATTRIBUTE b SET DATA TYPE integer;
18071807
\d test_type
18081808
Composite type "public.test_type"
1809-
Column | Type
1810-
--------+---------
1811-
a | integer
1812-
b | integer
1809+
Column | Type | Modifiers
1810+
--------+---------+-----------
1811+
a | integer |
1812+
b | integer |
18131813

18141814
ALTER TYPE test_type DROP ATTRIBUTE b;
18151815
\d test_type
18161816
Composite type "public.test_type"
1817-
Column | Type
1818-
--------+---------
1819-
a | integer
1817+
Column | Type | Modifiers
1818+
--------+---------+-----------
1819+
a | integer |
18201820

18211821
ALTER TYPE test_type DROP ATTRIBUTE c; -- fails
18221822
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
18251825
ALTER TYPE test_type DROP ATTRIBUTE a, ADD ATTRIBUTE d boolean;
18261826
\d test_type
18271827
Composite type "public.test_type"
1828-
Column | Type
1829-
--------+---------
1830-
d | boolean
1828+
Column | Type | Modifiers
1829+
--------+---------+-----------
1830+
d | boolean |
18311831

18321832
ALTER TYPE test_type RENAME ATTRIBUTE a TO aa;
18331833
ERROR: column "a" does not exist
18341834
ALTER TYPE test_type RENAME ATTRIBUTE d TO dd;
18351835
\d test_type
18361836
Composite type "public.test_type"
1837-
Column | Type
1838-
--------+---------
1839-
dd | boolean
1837+
Column | Type | Modifiers
1838+
--------+---------+-----------
1839+
dd | boolean |
18401840

18411841
DROP TYPE test_type;
18421842
CREATE TYPE test_type1 AS (a int, b text);
@@ -1847,10 +1847,10 @@ CREATE TYPE test_type2 AS (a int, b text);
18471847
CREATE TABLE test_tbl2 OF test_type2;
18481848
\d test_type2
18491849
Composite type "public.test_type2"
1850-
Column | Type
1851-
--------+---------
1852-
a | integer
1853-
b | text
1850+
Column | Type | Modifiers
1851+
--------+---------+-----------
1852+
a | integer |
1853+
b | text |
18541854

18551855
\d test_tbl2
18561856
Table "public.test_tbl2"
@@ -1866,11 +1866,11 @@ HINT: Use ALTER ... CASCADE to alter the typed tables too.
18661866
ALTER TYPE test_type2 ADD ATTRIBUTE c text CASCADE;
18671867
\d test_type2
18681868
Composite type "public.test_type2"
1869-
Column | Type
1870-
--------+---------
1871-
a | integer
1872-
b | text
1873-
c | text
1869+
Column | Type | Modifiers
1870+
--------+---------+-----------
1871+
a | integer |
1872+
b | text |
1873+
c | text |
18741874

18751875
\d test_tbl2
18761876
Table "public.test_tbl2"
@@ -1886,12 +1886,12 @@ ERROR: cannot alter type "test_type2" because it is the type of a typed table
18861886
HINT: Use ALTER ... CASCADE to alter the typed tables too.
18871887
ALTER TYPE test_type2 ALTER ATTRIBUTE b TYPE varchar CASCADE;
18881888
\d test_type2
1889-
Composite type "public.test_type2"
1890-
Column | Type
1891-
--------+-------------------
1892-
a | integer
1893-
b | character varying
1894-
c | text
1889+
Composite type "public.test_type2"
1890+
Column | Type | Modifiers
1891+
--------+-------------------+-----------
1892+
a | integer |
1893+
b | character varying |
1894+
c | text |
18951895

18961896
\d test_tbl2
18971897
Table "public.test_tbl2"
@@ -1908,10 +1908,10 @@ HINT: Use ALTER ... CASCADE to alter the typed tables too.
19081908
ALTER TYPE test_type2 DROP ATTRIBUTE b CASCADE;
19091909
\d test_type2
19101910
Composite type "public.test_type2"
1911-
Column | Type
1912-
--------+---------
1913-
a | integer
1914-
c | text
1911+
Column | Type | Modifiers
1912+
--------+---------+-----------
1913+
a | integer |
1914+
c | text |
19151915

19161916
\d test_tbl2
19171917
Table "public.test_tbl2"
@@ -1927,10 +1927,10 @@ HINT: Use ALTER ... CASCADE to alter the typed tables too.
19271927
ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa CASCADE;
19281928
\d test_type2
19291929
Composite type "public.test_type2"
1930-
Column | Type
1931-
--------+---------
1932-
aa | integer
1933-
c | text
1930+
Column | Type | Modifiers
1931+
--------+---------+-----------
1932+
aa | integer |
1933+
c | text |
19341934

19351935
\d test_tbl2
19361936
Table "public.test_tbl2"

src/test/regress/expected/collate.linux.utf8.out

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,9 +1041,9 @@ Table "public.collate_dep_test1"
10411041

10421042
\d collate_dep_test2
10431043
Composite type "public.collate_dep_test2"
1044-
Column | Type
1045-
--------+---------
1046-
x | integer
1044+
Column | Type | Modifiers
1045+
--------+---------+-----------
1046+
x | integer |
10471047

10481048
DROP TABLE collate_dep_test1, collate_dep_test4t;
10491049
DROP TYPE collate_dep_test2;

0 commit comments

Comments
 (0)