Question

I have a doubt regarding the result of a specific query.

SELECT final_copy,num_chars FROM table1 t1,table2 t2

WHERE t1.numid = t2.id

Getting two rows with this query,

final_copy | num_chars

------------+-----------------

     1 | 1272
       | 1075

Here final_copy 1 is the latest record inserted. This query was running in a loop and always returning the record with final_copy as 1 in the first row consistently. As you can see the query was not using any 'order by'.

But now the query results are getting inconsistent, i.e. the rows are not returned based on there last insert date.

My doubt is that is there any chance of changing the behavior of query results when the vacuum or reindexing. Last week I have done reindexing and vacuum of these tables. Will that change the behavior of the results?

My database is postgresql version 7.4.3.

Was it helpful?

Solution

Lack of an order by clause yields, per sql specs, an undefined order. You need to specify the order (potentially using an extra column, if needed) if you want them in the order they were inserted.

(Technically, the order you're seeing is that in which rows are physically read by the query's plan, meaning the order is subject to both mvcc's internal cuisine and the specific plan you end up using, which may vary due to table statistics and your joining two tables.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top