Re: Help ... Unexpected results when using limit/offset with

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: Barbara Cosentino <bcosentino(at)ncircle(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Help ... Unexpected results when using limit/offset with
Date: 2007-01-19 00:40:56
Message-ID: 20070118163103.R36654@megazone.bigpanda.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Thu, 18 Jan 2007, Barbara Cosentino wrote:

> Then I perform the following selects
>
> SELECT host_id, host_datum_type_id, host_datum_source_id, data
> FROM nc_host_datum INNER JOIN nc_host USING (host_id)
> WHERE audit_id=2041
> ORDER BY host_id
> LIMIT 49 OFFSET 1372;
>
> And
>
> SELECT host_id, host_datum_type_id, host_datum_source_id, data
> FROM nc_host_datum INNER JOIN nc_host USING (host_id)
> WHERE audit_id=2041
> ORDER BY host_id
> LIMIT 49 OFFSET 1421;
>
> A portion of the output follows.
>
> host_id | host_datum_type_id | host_datum_source_id | data
> ---------+--------------------+----------------------+--------------
> :
> :
>
> 963710 | 58 | 17| harrish
> 963711 | 27 | 3 | 1
> 963711 | 28 | 3 | 1
> (49 rows)
>
>
> host_id | host_datum_type_id | host_datum_source_id | data
> ---------+--------------------+----------------------+--------------
> 963711 | 28 | 3 | 1
> 963711 | 58 | 17 | lmitchel
> 963711 | 39 | 3 | us.aegon.com
> :
> :
> (49 rows)
>

> Notice that host_id = 963711 and host_datum_type_id = 28 is repeated
> twice. Since the offset is not overlapping, how can this happen?

I'd suggest adding host_datum_type_id to the order by so that you have a
guarantee of the order that the rows for a given host_id will come,
otherwise I don't think you can assume anything within one host_id which
means you could get the same row at different effective offsets in
different runs of the base query (especially if you hit a point where the
plan changes).

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message paallen 2007-01-19 12:41:19 Query to return schema/table/columname/columntype
Previous Message Barbara Cosentino 2007-01-18 23:51:12 Help ... Unexpected results when using limit/offset with select statement..DB corruption?