Use a composite index that starts with a nullable grouping property for ordered groupings on PostgreSQL

Open
#1,996 0 comments 0 reactions 0 assignees View on GitHub

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

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

bug
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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from lsfusion/platform

All issues in lsfusion/platform

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.