Postgres offset and limit bug

Started by Mingover 3 years ago5 messagesbugs
Jump to latest
#1Ming
ming@cleanstormwater.com.au

Hello,

We recently found a strange behavior on Postgres SQL. The offset function will not acting properly when having limit in the query. I want to confirm that it's an existing bug or it's my syntax is wrong.

Please find the attachments for details.

Client: PgAdmin
Client System: Windows 11
Instance: AWS RDS PostgreSQL 13.4

Mingxuan Chen [https://cdn.smartresumewizard.com/images/icons/icon_linkedin.png] <https://www.linkedin.com/in/mingxuan-chen-link/&gt;
Software Engineer a: w:
CleanStormWater p: 0461602352 e: mingxuan_chen@outlook.com

Attachments:

0c69a15182f5490266446db65bedd4e.pngimage/png; name=0c69a15182f5490266446db65bedd4e.pngDownload
c1473d0ba28168df582acca9dc28ad6.pngimage/png; name=c1473d0ba28168df582acca9dc28ad6.pngDownload
1679c85c393d1d13b53425103950d70.jpgimage/jpeg; name=1679c85c393d1d13b53425103950d70.jpgDownload
#2David Rowley
dgrowleyml@gmail.com
In reply to: Ming (#1)
Re: Postgres offset and limit bug

On Thu, 8 Sept 2022 at 19:20, Ming <ming@cleanstormwater.com.au> wrote:

We recently found a strange behavior on Postgres SQL. The offset function will not acting properly when having limit in the query. I want to confirm that it's an existing bug or it's my syntax is wrong.

Nothing looks wrong from here. Can you describe what behaviour you're expecting?

David

#3Ming
ming@cleanstormwater.com.au
In reply to: David Rowley (#2)
Re: Postgres offset and limit bug

Hello David,

The issue is when I have limit 1 with the offset, the first record id will be 45 (which should be 46) . (Although, it’s ordered by path, the of is actually ordered as well.) But without the limit 1 the results will be correct. And when I plus one to the offset value, the actual offset will increase by 2.

In general, the offset value with 45 is behave as offset 44. But when change the value to other thing, the results will be all good.

Regards,
Ming

Get Outlook for iOS<https://aka.ms/o0ukef&gt;
________________________________
From: David Rowley <dgrowleyml@gmail.com>
Sent: Thursday, September 8, 2022 6:36:04 PM
To: Ming <ming@cleanstormwater.com.au>
Cc: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>
Subject: Re: Postgres offset and limit bug

On Thu, 8 Sept 2022 at 19:20, Ming <ming@cleanstormwater.com.au> wrote:

We recently found a strange behavior on Postgres SQL. The offset function will not acting properly when having limit in the query. I want to confirm that it's an existing bug or it's my syntax is wrong.

Nothing looks wrong from here. Can you describe what behaviour you're expecting?

David

#4David Rowley
dgrowleyml@gmail.com
In reply to: Ming (#3)
Re: Postgres offset and limit bug

On Thu, 8 Sept 2022 at 20:49, Ming <ming@cleanstormwater.com.au> wrote:

The issue is when I have limit 1 with the offset, the first record id will be 45 (which should be 46) . (Although, it’s ordered by path, the of is actually ordered as well.) But without the limit 1 the results will be correct. And when I plus one to the offset value, the actual offset will increase by 2.

In general, the offset value with 45 is behave as offset 44. But when change the value to other thing, the results will be all good.

That's probably because the path for 45 and 46 are the same and you've
only specified that you want the results ordered by path. If there are
duplicates then the order that duplicates appear in will be
non-deterministic. Maybe you'll want to consider adding
source_template_id to the ORDER BY.

David

#5Ming
ming@cleanstormwater.com.au
In reply to: David Rowley (#4)
Re: Postgres offset and limit bug

Thanks David~

Get Outlook for iOS<https://aka.ms/o0ukef&gt;
________________________________
From: David Rowley <dgrowleyml@gmail.com>
Sent: Thursday, September 8, 2022 7:03:33 PM
To: Ming <ming@cleanstormwater.com.au>
Cc: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>
Subject: Re: Postgres offset and limit bug

On Thu, 8 Sept 2022 at 20:49, Ming <ming@cleanstormwater.com.au> wrote:

The issue is when I have limit 1 with the offset, the first record id will be 45 (which should be 46) . (Although, it’s ordered by path, the of is actually ordered as well.) But without the limit 1 the results will be correct. And when I plus one to the offset value, the actual offset will increase by 2.

In general, the offset value with 45 is behave as offset 44. But when change the value to other thing, the results will be all good.

That's probably because the path for 45 and 46 are the same and you've
only specified that you want the results ordered by path. If there are
duplicates then the order that duplicates appear in will be
non-deterministic. Maybe you'll want to consider adding
source_template_id to the ORDER BY.

David