PostgreSQLでタイムゾーンあり/なしの日時データ型の使い方の違い

環境

システムプラットフォーム:Microsoft Windows (64-bit) 10 バージョン:5.6.4

ドキュメントの目的

PostgreSQLにおけるタイムゾーンあり/なしの日時データ型の使用方法と、タイムスタンプの理解と使用方法に関する学習。

詳細情報

この記事では、日時データ型におけるタイムゾーンあり/なしの違いの使用方法、タイムスタンプの理解と使用方法を説明します。

データ型名								ストレージサイズ		説明					最小値			最大値			解像度
timestamp [ (p) ] [ without time zone ]	8バイト	日付と時刻を含む(タイムゾーンなし)	4713 BC		294276  AD	1マイクロ秒 / 14桁
timestamp [ (p) ] with time zone	    8バイト	日付と時刻を含み、タイムゾーンあり	4713 BC		294276 AD		1マイクロ秒 / 14桁
date									4バイト	日付(時刻なし)			4713 BC		5874897 AD	1日
time [ (p) ] [ without time zone ]		8バイト	時刻のみ(日付なし)		00:00:00		24:00:00	1マイクロ秒 / 14桁
time [ (p) ] with time zone		       12バイト 時刻のみ(日付なし)、タイムゾーンあり	00:00:00+1459	24:00:00-1459	1マイクロ秒 / 14桁
interval [ fields ] [ (p) ]				16バイト	時間間隔				-178000000年  178000000年	1マイクロ秒 / 14桁

注意:SQL標準ではtimestampはtimestamp without time zoneと同等とされ、PostgreSQLもこの動作を推奨しています。timestamptzはtimestamp with time zoneの省略形として受け入れられ、これはPostgreSQLの拡張機能です。

  1. 日付データ型におけるタイムゾーンあり/なしの形式

タイムゾーンあり:

db_test=# select current_timestamp::timestamptz;
              current_timestamp
----------------------------------------
 2023-05-20 10:30:45.123456+09
(1 行)

タイムゾーンなし:

db_test=# select current_timestamp::timestamp;
            current_timestamp
----------------------------------
 2023-05-20 10:31:22.789012
(1 行)

日付に精度を設定:

db_test=# select current_timestamp::timestamp(2);
          current_timestamp
-------------------------------
 2023-05-20 10:32:15.67
(1 行)
db_test=# select current_timestamp::timestamptz(2);
           current_timestamp
-------------------------------
 2023-05-20 10:33:44.12+09
(1 行)

  1. 時刻データ型におけるタイムゾーンあり/なしの形式

タイムゾーンなし:

db_test=# select current_time::time without time zone;

       current_time
------------------------
 10:35:11.345678
(1 行)

db_test=# select current_time::time;
       current_time
------------------------
 10:36:22.765432
(1 行)

注意:timeのみ指定はtime without time zoneと同等です。

タイムゾーンあり:

db_test=# select current_time::time with time zone;
        current_time
----------------------
 10:37:33.987654+09
(1 行)

日時/時刻型のテーブル作成:

db_test=# create table datetime_sample(event_time timestamp,event_tz timestamptz,start_time time,end_time time with time zone);
CREATE TABLE
db_test=# insert into datetime_sample values(current_timestamp,current_timestamp,current_timestamp,current_timestamp);
INSERT 0 1
db_test=# select * from datetime_sample;
event_time              |event_tz               |    start_time |     end_time
--------------------------------+--------------------------------+---------------+--------------------
 2023-05-20 10:39:45.123456 | 2023-05-20 10:39:45.123456+09 | 10:39:45.123456 | 10:39:45.123456+09
(1 行)

二、タイムスタンプの使用

SQL標準では、「+」または「-」記号の存在と時刻の後のタイムゾーンオフセットによってtimestamp without time zoneとtimestamp with time zoneリテラルを区別します。したがって、標準に基づけば、

TIMESTAMP '2023-05-20 14:20:35' はtimestamp without time zoneですが、

TIMESTAMP '2023-05-20 14:20:35.987654+09'

はtimestamp with time zoneです。PostgreSQLはリテラルの型を決定する前にその内容をチェックすることはありません。そのため、上記の両方をtimestamp without time zoneとして扱います。したがって、これらのリテラルをtimestamp with time zoneとして扱うには、明示的な型指定を正しく行う必要があります:

TIMESTAMP WITH TIME ZONE '2023-05-20 14:20:35.987654+09'

リテラルがtimestamp without time zoneとして確定している場合、PostgreSQLは指定されているタイムゾーンを黙って無視します。つまり、結果の値は入力値の日付/時刻フィールドから派生し、タイムゾーンの調整は行われません。

タグ: PostgreSQL timestamp timezone Date time

6月15日 19:42 投稿