fix some line break logic advice2
authorRobert Haas <rhaas@postgresql.org>
Fri, 18 Jul 2025 19:14:47 +0000 (15:14 -0400)
committerRobert Haas <rhaas@postgresql.org>
Fri, 18 Jul 2025 19:14:47 +0000 (15:14 -0400)
contrib/pg_plan_advice/pgpa_output.c

index 1aca7fd6792981a636e463eb7d93c139b1c84793..1b660791baf9c3da8076654aa06cbaa40be04850 100644 (file)
@@ -55,10 +55,22 @@ pgpa_output_advice(StringInfo buf, pgpa_plan_walker_context *walker,
        ListCell   *lc;
        pgpa_output_context context;
 
+       /*
+        * If the user chooses to use EXPLAIN (PLAN_ADVICE) in an 80-column window
+        * from a psql client with default settings, psql will add one space to the
+        * left of the output and EXPLAIN will add two more to the left of the
+        * advice. Thus, lines of more than 77 characters will wrap. We set the
+        * wrap limit to 76 here so that the output won't reach all the way to the
+        * very last column of the terminal.
+        *
+        * Of course, this is fairly arbitrary set of assumptions, and one could
+        * well make an argument for a different wrap limit, or for a configurable
+        * one.
+        */
        memset(&context, 0, sizeof(pgpa_output_context));
        context.rt_identifiers = rt_identifiers;
        context.buf = buf;
-       context.wrap_column = 79;       /* XXX */
+       context.wrap_column = 76;
 
        /*
         * Put all the top-level scans for each strategy into a single list.
@@ -94,6 +106,7 @@ pgpa_output_advice(StringInfo buf, pgpa_plan_walker_context *walker,
                appendStringInfo(context.buf, "JOIN_ORDER(");
                pgpa_output_unrolled_join(&context, ujoin);
                appendStringInfoChar(context.buf, ')');
+               pgpa_maybe_linebreak(context.buf, context.wrap_column);
        }
 
        /*
@@ -134,6 +147,7 @@ pgpa_output_advice(StringInfo buf, pgpa_plan_walker_context *walker,
                        }
                }
                appendStringInfoChar(buf, ')');
+               pgpa_maybe_linebreak(context.buf, context.wrap_column);
        }
 
        /*
@@ -178,6 +192,7 @@ pgpa_output_advice(StringInfo buf, pgpa_plan_walker_context *walker,
                        }
                }
                appendStringInfoChar(buf, ')');
+               pgpa_maybe_linebreak(context.buf, context.wrap_column);
        }
 
        /* Sort query features into one list per query feature type. */
@@ -241,6 +256,7 @@ pgpa_output_advice(StringInfo buf, pgpa_plan_walker_context *walker,
                        }
                }
                appendStringInfoChar(buf, ')');
+               pgpa_maybe_linebreak(context.buf, context.wrap_column);
        }
 }
 
@@ -458,6 +474,7 @@ pgpa_maybe_linebreak(StringInfo buf, int wrap_column)
        enlargeStringInfo(buf, 1);
        memmove(&buf->data[save_cursor] + 1, &buf->data[save_cursor],
                        buf->len - save_cursor);
-       buf->len++;
+       ++buf->cursor;
+       buf->data[++buf->len] = '\0';
        buf->data[save_cursor] = '\n';
 }