debugging code that shows it doesn't work
authorRobert Haas <rhaas@postgresql.org>
Wed, 21 May 2025 20:17:31 +0000 (16:17 -0400)
committerRobert Haas <rhaas@postgresql.org>
Wed, 21 May 2025 20:17:31 +0000 (16:17 -0400)
this prints out unrolled joins in a fairly raw format, and the result
is we can see we're not unrolling anything

contrib/pg_plan_advice/pg_plan_advice.c
contrib/pg_plan_advice/pgpa_join.c
contrib/pg_plan_advice/pgpa_join.h

index 4c867a136940590e407fb9669806eab10e586ddb..687211b17d640c83590297d632e8a96447a1e1da 100644 (file)
@@ -49,8 +49,21 @@ static void
 pgpa_check_plan(PlannedStmt *pstmt)
 {
        pgpa_plan_walker_context context;
+       StringInfoData  buf;
+       ListCell   *lc;
 
        memset(&context, 0, sizeof(pgpa_plan_walker_context));
        context.pstmt = pstmt;
        pgpa_plan_walker(&context, pstmt->planTree, NULL);
+
+       initStringInfo(&buf);
+       foreach(lc, context.unrolled_joins)
+       {
+               pgpa_unrolled_join *join = lfirst(lc);
+
+               appendStringInfoChar(&buf, ' ');
+               pgpa_debug_out_unrolled_join(&buf, join);
+       }
+
+       elog(WARNING, "unrolled joins:%s", buf.data);
 }
index 03f3a2c9b3371d7681d51dda657c51219f7fc286..ce136853f327d837109165943e4d25764915269e 100644 (file)
@@ -19,6 +19,11 @@ static pgpa_join_strategy pgpa_decompose_join(Plan *plan,
                                                                                          Plan **realinner,
                                                                                          Plan **realouter);
 
+static void pgpa_debug_out_join_member(StringInfo buf,
+                                                                          pgpa_join_member *member);
+static char *pgpa_cstring_join_clump_strategy(pgpa_join_clump_strategy strategy);
+static char *pgpa_cstring_join_strategy(pgpa_join_strategy strategy);
+
 /*
  * Should a given plan node be represented by a pgpa_unrolled_join, a
  * pgpa_clumped_join, or neither?
@@ -281,3 +286,93 @@ pgpa_decompose_join(Plan *plan, Plan **realinner, Plan **realouter)
        *realouter = outerplan;
        return strategy;
 }
+
+void
+pgpa_debug_out_clumped_join(StringInfo buf, pgpa_clumped_join *clump)
+{
+       char   *cstrategy;
+       int             rti = -1;
+       bool    first = true;
+
+       cstrategy = pgpa_cstring_join_clump_strategy(clump->strategy);
+       appendStringInfo(buf, "%s(", cstrategy);
+       while ((rti = bms_next_member(clump->relids, rti)) >= 0)
+       {
+               if (first)
+               {
+                       first = false;
+                       appendStringInfo(buf, "%d", rti);
+               }
+               else
+                       appendStringInfo(buf, " %d", rti);
+       }
+       appendStringInfoChar(buf, ')');
+}
+
+void
+pgpa_debug_out_unrolled_join(StringInfo buf, pgpa_unrolled_join *join)
+{
+       appendStringInfoChar(buf, '(');
+
+       pgpa_debug_out_join_member(buf, &join->outer);
+
+       for (int k = 0; k < join->ninner; ++k)
+       {
+               char   *cstrategy;
+
+               cstrategy = pgpa_cstring_join_strategy(join->strategy[k]);
+               appendStringInfo(buf, " %s ", cstrategy);
+               pgpa_debug_out_join_member(buf, &join->inner[k]);
+       }
+
+       appendStringInfoChar(buf, ')');
+}
+
+static void
+pgpa_debug_out_join_member(StringInfo buf, pgpa_join_member *member)
+{
+       if (member->clump_join != NULL)
+               pgpa_debug_out_clumped_join(buf, member->clump_join);
+       else if (member->unrolled_join != NULL)
+               pgpa_debug_out_unrolled_join(buf, member->unrolled_join);
+       else
+               appendStringInfo(buf, "%d", member->rti);
+}
+
+static char *
+pgpa_cstring_join_clump_strategy(pgpa_join_clump_strategy strategy)
+{
+       switch (strategy)
+       {
+               case JSTRAT_CLUMP_DEGENERATE:
+                       return "DEGENERATE";
+               case JSTRAT_CLUMP_FOREIGN:
+                       return "FOREIGN";
+               case JSTRAT_CLUMP_PARTITIONWISE:
+                       return "PARTITIONWISE";
+       }
+
+       Assert(false);
+}
+
+static char *
+pgpa_cstring_join_strategy(pgpa_join_strategy strategy)
+{
+       switch (strategy)
+       {
+               case JSTRAT_MERGE_JOIN_PLAIN:
+                       return "MERGE_JOIN_PLAIN";
+               case JSTRAT_MERGE_JOIN_MATERIALIZE:
+                       return "MERGE_JOIN_MATERIALIZE";
+               case JSTRAT_NESTED_LOOP_PLAIN:
+                       return "NESTED_LOOP_PLAIN";
+               case JSTRAT_NESTED_LOOP_MATERIALIZE:
+                       return "NESTED_LOOP_MATERIALIZE";
+               case JSTRAT_NESTED_LOOP_MEMOIZE:
+                       return "NESTED_LOOP_MEMOIZE";
+               case JSTRAT_HASH_JOIN:
+                       return "HASH_JOIN";
+       }
+
+       Assert(false);
+}
index 59336ee988b39a27bb6c6ed997f189a11296e578..a8f82deb3ad8e5340c08f95f4bb3c3d142b96260 100644 (file)
@@ -143,4 +143,9 @@ extern pgpa_unrolled_join *pgpa_build_unrolled_join(PlannedStmt *pstmt,
                                                                                                        pgpa_join_unroller *join_unroller);
 extern void pgpa_destroy_join_unroller(pgpa_join_unroller *join_unroller);
 
+extern void pgpa_debug_out_clumped_join(StringInfo buf,
+                                                                               pgpa_clumped_join *clump);
+extern void pgpa_debug_out_unrolled_join(StringInfo buf,
+                                                                                pgpa_unrolled_join *join);
+
 #endif