avg() bug?

Started by Patrik Kudoabout 25 years ago3 messagesgeneral
Jump to latest
#1Patrik Kudo
kudo@partitur.se

Hi!

I'm not sure if this is a bug, but it's sure unpleasant (for me).

(pseudocode)
create table t (a int2);

insert into t values (random number); <-- performed many, many times

now the following will return very unpredictable values:

select avg(a) from t;

while the following behaves as I would expect:

select avg(a::int4) from t;

I suppose avg() does a sum()/count() where sum returns a int2,
which will be too small to contain the sum if the table contains
many values?

Is this something I must know as a user, or is it a bug?

Regards,
Patrik Kudo

--
ech`echo xiun|tr nu oc|sed 'sx\([sx]\)\([xoi]\)xo un\2\1 is xg'`ol
K�nns det oklart? Fr�ga p�!

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Patrik Kudo (#1)
Re: avg() bug?

Patrik Kudo <kudo@partitur.se> writes:

I suppose avg() does a sum()/count() where sum returns a int2,
which will be too small to contain the sum if the table contains
many values?

Yes. This is changed in 7.1 --- sum() and avg() of int values now
return type 'numeric'.

regards, tom lane

#3Patrik Kudo
kudo@partitur.se
In reply to: Tom Lane (#2)
Re: avg() bug?

Thanks Tom!

I'll use sum(a::int4) until 7.1 then.

Regards,
Patrik Kudo

ech`echo xiun|tr nu oc|sed 'sx\([sx]\)\([xoi]\)xo un\2\1 is xg'`ol
K�nns det oklart? Fr�ga p�!

On Mon, 5 Mar 2001, Tom Lane wrote:

Show quoted text

Patrik Kudo <kudo@partitur.se> writes:

I suppose avg() does a sum()/count() where sum returns a int2,
which will be too small to contain the sum if the table contains
many values?

Yes. This is changed in 7.1 --- sum() and avg() of int values now
return type 'numeric'.

regards, tom lane