|
13 | 13 | */
|
14 | 14 |
|
15 | 15 | #include "postgres.h"
|
| 16 | +#include "ginsort.h" |
16 | 17 |
|
17 | 18 | #include "access/relscan.h"
|
18 | 19 | #include "miscadmin.h"
|
@@ -2187,60 +2188,59 @@ insertScanItem(GinScanOpaque so, bool recheck)
|
2187 | 2188 | tuplesort_putgin(so->sortstate, item);
|
2188 | 2189 | }
|
2189 | 2190 |
|
2190 |
| -// Datum |
2191 |
| -// gingettuple(PG_FUNCTION_ARGS) |
2192 |
| -// { |
2193 |
| -// IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0); |
2194 |
| -// bool recheck; |
2195 |
| -// GinScanOpaque so = (GinScanOpaque)scan->opaque; |
2196 |
| -// GinSortItem *item; |
2197 |
| -// bool should_free; |
2198 |
| -// |
2199 |
| -// if (so->firstCall) |
2200 |
| -// { |
2201 |
| -// so->norderbys = scan->numberOfOrderBys; |
2202 |
| -// |
2203 |
| -// /* |
2204 |
| -// * Set up the scan keys, and check for unsatisfiable query. |
2205 |
| -// */ |
2206 |
| -// if (GinIsNewKey(scan)) |
2207 |
| -// ginNewScanKey(scan); |
2208 |
| -// |
2209 |
| -// if (GinIsVoidRes(scan)) |
2210 |
| -// PG_RETURN_INT64(0); |
2211 |
| -// |
2212 |
| -// so->tbm = NULL; |
2213 |
| -// so->entriesIncrIndex = -1; |
2214 |
| -// so->firstCall = false; |
2215 |
| -// so->sortstate = tuplesort_begin_gin(work_mem, so->norderbys, false); |
2216 |
| -// |
2217 |
| -// scanPendingInsert(scan); |
2218 |
| -// |
2219 |
| -// /* |
2220 |
| -// * Now scan the main index. |
2221 |
| -// */ |
2222 |
| -// startScan(scan); |
2223 |
| -// |
2224 |
| -// while (scanGetItem(scan, &so->iptr, &so->iptr, &recheck)) |
2225 |
| -// { |
2226 |
| -// insertScanItem(so, recheck); |
2227 |
| -// } |
2228 |
| -// tuplesort_performsort(so->sortstate); |
2229 |
| -// } |
2230 |
| -// |
2231 |
| -// item = tuplesort_getgin(so->sortstate, true, &should_free); |
2232 |
| -// if (item) |
2233 |
| -// { |
2234 |
| -// scan->xs_ctup.t_self = item->iptr; |
2235 |
| -// scan->xs_recheck = item->recheck; |
2236 |
| -// |
2237 |
| -// if (should_free) |
2238 |
| -// pfree(item); |
2239 |
| -// PG_RETURN_BOOL(true); |
2240 |
| -// } |
2241 |
| -// else |
2242 |
| -// { |
2243 |
| -// PG_RETURN_BOOL(false); |
2244 |
| -// } |
2245 |
| -// } |
| 2191 | +Datum |
| 2192 | +gingettuple(PG_FUNCTION_ARGS) |
| 2193 | +{ |
| 2194 | + IndexScanDesc scan = (IndexScanDesc) PG_GETARG_POINTER(0); |
| 2195 | + bool recheck; |
| 2196 | + GinScanOpaque so = (GinScanOpaque)scan->opaque; |
| 2197 | + GinSortItem *item; |
| 2198 | + bool should_free; |
| 2199 | + |
| 2200 | + if (so->firstCall) |
| 2201 | + { |
| 2202 | + so->norderbys = scan->numberOfOrderBys; |
| 2203 | + |
| 2204 | + /* |
| 2205 | + * Set up the scan keys, and check for unsatisfiable query. |
| 2206 | + */ |
| 2207 | + if (GinIsNewKey(scan)) |
| 2208 | + ginNewScanKey(scan); |
| 2209 | + |
| 2210 | + if (GinIsVoidRes(scan)) |
| 2211 | + PG_RETURN_INT64(0); |
| 2212 | + |
| 2213 | + so->tbm = NULL; |
| 2214 | + so->entriesIncrIndex = -1; |
| 2215 | + so->firstCall = false; |
| 2216 | + so->sortstate = tuplesort_begin_gin(work_mem, so->norderbys, false); |
| 2217 | + |
| 2218 | + scanPendingInsert(scan); |
| 2219 | + |
| 2220 | + /* |
| 2221 | + * Now scan the main index. |
| 2222 | + */ |
| 2223 | + startScan(scan); |
2246 | 2224 |
|
| 2225 | + while (scanGetItem(scan, &so->iptr, &so->iptr, &recheck)) |
| 2226 | + { |
| 2227 | + insertScanItem(so, recheck); |
| 2228 | + } |
| 2229 | + tuplesort_performsort(so->sortstate); |
| 2230 | + } |
| 2231 | + |
| 2232 | + item = tuplesort_getgin(so->sortstate, true, &should_free); |
| 2233 | + if (item) |
| 2234 | + { |
| 2235 | + scan->xs_ctup.t_self = item->iptr; |
| 2236 | + scan->xs_recheck = item->recheck; |
| 2237 | + |
| 2238 | + if (should_free) |
| 2239 | + pfree(item); |
| 2240 | + PG_RETURN_BOOL(true); |
| 2241 | + } |
| 2242 | + else |
| 2243 | + { |
| 2244 | + PG_RETURN_BOOL(false); |
| 2245 | + } |
| 2246 | +} |
0 commit comments