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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:59:40+00:00 2026-06-03T07:59:40+00:00

I have a many-to-many join table in Postgres that I would like to index

  • 0

I have a many-to-many join table in Postgres that I would like to index to A) increase performance (obviously) and B) enforce uniqueness. For example:

a_id | b_id
1    | 2     <- okay
1    | 3     <- okay
2    | 3     <- okay
1    | 3     <- not okay (same as row 2)

Is it possible to have a single index on two columns that enforces uniqueness in the values? What type of index should I use?

  • 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-03T07:59:41+00:00Added an answer on June 3, 2026 at 7:59 am

    As Primary Key

    Do this if that unique is primary key:

    create table tbl(
       a_id int not null,
       b_id int not null,
       constraint tbl_pkey primary key(a_id,b_id)
    );
    

    Not Primary Key

    Do this if that unique is non-primary key:

    create table tbl(
    
       -- other primary key here, e.g.:
       -- id serial primary key,
    
       a_id int not null,
       b_id int not null,
       constraint tbl_unique unique(a_id,b_id)
    );
    

    Existing Table

    If you have existing table, do this instead:

    alter table tbl
          add constraint tbl_unique unique(a_id, b_id)
    

    That alter table display this message:

    NOTICE:  ALTER TABLE / ADD UNIQUE will create implicit index "tbl_unique" for table "tbl"
    
    
    Query returned successfully with no result in 22 ms.
    

    Drop

    If you wanted to drop that constraint(you might want to make unique a combination of 3 fields):

    ALTER TABLE tbl DROP CONSTRAINT tbl_unique;
    

    Index & Constraint & Nulls

    Regarding index, from Postgres doc:

    PostgreSQL automatically creates a unique index when a unique
    constraint or primary key is defined for a table

    Source: http://www.postgresql.org/docs/9.1/static/indexes-unique.html


    If uniqueness depends on some rules, you shall use CREATE UNIQUE INDEX, for example:

    Given this:

    CREATE TABLE tbl
    (
      a_id integer NOT NULL,
      b_id integer NULL  
    );
    
    alter table tbl
        add constraint tbl_unique unique(a_id, b_id);
    

    That unique can catch these duplicates, this will be rejected by database:

    insert into tbl values
    (1,1),
    (1,1);
    

    Yet that UNIQUE CONSTRAINT cannot catch duplicate nulls. Nulls serves as unknown, they serves as wildcard, that’s why it’s allowed to have multiple nulls in unique constraint. This will be accepted by database:

    insert into tbl values
    (1,1),
    (1,null), -- think of this null as wildcard, some real value can be assigned later.
    (1,null); -- and so is this. that's why both of these nulls are allowed
    

    Think of UNIQUE CONSTRAINT that it allows deferred uniqueness, hence the acceptance of null values above.

    If you want only one wildcard(null b_id) per a_id, aside from the unique constraint, you need to add a UNIQUE INDEX. UNIQUE CONSTRAINT can’t have an expression on them. INDEX and UNIQUE INDEX can. This will be your complete DDL for rejecting multiple null;

    This will be your complete DDL:

    CREATE TABLE tbl
    (
      a_id integer NOT NULL,
      b_id integer NULL  
    );
    alter table tbl
        add constraint tbl_unique unique(a_id, b_id);
    
    create unique index tbl_unique_a_id on tbl(a_id) where b_id is null;      
    

    This will be rejected by your database now:

    insert into tbl values
    (1,1),
    (1,null),
    (1,null);
    

    This will be allowed:

    insert into tbl values
    (1,1),
    (1,null);
    

    Related to http://www.ienablemuch.com/2010/12/postgresql-said-sql-server2008-said-non.html

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

Sidebar

Related Questions

I have two classes with many-to-many relation so I created a join table in-between
I have a many-to-many relationship between two objects with a join table. I need
I have a table with a one to many mapping to a table that
UPDATED: to include model relationships I have a many to many join table between
If I have two objects that have a many-to-many relationship, I would typically model
Using Postgres v. 8.4.2. I have a standard three-table setup for a many-to-many relationship.
I have a users model that can have many holidays through a rich join
I have many tables that use Lookup/Enum references for most of their column values.
I have many links in my page. For example <a href=/promotions/download/schools/australia.aspx>Australia</a> Now I want
I have many forms that use AJAX (w/ jQuery) for validation and data submission.

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.