mirror of
https://github.com/postgres/postgres.git
synced 2025-10-22 14:32:25 +03:00
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
This commit is contained in:
@@ -44,8 +44,10 @@ static void prune_lexemes_hashtable(HTAB *lexemes_tab, int b_current);
|
||||
static uint32 lexeme_hash(const void *key, Size keysize);
|
||||
static int lexeme_match(const void *key1, const void *key2, Size keysize);
|
||||
static int lexeme_compare(const void *key1, const void *key2);
|
||||
static int trackitem_compare_frequencies_desc(const void *e1, const void *e2);
|
||||
static int trackitem_compare_lexemes(const void *e1, const void *e2);
|
||||
static int trackitem_compare_frequencies_desc(const void *e1, const void *e2,
|
||||
void *arg);
|
||||
static int trackitem_compare_lexemes(const void *e1, const void *e2,
|
||||
void *arg);
|
||||
|
||||
|
||||
/*
|
||||
@@ -347,8 +349,8 @@ compute_tsvector_stats(VacAttrStats *stats,
|
||||
*/
|
||||
if (num_mcelem < track_len)
|
||||
{
|
||||
qsort(sort_table, track_len, sizeof(TrackItem *),
|
||||
trackitem_compare_frequencies_desc);
|
||||
qsort_interruptible(sort_table, track_len, sizeof(TrackItem *),
|
||||
trackitem_compare_frequencies_desc, NULL);
|
||||
/* reset minfreq to the smallest frequency we're keeping */
|
||||
minfreq = sort_table[num_mcelem - 1]->frequency;
|
||||
}
|
||||
@@ -376,8 +378,8 @@ compute_tsvector_stats(VacAttrStats *stats,
|
||||
* presorted we can employ binary search for that. See
|
||||
* ts_selfuncs.c for a real usage scenario.
|
||||
*/
|
||||
qsort(sort_table, num_mcelem, sizeof(TrackItem *),
|
||||
trackitem_compare_lexemes);
|
||||
qsort_interruptible(sort_table, num_mcelem, sizeof(TrackItem *),
|
||||
trackitem_compare_lexemes, NULL);
|
||||
|
||||
/* Must copy the target values into anl_context */
|
||||
old_context = MemoryContextSwitchTo(stats->anl_context);
|
||||
@@ -510,10 +512,10 @@ lexeme_compare(const void *key1, const void *key2)
|
||||
}
|
||||
|
||||
/*
|
||||
* qsort() comparator for sorting TrackItems on frequencies (descending sort)
|
||||
* Comparator for sorting TrackItems on frequencies (descending sort)
|
||||
*/
|
||||
static int
|
||||
trackitem_compare_frequencies_desc(const void *e1, const void *e2)
|
||||
trackitem_compare_frequencies_desc(const void *e1, const void *e2, void *arg)
|
||||
{
|
||||
const TrackItem *const *t1 = (const TrackItem *const *) e1;
|
||||
const TrackItem *const *t2 = (const TrackItem *const *) e2;
|
||||
@@ -522,10 +524,10 @@ trackitem_compare_frequencies_desc(const void *e1, const void *e2)
|
||||
}
|
||||
|
||||
/*
|
||||
* qsort() comparator for sorting TrackItems on lexemes
|
||||
* Comparator for sorting TrackItems on lexemes
|
||||
*/
|
||||
static int
|
||||
trackitem_compare_lexemes(const void *e1, const void *e2)
|
||||
trackitem_compare_lexemes(const void *e1, const void *e2, void *arg)
|
||||
{
|
||||
const TrackItem *const *t1 = (const TrackItem *const *) e1;
|
||||
const TrackItem *const *t2 = (const TrackItem *const *) e2;
|
||||
|
Reference in New Issue
Block a user