Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fGj9R-0008AF-JK for pgsql-general@arkaria.postgresql.org; Thu, 10 May 2018 10:50:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fGj9Q-0005o9-Ij for pgsql-general@arkaria.postgresql.org; Thu, 10 May 2018 10:50:20 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fGj9Q-0005nb-59 for pgsql-general@lists.postgresql.org; Thu, 10 May 2018 10:50:20 +0000 Received: from mail-wr0-x230.google.com ([2a00:1450:400c:c0c::230]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fGj9I-0004QT-SE for pgsql-general@postgresql.org; Thu, 10 May 2018 10:50:18 +0000 Received: by mail-wr0-x230.google.com with SMTP id p4-v6so1549427wrh.3 for ; Thu, 10 May 2018 03:50:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ocado.com; s=google; h=reply-to:subject:to:cc:references:from:organization:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=EqOcStZSMS+IiSKO0qN90pwEHHNhT2GficW2HA0GjRk=; b=qFMGtBlURb7EC6Q6K02sHNV8BpmJq1uO6ZPcNuc5OA0gy0CcowXFCstWQvev/GJgmf H1eM0UIl4jSeed4fbNS9lo7QusBc+q2isBTFoChjkV4DctSoKOcI9gHMh80HhmCdJdJj Ya6XTz9QS0NbHlL7IzxTdnOSELKO4PuIc+mgM+8jxWKlN9RBTVYjIJeF4ZsPI6sA8Fli /F8Tug7NC8VjyyTnw7FsB/eoShxQywGNhBabUccEc7jf7AXMZgDfyZsLpaZ3Bk9R0/pJ VJd1QAByFe5ijnJ7JRZIZDO92ZnaLwc8qYKtHgGewL5jS7lhePv0tFlAllBitbeCGJsb jWIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:subject:to:cc:references:from :organization:message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=EqOcStZSMS+IiSKO0qN90pwEHHNhT2GficW2HA0GjRk=; b=AfPWqYzyknxtNwWUUFaGQJd0sJVp5vmyfM/dk1y16qkUckyCjM2kIMuK4IBb+YW2x/ MDTuWjXtkkk6CUpkA3ryp/GiYZoIQQ0M72qYGdz7yO2BMH1FRcRf5J0QV5rt01sQgKLx /eY4l0mTZDajbVF5Ymyhk/A//Tvh2KDWbQpp9Fkg3VvHzwKzc+PSTA/O0p9PaHj9hD2w 5dLxxHB1u/3Tt+iueGni0RX9z9ASZ0/iznPzeDitPpgqfPhIhGXvE5QEgBItdIa8P2ar QUJDqFLnlWj2liX+FAtxLoh1UJalKWR+Z4+b9gXqX9TEg08vs9t2iXjrJV6LusUY0kX/ N8cg== X-Gm-Message-State: ALKqPwccGTTuVAP836gfwpyBytYQVQNFHvHp/k6Fc8IATdW6RAze7x01 qBxEwulcKjQ4D71KhM56a+u2aZHl54hU7ZtWbdJ0ArFQ20eqD9d2/AORAhVMSHHehIRQxHC0Vkd ltzaFZMKBK5oiqvXre31rDw== X-Google-Smtp-Source: AB8JxZpehy1tcxCq4/Ck7FadRGQ5AXVIDfW4qpwUIS5rURwmMYrKSTpwg9LSYXgH6TcF0l7wIfNvZA== X-Received: by 2002:adf:c54b:: with SMTP id s11-v6mr957738wrf.46.1525949410710; Thu, 10 May 2018 03:50:10 -0700 (PDT) Received: from [10.62.12.115] (apple.ocado.com. [91.206.0.35]) by smtp.gmail.com with ESMTPSA id y68-v6sm1086081wrb.91.2018.05.10.03.50.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 10 May 2018 03:50:10 -0700 (PDT) Reply-To: f.venchiarutti@ocado.com Subject: Re: How do I get the SQL statement in a trigger? To: Pavel Stehule , a <372660931@qq.com> Cc: pgsql-general References: From: Fabio Ugo Venchiarutti Organization: Ocado Technology Message-ID: Date: Thu, 10 May 2018 11:50:09 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Language: en-US Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Querying over pg_stat_activity with clauses in a trigger can be quite=20 expensive if your write transaction rate is high. You may want to look into the current_query() function documented at=20 https://www.postgresql.org/docs/current/static/functions-info.html On 10/05/18 11:44, Pavel Stehule wrote: >=20 >=20 > 2018-05-10 12:42 GMT+02:00 a <372660931@qq.com >= : >=20 > Thanks a lot, would you please be so kind to tell me more about what > is top level query?? >=20 >=20 > for example - if you run some queries from PLpgSQL functions (triggers),= =20 > then these queries are not top queries. >=20 > Regards >=20 > Pavel >=20 >=20 >=20 > ------------------=C2=A0Original message=C2=A0------------------ > *From:* "Pavel Stehule"__; > *Sendtime:* Thursday, May 10, 2018 6:38 PM > *To:* "a"<372660931@qq.com >; > *Cc:* "pgsql-general"__; > *Subject:* Re: How do I get the SQL statement in a trigger? >=20 > Hi >=20 > 2018-05-10 12:23 GMT+02:00 a <372660931@qq.com > >: >=20 > Hi I would like to write a trigger that recorded every sql > statement under the effected entry. So if it is possible to > retrieve the sql statement within a trigger?? >=20 >=20 > You can read a tom command from pg_stat_activity table >=20 > =C2=A0postgres=3D# select query from pg_stat_activity where pid =3D > pg_backend_pid(); > +------------------------------------------------------------------+ > |=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 query =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 | > +------------------------------------------------------------------+ > | select query from pg_stat_activity where pid =3D pg_backend_pid(); = | > +------------------------------------------------------------------+ > (1 row) >=20 > But this information is available only for top level query. >=20 > Regards >=20 > Pavel >=20 >=20 --=20 Regards Fabio Ugo Venchiarutti Data Services Department Ocado Technology --=20 Notice: =C2=A0This email is confidential and may contain copyright material= of=20 members of the Ocado Group. Opinions and views expressed in this message=20 may not necessarily reflect the opinions and views of the members of the=20 Ocado Group.=C2=A0 =C2=A0 If you are not the intended recipient, please notify us=20 immediately and delete all copies of this message. Please note that it is= =20 your responsibility to scan this message for viruses.=C2=A0 =C2=A0 Fetch and Sizzle=20 are trading names of Speciality Stores Limited and Fabled is a trading name= =20 of Marie Claire Beauty Limited, both members of the Ocado Group. =C2=A0 References to the =E2=80=9COcado Group=E2=80=9D are to Ocado Group plc (reg= istered in=20 England and Wales with number 7098618) and its subsidiary undertakings (as= =20 that expression is defined in the Companies Act 2006) from time to time. = =C2=A0 The registered office of Ocado Group plc is=C2=A0Buildings One & Two, Tride= nt=20 Place, Mosquito Way, Hatfield, Hertfordshire, AL10 9UL.