X-Original-To: pgsql-interfaces-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 0A32FD1B269 for ; Wed, 16 Jun 2004 15:16:40 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 03776-08 for ; Wed, 16 Jun 2004 18:16:27 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 1353AD1B187 for ; Wed, 16 Jun 2004 15:15:59 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.11/8.12.11) with ESMTP id i5GIFvuu023855; Wed, 16 Jun 2004 14:15:57 -0400 (EDT) To: Rich Cullingford Cc: pgsql-interfaces@postgresql.org Subject: Re: Problems creating indexes with IMMUTABLE functions In-reply-to: <40D06BEC.3060102@sysd.com> References: <40D06BEC.3060102@sysd.com> Comments: In-reply-to Rich Cullingford message dated "Wed, 16 Jun 2004 11:49:00 -0400" Date: Wed, 16 Jun 2004 14:15:57 -0400 Message-ID: <23854.1087409757@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200406/45 X-Sequence-Number: 4973 Rich Cullingford writes: > sysd=> create index bnoz_idx on evidence (bnoz(alert_date, period)); > ERROR: functions in index expression must be marked IMMUTABLE How is bnoz declared, exactly? You did not show us the function signature. > sysd=> create index bnoz_idx on evidence ((alert_date || ' ' || period)); > ERROR: functions in index expression must be marked IMMUTABLE Assuming alert_date is of type DATE, this would fail because the date-to-text coercion function is not immutable (for the simple reason that its results depend on the DateStyle variable as well as the input value). I am guessing that bnoz is declared to take type text as its first argument, which means that the above index declaration includes an implicit date-to-text coercion as part of the index expression, which quite rightly causes the CREATE INDEX to fail. You'd not want your index to break if you changed DateStyle. When I tried to duplicate this, I declared bnoz as taking type DATE, and the CREATE INDEX worked just fine. However, I then had a time bomb on my hands, because the index entries in fact depended on the setting of DateStyle --- the internal conversion occurring inside bnoz isn't immutable, and so I was lying to claim that bnoz was. The safe way to approach this would be to declare bnoz to take date, and be careful to do the text conversion inside it in a DateStyle-independent manner, perhaps using to_char(). Better watch out for TimeZone dependencies, too. I think you'd need to write something like to_char($1::timestamp without time zone, 'YYYY/MM/DD') to be sure about that. (Of course you can pick any date format you like here, you just can't change your mind without rebuilding the index.) regards, tom lane