pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
psql variables in the DO command
11+ messages / 2 participants
[nested] [flat]

* psql variables in the DO command
@ 2018-03-05 13:13  Pavel Luzanov <p.luzanov@postgrespro.ru>
  0 siblings, 1 reply; 11+ messages in thread

From: Pavel Luzanov @ 2018-03-05 13:13 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Hello,

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

postgres=# \set var 'Hello, World!'

postgres=# do $$begin raise notice '%', :'var'; end;$$;

ERROR:  syntax error at or near ":"

LINE 1: do $$begin raise notice '%', :'var'; end;$$;

                                      ^


-- 

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





^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: psql variables in the DO command
@ 2018-03-05 13:42  Pavel Stehule <pavel.stehule@gmail.com>
  parent: Pavel Luzanov <p.luzanov@postgrespro.ru>
  0 siblings, 1 reply; 11+ messages in thread

From: Pavel Stehule @ 2018-03-05 13:42 UTC (permalink / raw)
  To: Pavel Luzanov <p.luzanov@postgrespro.ru>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>

Hi

2018-03-05 14:13 GMT+01:00 Pavel Luzanov <p.luzanov@postgrespro.ru>:

> Hello,
>
> 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.

you can copy psql variables to GUC variables by set_config function, and
then on server side use current_setting function for getting the content.

Regards

Pavel



>
> postgres=# \set var 'Hello, World!'
>
> postgres=# do $$begin raise notice '%', :'var'; end;$$;
>
> ERROR:  syntax error at or near ":"
>
> LINE 1: do $$begin raise notice '%', :'var'; end;$$;
>
>                                      ^
>
>
> --
>
> -----
> Pavel Luzanov
> Postgres Professional: http://www.postgrespro.com
> The Russian Postgres Company
>
>
>

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: psql variables in the DO command
@ 2018-03-05 13:52  Pavel Luzanov <p.luzanov@postgrespro.ru>
  parent: Pavel Stehule <pavel.stehule@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Pavel Luzanov @ 2018-03-05 13:52 UTC (permalink / raw)
  To: Pavel Stehule <pavel.stehule@gmail.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>

On 05.03.2018 16:42, 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.

>
> you can copy psql variables to GUC variables by set_config function, 
> and then on server side use current_setting function for getting the 
> content.
Yes, I know about workarounds.


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

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: psql variables in the DO command
@ 2018-03-05 13:56  Pavel Stehule <pavel.stehule@gmail.com>
  parent: Pavel Luzanov <p.luzanov@postgrespro.ru>
  0 siblings, 1 reply; 11+ messages in thread

From: Pavel Stehule @ 2018-03-05 13:56 UTC (permalink / raw)
  To: Pavel Luzanov <p.luzanov@postgrespro.ru>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>

2018-03-05 14:52 GMT+01:00 Pavel Luzanov <p.luzanov@postgrespro.ru>:

> On 05.03.2018 16:42, 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.

postgres=# \set xxx ahoj
postgres=# select ':xxx';
+----------+
| ?column? |
+----------+
| :xxx     |
+----------+
(1 row)


Regards

Pavel


>
>
> you can copy psql variables to GUC variables by set_config function, and
> then on server side use current_setting function for getting the content.
>
> Yes, I know about workarounds.
>
>
> -----
> Pavel Luzanov
> Postgres Professional: http://www.postgrespro.com
> The Russian Postgres Company
>
>

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: psql variables in the DO command
@ 2018-03-05 14:02  Pavel Luzanov <p.luzanov@postgrespro.ru>
  parent: Pavel Stehule <pavel.stehule@gmail.com>
  0 siblings, 2 replies; 11+ messages in thread

From: Pavel Luzanov @ 2018-03-05 14:02 UTC (permalink / raw)
  To: Pavel Stehule <pavel.stehule@gmail.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>

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

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: psql variables in the DO command
@ 2018-03-05 14:22  Pavel Luzanov <p.luzanov@postgrespro.ru>
  parent: Pavel Luzanov <p.luzanov@postgrespro.ru>
  1 sibling, 0 replies; 11+ messages in thread

From: Pavel Luzanov @ 2018-03-05 14:22 UTC (permalink / raw)
  To: Pavel Stehule <pavel.stehule@gmail.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>

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

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: psql variables in the DO command
@ 2018-03-05 15:01  Pavel Stehule <pavel.stehule@gmail.com>
  parent: Pavel Luzanov <p.luzanov@postgrespro.ru>
  1 sibling, 1 reply; 11+ messages in thread

From: Pavel Stehule @ 2018-03-05 15:01 UTC (permalink / raw)
  To: Pavel Luzanov <p.luzanov@postgrespro.ru>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>

2018-03-05 15:02 GMT+01:00 Pavel Luzanov <p.luzanov@postgrespro.ru>:

> 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.
>

It is most correct when you thinking about it.

1. :xx is out of SQL syntax, so can by safely used. There is not risk of
unwanted usage.

2. but string literal can contain :xxx symbols and not necessary it means
so it should be usage of psql variable - so additional syntax for disabling
evaluation should be necessary

3. I understand to request to use psql variables in DO command. But you
should remember - body of DO command is string. body of any function is
string too. Some unwanted psql variable evaluation in CREATE FUNCTION can
be tragic.

Unfortunately DO command is half baked - and doesn't support parameters. I
am working on schema variables and I hope it will be a solution of this
issue:

CREATE VARIABLE var as integer;

LET var = :psqlintvar;

DO $$
BEGIN
  RAISE NOTICE '%', var;
END;
$$;



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

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: psql variables in the DO command
@ 2018-03-05 15:19  Pavel Luzanov <p.luzanov@postgrespro.ru>
  parent: Pavel Stehule <pavel.stehule@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Pavel Luzanov @ 2018-03-05 15:19 UTC (permalink / raw)
  To: Pavel Stehule <pavel.stehule@gmail.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>

On 05.03.2018 18:01, Pavel Stehule wrote:
>
> It is most correct when you thinking about it.
>
> 1. :xx is out of SQL syntax, so can by safely used. There is not risk 
> of unwanted usage.
But there is absence of wanted usage too.
>
> 2. but string literal can contain :xxx symbols and not necessary it 
> means so it should be usage of psql variable - so additional syntax 
> for disabling evaluation should be necessary
Yes and Oracle sqlplus (I khow than you know this tool) has special 
command to control this: set define ...

>
> Unfortunately DO command is half baked - and doesn't support 
> parameters. I am working on schema variables and I hope it will be a 
> solution of this issue:
>
> CREATE VARIABLE var as integer;
>
> LET var = :psqlintvar;
>
> DO $$
> BEGIN
>   RAISE NOTICE '%', var;
> END;
> $$;
>

It will be great. I already commented it in your blog.

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

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: psql variables in the DO command
@ 2018-03-05 15:35  Pavel Stehule <pavel.stehule@gmail.com>
  parent: Pavel Luzanov <p.luzanov@postgrespro.ru>
  0 siblings, 1 reply; 11+ messages in thread

From: Pavel Stehule @ 2018-03-05 15:35 UTC (permalink / raw)
  To: Pavel Luzanov <p.luzanov@postgrespro.ru>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>

2018-03-05 16:19 GMT+01:00 Pavel Luzanov <p.luzanov@postgrespro.ru>:

> On 05.03.2018 18:01, Pavel Stehule wrote:
>
>
> It is most correct when you thinking about it.
>
> 1. :xx is out of SQL syntax, so can by safely used. There is not risk of
> unwanted usage.
>
> But there is absence of wanted usage too.
>

How much strong and often? The parser of SQL in psql is not nice - and I
understand so nobody would to complicate syntax. Current design is SAFE and
good enough. The problem is not is a evaluation, but in DO implementation.

>
> 2. but string literal can contain :xxx symbols and not necessary it means
> so it should be usage of psql variable - so additional syntax for disabling
> evaluation should be necessary
>
> Yes and Oracle sqlplus (I khow than you know this tool) has special
> command to control this: set define ...
>
>
> Unfortunately DO command is half baked - and doesn't support parameters. I
> am working on schema variables and I hope it will be a solution of this
> issue:
>
> CREATE VARIABLE var as integer;
>
> LET var = :psqlintvar;
>
> DO $$
> BEGIN
>   RAISE NOTICE '%', var;
> END;
> $$;
>
>
> It will be great. I already commented it in your blog.
>

I am slowly working on prototype. The work is simple, when variables are
just scalars. But it is much harder, when we allow composite variables.
When prototype will be done, I invite any cooperation - there are lot of
question - and one very hard - where and how the variable should be stored
(values) - now I have workaround, but it is pretty ugly code.

CREATE VARIABLE x INT;
LET x = 10;
BEGIN;
DROP VARIABLE x;
ROLLBACK;
SELECT x; -- should be 10 .. for this situation, the PostgreSQL internal
caches are not prepared

Regards

Pavel


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

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: psql variables in the DO command
@ 2018-03-06 09:17  Pavel Luzanov <p.luzanov@postgrespro.ru>
  parent: Pavel Stehule <pavel.stehule@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Pavel Luzanov @ 2018-03-06 09:17 UTC (permalink / raw)
  To: Pavel Stehule <pavel.stehule@gmail.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>

On 05.03.2018 18:35, Pavel Stehule wrote:
> I am slowly working on prototype. The work is simple, when variables 
> are just scalars. But it is much harder, when we allow composite 
> variables. When prototype will be done, I invite any cooperation - 
> there are lot of question - and one very hard - where and how the 
> variable should be stored (values) - now I have workaround, but it is 
> pretty ugly code.
I'm not a big expert on postgres internals, but ready to participate.

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

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: psql variables in the DO command
@ 2018-03-06 09:25  Pavel Stehule <pavel.stehule@gmail.com>
  parent: Pavel Luzanov <p.luzanov@postgrespro.ru>
  0 siblings, 0 replies; 11+ messages in thread

From: Pavel Stehule @ 2018-03-06 09:25 UTC (permalink / raw)
  To: Pavel Luzanov <p.luzanov@postgrespro.ru>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>

2018-03-06 10:17 GMT+01:00 Pavel Luzanov <p.luzanov@postgrespro.ru>:

> On 05.03.2018 18:35, Pavel Stehule wrote:
>
> I am slowly working on prototype. The work is simple, when variables are
> just scalars. But it is much harder, when we allow composite variables.
> When prototype will be done, I invite any cooperation - there are lot of
> question - and one very hard - where and how the variable should be stored
> (values) - now I have workaround, but it is pretty ugly code.
>
> I'm not a big expert on postgres internals, but ready to participate.
>

big thanks. I'll start new thread to stop do offtopic in this place.

Thank you

Pavel

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

^ permalink  raw  reply  [nested|flat] 11+ messages in thread


end of thread, other threads:[~2018-03-06 09:25 UTC | newest]

Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-03-05 13:13 psql variables in the DO command Pavel Luzanov <p.luzanov@postgrespro.ru>
2018-03-05 13:42 ` Pavel Stehule <pavel.stehule@gmail.com>
2018-03-05 13:52   ` Pavel Luzanov <p.luzanov@postgrespro.ru>
2018-03-05 13:56     ` Pavel Stehule <pavel.stehule@gmail.com>
2018-03-05 14:02       ` Pavel Luzanov <p.luzanov@postgrespro.ru>
2018-03-05 14:22         ` Pavel Luzanov <p.luzanov@postgrespro.ru>
2018-03-05 15:01         ` Pavel Stehule <pavel.stehule@gmail.com>
2018-03-05 15:19           ` Pavel Luzanov <p.luzanov@postgrespro.ru>
2018-03-05 15:35             ` Pavel Stehule <pavel.stehule@gmail.com>
2018-03-06 09:17               ` Pavel Luzanov <p.luzanov@postgrespro.ru>
2018-03-06 09:25                 ` Pavel Stehule <pavel.stehule@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox