aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/sort/qsort_interruptible.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2022-07-12 16:30:36 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2022-07-12 16:30:36 -0400
commitaf72b0889441e5ece199f782511d84d72fa6b88c (patch)
treec3a419f5a45ddd69ef4eb9efbabdd0f9fc59d859 /src/backend/utils/sort/qsort_interruptible.c
parent5e7608e81ebb2a9e2a72ab771eba3c620c0b42a6 (diff)
downloadpostgresql-af72b0889441e5ece199f782511d84d72fa6b88c.tar.gz
postgresql-af72b0889441e5ece199f782511d84d72fa6b88c.zip
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.GQ28830@telsasoft.com
Diffstat (limited to 'src/backend/utils/sort/qsort_interruptible.c')
-rw-r--r--src/backend/utils/sort/qsort_interruptible.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/backend/utils/sort/qsort_interruptible.c b/src/backend/utils/sort/qsort_interruptible.c
new file mode 100644
index 00000000000..f179b256248
--- /dev/null
+++ b/src/backend/utils/sort/qsort_interruptible.c
@@ -0,0 +1,16 @@
+/*
+ * qsort_interruptible.c: qsort_arg that includes CHECK_FOR_INTERRUPTS
+ */
+
+#include "postgres.h"
+#include "miscadmin.h"
+
+#define ST_SORT qsort_interruptible
+#define ST_ELEMENT_TYPE_VOID
+#define ST_COMPARATOR_TYPE_NAME qsort_arg_comparator
+#define ST_COMPARE_RUNTIME_POINTER
+#define ST_COMPARE_ARG_TYPE void
+#define ST_SCOPE
+#define ST_DEFINE
+#define ST_CHECK_FOR_INTERRUPTS
+#include "lib/sort_template.h"