Received: from localhost (unknown [200.46.204.183]) by developer.postgresql.org (Postfix) with ESMTP id 434F02E003E for ; Sun, 4 May 2008 16:05:29 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 14605-05 for ; Sun, 4 May 2008 16:05:23 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from mailrelay.edis.at (mailrelay.edis.at [81.223.48.5]) by developer.postgresql.org (Postfix) with ESMTP id A7BD82E0030 for ; Sun, 4 May 2008 16:05:25 -0300 (ADT) Received: from ws001.ltsp (91.83.18.18.pool.invitel.hu [::ffff:91.83.18.18]) (AUTH: CRAM-MD5 zb@cybertec.at, TLS: TLSv1/SSLv3,256bits,AES256-SHA) by mailrelay.edis.at with esmtp; Sun, 04 May 2008 21:05:22 +0200 id 000E7477.481E08F3.00001D68 Message-ID: <481E08F1.3000604@cybertec.at> Date: Sun, 04 May 2008 21:05:21 +0200 From: Zoltan Boszormenyi User-Agent: Thunderbird 2.0.0.12 (X11/20080226) MIME-Version: 1.0 To: Hannes Dorbath CC: "Joshua D. Drake" , pgsql-performance@postgresql.org Subject: Re: Fastest way / best practice to calculate "next birthdays" References: <481DE036.90706@commandprompt.com> <481DF76D.3060807@theendofthetunnel.de> In-Reply-To: <481DF76D.3060807@theendofthetunnel.de> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200805/48 X-Sequence-Number: 30016 Hannes Dorbath =EDrta: > Joshua D. Drake wrote: >> postgres=3D# SELECT current_date|| ' a ' || to_char(current_date, 'Day= '), >> current_date + '1 Year'::interval || ' a ' || to_char(current_date + = >> '1 Year'::interval, 'Day') as next_birthday; >> ?column? | next_birthday >> ------------------------+--------------------------------- >> 2008-05-04 a Sunday | 2009-05-04 00:00:00 a Monday >> >> ? > > Sorry, I think I phrased the question badly. What I'm after basically i= s: > > http://www.depesz.com/index.php/2007/10/26/who-has-birthday-tomorrow/ If you define the same functional index as in the above link: CREATE OR REPLACE FUNCTION indexable_month_day(date) RETURNS TEXT as $BOD= Y$ SELECT to_char($1, 'MM-DD'); $BODY$ language 'sql' IMMUTABLE STRICT; create table user_birthdate ( id serial not null primary key, birthdate date ); create index user_birthdate_day_idx on user_birthdate (=20 indexable_month_day(birthdate) ); Then you can use this query: select count(*) from user_birthdate where indexable_month_day(birthdate) = > '02-28' and indexable_month_day(birthdate) <=3D '03-01'; In a generic and parametrized way: select * from user_birthdate where indexable_month_day(birthdate) > indexable_month_day(now()::date) and indexable_month_day(birthdate) <=3D indexable_month_day((now() + '1=20 days'::interval)::date); This will still use the index and it will work for the poor ones who have birthday every 4 years, too. Assume, it's 02-08 today, 03-01=20 the next day. The now() < X <=3D now() + 1 day range will find 02-29. --=20 ---------------------------------- Zolt=E1n B=F6sz=F6rm=E9nyi Cybertec Sch=F6nig & Sch=F6nig GmbH http://www.postgresql.at/