Invent qsort_interruptible().
authorTom Lane
Tue, 12 Jul 2022 20:30:36 +0000 (16:30 -0400)
committerTom Lane
Tue, 12 Jul 2022 20:30:36 +0000 (16:30 -0400)
commitaf72b0889441e5ece199f782511d84d72fa6b88c
treec3a419f5a45ddd69ef4eb9efbabdd0f9fc59d859
parent5e7608e81ebb2a9e2a72ab771eba3c620c0b42a6
Invent qsort_interruptible().

Justin Pryzby reported that some scenarios could cause gathering
of extended statistics to spend many seconds in an un-cancelable
qsort() operation.  To fix, invent qsort_interruptible(), which is
just like qsort_arg() except that it will also do CHECK_FOR_INTERRUPTS
every so often.  This bloats the backend by a couple of kB, which
seems like a good investment.  (We considered just enabling
CHECK_FOR_INTERRUPTS in the existing qsort and qsort_arg functions,
but there are some callers for which that'd demonstrably be unsafe.
Opt-in seems like a better way.)

For now, just apply qsort_interruptible() in statistics collection.
There's probably more places where it could be useful, but we can
always change other call sites as we find problems.

Back-patch to v14.  Before that we didn't have extended stats on
expressions, so that the problem was less severe.  Also, this patch
depends on the sort_template infrastructure introduced in v14.

Tom Lane and Justin Pryzby

Discussion: https://postgr.es/m/20220509000108[email protected]
src/backend/commands/analyze.c
src/backend/statistics/extended_stats.c
src/backend/statistics/mcv.c
src/backend/statistics/mvdistinct.c
src/backend/tsearch/ts_typanalyze.c
src/backend/utils/adt/array_typanalyze.c
src/backend/utils/adt/rangetypes_typanalyze.c
src/backend/utils/sort/Makefile
src/backend/utils/sort/qsort_interruptible.c [new file with mode: 0644]
src/include/port.h