PostgreSQL 実行時に変化する現在日付と時刻を取得する

PostgreSQL 実行時に変化する現在日付と時刻を取得する

PostgreSQLで、実行時に変化する現在日付と時刻を取得する手順を記述してます。

環境

  • OS Rocky Linux release 8.4 (Green Obsidian)
  • PostgreSQL 14.0
  • pgadmin 6.0

手順

実行時に変化する現在日付と時刻を取得する場合は、「clock_timestamp()」を利用します。

clock_timestamp()

実際に、待機時間を使用して変化しているかを確認してみます。

SELECT 
clock_timestamp(),
pg_sleep(3),
clock_timestamp(),
pg_sleep(3),
clock_timestamp();

実行結果

待機時間後に「clock_timestamp()」が実行されていることが確認できます。

current_timestamp

「current_timestamp」の場合は、トランザクション開始時に実行されるため、待機時間を作っても同じ結果となります。

SELECT 
current_timestamp,
pg_sleep(3),
current_timestamp,
pg_sleep(3),
current_timestamp;

実行結果

同じ時間で取得されています。