From b47b4dbf683f13e6ef09fa0d93aa6e84f3d00819 Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Wed, 3 Feb 2016 14:17:35 -0500 Subject: Extend sortsupport for text to more opclasses. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Have varlena.c expose an interface that allows the char(n), bytea, and bpchar types to piggyback on a now-generalized SortSupport for text. This pushes a little more knowledge of the bpchar/char(n) type into varlena.c than might be preferred, but that seems like the approach that creates least friction. Also speed things up for index builds that use text_pattern_ops or varchar_pattern_ops. This patch does quite a bit of renaming, but it seems likely to be worth it, so as to avoid future confusion about the fact that this code is now more generally used than the old names might have suggested. Peter Geoghegan, reviewed by Álvaro Herrera and Andreas Karlsson, with small tweaks by me. --- src/backend/utils/adt/varchar.c | 55 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 6 deletions(-) (limited to 'src/backend/utils/adt/varchar.c') diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c index 0498fef4049..94d6da5eb57 100644 --- a/src/backend/utils/adt/varchar.c +++ b/src/backend/utils/adt/varchar.c @@ -17,6 +17,7 @@ #include "access/hash.h" #include "access/tuptoaster.h" +#include "catalog/pg_collation.h" #include "libpq/pqformat.h" #include "nodes/nodeFuncs.h" #include "utils/array.h" @@ -649,14 +650,21 @@ varchartypmodout(PG_FUNCTION_ARGS) *****************************************************************************/ /* "True" length (not counting trailing blanks) of a BpChar */ -static int +static inline int bcTruelen(BpChar *arg) { - char *s = VARDATA_ANY(arg); + return bpchartruelen(VARDATA_ANY(arg), VARSIZE_ANY_EXHDR(arg)); +} + +int +bpchartruelen(char *s, int len) +{ int i; - int len; - len = VARSIZE_ANY_EXHDR(arg); + /* + * Note that we rely on the assumption that ' ' is a singleton unit on + * every supported multibyte server encoding. + */ for (i = len - 1; i >= 0; i--) { if (s[i] != ' ') @@ -858,6 +866,23 @@ bpcharcmp(PG_FUNCTION_ARGS) PG_RETURN_INT32(cmp); } +Datum +bpchar_sortsupport(PG_FUNCTION_ARGS) +{ + SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0); + Oid collid = ssup->ssup_collation; + MemoryContext oldcontext; + + oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt); + + /* Use generic string SortSupport */ + varstr_sortsupport(ssup, collid, true); + + MemoryContextSwitchTo(oldcontext); + + PG_RETURN_VOID(); +} + Datum bpchar_larger(PG_FUNCTION_ARGS) { @@ -926,8 +951,9 @@ hashbpchar(PG_FUNCTION_ARGS) /* * The following operators support character-by-character comparison * of bpchar datums, to allow building indexes suitable for LIKE clauses. - * Note that the regular bpchareq/bpcharne comparison operators are assumed - * to be compatible with these! + * Note that the regular bpchareq/bpcharne comparison operators, and + * regular support functions 1 and 2 with "C" collation are assumed to be + * compatible with these! */ static int @@ -1030,3 +1056,20 @@ btbpchar_pattern_cmp(PG_FUNCTION_ARGS) PG_RETURN_INT32(result); } + + +Datum +btbpchar_pattern_sortsupport(PG_FUNCTION_ARGS) +{ + SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0); + MemoryContext oldcontext; + + oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt); + + /* Use generic string SortSupport, forcing "C" collation */ + varstr_sortsupport(ssup, C_COLLATION_OID, true); + + MemoryContextSwitchTo(oldcontext); + + PG_RETURN_VOID(); +} -- cgit v1.2.3