Skip to content

Commit c7654f6

Browse files
committed
Fix representation of SORT_TYPE_STILL_IN_PROGRESS.
It turns out that the code did indeed rely on a zeroed TuplesortInstrumentation.sortMethod field to indicate "this worker never did anything", although it seems the issue only comes up during certain race-condition-y cases. Hence, rearrange the TuplesortMethod enum to restore SORT_TYPE_STILL_IN_PROGRESS to having the value zero, and add some comments reinforcing that that isn't optional. Also future-proof a loop over the possible values of the enum. sizeof(bits32) happened to be the correct limit value, but only by purest coincidence. Per buildfarm and local investigation. Discussion: https://postgr.es/m/[email protected]
1 parent 4c04be9 commit c7654f6

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/backend/commands/explain.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -2762,14 +2762,14 @@ show_incremental_sort_group_info(IncrementalSortGroupInfo *groupInfo,
27622762
List *methodNames = NIL;
27632763

27642764
/* Generate a list of sort methods used across all groups. */
2765-
for (int bit = 0; bit < sizeof(bits32); ++bit)
2765+
for (int bit = 0; bit < NUM_TUPLESORTMETHODS; bit++)
27662766
{
2767-
if (groupInfo->sortMethods & (1 << bit))
2767+
TuplesortMethod sortMethod = (1 << bit);
2768+
2769+
if (groupInfo->sortMethods & sortMethod)
27682770
{
2769-
TuplesortMethod sortMethod = (1 << bit);
2770-
const char *methodName;
2771+
const char *methodName = tuplesort_method_name(sortMethod);
27712772

2772-
methodName = tuplesort_method_name(sortMethod);
27732773
methodNames = lappend(methodNames, unconstify(char *, methodName));
27742774
}
27752775
}

src/include/utils/tuplesort.h

+13-7
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,24 @@ typedef struct SortCoordinateData *SortCoordinate;
6262
* TuplesortInstrumentation can't contain any pointers because we
6363
* sometimes put it in shared memory.
6464
*
65-
* TuplesortMethod is used in a bitmask in Increment Sort's shared memory
66-
* instrumentation so needs to have each value be a separate bit.
65+
* The parallel-sort infrastructure relies on having a zero TuplesortMethod
66+
* indicate that a worker never did anything, so we assign zero to
67+
* SORT_TYPE_STILL_IN_PROGRESS. The other values of this enum can be
68+
* OR'ed together to represent a situation where different workers used
69+
* different methods, so we need a separate bit for each one. Keep the
70+
* NUM_TUPLESORTMETHODS constant in sync with the number of bits!
6771
*/
6872
typedef enum
6973
{
70-
SORT_TYPE_STILL_IN_PROGRESS = 1 << 0,
71-
SORT_TYPE_TOP_N_HEAPSORT = 1 << 1,
72-
SORT_TYPE_QUICKSORT = 1 << 2,
73-
SORT_TYPE_EXTERNAL_SORT = 1 << 3,
74-
SORT_TYPE_EXTERNAL_MERGE = 1 << 4
74+
SORT_TYPE_STILL_IN_PROGRESS = 0,
75+
SORT_TYPE_TOP_N_HEAPSORT = 1 << 0,
76+
SORT_TYPE_QUICKSORT = 1 << 1,
77+
SORT_TYPE_EXTERNAL_SORT = 1 << 2,
78+
SORT_TYPE_EXTERNAL_MERGE = 1 << 3
7579
} TuplesortMethod;
7680

81+
#define NUM_TUPLESORTMETHODS 4
82+
7783
typedef enum
7884
{
7985
SORT_SPACE_TYPE_DISK,

0 commit comments

Comments
 (0)