8.3 PLpgSQL Can't Compare Records?

Lists: pgsql-hackers
From: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: 8.3 PLpgSQL Can't Compare Records?
Date: 2009-07-01 17:35:10
Message-ID: 87842292-834B-4CF4-B56B-88EB4A30F7E4@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

This code:

CREATE OR REPLACE FUNCTION foo() returns boolean as $$
DECLARE
have_rec record;
want_rec record;
BEGIN
have_rec := row(1, 2);
want_rec := row(3, 5);
RETURN have_rec IS DISTINCT FROM want_rec;
END;
$$ language plpgsql;

SELECT ROW(1, 2) IS DISTINCT FROM ROW(3, 5);

SELECT foo();
DROP FUNCTION foo();

Works as expected on 8.4, outputting:

?column?
----------
t
(1 row)

Time: 48.626 ms
foo
-----
t
(1 row)

On 8.3, however, the row comparisons in the SQL statement works, but
fails in the PL/pgSQL function, with this output:

?column?
----------
t
(1 row)

psql:huh.sql:14: ERROR: operator does not exist: record = record
LINE 1: SELECT $1 IS DISTINCT FROM $2
^
HINT: No operator matches the given name and argument type(s).
You might need to add explicit type casts.
QUERY: SELECT $1 IS DISTINCT FROM $2
CONTEXT: PL/pgSQL function "foo" line 7 at RETURN

Is this a known issue in 8.3? If so, is there a known workaround?

Thanks,

David


From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: 8.3 PLpgSQL Can't Compare Records?
Date: 2009-07-01 18:45:04
Message-ID: b42b73150907011145s7852a85aob70db7d212dcc0bd@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Wed, Jul 1, 2009 at 1:35 PM, David E. Wheeler<david(at)kineticode(dot)com> wrote:
> This code:
>
>    CREATE OR REPLACE FUNCTION foo() returns boolean as $$
>    DECLARE
>        have_rec record;
>        want_rec record;
>    BEGIN
>        have_rec := row(1, 2);
>        want_rec := row(3, 5);
>        RETURN have_rec IS DISTINCT FROM want_rec;
>    END;
>    $$ language plpgsql;
>
>    SELECT ROW(1, 2) IS DISTINCT FROM ROW(3, 5);
>
>    SELECT foo();
>    DROP FUNCTION foo();
>
> Works as expected on 8.4, outputting:
>
>     ?column?
>    ----------
>     t
>    (1 row)
>
>    Time: 48.626 ms
>     foo
>    -----
>     t
>    (1 row)
>
> On 8.3, however, the row comparisons in the SQL statement works, but fails
> in the PL/pgSQL function, with this output:
>
>     ?column?
>    ----------
>     t
>    (1 row)
>
>    psql:huh.sql:14: ERROR:  operator does not exist: record = record
>    LINE 1: SELECT   $1  IS DISTINCT FROM  $2
>                         ^
>    HINT:  No operator matches the given name and argument type(s). You might
> need to add explicit type casts.
>    QUERY:  SELECT   $1  IS DISTINCT FROM  $2
>    CONTEXT:  PL/pgSQL function "foo" line 7 at RETURN
>
>
> Is this a known issue in 8.3? If so, is there a known workaround?
>

fyi: works in 8.4, as part of a broad fix of composite type comparison ops

merlin


From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: 8.3 PLpgSQL Can't Compare Records?
Date: 2009-07-01 18:47:40
Message-ID: b42b73150907011147y1cf92109k6590bd7c9f4cd241@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Wed, Jul 1, 2009 at 2:45 PM, Merlin Moncure<mmoncure(at)gmail(dot)com> wrote:
> On Wed, Jul 1, 2009 at 1:35 PM, David E. Wheeler<david(at)kineticode(dot)com> wrote:
>> This code:
>>
>>    CREATE OR REPLACE FUNCTION foo() returns boolean as $$
>>    DECLARE
>>        have_rec record;
>>        want_rec record;
>>    BEGIN
>>        have_rec := row(1, 2);
>>        want_rec := row(3, 5);
>>        RETURN have_rec IS DISTINCT FROM want_rec;
>>    END;
>>    $$ language plpgsql;
>>
>>    SELECT ROW(1, 2) IS DISTINCT FROM ROW(3, 5);
>>
>>    SELECT foo();
>>    DROP FUNCTION foo();
>>
>> Works as expected on 8.4, outputting:
>>
>>     ?column?
>>    ----------
>>     t
>>    (1 row)
>>
>>    Time: 48.626 ms
>>     foo
>>    -----
>>     t
>>    (1 row)
>>
>> On 8.3, however, the row comparisons in the SQL statement works, but fails
>> in the PL/pgSQL function, with this output:
>>
>>     ?column?
>>    ----------
>>     t
>>    (1 row)
>>
>>    psql:huh.sql:14: ERROR:  operator does not exist: record = record
>>    LINE 1: SELECT   $1  IS DISTINCT FROM  $2
>>                         ^
>>    HINT:  No operator matches the given name and argument type(s). You might
>> need to add explicit type casts.
>>    QUERY:  SELECT   $1  IS DISTINCT FROM  $2
>>    CONTEXT:  PL/pgSQL function "foo" line 7 at RETURN
>>
>>
>> Is this a known issue in 8.3? If so, is there a known workaround?
>>
>
> fyi: works in 8.4, as part of a broad fix of composite type comparison ops

whoops, you knew that already :-). one possible workaround is:

select $1::text is distinct from $2::text;

merlin


From: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
To: Merlin Moncure <mmoncure(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: 8.3 PLpgSQL Can't Compare Records?
Date: 2009-07-01 19:36:21
Message-ID: 95EEDB4E-CAB0-43DB-A4C0-DA41D388D9F0@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Jul 1, 2009, at 11:47 AM, Merlin Moncure wrote:

>> fyi: works in 8.4, as part of a broad fix of composite type
>> comparison ops
>
> whoops, you knew that already :-). one possible workaround is:
>
> select $1::text is distinct from $2::text

Yes, and that's what I'm doing, although it is significantly less
precise, in that:

* Columns with different types may successfully compare (e.g., NULL
and '')
* No (easy) way to tell if two records have different numbers of columns

But it's good enough for 8.3 if there is no other workaround.

Thanks,

David


From: "Albe Laurenz" <laurenz(dot)albe(at)wien(dot)gv(dot)at>
To: "David E(dot) Wheeler *EXTERN*" <david(at)kineticode(dot)com>, "PostgreSQL Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: 8.3 PLpgSQL Can't Compare Records?
Date: 2009-07-07 07:49:39
Message-ID: D960CB61B694CF459DCFB4B0128514C202FF66A7@exadv11.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

David E. Wheeler wrote:
> This code:
>
> CREATE OR REPLACE FUNCTION foo() returns boolean as $$
> DECLARE
> have_rec record;
> want_rec record;
> BEGIN
> have_rec := row(1, 2);
> want_rec := row(3, 5);
> RETURN have_rec IS DISTINCT FROM want_rec;
> END;
> $$ language plpgsql;
>
> SELECT ROW(1, 2) IS DISTINCT FROM ROW(3, 5);
>
> SELECT foo();
> DROP FUNCTION foo();
>
> Works as expected on 8.4, outputting:
>
[...]
>
> On 8.3, however, the row comparisons in the SQL statement works, but
> fails in the PL/pgSQL function, with this output:
>
> ?column?
> ----------
> t
> (1 row)
>
> psql:huh.sql:14: ERROR: operator does not exist: record = record
> LINE 1: SELECT $1 IS DISTINCT FROM $2
> ^
> HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
> QUERY: SELECT $1 IS DISTINCT FROM $2
> CONTEXT: PL/pgSQL function "foo" line 7 at RETURN
>
>
> Is this a known issue in 8.3? If so, is there a known workaround?

The change is probably here:
http://archives.postgresql.org/pgsql-committers/2008-10/msg00110.php

So I think it is safe to argue that this is not a bug in 8.3, but an improvement in 8.4.

Yours,
Laurenz Albe


From: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
To: Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>
Cc: "PostgreSQL Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: 8.3 PLpgSQL Can't Compare Records?
Date: 2009-07-07 16:02:53
Message-ID: E97A56FD-E964-44D4-BE66-2A5480FE7A65@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Jul 7, 2009, at 12:49 AM, Albe Laurenz wrote:

>> Is this a known issue in 8.3? If so, is there a known workaround?
>
> The change is probably here:
> http://archives.postgresql.org/pgsql-committers/2008-10/msg00110.php
>
> So I think it is safe to argue that this is not a bug in 8.3, but an
> improvement in 8.4.

Right, good find, thanks.

David