pgForma...Beautify(3)
π NAME π
pgFormatter::Beautify β Library for pretty-printing SQL queries User Contributed Perl Documentation Β· manual page pgFormatter::Beautify(3)
okay so bestie, this is the library for making SQL look cute and readable. It pretty-prints queries. Thatβs the whole main-character job. β¨
β¨ VERSION β¨
Version 5.11
π SYNOPSIS π
This module can be used to reformat a given SQL query, and it can optionally anonymize parameters. π«Ά
The output can be either plain text, or it can be HTML with appropriate styles so that it can be displayed on a web page.
Example usage:
my $beautifier = pgFormatter::Beautify->new();
$beautifier->query( 'select a,b,c from d where e = f' );
$beautifier->beautify();
my $nice_txt = $beautifier->content();
$beautifier->format('html');
$beautifier->beautify();
my $nice_html = $beautifier->content();
$beautifier->format('html');
$beautifier->anonymize();
$beautifier->beautify();
my $nice_anonymized_html = $beautifier->content();
$beautifier->format();
$beautifier->beautify();
$beautifier->wrap_lines()
my $wrapped_txt = $beautifier->content();
πΈ FUNCTIONS πΈ
Hereβs the whole function roster, bestie. Every single one is invited to the party. π
π«Ά new
Generic constructor β it creates the object, sets defaults, and reads config from the given hash with options.
Takes options as hash. Following options are recognized:
breakβ the string used for linebreaks. Default is"\n".colorizeβ if set tofalse, CSS style will not be applied to html output. Itβs used internally to display errors in CGI mode withour style. πcommaβ set comma at beginning or end of a line in a parameter list.endβ put comma at end of the list (default)startβ put comma at beginning of the list
comma_breakβ add new-line after each comma inINSERTstatements.formatβ set beautify format to apply to the content (default:text).textβ output content as plain/text (command line mode default)htmlβ output text/html with CSS style applied to content (CGI mode default)
functionsβ list (arrayref) of strings that are function names.keywordsβ list (arrayref) of strings that are keywords.multilineβ use multi-line search for placeholder regex, seeplaceholder.no_commentsβ if set totrue, comments will be removed from query.no_groupingβ if set totrue, statements will not be grouped in a transaction, and an extra newline character will be added between statements like outside a transaction.placeholderβ use the specified regex to find code that must not be changed in the query.queryβ query to beautify.rulesβ hash of rules β uses rule semantics fromSQL::Beautify.spaceβ character(s) to be used as space for indentation.spacesβ how many spaces to use for indentation.uc_functionsβ what to do with function names:0β do not change1β change to lower case2β change to upper case3β change to Capitalized
separatorβ string used as dynamic code separator, default is single quote.uc_keywordsβ what to do with keywords β meaning of value like withuc_functions.uc_typesβ what to do with data types β meaning of value like withuc_functions.wrapβ wraps given keywords in pre- and post- markup. Specific docs inSQL::Beautify.format_typeβ try an other formatting.wrap_limitβ wrap queries at a certain length.wrap_afterβ number of column after which lists must be wrapped.wrap_commentβ apply wrapping to comments starting with--.numberingβ statement numbering as a comment before each query.redshiftβ add Redshift keywords (obsolete, use--extra-keyword).no_extra_lineβ do not add an extra empty line at end of the output.keep_newlineβ preserve empty line in plpgsql code.no_space_functionβ remove space before function call and open parenthesis.redundant_parenthesisβ do not eliminate redundant parenthesis in DML queries.vertical_alignβ vertically alignCREATE TABLEcolumn definitions.
For defaults, please check function set_defaults.
π«Ά query
Accessor to query string. Both reads:
$object->query()
, and writes:
$object->query( $something )
π«Ά content
Accessor to content of results. Must be called after $object->beautify().
This can be either plain text or html following the format asked by the client with the $object->format() method.
π«Ά highlight_code
Makes result html with styles set for highlighting.
π«Ά tokenize_sql
Splits input SQL into tokens.
Code lifted from SQL::Beautify
π«Ά _parse_create_table_column
Parse one single-line CREATE TABLE column definition into the parts needed by vertical alignment. A trailing comment is stored separately from the SQL. Returns undef for table constraints, embedded comments, multiline input, and definitions that do not contain both a column name and a data type.
This method only analyzes input. It does not change the formatted SQL.
π«Ά _render_sql_tokens
Render a list of SQL tokens as one compact, single-line SQL fragment.
This helper is intentionally limited to the fragments produced by _parse_create_table_column(). It preserves token order and only decides where spaces belong. It does not perform alignment or change keyword casing.
π«Ά _pad_right
Pad a string with spaces until it reaches the requested width.
The helper returns the original string unchanged when it is already at least as wide as the requested width. Alignment uses spaces deliberately: tabs would make the result depend on the tab width configured by the editor displaying the SQL.
π«Ά _align_create_table_column_keyword
Align a top-level keyword within the remainder of parsed CREATE TABLE columns.
For every matching row, the tokens before the keyword are treated as a prefix. Shorter prefixes are padded so the keyword starts in the same output column. Keywords nested inside parentheses or array brackets are ignored.
The method updates the rendered remainder stored in each matching row. It does not reorder tokens or affect rows that do not contain the requested keyword.
π«Ά _parenthesis_delta
Return the net parenthesis depth change for one SQL line.
The existing SQL tokenizer is reused so parentheses inside quoted strings and comments are not mistaken for structural parentheses.
π«Ά _create_table_start
Inspect one formatted line for the beginning of a regular CREATE TABLE body.
The return value is a three-element list: whether the line begins a supported CREATE TABLE statement, whether its opening parenthesis is present, and the net parenthesis depth on that line. When the opening parenthesis is absent it may occur on the following line.
CREATE TABLE AS, PARTITION OF, and typed-table forms are deliberately skipped because their parentheses do not necessarily contain ordinary column definitions.
π«Ά _align_create_table_body
Align eligible top-level lines from one already formatted CREATE TABLE body.
Only lines that begin and end at the table body's direct parenthesis depth are sent to the column-group aligner. This excludes continuation lines belonging to multiline CHECK expressions or other nested constructs while still allowing single-line type modifiers and function calls.
π«Ά _align_create_table_columns
Apply vertical column alignment to regular CREATE TABLE statements in a fully formatted SQL document.
The method is a final text-only rendering pass. It preserves all lines outside supported table bodies and delegates actual row formatting to the smaller parser, renderer, and group-alignment helpers.
π«Ά _align_create_table_column_comments
Append trailing comments at a shared output column.
The SQL portion of every supported column is measured after name, declaration, and DEFAULT alignment. Only rows containing comments receive padding, so rows without comments do not gain trailing whitespace. Unsupported lines are ignored when calculating the comment column.
π«Ά _align_create_table_column_group
Align a group of single-line CREATE TABLE column definitions.
The method aligns the column names and the beginning of each remainder, while preserving indentation, token order, trailing commas, and unsupported lines. It only returns rendered lines; it does not modify the formatter output.
π«Ά beautify
Beautify SQL.
After calling this function, $object->content() will contain nicely indented result.
Code lifted from SQL::Beautify
π«Ά _add_token
Add a token to the beautified string.
Code lifted from SQL::Beautify
π«Ά _over
Increase the indentation level.
Code lifted from SQL::Beautify
π«Ά _back
Decrease the indentation level.
Code lifted from SQL::Beautify
π«Ά _indent
Return a string of spaces according to the current indentation level and the spaces setting for indenting.
Code lifted from SQL::Beautify
π«Ά _new_line
Add a line break, but make sure there are no empty lines.
Code lifted from SQL::Beautify
π«Ά _next_token
Have a look at the token that's coming up next.
Code lifted from SQL::Beautify
π«Ά _next_token_skip_comment
Have a look at the token that's coming up next omitting comments.
Code lifted from SQL::Beautify
π«Ά _token
Get the next token, removing it from the list of remaining tokens.
Code lifted from SQL::Beautify
π«Ά _is_keyword
Check if a token is a known SQL keyword.
Code lifted from SQL::Beautify
π«Ά _is_type
Check if a token is a known SQL type.
π«Ά _is_comment
Check if a token is a SQL or C style comment.
π«Ά _is_function
Check if a token is a known SQL function.
Code lifted from SQL::Beautify and rewritten to check one long regexp instead of a lot of small ones.
π«Ά add_keywords
Add new keywords to highlight.
Code lifted from SQL::Beautify
π«Ά _re_from_list
Create compiled regexp from prefix, suffix and and a list of values to match.
π«Ά _refresh_functions_re
Refresh compiled regexp for functions.
π«Ά add_functions
Add new functions to highlight.
Code lifted from SQL::Beautify
π«Ά add_rule
Add new rules.
Code lifted from SQL::Beautify
π«Ά _get_rule
Find custom rule for a token.
Code lifted from SQL::Beautify
π«Ά _process_rule
Applies defined rule.
Code lifted from SQL::Beautify
π«Ά _is_constant
Check if a token is a constant.
Code lifted from SQL::Beautify
π«Ά _is_punctuation
Check if a token is punctuation.
π«Ά _generate_anonymized_string
Simply generate a random string, thanks to Perlmonks.
Returns original in certain cases which don't require anonymization, like timestamps, or intervals.
π«Ά anonymize
Anonymize litteral in SQL queries by replacing parameters with fake values.
π«Ά set_defaults
Sets defaults for newly created objects.
Currently defined defaults:
So here's the rest of the default-value lineup, still part of the FUNCTIONS section β every single one, no skips, because we don't do that here π
spaces => 4β four spaces, that's the vibespace => ' 'β one literal space characterbreak => "\n"β a newlineuc_keywords => 2uc_functions => 0uc_types => 1no_comments => 0no_grouping => 0placeholder => ''β empty string, yepmultiline => 0separator => ''β also emptycomma => 'end'β the stringendformat => 'text'colorize => 1format_type => 0wrap_limit => 0wrap_after => 0wrap_comment => 0no_extra_line => 0keep_newline => 0no_space_function => 0redundant_parenthesis => 0vertical_align => 0
That's the whole roster π twenty-three lil defaults, all accounted for.
π format
Sets the output format, bestie. Possible values are 'text' and 'html'.
Default is text output. And if the format is wrong, it returns 0 and just uses the default β so no chaos, it self-corrects π«Ά
π set_dicts
Sets all the various dictionaries β you know, the lists of keywords, functions, symbols, and the like.
It got moved into its own separate function so it can live at the very end of the module, which makes the rest of the code way easier to read. Main character organization, honestly β¨
π¦ _remove_dynamic_code
Internal function, used to hide dynamic code in plpgsql from the parser.
The original values get restored with _restore_dynamic_code() β it's giving "hide and seek but make it comp-sci" π
π§ _restore_dynamic_code
Internal function used to restore plpgsql dynamic code that was removed by _remove_dynamic_code().
The comeback arc we all deserved π
π _quote_operator
Internal function used to quote an operator with multiple characters so it gets tokenized as a single word.
The original values get restored with _restore_operator() β nothing stays quoted forever, periodt.
πΈ _restore_operator
Internal function used to restore the operator that was removed by _quote_operator().
Put it back, put it back π«Ά
π _quote_comment_stmt
Internal function used to replace a constant in a COMMENT statement so it gets tokenized as a single word.
The original values get restored with _restore_comment_stmt() β again, temporary era only.
β _restore_comment_stmt
Internal function used to restore the comment string that was removed by _quote_comment_stmt().
The comment said "I'll be back" and it meant it π
π₯ _remove_comments
Internal function used to remove comments in SQL code, and the reason is so cute β it just simplifies the work of wrap_lines.
Heads up though: those comments must be restored with _restore_comments() β don't leave them deleted, bestie π
π _restore_comments
Internal function used to restore comments in SQL code that was removed by _remove_comments().
Full circle moment, we love to see it β¨
π wrap_lines
Internal function used to wrap a line at a certain length.
Short, sweet, iconic, no notes π
π AUTHOR
pgFormatter is an original work from Gilles Darold β founder energy, we stan π
π BUGS
Please report any bugs or feature requests to: https://github.com/darold/pgFormatter/issues
(Yes, actually tell them β they want to know, fr fr π)
Β©οΈ COPYRIGHT
Copyright 2012-2026 Gilles Darold. All rights reserved.
π LICENSE
pgFormatter is free software distributed under the PostgreSQL Licence.
A modified version of the SQL::Beautify Perl Module is embedded in pgFormatter with copyright (C) 2009 by Jonas Kramer and is published under the terms of the Artistic License 2.0.
Okay that's the whole page, bestie β and shoutout to the footer for the lore: this one's fromperl v5.42.3, dated1980-01-01, and the page itself ispgForma...Beautify(3). Slay π β¨
π¦ where this page comes from
- nixpkgs unstable: perl5.42.3-pgformatter 5.11
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms