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 1esqhV-000196-Hx for pgsql-general@arkaria.postgresql.org; Mon, 05 Mar 2018 14:02:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1esqhU-0006yP-2d for pgsql-general@arkaria.postgresql.org; Mon, 05 Mar 2018 14:02:48 +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 1esqhS-0006ha-CR for pgsql-general@lists.postgresql.org; Mon, 05 Mar 2018 14:02:46 +0000 Received: from mail.postgrespro.ru ([93.174.131.138]) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1esqhO-0002IM-6c for pgsql-general@lists.postgresql.org; Mon, 05 Mar 2018 14:02:45 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id 506B321C1CAE; Mon, 5 Mar 2018 17:02:41 +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 0CD6821C1CA5; Mon, 5 Mar 2018 17:02:41 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1520258561; bh=YQ3jON+kiI6ma5exZktcXe7ElYu1PS6vq0I5zTVct9w=; h=Subject:To:Cc:References:From:Date:In-Reply-To; b=SP8mN8dUflSO0h3ZBB191YwvQQKqd5lCSvOIztVq9l6PztA9wv7eo+ttNsxmcuxBq R6LIvjHwmU0EL+b6tg+byGck+sZPx0R0Iq0tf3vi1+LfVBx1osUdzGNgmOGq1r0CNg jWrJ6w655hcnk0Rz3BDaikoH7loXSxZgCLbvq0G4= Subject: Re: psql variables in the DO command To: Pavel Stehule Cc: pgsql-general References: <5bae153c-ade4-ad24-bd46-857993240cfd@postgrespro.ru> From: Pavel Luzanov Message-ID: Date: Mon, 5 Mar 2018 17:02:40 +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="------------F272DB9258AD0CB382CA5E38" 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. --------------F272DB9258AD0CB382CA5E38 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 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 --------------F272DB9258AD0CB382CA5E38 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit 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
--------------F272DB9258AD0CB382CA5E38--