Expand description
What the parser is never handed: input past MAX_INPUT_BYTES, and
input whose tree could be deep enough to exhaust the parse’s stack.
PostgreSQL’s parser bounds right-nested forms with bison’s own stack,
but not left-deep chains (a + b + …, … UNION …, … JOIN …), whose
tree is as deep as the chain; the C functions that write the tree out
recurse once per level. A lexical scan bounds both before parsing. It
reads quoting and comments as the server’s lexer does, so text inside a
literal or comment is never structure, and it over-counts everything
else.
Constants§
- MAX_
BRACKET_ DEPTH (,[andCASE … ENDlevels open at once.- MAX_
CHAIN_ DEPTH - An upper bound on the tree’s depth along any path (see
check). - MAX_
COMMENT_ DEPTH - Block comments open at once.
- MAX_
INPUT_ BYTES - The longest statement parsed. Within the other bounds a parse’s time and
memory grow linearly with its input, by up to about 6 µs and 4.3 KB a
byte; at this size the costliest measured shape, a list of short
left-deep operator chains (
SELECT 1, a&a&…&a, …), takes 0.5 s and 416 MB (release, Apple silicon), a tenth of the default parse timeout and two fifths of a process’s address space. - MAX_
OPERATOR_ RUN - Operator characters (
~!@#^&|?+-/%<>=) in a row outside comments and quoted text, and inside a block comment from a nested/on. The lexer reads a run again from each point where it puts text back: the operator rule after a trailing+or-, and the comment rule after each nested/, each time to the run's end. So such a run costs up to its length squared; with the cap each byte is read a few hundred times at most. A run inside a comment with no nested/` in it is read once. - MAX_
WARNED_ FORMS GLOBAL TEMPandGLOBAL TEMPORARY, which the grammar parses with a warning. Each warning counts the characters before it, so their cost grows with their number times the input’s length.
Functions§
- check
- Refuses, as
TooLarge, input past one of the bounds.