@@ -279,6 +279,13 @@ struct Tuplesortstate
279
279
*/
280
280
SortTupleComparator comparetup ;
281
281
282
+ /*
283
+ * Alter datum1 representation in the SortTuple's array back from the
284
+ * abbreviated key to the first column value.
285
+ */
286
+ void (* removeabbrev ) (Tuplesortstate * state , SortTuple * stups ,
287
+ int count );
288
+
282
289
/*
283
290
* Function to write a stored tuple onto tape. The representation of the
284
291
* tuple on tape need not be the same as it is in memory; requirements on
@@ -540,6 +547,7 @@ struct Sharedsort
540
547
pfree(buf); \
541
548
} while(0)
542
549
550
+ #define REMOVEABBREV (state ,stup ,count ) ((*(state)->removeabbrev) (state, stup, count))
543
551
#define COMPARETUP (state ,a ,b ) ((*(state)->comparetup) (a, b, state))
544
552
#define WRITETUP (state ,tape ,stup ) ((*(state)->writetup) (state, tape, stup))
545
553
#define READTUP (state ,stup ,tape ,len ) ((*(state)->readtup) (state, stup, tape, len))
@@ -629,6 +637,14 @@ static void reversedirection(Tuplesortstate *state);
629
637
static unsigned int getlen (LogicalTape * tape , bool eofOK );
630
638
static void markrunend (LogicalTape * tape );
631
639
static void * readtup_alloc (Tuplesortstate * state , Size tuplen );
640
+ static void removeabbrev_heap (Tuplesortstate * state , SortTuple * stups ,
641
+ int count );
642
+ static void removeabbrev_cluster (Tuplesortstate * state , SortTuple * stups ,
643
+ int count );
644
+ static void removeabbrev_index (Tuplesortstate * state , SortTuple * stups ,
645
+ int count );
646
+ static void removeabbrev_datum (Tuplesortstate * state , SortTuple * stups ,
647
+ int count );
632
648
static int comparetup_heap (const SortTuple * a , const SortTuple * b ,
633
649
Tuplesortstate * state );
634
650
static void writetup_heap (Tuplesortstate * state , LogicalTape * tape ,
@@ -1042,6 +1058,7 @@ tuplesort_begin_heap(TupleDesc tupDesc,
1042
1058
sortopt & TUPLESORT_RANDOMACCESS ,
1043
1059
PARALLEL_SORT (state ));
1044
1060
1061
+ state -> removeabbrev = removeabbrev_heap ;
1045
1062
state -> comparetup = comparetup_heap ;
1046
1063
state -> writetup = writetup_heap ;
1047
1064
state -> readtup = readtup_heap ;
@@ -1117,6 +1134,7 @@ tuplesort_begin_cluster(TupleDesc tupDesc,
1117
1134
sortopt & TUPLESORT_RANDOMACCESS ,
1118
1135
PARALLEL_SORT (state ));
1119
1136
1137
+ state -> removeabbrev = removeabbrev_cluster ;
1120
1138
state -> comparetup = comparetup_cluster ;
1121
1139
state -> writetup = writetup_cluster ;
1122
1140
state -> readtup = readtup_cluster ;
@@ -1221,6 +1239,7 @@ tuplesort_begin_index_btree(Relation heapRel,
1221
1239
sortopt & TUPLESORT_RANDOMACCESS ,
1222
1240
PARALLEL_SORT (state ));
1223
1241
1242
+ state -> removeabbrev = removeabbrev_index ;
1224
1243
state -> comparetup = comparetup_index_btree ;
1225
1244
state -> writetup = writetup_index ;
1226
1245
state -> readtup = readtup_index ;
@@ -1297,6 +1316,7 @@ tuplesort_begin_index_hash(Relation heapRel,
1297
1316
1298
1317
state -> nKeys = 1 ; /* Only one sort column, the hash code */
1299
1318
1319
+ state -> removeabbrev = removeabbrev_index ;
1300
1320
state -> comparetup = comparetup_index_hash ;
1301
1321
state -> writetup = writetup_index ;
1302
1322
state -> readtup = readtup_index ;
@@ -1337,6 +1357,7 @@ tuplesort_begin_index_gist(Relation heapRel,
1337
1357
1338
1358
state -> nKeys = IndexRelationGetNumberOfKeyAttributes (indexRel );
1339
1359
1360
+ state -> removeabbrev = removeabbrev_index ;
1340
1361
state -> comparetup = comparetup_index_btree ;
1341
1362
state -> writetup = writetup_index ;
1342
1363
state -> readtup = readtup_index ;
@@ -1400,6 +1421,7 @@ tuplesort_begin_datum(Oid datumType, Oid sortOperator, Oid sortCollation,
1400
1421
sortopt & TUPLESORT_RANDOMACCESS ,
1401
1422
PARALLEL_SORT (state ));
1402
1423
1424
+ state -> removeabbrev = removeabbrev_datum ;
1403
1425
state -> comparetup = comparetup_datum ;
1404
1426
state -> writetup = writetup_datum ;
1405
1427
state -> readtup = readtup_datum ;
@@ -1858,8 +1880,6 @@ tuplesort_puttupleslot(Tuplesortstate *state, TupleTableSlot *slot)
1858
1880
else
1859
1881
{
1860
1882
/* Abort abbreviation */
1861
- int i ;
1862
-
1863
1883
stup .datum1 = original ;
1864
1884
1865
1885
/*
@@ -1871,20 +1891,7 @@ tuplesort_puttupleslot(Tuplesortstate *state, TupleTableSlot *slot)
1871
1891
* sorted on tape, since serialized tuples lack abbreviated keys
1872
1892
* (TSS_BUILDRUNS state prevents control reaching here in any case).
1873
1893
*/
1874
- for (i = 0 ; i < state -> memtupcount ; i ++ )
1875
- {
1876
- SortTuple * mtup = & state -> memtuples [i ];
1877
-
1878
- htup .t_len = ((MinimalTuple ) mtup -> tuple )-> t_len +
1879
- MINIMAL_TUPLE_OFFSET ;
1880
- htup .t_data = (HeapTupleHeader ) ((char * ) mtup -> tuple -
1881
- MINIMAL_TUPLE_OFFSET );
1882
-
1883
- mtup -> datum1 = heap_getattr (& htup ,
1884
- state -> sortKeys [0 ].ssup_attno ,
1885
- state -> tupDesc ,
1886
- & mtup -> isnull1 );
1887
- }
1894
+ REMOVEABBREV (state , state -> memtuples , state -> memtupcount );
1888
1895
}
1889
1896
1890
1897
puttuple_common (state , & stup );
@@ -1943,8 +1950,6 @@ tuplesort_putheaptuple(Tuplesortstate *state, HeapTuple tup)
1943
1950
else
1944
1951
{
1945
1952
/* Abort abbreviation */
1946
- int i ;
1947
-
1948
1953
stup .datum1 = original ;
1949
1954
1950
1955
/*
@@ -1957,16 +1962,7 @@ tuplesort_putheaptuple(Tuplesortstate *state, HeapTuple tup)
1957
1962
* (TSS_BUILDRUNS state prevents control reaching here in any
1958
1963
* case).
1959
1964
*/
1960
- for (i = 0 ; i < state -> memtupcount ; i ++ )
1961
- {
1962
- SortTuple * mtup = & state -> memtuples [i ];
1963
-
1964
- tup = (HeapTuple ) mtup -> tuple ;
1965
- mtup -> datum1 = heap_getattr (tup ,
1966
- state -> indexInfo -> ii_IndexAttrNumbers [0 ],
1967
- state -> tupDesc ,
1968
- & mtup -> isnull1 );
1969
- }
1965
+ REMOVEABBREV (state , state -> memtuples , state -> memtupcount );
1970
1966
}
1971
1967
}
1972
1968
@@ -2023,8 +2019,6 @@ tuplesort_putindextuplevalues(Tuplesortstate *state, Relation rel,
2023
2019
else
2024
2020
{
2025
2021
/* Abort abbreviation */
2026
- int i ;
2027
-
2028
2022
stup .datum1 = original ;
2029
2023
2030
2024
/*
@@ -2036,16 +2030,7 @@ tuplesort_putindextuplevalues(Tuplesortstate *state, Relation rel,
2036
2030
* sorted on tape, since serialized tuples lack abbreviated keys
2037
2031
* (TSS_BUILDRUNS state prevents control reaching here in any case).
2038
2032
*/
2039
- for (i = 0 ; i < state -> memtupcount ; i ++ )
2040
- {
2041
- SortTuple * mtup = & state -> memtuples [i ];
2042
-
2043
- tuple = mtup -> tuple ;
2044
- mtup -> datum1 = index_getattr (tuple ,
2045
- 1 ,
2046
- RelationGetDescr (state -> indexRel ),
2047
- & mtup -> isnull1 );
2048
- }
2033
+ REMOVEABBREV (state , state -> memtuples , state -> memtupcount );
2049
2034
}
2050
2035
2051
2036
puttuple_common (state , & stup );
@@ -2109,8 +2094,6 @@ tuplesort_putdatum(Tuplesortstate *state, Datum val, bool isNull)
2109
2094
else
2110
2095
{
2111
2096
/* Abort abbreviation */
2112
- int i ;
2113
-
2114
2097
stup .datum1 = original ;
2115
2098
2116
2099
/*
@@ -2123,12 +2106,7 @@ tuplesort_putdatum(Tuplesortstate *state, Datum val, bool isNull)
2123
2106
* (TSS_BUILDRUNS state prevents control reaching here in any
2124
2107
* case).
2125
2108
*/
2126
- for (i = 0 ; i < state -> memtupcount ; i ++ )
2127
- {
2128
- SortTuple * mtup = & state -> memtuples [i ];
2129
-
2130
- mtup -> datum1 = PointerGetDatum (mtup -> tuple );
2131
- }
2109
+ REMOVEABBREV (state , state -> memtuples , state -> memtupcount );
2132
2110
}
2133
2111
}
2134
2112
@@ -3985,6 +3963,26 @@ readtup_alloc(Tuplesortstate *state, Size tuplen)
3985
3963
* Routines specialized for HeapTuple (actually MinimalTuple) case
3986
3964
*/
3987
3965
3966
+ static void
3967
+ removeabbrev_heap (Tuplesortstate * state , SortTuple * stups , int count )
3968
+ {
3969
+ int i ;
3970
+
3971
+ for (i = 0 ; i < count ; i ++ )
3972
+ {
3973
+ HeapTupleData htup ;
3974
+
3975
+ htup .t_len = ((MinimalTuple ) stups [i ].tuple )-> t_len +
3976
+ MINIMAL_TUPLE_OFFSET ;
3977
+ htup .t_data = (HeapTupleHeader ) ((char * ) stups [i ].tuple -
3978
+ MINIMAL_TUPLE_OFFSET );
3979
+ stups [i ].datum1 = heap_getattr (& htup ,
3980
+ state -> sortKeys [0 ].ssup_attno ,
3981
+ state -> tupDesc ,
3982
+ & stups [i ].isnull1 );
3983
+ }
3984
+ }
3985
+
3988
3986
static int
3989
3987
comparetup_heap (const SortTuple * a , const SortTuple * b , Tuplesortstate * state )
3990
3988
{
@@ -4103,6 +4101,23 @@ readtup_heap(Tuplesortstate *state, SortTuple *stup,
4103
4101
* comparisons per a btree index definition)
4104
4102
*/
4105
4103
4104
+ static void
4105
+ removeabbrev_cluster (Tuplesortstate * state , SortTuple * stups , int count )
4106
+ {
4107
+ int i ;
4108
+
4109
+ for (i = 0 ; i < count ; i ++ )
4110
+ {
4111
+ HeapTuple tup ;
4112
+
4113
+ tup = (HeapTuple ) stups [i ].tuple ;
4114
+ stups [i ].datum1 = heap_getattr (tup ,
4115
+ state -> indexInfo -> ii_IndexAttrNumbers [0 ],
4116
+ state -> tupDesc ,
4117
+ & stups [i ].isnull1 );
4118
+ }
4119
+ }
4120
+
4106
4121
static int
4107
4122
comparetup_cluster (const SortTuple * a , const SortTuple * b ,
4108
4123
Tuplesortstate * state )
@@ -4272,6 +4287,23 @@ readtup_cluster(Tuplesortstate *state, SortTuple *stup,
4272
4287
* functions can be shared.
4273
4288
*/
4274
4289
4290
+ static void
4291
+ removeabbrev_index (Tuplesortstate * state , SortTuple * stups , int count )
4292
+ {
4293
+ int i ;
4294
+
4295
+ for (i = 0 ; i < count ; i ++ )
4296
+ {
4297
+ IndexTuple tuple ;
4298
+
4299
+ tuple = stups [i ].tuple ;
4300
+ stups [i ].datum1 = index_getattr (tuple ,
4301
+ 1 ,
4302
+ RelationGetDescr (state -> indexRel ),
4303
+ & stups [i ].isnull1 );
4304
+ }
4305
+ }
4306
+
4275
4307
static int
4276
4308
comparetup_index_btree (const SortTuple * a , const SortTuple * b ,
4277
4309
Tuplesortstate * state )
@@ -4504,6 +4536,15 @@ readtup_index(Tuplesortstate *state, SortTuple *stup,
4504
4536
* Routines specialized for DatumTuple case
4505
4537
*/
4506
4538
4539
+ static void
4540
+ removeabbrev_datum (Tuplesortstate * state , SortTuple * stups , int count )
4541
+ {
4542
+ int i ;
4543
+
4544
+ for (i = 0 ; i < count ; i ++ )
4545
+ stups [i ].datum1 = PointerGetDatum (stups [i ].tuple );
4546
+ }
4547
+
4507
4548
static int
4508
4549
comparetup_datum (const SortTuple * a , const SortTuple * b , Tuplesortstate * state )
4509
4550
{
0 commit comments