Re: Unexpected behaviour of encode()

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bill Moran <wmoran(at)potentialtech(dot)com>
Cc: Merlin Moncure <mmoncure(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Unexpected behaviour of encode()
Date: 2013-03-26 18:47:38
Message-ID: 16965.1364323658@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Bill Moran <wmoran(at)potentialtech(dot)com> writes:
> If I just do:
> SELECT 'can''t'::text;
> I get "can't" which is what I'd expect. I would then expect
> encode to escape the ' somehow. Even c-style escaping, like
> "can\'t" would have been less surprising to me.

> If there's something I'm missing, I'm still missing it.

The manual says that 'escape' encoding "merely outputs null bytes as
\000 and doubles backslashes". (The reason to double backslashes is to
make \000 unambiguous, of course.) The point of this is to sanitize
bytea data sufficiently to allow it to be transported as text. If you
want to transport it as a SQL literal, that's a tighter constraint that
would require some other escaping method, or at least passing the result
through something like quote_literal.

(Having said that, I wonder though if "escape" doesn't need more
thought. The output is only valid text in SQL_ASCII or single-byte
encodings, otherwise there's risk of encoding violations.)

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Laurence Rowe 2013-03-26 21:59:19 Building an invalidation queue in Postgres
Previous Message Merlin Moncure 2013-03-26 18:41:55 Re: Unexpected behaviour of encode()