Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!