Re: plpgsql: numeric assignment to an integer variable errors out

Lists: pgsql-hackers
From: "Nikhil Sontakke" <nikhil(dot)sontakke(at)enterprisedb(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: plpgsql: numeric assignment to an integer variable errors out
Date: 2008-12-11 12:35:45
Message-ID: a301bfd90812110435l399acc41n6a74caa51f8ab229@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

The following plpgsql function errors out with cvs head:

CREATE function test_assign() returns void
AS
$$ declare x int;
BEGIN
x := 9E3/2;
END
$$ LANGUAGE 'plpgsql';

postgres=# select test_assign();
ERROR: invalid input syntax for integer: "4500.0000000000000000"
CONTEXT: PL/pgSQL function "test_assign" line 3 at assignment

We do have an existing cast from numeric to type integer. But here basically
we convert the value to string in exec_cast_value before calling int4in. And
then use of strtol in pg_atoi leads to this complaint. Guess converting the
value to string is not always a good strategy.

Regards,
Nikhils
--
http://www.enterprisedb.com


From: "Nikhil Sontakke" <nikhil(dot)sontakke(at)enterprisedb(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: plpgsql: numeric assignment to an integer variable errors out
Date: 2008-12-29 15:02:13
Message-ID: a301bfd90812290702p50271226j75e0da1e1b7432ab@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi,

<nikhil(dot)sontakke(at)enterprisedb(dot)com> wrote:

> The following plpgsql function errors out with cvs head:
>
> CREATE function test_assign() returns void
> AS
> $$ declare x int;
> BEGIN
> x := 9E3/2;
> END
> $$ LANGUAGE 'plpgsql';
>
> postgres=# select test_assign();
> ERROR: invalid input syntax for integer: "4500.0000000000000000"
> CONTEXT: PL/pgSQL function "test_assign" line 3 at assignment
>
> We do have an existing cast from numeric to type integer. But here
> basically we convert the value to string in exec_cast_value before calling
> int4in. And then use of strtol in pg_atoi leads to this complaint. Guess
> converting the value to string is not always a good strategy.
>

PFA, patch which uses find_coercion_pathway to find a direct
COERCION_PATH_FUNC function and uses that if it is available. Or is there a
better approach? Seems to handle the above issue with this patch.

Regards,
Nikhils
--
http://www.enterprisedb.com

Attachment Content-Type Size
plpgsql_numeric_bug.patch text/x-diff 2.0 KB

From: "Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com>
To: "Nikhil Sontakke" <nikhil(dot)sontakke(at)enterprisedb(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: plpgsql: numeric assignment to an integer variable errors out
Date: 2008-12-29 17:10:17
Message-ID: 162867790812290910y3d0d5420ube7b5db8d6b53cfc@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2008/12/29 Nikhil Sontakke <nikhil(dot)sontakke(at)enterprisedb(dot)com>:
> Hi,
>
> <nikhil(dot)sontakke(at)enterprisedb(dot)com> wrote:
>>
>> The following plpgsql function errors out with cvs head:
>>
>> CREATE function test_assign() returns void
>> AS
>> $$ declare x int;
>> BEGIN
>> x := 9E3/2;
>> END
>> $$ LANGUAGE 'plpgsql';
>>
>> postgres=# select test_assign();
>> ERROR: invalid input syntax for integer: "4500.0000000000000000"
>> CONTEXT: PL/pgSQL function "test_assign" line 3 at assignment
>>
>> We do have an existing cast from numeric to type integer. But here
>> basically we convert the value to string in exec_cast_value before calling
>> int4in. And then use of strtol in pg_atoi leads to this complaint. Guess
>> converting the value to string is not always a good strategy.
>
> PFA, patch which uses find_coercion_pathway to find a direct
> COERCION_PATH_FUNC function and uses that if it is available. Or is there a
> better approach? Seems to handle the above issue with this patch.

+1

I thing, so some values should by cached, current patch could by slow.

Regards
Pavel Stehule

>
> Regards,
> Nikhils
> --
> http://www.enterprisedb.com
>
>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers
>
>


From: "Nikhil Sontakke" <nikhil(dot)sontakke(at)enterprisedb(dot)com>
To: "Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: plpgsql: numeric assignment to an integer variable errors out
Date: 2008-12-30 07:04:02
Message-ID: a301bfd90812292304w39479604sf3539433b8b50ed1@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi,

> >
> >> The following plpgsql function errors out with cvs head:
> >>
> >> CREATE function test_assign() returns void
> >> AS
> >> $$ declare x int;
> >> BEGIN
> >> x := 9E3/2;
> >> END
> >> $$ LANGUAGE 'plpgsql';
> >>
> >> postgres=# select test_assign();
> >> ERROR: invalid input syntax for integer: "4500.0000000000000000"
> >> CONTEXT: PL/pgSQL function "test_assign" line 3 at assignment
> >>
> >> We do have an existing cast from numeric to type integer. But here
> >> basically we convert the value to string in exec_cast_value before
> calling
> >> int4in. And then use of strtol in pg_atoi leads to this complaint. Guess
> >> converting the value to string is not always a good strategy.
> >
> > PFA, patch which uses find_coercion_pathway to find a direct
> > COERCION_PATH_FUNC function and uses that if it is available. Or is there
> a
> > better approach? Seems to handle the above issue with this patch.
>
> +1
>
> I thing, so some values should by cached, current patch could by slow.

Agreed, it can slow things down a bit especially since we are only
interested in the COERCION_PATH_FUNC case. What we need is a much simpler
pathway function which searches in the SysCache and returns back with the
valid/invalid castfunc immediately.

PFA, version 2.0 of this patch with these changes in place. I could have
added a generic function in parse_coerce.c, but thought the use case was
restricted to plpgsql and hence I have kept it within pl_exec.c for now.

Regards,
Nikhils
--
http://www.enterprisedb.com

Attachment Content-Type Size
plpgsql_numeric_bug_v2.0.patch text/x-diff 3.1 KB

From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Nikhil Sontakke <nikhil(dot)sontakke(at)enterprisedb(dot)com>
Cc: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: plpgsql: numeric assignment to an integer variable errors out
Date: 2009-01-21 23:59:12
Message-ID: 200901212359.n0LNxC618241@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Nikhil Sontakke wrote:
> > > PFA, patch which uses find_coercion_pathway to find a direct
> > > COERCION_PATH_FUNC function and uses that if it is available. Or is there
> > a
> > > better approach? Seems to handle the above issue with this patch.
> >
> > +1
> >
> > I thing, so some values should by cached, current patch could by slow.
>
>
> Agreed, it can slow things down a bit especially since we are only
> interested in the COERCION_PATH_FUNC case. What we need is a much simpler
> pathway function which searches in the SysCache and returns back with the
> valid/invalid castfunc immediately.
>
> PFA, version 2.0 of this patch with these changes in place. I could have
> added a generic function in parse_coerce.c, but thought the use case was
> restricted to plpgsql and hence I have kept it within pl_exec.c for now.

Where are we on this? 8.5?

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Bruce Momjian <bruce(at)momjian(dot)us>
Cc: Nikhil Sontakke <nikhil(dot)sontakke(at)enterprisedb(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: plpgsql: numeric assignment to an integer variable errors out
Date: 2009-01-22 05:37:37
Message-ID: 162867790901212137u17d57a37nd6f9508bcb98d16c@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2009/1/22 Bruce Momjian <bruce(at)momjian(dot)us>:
> Nikhil Sontakke wrote:
>> > > PFA, patch which uses find_coercion_pathway to find a direct
>> > > COERCION_PATH_FUNC function and uses that if it is available. Or is there
>> > a
>> > > better approach? Seems to handle the above issue with this patch.
>> >
>> > +1
>> >
>> > I thing, so some values should by cached, current patch could by slow.
>>
>>
>> Agreed, it can slow things down a bit especially since we are only
>> interested in the COERCION_PATH_FUNC case. What we need is a much simpler
>> pathway function which searches in the SysCache and returns back with the
>> valid/invalid castfunc immediately.
>>
>> PFA, version 2.0 of this patch with these changes in place. I could have
>> added a generic function in parse_coerce.c, but thought the use case was
>> restricted to plpgsql and hence I have kept it within pl_exec.c for now.
>
> Where are we on this? 8.5?
>

I hope, so this patch will be in next commitfest

Pavel

> --
> Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
> EnterpriseDB http://enterprisedb.com
>
> + If your life is a hard drive, Christ can be your backup. +
>


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Bruce Momjian <bruce(at)momjian(dot)us>
Cc: Nikhil Sontakke <nikhil(dot)sontakke(at)enterprisedb(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: Re: plpgsql: numeric assignment to an integer variable errors out
Date: 2009-01-23 21:43:39
Message-ID: 162867790901231343r3be5bf85h13f7e4ff6140e336@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hello

I tested patch v2.0, and I thing, so this patch should be used as bug
fix. It has same or little bit better speed than current and solve
some problems with numeric's implicit casting in plpgsql. But this is
only an half solution.

The core of problem is in lazy casting of plpgsql. We need to modify
execution plan for pl expression when result is different than
destination type. For almost expression we know destination type.

there is some sample of effectiveness:
postgres=# create or replace function test1()
returns int as $$
declare s int := 0;
begin
for i in 1..100000 loop
s := 4e3; -- numeric
end loop;
return s;
end;
$$ language plpgsql immutable;
CREATE FUNCTION
Time: 5,140 ms
postgres=# create or replace function test2()
returns int as $$
declare s int := 0;
begin
for i in 1..100000 loop
s := 4e3::int;
end loop;
return s;
end;
$$ language plpgsql immutable;
CREATE FUNCTION
Time: 416,240 ms
postgres=# select test1();
test1
-------
4000
(1 row)

Time: 161,048 ms
postgres=# select test2();
test2
-------
4000
(1 row)

Time: 68,110 ms
postgres=# select test1();
test1
-------
4000
(1 row)

Time: 171,020 ms
postgres=# select test2();
test2
-------
4000
(1 row)

Time: 61,771 ms
postgres=#

Regards
Pavel Stehule

2009/1/22 Bruce Momjian <bruce(at)momjian(dot)us>:
> Nikhil Sontakke wrote:
>> > > PFA, patch which uses find_coercion_pathway to find a direct
>> > > COERCION_PATH_FUNC function and uses that if it is available. Or is there
>> > a
>> > > better approach? Seems to handle the above issue with this patch.
>> >
>> > +1
>> >
>> > I thing, so some values should by cached, current patch could by slow.
>>
>>
>> Agreed, it can slow things down a bit especially since we are only
>> interested in the COERCION_PATH_FUNC case. What we need is a much simpler
>> pathway function which searches in the SysCache and returns back with the
>> valid/invalid castfunc immediately.
>>
>> PFA, version 2.0 of this patch with these changes in place. I could have
>> added a generic function in parse_coerce.c, but thought the use case was
>> restricted to plpgsql and hence I have kept it within pl_exec.c for now.
>
> Where are we on this? 8.5?
>
> --
> Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
> EnterpriseDB http://enterprisedb.com
>
> + If your life is a hard drive, Christ can be your backup. +
>


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Nikhil Sontakke <nikhil(dot)sontakke(at)enterprisedb(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: plpgsql: numeric assignment to an integer variable errors out
Date: 2010-02-27 03:12:38
Message-ID: 201002270312.o1R3Ccj22792@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


Whatever happened to this patch?

---------------------------------------------------------------------------

Nikhil Sontakke wrote:
> Hi,
>
> <nikhil(dot)sontakke(at)enterprisedb(dot)com> wrote:
>
> > The following plpgsql function errors out with cvs head:
> >
> > CREATE function test_assign() returns void
> > AS
> > $$ declare x int;
> > BEGIN
> > x := 9E3/2;
> > END
> > $$ LANGUAGE 'plpgsql';
> >
> > postgres=# select test_assign();
> > ERROR: invalid input syntax for integer: "4500.0000000000000000"
> > CONTEXT: PL/pgSQL function "test_assign" line 3 at assignment
> >
> > We do have an existing cast from numeric to type integer. But here
> > basically we convert the value to string in exec_cast_value before calling
> > int4in. And then use of strtol in pg_atoi leads to this complaint. Guess
> > converting the value to string is not always a good strategy.
> >
>
> PFA, patch which uses find_coercion_pathway to find a direct
> COERCION_PATH_FUNC function and uses that if it is available. Or is there a
> better approach? Seems to handle the above issue with this patch.
>
> Regards,
> Nikhils
> --
> http://www.enterprisedb.com

[ Attachment, skipping... ]

>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com
PG East: http://www.enterprisedb.com/community/nav-pg-east-2010.do
+ If your life is a hard drive, Christ can be your backup. +


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bruce Momjian <bruce(at)momjian(dot)us>
Cc: Nikhil Sontakke <nikhil(dot)sontakke(at)enterprisedb(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: plpgsql: numeric assignment to an integer variable errors out
Date: 2010-02-27 03:27:16
Message-ID: 11846.1267241236@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Bruce Momjian <bruce(at)momjian(dot)us> writes:
> Whatever happened to this patch?

I think we bounced it on the grounds that it would represent a
fundamental change in plpgsql behavior and break a whole lot of
applications. People have been relying on plpgsql's coerce-via-IO
assignment behavior for ten years. If you prefer coerce via
cast conversion, you can get that by writing an explicit cast.

Now it is true that a lot of the uses for that were subsumed when
we added coerce-via-IO to the native cast capabilities; but I'm
still quite scared of what this would break, and I don't see any
field demand for a change.

regards, tom lane


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Nikhil Sontakke <nikhil(dot)sontakke(at)enterprisedb(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: plpgsql: numeric assignment to an integer variable errors out
Date: 2010-02-27 03:29:30
Message-ID: 201002270329.o1R3TUV26096@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Tom Lane wrote:
> Bruce Momjian <bruce(at)momjian(dot)us> writes:
> > Whatever happened to this patch?
>
> I think we bounced it on the grounds that it would represent a
> fundamental change in plpgsql behavior and break a whole lot of
> applications. People have been relying on plpgsql's coerce-via-IO
> assignment behavior for ten years. If you prefer coerce via
> cast conversion, you can get that by writing an explicit cast.
>
> Now it is true that a lot of the uses for that were subsumed when
> we added coerce-via-IO to the native cast capabilities; but I'm
> still quite scared of what this would break, and I don't see any
> field demand for a change.

Thanks. Sorry to be asking so many questions but it is the only way I
can be sure we have covered everything.

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com
PG East: http://www.enterprisedb.com/community/nav-pg-east-2010.do
+ If your life is a hard drive, Christ can be your backup. +


From: Nikhil Sontakke <nikhil(dot)sontakke(at)enterprisedb(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Bruce Momjian <bruce(at)momjian(dot)us>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: plpgsql: numeric assignment to an integer variable errors out
Date: 2010-03-02 06:48:22
Message-ID: a301bfd91003012248q6dbafb4bh8e4a826b63e5489a@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi,

> Now it is true that a lot of the uses for that were subsumed when
> we added coerce-via-IO to the native cast capabilities; but I'm
> still quite scared of what this would break, and I don't see any
> field demand for a change.

Well, we have had one of our EDB connectors facing issues because of
this existing conversion mechanism.

I think this is a small patch which tries to do the "right" thing, no?

Regards,
Nikhils
--
http://www.enterprisedb.com