Re: Is it possible to get query_string value in an event trigger?

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Jeremy Finzel <finzelj(at)gmail(dot)com>
Cc: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Is it possible to get query_string value in an event trigger?
Date: 2017-05-24 12:52:17
Message-ID: CA+TgmoazVSSYkKp6qpGZPkw7nmcPSt4koPHt_9x_XFupLuTF+g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

:On Mon, May 22, 2017 at 2:18 PM, Jeremy Finzel <finzelj(at)gmail(dot)com> wrote:
> Hello. Is it possible to get the currently executing query in an event
> trigger, for example, a create table event trigger function firing after
> ddl_command_end, aside from checking pg_stat_activity for the current
> process?
>
> When I am debugging the source code, after executing a statement I can see
> the variable query_string which has the entire SQL statement, however, I
> would be very interested if it's possible to get this query_string value in
> a c function called during an event trigger, for a given process, or if by
> that time the memory has been freed and/or it's just not possible for some
> other reason to get the query string of a particular process.
>
> Any thoughts much appreciated.

I don't think there's a totally reliable way to do it today. You
could look at debug_query_string, but that's the toplevel query
string, not necessarily the query string that fired the event trigger.
It looks like ProcessUtilitySlow() has the query_string, but it's not
passed down to EventTriggerDDLCommandStart(),
EventTriggerDDLCommandEnd(), EventTriggerSQLDrop(), or
EventTriggerTableRewrite(). In the first three cases, it seems like
that would be pretty easy to change, but the last one looks harder.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Andres Freund 2017-05-24 13:04:41 Re: [HACKERS] Concurrent ALTER SEQUENCE RESTART Regression
Previous Message Robert Haas 2017-05-24 12:44:32 Re: Broken hint bits (freeze)