I’m getting a very strange behaviour from the pg driver. It’s a dead simple query: SELECT id, other_id FROM t WHERE a = '$1' and b = '$2'
I loops over a list of values to see for each value if there’s already a record. The query returns two ids of type Int64
and the second one can be nil
. When there is no record, the result set should not have any rows.
values.each do |value|
result = db.connection.query_one?(query, value.a, value.b, as: {Int64, Int64?})
next unless result
id, other_id = result
process(id, other_id)
end
The query works as expected when it’s executed in isolation. But in the loop, it only works when a record exists. If not, it still returns a result where the second id is nil
(as expected), but the first id has a weird value which seems to lie roughly between 58_000 and 63_000. So I’m guessing it might be some kind of special value or something. But it seems to be different every time.
The code runs in a single fiber and I’m sure there is no overlapping queries on the connection.It would be pretty unlikely to result in identical behaviour every time for different value lists (identical apart from the exact value returned as id).
Does anyone have an idea what might be going on?