Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

201
Vistas
PostgreSQL gist index

I have a table with two date like dateTo and dateFrom, i would like use daterange approach in queries and a gist index, but it seem doesn't work. The table looks like:

CREATE TABLE test (
  id         bigeserial,
  begin_date date,
  end_date   date
);
CREATE INDEX "idx1"
  ON test
  USING gist (daterange(begin_date, end_date));

Then when i try to explain a query like:

SELECT t.*
FROM test t
WHERE daterange(t.begin_date,t.end_date,'[]') && daterange('2015-12-30 00:00:00.0','2016-10-28 00:00:00.0','[]')

i get a Seq Scan.

Is this usage of gist index wrong, or is this scenario not feasible?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You have an index on the expression daterange(begin_date, end_date), but you query your table with daterange(begin_date, end_date, '[]') && .... PostgreSQL won't do math instead of you. To re-phrase your problem, it is like you're indexing (int_col + 2) and querying WHERE int_col + 1 > 2. Because the two expressions are different, the index will not be used in any circumstances. But as you can see, you can do the math (i.e. re-phrase the formula) sometimes.

You'll either need:

CREATE INDEX idx1 ON test USING gist (daterange(begin_date, end_date, '[]'));

Or:

CREATE INDEX idx2 ON test USING gist (daterange(begin_date, end_date + 1));

Note: both of them creates a range which includes end_date. The latter one uses the fact that daterange is discrete.

And use the following predicates for each of the indexes above:

WHERE daterange(begin_date, end_date, '[]') && daterange(?, ?, ?)

Or:

WHERE daterange(begin_date, end_date + 1) && daterange(?, ?, ?)

Note: the third parameter of the range constructor on the right side of && does not matter (in the context of index usage).

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda