pgsql: Avoid recursion when processing simple lists of AND'ed or OR'ed

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Avoid recursion when processing simple lists of AND'ed or OR'ed
Date: 2014-06-16 19:55:56
Message-ID: E1Wwd0W-00074H-UW@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Avoid recursion when processing simple lists of AND'ed or OR'ed clauses.

Since most of the system thinks AND and OR are N-argument expressions
anyway, let's have the grammar generate a representation of that form when
dealing with input like "x AND y AND z AND ...", rather than generating
a deeply-nested binary tree that just has to be flattened later by the
planner. This avoids stack overflow in parse analysis when dealing with
queries having more than a few thousand such clauses; and in any case it
removes some rather unsightly inconsistencies, since some parts of parse
analysis were generating N-argument ANDs/ORs already.

It's still possible to get a stack overflow with weirdly parenthesized
input, such as "x AND (y AND (z AND ( ... )))", but such cases are not
mainstream usage. The maximum depth of parenthesization is already
limited by Bison's stack in such cases, anyway, so that the limit is
probably fairly platform-independent.

Patch originally by Gurjeet Singh, heavily revised by me

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/2146f13408cdb85c738364fe8f7965209e08c6be

Modified Files
--------------
contrib/postgres_fdw/deparse.c | 3 -
src/backend/nodes/nodeFuncs.c | 8 +++
src/backend/nodes/outfuncs.c | 9 ---
src/backend/optimizer/prep/prepjointree.c | 6 +-
src/backend/optimizer/prep/prepqual.c | 13 ++--
src/backend/optimizer/util/clauses.c | 15 ++--
src/backend/parser/gram.y | 110 ++++++++++++++++++++---------
src/backend/parser/parse_clause.c | 21 +++---
src/backend/parser/parse_expr.c | 98 +++++++++++--------------
src/include/nodes/parsenodes.h | 3 -
src/include/nodes/primnodes.h | 8 +--
src/test/regress/expected/rules.out | 2 +-
12 files changed, 155 insertions(+), 141 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Heikki Linnakangas 2014-06-17 05:53:19 pgsql: Revert accidental change of WAL_DEBUG default.
Previous Message Bruce Momjian 2014-06-16 19:33:23 pgsql: Use type pgsocket for Windows pipe emulation socket calls