Skip site navigation (1) Skip section navigation (2)

Re: bug #7499 additional comments

From: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
To: <lirex(dot)software(at)gmail(dot)com>,<pgsql-bugs(at)postgresql(dot)org>
Subject: Re: bug #7499 additional comments
Date: 2012-08-22 21:19:21
Message-ID: 503506890200002500049A1F@gw.wicourts.gov (view raw or flat)
Thread:
Lists: pgsql-bugs
Denis Kolesnik <lirex(dot)software(at)gmail(dot)com> wrote:
 
> My arguments are:
> 
>  is that even
> select id, str_last_name from tbl_owners_individual where id in
> (83,175,111,1) order by id;
> 
>  id  |    str_last_name
> -----+----------------------
>    1 | Kolesnik
>   83 | GXXXXXXXXX
>  111 | Kolesnik
>  175 | GXXXXXXXXX
> (4 ******)
> 
> select id, str_last_name from tbl_owners_individual where id in
> (83,175,111,1) order by str_last_name;
> 
>  id  |    str_last_name
> -----+----------------------
>   83 | GXXXXXXXXX
>  175 | GXXXXXXXXX
>    1 | Kolesnik
>  111 | Kolesnik
> (4 ******)
> 
> Compare this 2 results and you see, that even if the records with
> the same last names do not come directly one after other then "id
> 1" always closer to the top, then "id 111" and "id 83" always
> clother to the top then "id 175". It proves, that the sorting by
> id remains always even if only among records for the same
> lastname.
 
[sigh]
 
It proves no such thing.  It happened to pick a path *that time*
which happened to generate them in that order.  There are no
guarantees that it always will.  Do you imagine that a column named
"id" has any special properties compared to a column by any other
name?  It doesn't.  What do you imagine would happen if you had
columns with integers in different sequences?
 
Please run this script on your system before your next post:
 
drop if exists table tbl_test;
create table tbl_test
  (id int not null primary key,
   str_last_name text not null,
   misc text);
insert into tbl_test values
  (1, 'Kolesnik'),
  (83, 'GXXXXXXXXX'),
  (111, 'Kolesnik'),
  (175, 'GXXXXXXXXX');
select id, str_last_name from tbl_test
  where id in (83,175,111,1) order by str_last_name;
update tbl_test set misc = 'x' where id = 1;
select id, str_last_name from tbl_test
  where id in (83,175,111,1) order by str_last_name;
analyze tbl_test;
select id, str_last_name from tbl_test
  where id in (83,175,111,1) order by str_last_name;
 
> You would sugguest, that one should read documentation.
> 
> in the (where with ... replaced a directory in which the
> PostgreSQL installed)
> ...PostgreSQL\9.1\doc\postgresql\html\queries-limit.html
> 
> "...When using LIMIT, it is important to use an ORDER BY clause
> that constrains the result rows into a unique order. .."
> 
> here asked to use "ORDER BY" which is done in every query above.
 
No, it asked to specify ORDER BY such that it "constrains the result
rows into a unique order" -- which you are not doing in your
examples.  That is exactly what you *should* do to get the results
you want.
 
> "...The query optimizer takes LIMIT into account when generating
> query plans, so you are very likely to get different plans
> (yielding different row orders) depending on what you give for
> LIMIT and OFFSET.  Thus, using different LIMIT/OFFSET values to
> select different subsets of a query result will give inconsistent
> results unless you enforce a predictable result ordering with
> ORDER BY. This is not a bug; ..."
> 
> the values of "ORDER BY" for LIMIT/OFFSET are not different as you
> see. All requirements are filled.
 
Not even close.  Read it again.  Your ORDER BY clause is not
guaranteeing predictable results.  It would if you added the primary
key columns to the ORDER BY clause (assuming there is a primary
key).
 
You would be well served to fix your query and move on.  There's no
way we're going to make it behave the way you are requesting.  The
current behavior conforms to the SQL standard, performs better than
it could if it worked the way you suggest, and allows you to get the
results you want by fully specifying the ORDER BY clause.
 
-Kevin



pgsql-bugs by date

Next:From: Tom LaneDate: 2012-08-22 22:13:18
Subject: Re: GROUP BY checks inadequate when set returning functions in column list
Previous:From: Denis KolesnikDate: 2012-08-22 20:12:21
Subject: bug #7499 additional comments

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group