Skip to content

Commit acfa1f4

Browse files
committed
Fix pg_dump to handle collations applied to columns of composite types.
CREATE TYPE and ALTER TYPE ADD ATTRIBUTE handle this, so I suppose it's an intended feature, but pg_dump didn't know about it.
1 parent 49a642a commit acfa1f4

File tree

1 file changed

+57
-11
lines changed

1 file changed

+57
-11
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7944,24 +7944,50 @@ dumpCompositeType(Archive *fout, TypeInfo *tyinfo)
79447944
int ntups;
79457945
int i_attname;
79467946
int i_atttypdefn;
7947+
int i_attcollation;
79477948
int i_typrelid;
79487949
int i;
79497950

79507951
/* Set proper schema search path so type references list correctly */
79517952
selectSourceSchema(tyinfo->dobj.namespace->dobj.name);
79527953

79537954
/* Fetch type specific details */
7954-
/* We assume here that remoteVersion must be at least 70300 */
7955-
7956-
appendPQExpBuffer(query, "SELECT a.attname, "
7957-
"pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
7958-
"typrelid "
7959-
"FROM pg_catalog.pg_type t, pg_catalog.pg_attribute a "
7960-
"WHERE t.oid = '%u'::pg_catalog.oid "
7961-
"AND a.attrelid = t.typrelid "
7962-
"AND NOT a.attisdropped "
7963-
"ORDER BY a.attnum ",
7964-
tyinfo->dobj.catId.oid);
7955+
if (fout->remoteVersion >= 90100)
7956+
{
7957+
/*
7958+
* attcollation is new in 9.1. Since we only want to dump COLLATE
7959+
* clauses for attributes whose collation is different from their
7960+
* type's default, we use a CASE here to suppress uninteresting
7961+
* attcollations cheaply.
7962+
*/
7963+
appendPQExpBuffer(query, "SELECT a.attname, "
7964+
"pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
7965+
"CASE WHEN a.attcollation <> at.typcollation "
7966+
"THEN a.attcollation ELSE 0 END AS attcollation, "
7967+
"ct.typrelid "
7968+
"FROM pg_catalog.pg_type ct, pg_catalog.pg_attribute a, "
7969+
"pg_catalog.pg_type at "
7970+
"WHERE ct.oid = '%u'::pg_catalog.oid "
7971+
"AND a.attrelid = ct.typrelid "
7972+
"AND a.atttypid = at.oid "
7973+
"AND NOT a.attisdropped "
7974+
"ORDER BY a.attnum ",
7975+
tyinfo->dobj.catId.oid);
7976+
}
7977+
else
7978+
{
7979+
/* We assume here that remoteVersion must be at least 70300 */
7980+
appendPQExpBuffer(query, "SELECT a.attname, "
7981+
"pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
7982+
"0 AS attcollation, "
7983+
"ct.typrelid "
7984+
"FROM pg_catalog.pg_type ct, pg_catalog.pg_attribute a "
7985+
"WHERE ct.oid = '%u'::pg_catalog.oid "
7986+
"AND a.attrelid = ct.typrelid "
7987+
"AND NOT a.attisdropped "
7988+
"ORDER BY a.attnum ",
7989+
tyinfo->dobj.catId.oid);
7990+
}
79657991

79667992
res = PQexec(g_conn, query->data);
79677993
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
@@ -7970,6 +7996,7 @@ dumpCompositeType(Archive *fout, TypeInfo *tyinfo)
79707996

79717997
i_attname = PQfnumber(res, "attname");
79727998
i_atttypdefn = PQfnumber(res, "atttypdefn");
7999+
i_attcollation = PQfnumber(res, "attcollation");
79738000
i_typrelid = PQfnumber(res, "typrelid");
79748001

79758002
if (binary_upgrade)
@@ -7987,11 +8014,30 @@ dumpCompositeType(Archive *fout, TypeInfo *tyinfo)
79878014
{
79888015
char *attname;
79898016
char *atttypdefn;
8017+
Oid attcollation;
79908018

79918019
attname = PQgetvalue(res, i, i_attname);
79928020
atttypdefn = PQgetvalue(res, i, i_atttypdefn);
8021+
attcollation = atooid(PQgetvalue(res, i, i_attcollation));
79938022

79948023
appendPQExpBuffer(q, "\n\t%s %s", fmtId(attname), atttypdefn);
8024+
8025+
/* Add collation if not default for the column type */
8026+
if (OidIsValid(attcollation))
8027+
{
8028+
CollInfo *coll;
8029+
8030+
coll = findCollationByOid(attcollation);
8031+
if (coll)
8032+
{
8033+
/* always schema-qualify, don't try to be smart */
8034+
appendPQExpBuffer(q, " COLLATE %s.",
8035+
fmtId(coll->dobj.namespace->dobj.name));
8036+
appendPQExpBuffer(q, "%s",
8037+
fmtId(coll->dobj.name));
8038+
}
8039+
}
8040+
79958041
if (i < ntups - 1)
79968042
appendPQExpBuffer(q, ",");
79978043
}

0 commit comments

Comments
 (0)