| 1 |
-- |
| 2 |
-- Commit Timestamp |
| 3 |
-- |
| 4 |
SHOW track_commit_timestamp; |
| 5 |
track_commit_timestamp |
| 6 |
------------------------ |
| 7 |
on |
| 8 |
(1 row) |
| 9 |
|
| 10 |
CREATE TABLE committs_test(id serial, ts timestamptz default now()); |
| 11 |
INSERT INTO committs_test DEFAULT VALUES; |
| 12 |
INSERT INTO committs_test DEFAULT VALUES; |
| 13 |
INSERT INTO committs_test DEFAULT VALUES; |
| 14 |
SELECT id, |
| 15 |
pg_xact_commit_timestamp(xmin) >= ts, |
| 16 |
pg_xact_commit_timestamp(xmin) <= now(), |
| 17 |
pg_xact_commit_timestamp(xmin) - ts < '60s' -- 60s should give a lot of reserve |
| 18 |
FROM committs_test |
| 19 |
ORDER BY id; |
| 20 |
id | ?column? | ?column? | ?column? |
| 21 |
----+----------+----------+---------- |
| 22 |
1 | t | t | t |
| 23 |
2 | t | t | t |
| 24 |
3 | t | t | t |
| 25 |
(3 rows) |
| 26 |
|
| 27 |
DROP TABLE committs_test; |
| 28 |
SELECT pg_xact_commit_timestamp('0'::xid); |
| 29 |
ERROR: cannot retrieve commit timestamp for transaction 0 |
| 30 |
SELECT pg_xact_commit_timestamp('1'::xid); |
| 31 |
pg_xact_commit_timestamp |
| 32 |
-------------------------- |
| 33 |
|
| 34 |
(1 row) |
| 35 |
|
| 36 |
SELECT pg_xact_commit_timestamp('2'::xid); |
| 37 |
pg_xact_commit_timestamp |
| 38 |
-------------------------- |
| 39 |
|
| 40 |
(1 row) |
| 41 |
|
| 42 |
SELECT x.xid::text::bigint > 0 as xid_valid, |
| 43 |
x.timestamp > '-infinity'::timestamptz AS ts_low, |
| 44 |
x.timestamp <= now() AS ts_high, |
| 45 |
roident != 0 AS valid_roident |
| 46 |
FROM pg_last_committed_xact() x; |
| 47 |
xid_valid | ts_low | ts_high | valid_roident |
| 48 |
-----------+--------+---------+--------------- |
| 49 |
t | t | t | f |
| 50 |
(1 row) |
| 51 |
|
| 52 |
-- Test non-normal transaction ids. |
| 53 |
SELECT * FROM pg_xact_commit_timestamp_origin(NULL); -- ok, NULL |
| 54 |
timestamp | roident |
| 55 |
-----------+--------- |
| 56 |
| |
| 57 |
(1 row) |
| 58 |
|
| 59 |
SELECT * FROM pg_xact_commit_timestamp_origin('0'::xid); -- error |
| 60 |
ERROR: cannot retrieve commit timestamp for transaction 0 |
| 61 |
SELECT * FROM pg_xact_commit_timestamp_origin('1'::xid); -- ok, NULL |
| 62 |
timestamp | roident |
| 63 |
-----------+--------- |
| 64 |
| |
| 65 |
(1 row) |
| 66 |
|
| 67 |
SELECT * FROM pg_xact_commit_timestamp_origin('2'::xid); -- ok, NULL |
| 68 |
timestamp | roident |
| 69 |
-----------+--------- |
| 70 |
| |
| 71 |
(1 row) |
| 72 |
|
| 73 |
-- Test transaction without replication origin |
| 74 |
SELECT txid_current() as txid_no_origin \gset |
| 75 |
SELECT x.timestamp > '-infinity'::timestamptz AS ts_low, |
| 76 |
x.timestamp <= now() AS ts_high, |
| 77 |
roident != 0 AS valid_roident |
| 78 |
FROM pg_last_committed_xact() x; |
| 79 |
ts_low | ts_high | valid_roident |
| 80 |
--------+---------+--------------- |
| 81 |
t | t | f |
| 82 |
(1 row) |
| 83 |
|
| 84 |
SELECT x.timestamp > '-infinity'::timestamptz AS ts_low, |
| 85 |
x.timestamp <= now() AS ts_high, |
| 86 |
roident != 0 AS valid_roident |
| 87 |
FROM pg_xact_commit_timestamp_origin(:'txid_no_origin') x; |
| 88 |
ts_low | ts_high | valid_roident |
| 89 |
--------+---------+--------------- |
| 90 |
t | t | f |
| 91 |
(1 row) |
| 92 |
|
| 93 |
-- Test transaction with replication origin |
| 94 |
SELECT pg_replication_origin_create('regress_commit_ts: get_origin') != 0 |
| 95 |
AS valid_roident; |
| 96 |
valid_roident |
| 97 |
--------------- |
| 98 |
t |
| 99 |
(1 row) |
| 100 |
|
| 101 |
SELECT pg_replication_origin_session_setup('regress_commit_ts: get_origin'); |
| 102 |
pg_replication_origin_session_setup |
| 103 |
------------------------------------- |
| 104 |
|
| 105 |
(1 row) |
| 106 |
|
| 107 |
SELECT txid_current() as txid_with_origin \gset |
| 108 |
SELECT x.timestamp > '-infinity'::timestamptz AS ts_low, |
| 109 |
x.timestamp <= now() AS ts_high, |
| 110 |
r.roname |
| 111 |
FROM pg_last_committed_xact() x, pg_replication_origin r |
| 112 |
WHERE r.roident = x.roident; |
| 113 |
ts_low | ts_high | roname |
| 114 |
--------+---------+------------------------------- |
| 115 |
t | t | regress_commit_ts: get_origin |
| 116 |
(1 row) |
| 117 |
|
| 118 |
SELECT x.timestamp > '-infinity'::timestamptz AS ts_low, |
| 119 |
x.timestamp <= now() AS ts_high, |
| 120 |
r.roname |
| 121 |
FROM pg_xact_commit_timestamp_origin(:'txid_with_origin') x, pg_replication_origin r |
| 122 |
WHERE r.roident = x.roident; |
| 123 |
ts_low | ts_high | roname |
| 124 |
--------+---------+------------------------------- |
| 125 |
t | t | regress_commit_ts: get_origin |
| 126 |
(1 row) |
| 127 |
|
| 128 |
SELECT pg_replication_origin_session_reset(); |
| 129 |
pg_replication_origin_session_reset |
| 130 |
------------------------------------- |
| 131 |
|
| 132 |
(1 row) |
| 133 |
|
| 134 |
SELECT pg_replication_origin_drop('regress_commit_ts: get_origin'); |
| 135 |
pg_replication_origin_drop |
| 136 |
---------------------------- |
| 137 |
|
| 138 |
(1 row) |
| 139 |
|