Skip to main content
added 5 characters in body
Source Link
Laurenz Albe
  • 56.4k
  • 4
  • 50
  • 82

You always need a column in an index.

You could try to index a constant that occupies only one byte:

SELECT typname FROM pg_type WHERE typlen = 1;

 typname 
---------
 bool
 char
(2 rows)

If you choose boolean, that would be

CREATE INDEX ON foos ((TRUE)) WHERE thing_id IS NULL;

But my recommendation is to index id. Sure, that would make the index somewhat bigger, but your second query could use a much faster index-only scan.

You always need a column in an index.

You could try to index a constant that occupies only one byte:

SELECT typname FROM pg_type WHERE typlen = 1;

 typname 
---------
 bool
 char
(2 rows)

If you choose boolean, that would be

CREATE INDEX ON ((TRUE)) WHERE thing_id IS NULL;

But my recommendation is to index id. Sure, that would make the index somewhat bigger, but your second query could use a much faster index-only scan.

You always need a column in an index.

You could try to index a constant that occupies only one byte:

SELECT typname FROM pg_type WHERE typlen = 1;

 typname 
---------
 bool
 char
(2 rows)

If you choose boolean, that would be

CREATE INDEX ON foos ((TRUE)) WHERE thing_id IS NULL;

But my recommendation is to index id. Sure, that would make the index somewhat bigger, but your second query could use a much faster index-only scan.

Source Link
Laurenz Albe
  • 56.4k
  • 4
  • 50
  • 82

You always need a column in an index.

You could try to index a constant that occupies only one byte:

SELECT typname FROM pg_type WHERE typlen = 1;

 typname 
---------
 bool
 char
(2 rows)

If you choose boolean, that would be

CREATE INDEX ON ((TRUE)) WHERE thing_id IS NULL;

But my recommendation is to index id. Sure, that would make the index somewhat bigger, but your second query could use a much faster index-only scan.