pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
psql syntax for array of strings in a variable?
5+ messages / 4 participants
[nested] [flat]

* psql syntax for array of strings in a variable?
@ 2021-10-29 17:21  Philip Semanchuk <philip@americanefficient.com>
  0 siblings, 2 replies; 5+ messages in thread

From: Philip Semanchuk @ 2021-10-29 17:21 UTC (permalink / raw)
  To: PostgreSQL General <pgsql-general@lists.postgresql.org>

Hi,
I would appreciate help with the syntax for querying an array of strings declared as a psql variable. Here's an example.

\set important_days ARRAY['monday', 'friday']

select 1 where 'monday' = ANY(:important_days);
ERROR:  42703: column "monday" does not exist
LINE 1: select 1 where 'monday' = ANY(ARRAY[monday,friday]);

select 1 where 'monday' = ANY(:"important_days");
ERROR:  42703: column "ARRAY[monday,friday]" does not exist
LINE 1: select 1 where 'monday' = ANY("ARRAY[monday,friday]");

I'm doing something wrong but I can't figure out what.

My real-world use case is that I have a psql script that will execute several queries on a long list of strings, and rather than repeat those strings over and over in the script, I'd like to declare them once at the top of the script and then refer to the variable after that. Bonus points if there's a way to do a multiline declaration like --

\set important_days ARRAY['monday', 
                          'friday']

Thanks for reading
Philip




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

* Re: psql syntax for array of strings in a variable?
@ 2021-10-29 17:34  David G. Johnston <david.g.johnston@gmail.com>
  parent: Philip Semanchuk <philip@americanefficient.com>
  1 sibling, 1 reply; 5+ messages in thread

From: David G. Johnston @ 2021-10-29 17:34 UTC (permalink / raw)
  To: Philip Semanchuk <philip@americanefficient.com>; +Cc: PostgreSQL General <pgsql-general@lists.postgresql.org>

On Friday, October 29, 2021, Philip Semanchuk <philip@americanefficient.com>
wrote:

> Hi,
> I would appreciate help with the syntax for querying an array of strings
> declared as a psql variable. Here's an example.
>
> \set important_days ARRAY['monday', 'friday']


Not sure why the single quotes are getting stripped out but that is the
issue.  Maybe double them up to escape them like in a normal text literal?

Otherwise consider just doing a comma delimited string and using
string_to_array in the sql to turn it into an array at runtime.



> \set important_days ARRAY['monday',
>                           'friday']
>

Not with \set.  You could write sql to output the text and then execute it
using \gexec to store the column value in a variable.  That too may be a
solution to the missing single quotes.

David J.

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

* Re: psql syntax for array of strings in a variable?
@ 2021-10-29 17:52  Pavel Stehule <pavel.stehule@gmail.com>
  parent: Philip Semanchuk <philip@americanefficient.com>
  1 sibling, 0 replies; 5+ messages in thread

From: Pavel Stehule @ 2021-10-29 17:52 UTC (permalink / raw)
  To: Philip Semanchuk <philip@americanefficient.com>; +Cc: PostgreSQL General <pgsql-general@lists.postgresql.org>

Hi

pá 29. 10. 2021 v 19:21 odesílatel Philip Semanchuk <
philip@americanefficient.com> napsal:

> Hi,
> I would appreciate help with the syntax for querying an array of strings
> declared as a psql variable. Here's an example.
>
> \set important_days ARRAY['monday', 'friday']
>
> select 1 where 'monday' = ANY(:important_days);
> ERROR:  42703: column "monday" does not exist
> LINE 1: select 1 where 'monday' = ANY(ARRAY[monday,friday]);
>
> select 1 where 'monday' = ANY(:"important_days");
> ERROR:  42703: column "ARRAY[monday,friday]" does not exist
> LINE 1: select 1 where 'monday' = ANY("ARRAY[monday,friday]");
>
> I'm doing something wrong but I can't figure out what.
>
> My real-world use case is that I have a psql script that will execute
> several queries on a long list of strings, and rather than repeat those
> strings over and over in the script, I'd like to declare them once at the
> top of the script and then refer to the variable after that. Bonus points
> if there's a way to do a multiline declaration like --
>
> \set important_days ARRAY['monday',
>                           'friday']
>
> Thanks for reading
> Philip
>

psql variables can hold only text. There is not any type - all is just text.

Regards

Pavel

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

* Re: psql syntax for array of strings in a variable?
@ 2021-10-29 18:05  Tom Lane <tgl@sss.pgh.pa.us>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Tom Lane @ 2021-10-29 18:05 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Philip Semanchuk <philip@americanefficient.com>; PostgreSQL General <pgsql-general@lists.postgresql.org>

"David G. Johnston" <david.g.johnston@gmail.com> writes:
> On Friday, October 29, 2021, Philip Semanchuk <philip@americanefficient.com>
> wrote:
>> I would appreciate help with the syntax for querying an array of strings
>> declared as a psql variable. Here's an example.
>> 
>> \set important_days ARRAY['monday', 'friday']

> Not sure why the single quotes are getting stripped out but that is the
> issue.  Maybe double them up to escape them like in a normal text literal?

Yeah, that's just the way that \set works (and most other psql backslash
commands, I believe).  You've likely got an issue with whitespace
disappearing, too, though that might be harmless in this specific example.

regression=# \set foo 'bar baz'
regression=# \echo :foo
bar baz
regression=# \set foo 'bar ''baz'
regression=# \echo :foo
bar 'baz
regression=# \set foo bar ''baz 
regression=# \echo :foo
barbaz

Not sure offhand how well-documented this is.

			regards, tom lane





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

* Re: psql syntax for array of strings in a variable?
@ 2021-10-29 18:44  Philip Semanchuk <philip@americanefficient.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 5+ messages in thread

From: Philip Semanchuk @ 2021-10-29 18:44 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: David G. Johnston <david.g.johnston@gmail.com>; PostgreSQL General <pgsql-general@lists.postgresql.org>



> On Oct 29, 2021, at 2:05 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> 
> "David G. Johnston" <david.g.johnston@gmail.com> writes:
>> On Friday, October 29, 2021, Philip Semanchuk <philip@americanefficient.com>
>> wrote:
>>> I would appreciate help with the syntax for querying an array of strings
>>> declared as a psql variable. Here's an example.
>>> 
>>> \set important_days ARRAY['monday', 'friday']
> 
>> Not sure why the single quotes are getting stripped out but that is the
>> issue.  Maybe double them up to escape them like in a normal text literal?
> 
> Yeah, that's just the way that \set works (and most other psql backslash
> commands, I believe).  You've likely got an issue with whitespace
> disappearing, too, though that might be harmless in this specific example.
> 
> regression=# \set foo 'bar baz'
> regression=# \echo :foo
> bar baz
> regression=# \set foo 'bar ''baz'
> regression=# \echo :foo
> bar 'baz
> regression=# \set foo bar ''baz 
> regression=# \echo :foo
> barbaz
> 
> Not sure offhand how well-documented this is.

Thanks, all. Glad to know I wasn’t missing something obvious. 

> On Oct 29, 2021, at 1:52 PM, Pavel Stehule <pavel.stehule@gmail.com> wrote:

> psql variables can hold only text. There is not any type - all is just text.


^^^ This was especially helpful; I’d never considered that before.

Cheers
Philip








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


end of thread, other threads:[~2021-10-29 18:44 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-10-29 17:21 psql syntax for array of strings in a variable? Philip Semanchuk <philip@americanefficient.com>
2021-10-29 17:34 ` David G. Johnston <david.g.johnston@gmail.com>
2021-10-29 18:05   ` Tom Lane <tgl@sss.pgh.pa.us>
2021-10-29 18:44     ` Philip Semanchuk <philip@americanefficient.com>
2021-10-29 17:52 ` 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