Re: weirdness with the a sql update - Mailing list pgsql-general

From Martijn van Oosterhout
Subject Re: weirdness with the a sql update
Date
Msg-id 20050804143522.GA25982@svana.org
Whole thread Raw
In response to weirdness with the a sql update  (Tony Caduto <tony_caduto@amsoftwaredesign.com>)
List pgsql-general
On Thu, Aug 04, 2005 at 09:19:44AM -0500, Tony Caduto wrote:
> Hi,
>
> I just noticed this, if I do a update like this:
>
> update new_requests set name = 'tony' where request_id = 2
>
> If I do a select * from new_requests that record I just updated is now
> at the bottom , before the update it was at the top?

Table in SQL don't have an implicit order. If you want the rows in a
particular order, you need to use ORDER BY.

> Why is Postgresql changing the ordering of the results after a simple
> update?

Because it's silly to order stuff if you didn't ask for a particular
order. Waste of CPU cycles.

> It almost looks like the record is being dropped and then readded to the
> end.

Under the hood, that's what happens. But it might appear at the end, in
the middle, anywhere. If want an order, use ORDER BY, otherwise it'll
be random.

Hope this helps,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.

Attachment

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: What happens when wal fails?
Next
From: Douglas McNaught
Date:
Subject: Re: weirdness with the a sql update