Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 653BB47652E for ; Wed, 15 Jan 2003 07:13:16 -0500 (EST) Received: from krogh.officenet.no (unknown [193.212.174.47]) by postgresql.org (Postfix) with ESMTP id 4609F475D64 for ; Wed, 15 Jan 2003 07:09:59 -0500 (EST) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by krogh.officenet.no (Postfix) with ESMTP id CC5FFB5BFD; Wed, 15 Jan 2003 07:08:02 -0500 (EST) From: Andreas Joseph Krogh Organization: OfficeNet AS To: Achilleus Mantzios Subject: Re: index on to_char(created, 'YYYY') doesn't work Date: Wed, 15 Jan 2003 12:08:01 +0000 User-Agent: KMail/1.5 References: In-Reply-To: Cc: pgsql-sql@postgresql.org MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Description: clearsigned data Content-Disposition: inline Message-Id: <200301151208.01894.andreak@officenet.no> X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200301/210 X-Sequence-Number: 11075 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Wednesday 15 January 2003 16:12, you wrote: > On Wed, 15 Jan 2003, Andreas Joseph Krogh wrote: > > -----BEGIN PGP SIGNED MESSAGE----- > > Hash: SHA1 > > > > On Wednesday 15 January 2003 11:37, you wrote: > > > The following does not work: > > > > > > create index session_u_idx on session (to_char(created, 'YYYY')); > > > ERROR: parser: parse error at or near "'YYYY'" at character 57 > > > > > > Can I make a function to do this and index using the result of that > > > funtion? Do anyone have an example of such a function? > > > > I tried the following function: > > - ----------------------------------------------------------------- > > create function drus (timestamp) returns varchar AS' > > DECLARE > > str_created VARCHAR; > > created ALIAS FOR $1; > > BEGIN > > str_created:=3D to_char(created, ''YYYY''); > > RETURN str_created; > > END; > > ' LANGUAGE 'plpgsql'; > > add > WITH (iscachable) Thank you, not _that_ works:-) But now this doesn't work: create index session_u_idx on session (drus(created), username); Can't I have multicolumn-indexes with functions? Any idea how to rewrite th= at=20 so it works? Here is my session table: CREATE TABLE session ( session_id varchar(256) NOT NULL PRIMARY KEY, created timestamp DEFAULT 'now' NOT NULL, last_accessed timestamp NOT NULL, destroyed timestamp NOT NULL, username varchar -- Allow sessions from not logged in users ); Here is my query I wish to optimize using indexes: SELECT to_char(created, 'IW') as week, count(session_id) from session WHERE= =20 username IS NULL and to_char(created, 'YYYY') =3D '2002' group by week ORDE= R BY=20 week; Any hints on optimizing this query, index-usage etc? - --=20 Andreas Joseph Krogh There will always be someone who agrees with you but is, inexplicably, a moron. gpg public_key: http://dev.officenet.no/~andreak/public_key.asc -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.7 (GNU/Linux) iD8DBQE+JU8hUopImDh2gfQRAuA5AKCXyqCZk92d6oCgyJ/Auf8c4xkSaQCgr4Lq /+r2WSydbYWXNomMvbmt2E8=3D =3DN6NQ -----END PGP SIGNATURE-----