Skip to content

Commit de6c439

Browse files
committed
Limit overall indentation in rule/view dumps.
Continuing to indent no matter how deeply nested we get doesn't really do anything for readability; what's worse, it results in O(N^2) total whitespace, which can become a performance and memory-consumption issue. To address this, once we get past 40 characters of indentation, reduce the indentation step distance 4x, and also limit the maximum indentation by reducing it modulo 40. This latter choice is a bit weird at first glance, but it seems to preserve readability better than a simple cap would do. Back-patch to 9.3, because since commit 62e6664 the performance issue is a hazard for pg_dump. Greg Stark and Tom Lane
1 parent 164acbe commit de6c439

File tree

2 files changed

+140
-116
lines changed

2 files changed

+140
-116
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
#define PRETTYINDENT_JOIN 4
7070
#define PRETTYINDENT_VAR 4
7171

72+
#define PRETTYINDENT_LIMIT 40 /* wrap limit */
73+
7274
/* Pretty flags */
7375
#define PRETTYFLAG_PAREN 1
7476
#define PRETTYFLAG_INDENT 2
@@ -6258,14 +6260,36 @@ appendContextKeyword(deparse_context *context, const char *str,
62586260

62596261
if (PRETTY_INDENT(context))
62606262
{
6263+
int indentAmount;
6264+
62616265
context->indentLevel += indentBefore;
62626266

62636267
/* remove any trailing spaces currently in the buffer ... */
62646268
removeStringInfoSpaces(buf);
62656269
/* ... then add a newline and some spaces */
62666270
appendStringInfoChar(buf, '\n');
6267-
appendStringInfoSpaces(buf,
6268-
Max(context->indentLevel, 0) + indentPlus);
6271+
6272+
if (context->indentLevel < PRETTYINDENT_LIMIT)
6273+
indentAmount = Max(context->indentLevel, 0) + indentPlus;
6274+
else
6275+
{
6276+
/*
6277+
* If we're indented more than PRETTYINDENT_LIMIT characters, try
6278+
* to conserve horizontal space by reducing the per-level
6279+
* indentation. For best results the scale factor here should
6280+
* divide all the indent amounts that get added to indentLevel
6281+
* (PRETTYINDENT_STD, etc). It's important that the indentation
6282+
* not grow unboundedly, else deeply-nested trees use O(N^2)
6283+
* whitespace; so we also wrap modulo PRETTYINDENT_LIMIT.
6284+
*/
6285+
indentAmount = PRETTYINDENT_LIMIT +
6286+
(context->indentLevel - PRETTYINDENT_LIMIT) /
6287+
(PRETTYINDENT_STD / 2);
6288+
indentAmount %= PRETTYINDENT_LIMIT;
6289+
/* scale/wrap logic affects indentLevel, but not indentPlus */
6290+
indentAmount += indentPlus;
6291+
}
6292+
appendStringInfoSpaces(buf, indentAmount);
62696293

62706294
appendStringInfoString(buf, str);
62716295

0 commit comments

Comments
 (0)