Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pagination): fix bug in cursor pagination for PL/pgSQL SETOF functions #559

Merged
merged 3 commits into from
Nov 6, 2019

Conversation

benjie
Copy link
Member

@benjie benjie commented Nov 6, 2019

Fixes graphile/crystal#1171

useAsterisk runs certain code in a subquery as an optimisation; so:

select /* COMPLEX STUFF */
from /* EXPENSIVE THING */
limit 2 offset 2

becomes

select /* COMPLEX STUFF */
from (
  select *
  from /* EXPENSIVE THING */
  limit 2 offset 2
) as __alias__

This is great because it means that PostgreSQL can process the rows in the subquery more efficiently, and doesn't have to execute the "COMPLEX STUFF" until it's thrown most of the rows from "EXPENSIVE THING" away. Less computation = faster queries (even though they're a little more complex).

HOWEVER because we've moved the LIMIT/OFFSET to the subquery, any calls to row_number() are now different than they were before because they no longer include the OFFSET. In this code, we add it back in. It's kinda hacky, but it's a performance optimisation so we're just going to have to suck it up 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pagination via cursor on custom query functions does not work (4.4.x)
1 participant