Re: ERROR: value out of range: underflow

From: Thom Brown <thombrown(at)gmail(dot)com>
To: Anton Maksimenkov <anton200(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: ERROR: value out of range: underflow
Date: 2010-02-26 12:15:59
Message-ID: bddc86151002260415j65a0787bs91bc3a102ed74b2a@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 26 February 2010 12:02, Anton Maksimenkov <anton200(at)gmail(dot)com> wrote:
> Hi.
>
> I have a simple function.
> CREATE OR REPLACE FUNCTION myf_convert_phone18digits(
>  in_phone VARCHAR
> ) RETURNS BIGINT
> -- IMMUTABLE
> AS $$
> DECLARE
>  t_extent_len BIGINT;
>  t_phone_18 BIGINT;
>  t_multiplier BIGINT;
> BEGIN
>
>  IF in_phone IS NULL OR in_phone = '' THEN
>        RAISE EXCEPTION 'in_phone[%] IS NULL OR =''''!', in_phone;
>  END IF;
>
>  t_extent_len := 18 - length(in_phone);
>  t_multiplier := 10::BIGINT ^ t_extent_len::BIGINT; --<<< ERROR HERE
>  t_phone_18 := (in_phone)::BIGINT;
>  t_phone_18 := t_phone_18 * t_multiplier;
>
>  RETURN t_phone_18;
>
> END;
> $$ LANGUAGE plpgsql;
>
>
> When I try it in pgAdmin, first time it show me error:
> --
> ERROR:  value out of range: underflow
> CONTEXT:  PL/pgSQL function "myf_convert_phone18digits" line 12 at assignment
> --
> SECOND (and consequences) time it works and just show result 771506000000000000
>
> I get same problem when I try to call query from web (php).
> I can't do "second time" here, so web always fail with "ERROR:  value
> out of range: underflow"
>
> What's the problem? What can I do with it?
> --
> antonvm
>

If t_extent_len is 19 or more, it will fail as it will exceed the
maximum range of bigint.

Are you sure you wish to perform calculations on a phone number? Why
not pad it out? Like: select rpad('2329382',18,'0') which would
return "232938200000000000"

Regards

Thom

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ivan Sergio Borgonovo 2010-02-26 14:23:24 Re: C function to create tsquery not working
Previous Message Anton Maksimenkov 2010-02-26 12:02:30 ERROR: value out of range: underflow