Fix plancache refcount leak after error in ExecuteQuery.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 16 Jun 2021 23:30:17 +0000 (19:30 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 16 Jun 2021 23:30:17 +0000 (19:30 -0400)
commit131ea3e908d3c97a2fe1ab25cce5046dd5cb905f
tree47230cb956b2c74b3fca3aaf9cea17313e6dbb0c
parent99cea49d6525e30bc3768e4ffb95377e7584dea1
Fix plancache refcount leak after error in ExecuteQuery.

When stuffing a plan from the plancache into a Portal, one is
not supposed to risk throwing an error between GetCachedPlan and
PortalDefineQuery; if that happens, the plan refcount incremented
by GetCachedPlan will be leaked.  I managed to break this rule
while refactoring code in 9dbf2b7d7.  There is no visible
consequence other than some memory leakage, and since nobody is
very likely to trigger the relevant error conditions many times
in a row, it's not surprising we haven't noticed.  Nonetheless,
it's a bug, so rearrange the order of operations to remove the
hazard.

Noted on the way to looking for a better fix for bug #17053.
This mistake is pretty old, so back-patch to all supported
branches.
src/backend/commands/prepare.c