Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YJqI2-0003a5-RC for pgsql-bugs@arkaria.postgresql.org; Fri, 06 Feb 2015 21:18:14 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YJqI0-00022m-PG for pgsql-bugs@arkaria.postgresql.org; Fri, 06 Feb 2015 21:18:12 +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 1YJPZ9-0000km-Qe for pgsql-bugs@postgresql.org; Thu, 05 Feb 2015 16:46:07 +0000 Received: from smarthost01a.mail.zen.net.uk ([212.23.1.1]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YJPZ6-0001a8-AK for pgsql-bugs@postgresql.org; Thu, 05 Feb 2015 16:46:06 +0000 Received: from [88.96.211.230] (helo=ash.forest) by smarthost01a.mail.zen.net.uk with esmtps (TLS1.2:RSA_AES_128_CBC_SHA1:128) (Exim 4.80) (envelope-from ) id 1YJPZ4-0000Qa-EI for pgsql-bugs@postgresql.org; Thu, 05 Feb 2015 16:46:02 +0000 Received: from ossl.co.uk ([88.96.211.230] helo=[10.0.0.12]) by ash.forest with esmtpa (Exim 4.84) (envelope-from ) id 1YJPZ3-00043K-Of for pgsql-bugs@postgresql.org; Thu, 05 Feb 2015 16:46:02 +0000 Message-ID: <54D39E4A.9030600@opalsoftware.co.uk> Date: Thu, 05 Feb 2015 16:46:02 +0000 From: Mark Simonetti User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: pgsql-bugs@postgresql.org Subject: Function parameter type precision modifiers ignored. Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "ash.forest", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see @@CONTACT_ADDRESS@@ for details. Content preview: Hi, I don't know if this is a bug as such, but the behaviour certainly confused me for a while : - Given the following PostgreSQL functions: CREATE OR REPLACE FUNCTION fn_dtm ( dtm timestamptz(0)) RETURNS void AS $$ BEGIN RAISE NOTICE 'fn: %, %', dtm, dtm::timestamptz(0); END; $$ LANGUAGE plpgsql; [...] Content analysis details: (-0.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 2.7 DNS_FROM_AHBL_RHSBL RBL: Envelope sender listed in dnsbl.ahbl.org [listed in opalsoftware.co.uk.rhsbl.ahbl.org. IN] [A] -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] X-Originating-smarthost01a-IP: [88.96.211.230] 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 Hi, I don't know if this is a bug as such, but the behaviour certainly confused me for a while : - Given the following PostgreSQL functions: CREATE OR REPLACE FUNCTION fn_dtm ( dtm timestamptz(0)) RETURNS void AS $$ BEGIN RAISE NOTICE 'fn: %, %', dtm, dtm::timestamptz(0); END; $$ LANGUAGE plpgsql; CREATE OR REPLACE FUNCTION fn_num ( num numeric(5, 2)) RETURNS void AS $$ BEGIN RAISE NOTICE 'num: %, %', num, num::numeric(5, 2); END; $$ LANGUAGE plpgsql; Would you expect the output of these functions to show the result as per the type declared in the function parameter? I was very surprised to find that the precision was retained and shown in the "NOTICE" despite the parameter type limiting the precision. Is this a bug or am I just thinking about it the wrong way? Here is the output: db=> SELECT fn_dtm(now()); NOTICE: fn: 2015-02-05 10:25:44.184+00, 2015-02-05 10:25:44+00 db=> SELECT fn_num(1.23456789); NOTICE: num: 1.23456789, 1.23 I am using PostgreSQL 9.3. CREATE OR REPLACE FUNCTION fn_dtm ( dtm timestamptz(0)) RETURNS void AS $$ BEGIN dtm = dtm::timestamptz(0); ----- CHANGE PRECISION RAISE NOTICE 'fn: %', dtm; END; $$ LANGUAGE plpgsql; Gives: db=> SELECT fn_dtm(now()); NOTICE: fn: 2015-02-05 10:38:38+00 I don't know if the behaviour is documented anywhere (sorry if I've missed it), but not knowing this really confused me for a good couple of hours. For fun here was the scenario : - 1) For my monitoring system I have a readings table. In my readings table I store the timestamp only to a 1 second precision (timestamptz(0)). 2) When a new "live" reading comes in, I use "now()" to get the timestamp. I then pass this to another function with the reading parameters, including a time argument (of type timestamptz(0)) which gets passed now()). Lets say now() is 2015-02-05 16:35:38.923. 3) The new reading gets stored in the database, and since the field type is timestamptz(0) it gets rounded up to (2015-02-05 16:35:39). 4) If the new reading indicates an alarm state, I do a pg_notify to send an event to my app which includes the passed in timestamp. Now I assumed because of the parameter type this would not include milliseconds, so naturally I ignore anything after the seconds when I parse the notification event.. So I end up with 2015-02-05 16:35:38. Spot the difference to the stored timestamp? 5) The application then periodically loads the readings in alarm from the database based on the events received. So I do SELECT ..... WHERE reading_dtm = '2015-02-05 16:35:38' etc. Obviously nothing is returned as the timestamp is out by one second (except where the original reading timestamp was not rounded up). I've fixed it by just doing p_reading_dtm = p_reading_dtm::timestamptz(0) at the start of my function. Seems odd though when the parameter type is already timestamptz(0). Regards, Mark. -- -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs