Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 478cacb

Browse files
committed
Ensure consistent name matching behavior in processSQLNamePattern().
Prior to v12, if you used a collation-sensitive regex feature in a pattern handled by processSQLNamePattern() (for instance, \d '\\w+' in psql), the behavior you got matched the database's default collation. Since commit 586b98f you'd usually get C-collation behavior, because the catalog "name"-type columns are now marked as COLLATE "C". Add explicit COLLATE specifications to restore the prior behavior. (Note for whoever writes the v12 release notes: the need for this shows that while 586b98f preserved pre-v12 behavior of "name" columns for simple comparison operators, it changed the behavior of regex operators on those columns. Although this patch fixes it for pattern matches generated by our own tools, user-written queries will still be affected. So we'd better mention this issue as a compatibility item.) Daniel Vérité Discussion: https://postgr.es/m/701e51f0-0ec0-4e70-a365-1958d66dd8d2@manitou-mail.org
1 parent 86cc06d commit 478cacb

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/fe_utils/string_utils.c

+14
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,12 @@ processSQLNamePattern(PGconn *conn, PQExpBuffer buf, const char *pattern,
956956
* Now decide what we need to emit. We may run under a hostile
957957
* search_path, so qualify EVERY name. Note there will be a leading "^("
958958
* in the patterns in any case.
959+
*
960+
* We want the regex matches to use the database's default collation where
961+
* collation-sensitive behavior is required (for example, which characters
962+
* match '\w'). That happened by default before PG v12, but if the server
963+
* is >= v12 then we need to force it through explicit COLLATE clauses,
964+
* otherwise the "C" collation attached to "name" catalog columns wins.
959965
*/
960966
if (namebuf.len > 2)
961967
{
@@ -971,16 +977,22 @@ processSQLNamePattern(PGconn *conn, PQExpBuffer buf, const char *pattern,
971977
appendPQExpBuffer(buf,
972978
"(%s OPERATOR(pg_catalog.~) ", namevar);
973979
appendStringLiteralConn(buf, namebuf.data, conn);
980+
if (PQserverVersion(conn) >= 120000)
981+
appendPQExpBufferStr(buf, " COLLATE pg_catalog.default");
974982
appendPQExpBuffer(buf,
975983
"\n OR %s OPERATOR(pg_catalog.~) ",
976984
altnamevar);
977985
appendStringLiteralConn(buf, namebuf.data, conn);
986+
if (PQserverVersion(conn) >= 120000)
987+
appendPQExpBufferStr(buf, " COLLATE pg_catalog.default");
978988
appendPQExpBufferStr(buf, ")\n");
979989
}
980990
else
981991
{
982992
appendPQExpBuffer(buf, "%s OPERATOR(pg_catalog.~) ", namevar);
983993
appendStringLiteralConn(buf, namebuf.data, conn);
994+
if (PQserverVersion(conn) >= 120000)
995+
appendPQExpBufferStr(buf, " COLLATE pg_catalog.default");
984996
appendPQExpBufferChar(buf, '\n');
985997
}
986998
}
@@ -997,6 +1009,8 @@ processSQLNamePattern(PGconn *conn, PQExpBuffer buf, const char *pattern,
9971009
WHEREAND();
9981010
appendPQExpBuffer(buf, "%s OPERATOR(pg_catalog.~) ", schemavar);
9991011
appendStringLiteralConn(buf, schemabuf.data, conn);
1012+
if (PQserverVersion(conn) >= 120000)
1013+
appendPQExpBufferStr(buf, " COLLATE pg_catalog.default");
10001014
appendPQExpBufferChar(buf, '\n');
10011015
}
10021016
}

0 commit comments

Comments
 (0)