问题
I know that there is a way to list all prepared statements for the current session by selecting all rows from the pg_prepared_statements
table, but is there a way to see all prepared statements for all active sessions? I think I'm looking for something like an administrator function, but I can't find anything like it in the docs.
回答1:
Nope. AFAIK prepared statements are local to a backend; other backends just don't know they exist. You'd need to modify the server to add additional inter-process communication to allow one backend to ask the others about prepared statements.
The backends initally appeared to share the same pg_prepared_statements
table storage, as:
SELECT relfilenode FROM pg_class WHERE relname = 'pg_prepared_statements';
returns the same relfilenode from different backends. I was surprised, as I thought prepared statements had no on-disk presence. If they were on disk I guess you could use the functions from the pageinspect contrib module to read the raw tuples or table pages. Visibility would be a problem; how would you know what was related to a dead/terminated backend, and what was valid?
I tried that, and found that pg_prepared_statements
is actually a view:
regress=# SELECT * FROM heap_page_items(get_raw_page('pg_prepared_statements', 1));
ERROR: cannot get raw page from view "pg_prepared_statements"
specifically a view over the pg_prepared_statement()
function. So there's nothing to inspect. It's backend internal.
This seems like one of those "... but why would you do that?" questions, which is often a sign that someone is asking about a solution they've envisioned to their real problem, rather than asking about their real problem.
So: Why do you want that? What's the real problem you're trying to solve?
来源:https://stackoverflow.com/questions/12159422/how-to-list-all-prepared-statements-for-all-active-sessions