Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6030249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:00:31+00:00 2026-05-23T05:00:31+00:00

I have a PostgreSQL database with events. Each event has a datetime or an

  • 0

I have a PostgreSQL database with events. Each event has a datetime or an interval. Common data are stored in the events table and dates are stored in either events_dates (datetime field) or events_intervals (starts_date, ends_date both are date fields).

Sample datetime events

  • I was born on 1930-06-09
  • I got my driver’s license on 1950-07-12
  • Christmas is on 1900-12-24 (1900 is reserved for yearly reoccuring events)

Sample interval events

  • I’ll be on vacation from 2011-06-09 till 2011-07-23

Now I have a user that will want to look up these events. They will be able to fill out a form with from and to fields and in those fields they can enter full date, day, month, year, day and month, day and year, month and year in one or both fields.

Sample queries

  • From May 3 to 2012 December 21 will look for events between May 3 and December 21 whose max year is 2012
  • From day 3 to day 15 will look for events between the 3rd and 15th day of every month and year
  • From day 3 will look for events on the 3rd day of every month and year (same if from is empty and to is not)
  • From May 3 to June will look for events between May 3 and last day of June of every year
  • etc.

Any tips on how to write a maintanable query (it doesn’t necessarily have to be fast)?

Some things that we thought of

  • write all possible from, to and day/month/year combinations – not maintable
  • compare dates as strings e.g. input: ____-06-__ where _ is a wildcard – I wouldn’t have to generate all possible combinations but this doesn’t work for intervals
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:00:32+00:00Added an answer on May 23, 2026 at 5:00 am

    You can write maintainable queries that additionally are fast by using the pg/temporal extension:

    https://github.com/jeff-davis/PostgreSQL-Temporal

    create index on events using gist(period(start_date, end_date));
    
    select *
    from events
    where period(start_date, end_date) @> :date;
    
    select *
    from events
    where period(start_date, end_date) && period(:start, :end);
    

    You can even use it to disallow overlaps as a table constraint:

    alter table events
    add constraint overlap_excl
    exclude using gist(period(start_date, end_date) WITH &&);
    

    write all possible from, to and day/month/year combinations – not maintable

    It’s actually more maintainable than you might think, e.g.:

    select *
    from events
    join generate_series(:start_date, :end_date, :interval) as datetime
    on start_date <= datetime and datetime < end_date;
    

    But it’s much better to use the above-mentioned period type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a name of table or view in PostgreSQL database and need to
In PostgreSQL 8.3 database I have bookings table referencing booking_transactions table by ID. So
I am using PostgreSQL database. I have the data like below. id name1 name2
I have a postgresql database that has this column structure: Author id name Book
I have a Postgresql database on which I want to do a few cascading
I currently have an MS Access application that connects to a PostgreSQL database via
I have a really big database (running on PostgreSQL) containing a lot of tables
Right now I have a database (about 2-3 GB) in PostgreSQL, which serves as
I have a postgres database with a user table (userid, firstname, lastname) and a
I have a Rails project which has a Postgres database for the actual application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.