Re: BUG #4908: escaping and dollar quotes: "ERROR: unterminated string"

Lists: pgsql-bugs
From: "Jack Douglas" <jackpdouglas(at)gmail(dot)com>
To: pgsql-bugs(at)postgresql(dot)org
Subject: BUG #4908: escaping and dollar quotes: "ERROR: unterminated string"
Date: 2009-07-08 20:53:03
Message-ID: 200907082053.n68Kr3Xm090266@wwwmaster.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs


The following bug has been logged online:

Bug reference: 4908
Logged by: Jack Douglas
Email address: jackpdouglas(at)gmail(dot)com
PostgreSQL version: 8.3.7
Operating system: Debian Lenny
Description: escaping and dollar quotes: "ERROR: unterminated
string"
Details:

Am I missing something obvious here - I understand from the documentation no
escapes are counted in dollar quoted strings?

postgres=> create or replace function temp() returns text language plpgsql
AS $$
postgres$> begin
postgres$> return '\';
postgres$> end; $$;
ERROR: unterminated string
CONTEXT: compile of PL/pgSQL function "temp" near line 2

I use the following as a workaround:

postgres=> create or replace function temp() returns text language plpgsql
AS $$
postgres$> begin
postgres$> return rtrim('\ ');
postgres$> end; $$;
CREATE FUNCTION

obviously this is a contrived test case to demonstrate the problem, not my
real function :-)


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Jack Douglas" <jackpdouglas(at)gmail(dot)com>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #4908: escaping and dollar quotes: "ERROR: unterminated string"
Date: 2009-07-08 21:06:00
Message-ID: 10216.1247087160@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

"Jack Douglas" <jackpdouglas(at)gmail(dot)com> writes:
> Am I missing something obvious here - I understand from the documentation no
> escapes are counted in dollar quoted strings?

Yes, and yes.

> postgres=> create or replace function temp() returns text language plpgsql
> AS $$
> postgres$> begin
> postgres$> return '\';
> postgres$> end; $$;
> ERROR: unterminated string
> CONTEXT: compile of PL/pgSQL function "temp" near line 2

The function body contains
return '\';

and that string literal causes a syntax error when we come to parse the
RETURN statement. You could do
return '\\';
or
return $q$\$q$;
or
return $$\$$;
but the last requires using other $-delimiters around the function body.

Or you could turn on standard_conforming_strings if you'd prefer not to
deal with escapes.

regards, tom lane


From: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
To: "Jack Douglas" <jackpdouglas(at)gmail(dot)com>, "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: <pgsql-bugs(at)postgresql(dot)org>
Subject: Re: BUG #4908: escaping and dollar quotes: "ERROR: unterminated string"
Date: 2009-07-08 21:22:45
Message-ID: 4A54C7D502000025000285EF@gw.wicourts.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> Or you could turn on standard_conforming_strings if you'd prefer not
> to deal with escapes.

That doesn't help with this, because of the separate pgpgsql parser:

ccdev=> select version();
version
-----------------------------------------------------------------------------------------------------
PostgreSQL 8.3.7 on x86_64-unknown-linux-gnu, compiled by GCC gcc
(GCC) 4.1.2 20070115 (SUSE Linux)
(1 row)

ccdev=> show standard_conforming_strings ;
standard_conforming_strings
-----------------------------
on
(1 row)

ccdev=> create or replace function temp() returns text language
plpgsql
AS $$
begin
return '\';
end; $$;
ERROR: unterminated string
CONTEXT: compile of PL/pgSQL function "temp" near line 2

-Kevin


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
Cc: "Jack Douglas" <jackpdouglas(at)gmail(dot)com>, pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #4908: escaping and dollar quotes: "ERROR: unterminated string"
Date: 2009-07-08 21:51:23
Message-ID: 10985.1247089883@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

"Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov> writes:
> Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> Or you could turn on standard_conforming_strings if you'd prefer not
>> to deal with escapes.

> That doesn't help with this, because of the separate pgpgsql parser:

Ah, sorry, it does work in 8.4 but not before.

regards, tom lane


From: Jack Douglas <jackpdouglas(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #4908: escaping and dollar quotes: "ERROR: unterminated string"
Date: 2009-07-09 15:51:59
Message-ID: 6a9af8810907090851k114ff793ya96bc7279162c31f@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

> The function body contains
>        return '\';
>
> and that string literal causes a syntax error when we come to parse the
> RETURN statement.

Thanks for the explanation - very clear.