If I am reading the code in utils/activity/backend_status.c correctly, I not only see where it is happening (pgstat_report_activity), but why it is happening as well. There is definitely a race condition as we know what the query is before we have a chance to parse it and generate a query identifier for it. I'm not sure of the solution - we cannot exclude null query identifiers from the output, or callers will see the backend disappear briefly! We cannot avoid setting it to 0, as then we could have a query != query_id. We could store the query string somewhere and only reveal it once we have the identifier, but that's also tricky because we risk not setting it (and because we also set other things at the same time as the name, which would then be further disconnected). But maybe its the best option? I do agree this is a bug (and like Srinath, I can duplicate easily).
From:
PG Bug reporting form Date: Subject:
BUG #19034: Recursive function with sql_body can replace an existing function but can not be created on it's own