Skip to content

Commit cf1f323

Browse files
michaelpqCommitfest Bot
authored andcommitted
Replace callers of dynahash.h's my_log() by equivalent in pg_bitutils.h
All these callers use 4-byte signed integers for their variables, hence they do not require my_log2() maximum based on int64. This eases an upcoming removal of my_log2(). ExecChooseHashTableSize(), that calculates the number of buckets to use for hash tables, is now capped at 2^30, to keep pg_ceil_log2_32() on the same side. nodeAgg.c is already capped at 1024, and worker.c relies on the maximum number of subxacts.
1 parent 06473f5 commit cf1f323

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

src/backend/executor/nodeAgg.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@
267267
#include "utils/acl.h"
268268
#include "utils/builtins.h"
269269
#include "utils/datum.h"
270-
#include "utils/dynahash.h"
271270
#include "utils/expandeddatum.h"
272271
#include "utils/injection_point.h"
273272
#include "utils/logtape.h"
@@ -2115,7 +2114,7 @@ hash_choose_num_partitions(double input_groups, double hashentrysize,
21152114
npartitions = (int) dpartitions;
21162115

21172116
/* ceil(log2(npartitions)) */
2118-
partition_bits = my_log2(npartitions);
2117+
partition_bits = pg_ceil_log2_32(npartitions);
21192118

21202119
/* make sure that we don't exhaust the hash bits */
21212120
if (partition_bits + used_bits >= 32)

src/backend/executor/nodeHash.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include "executor/nodeHashjoin.h"
3737
#include "miscadmin.h"
3838
#include "port/pg_bitutils.h"
39-
#include "utils/dynahash.h"
4039
#include "utils/lsyscache.h"
4140
#include "utils/memutils.h"
4241
#include "utils/syscache.h"
@@ -340,7 +339,7 @@ MultiExecParallelHash(HashState *node)
340339
*/
341340
hashtable->curbatch = -1;
342341
hashtable->nbuckets = pstate->nbuckets;
343-
hashtable->log2_nbuckets = my_log2(hashtable->nbuckets);
342+
hashtable->log2_nbuckets = pg_ceil_log2_32(hashtable->nbuckets);
344343
hashtable->totalTuples = pstate->total_tuples;
345344

346345
/*
@@ -480,7 +479,7 @@ ExecHashTableCreate(HashState *state)
480479
&nbuckets, &nbatch, &num_skew_mcvs);
481480

482481
/* nbuckets must be a power of 2 */
483-
log2_nbuckets = my_log2(nbuckets);
482+
log2_nbuckets = pg_ceil_log2_32(nbuckets);
484483
Assert(nbuckets == (1 << log2_nbuckets));
485484

486485
/*
@@ -935,6 +934,13 @@ ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew,
935934
Assert(nbuckets > 0);
936935
Assert(nbatch > 0);
937936

937+
/*
938+
* Cap nbuckets, for power of 2 calculations. This maximum is safe
939+
* for pg_ceil_log2_32().
940+
*/
941+
if (nbuckets > PG_INT32_MAX / 2)
942+
nbuckets = PG_INT32_MAX / 2;
943+
938944
*numbuckets = nbuckets;
939945
*numbatches = nbatch;
940946
}
@@ -3499,7 +3505,7 @@ ExecParallelHashTableSetCurrentBatch(HashJoinTable hashtable, int batchno)
34993505
dsa_get_address(hashtable->area,
35003506
hashtable->batches[batchno].shared->buckets);
35013507
hashtable->nbuckets = hashtable->parallel_state->nbuckets;
3502-
hashtable->log2_nbuckets = my_log2(hashtable->nbuckets);
3508+
hashtable->log2_nbuckets = pg_ceil_log2_32(hashtable->nbuckets);
35033509
hashtable->current_chunk = NULL;
35043510
hashtable->current_chunk_shared = InvalidDsaPointer;
35053511
hashtable->batches[batchno].at_least_one_chunk = false;

src/backend/replication/logical/worker.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@
276276
#include "storage/procarray.h"
277277
#include "tcop/tcopprot.h"
278278
#include "utils/acl.h"
279-
#include "utils/dynahash.h"
280279
#include "utils/guc.h"
281280
#include "utils/inval.h"
282281
#include "utils/lsyscache.h"
@@ -5092,7 +5091,7 @@ subxact_info_read(Oid subid, TransactionId xid)
50925091
len = sizeof(SubXactInfo) * subxact_data.nsubxacts;
50935092

50945093
/* we keep the maximum as a power of 2 */
5095-
subxact_data.nsubxacts_max = 1 << my_log2(subxact_data.nsubxacts);
5094+
subxact_data.nsubxacts_max = 1 << pg_ceil_log2_32(subxact_data.nsubxacts);
50965095

50975096
/*
50985097
* Allocate subxact information in the logical streaming context. We need

0 commit comments

Comments
 (0)