Make pg_get_expr() more bulletproof.
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 9 Jan 2022 17:43:09 +0000 (12:43 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 9 Jan 2022 17:43:09 +0000 (12:43 -0500)
commit6867f963e319934cbdafeb4bd5beaea5797c7be2
treec055dadf62dfd6b32ad9b631dcd3065e30acd8ce
parent96a6f11c06252b046ca9a6ee1f581a2f5d599301
Make pg_get_expr() more bulletproof.

Since this function is defined to accept pg_node_tree values, it could
get applied to any nodetree that can appear in a cataloged pg_node_tree
column.  Some such cases can't be supported --- for example, its API
doesn't allow providing referents for more than one relation --- but
we should try to throw a user-facing error rather than an internal
error when encountering such a case.

In support of this, extend expression_tree_walker/mutator to be sure
they'll work on any such node tree (which basically means adding
support for relpartbound node types).  That allows us to run pull_varnos
and check for the case of multiple relations before we start processing
the tree.  The alternative of changing the low-level error thrown for an
out-of-range varno isn't appealing, because that could mask actual bugs
in other usages of ruleutils.

Per report from Justin Pryzby.  This is basically cosmetic, so no
back-patch.

Discussion: https://postgr.es/m/20211219205422.GT17618@telsasoft.com
src/backend/nodes/nodeFuncs.c
src/backend/optimizer/util/var.c
src/backend/utils/adt/ruleutils.c