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

Started by Jack Douglasalmost 17 years ago5 messagesbugs
Jump to latest
#1Jack Douglas
jackpdouglas@gmail.com

The following bug has been logged online:

Bug reference: 4908
Logged by: Jack Douglas
Email address: jackpdouglas@gmail.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 :-)

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Jack Douglas (#1)
Re: BUG #4908: escaping and dollar quotes: "ERROR: unterminated string"

"Jack Douglas" <jackpdouglas@gmail.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

#3Kevin Grittner
Kevin.Grittner@wicourts.gov
In reply to: Tom Lane (#2)
Re: BUG #4908: escaping and dollar quotes: "ERROR: unterminated string"

Tom Lane <tgl@sss.pgh.pa.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

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Kevin Grittner (#3)
Re: BUG #4908: escaping and dollar quotes: "ERROR: unterminated string"

"Kevin Grittner" <Kevin.Grittner@wicourts.gov> writes:

Tom Lane <tgl@sss.pgh.pa.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

#5Jack Douglas
jackpdouglas@gmail.com
In reply to: Tom Lane (#2)
Re: BUG #4908: escaping and dollar quotes: "ERROR: unterminated string"

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.