Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 60DC9632C85 for ; Wed, 23 Dec 2009 10:27:45 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 07593-10 for ; Wed, 23 Dec 2009 14:27:26 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtp-out114.alice.it (smtp-out114.alice.it [85.37.17.114]) by mail.postgresql.org (Postfix) with ESMTP id 463B76323B6 for ; Wed, 23 Dec 2009 10:27:34 -0400 (AST) Received: from FBCMMO01.fbc.local ([192.168.68.195]) by smtp-out114.alice.it with Microsoft SMTPSVC(6.0.3790.3959); Wed, 23 Dec 2009 15:27:31 +0100 Received: from FBCMCL01B07.fbc.local ([192.168.171.45]) by FBCMMO01.fbc.local with Microsoft SMTPSVC(6.0.3790.3959); Wed, 23 Dec 2009 15:27:30 +0100 Received: from lan.webthatworks.it ([79.0.145.79]) by FBCMCL01B07.fbc.local with Microsoft SMTPSVC(6.0.3790.3959); Wed, 23 Dec 2009 15:27:29 +0100 Received: from dawn.webthatworks.it (dawn.webthatworks.it [192.168.1.12]) by lan.webthatworks.it (Postfix) with ESMTP id 8CE4E30056A for ; Wed, 23 Dec 2009 15:27:28 +0100 (CET) Received: from dawn.webthatworks.it (localhost [127.0.0.1]) by dawn.webthatworks.it (Postfix) with ESMTP id 564E33DA93 for ; Wed, 23 Dec 2009 15:27:28 +0100 (CET) Date: Wed, 23 Dec 2009 15:27:28 +0100 From: Ivan Sergio Borgonovo To: pgsql-sql@postgresql.org Subject: Re: short-cutting if sum()>constant Message-ID: <20091223152728.1e38dd1c@dawn.webthatworks.it> In-Reply-To: <162867790912230535m6c84a1cfodea1c269d3d84301@mail.gmail.com> References: <20091222133527.3007bd96@dawn.webthatworks.it> <162867790912221147r7e3041fej44332a05faf915d6@mail.gmail.com> <20091223000031.54ad09b5@dawn.webthatworks.it> <20091223010940.51c5c069@dawn.webthatworks.it> <20091223024635.48d66554@dawn.webthatworks.it> <162867790912222338v73261b73u5c4e88a5ce85918f@mail.gmail.com> <20091223115628.1709caf5@dawn.webthatworks.it> <162867790912230352v5b6e395fm1880111c04eaa626@mail.gmail.com> <20091223140116.1c79d821@dawn.webthatworks.it> <162867790912230535m6c84a1cfodea1c269d3d84301@mail.gmail.com> Organization: http://www.webthatworks.it X-Mailer: Claws Mail 3.7.3 (GTK+ 2.18.5; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable X-OriginalArrivalTime: 23 Dec 2009 14:27:29.0676 (UTC) FILETIME=[0EBC84C0:01CA83DC] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.282 tagged_above=-10 required=5 tests=AWL=0.317, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200912/76 X-Sequence-Number: 33514 On Wed, 23 Dec 2009 14:35:27 +0100 Pavel Stehule wrote: > a) simplicity. PLpgSQL interpret is very simple. > b) mostly types are little bit different behave than natural C > types - domains are different, C types doesn't know NULL value, ... But well... there should be a mapping somewhere between SQL int and C and it can be plainly reused. $b =3D 5 + "10"; is going to be far more problematic to execute... but I guess that's exactly the problem. create or replace function tano(z int, out b int) returns int as $$ declare x varchar(4) =3D '12'; y int =3D 10; begin b :=3D x + y; return; end; $$ language plpgsql; select * from tano(1); ERROR: operator does not exist: character varying + integer LINE 1: SELECT $1 + $2 ^ =46rom what I can guess... the interpreter is sending SQL commands to pg and waiting result back. So actually the "interpreter" isn't actually aware of data types... it just relies on SQL to spot data type mismatch. That means... that everything is converted back and forward to *text*. On the other side once a SQL command is interpreted it actually knows what data types are and can pretty quickly sum int to int just taking care of nulls and overflows that would be anyway problems for any other language trying to sum "SQL ints". Even if it had to sum int and bigint the code could be optimised for just that and the need of a cast should be known in advance before every "loop". Did I get it? That's what you were trying to make me understand with: > It little bit nonsense. On 99% plpgsql use SPI api and work with > variables via query interface. PLpgSQL can do loop statement, if > statement, but for all others use internal query based API. > this query is little but different, than you original request, but > it could work for you. Yep... making clear a is an int simplify the problem quite a lot. But you couldn't use generate_series if a was not an int. thanks --=20 Ivan Sergio Borgonovo http://www.webthatworks.it