64
64
65
65
<table id="spgist-builtin-opclasses-table">
66
66
<title>Built-in <acronym>SP-GiST</acronym> Operator Classes</title>
67
- <tgroup cols="3 ">
67
+ <tgroup cols="4 ">
68
68
<thead>
69
69
<row>
70
70
<entry>Name</entry>
71
71
<entry>Indexed Data Type</entry>
72
72
<entry>Indexable Operators</entry>
73
+ <entry>Ordering Operators</entry>
73
74
</row>
74
75
</thead>
75
76
<tbody>
84
85
<literal>>^</literal>
85
86
<literal>~=</literal>
86
87
</entry>
88
+ <entry>
89
+ <literal><-></literal>
90
+ </entry>
87
91
</row>
88
92
<row>
89
93
<entry><literal>quad_point_ops</literal></entry>
96
100
<literal>>^</literal>
97
101
<literal>~=</literal>
98
102
</entry>
103
+ <entry>
104
+ <literal><-></literal>
105
+ </entry>
99
106
</row>
100
107
<row>
101
108
<entry><literal>range_ops</literal></entry>
111
118
<literal>>></literal>
112
119
<literal>@></literal>
113
120
</entry>
121
+ <entry>
122
+ </entry>
114
123
</row>
115
124
<row>
116
125
<entry><literal>box_ops</literal></entry>
129
138
<literal>|>></literal>
130
139
<literal>|&></literal>
131
140
</entry>
141
+ <entry>
142
+ </entry>
132
143
</row>
133
144
<row>
134
145
<entry><literal>poly_ops</literal></entry>
147
158
<literal>|>></literal>
148
159
<literal>|&></literal>
149
160
</entry>
161
+ <entry>
162
+ <literal><-></literal>
163
+ </entry>
150
164
</row>
151
165
<row>
152
166
<entry><literal>text_ops</literal></entry>
163
177
<literal>~>~</literal>
164
178
<literal>^@</literal>
165
179
</entry>
180
+ <entry>
181
+ </entry>
166
182
</row>
167
183
<row>
168
184
<entry><literal>inet_ops</literal></entry>
180
196
<literal><=</literal>
181
197
<literal>=</literal>
182
198
</entry>
199
+ <entry>
200
+ </entry>
183
201
</row>
184
202
</tbody>
185
203
</tgroup>
191
209
supports the same operators but uses a different index data structure which
192
210
may offer better performance in some applications.
193
211
</para>
212
+ <para>
213
+ The <literal>quad_point_ops</literal>, <literal>kd_point_ops</literal> and
214
+ <literal>poly_ops</literal> operator classes support the <literal><-></literal>
215
+ ordering operator, which enables the k-nearest neighbor (<literal>k-NN</literal>)
216
+ search over indexed point or polygon datasets.
217
+ </para>
194
218
195
219
</sect1>
196
220
@@ -630,7 +654,10 @@ CREATE FUNCTION my_inner_consistent(internal, internal) RETURNS void ...
630
654
typedef struct spgInnerConsistentIn
631
655
{
632
656
ScanKey scankeys; /* array of operators and comparison values */
633
- int nkeys; /* length of array */
657
+ ScanKey orderbys; /* array of ordering operators and comparison
658
+ * values */
659
+ int nkeys; /* length of scankeys array */
660
+ int norderbys; /* length of orderbys array */
634
661
635
662
Datum reconstructedValue; /* value reconstructed at parent */
636
663
void *traversalValue; /* opclass-specific traverse value */
@@ -653,6 +680,7 @@ typedef struct spgInnerConsistentOut
653
680
int *levelAdds; /* increment level by this much for each */
654
681
Datum *reconstructedValues; /* associated reconstructed values */
655
682
void **traversalValues; /* opclass-specific traverse values */
683
+ double **distances; /* associated distances */
656
684
} spgInnerConsistentOut;
657
685
</programlisting>
658
686
@@ -667,6 +695,8 @@ typedef struct spgInnerConsistentOut
667
695
In particular it is not necessary to check <structfield>sk_flags</structfield> to
668
696
see if the comparison value is NULL, because the SP-GiST core code
669
697
will filter out such conditions.
698
+ The array <structfield>orderbys</structfield>, of length <structfield>norderbys</structfield>,
699
+ describes ordering operators (if any) in the same manner.
670
700
<structfield>reconstructedValue</structfield> is the value reconstructed for the
671
701
parent tuple; it is <literal>(Datum) 0</literal> at the root level or if the
672
702
<function>inner_consistent</function> function did not provide a value at the
@@ -709,6 +739,10 @@ typedef struct spgInnerConsistentOut
709
739
of <structname>spgConfigOut</structname>.<structfield>leafType</structfield> type
710
740
reconstructed for each child node to be visited; otherwise, leave
711
741
<structfield>reconstructedValues</structfield> as NULL.
742
+ If ordered search is performed, set <structfield>distances</structfield>
743
+ to an array of distance values according to <structfield>orderbys</structfield>
744
+ array (nodes with lowest distances will be processed first). Leave it
745
+ NULL otherwise.
712
746
If it is desired to pass down additional out-of-band information
713
747
(<quote>traverse values</quote>) to lower levels of the tree search,
714
748
set <structfield>traversalValues</structfield> to an array of the appropriate
@@ -717,6 +751,7 @@ typedef struct spgInnerConsistentOut
717
751
Note that the <function>inner_consistent</function> function is
718
752
responsible for palloc'ing the
719
753
<structfield>nodeNumbers</structfield>, <structfield>levelAdds</structfield>,
754
+ <structfield>distances</structfield>,
720
755
<structfield>reconstructedValues</structfield>, and
721
756
<structfield>traversalValues</structfield> arrays in the current memory context.
722
757
However, any output traverse values pointed to by
@@ -747,7 +782,10 @@ CREATE FUNCTION my_leaf_consistent(internal, internal) RETURNS bool ...
747
782
typedef struct spgLeafConsistentIn
748
783
{
749
784
ScanKey scankeys; /* array of operators and comparison values */
750
- int nkeys; /* length of array */
785
+ ScanKey orderbys; /* array of ordering operators and comparison
786
+ * values */
787
+ int nkeys; /* length of scankeys array */
788
+ int norderbys; /* length of orderbys array */
751
789
752
790
Datum reconstructedValue; /* value reconstructed at parent */
753
791
void *traversalValue; /* opclass-specific traverse value */
@@ -759,8 +797,10 @@ typedef struct spgLeafConsistentIn
759
797
760
798
typedef struct spgLeafConsistentOut
761
799
{
762
- Datum leafValue; /* reconstructed original data, if any */
763
- bool recheck; /* set true if operator must be rechecked */
800
+ Datum leafValue; /* reconstructed original data, if any */
801
+ bool recheck; /* set true if operator must be rechecked */
802
+ bool recheckDistances; /* set true if distances must be rechecked */
803
+ double *distances; /* associated distances */
764
804
} spgLeafConsistentOut;
765
805
</programlisting>
766
806
@@ -775,6 +815,8 @@ typedef struct spgLeafConsistentOut
775
815
In particular it is not necessary to check <structfield>sk_flags</structfield> to
776
816
see if the comparison value is NULL, because the SP-GiST core code
777
817
will filter out such conditions.
818
+ The array <structfield>orderbys</structfield>, of length <structfield>norderbys</structfield>,
819
+ describes the ordering operators in the same manner.
778
820
<structfield>reconstructedValue</structfield> is the value reconstructed for the
779
821
parent tuple; it is <literal>(Datum) 0</literal> at the root level or if the
780
822
<function>inner_consistent</function> function did not provide a value at the
@@ -803,6 +845,12 @@ typedef struct spgLeafConsistentOut
803
845
<structfield>recheck</structfield> may be set to <literal>true</literal> if the match
804
846
is uncertain and so the operator(s) must be re-applied to the actual
805
847
heap tuple to verify the match.
848
+ If ordered search is performed, set <structfield>distances</structfield>
849
+ to an array of distance values according to <structfield>orderbys</structfield>
850
+ array. Leave it NULL otherwise. If at least one of returned distances
851
+ is not exact, set <structfield>recheckDistances</structfield> to true.
852
+ In this case, the executor will calculate the exact distances after
853
+ fetching the tuple from the heap, and will reorder the tuples if needed.
806
854
</para>
807
855
</listitem>
808
856
</varlistentry>
0 commit comments