If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > PostgreSQL Rearranges My Column Data

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-06-11, 21:52
CarlosinFL CarlosinFL is offline
Registered User
 
Join Date: Oct 2010
Location: Orlando, FL
Posts: 184
PostgreSQL Rearranges My Column Data

Is there a reason that PostgreSQL re-arranges my column data after I make a change?

For example:

Code:
iamunix=# SELECT * FROM zoo;
 id | animal | color | name  |    dob     |    dod
----+--------+-------+-------+------------+------------
  1 | snake  | black | Marty | 2009-05-19 |
  2 | bird   | blue  | Jake  | 2011-09-03 |
  3 | monkey | brown | Simon | 1981-02-11 | 2011-02-13
(3 rows)
Now if I edit id 1 or 2 with ALTER, the next time I run the same command above, I now have:

Code:
iamunix=# SELECT * FROM zoo;
 id | animal | color | name  |    dob     |    dod
----+--------+-------+-------+------------+------------
  2 | bird   | blue  | Jake  | 2011-09-03 |
  3 | monkey | brown | Simon | 1981-02-11 | 2011-02-13
  1 | snake  | black | Marty | 2009-05-19 |
(3 rows)
Is there a logical reason for this? I've only noticed this in PostgreSQL.
Reply With Quote
  #2 (permalink)  
Old 04-06-11, 22:40
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,538
Quote:
Originally Posted by CarlosinFL View Post
Is there a logical reason for this?
yes

the reason is your ORDER BY clause

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 04-07-11, 02:32
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Quote:
Originally Posted by CarlosinFL View Post
I've only noticed this in PostgreSQL.
Actually all DBMS behave like that.

As r937 has pointed out, the lack of an ORDER BY is the reason.

The only way to guarantee a specific order of your row is to use an ORDER BY

Actually you don't necessarily need to change the data to see this behaviour
PostgreSQL (or other DBMS). PostgreSQL can e.g. use the result of another's session table scan for your statement (to optimize I/O).

Unless you explicitely request a sort order the DBMS is free to return the rows in any order it seems suitable (to speed up the processing)

Btw: your terminology is a bit confusing. You did not "edit" the rows using "ALTER" but "UPDATE"
Reply With Quote
  #4 (permalink)  
Old 04-07-11, 11:49
CarlosinFL CarlosinFL is offline
Registered User
 
Join Date: Oct 2010
Location: Orlando, FL
Posts: 184
Quote:
Originally Posted by shammat View Post
Actually all DBMS behave like that.
I guess coming from MySQL and relating their behavior to PostgreSQL isn't good considering they're very non compliant. I since have switched to PostgreSQL and assumed it would do the same w/o having to use the ORDER BY command.

Thanks for the heads up.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On