Closed Winches closed 3 years ago
Hi!
The COMB values above are in the correct order (i.e., the value of the GUIDs follows the same order as the value of the IDs). You can see this in the first 6 hex bytes of each GUID. That's the same order PostgreSql will use when you ORDER BY [id]
.
I'm guessing what you're showing above is the order in which the records were inserted into the database, i.e., by selecting from the table in an unordered fashion (without ORDER BY
and without first issuing a CLUSTER
command on the table).
The insert order is determined by your database code. It appears you're using Entity Framework? I don't use EF, but my understanding is that DbContext.SaveChanges()
does not guarantee that records are saved in the same order they are added to the list being saved.
Instead (again, based on my limited understanding), EF attempts to create a dependency graph of the saved objects so it can save them in an order that guarantees referential integrity.
The problem is, the creation of that dependency graph, even if it finds no dependencies, does not preserve the original save order. In fact, in this case, it nearly reverses them, which is what you might expect if EF is using a tree structure of some sort and then using Preorder or similar traversal to save them.
I don't think there's a way to override EF's insertion order. Since PostgreSql doesn't support clustered indexes (or didn't the last time I used it), that means you will need to ORDER BY [id]
to retrieve the records in the intended order.
If you need your PostgreSql table to have its records stored in ID order (whether you're using a UUID/COMB or not), you'll need to bypass EF and issue the insert commands more directly through Npgsql. But even then, updates to those records later will not necessarily preserve that order.
Hope that helps!
Hi!
The COMB values above are in the correct order (i.e., the value of the GUIDs follows the same order as the value of the IDs). You can see this in the first 6 hex bytes of each GUID. That's the same order PostgreSql will use when you
ORDER BY [id]
.I'm guessing what you're showing above is the order in which the records were inserted into the database, i.e., by selecting from the table in an unordered fashion (without
ORDER BY
and without first issuing aCLUSTER
command on the table).The insert order is determined by your database code. It appears you're using Entity Framework? I don't use EF, but my understanding is that
DbContext.SaveChanges()
does not guarantee that records are saved in the same order they are added to the list being saved.Instead (again, based on my limited understanding), EF attempts to create a dependency graph of the saved objects so it can save them in an order that guarantees referential integrity.
The problem is, the creation of that dependency graph, even if it finds no dependencies, does not preserve the original save order. In fact, in this case, it nearly reverses them, which is what you might expect if EF is using a tree structure of some sort and then using Preorder or similar traversal to save them.
I don't think there's a way to override EF's insertion order. Since PostgreSql doesn't support clustered indexes (or didn't the last time I used it), that means you will need to
ORDER BY [id]
to retrieve the records in the intended order.If you need your PostgreSql table to have its records stored in ID order (whether you're using a UUID/COMB or not), you'll need to bypass EF and issue the insert commands more directly through Npgsql. But even then, updates to those records later will not necessarily preserve that order.
Hope that helps!
Thanks for helpful reply. Your guessing is right.I'm inserting records with Entity Framework Core for sequential Guid test.The problem is as you said the insert order of entity is different with original order.
When using
RT.Comb.Provider.PostgreSql
, the generated Guids are not in sorted order in Postgresql 13.3.