Re: psql variables in the DO command

From: Pavel Luzanov <p(dot)luzanov(at)postgrespro(dot)ru>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: psql variables in the DO command
Date: 2018-03-05 14:22:02
Message-ID: e9c976bc-60b8-1bf0-428b-1fdc22596041@postgrespro.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ron Johnson 2018-03-05 14:23:12 Re: Requiring pass and database psql shell command
Previous Message Adrian Klaver 2018-03-05 14:22:01 Re: What is wrong with my pgadmin?