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 8890039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:24:32+00:00 2026-06-14T22:24:32+00:00

PostgreSql 9.2 Reservation table is defined as CREATE EXTENSION btree_gist; CREATE TABLE schedule (

  • 0

PostgreSql 9.2 Reservation table is defined as

CREATE EXTENSION btree_gist;
CREATE TABLE schedule (
  id serial primary key,
  during tsrange not null,
  EXCLUDE USING gist (during WITH &&)
);

Holidays are listed in table

CREATE TABLE holiday ( day primary key );

Work hours are from 8 to 18:00 in work days and reservatons can be done by 30 minute intervals only.
How to add constraints to during values so that it allows only reservations during work time:

  1. Start and end dates in tsrange are always same.
  2. Start and end dates cannot be saturday and sunday
  3. Start and end dates cannot appear in public holiday table
  4. Start time can be only 8:00 , 8:30, 9:00, 9:30, … 16:00, 16:30, 17:00 or 17:30 inclusive
  5. End time can be only 8:30, 9:00, 9:30, … 16:00, 16:30, 17:00, 17:30 or 18:00 exclusive

Is it reasonable to add those or some of those constraints to this table ?
If yes, how to add ?
schedule table structure can changed if this helps.

  • 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-06-14T22:24:33+00:00Added an answer on June 14, 2026 at 10:24 pm

    You need to change you table definition and add some check constraints:

    CREATE TABLE schedule (
      id serial primary key,
      during tsrange not null check(
        (lower(during)::date = upper(during)::date) and 
        (date_trunc('hour', upper(during)) + INTERVAL '30 min' * ROUND(date_part('minute', upper(during)) / 30.0) = upper(during)) and
        (date_trunc('hour', lower(during)) + INTERVAL '30 min' * ROUND(date_part('minute', lower(during)) / 30.0) = lower(during)) and
        (lower(during)::time >= '8:00'::time and upper(during)::time <= '18:00'::time) and
        (date_part('dow', lower(during)) in (1,2,3,4,5) and date_part('dow', upper(during)) in (1,2,3,4,5))
      ),
      EXCLUDE USING gist (during WITH &&)
    );
    

    The checks are in this order

    • start and end day are the same
    • start / end can must be on 30 min boundary
    • and between 8:00 .. 18:00
    • only weekdays

    We need something in holiday table:
    insert into holiday values (‘2012-11-28’);

    check can not reference other table hence we need trigger function (it might be better to put all check into this function i.e. have them at one place):

    create function holiday_check() returns trigger language plpgsql stable as $$
    begin
        if exists (select * from holiday where day in (lower(NEW.during)::date, upper(NEW.during)::date)) then
            raise exception 'public holiday';
        else
            return NEW;
        end if;
    end;
    $$;
    

    Then we need to create triggers before insert/update:

    create trigger holiday_check_i before insert on schedule for each row execute procedure holiday_check();
    create trigger holiday_check_u before update on schedule for each row execute procedure holiday_check();
    

    Finally, some tests:

    -- OK
    insert into schedule(during) values (tsrange('2012-11-26 08:00', '2012-11-26 09:00'));
    INSERT 0 1
    
    -- out of business hours
    insert into schedule(during) values (tsrange('2012-11-26 04:00', '2012-11-26 05:00'));
    ERROR:  new row for relation "schedule" violates check constraint "schedule_during_check"
    DETAIL:  Failing row contains (12, ["2012-11-26 04:00:00","2012-11-26 05:00:00")).
    
    -- End time can be only 8:30, 9:00, 9:30, ... 16:00, 16:30, 17:00, 17:30 or 18:00 exclusive
    insert into schedule(during) values (tsrange('2012-11-26 08:00', '2012-11-26 09:10'));
    ERROR:  new row for relation "schedule" violates check constraint "schedule_during_check"
    DETAIL:  Failing row contains (13, ["2012-11-26 08:00:00","2012-11-26 09:10:00")).
    
    -- Start time can be only 8:00 , 8:30, 9:00, 9:30, ... 16:00, 16:30, 17:00 or 17:30 inclusive
    insert into schedule(during) values (tsrange('2012-11-26 11:24', '2012-11-26 13:00'));
    ERROR:  new row for relation "schedule" violates check constraint "schedule_during_check"
    DETAIL:  Failing row contains (14, ["2012-11-26 11:24:00","2012-11-26 13:00:00")).
    
    -- holiday
    insert into schedule(during) values (tsrange('2012-11-28 10:00', '2012-11-28 13:00'));
    ERROR:  public holiday
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In PostgreSQL I have table comments with primary key comment_id ( VARCHAR of length
Using PostgreSQL 8.4 and with a table such as this: create table log (
Postgresql 9.2 reservation table contains objekt2 column whose type is char(10) . The following
In PostgreSQL 8.3, let's say I have a table called widgets with the following:
I'm using PostgreSQL 9.1.3 and the following functions: CREATE OR REPLACE FUNCTION cad(INOUT args
Postgresql is not cooperating. I did a yum of postgresql and postgresql-server and that
PostgreSQL stores statistics about tables in the system table called pg_class. The query planner
PostgreSQL 8.4 on Linux. I have a function: CREATE OR REPLACE FUNCTION production.add_customer (
In PostgreSQL 8.3 database I have bookings table referencing booking_transactions table by ID. So
Using PostgreSQL, supposing a table like the following: 12184 | 4 | 83 12183

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.