pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Herouth Maoz <herouth@oumail.openu.ac.il>
To: Steven Bradley <sbradley@llnl.gov>
To: Esteban Chiner Sanz <echiner@tissat.es>
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] Problem with timestamp and primary key.
Date: Tue, 13 Jul 1999 19:06:54 +0300
Message-ID: <l03130302b3b10f688e62@[147.233.159.109]> (raw)
In-Reply-To: <3.0.5.32.19990713082450.00927100@poptop.llnl.gov>
References: <378AE74F.C804CB78@tissat.es>

At 18:24 +0300 on 13/07/1999, Steven Bradley wrote:


> I have the same problem, except that my TIMESTAMP column is not the PK;
> instead it's just a column that I wanted to index.  Unfortunately, there is
> no solution since Postgres currently does not support indexes on TIMESTAMPS
> (Postgres automatically tries to create an index on all PKs) and does not
> provide JDBC access to DATETIME columns.  For your particular situation you
> might try placing the TIMESTAMP column outside the PK (and not have it
> indexed) and then use a surrogate key with a sequence.  This isn't entirely
> normalized, but I've seen worse!

I think I have a good solution.

The column in the database should be datetime, because that's what you can
index. Right?

The column returned from a query should be timestamp, because that's what
JDBC parses correctl. Right?

So, when you are doing something like:

SELECT the_datetime_col, other_cols
FROM the_table
WHERE the_datetime_col = 'some value';

You should put a function that converts the_datetime_col to timestamp. But
only in the returned columns! If you put a conversion function in the WHERE
clause, the index will not be used.

But how to convert? timestamp( the_datetime_col) doesn't work (Hey, it's a
bug. A function exists which is supposed to be doing this).

Well, define it yourself:

testing=> CREATE FUNCTION to_stamp( datetime ) RETURNS timestamp AS
testing-> 'SELECT timestamp_in( datetime_out( $1 ) ) WHERE $1 IS NOT NULL'
testing-> LANGUAGE 'sql';
CREATE

(I found that without WHERE clause it will bug on NULL input, so don't
leave it out).

testing=> select dt, to_stamp( dt ) as ts from test2;
dt                          |ts
----------------------------+----------------------
Sat May 15 13:30:00 1948 IST|1948-05-15 13:30:00+02
Wed Jan 15 16:00:00 1969 IST|1969-01-15 16:00:00+02
Sun Oct 21 02:00:00 1973 IST|1973-10-21 02:00:00+02
Tue Jul 13 14:05:00 1999 IDT|1999-07-13 14:05:00+03
(4 rows)

(dt is a datetime column).

Thus, your query should be:

SELECT to_stamp( the_datetime_col ), other_cols
FROM the_table
WHERE the_datetime_col = 'some value';

HTH,
Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma





view thread (4+ messages)  latest in thread

Message-ID: <l03130302b3b10f688e62@[147.233.159.109]>
Permalink:  ../l03130302b3b10f688e62@%5B147.233.159.109%5D/
Also on:    postgresql.org/message-id/l03130302b3b10f688e62@[147.233.159.109]

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: herouth@oumail.openu.ac.il, sbradley@llnl.gov, echiner@tissat.es
  Subject: Re: [SQL] Problem with timestamp and primary key.
  In-Reply-To: <l03130302b3b10f688e62@[147.233.159.109]>
  References: <378AE74F.C804CB78@tissat.es> <l03130302b3b10f688e62@[147.233.159.109]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox