Does PostgreSQL guarantee unique timestamps?
8-byte integers are now the default for Postgres 8.4, so it allows microsecond values to be stored.
I don't care too much about real microsecond precision (probably depends on OS capabilities?!) - But does Postgres guarantee, that the timestamp values (inserted by current_timestamp
) are always different between any two transaction开发者_高级运维s?
No.
I did a small test, inserting current_timestamp
from 5 parallell clients in a table, 3 of 3463 records got the same timestamp.
Yes, precision depends on the OS. Within a single transaction the timestamps are all the same when using CURRENT_TIMESTAMP, when using CLOCK_TIMESTAMP they might be different. Different transactions can have different CURRENT_TIMESTAMPS, depends on how fast you can insert/update.
If you need uniqueness, add a unique constraint.
Even if it does, it seems dangerous to rely on the values being unique. If you need a unique key you are better off using an alternative such as a serial
column.
Luckily no, since that would mean a "please make this slower than it has to be" option.
If you really need what is really a serial, but which looks like a timestamp, then add some microseconds on based on the result of a serial!
I agree with the recommendation of adding a serial. But if what you are trying to do doesn't merit a schema change, you could use the system column xmin to separate rows with the same timestamp but from different transactions.
SELECT ts, xmin, count(1)
FROM foo
GROUP BY ts, xmin
精彩评论