Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Xv6yj-0003Rk-0f for pgsql-bugs@arkaria.postgresql.org; Sun, 30 Nov 2014 16:04:05 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Xv6yi-0002HC-8h for pgsql-bugs@arkaria.postgresql.org; Sun, 30 Nov 2014 16:04:04 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XugtM-0006T8-0B for pgsql-bugs@postgresql.org; Sat, 29 Nov 2014 12:12:48 +0000 Received: from mail-ig0-f173.google.com ([209.85.213.173]) by magus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XugtH-0008P6-IR for pgsql-bugs@postgresql.org; Sat, 29 Nov 2014 12:12:46 +0000 Received: by mail-ig0-f173.google.com with SMTP id r2so10937773igi.12 for ; Sat, 29 Nov 2014 04:12:40 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:mime-version:references:in-reply-to:date :message-id:subject:to:cc:content-type; bh=sIZkansIoPzSaKe7yD8Cs7LwIX1f/7vB/NsOTDj2EIc=; b=bkzcC0zF9CXzsRzCFTrc8jp0KsU21M/5NxiJqk3lbUS3cDAgyrtVi06Zfxxk2FjUfZ HTv4JT7guzCOeQJgNyduGQ591A6cPkJyS85dlddRHMAs8tVxRiqi1VXJOCDI0qRDlhYj dW10dj0v5Jiwau/Yc7kge34Iq9EfEOLbYn4UcQmn4eVr0+E4Fho2oDH1ipbfl+VpwhAA 6BfQ2vMY4tb0NeIQ9KsC9u9gw9es4FXU6+wv2sBA+U+vkQJxzzfhho8LBC6qUSRzCWDa 2RR9F8LsHNg2pmNld4y79PN3Bxr+0F/aE/ISA/I1Wsm/rhIa5RZvfsdg6+0Ot+VN1T5S ExPA== X-Gm-Message-State: ALoCoQkOGQGkvczHKRhywvoN8xLe9MoqMLpO5toJDiQ4QZXMgOuFul7L8HIl+F2rLhta0yGQoA4b X-Received: by 10.50.43.197 with SMTP id y5mr36993676igl.33.1417263160555; Sat, 29 Nov 2014 04:12:40 -0800 (PST) From: Tommaso Sala Mime-Version: 1.0 (1.0) References: <20141125111529.15508.34881@wrigleys.postgresql.org> <14914.1417017051@sss.pgh.pa.us> <15613.1417121313@sss.pgh.pa.us> <20726.1417220254@sss.pgh.pa.us> <25038.1417228384@sss.pgh.pa.us> In-Reply-To: <25038.1417228384@sss.pgh.pa.us> Date: Sat, 29 Nov 2014 13:12:39 +0100 Message-ID: <-8362677256987463621@unknownmsgid> Subject: Re: BUG #12053: Strange behavior for numeric types with unspecified precision-scale To: Tom Lane Cc: "pgsql-bugs@postgresql.org" Content-Type: multipart/alternative; boundary=089e011826083d1d820508fe4b19 X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --089e011826083d1d820508fe4b19 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable The strange fact is that if you try to write 0.000001, it gets displayed correctly I'd agree with your proposed solution, if I only set a dscale of 2. But I didn't. The column type was set as just numeric. The documentation for numeric says: Specifying: NUMERIC without any precision or scale creates a column in which numeric values of any precision and scale can be stored, up to the implementation limit on precision. A column of this kind will not coerce input values to any particular scale, whereas numeric columns with a declared scale will coerce input values to that scale. So I'd expect a "just numeric" column to contain almost everything! And this is what I see for almost every value, but 0.001. As I said before 0.000001 gets displayed correctly and 0.0009895846 do as well! *Tommaso Sala* *Developer* ------------------------------ *Computer Line Associates* www.cla-it.eu Via della Viggioletta, 8 29121 Piacenza, Italy tel. +39 (0523) 1865038 *Nota di riservatezza:* Il presente messaggio, corredato dei relativi allegati, contiene informazioni da considerarsi strettamente riservate, ed =C3=A8 destinato esclusivamente al destinatario sopra indicato, il quale = =C3=A8 l'unico autorizzato ad usarlo, copiarlo e, sotto la propria responsabilit= =C3=A0, diffonderlo. Chiunque ricevesse questo messaggio per errore o comunque lo leggesse senza esserne legittimato =C3=A8 avvertito che trattenerlo, copiar= lo, divulgarlo, distribuirlo a persone diverse dal destinatario =C3=A8 severame= nte proibito, ed =C3=A8 pregato di rinviarlo immediatamente al mittente distruggendone l'originale. Grazie. *Confidentiality notice:* This message, together with its attachments, contains strictly confidential information and is intended only for the addressee identified above, who is the sole party authorized to use and copy it and, assuming any related liability, to forward it to others. Anyone receiving this message by mistake or reading it without authorization is hereby notified that storage, reproduction, disclosure or distribution of the message to persons other than the addressee is strictly forbidden. They are asked to return the message immediately to the sender and to erase the original message received. Thank you. Il giorno 29/nov/2014, alle ore 03:33, Tom Lane ha scritto: I wrote: So the question is whether it's worth adding logic to numeric_recv to guard against bogus dscale values. I think that detecting this case would probably add a noticeable number of cycles to numeric_in. (We can't just count the number of digits received, since (a) trailing zeroes past dscale are OK, and (b) what we have at this point is base-10000 digits not base-10 digits...) I guess we're usually willing to expend cycles to guard against client error, so maybe we should do it here too. I had originally been thinking of throwing an error if the presented dscale was too small for the number of digits sent, but after some reflection it seems like it'd be safer to just silently truncate the extra digits away. If we throw an error it's likely to break applications that are dependent on this buggy data adapter, and I'm not sure that the users will thank us for that. Truncating the extra digits will make the value actually match what it would've printed as, and if we grant that the Devart folk did any testing of their code at all, they probably looked at what was printed and thought that that was what they intended. That is, I'm assuming that dscale =3D 2 means they only want 2 decimal places in the value. So I propose the attached patch, which requires only a minimal amount of new code and is about as fast as we're going to get if we want to check this issue at all. (Note: the apparently new error condition for out-of-range dscale doesn't create a backwards compatibility hazard, because make_result would've barfed anyway. This is just a more to-the-point error message.) regards, tom lane diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index d61af92..c73f9bc 100644 *** a/src/backend/utils/adt/numeric.c --- b/src/backend/utils/adt/numeric.c *************** numeric_recv(PG_FUNCTION_ARGS) *** 717,722 **** --- 717,724 ---- alloc_var(&value, len); value.weight =3D (int16) pq_getmsgint(buf, sizeof(int16)); + /* we allow any int16 for weight --- OK? */ + value.sign =3D (uint16) pq_getmsgint(buf, sizeof(uint16)); if (!(value.sign =3D=3D NUMERIC_POS || value.sign =3D=3D NUMERIC_NEG || *************** numeric_recv(PG_FUNCTION_ARGS) *** 726,731 **** --- 728,738 ---- errmsg("invalid sign in external \"numeric\" value"))); value.dscale =3D (uint16) pq_getmsgint(buf, sizeof(uint16)); + if ((value.dscale & NUMERIC_DSCALE_MASK) !=3D value.dscale) + ereport(ERROR, + (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION), + errmsg("invalid scale in external \"numeric\" value"))); + for (i =3D 0; i < len; i++) { NumericDigi= t d =3D pq_getmsgint(buf, sizeof(NumericDigit)); *************** numeric_recv(PG_FUNCTION_ARGS) *** 737,742 **** --- 744,757 ---- value.digits[i] =3D d; } + /* + * If the given dscale would hide any digits= , truncate those digits away. + * We could alternatively throw an error, but that would take a bunch of + * extra code (about as much as trunc_var involves), and it might cause + * client compatibility issues. + */ + trunc_var(&value, value.dscale); + apply_typmod(&value, typmod); res =3D make_result(&value); --089e011826083d1d820508fe4b19 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
The strange fact is that if you tr= y to write 0.000001, it gets displayed correctly

I'd agre= e with your proposed solution, if I only set a dscale of 2.
But I= didn't. The column type was set as just numeric.
The documen= tation for numeric says:

Speci= fying:
NUMERIC

without any precision or scale creates a column= in which numeric values of any precision and scale can be stored, up to th= e implementation limit on precision. A column of this kind will not coerce = input values to any particular scale, whereas=C2=A0numer= ic=C2=A0columns with a declared scale will coerce input values to that= scale.

So I'd expect a "just numeric= " column to contain almost everything!
And this is what I see for = almost every value, but 0.001.
As I said before 0.000001 gets dis= played correctly and 0.0009895846 do as well!

Tommaso Sala
Devel= oper


Computer Line Associates
www.cla-it.eu

Via della Viggioletta, 8
29121= Piacenza, Italy
tel.=C2=A0+39 (0523) 186503= 8



=

Nota di riservatezza:=C2=A0Il prese= nte messaggio, corredato dei relativi allegati, contiene informazioni da co= nsiderarsi strettamente riservate, ed =C3=A8 destinato esclusivamente al de= stinatario sopra indicato, il quale =C3=A8 l'unico autorizzato ad usarl= o, copiarlo e, sotto la propria responsabilit=C3=A0, diffonderlo. Chiunque = ricevesse questo messaggio per errore o comunque lo leggesse senza esserne = legittimato =C3=A8 avvertito che trattenerlo, copiarlo, divulgarlo, distrib= uirlo a persone diverse dal destinatario =C3=A8 severamente proibito, ed = =C3=A8 pregato di rinviarlo immediatamente al mittente distruggendone l'= ;originale.=C2=A0Grazie.

= Confidentiality notice:=C2=A0This message, together with its attachment= s, contains=C2=A0strictly=C2=A0confidential information and is=C2=A0intende= d only=C2=A0for the addressee identified above, who is the sole party autho= rized=C2=A0to use and copy it and,=C2=A0assuming any related liability, to = forward=C2=A0it to others.=C2=A0Anyone receiving this message by mistake or= reading it without authorization is hereby notified=C2=A0that=C2=A0storage= , reproduction, disclosure or=C2=A0distribution of the=C2=A0message to pers= ons other than the addressee is strictly forbidden. They are=C2=A0asked to= =C2=A0return the message=C2=A0immediately to the sender and to erase the or= iginal message received.=C2=A0Thank you.


Il = giorno 29/nov/2014, alle ore 03:33, Tom Lane <tgl@sss.pgh.pa.us> ha scritto:

I wrote:
= So the question is whether it's worth adding logic to numeric_recv
to guard against bogus d= scale values.=C2=A0 I think that detecting this
case would probably add a noticeable number of = cycles to numeric_in.
(We can't just count the number of digits received, since (a) trailin= g
zeroes past dscale= are OK, and (b) what we have at this point is
base-10000 digits not base-10 digits...) =C2=A0I= guess we're usually willing
to expend cycles to guard against client error, so maybe we sh= ould
do it here too.=

I had originally been thinki= ng of throwing an error if the presented
dscale was too sma= ll for the number of digits sent, but after some
reflection= it seems like it'd be safer to just silently truncate the
extra digits away.=C2=A0 If we throw an error it's likely to break = applications
that are dependent on this buggy data adapter,= and I'm not sure that the
users will thank us for that= .=C2=A0 Truncating the extra digits will make the
value act= ually match what it would've printed as, and if we grant that the
Devart folk did any testing of their code at all, they probably = looked at
what was printed and thought that that was what t= hey intended.=C2=A0 That is,
I'm assuming that dscale = =3D 2 means they only want 2 decimal places in
the value.

So I propose the attached patch, which requ= ires only a minimal amount
of new code and is about as fast= as we're going to get if we want to
check this issue a= t all. =C2=A0(Note: the apparently new error condition
for = out-of-range dscale doesn't create a backwards compatibility hazard,

because make_result would've barfed anyway.=C2=A0 This is= just a more
to-the-point error message.)
<= /span>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0regards, tom lane=

diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/num= eric.c index d61af92..c73f9bc 100644 *** a/src/backend/utils/adt/numeric.c --- b/src/backend/utils/adt/numeric.c *************** numeric_recv(PG_FUNCTION_ARGS) *** 717,722 **** --- 717,724 ---- alloc_var(&value, len); =20 value.weight =3D (int16) pq_getmsgint(buf, sizeof(int16)); + /* we allow any int16 for weight --- OK? */ +=20 value.sign =3D (uint16) pq_getmsgint(buf, sizeof(uint16)); if (!(value.sign =3D=3D NUMERIC_POS || value.sign =3D=3D NUMERIC_NEG || *************** numeric_recv(PG_FUNCTION_ARGS) *** 726,731 **** --- 728,738 ---- errmsg("invalid sign in external \"numeric\" value&qu= ot;))); =20 value.dscale =3D (uint16) pq_getmsgint(buf, sizeof(uint16)); + if ((value.dscale & NUMERIC_DSCALE_MASK) !=3D value.dscale) + ereport(ERROR, + (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION), + errmsg("invalid scale in external \"numeric\" value&q= uot;))); +=20 for (i =3D 0; i < len; i++) { NumericDigit d =3D pq_getmsgint(buf, sizeof(NumericDigit)); *************** numeric_recv(PG_FUNCTION_ARGS) *** 737,742 **** --- 744,757 ---- value.digits[i] =3D d; } =20 + /* + * If the given dscale would hide any digits, truncate those digits away= . + * We could alternatively throw an error, but that would take a bunch of + * extra code (about as much as trunc_var involves), and it might cause + * client compatibility issues. + */ + trunc_var(&value, value.dscale); +=20 apply_typmod(&value, typmod); =20 res =3D make_result(&value);
--089e011826083d1d820508fe4b19--