Skip to content

Commit 1766a5b

Browse files
committed
Tweak collation setup for GIN index comparison functions.
Honor index column's collation spec if there is one, don't go to the expense of calling get_typcollation when we can reasonably assume that all GIN storage types will use default collation, and be sure to set a collation for the comparePartialFn too.
1 parent c5ff3ff commit 1766a5b

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/backend/access/gin/ginutil.c

+23-3
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616

1717
#include "access/gin_private.h"
1818
#include "access/reloptions.h"
19+
#include "catalog/pg_collation.h"
1920
#include "catalog/pg_type.h"
2021
#include "miscadmin.h"
2122
#include "storage/bufmgr.h"
2223
#include "storage/freespace.h"
2324
#include "storage/indexfsm.h"
2425
#include "storage/lmgr.h"
25-
#include "utils/lsyscache.h"
2626

2727

2828
/*
@@ -63,8 +63,23 @@ initGinState(GinState *state, Relation index)
6363
fmgr_info_copy(&(state->compareFn[i]),
6464
index_getprocinfo(index, i + 1, GIN_COMPARE_PROC),
6565
CurrentMemoryContext);
66-
fmgr_info_set_collation(get_typcollation(index->rd_att->attrs[i]->atttypid),
67-
&(state->compareFn[i]));
66+
67+
/*
68+
* If the index column has a specified collation, index_getprocinfo
69+
* will have installed it into the fmgr info, and we should honor it.
70+
* However, we may have a collatable storage type for a noncollatable
71+
* indexed data type (for instance, hstore uses text index entries).
72+
* If there's no index collation then specify default collation in
73+
* case the comparison function needs one. This is harmless if the
74+
* comparison function doesn't care about collation, so we just do it
75+
* unconditionally. (We could alternatively call get_typcollation,
76+
* but that seems like expensive overkill --- there aren't going to be
77+
* any cases where a GIN storage type has a nondefault collation.)
78+
*/
79+
if (!OidIsValid(state->compareFn[i].fn_collation))
80+
fmgr_info_set_collation(DEFAULT_COLLATION_OID,
81+
&(state->compareFn[i]));
82+
6883
fmgr_info_copy(&(state->extractValueFn[i]),
6984
index_getprocinfo(index, i + 1, GIN_EXTRACTVALUE_PROC),
7085
CurrentMemoryContext);
@@ -84,6 +99,11 @@ initGinState(GinState *state, Relation index)
8499
index_getprocinfo(index, i + 1, GIN_COMPARE_PARTIAL_PROC),
85100
CurrentMemoryContext);
86101

102+
/* As above, install collation spec in case compare fn needs it */
103+
if (!OidIsValid(state->comparePartialFn[i].fn_collation))
104+
fmgr_info_set_collation(DEFAULT_COLLATION_OID,
105+
&(state->comparePartialFn[i]));
106+
87107
state->canPartialMatch[i] = true;
88108
}
89109
else

0 commit comments

Comments
 (0)