|
91 | 91 | * Portions Copyright (c) 1994, Regents of the University of California
|
92 | 92 | *
|
93 | 93 | * IDENTIFICATION
|
94 |
| - * $PostgreSQL: pgsql/src/backend/utils/sort/tuplesort.c,v 1.82 2008/03/16 23:15:08 tgl Exp $ |
| 94 | + * $PostgreSQL: pgsql/src/backend/utils/sort/tuplesort.c,v 1.83 2008/03/17 03:45:36 tgl Exp $ |
95 | 95 | *
|
96 | 96 | *-------------------------------------------------------------------------
|
97 | 97 | */
|
|
100 | 100 |
|
101 | 101 | #include <limits.h>
|
102 | 102 |
|
| 103 | +#include "access/genam.h" |
103 | 104 | #include "access/hash.h"
|
104 | 105 | #include "access/heapam.h"
|
105 | 106 | #include "access/nbtree.h"
|
@@ -346,6 +347,7 @@ struct Tuplesortstate
|
346 | 347 | bool enforceUnique; /* complain if we find duplicate tuples */
|
347 | 348 |
|
348 | 349 | /* These are specific to the index_hash subcase: */
|
| 350 | + FmgrInfo *hash_proc; /* call info for the hash function */ |
349 | 351 | uint32 hash_mask; /* mask for sortable part of hash code */
|
350 | 352 |
|
351 | 353 | /*
|
@@ -676,6 +678,14 @@ tuplesort_begin_index_hash(Relation indexRel,
|
676 | 678 | state->reversedirection = reversedirection_index_hash;
|
677 | 679 |
|
678 | 680 | state->indexRel = indexRel;
|
| 681 | + |
| 682 | + /* |
| 683 | + * We look up the index column's hash function just once, to avoid |
| 684 | + * chewing lots of cycles in repeated index_getprocinfo calls. This |
| 685 | + * assumes that our caller holds the index relation open throughout the |
| 686 | + * sort, else the pointer obtained here might cease to be valid. |
| 687 | + */ |
| 688 | + state->hash_proc = index_getprocinfo(indexRel, 1, HASHPROC); |
679 | 689 | state->hash_mask = hash_mask;
|
680 | 690 |
|
681 | 691 | MemoryContextSwitchTo(oldcontext);
|
@@ -2809,10 +2819,11 @@ comparetup_index_hash(const SortTuple *a, const SortTuple *b,
|
2809 | 2819 |
|
2810 | 2820 | /* Compute hash codes and mask off bits we don't want to sort by */
|
2811 | 2821 | Assert(!a->isnull1);
|
| 2822 | + hash1 = DatumGetUInt32(FunctionCall1(state->hash_proc, a->datum1)) |
| 2823 | + & state->hash_mask; |
2812 | 2824 | Assert(!b->isnull1);
|
2813 |
| - |
2814 |
| - hash1 = _hash_datum2hashkey(state->indexRel, a->datum1) & state->hash_mask; |
2815 |
| - hash2 = _hash_datum2hashkey(state->indexRel, b->datum1) & state->hash_mask; |
| 2825 | + hash2 = DatumGetUInt32(FunctionCall1(state->hash_proc, b->datum1)) |
| 2826 | + & state->hash_mask; |
2816 | 2827 |
|
2817 | 2828 | if (hash1 > hash2)
|
2818 | 2829 | return 1;
|
|
0 commit comments