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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:15:40+00:00 2026-06-11T10:15:40+00:00

I have a situation where I need to ensure that there is only one

  • 0

I have a situation where I need to ensure that there is only one active record with the same object_id and user_id at any time. Here is a representative table:

CREATE TABLE actions (
    id SERIAL PRIMARY KEY,
    object_id integer,
    user_id integer,
    active boolean default true,
    created_at timestamptz default now()
);

By only one active record at a time, I mean you could have a sequence of inserts like the following:

insert into actions (object_id, user_id, active) values (1, 1, true);
insert into actions (object_id, user_id, active) values (1, 1, false);

but doing a subsequent

insert into actions (object_id, user_id, active) values (1, 1, true);

should fail because at this point in time, there already exists 1 active tuple with object_id = 1 and user_id = 1.

I’m using PostgreSQL 8.4.

I saw this post which looks interesting, but its Oracle specific.

I also saw this post but it requires more care regarding the transaction isolation level. I don’t think it would work as-is in read committed mode.

My question is what other options are available to unsure this kind of constraint?

Edit: Removed the third insert in the first set. I think it was confusing the example. I also added the created_at time stamp to help with the context. To reiterate, there can be multiple (object_id, user_id, false) tuples, but only one (object_id, user_id, true) tuple.

Update: I accepted Craig’s answer, but for others who may stumble upon something similar, here is another possible (though suboptimal) solution.

CREATE TABLE action_consistency (
    object_id integer,
    user_id integer,
    count integer default 0,
    primary key (object_id, user_id),
    check (count >= 0 AND count <= 1)
);


CREATE OR REPLACE FUNCTION keep_action_consistency()
  RETURNS TRIGGER AS
$BODY$
    BEGIN

    IF NEW.active THEN
        UPDATE action_consistency
        SET count = count + 1
        WHERE object_id = NEW.object_id AND
        user_id   = NEW.user_id;

        INSERT INTO action_consistency (object_id, user_id, count)
        SELECT NEW.object_id, NEW.user_id, 1
        WHERE NOT EXISTS (SELECT 1 
                          FROM action_consistency
                          WHERE object_id = NEW.object_id AND
                                user_id   = NEW.user_id);
    ELSE
        -- assuming insert will be active for simplicity
        UPDATE action_consistency
        SET count = count - 1
        WHERE object_id = NEW.object_id AND
        user_id   = NEW.user_id;
    END IF;

    RETURN NEW;

    END;
$BODY$
LANGUAGE plpgsql;

CREATE TRIGGER ensure_action_consistency AFTER INSERT OR UPDATE ON actions
    FOR EACH ROW EXECUTE PROCEDURE keep_action_consistency();

It requires the use of a tracking table. For what I hope are obvious reasons, this is not at all desirable. It means that you have an additional row each distinct (object_id, user_id) in actions.

Another reason why I accepted @Craig Ringer’s answer is that there are foreign key references to actions.id in other tables that are also rendered inactive when a given action tuple changes state. This why the history table is less ideal in this scenario. Thank you for the comments and answers.

  • 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-11T10:15:41+00:00Added an answer on June 11, 2026 at 10:15 am

    Given your specification that you want to limit only one entry to being active at a time, try:

    CREATE TABLE actions (
        id SERIAL PRIMARY KEY,
        object_id integer,
        user_id integer,
        active boolean default true,
        created_at timestamptz default now()
    );
    
    CREATE UNIQUE INDEX actions_unique_active_y ON actions(object_id,user_id) WHERE (active = 't');
    

    This is a partial unique index, a PostgreSQL specific feature – see partial indexes. It constrains the set such that only one (object_id,user_id) tuple may exist where active is true.

    While that strictly answers your question as you explained further in comments, I think wildplasser’s answer describes the more correct choice and best approach.

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

Sidebar

Related Questions

I have the following situation: There is a windows folder that has been mounted
i have a situation where i need to calculate the remaining time every 6
I have a situation where I need to programmatically set something that has a
I have a situation where I need to find time spans between value changes.
We have a situation that need to user web form just like an ExcelSheet,
I have situation where I need to change the order of the columns/adding new
I have a situation where I need to dynamically build up a list of
I have a situation where I need to pass two parameters to an action.
I have a situation where I need to update a control referenced in a
I have a situation where I need to notify some users when something in

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.