OSDN Git Service

Make HashJoin hint more coercive.
authorKyotaro Horiguchi <horikyoga.ntt@gmail.com>
Thu, 29 Oct 2020 00:59:35 +0000 (09:59 +0900)
committerKyotaro Horiguchi <horikyoga.ntt@gmail.com>
Thu, 29 Oct 2020 12:12:16 +0000 (21:12 +0900)
Even with HashJoin hint, hash joins may be rejected by planner if hash
table for the inner-rel is estimated too large.  Make HashJoin hint
more coercive by temporarily increasing work_mem. This change affects
only join-searching so no significant side-effects are expected.

pg_hint_plan.c

index 9fe17db..9378c75 100644 (file)
@@ -2770,6 +2770,25 @@ set_join_config_options(unsigned char enforce_mask, GucContext context)
        SET_CONFIG_OPTION("enable_nestloop", ENABLE_NESTLOOP);
        SET_CONFIG_OPTION("enable_mergejoin", ENABLE_MERGEJOIN);
        SET_CONFIG_OPTION("enable_hashjoin", ENABLE_HASHJOIN);
+
+       /*
+        * Hash join may be rejected for the reason of estimated memory usage. Try
+        * getting rid of that limitation. This change on work_mem is reverted just
+        * after searching join path so no suginificant side-effects are expected.
+        */
+       if (enforce_mask == ENABLE_HASHJOIN)
+       {
+               char                    buf[32];
+
+               /* See final_cost_hashjoin(). */
+               if (work_mem < MAX_KILOBYTES)
+               {
+                       snprintf(buf, sizeof(buf), UINT64_FORMAT, (uint64)MAX_KILOBYTES);
+                       set_config_option_noerror("work_mem", buf,
+                                                                         context, PGC_S_SESSION, GUC_ACTION_SAVE,
+                                                                         true, ERROR);
+               }
+       }
 }
 
 /*