timestamp型で登録したデータが30分以上前だったら、削除する。

実現したかったことは題名の通り。

テーブルは下記のように作成。

CREATE TABLE (
  insert_time timestamp default now() primary key,
  sid         character(5)
)

構成は下記の通り。

   Column    |            Type             |       Modifiers        
-------------+-----------------------------+------------------------
 insert_time | timestamp without time zone | not null default now()
 sid         | character(5)                | 

データが格納されている様子はこんな感じ。

        insert_time         |  sid  
----------------------------+-------
 2010-12-08 23:47:59.298005 | abcde
 2010-12-08 23:48:08.225945 | aaaaa

で、

テーブルに格納したデータが現在時刻より、30分以上前であるものを抽出するには下記の通り。

select * from hoge_table where insert_time <= cast(now() - interval '30 minutes' as timestamp);


http://www.hoge256.net/2008/04/116.html
http://www.hoge256.net/2007/07/48.html
Postgres の timestamp型について SELECT文のWHERE句で現在の時… - 人力検索はてな
SQLでtimestamp型のデータを抽出する場合に、時間範囲を指定して… - 人力検索はてな
http://www.postgresql.jp/document/pg824doc/html/functions-datetime.html