| 796 | */ |
| 797 | |
| 798 | void |
| 799 | ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew, |
| 800 | uint64 operatorMemKB, |
| 801 | bool try_combined_hash_mem, |
| 802 | int parallel_workers, |
| 803 | size_t *space_allowed, |
| 804 | int *numbuckets, |
| 805 | int *numbatches, |
| 806 | int *num_skew_mcvs) |
| 807 | { |
| 808 | int tupsize; |
| 809 | double inner_rel_bytes; |
| 810 | size_t hash_table_bytes; |
| 811 | size_t bucket_bytes; |
| 812 | size_t max_pointers; |
| 813 | int nbatch = 1; |
| 814 | int nbuckets; |
| 815 | double dbuckets; |
| 816 | |
| 817 | /* Force a plausible relation size if no info */ |
| 818 | if (ntuples <= 0.0) |
| 819 | ntuples = 1000.0; |
| 820 | |
| 821 | /* |
| 822 | * Estimate tupsize based on footprint of tuple in hashtable... note this |
| 823 | * does not allow for any palloc overhead. The manipulations of spaceUsed |
| 824 | * don't count palloc overhead either. |
| 825 | */ |
| 826 | tupsize = ExecHashRowSize(tupwidth); |
| 827 | inner_rel_bytes = ntuples * tupsize; |
| 828 | |
| 829 | /* |
| 830 | * Compute in-memory hashtable size limit from GUCs. |
| 831 | */ |
| 832 | hash_table_bytes = operatorMemKB * 1024L; |
| 833 | |
| 834 | /* |
| 835 | * Parallel Hash tries to use the combined hash_mem of all workers to |
| 836 | * avoid the need to batch. If that won't work, it falls back to hash_mem |
| 837 | * per worker and tries to process batches in parallel. |
| 838 | */ |
| 839 | if (try_combined_hash_mem && parallel_workers > 0) |
| 840 | { |
| 841 | /* Careful, this could overflow size_t */ |
| 842 | double newlimit; |
| 843 | |
| 844 | /* CBDB_PARALLEL_FIXME: if we enable pg style parallel some day, we should reconsider it. */ |
| 845 | newlimit = (double) hash_table_bytes * (double) parallel_workers; |
| 846 | newlimit = Min(newlimit, (double) SIZE_MAX); |
| 847 | hash_table_bytes = (size_t) newlimit; |
| 848 | } |
| 849 | |
| 850 | *space_allowed = hash_table_bytes; |
| 851 | |
| 852 | /* |
| 853 | * If skew optimization is possible, estimate the number of skew buckets |
| 854 | * that will fit in the memory allowed, and decrement the assumed space |
| 855 | * available for the main hash table accordingly. |
no test coverage detected