Re: Re: BUG #11103: to_json() does not convert correctly DOMAINs type since 9.3.5 (int expected instead of string) ?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: David G Johnston <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: pgsql-bugs(at)postgresql(dot)org, dgrelaud(at)ideolys(dot)com
Subject: Re: Re: BUG #11103: to_json() does not convert correctly DOMAINs type since 9.3.5 (int expected instead of string) ?
Date: 2014-08-07 20:59:20
Message-ID: 20389.1407445160@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

David G Johnston <david(dot)g(dot)johnston(at)gmail(dot)com> writes:
> Michael Paquier wrote
>> On Fri, Aug 1, 2014 at 12:24 AM, <dgrelaud@> wrote:
>>> With 9.3.5, it returns a JSON string : '2' (with quotes)
>>> With 9.3.4, it returns a JSON int : 2 (without quotes)

> Since the unsigned_int4 is a domain now, in head, it would appear without
> the quotes. Before the patch, in 9.3.4, it also appeared without the
> quotes. After the patch, in 9.3.5, it now has quotes so the patch has
> already changed user-visible behavior which is the reason Tom didn't
> back-patch the part that pertained to domains.

Hm ... actually the intent of that was to *not* cause a behavior change in
pre-9.4. But it looks like things are more complicated than I thought.

A domain inherits the typcategory of its base type, which means that in
the old coding that relied on typcategory to decide what to do, domains
*were* treated the same as their base types, in some cases anyway.
Specifically it looks like the old behavior was:

* If there was a cast function to json, you got the behavior of that
cast function. The cast had to be from exactly the given type (ie,
in this case, the domain not its base type).

* If the type was JSON (or JSONB, though that's not relevant to pre-9.4),
you got the JSON-appropriate behavior. Again, it had to be exactly JSON
and not a domain over same.

* Otherwise you got classification according to typcategory, which *would*
effectively look through domains. Specifically, domains over boolean
and domains over numeric types would be converted like their base types.

So I was thinking about not changing the first two cases and failed to
realize the side-effects for the last case.

I'm inclined to think that the best thing to do is add the getBaseType
call in the older branches too. That will restore the previous behavior
for domains over booleans and numerics. It will change the behavior
for the other two cases, which would affect domains over json and hstore
in particular; but arguably that's a bug fix. The old behavior was
just plain inconsistent, because it effectively looked through domains
over some types and not domains over others.

Thoughts, objections?

regards, tom lane

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2014-08-07 21:42:15 Re: BUG #11109: No Toast compression on JSON, JSONB columns
Previous Message kuon 2014-08-07 19:18:56 BUG #11129: Centos 7 official contrib package is missing uuid-ossp