In my opinion it is a bug. Documentation clearly states that DISCARD ALL "resets the session to its initial state", which is not true. So either it's a documentation error and DISCARD ALL isn't meant to completely reset the state, or it's meant to completely reset the state and then it's a bug in its implementation (whether it is a missing feature or bug in existing one). I agree that Implementing a new user-exposed command and then calling it within DISCARD ALL is probably the right solution to the problem.
Generally inside Postgres are more internal counters that ensure some unique numbers or names, that are not reset by DISCARD ALL. DISCARD ALL is synonym for
CLOSE ALL;
SET SESSION AUTHORIZATION DEFAULT;
RESET ALL;
DEALLOCATE ALL;
UNLISTEN *;
SELECT pg_advisory_unlock_all();
DISCARD PLANS;
DISCARD TEMP;
DISCARD SEQUENCES;
other features not promised. It is hard to say if unique numbers should be resetted too. There are two reasons - possibly performance impact, and it can be hard to implement it. There is not any list of these counters (some counters are inside extensions). It can be better explained in doc.
Bug reference: 18694 Logged by: David Pavlíček Email address: pavlicek.david@gmail.com PostgreSQL version: 13.13 Operating system: Linux Description:
In my opinion, the DISCARD ALL command should reset the internal execution counters of plpgsql functions.
To my knowledge there is no user-exposed way to reset those counters. Thus discard all cannot reset them. It is a missing feature, not a bug. One that I agree makes sense to add.