LoginSignup
7
1

知らなかったPostgreSQLとMySQLのCURRENT_TIMESTAMPの違い

Posted at

結論

  • PostgresqlのCURRENT_TIMESTAMPはトランザクションを開始していれば、トランザクション開始日時(マイクロ秒+タイムゾーン)
  • MySQLのCURRENT_TIMESTAMPはSQL実行日時(秒)

実験

Postgresql(トランザクションなし)

* SQL実行ごとにtimestampが変化する

test=# select current_timestamp;
       current_timestamp
-------------------------------
 2024-04-15 12:21:01.667985+09
(1 row)

(10秒ほどまつ)

test=# select current_timestamp;
       current_timestamp
-------------------------------
 2024-04-15 12:21:13.965251+09
(1 row)

Postgresql(トランザクションあり)

* SQL実行ごとにtimestampが変化しない

md=# start transaction;
START TRANSACTION
md=*# select current_timestamp;
       current_timestamp
-------------------------------
 2024-04-15 12:22:05.814094+09
(1 row)

(同じく10秒ほどまつ)

md=*# select current_timestamp;
       current_timestamp
-------------------------------
 2024-04-15 12:22:05.814094+09
(1 row)

MySQL(トランザクションなし)

  • SQL実行ごとにtimestampが変化する
mysql> select current_timestamp;
+---------------------+
| current_timestamp   |
+---------------------+
| 2024-04-15 13:13:09 |
+---------------------+
1 row in set (0.00 sec)

(同じく10秒ほどまつ)

mysql> select current_timestamp;
+---------------------+
| current_timestamp   |
+---------------------+
| 2024-04-15 13:13:19 |
+---------------------+
1 row in set (0.01 sec)

MySQL(トランザクションあり)

  • SQL実行ごとにtimestampが変化する
mysql> start transaction;
Query OK, 0 rows affected (0.01 sec)

mysql> select current_timestamp;
+---------------------+
| current_timestamp   |
+---------------------+
| 2024-04-15 13:14:48 |
+---------------------+
1 row in set (0.01 sec)

(同じく10秒ほどまつ)

mysql> select current_timestamp;
+---------------------+
| current_timestamp   |
+---------------------+
| 2024-04-15 13:15:01 |
+---------------------+
1 row in set (0.01 sec)
7
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
1