Re: Unexpected behaviour of numeric datatype when mixed with,float4, domains and plpgsql variables - Mailing list pgsql-bugs

From Tom Lane
Subject Re: Unexpected behaviour of numeric datatype when mixed with,float4, domains and plpgsql variables
Date
Msg-id 16029.1113167894@sss.pgh.pa.us
Whole thread Raw
In response to Unexpected behaviour of numeric datatype when mixed with,float4, domains and plpgsql variables  (Ezequiel Tolnay <mail@etolnay.com.ar>)
Responses Re: Unexpected behaviour of numeric datatype when mixed with,float4,
List pgsql-bugs
Ezequiel Tolnay <mail@etolnay.com.ar> writes:
> CREATE DOMAIN currency AS numeric(15,4);
> CREATE TABLE test (id serial, amt currency);
> CREATE FUNCTION f_test(currency) RETURNS currency AS $$
> DECLARE n currency;
> BEGIN n := $1 * 0.2::float4;
>        INSERT INTO test (amt) VALUES (n); RETURN n;
> END $$ LANGUAGE PLPGSQL;

plpgsql doesn't currently enforce domain constraints, so the assignment
to n isn't doing the rounding that you expect.  Until someone gets
around to fixing that, an explicit coercion is probably what you need:

    n := cast($1 * 0.2::float4 AS currency);

Keep in mind also that declaring a function result value as a domain
is pretty dangerous, because none of the PLs enforce domain constraints
on their results.

            regards, tom lane

pgsql-bugs by date:

Previous
From: Tom Lane
Date:
Subject: Re: BUG #1591: BETWEEN NULL AND NULL causes crash
Next
From: Tom Lane
Date:
Subject: Re: BUG #1584: undefined symbol _tas