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

Try and maintain ordering of functions #2265

Merged
merged 26 commits into from
Dec 3, 2024
Merged

Try and maintain ordering of functions #2265

merged 26 commits into from
Dec 3, 2024

Conversation

benjie
Copy link
Member

@benjie benjie commented Dec 3, 2024

Description

For a statement like this:

select *
from my_function()

PostgreSQL does not guarantee that the order of rows returned will be the same as the orders defined in the function - this is because you need an explicit order by statement in this statement to guarantee order.

However, most of the time, the order is maintained due to the way in which PostgreSQL works... But this goes out the window once you add a join - if PostgreSQL decides it's better to evaluate your joined table first and merge with your function call afterwards, then your order can be lost.

This PR attempts to maintain the order of functions more reliably by refusing to inline PgSelect statements into parent queries if the parent query has originated from a set-returning function, and thus has an implicit order.

Originally (see the commit history) I was hoping that we could use WITH ORDINALITY to solve this, but alas this breaks computed column functions since we can no longer pass the resulting record into the function, since it gains an ordinality column. We could work around that, but it'd make the queries really ugly. Another option would be to move all joins to be subqueries instead, like in V4, but no thanks!

In conclusion: you should explicitly order your functions... but if you don't, this should hopefully mean order is generally maintained, maybe, probably, we hope 🤞

Performance impact

Deliberately disables inlining of some queries, resulting in additional roundtrips to the PostgreSQL server.

Security impact

Other than performance, none known.

Checklist

  • My code matches the project's code style and yarn lint:fix passes.
  • I've added tests for the new feature, and yarn test passes.
  • I have detailed the new feature in the relevant documentation.
  • I have added this feature to 'Pending' in the RELEASE_NOTES.md file (if one exists).
  • If this is a breaking change I've explained why.

Copy link

changeset-bot bot commented Dec 3, 2024

🦋 Changeset detected

Latest commit: 135e8c1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
graphile-build-pg Patch
postgraphile Patch
@dataplan/pg Patch
graphile-utils Patch
pgl Patch
graphile Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Comment on lines +2523 to +2527
// Don't want to make this a join as it can result in the order being
// messed up
if (t2.hasImplicitOrder && !this.joinAsLateral && this.isUnique) {
continue;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

22 commits... but this is the bit that matters.

@benjie benjie merged commit c38606a into main Dec 3, 2024
42 checks passed
@benjie benjie deleted the issue-2210 branch December 3, 2024 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Non-deterministic sorting when querying a setof function and relational data
1 participant