Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1esr0D-0002fe-GM for pgsql-general@arkaria.postgresql.org; Mon, 05 Mar 2018 14:22:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1esr0B-00025g-Nk for pgsql-general@arkaria.postgresql.org; Mon, 05 Mar 2018 14:22:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1esr0B-00025X-8j for pgsql-general@lists.postgresql.org; Mon, 05 Mar 2018 14:22:07 +0000 Received: from mail.postgrespro.ru ([93.174.131.138]) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1esr08-0002iI-2n for pgsql-general@lists.postgresql.org; Mon, 05 Mar 2018 14:22:06 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id 4352221C1CB6; Mon, 5 Mar 2018 17:22:03 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=x tagged_above=-99 required=4 WHITELISTED tests=[] autolearn=unavailable Received: from [192.168.27.41] (gw.postgrespro.ru [93.174.131.141]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id 0C55F21C1CB3; Mon, 5 Mar 2018 17:22:02 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1520259723; bh=Y74/BwokNQDQC62lXtjGyKMBXg+iBwAwEO+JZO3jyNg=; h=Subject:From:To:Cc:References:Date:In-Reply-To; b=CGZTCEVrsYdjzrAHVeEY9YJ1ddMAvOSWvXzwxqbYkdcbj3I+B3B36ApXP2H+ZgoUN QSk1cEMDOqK+dY+gWmxJ703DZTv03gBo9AhSp7BwoLSVZ2rY4LHKsigtwyW01Z9aM1 nL0IsVejMScxXQQWyXmKOFXt7LtncB7uuUjC+yAk= Subject: Re: psql variables in the DO command From: Pavel Luzanov To: Pavel Stehule Cc: pgsql-general References: <5bae153c-ade4-ad24-bd46-857993240cfd@postgrespro.ru> Message-ID: Date: Mon, 5 Mar 2018 17:22:02 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------966ABE56BC34F5A5A8C253EB" Content-Language: ru-RU List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------966ABE56BC34F5A5A8C253EB Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Another possible, but inconvenient workaround - constructing the right string before execution: postgres=# \set var 'Hello, World!' postgres=# \set cmd '$$begin raise notice ''%'', ' :'var' '; end;$$;' postgres=# do :cmd; NOTICE:  Hello, World! DO ----- Pavel Luzanov Postgres Professional: http://www.postgrespro.com The Russian Postgres Company On 05.03.2018 17:02, Pavel Luzanov wrote: > On 05.03.2018 16:56, Pavel Stehule wrote: >> >>> >>> I can't use psql variable in the DO command. Is it >>> intentional behavior? >>> >>> >>> yes. psql variables living on client side, and are not >>> accessible from server side . DO command is executed on server side. >> >> But SELECT command also executed on a server side )) >> I thought that the command is sent to the server after variable's >> replacement. >> >> >> The psql variables are injected into SQL string before execution >> (before SQL string is sent to server). But this injection is disabled >> inside strings - and body of DO command is passed as string. > Yes, now I understand this. But at first glance this is not an obvious > behavior. > > ----- > Pavel Luzanov > Postgres Professional:http://www.postgrespro.com > The Russian Postgres Company --------------966ABE56BC34F5A5A8C253EB Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit Another possible, but inconvenient workaround - constructing the right string before execution:

postgres=# \set var 'Hello, World!'
postgres=# \set cmd '$$begin raise notice ''%'', ' :'var' '; end;$$;'
postgres=# do :cmd;
NOTICE:  Hello, World!
DO

-----
Pavel Luzanov
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company
On 05.03.2018 17:02, Pavel Luzanov wrote:
On 05.03.2018 16:56, Pavel Stehule wrote:

I can't use psql variable in the DO command. Is it intentional behavior?

yes. psql variables living on client side, and are not accessible from server side . DO command is executed on server side.

But SELECT command also executed on a server side ))
I thought that the command is sent to the server after variable's replacement.

The psql variables are injected into SQL string before execution (before SQL string is sent to server). But this injection is disabled inside strings - and body of DO command is passed as string.
Yes, now I understand this. But at first glance this is not an obvious behavior.

-----
Pavel Luzanov
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company

--------------966ABE56BC34F5A5A8C253EB--