반응형

PostgreSQL 트랜잭션 안에서 Now() 값이 변화하지 않는다.

 

하나의 트랜잭션 안에서 동일한 값을 사용해야 하면 상관은 없지만, 그렇지 않을 경우는 now() 함수를 사용하면 된다.

 

하나의 트랜잭션 안에서 다른 시간 값을 사용하려면, clock_timestamp()값을 사용해야 한다.

 

now() = current_timestamp = transaction_timestamp() : 트랜잭션의 시작 시간을 리턴

clock_timestamp() : 현재 시간 리턴

 

테이블에서 실제 Insert 시간이 필요할 경우는 now() 사용하지 말고 clock_timestamp() 사용해야 실제 insert 시간을 넣을 있다.

begin 이용해서 동일한 시간대의 값을 여러 테이블에 저장할 경우는 데이터 일관성을 맞추기 위해서 now()

 

[local]:5413 postgres@postgres # select now(), current_timestamp , clock_timestamp() ;
+-------------------------------------------+--------------------------------------------+-------------------------------------------+
|                      now                    |           current_timestamp        |           clock_timestamp          |
+-------------------------------------------+--------------------------------------------+-------------------------------------------+
| 2022-06-07 11:08:54.631999+09 | 2022-06-07 11:08:54.631999+09 | 2022-06-07 11:08:54.632143+09 |
+-------------------------------------------+--------------------------------------------+-------------------------------------------+


[local]:5413 postgres@postgres # begin ;

[local]:5413 postgres@postgres # select now(), current_timestamp , clock_timestamp() ;
+-------------------------------------------+--------------------------------------------+-------------------------------------------+
|                      now                    |           current_timestamp        |           clock_timestamp          |
+-------------------------------------------+--------------------------------------------+-------------------------------------------+
| 2022-06-07 11:09:11.256782+09 | 2022-06-07 11:09:11.256782+09 | 2022-06-07 11:09:13.608794+09 |
+-------------------------------------------+--------------------------------------------+-------------------------------------------+

[local]:5413 postgres@postgres # select now(), current_timestamp , clock_timestamp() ;

+-------------------------------------------+--------------------------------------------+-------------------------------------------+
|                      now                    |           current_timestamp        |           clock_timestamp          |
+-------------------------------------------+--------------------------------------------+-------------------------------------------+
| 2022-06-07 11:09:11.256782+09 | 2022-06-07 11:09:11.256782+09 | 2022-06-07 11:09:15.537125+09 |
+-------------------------------------------+--------------------------------------------+-------------------------------------------+

[local]:5413 postgres@postgres # select now(), current_timestamp , clock_timestamp() ;

+-------------------------------------------+--------------------------------------------+-------------------------------------------+
|                      now                    |           current_timestamp        |           clock_timestamp          |
+-------------------------------------------+--------------------------------------------+-------------------------------------------+
| 2022-06-07 11:09:11.256782+09 | 2022-06-07 11:09:11.256782+09 | 2022-06-07 11:09:17.224676+09 |
+-------------------------------------------+--------------------------------------------+-------------------------------------------+

[local]:5413 postgres@postgres # rollback ;

 

반응형

+ Recent posts