By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
Help with an interesting Postgres question: Why isn't an Index Only Scan used on a partition accessed via the parent table?.
CREATE TABLE a (
id text
);
INSERT INTO a VALUES ('A'), ('B'), ('C');
CREATE TABLE b (
id text,
val int
);
INSERT INTO b VALUES ('A',4), ('B', 5), ('B', 6), ('C', 7);
CREATE TABLE c (
id text,
val int
);
INSERT INTO c VALUES ('A', 7), ('B', 8), ('C', 9)
3 rows affected
4 rows affected
3 rows affected
SELECT
a.id,
b.val
FROM a
LEFT JOIN b ON a.id = b.id
GROUP BY GROUPING SETS ((a.id), (a.id, b.val))
id | val |
---|---|
B | 5 |
A | 4 |
B | 6 |
C | 7 |
B | null |
C | null |
A | null |
SELECT
ab.*,
c.val
FROM (
SELECT
a.id,
b.val
FROM a
LEFT JOIN b ON a.id = b.id
GROUP BY GROUPING SETS ((a.id), (a.id, b.val))
) ab
LEFT JOIN c ON ab.id = c.id AND ab.val IS NULL
id | val | val |
---|---|---|
A | 4 | null |
A | null | 7 |
B | 5 | null |
B | 6 | null |
B | null | 8 |
C | 7 | null |
C | null | 9 |