Optimizer Hint, to ignore limit and offset in optimizer plan

Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Optimizer Hint, to ignore limit and offset in optimizer plan

Martin Handsteiner

Hello,

 

I’m aware, that taking limit and offset into account of optimizer plan is not a bug.

 

Nevertheless it is very often an unwanted feature.

 

As the postgres db has the issue with not supporting cursors over commit/ rollback, it is necessary to use the limit and offset mechanism.

 

The problem now is, with either

  • not always possible to ensure a proper sort (application with sort on header click),
  • and also on complex queries and a lot of data, that will be slow when sorting the result.

 

So if there would be an optimizer hint, that tells the optimizer to ignore limit and offset on generating a plan, it would be perfect.

 

Mainly having the same optimizer plan without looking on limit and offset, the possibitlity of having mixed data is drastically reduced.

 

Is there possibly already an optimizer hint, to trickout the optimizer, and tell him, that I want all the data, even if there is for eg a limit 10 clause in the select?

 

Thanks

  Martin

Reply | Threaded
Open this post in threaded view
|

Re: Optimizer Hint, to ignore limit and offset in optimizer plan

Pavel Stehule
Hi

pá 26. 6. 2020 v 10:49 odesílatel Martin Handsteiner <[hidden email]> napsal:

Hello,

 

I’m aware, that taking limit and offset into account of optimizer plan is not a bug.

 

Nevertheless it is very often an unwanted feature.

 

As the postgres db has the issue with not supporting cursors over commit/ rollback, it is necessary to use the limit and offset mechanism.

 

The problem now is, with either

  • not always possible to ensure a proper sort (application with sort on header click),
  • and also on complex queries and a lot of data, that will be slow when sorting the result.

 

So if there would be an optimizer hint, that tells the optimizer to ignore limit and offset on generating a plan, it would be perfect.

 

Mainly having the same optimizer plan without looking on limit and offset, the possibitlity of having mixed data is drastically reduced.

 

Is there possibly already an optimizer hint, to trickout the optimizer, and tell him, that I want all the data, even if there is for eg a limit 10 clause in the select?


There is an old trick (workaround) based on usage OFFSET 0.

you can use

SELECT * FROM (SELECT * FROM xxx OFFSET 0) s LIMIT 10;

In this case, a inner query isn't planned with LIMIT number.

Regards

Pavel

 

Thanks

  Martin

Reply | Threaded
Open this post in threaded view
|

Re: Optimizer Hint, to ignore limit and offset in optimizer plan

Bruce Momjian
In reply to this post by Martin Handsteiner
On Fri, Jun 26, 2020 at 08:49:19AM +0000, Martin Handsteiner wrote:

> Hello,
>
>  
>
> I’m aware, that taking limit and offset into account of optimizer plan is not a
> bug.
>
> Nevertheless it is very often an unwanted feature.
>
> As the postgres db has the issue with not supporting cursors over commit/
> rollback, it is necessary to use the limit and offset mechanism.

Uh, have you considered WITH HOLD cursors:

        WITH HOLD specifies that the cursor can continue to be used after the
        transaction that created it successfully commits.  WITHOUT HOLD
        specifies that the cursor cannot be used outside of the transaction that
        created it. If neither WITHOUT HOLD nor WITH HOLD is specified, WITHOUT
        HOLD is the default.

--
  Bruce Momjian  <[hidden email]>        https://momjian.us
  EnterpriseDB                             https://enterprisedb.com

  The usefulness of a cup is in its emptiness, Bruce Lee



Reply | Threaded
Open this post in threaded view
|

Re: Optimizer Hint, to ignore limit and offset in optimizer plan

Simon Riggs
In reply to this post by Martin Handsteiner
On Fri, 26 Jun 2020 at 09:49, Martin Handsteiner <[hidden email]> wrote:

Hello,

 

I’m aware, that taking limit and offset into account of optimizer plan is not a bug.

 

Nevertheless it is very often an unwanted feature.

 

As the postgres db has the issue with not supporting cursors over commit/ rollback, it is necessary to use the limit and offset mechanism.

 

The problem now is, with either

  • not always possible to ensure a proper sort (application with sort on header click),
  • and also on complex queries and a lot of data, that will be slow when sorting the result.

 

So if there would be an optimizer hint, that tells the optimizer to ignore limit and offset on generating a plan, it would be perfect.

 

Mainly having the same optimizer plan without looking on limit and offset, the possibitlity of having mixed data is drastically reduced.

 

Is there possibly already an optimizer hint, to trickout the optimizer, and tell him, that I want all the data, even if there is for eg a limit 10 clause in the select?


It would be useful to have an additional optimizer flag, such as

enable_costlimit = true (default) | false 

--
Simon Riggs                http://www.2ndQuadrant.com/
Mission Critical Databases
Reply | Threaded
Open this post in threaded view
|

AW: Optimizer Hint, to ignore limit and offset in optimizer plan

Martin Handsteiner

Hello,

 

yes, this would exactly be the feature I was talking about.

 

enable_costlimit = true (default) | false 

 

How high is the possibility, that this feature will find the way into the postgres db?

 

Regards

  Martin

 

Von: Simon Riggs <[hidden email]>
Gesendet: Samstag, 27. Juni 2020 13:09
An: Martin Handsteiner <[hidden email]>
Cc: [hidden email]
Betreff: Re: Optimizer Hint, to ignore limit and offset in optimizer plan

 

On Fri, 26 Jun 2020 at 09:49, Martin Handsteiner <[hidden email]> wrote:

Hello,

 

I’m aware, that taking limit and offset into account of optimizer plan is not a bug.

 

Nevertheless it is very often an unwanted feature.

 

As the postgres db has the issue with not supporting cursors over commit/ rollback, it is necessary to use the limit and offset mechanism.

 

The problem now is, with either

  • not always possible to ensure a proper sort (application with sort on header click),
  • and also on complex queries and a lot of data, that will be slow when sorting the result.

 

So if there would be an optimizer hint, that tells the optimizer to ignore limit and offset on generating a plan, it would be perfect.

 

Mainly having the same optimizer plan without looking on limit and offset, the possibitlity of having mixed data is drastically reduced.

 

Is there possibly already an optimizer hint, to trickout the optimizer, and tell him, that I want all the data, even if there is for eg a limit 10 clause in the select?

 

It would be useful to have an additional optimizer flag, such as

 

enable_costlimit = true (default) | false 

 

--

Simon Riggs                http://www.2ndQuadrant.com/
Mission Critical Databases

Reply | Threaded
Open this post in threaded view
|

Re: AW: Optimizer Hint, to ignore limit and offset in optimizer plan

Tom Lane-2
Martin Handsteiner <[hidden email]> writes:
> yes, this would exactly be the feature I was talking about.
> enable_costlimit = true (default) | false
> How high is the possibility, that this feature will find the way into the postgres db?

Not very good, unless you can present a far more convincing use-case.

If I understand your desire, it is that issuing the "same" query in
different transactions would generate identical overall results
despite varying the offset/limit so as to fetch different parts of
that unchanging result.

The problem with this is that whether changing the offset/limit
changes the plan shape is just one of many reasons why you might
not get a consistent result --- the most unavoidable being that
other transactions might commit data changes.

Moreover, you insist that you shouldn't have to use an ORDER BY
to get these consistent results.  Sorry, but SQL is *defined* to
not produce consistent row ordering without ORDER BY.  Changing
that isn't a matter of some optimizer hint somewhere, it's a very
fundamental thing in many places.

I'd counsel taking another look at the suggestion made upthread
to use a cursor WITH HOLD.

                        regards, tom lane


Reply | Threaded
Open this post in threaded view
|

Re: AW: Optimizer Hint, to ignore limit and offset in optimizer plan

Erik Brandsberg
New request:  If order by is not specified with an order by clause, allow an option to be set that explicitly randomizes the results to break everything that relies on the order.  :)  This would primarily be used for QA work to find code that depends on an undefined order.

And waving hi at a fellow Yinzer!

On Mon, Jun 29, 2020 at 10:30 AM Tom Lane <[hidden email]> wrote:
Martin Handsteiner <[hidden email]> writes:
> yes, this would exactly be the feature I was talking about.
> enable_costlimit = true (default) | false
> How high is the possibility, that this feature will find the way into the postgres db?

Not very good, unless you can present a far more convincing use-case.

If I understand your desire, it is that issuing the "same" query in
different transactions would generate identical overall results
despite varying the offset/limit so as to fetch different parts of
that unchanging result.

The problem with this is that whether changing the offset/limit
changes the plan shape is just one of many reasons why you might
not get a consistent result --- the most unavoidable being that
other transactions might commit data changes.

Moreover, you insist that you shouldn't have to use an ORDER BY
to get these consistent results.  Sorry, but SQL is *defined* to
not produce consistent row ordering without ORDER BY.  Changing
that isn't a matter of some optimizer hint somewhere, it's a very
fundamental thing in many places.

I'd counsel taking another look at the suggestion made upthread
to use a cursor WITH HOLD.

                        regards, tom lane




--
Erik Brandsberg
[hidden email]

www.heimdalldata.com
+1 (866) 433-2824 x 700
AWS Competency Program
Reply | Threaded
Open this post in threaded view
|

Re: AW: Optimizer Hint, to ignore limit and offset in optimizer plan

Bruce Momjian
On Mon, Jun 29, 2020 at 10:33:43AM -0400, Erik Brandsberg wrote:
> New request:  If order by is not specified with an order by clause, allow an
> option to be set that explicitly randomizes the results to break everything
> that relies on the order.  :)  This would primarily be used for QA work to find
> code that depends on an undefined order.

You mean LIMIT/OFFSET without ORDER BY?  I think issuing a warning,
perhaps when some "novice" mode is enabled, would work well.

--
  Bruce Momjian  <[hidden email]>        https://momjian.us
  EnterpriseDB                             https://enterprisedb.com

  The usefulness of a cup is in its emptiness, Bruce Lee



Reply | Threaded
Open this post in threaded view
|

AW: AW: Optimizer Hint, to ignore limit and offset in optimizer plan

Martin Handsteiner
In reply to this post by Tom Lane-2
> Not very good, unless you can present a far more convincing use-case.

The use-case is saving memory and increase speed for showing data in applications.
This sounds simple, but please have a look at the scenarios and problems.

I'm aware, that an optimizer hint "enable_costlimit = true (default) | false" will not guarantee 100% transaction save data, of course.

This is also not the case by using an order by, there is still the same problem.
(for eg, forums are paged with limit, and ordered by creation date desc of the post. Switching to the second page (limit 10 offset 10) can show a post again, that was already on the first page, when there is a new post in the meanwhile.
But mainly every user can live with the result, as long as there are not completely nondeterministic results on paging)

In complex applications, there have to be shown a lot of data, sometimes really complex views.

Some Use Cases we have:
-  A complex, already tuned view that delivers the first rows of data in a view seconds. The user can page and view the next data, the paging will get slower, every page.
On a point, he has to define better search parameters, or the user can start a job that creates a full report without using the limit, but it takes time.
Very often the users are satisfied with the first pages. (They can work with the first pages of the result. On next day, due to their work they get again first pages on which they can work on)
Any order by will cause the view to take minutes, even if the first page will only show 10 rows, because the whole query has to be finished by the db and then ordered. This would not be practicable for any application, that the user has to wait minutes after a click...

- A view that delivers millions of result rows. The application has an sort on header functionality, where the user can sort on any column.
So there is no chance of setting a order by that will ensure a proper page order (eg sort on groupname).

If limit and offset would not be taken into account by optimizer, the above use cases would work acceptable. For transaction save problems, there has to always created a solution without limit and offset, of course.
But exact the use cases where the limit and offset is needed (reducing memory and increase speed), the limit and offset clause causes non deterministic results for paging.

> I'd counsel taking another look at the suggestion made upthread to use a cursor WITH HOLD.

I will try, but I have found so far searching the internet, that the postgres jdbc driver does not support holdable cursors.
I have to check it by my own.
Anyway, I was already told, that this belongs to the jdbc mailing list, and seams to be for now not an option as solution, if it is not supported by jdbc.


Regards
  Martin Handsteiner




-----Ursprüngliche Nachricht-----
Von: Tom Lane <[hidden email]>
Gesendet: Montag, 29. Juni 2020 16:30
An: Martin Handsteiner <[hidden email]>
Cc: Simon Riggs <[hidden email]>; [hidden email]
Betreff: Re: AW: Optimizer Hint, to ignore limit and offset in optimizer plan

Martin Handsteiner <[hidden email]> writes:
> yes, this would exactly be the feature I was talking about.
> enable_costlimit = true (default) | false How high is the possibility,
> that this feature will find the way into the postgres db?

Not very good, unless you can present a far more convincing use-case.

If I understand your desire, it is that issuing the "same" query in different transactions would generate identical overall results despite varying the offset/limit so as to fetch different parts of that unchanging result.

The problem with this is that whether changing the offset/limit changes the plan shape is just one of many reasons why you might not get a consistent result --- the most unavoidable being that other transactions might commit data changes.

Moreover, you insist that you shouldn't have to use an ORDER BY to get these consistent results.  Sorry, but SQL is *defined* to not produce consistent row ordering without ORDER BY.  Changing that isn't a matter of some optimizer hint somewhere, it's a very fundamental thing in many places.

I'd counsel taking another look at the suggestion made upthread to use a cursor WITH HOLD.

                        regards, tom lane