Reputation: 11137
for the following:
updated_at | created_at | id
----------------------------+----------------------------+---------
2016-08-26 12:33:35.900201 | 2016-08-25 12:33:13.782502 | 2951380
2016-08-26 12:33:35.916025 | 2016-08-25 12:33:13.781838 | 2951379
2016-08-25 12:33:13.684854 | 2016-08-25 12:33:13.684854 | 2951377
2016-08-25 12:33:13.684753 | 2016-08-25 12:33:13.684753 | 2951378
2016-08-25 12:33:13.652293 | 2016-08-25 12:33:13.652293 | 2951376
2016-08-26 12:32:59.669535 | 2016-08-25 12:33:13.589147 | 2951375
2016-08-26 12:32:59.680676 | 2016-08-25 12:33:13.556841 | 2951374
2016-08-26 12:32:59.559429 | 2016-08-25 12:33:13.496964 | 2951373
2016-08-26 12:32:59.573863 | 2016-08-25 12:33:13.461594 | 2951372
2016-08-26 12:31:10.338129 | 2016-08-25 12:33:13.400724 | 2951371
ID 2951378
has an earlier created_at (and updated_at)
than the 2951377
record!
Anyone have any idea how that may happen, this records inserted by Queue worker handler.
Upvotes: 0
Views: 27
Reputation: 11137
it looks like Rails calculates the timestamps, rather than relying on the database to do so
https://github.com/rails/rails/blob/55dfa009769962367c58563480c9f776ae0f53ea/activerecord/lib/active_record/timestamp.rb#L120
so it makes sense that this kind of situation can happen when multiple workers are saving records at the same time.
Upvotes: 0
Reputation: 24409
Imagine several transactions that are occurring simultaneously. They all need auto-generated ids. But the database cannot reserve the same ids for each transaction, because if they all succeed, they will override each other on commit.
So, each transaction gets its own set of auto-incremented values. Transaction A might start before transaction B, and get some ids allocated, but then B finishes first and it's larger IDs get saved with an earlier time.
It's not a sign of any error. It is a reminder that you should never assume the order of auto-generated IDs correlates to the sequence of events in a DB.
Upvotes: 1