Is there a way to figure out which statement from the block is currently running in Postgres? (Even extra extensions or tracing might be an option)
Below is the quick way to reproduce but in real scenario the block is way more complicated.
do
$$
declare
x int;
c char;
d int := 3;
begin
select pg_sleep(d), 11112 into c, x;
select pg_sleep(d), 11113 into c, x;
select pg_sleep(d), 11114 into c, x;
end
$$;
pg_stat_statements
/ pg_stat_activity
show only the main block, not specific queries.
2
Answers
The simple solution is to throw in, at positions you want to know …
Obviously only visible for the same session.
auto_explain
You can achieve that for all statements without altering the code block using
auto_explain
. It’s a bit heavy-handed because it also reports the query plan for each. (Very short for the given example, but may get quite verbose for complex queries.) But I am not aware of a simpler way. See:Demo:
Technically, you can just flip
debug_print_plan
on and it’ll do exactly that:Problem is, that’ll keep throwing whole parse trees at you. While those do hold everything about the currently processed statement, I wouldn’t expect anyone to prefer or even tolerate that format if all you wanted was to see a checkpoint, the line it’s on:
You can see
pg_sleep()
only as itsoid
under:funcid 2626
a few miles into this (this isn’t all it spewed out):As already suggested by @Erwin Brandstetter, it’s best to equip your code with
RAISE
at adequate message levels.