44
44
#endif
45
45
46
46
/*
47
- * We need extra field in a state structure but we should not modify struct RumTuplesortstate
48
- * which is inherited from Tuplesortstate core function.
47
+ * We need extra field in a state structure but we should not modify struct
48
+ * RumTuplesortstate which is inherited from Tuplesortstate core function.
49
49
*/
50
50
typedef struct RumTuplesortstateExt
51
51
{
@@ -55,18 +55,28 @@ typedef struct RumTuplesortstateExt
55
55
56
56
static int compare_rum_itempointer (ItemPointerData p1 , ItemPointerData p2 );
57
57
static int comparetup_rum (const SortTuple * a , const SortTuple * b ,
58
- RumTuplesortstate * state , bool compareItemPointer );
58
+ RumTuplesortstate * state , bool compareItemPointer );
59
59
static int comparetup_rum_true (const SortTuple * a , const SortTuple * b ,
60
- RumTuplesortstate * state );
60
+ RumTuplesortstate * state );
61
61
static int comparetup_rum_false (const SortTuple * a , const SortTuple * b ,
62
- RumTuplesortstate * state );
62
+ RumTuplesortstate * state );
63
63
static int comparetup_rumitem (const SortTuple * a , const SortTuple * b ,
64
- RumTuplesortstate * state );
65
- static void copytup_rum (RumTuplesortstate * state , SortTuple * stup , void * tup );
66
- static void copytup_rumitem (RumTuplesortstate * state , SortTuple * stup , void * tup );
67
- static void * rum_tuplesort_getrum_internal (RumTuplesortstate * state , bool forward , bool * should_free );
64
+ RumTuplesortstate * state );
65
+ static void copytup_rum (RumTuplesortstate * state , SortTuple * stup , void * tup );
66
+ static void copytup_rumitem (RumTuplesortstate * state , SortTuple * stup ,
67
+ void * tup );
68
+ static void * rum_tuplesort_getrum_internal (RumTuplesortstate * state ,
69
+ bool forward , bool * should_free );
70
+
71
+ #if PG_VERSION_NUM >= 160000
72
+ # define TSS_GET (state ) \
73
+ TuplesortstateGetPublic((state))
74
+ #else
75
+ # define TSS_GET (state ) \
76
+ (state)
77
+ #endif
68
78
69
- static int
79
+ static inline int
70
80
compare_rum_itempointer (ItemPointerData p1 , ItemPointerData p2 )
71
81
{
72
82
if (p1 .ip_blkid .bi_hi < p2 .ip_blkid .bi_hi )
@@ -88,7 +98,8 @@ compare_rum_itempointer(ItemPointerData p1, ItemPointerData p2)
88
98
}
89
99
90
100
static int
91
- comparetup_rum (const SortTuple * a , const SortTuple * b , RumTuplesortstate * state , bool compareItemPointer )
101
+ comparetup_rum (const SortTuple * a , const SortTuple * b ,
102
+ RumTuplesortstate * state , bool compareItemPointer )
92
103
{
93
104
RumSortItem * i1 ,
94
105
* i2 ;
@@ -104,7 +115,7 @@ comparetup_rum(const SortTuple *a, const SortTuple *b, RumTuplesortstate * state
104
115
i1 = (RumSortItem * ) a -> tuple ;
105
116
i2 = (RumSortItem * ) b -> tuple ;
106
117
107
- for (i = 1 ; i < state -> nKeys ; i ++ )
118
+ for (i = 1 ; i < TSS_GET ( state ) -> nKeys ; i ++ )
108
119
{
109
120
if (i1 -> data [i ] < i2 -> data [i ])
110
121
return -1 ;
@@ -122,19 +133,22 @@ comparetup_rum(const SortTuple *a, const SortTuple *b, RumTuplesortstate * state
122
133
}
123
134
124
135
static int
125
- comparetup_rum_true (const SortTuple * a , const SortTuple * b , RumTuplesortstate * state )
136
+ comparetup_rum_true (const SortTuple * a , const SortTuple * b ,
137
+ RumTuplesortstate * state )
126
138
{
127
139
return comparetup_rum (a , b , state , true);
128
140
}
129
141
130
142
static int
131
- comparetup_rum_false (const SortTuple * a , const SortTuple * b , RumTuplesortstate * state )
143
+ comparetup_rum_false (const SortTuple * a , const SortTuple * b ,
144
+ RumTuplesortstate * state )
132
145
{
133
146
return comparetup_rum (a , b , state , false);
134
147
}
135
148
136
149
static int
137
- comparetup_rumitem (const SortTuple * a , const SortTuple * b , RumTuplesortstate * state )
150
+ comparetup_rumitem (const SortTuple * a , const SortTuple * b ,
151
+ RumTuplesortstate * state )
138
152
{
139
153
RumItem * i1 ,
140
154
* i2 ;
@@ -171,23 +185,24 @@ comparetup_rumitem(const SortTuple *a, const SortTuple *b, RumTuplesortstate * s
171
185
}
172
186
173
187
static void
174
- copytup_rum (RumTuplesortstate * state , SortTuple * stup , void * tup )
188
+ copytup_rum (RumTuplesortstate * state , SortTuple * stup , void * tup )
175
189
{
176
190
RumSortItem * item = (RumSortItem * ) tup ;
191
+ int nKeys = TSS_GET (state )-> nKeys ;
177
192
178
- stup -> datum1 = Float8GetDatum (state -> nKeys > 0 ? item -> data [0 ] : 0 );
193
+ stup -> datum1 = Float8GetDatum (nKeys > 0 ? item -> data [0 ] : 0 );
179
194
stup -> isnull1 = false;
180
195
stup -> tuple = tup ;
181
- USEMEM (state , GetMemoryChunkSpace (tup ));
196
+ // USEMEM(state, GetMemoryChunkSpace(tup));
182
197
}
183
198
184
199
static void
185
- copytup_rumitem (RumTuplesortstate * state , SortTuple * stup , void * tup )
200
+ copytup_rumitem (RumTuplesortstate * state , SortTuple * stup , void * tup )
186
201
{
187
202
stup -> isnull1 = true;
188
203
stup -> tuple = palloc (sizeof (RumScanItem ));
189
204
memcpy (stup -> tuple , tup , sizeof (RumScanItem ));
190
- USEMEM (state , GetMemoryChunkSpace (stup -> tuple ));
205
+ // USEMEM(state, GetMemoryChunkSpace(stup->tuple));
191
206
}
192
207
193
208
#if PG_VERSION_NUM >= 150000
@@ -200,19 +215,27 @@ copytup_rumitem(RumTuplesortstate * state, SortTuple *stup, void *tup)
200
215
#define TAPE (state , LT_ARG ) state->tapeset, LT_ARG
201
216
#endif
202
217
218
+ static void readtup_rum (RumTuplesortstate * state , SortTuple * stup ,
219
+ LT_TYPE LT_ARG , unsigned int len );
220
+
221
+ static void readtup_rumitem (RumTuplesortstate * state , SortTuple * stup ,
222
+ LT_TYPE LT_ARG , unsigned int len );
223
+
203
224
static Size
204
- rum_item_size (RumTuplesortstate * state )
225
+ rum_item_size (RumTuplesortstate * state )
205
226
{
206
- if (state -> copytup == copytup_rum )
207
- return RumSortItemSize (state -> nKeys );
208
- else if (state -> copytup == copytup_rumitem )
227
+ if (TSS_GET ( state ) -> readtup == readtup_rum )
228
+ return RumSortItemSize (TSS_GET ( state ) -> nKeys );
229
+ else if (TSS_GET ( state ) -> readtup == readtup_rumitem )
209
230
return sizeof (RumScanItem );
210
231
else
211
232
elog (FATAL , "Unknown RUM state" );
233
+ return 0 ; /* Silence compiler */
212
234
}
213
235
214
236
static void
215
- writetup_rum_internal (RumTuplesortstate * state , LT_TYPE LT_ARG , SortTuple * stup )
237
+ writetup_rum_internal (RumTuplesortstate * state , LT_TYPE LT_ARG ,
238
+ SortTuple * stup )
216
239
{
217
240
void * item = stup -> tuple ;
218
241
size_t size = rum_item_size (state );
@@ -223,28 +246,29 @@ writetup_rum_internal(RumTuplesortstate * state, LT_TYPE LT_ARG, SortTuple *stup
223
246
LogicalTapeWrite (TAPE (state , LT_ARG ),
224
247
(void * ) item , size );
225
248
#if PG_VERSION_NUM >= 150000
226
- if (state -> sortopt & TUPLESORT_RANDOMACCESS ) /* need trailing length word? */
249
+ if (TSS_GET (state )-> sortopt & TUPLESORT_RANDOMACCESS ) /* need trailing
250
+ * length word? */
227
251
#else
228
- if (state -> randomAccess ) /* need trailing length word? */
252
+ if (TSS_GET ( state ) -> randomAccess ) /* need trailing length word? */
229
253
#endif
230
- LogicalTapeWrite (TAPE (state , LT_ARG ),
231
- ( void * ) & writtenlen , sizeof (writtenlen ));
254
+ LogicalTapeWrite (TAPE (TSS_GET ( state ) , LT_ARG ), ( void * ) & writtenlen ,
255
+ sizeof (writtenlen ));
232
256
}
233
257
234
258
static void
235
- writetup_rum (RumTuplesortstate * state , LT_TYPE LT_ARG , SortTuple * stup )
259
+ writetup_rum (RumTuplesortstate * state , LT_TYPE LT_ARG , SortTuple * stup )
236
260
{
237
261
writetup_rum_internal (state , LT_ARG , stup );
238
262
}
239
263
240
264
static void
241
- writetup_rumitem (RumTuplesortstate * state , LT_TYPE LT_ARG , SortTuple * stup )
265
+ writetup_rumitem (RumTuplesortstate * state , LT_TYPE LT_ARG , SortTuple * stup )
242
266
{
243
267
writetup_rum_internal (state , LT_ARG , stup );
244
268
}
245
269
246
270
static void
247
- readtup_rum_internal (RumTuplesortstate * state , SortTuple * stup ,
271
+ readtup_rum_internal (RumTuplesortstate * state , SortTuple * stup ,
248
272
LT_TYPE LT_ARG , unsigned int len , bool is_item )
249
273
{
250
274
unsigned int tuplen = len - sizeof (unsigned int );
@@ -253,36 +277,39 @@ readtup_rum_internal(RumTuplesortstate * state, SortTuple *stup,
253
277
254
278
Assert (tuplen == size );
255
279
256
- USEMEM (state , GetMemoryChunkSpace (item ));
280
+ // USEMEM(state, GetMemoryChunkSpace(item));
257
281
#if PG_VERSION_NUM >= 150000
258
282
LogicalTapeReadExact (LT_ARG , item , size );
259
283
#else
260
- LogicalTapeReadExact (state -> tapeset , LT_ARG , item , size );
284
+ LogicalTapeReadExact (TSS_GET ( state ) -> tapeset , LT_ARG , item , size );
261
285
#endif
262
286
stup -> tuple = item ;
263
287
stup -> isnull1 = is_item ;
264
288
265
289
if (!is_item )
266
- stup -> datum1 = Float8GetDatum (state -> nKeys > 0 ? ((RumSortItem * ) item )-> data [0 ] : 0 );
290
+ stup -> datum1 = Float8GetDatum (TSS_GET (state )-> nKeys > 0 ?
291
+ ((RumSortItem * ) item )-> data [0 ] : 0 );
267
292
#if PG_VERSION_NUM >= 150000
268
- if (state -> sortopt & TUPLESORT_RANDOMACCESS ) /* need trailing length word? */
293
+ if (TSS_GET (state )-> sortopt & TUPLESORT_RANDOMACCESS ) /* need trailing
294
+ * length word? */
269
295
LogicalTapeReadExact (LT_ARG , & tuplen , sizeof (tuplen ));
270
296
#else
271
- if (state -> randomAccess )
272
- LogicalTapeReadExact (state -> tapeset , LT_ARG , & tuplen , sizeof (tuplen ));
297
+ if (TSS_GET (state )-> randomAccess )
298
+ LogicalTapeReadExact (TSS_GET (state )-> tapeset , LT_ARG , & tuplen ,
299
+ sizeof (tuplen ));
273
300
#endif
274
301
}
275
302
276
303
static void
277
- readtup_rum (RumTuplesortstate * state , SortTuple * stup ,
278
- LT_TYPE LT_ARG , unsigned int len )
304
+ readtup_rum (RumTuplesortstate * state , SortTuple * stup , LT_TYPE LT_ARG ,
305
+ unsigned int len )
279
306
{
280
307
readtup_rum_internal (state , stup , LT_ARG , len , false);
281
308
}
282
309
283
310
static void
284
- readtup_rumitem (RumTuplesortstate * state , SortTuple * stup ,
285
- LT_TYPE LT_ARG , unsigned int len )
311
+ readtup_rumitem (RumTuplesortstate * state , SortTuple * stup , LT_TYPE LT_ARG ,
312
+ unsigned int len )
286
313
{
287
314
readtup_rum_internal (state , stup , LT_ARG , len , true);
288
315
}
@@ -305,7 +332,7 @@ rum_tuplesort_begin_rum(int workMem, int nKeys, bool randomAccess,
305
332
#endif
306
333
MemoryContext oldcontext ;
307
334
308
- oldcontext = MemoryContextSwitchTo (state -> sortcontext );
335
+ oldcontext = MemoryContextSwitchTo (TSS_GET ( state ) -> sortcontext );
309
336
310
337
#ifdef TRACE_SORT
311
338
if (trace_sort )
@@ -314,12 +341,11 @@ rum_tuplesort_begin_rum(int workMem, int nKeys, bool randomAccess,
314
341
nKeys , workMem , randomAccess ? 't' : 'f' );
315
342
#endif
316
343
317
- state -> nKeys = nKeys ;
318
-
319
- state -> comparetup = compareItemPointer ? comparetup_rum_true : comparetup_rum_false ;
320
- state -> copytup = copytup_rum ;
321
- state -> writetup = writetup_rum ;
322
- state -> readtup = readtup_rum ;
344
+ TSS_GET (state )-> nKeys = nKeys ;
345
+ TSS_GET (state )-> comparetup = compareItemPointer ? comparetup_rum_true :
346
+ comparetup_rum_false ;
347
+ TSS_GET (state )-> writetup = writetup_rum ;
348
+ TSS_GET (state )-> readtup = readtup_rum ;
323
349
324
350
MemoryContextSwitchTo (oldcontext );
325
351
@@ -333,7 +359,7 @@ rum_tuplesort_begin_rumitem(int workMem, FmgrInfo *cmp)
333
359
RumTuplesortstateExt * rs ;
334
360
MemoryContext oldcontext ;
335
361
336
- oldcontext = MemoryContextSwitchTo (state -> sortcontext );
362
+ oldcontext = MemoryContextSwitchTo (TSS_GET ( state ) -> sortcontext );
337
363
338
364
/* Allocate extended state in the same context as state */
339
365
rs = palloc (sizeof (* rs ));
@@ -345,10 +371,9 @@ rum_tuplesort_begin_rumitem(int workMem, FmgrInfo *cmp)
345
371
#endif
346
372
347
373
rs -> cmp = cmp ;
348
- state -> comparetup = comparetup_rumitem ;
349
- state -> copytup = copytup_rumitem ;
350
- state -> writetup = writetup_rumitem ;
351
- state -> readtup = readtup_rumitem ;
374
+ TSS_GET (state )-> comparetup = comparetup_rumitem ;
375
+ TSS_GET (state )-> writetup = writetup_rumitem ;
376
+ TSS_GET (state )-> readtup = readtup_rumitem ;
352
377
memcpy (& rs -> ts , state , sizeof (RumTuplesortstate ));
353
378
pfree (state ); /* just to be sure *state isn't used anywhere
354
379
* else */
@@ -368,7 +393,7 @@ rum_tuplesort_begin_rumitem(int workMem, FmgrInfo *cmp)
368
393
* pointers afterwards!
369
394
*/
370
395
void
371
- rum_tuplesort_end (RumTuplesortstate * state )
396
+ rum_tuplesort_end (RumTuplesortstate * state )
372
397
{
373
398
#if PG_VERSION_NUM >= 130000
374
399
tuplesort_free (state );
@@ -382,39 +407,57 @@ rum_tuplesort_end(RumTuplesortstate * state)
382
407
* RumSortItem.
383
408
*/
384
409
MemoryContext
385
- rum_tuplesort_get_memorycontext (RumTuplesortstate * state )
410
+ rum_tuplesort_get_memorycontext (RumTuplesortstate * state )
386
411
{
387
- return state -> sortcontext ;
412
+ return TSS_GET ( state ) -> sortcontext ;
388
413
}
389
414
390
415
void
391
416
rum_tuplesort_putrum (RumTuplesortstate * state , RumSortItem * item )
392
417
{
393
- tuplesort_puttupleslot (state , (TupleTableSlot * ) item );
418
+ MemoryContext oldcontext ;
419
+ SortTuple stup ;
420
+
421
+ oldcontext = MemoryContextSwitchTo (rum_tuplesort_get_memorycontext (state ));
422
+ copytup_rum (state , & stup , item );
423
+ puttuple_common (state , & stup );
424
+
425
+ MemoryContextSwitchTo (oldcontext );
394
426
}
395
427
396
428
void
397
429
rum_tuplesort_putrumitem (RumTuplesortstate * state , RumScanItem * item )
398
430
{
399
- tuplesort_puttupleslot (state , (TupleTableSlot * ) item );
431
+ MemoryContext oldcontext ;
432
+ SortTuple stup ;
433
+
434
+ oldcontext = MemoryContextSwitchTo (rum_tuplesort_get_memorycontext (state ));
435
+ copytup_rumitem (state , & stup , item );
436
+ puttuple_common (state , & stup );
437
+
438
+ MemoryContextSwitchTo (oldcontext );
400
439
}
401
440
402
441
void
403
- rum_tuplesort_performsort (RumTuplesortstate * state )
442
+ rum_tuplesort_performsort (RumTuplesortstate * state )
404
443
{
405
444
tuplesort_performsort (state );
406
445
}
407
446
408
447
/*
409
- * Internal routine to fetch the next index tuple in either forward or back direction.
410
- * Returns NULL if no more tuples. Returned tuple belongs to tuplesort memory context. Caller may not rely on tuple remaining valid after any further manipulation of tuplesort.
448
+ * Internal routine to fetch the next index tuple in either forward or back
449
+ * direction. Returns NULL if no more tuples. Returned tuple belongs to
450
+ * tuplesort memory context. Caller may not rely on tuple remaining valid after
451
+ * any further manipulation of tuplesort.
452
+ *
411
453
* If *should_free is set, the caller must pfree stup.tuple when done with it.
412
454
*
413
- * NOTE: in PG 10 and newer tuple is always allocated tuple in tuplesort context and
414
- * should not be freed by caller.
455
+ * NOTE: in PG 10 and newer tuple is always allocated tuple in tuplesort context
456
+ * and should not be freed by caller.
415
457
*/
416
458
static void *
417
- rum_tuplesort_getrum_internal (RumTuplesortstate * state , bool forward , bool * should_free )
459
+ rum_tuplesort_getrum_internal (RumTuplesortstate * state , bool forward ,
460
+ bool * should_free )
418
461
{
419
462
#if PG_VERSION_NUM >= 100000
420
463
* should_free = false;
@@ -425,13 +468,16 @@ rum_tuplesort_getrum_internal(RumTuplesortstate * state, bool forward, bool *sho
425
468
}
426
469
427
470
RumSortItem *
428
- rum_tuplesort_getrum (RumTuplesortstate * state , bool forward , bool * should_free )
471
+ rum_tuplesort_getrum (RumTuplesortstate * state , bool forward , bool * should_free )
429
472
{
430
- return (RumSortItem * ) rum_tuplesort_getrum_internal (state , forward , should_free );
473
+ return (RumSortItem * ) rum_tuplesort_getrum_internal (state , forward ,
474
+ should_free );
431
475
}
432
476
433
477
RumScanItem *
434
- rum_tuplesort_getrumitem (RumTuplesortstate * state , bool forward , bool * should_free )
478
+ rum_tuplesort_getrumitem (RumTuplesortstate * state , bool forward ,
479
+ bool * should_free )
435
480
{
436
- return (RumScanItem * ) rum_tuplesort_getrum_internal (state , forward , should_free );
481
+ return (RumScanItem * ) rum_tuplesort_getrum_internal (state , forward ,
482
+ should_free );
437
483
}
0 commit comments