Re: BUG #4908: escaping and dollar quotes: "ERROR: unterminated string" - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #4908: escaping and dollar quotes: "ERROR: unterminated string"
Date
Msg-id 10216.1247087160@sss.pgh.pa.us
Whole thread Raw
In response to BUG #4908: escaping and dollar quotes: "ERROR: unterminated string"  ("Jack Douglas" <jackpdouglas@gmail.com>)
Responses Re: BUG #4908: escaping and dollar quotes: "ERROR: unterminated string"  ("Kevin Grittner" <Kevin.Grittner@wicourts.gov>)
Re: BUG #4908: escaping and dollar quotes: "ERROR: unterminated string"  (Jack Douglas <jackpdouglas@gmail.com>)
List pgsql-bugs
"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

pgsql-bugs by date:

Previous
From: "Jack Douglas"
Date:
Subject: BUG #4908: escaping and dollar quotes: "ERROR: unterminated string"
Next
From: "Kevin Grittner"
Date:
Subject: Re: BUG #4908: escaping and dollar quotes: "ERROR: unterminated string"