pglex

package
v1.169.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: Apache-2.0 Imports: 1 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package pglex reads PostgreSQL SQL text lexically: where its top-level statements end, and where a string literal, a quoted identifier, a dollar-quoted body or a comment starts and stops. It parses nothing; the application database kind's statement checks, its migration lint and the admin console's statement lists read SQL through it (#2410, #2427).

Index

Constants

View Source
const BuiltAtRunTime = "SQL built at run time"

BuiltAtRunTime is the object PasswordLiteral reports for SQL that is built at run time and gives a password (#2750): an EXECUTE of anything but one literal, or a dblink call with an argument that is not a literal, in a unit that names a role, user or group and PASSWORD. Such SQL cannot be read statically, so it is refused rather than passed.

View Source
const RedactedLiteral = "'[REDACTED]'"

RedactedLiteral is what RedactPasswords writes in place of a password literal: a string literal still, so the statement reads as SQL.

Variables

This section is empty.

Functions

func BuildsSQL added in v1.166.4

func BuildsSQL(unit string) bool

BuildsSQL reports whether unit runs SQL it builds at run time: an EXECUTE whose argument is anything but one literal (format(...), a concatenation, a variable), or a dblink or dblink_exec call with an argument that is not a literal. EXECUTE as a privilege (GRANT EXECUTE ON) or a trigger's EXECUTE FUNCTION is not one.

func FirstWord added in v1.153.2

func FirstWord(sql string) string

FirstWord is the lowercased first word of sql after whitespace, comments and opening parentheses: the keyword a statement is read by.

func IsEString

func IsEString(sql string, i int) bool

IsEString reports whether the quote at i opens an E'...' string, whose backslashes escape.

func IsIdentByte

func IsIdentByte(c byte) bool

IsIdentByte reports whether c may appear in an unquoted identifier.

func PasswordLiteral

func PasswordLiteral(stmt string) (object string, ok bool)

PasswordLiteral reports whether stmt, one statement, writes a credential: a role, user, group or user mapping given a PASSWORD other than NULL, in the statement itself, in a statement of a dollar-quoted body it carries (a DO block, a function's body), or in a string literal that reads as one once unquoted (a role given a password inside an EXECUTE literal, #2750). It returns the object the statement acts on ("CREATE ROLE"), or BuiltAtRunTime for a statement or body that builds SQL at run time naming a role and PASSWORD, which cannot be read. The migration review's password_literal rule and RedactPasswords both read a statement by it, so what the review refuses is what a stored copy has redacted.

func RedactPasswords

func RedactPasswords(sql string) string

RedactPasswords returns sql with every literal after the PASSWORD of a statement PasswordLiteral reports replaced by RedactedLiteral, and any other text as it is. Every literal after the PASSWORD is replaced, not only the first: what follows it (VALID UNTIL, a user mapping's later options) is read no further than that.

A statement that sets no password itself has each dollar-quoted body and string literal it carries read the same way, so a role created inside a DO block, a function's body or an EXECUTE '...' is redacted as one created at the top level; a string literal is written back quoted. A statement or body that builds SQL at run time naming a role and PASSWORD has every literal it holds replaced, since which one carries the password cannot be read (#2750).

func RunsConcurrently added in v1.153.2

func RunsConcurrently(sql string) bool

RunsConcurrently reports whether sql holds a CREATE, DROP or REINDEX statement with the word CONCURRENTLY outside its literals and comments, which PostgreSQL runs only outside a transaction block (#2490).

func SkipComment

func SkipComment(sql string, i int) int

SkipComment returns the index just past a comment starting at i, or i.

func SkipDollarQuoted

func SkipDollarQuoted(sql string, i int) int

SkipDollarQuoted skips $tag$ ... $tag$, or returns i when $ starts no dollar quote (a positional parameter, $1).

func SkipLexeme

func SkipLexeme(sql string, i int) int

SkipLexeme returns the index just past the literal, quoted identifier, dollar-quoted body or comment starting at i, or i when none starts there. One that never ends runs to the end of sql.

func SkipString

func SkipString(sql string, i int, backslash bool) int

SkipString skips a quoted run where a doubled quote is an escaped one and, in an E-string, a backslash escapes the next byte.

func Split

func Split(sql string) []string

Split cuts SQL into its top-level statements at the semicolons that end them, skipping a semicolon inside a string literal (with an E-string's backslash escapes), a quoted identifier, a dollar-quoted body (a function's, a DO block's), a comment, or a SQL-standard function body (BEGIN ATOMIC ... END, whose statements end in semicolons of their own). Each statement is trimmed; an empty one is dropped.

func StringLiterals added in v1.166.4

func StringLiterals(sql string) []string

StringLiterals returns the text each string literal in sql stands for once unquoted (a doubled quote read as one, and in an E'...' string an escaped quote too), in order: the SQL a body passes to EXECUTE or dblink_exec as a literal. A dollar-quoted body is not among them.

func Words added in v1.166.4

func Words(unit string) []string

Words is every word of unit in order, lower-cased, with the words of each literal it holds read in place: the SQL a unit builds at run time is in its literals, split across them and the code that joins them. A literal's words are read to maxBodyDepth.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL