Use a composite index that starts with a nullable grouping property for ordered groupings on PostgreSQL
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 49/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- java, postgresql
- Domain
- databases, performance
Research direction
Start by tracing how the platform translates ordered GROUP expressions and declared composite indexes into PostgreSQL queries; the issue gives a reproducible example and reports the plan on PostgreSQL 18.1. Compare the all-groups query with the single-group case fixed by #1727, then verify that PostgreSQL can use the declared index without a separate sort for the all-groups query.
Written by the indexing model from the issue text.
Description
Description
An order-dependent grouping with a BY block (GROUP LAST ... ORDER ... BY, and the other grouping types that take ORDER) that is computed for all groups at once cannot get the ordering it needs from the composite index declared for it — grouping property, ordering property, object — when the grouping property is stored in a nullable column, which is every property that is not a key of its table.
CLASS Rec;
grp = DATA LONG (Rec);
ord = DATA DATE (Rec);
val = DATA NUMERIC[10,2] (Rec);
INDEX grp(Rec r), ord(r), r;
lastVal (LONG g) = GROUP LAST val(Rec r) ORDER ord(r), r BY grp(r);
On PostgreSQL 18.1 the query that reads lastVal for every group at once — an assignment or an export over all objects, a recalculation of a materialized grouping — is planned as a sequential scan of the table followed by a sort of all its rows; with 2 000 000 rows in 50 000 groups that is an external merge sort of 82 MB. No plan reads the index above in order: the sort stays in the plan with sorting disabled in the planner.
When the value is read for one group, the same index is used (an index scan backward with LIMIT 1), which is what #1727 fixed.
Reason
The index is declared exactly for this grouping, by the rule for composite indexes (the property the rows are selected by first, the ordering one after it), and it works for one group, so nothing tells the developer that the query over all groups cannot use it. On large tables the sort can spill to disk. One verified workaround is an index created in the database by hand, with the default placement of NULL values on the grouping column, which the platform knows nothing about.
Fix
PostgreSQL can satisfy the ordering of a grouping with an ordered aggregate over all groups from the index (grouping property, ordering property, object) declared in the logic, without a separate sort, as it can with the manually created index.
- Dominant language
- Java
- Stars
- 214
- Forks
- 46
- PR merge metrics
- No merged PRs in 30d
Getting set up
- Ships a Dockerfile or Docker Compose file
- No pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from lsfusion/platform
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
-
bug
Difficulty 3/5 1-2 days Newbie friendliness 45/100
-
bug
Difficulty 4/5 3-5 days Newbie friendliness 30/100
-
Difficulty 3/5 1-2 days Newbie friendliness 62/100
All issues in lsfusion/platform
Similar issues
-
account assessment course feature lecture
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
Maintainers usually reply within 1 day
-
app:learninggoalhub enhancement good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 1-3 hours Newbie friendliness 86/100
Maintainers usually reply within 1 day
-
PropertyResolver fails on JDK classes with a non-public field named like the propertyPossibly taken @vaibhavgupta443 claimed this today. Opengood first issue
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
apache/wicket#1991 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100