DavyJonesLocker / postgres_ext-serializers

MIT License
324 stars 30 forks source link

Fix missing/inconsitent records without order #48

Closed felixbuenemann closed 8 years ago

felixbuenemann commented 8 years ago

If the relation query has no order, it's possible, that different records are selected for the ids and filter CTE temp tables, because when no ORDER BY is given, postgres record order is non-deterministic.

This leads to inconsistencies where embedded ids or records do not match up.

The solution is to constrain the filter CTE by joining the ids CTE, if there is no ORDER BY and the relation query has a LIMIT or OFFSET.

The most likely scenario for this problem is if you use pagination without specifying an order on the serialized relation. The easiest fix is to just add an order(:id) to the query, but we shouldn't break if no order is used.

TODO:

felixbuenemann commented 8 years ago

Adding a test for this is tricky, because we don't know which order postgres will return the records in and they could have the same order by coincidence.

felixbuenemann commented 8 years ago

Instead of adding a test that checks for random record order, we could at least check it is working as expected, when we do use limit/offset without order.

felixbuenemann commented 8 years ago

I've managed to write a test that reliably fails without this fix and rebased on master.

felixbuenemann commented 8 years ago

Btw. this could be rewritten as a subselect instead of a join which could potentially be faster, but require more logic.