Received: from localhost (unknown [200.46.204.183]) by developer.postgresql.org (Postfix) with ESMTP id AFF6F2E0046 for ; Tue, 22 Apr 2008 09:46:49 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 41094-08 for ; Tue, 22 Apr 2008 09:46:47 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from www.postnewspapers.com.au (www.postnewspapers.com.au [202.61.230.242]) by developer.postgresql.org (Postfix) with ESMTP id C909C2E002C for ; Tue, 22 Apr 2008 09:46:46 -0300 (ADT) Received: from mail.postnewspapers.com.au (202-89-185-120.static.dsl.amnet.net.au [202.89.185.120]) by www.postnewspapers.com.au (Postfix) with ESMTP id 7F710507; Tue, 22 Apr 2008 20:46:42 +0800 (WST) Received: from localhost (access [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id 178A31E0435; Tue, 22 Apr 2008 20:46:46 +0800 (WST) X-Virus-Scanned: Debian amavisd-new at postnewspapers.com.au Received: from mail.postnewspapers.com.au ([127.0.0.1]) by localhost (access.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id L3hJ9oGYx74r; Tue, 22 Apr 2008 20:46:45 +0800 (WST) Received: from [203.161.97.213] (203.161.97.213.static.amnet.net.au [203.161.97.213]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "Craig Ringer", Issuer "POST Certificate Authority" (verified OK)) by mail.postnewspapers.com.au (Postfix) with ESMTP id 188891E0434; Tue, 22 Apr 2008 20:46:45 +0800 (WST) Message-ID: <480DDE26.50405@postnewspapers.com.au> Date: Tue, 22 Apr 2008 20:46:30 +0800 From: Craig Ringer User-Agent: Thunderbird 2.0.0.12 (Windows/20080213) MIME-Version: 1.0 To: Nacef LABIDI Cc: Roland Voegtli , pgsql-sql@postgresql.org Subject: Re: After updating dataset the record goes to the end of the dataset References: <3B256469-4A00-44C3-8EB8-F49FA9F7259A@scnat.ch> In-Reply-To: X-Enigmail-Version: 0.95.6 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200804/138 X-Sequence-Number: 30677 Nacef LABIDI wrote: > Yes I don't issue any sort statement, and I indeed want the data to be show > as it is stored in the database. That's what you're getting. PostgreSQL has an MVCC design. How it works in general terms (simplified, but I lack the expertise to give a complete explanation even if one was required) is: When you update a row the old row is marked as dead and a new row is inserted. If there is no spare space in the table near the old row (assuming there's any reason for the DB to even try to put the new row near the old one) then the new row will be placed elsewhere, such as at the end of the table. In other words, after an UPDATE the row really is often at the end of the table. In any case as others have explained you should never rely on the database ordering of records; you should always use an ORDER BY if you care about order. The database makes no guarantees about the order of returned rows. The database may optimise row retrieval in ways you do not expect. For example, in recent versions of PostgreSQL if there is a sequential scan in progress on a table and you start another query that also runs a sequential scan on the table, PostgreSQL may synchronize the two scans. That'll cause your query to start part way through the table. If the table contains alphabetically ordered data you might get something like: J K L M ... etc ... A B C So ... in any database, always use ORDER BY if you care about order. Just because it usually works in some databases doesn't mean it won't break just rarely enough to drive you insane while debugging... -- Craig Ringer