Skip to content

Commit

Permalink
record/report: improve aligning and truncation of pg_stat_statements.…
Browse files Browse the repository at this point in the history
…query
  • Loading branch information
lesovsky committed Mar 15, 2019
1 parent f9b1499 commit 388b682
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
3 changes: 2 additions & 1 deletion cmd/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
1 change: 1 addition & 0 deletions cmd/record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
4 changes: 4 additions & 0 deletions doc/Changelog
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
12 changes: 11 additions & 1 deletion lib/stat/pgstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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]):

Expand Down
10 changes: 8 additions & 2 deletions report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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++
}

Expand Down

0 comments on commit 388b682

Please sign in to comment.