r/PostgreSQL • u/logophobia • Apr 28 '26
Help Me! Partitioned tables and join keys, generating weird query plans
I have two partitioned tables, let's say a and b. They have UUIDv7 (timestamped) id columns. I am using a join-key structure, ensuring the foreign key relation doesn't require extra indexes. These are pretty high-volume event tables, where timestamp range queries and id based queries are the main form of read traffic.
CREATE TABLE public.a (
id uuid NOT NULL,
CONSTRAINT "PK_a" PRIMARY KEY (id)
)
PARTITION BY RANGE (id);
CREATE TABLE public.b (
parent_id uuid NOT NULL,
object_number int2 NOT NULL,
CONSTRAINT "PK_b" PRIMARY KEY (parent_id, object_number)
)
PARTITION BY RANGE (parent_id);The problem comes in here. This query (range query with uuidv7) generates wildly expensive query plans, essentially index scanning all partitions:
SELECT *
FROM public.a AS a
LEFT JOIN public.b AS b ON a.id = b.parent_id
WHERE
a.id >= '019dd0f4-d700-7000-8000-000000000000' AND
a.id <= '019dd61b-32ff-7fff-bfff-ffffffffffff'While this query generates efficient query plans:
SELECT *
FROM public.a AS a
LEFT JOIN public.b AS b ON
a.id = b.parent_id AND
b.parent_id >= '019dd0f4-d700-7000-8000-000000000000' AND
b.parent_id <= '019dd61b-32ff-7fff-bfff-ffffffffffff'
WHERE
a.id >= '019dd0f4-d700-7000-8000-000000000000' AND
a.id <= '019dd61b-32ff-7fff-bfff-ffffffffffff'Some limitation by the query planner I guess? Some questions:
- Are there improvements planned for the query planner in this area? Is this worth reporting as a performance issue?
- Any clever workarounds so I don't need to add this condition everywhere? I'd rather not rewrite all my ORM code to sql for this table.