Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 1A0F212A65BC for ; Sat, 2 Jun 2012 14:51:12 -0300 (ADT) Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SasTj-0006Xb-2Q for pgsql-bugs@postgresql.org; Sat, 02 Jun 2012 17:51:11 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id q52Hos2O020574; Sat, 2 Jun 2012 13:50:54 -0400 (EDT) To: mfork00@yahoo.com cc: pgsql-bugs@postgresql.org Subject: Re: BUG #6673: Value out of range for type integer when adding WHERE clause In-reply-to: References: Comments: In-reply-to mfork00@yahoo.com message dated "Sat, 02 Jun 2012 17:34:13 -0000" Date: Sat, 02 Jun 2012 13:50:54 -0400 Message-ID: <20573.1338659454@sss.pgh.pa.us> From: Tom Lane X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201206/9 X-Sequence-Number: 33236 mfork00@yahoo.com writes: > However, the following query, which is just the above query with 'WHERE > card.id IS NULL' tacked on to the end fails with an integer out of range. > Important to know is that parsecardidfromreferencecode will return a valid > integer ONLY for the rows matching the WHERE clause. However, I cannot see > how adding the WHERE clause causes that error. It looks like the planner has chosen to do the joins in a different order here; the join to activation_event is now done last, so that the value of the function might be needed for rows it was not needed for otherwise. This could have occurred in the other version of the query too; it's all about relative row counts. Why don't you cast to bigint rather than integer, seeing that the function is evidently quite capable of returning a bigint value? If you really must use a join condition involving a function that can fail for some inputs, possibly the best solution is to mark the function volatile so that the planner will avoid rearranging stuff that uses it. This might do some considerable damage to the overall quality of the plan though. regards, tom lane