Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

204
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda