From 388b6824bb6083700fc47a401cbf62517c27809d Mon Sep 17 00:00:00 2001 From: Alexey Lesovsky Date: Fri, 15 Mar 2019 13:27:10 +0500 Subject: [PATCH] record/report: improve aligning and truncation of pg_stat_statements.query --- cmd/help.go | 3 ++- cmd/record/record.go | 1 + doc/Changelog | 4 ++++ lib/stat/pgstat.go | 12 +++++++++++- report/report.go | 10 ++++++++-- 5 files changed, 26 insertions(+), 4 deletions(-) diff --git a/cmd/help.go b/cmd/help.go index 9eb9114..a7a436f 100644 --- a/cmd/help.go +++ b/cmd/help.go @@ -105,6 +105,7 @@ Options: -c, --count number of stats samples to collect -f, --file file name where statistics to write to (default: pgcenter.stat.tar) -a, --append append statistics to file, instead of creating a new file + -t, --truncate maximum query length to record (default: 0, no limit) -1, --oneshot append single statistics snapshot and exit (alias for --append --interval 0 --count 1) General options: @@ -125,7 +126,7 @@ Options: -o, --order order values by column (default descending, use '+' sign before a column name for ascending order) -g, --grep filter values in specfied column (format: colname:filtertext) -l, --limit print only limited number of rows per sample (default: unlimited) - -t, --truncate maximum string size to print (default: 32) + -t, --truncate maximum string size to print (default: 32, 0 disables truncate) -i, --interval delta interval (default: 1s) Report options: diff --git a/cmd/record/record.go b/cmd/record/record.go index ec8ef88..adac5e4 100644 --- a/cmd/record/record.go +++ b/cmd/record/record.go @@ -40,6 +40,7 @@ func init() { CommandDefinition.Flags().Int32VarP(&opts.Count, "count", "c", -1, "number of stats samples to collect") CommandDefinition.Flags().StringVarP(&opts.OutputFile, "file", "f", defaultRecordFile, "file where stats are saved") CommandDefinition.Flags().BoolVarP(&opts.AppendFile, "append", "a", false, "append statistics to a file, instead of creating a new one") + CommandDefinition.Flags().IntVarP(&opts.TruncLimit, "truncate", "t", 0, "maximum query length to record (default: 0, no limit)") CommandDefinition.Flags().BoolVarP(&oneshot, "oneshot", "1", false, "append single statistics snapshot to file and exit") } diff --git a/doc/Changelog b/doc/Changelog index 9b55b14..5afcf3c 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -1,4 +1,8 @@ pgcenter (0.6.1) unstable; urgency=low + * improved aligning and query truncation + - record: records complete length of pg_stat_statements.query (be careful, it might produce high-volume stats file) + - record: add startup paramter for configuring truncation + - report: can use '--truncate 0', for disabling trucation only for pg_stat_statements.query (might produce an "eyes-bleeding" output) * reworked pg_stat_statements queries: - remove unnecessary GROUP BY, ORDER BY and aggregations - simplify usage of regexp_replace() diff --git a/lib/stat/pgstat.go b/lib/stat/pgstat.go index 256f6df..42f0c45 100644 --- a/lib/stat/pgstat.go +++ b/lib/stat/pgstat.go @@ -427,6 +427,8 @@ func (r *PGresult) Sort(key int, desc bool) { // SetAlign method aligns length of values depending of the columns width func (r *PGresult) SetAlign(widthes map[int]int, truncLimit int, dynamic bool) { + var lastColTruncLimit, lastColMaxWidth int + lastColTruncLimit = truncLimit truncLimit = utils.Max(truncLimit, colsTruncMinLimit) /* calculate max length of columns based on the longest value of the column */ @@ -459,7 +461,15 @@ func (r *PGresult) SetAlign(widthes map[int]int, truncLimit int, dynamic bool) { } // for last column set width using truncation limit case colidx == r.Ncols-1: - widthes[colidx] = truncLimit + // if truncation disabled, use width of the longest value, otherwise use the user-defined truncation limit + if lastColTruncLimit == 0 { + if lastColMaxWidth < valuelen { + lastColMaxWidth = valuelen + } + widthes[colidx] = lastColMaxWidth + } else { + widthes[colidx] = truncLimit + } // do nothing if length of value or column is less (or equal) than already specified width case aligningIsLengthLessOrEqualWidth(valuelen, colnamelen, widthes[colidx]): diff --git a/report/report.go b/report/report.go index f537e76..0773b62 100644 --- a/report/report.go +++ b/report/report.go @@ -150,7 +150,7 @@ func formatReport(d *stat.PGresult, opts *ReportOptions) { // align values for printing, use dynamic aligning if !opts.Context.Aligned { - d.SetAlign(opts.Context.ColsWidth, opts.TruncLimit, true) // we don't want truncate lines here, so just use high limit + d.SetAlign(opts.Context.ColsWidth, opts.TruncLimit, true) opts.Context.Aligned = true } } @@ -211,7 +211,13 @@ func printStatReport(d *stat.PGresult, opts ReportOptions, ts time.Time) (printe d.Result[rownum][colnum].String = d.Result[rownum][colnum].String[:width-1] + "~" } - fmt.Printf("%-*s", opts.Context.ColsWidth[i]+2, d.Result[rownum][colnum].String) + // last col with no truncation of not specified otherwise + if i != len(d.Cols) - 1 { + fmt.Printf("%-*s", opts.Context.ColsWidth[i]+2, d.Result[rownum][colnum].String) + } else { + fmt.Printf("%s", d.Result[rownum][colnum].String) + } + colnum++ }