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

Aperta
#1,996 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
49/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
java, postgresql

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Java
Stelle
214
Fork
46
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

  • Include un Dockerfile o un file Docker Compose
  • Nessun modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di lsfusion/platform

Tutte le issue di lsfusion/platform

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.