Обсуждение: BUG #4908: escaping and dollar quotes: "ERROR: unterminated string"

Поиск
Список
Период
Сортировка

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

От
"Jack Douglas"
Дата:
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 :-)

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

От
Tom Lane
Дата:
"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

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

От
"Kevin Grittner"
Дата:
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

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

От
Tom Lane
Дата:
"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

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

От
Jack Douglas
Дата:
> The function body contains
> =A0 =A0 =A0 =A0return '\';
>
> and that string literal causes a syntax error when we come to parse the
> RETURN statement.

Thanks for the explanation - very clear.