Thread: dollar-quoting in psql and in general

dollar-quoting in psql and in general

From
Philip Warner
Date:
While trying to understand dollar-quoting, I found the following in psql:
   select $a$ hello $a$;

behaves as expected, but psql does not like
   select $a$ \ $a$;
or   select $a$ \\ $a$;

Should it? How should a dollar-quote handle:
   $a$ \$a\$a $a$

?



----------------------------------------------------------------
Philip Warner                    |     __---_____
Albatross Consulting Pty. Ltd.   |----/       -  \
(A.B.N. 75 008 659 498)          |          /(@)   ______---_
Tel: (+61) 0500 83 82 81         |                 _________  \
Fax: (+61) 03 5330 3172          |                 ___________ |
Http://www.rhyme.com.au          |                /           \|                                 |    --________--
PGP key available upon request,  |  /
and from pgp.mit.edu:11371       |/  



Re: dollar-quoting in psql and in general

From
Tom Lane
Date:
Philip Warner <pjw@rhyme.com.au> writes:
> While trying to understand dollar-quoting, I found the following in psql:
>     select $a$ hello $a$;

> behaves as expected, but psql does not like

>     select $a$ \ $a$;
> or
>     select $a$ \\ $a$;

Define "does not like".  The behavior seems correct to me.

> Should it? How should a dollar-quote handle:
>     $a$ \$a\$a $a$

The value of that is " \$a\$a ".  Backslashes are not special inside
dollar quotes --- nor is anything else, except the matching close tag.
        regards, tom lane


Re: dollar-quoting in psql and in general

From
Philip Warner
Date:
At 12:47 PM 12/08/2004, Tom Lane wrote:
>Backslashes are not special inside
>dollar quotes --- nor is anything else, except the matching close tag

If they are not special, then shouldnt:
   select $a$\$a$;

result in
   ?column?      \

rather than an error?



----------------------------------------------------------------
Philip Warner                    |     __---_____
Albatross Consulting Pty. Ltd.   |----/       -  \
(A.B.N. 75 008 659 498)          |          /(@)   ______---_
Tel: (+61) 0500 83 82 81         |                 _________  \
Fax: (+61) 03 5330 3172          |                 ___________ |
Http://www.rhyme.com.au          |                /           \|                                 |    --________--
PGP key available upon request,  |  /
and from pgp.mit.edu:11371       |/ 



Re: dollar-quoting in psql and in general

From
Philip Warner
Date:
>At 12:47 PM 12/08/2004, Tom Lane wrote:
>>Backslashes are not special inside
>>dollar quotes --- nor is anything else, except the matching close tag
>
>If they are not special, then shouldnt:
>
>    select $a$\$a$;
>
>result in
>
>    ?column?
>       \
>
>rather than an error?

Oops. It does.




----------------------------------------------------------------
Philip Warner                    |     __---_____
Albatross Consulting Pty. Ltd.   |----/       -  \
(A.B.N. 75 008 659 498)          |          /(@)   ______---_
Tel: (+61) 0500 83 82 81         |                 _________  \
Fax: (+61) 03 5330 3172          |                 ___________ |
Http://www.rhyme.com.au          |                /           \|                                 |    --________--
PGP key available upon request,  |  /
and from pgp.mit.edu:11371       |/  



Re: dollar-quoting in psql and in general

From
Andrew Dunstan
Date:

Philip Warner wrote:

> While trying to understand dollar-quoting, I found the following in psql:
>
>    select $a$ hello $a$;
>
> behaves as expected, but psql does not like
>
>    select $a$ \ $a$;
> or
>    select $a$ \\ $a$;
>
> Should it? How should a dollar-quote handle:
>
>    $a$ \$a\$a $a$
>
> ?
>
>
andrew=# select $a$ \ $a$;?column?
---------- \
(1 row)

andrew=# select $a$ \\ $a$;?column?
---------- \\
(1 row)

It is behaving as *I* expect, and as designed. \ is not magical inside 
dollar quotes, and if it were it would defeat the whole purpose of 
having them.

Dollar quotes are really designed for strings that are reparsed - most 
typically function bodies. The strings inside them are therefore totally 
opaque, and there is no escape character. Otherwise, if we wanted the 
plpgsql parsers, say, to see \ we'd have to write \\, and the idea was 
to get away from \\, \', '' and friends.

cheers

andrew


Re: dollar-quoting in psql and in general

From
Philip Warner
Date:
At 01:07 PM 12/08/2004, Andrew Dunstan wrote:
>\ is not magical inside dollar quotes,

Sorry, I was confused by the manual: the paragraph that starts "C-style 
backslash escapes are also available..." is right after the paragraphs on 
dollar-quoting.

The section on dollar-quoting is also not explicit about valid tags, "zero 
or more characters" is all I can see. Can you give me a definitive answer 
as to what is valid? AFAICT, must be [A-Z,a-z,0-9,_]*, with non-numeric 
start. Is that right?






----------------------------------------------------------------
Philip Warner                    |     __---_____
Albatross Consulting Pty. Ltd.   |----/       -  \
(A.B.N. 75 008 659 498)          |          /(@)   ______---_
Tel: (+61) 0500 83 82 81         |                 _________  \
Fax: (+61) 03 5330 3172          |                 ___________ |
Http://www.rhyme.com.au          |                /           \|                                 |    --________--
PGP key available upon request,  |  /
and from pgp.mit.edu:11371       |/ 



Re: dollar-quoting in psql and in general

From
Tom Lane
Date:
Philip Warner <pjw@rhyme.com.au> writes:
> At 12:47 PM 12/08/2004, Tom Lane wrote:
>> Backslashes are not special inside
>> dollar quotes --- nor is anything else, except the matching close tag

> If they are not special, then shouldnt:
>     select $a$\$a$;
> result in
>     ?column?
>        \
> rather than an error?

Worksforme (TM).

regression=#     select $a$\$a$;?column? 
----------\
(1 row)

regression=# 

Are you sure your psql and backend are both current?
        regards, tom lane


Re: dollar-quoting in psql and in general

From
Tom Lane
Date:
Philip Warner <pjw@rhyme.com.au> writes:
> At 01:07 PM 12/08/2004, Andrew Dunstan wrote:
>> \ is not magical inside dollar quotes,

> Sorry, I was confused by the manual: the paragraph that starts "C-style 
> backslash escapes are also available..." is right after the paragraphs on 
> dollar-quoting.

The documentation for dollar-quoting is pretty awful at the moment, as
it seems to have been pasted in with no thought to context, and is
incomplete anyway.  I have on my to-do list to go over it, but if
someone else beats me to it I won't pout.

> The section on dollar-quoting is also not explicit about valid tags, "zero 
> or more characters" is all I can see.

Use the source, Luke ... scan.l explains

/* $foo$ style quotes ("dollar quoting")* The quoted string starts with $foo$ where "foo" is an optional string* in the
formof an identifier, except that it may not contain "$", * and extends to the first occurrence of an identical string.
* There is *no* processing of the quoted text.*/
 
dolq_start        [A-Za-z\200-\377_]
dolq_cont        [A-Za-z\200-\377_0-9]
dolqdelim        \$({dolq_start}{dolq_cont}*)?\$
        regards, tom lane


Re: dollar-quoting in psql and in general

From
Andrew Dunstan
Date:

Philip Warner wrote:

> At 01:07 PM 12/08/2004, Andrew Dunstan wrote:
>
>> \ is not magical inside dollar quotes,
>
>
> Sorry, I was confused by the manual: the paragraph that starts 
> "C-style backslash escapes are also available..." is right after the 
> paragraphs on dollar-quoting.
>
> The section on dollar-quoting is also not explicit about valid tags, 
> "zero or more characters" is all I can see. Can you give me a 
> definitive answer as to what is valid? AFAICT, must be 
> [A-Z,a-z,0-9,_]*, with non-numeric start. Is that right?
>
>

Don't forget the high-bit chars too. pgsqlscan.l says:

dolq_start      [A-Za-z\200-\377_]
dolq_cont       [A-Za-z\200-\377_0-9]
dolqdelim       \$({dolq_start}{dolq_cont}*)?\$

The scanner files for plpgsql and the backend have equivalent specs, so 
this is definitive.

cheers

andrew