The following bug has been logged on the website:
Bug reference: 18694
Logged by: David Pavlíček
Email address: pavlicek.david@gmail.com
PostgreSQL version: 13.13
Operating system: Linux
Description:
Recently, I encountered a issue where execution plans for SQL queries within
plpgsql functions differed significantly in a pooled database session, even
though after each call the session state was cleared using the DISCARD ALL
command. As per documentation, DISCARD ALL resets the session to its initial
state, which is probably not true for plpgsql exec counters. When
considering that execution plans for internal queries is generated based on
number of execution within single session (custom vs generic), this can have
huge impact on how the functions behaves in combination with connection
pooler, whether central one like pgbouncer or those built into client
libraries. In my opinion, the DISCARD ALL command should reset the internal
execution counters of plpgsql functions.