From: Nathan Bossart Date: Thu, 15 Feb 2024 10:53:11 -0600 Subject: [PATCH v8 1/3] Remove direct calls to pg_qsort(). Calls to this function might give the idea that pg_qsort() is somehow different than qsort(), when in fact all calls to qsort() in the Postgres tree are redirected to pg_qsort() via a macro. This commit removes all direct calls to pg_qsort() in favor of letting the macro handle the conversions. Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com --- contrib/pg_prewarm/autoprewarm.c | 4 ++-- src/backend/access/brin/brin_minmax_multi.c | 2 +- src/backend/optimizer/plan/analyzejoins.c | 4 ++-- src/backend/storage/buffer/bufmgr.c | 4 ++-- src/backend/utils/cache/syscache.c | 10 +++++----- src/include/port.h | 3 +++ src/port/qsort.c | 3 +++ 7 files changed, 18 insertions(+), 12 deletions(-) diff --git a/contrib/pg_prewarm/autoprewarm.c b/contrib/pg_prewarm/autoprewarm.c index 06ee21d496..248b9914a3 100644 --- a/contrib/pg_prewarm/autoprewarm.c +++ b/contrib/pg_prewarm/autoprewarm.c @@ -346,8 +346,8 @@ apw_load_buffers(void) FreeFile(file); /* Sort the blocks to be loaded. */ - pg_qsort(blkinfo, num_elements, sizeof(BlockInfoRecord), - apw_compare_blockinfo); + qsort(blkinfo, num_elements, sizeof(BlockInfoRecord), + apw_compare_blockinfo); /* Populate shared memory state. */ apw_state->block_info_handle = dsm_segment_handle(seg); diff --git a/src/backend/access/brin/brin_minmax_multi.c b/src/backend/access/brin/brin_minmax_multi.c index 3ffaad3e42..2c29aa3d4e 100644 --- a/src/backend/access/brin/brin_minmax_multi.c +++ b/src/backend/access/brin/brin_minmax_multi.c @@ -1369,7 +1369,7 @@ build_distances(FmgrInfo *distanceFn, Oid colloid, * Sort the distances in descending order, so that the longest gaps are at * the front. */ - pg_qsort(distances, ndistances, sizeof(DistanceValue), compare_distances); + qsort(distances, ndistances, sizeof(DistanceValue), compare_distances); return distances; } diff --git a/src/backend/optimizer/plan/analyzejoins.c b/src/backend/optimizer/plan/analyzejoins.c index 7dcb74572a..e494acd51a 100644 --- a/src/backend/optimizer/plan/analyzejoins.c +++ b/src/backend/optimizer/plan/analyzejoins.c @@ -2307,8 +2307,8 @@ remove_self_joins_recurse(PlannerInfo *root, List *joinlist, Relids toRemove) j++; } - pg_qsort(candidates, numRels, sizeof(SelfJoinCandidate), - self_join_candidates_cmp); + qsort(candidates, numRels, sizeof(SelfJoinCandidate), + self_join_candidates_cmp); /* * Iteratively form a group of relation indexes with the same oid and diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index eb1ec3b86d..07575ef312 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -3915,7 +3915,7 @@ DropRelationsAllBuffers(SMgrRelation *smgr_reln, int nlocators) /* sort the list of rlocators if necessary */ if (use_bsearch) - pg_qsort(locators, n, sizeof(RelFileLocator), rlocator_comparator); + qsort(locators, n, sizeof(RelFileLocator), rlocator_comparator); for (i = 0; i < NBuffers; i++) { @@ -4269,7 +4269,7 @@ FlushRelationsAllBuffers(SMgrRelation *smgrs, int nrels) /* sort the list of SMgrRelations if necessary */ if (use_bsearch) - pg_qsort(srels, nrels, sizeof(SMgrSortArray), rlocator_comparator); + qsort(srels, nrels, sizeof(SMgrSortArray), rlocator_comparator); for (i = 0; i < NBuffers; i++) { diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c index 162855b158..662f930864 100644 --- a/src/backend/utils/cache/syscache.c +++ b/src/backend/utils/cache/syscache.c @@ -146,14 +146,14 @@ InitCatalogCache(void) Assert(SysCacheSupportingRelOidSize <= lengthof(SysCacheSupportingRelOid)); /* Sort and de-dup OID arrays, so we can use binary search. */ - pg_qsort(SysCacheRelationOid, SysCacheRelationOidSize, - sizeof(Oid), oid_compare); + qsort(SysCacheRelationOid, SysCacheRelationOidSize, + sizeof(Oid), oid_compare); SysCacheRelationOidSize = qunique(SysCacheRelationOid, SysCacheRelationOidSize, sizeof(Oid), oid_compare); - pg_qsort(SysCacheSupportingRelOid, SysCacheSupportingRelOidSize, - sizeof(Oid), oid_compare); + qsort(SysCacheSupportingRelOid, SysCacheSupportingRelOidSize, + sizeof(Oid), oid_compare); SysCacheSupportingRelOidSize = qunique(SysCacheSupportingRelOid, SysCacheSupportingRelOidSize, sizeof(Oid), oid_compare); @@ -668,7 +668,7 @@ RelationSupportsSysCache(Oid relid) /* - * OID comparator for pg_qsort + * OID comparator for qsort */ static int oid_compare(const void *a, const void *b) diff --git a/src/include/port.h b/src/include/port.h index 263cf791a3..a2cebbbd68 100644 --- a/src/include/port.h +++ b/src/include/port.h @@ -438,6 +438,9 @@ extern bool pg_get_user_name(uid_t user_id, char *buffer, size_t buflen); extern bool pg_get_user_home_dir(uid_t user_id, char *buffer, size_t buflen); #endif +/* + * NB: Callers should use qsort() instead of calling pg_qsort() directly. + */ extern void pg_qsort(void *base, size_t nel, size_t elsize, int (*cmp) (const void *, const void *)); extern int pg_qsort_strcmp(const void *a, const void *b); diff --git a/src/port/qsort.c b/src/port/qsort.c index 7879e6cd56..78354584af 100644 --- a/src/port/qsort.c +++ b/src/port/qsort.c @@ -4,6 +4,9 @@ #include "c.h" +/* + * NB: Callers should use qsort() instead of calling pg_qsort() directly. + */ #define ST_SORT pg_qsort #define ST_ELEMENT_TYPE_VOID #define ST_COMPARE_RUNTIME_POINTER -- 2.25.1 --AhhlLboLdkugWU4S Content-Type: text/x-diff; charset=iso-8859-1 Content-Disposition: attachment; filename="v8-0002-Introduce-efficient-overflow-aware-integer-compar.patch" Content-Transfer-Encoding: 8bit