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

  • Home
  • SEARCH
  • 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 8791047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:47:22+00:00 2026-06-13T22:47:22+00:00

In PostgreSQL, when a combination of multiple columns is specified as the PRIMARY KEY

  • 0

In PostgreSQL, when a combination of multiple columns is specified as the PRIMARY KEY, how are the records ordered?

This is with the assumption that PostgreSQL orders the records in the order of the primary key. Does it?

Also, is the primary key automatically indexed in case of PostgreSQL?

  • 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-13T22:47:23+00:00Added an answer on June 13, 2026 at 10:47 pm

    This question makes the misguided assumption that the primary key imposes a table order at all. It doesn’t. PostgreSQL tables have no defined order, with or without a primary key; they’re a “heap” of rows arranged in page blocks. Ordering is imposed using the ORDER BY clause of queries when desired.

    You might be thinking that PostgreSQL tables are stored as index-oriented tables that’re stored on disk in primary key order, but that isn’t how Pg works. I think InnoDB stores tables organized by the primary key (but haven’t checked), and it’s optional in some other vendors’ databases using a feature often called “clustered indexes” or “index-organized tables”. This feature isn’t currently supported by PostgreSQL (as of 9.3 at least).

    That said, the PRIMARY KEY is implemented using a UNIQUE index, and there is an ordering to that index. It is sorted in ascending order from the left column of the index (and therefore the primary key) onward, as if it were ORDER BY col1 ASC, col2 ASC, col3 ASC;. The same is true of any other b-tree (as distinct from GiST or GIN) index in PostgreSQL, as they’re implemented using b+trees.

    So in the table:

    CREATE TABLE demo (
       a integer,
       b text, 
       PRIMARY KEY(a,b)
    );
    

    the system will automatically create the equivalent of:

    CREATE UNIQUE INDEX demo_pkey ON demo(a ASC, b ASC);
    

    This is reported to you when you create a table, eg:

    regress=>     CREATE TABLE demo (
    regress(>        a integer,
    regress(>        b text, 
    regress(>        PRIMARY KEY(a,b)
    regress(>     );
    NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "demo_pkey" for table "demo"
    CREATE TABLE
    

    You can see this index when examining the table:

    regress=> \d demo
         Table "public.demo"
     Column |  Type   | Modifiers 
    --------+---------+-----------
     a      | integer | not null
     b      | text    | not null
    Indexes:
        "demo_pkey" PRIMARY KEY, btree (a, b)
    

    You can CLUSTER on this index to re-order the table according to the primary key, but it’s a one-time operation. The system won’t maintain that ordering – though if there’s space free in the pages due to a non-default FILLFACTOR I think it will try to.

    One consequence of the inherent ordering of the index (but not the heap) is that it is much faster to search for:

    SELECT * FROM demo ORDER BY a, b;
    SELECT * FROM demo ORDER BY a;
    

    than:

    SELECT * FROM demo ORDER BY a DESC, b;
    

    and neither of these can use the primary key index at all, they’ll do a seqscan unless you have an index on b:

    SELECT * FROM demo ORDER BY b, a;
    SELECT * FROM demo ORDER BY b;
    

    This is becaues PostgreSQL can use an index on (a,b) almost as fast as an index on (a) alone. It cannot use an index on (a,b) as if it were an index on (b) alone – not even slowly, it just can’t.

    As for the DESC entry, for that one Pg must do a reverse index scan, which is slower than an ordinary forward index scan. If you’re seeing lots of reverse index scans in EXPLAIN ANALYZE and you can afford the performance cost of the extra index you can create an index on the field in DESC order.

    This is true for WHERE clauses, not just ORDER BY. You can use an index on (a,b) to search for WHERE a = 4 or WHERE a = 4 AND b = 3 but not to search for WHERE b = 3 alone.

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

Sidebar

Related Questions

In PostgreSQL, there is this very useful string_agg function, that allows query like: SELECT
I currently have 2000 records in a postgresql database being updated every minute that
In PostgreSQL: I have a Table that has 3 columns: CustomerNum, OrderNum, OrderDate .
I need a Postgresql Query that returns the count of every type of combination
PostgreSQL does not allow altering a view (i.e. adding column, changing column orders, adding
Using PostgreSQL triggers, is it possible to record the changes that have happened to
I'm using PostgreSQL 9.1, I thought it was supposed to support this XML integration
I have a parameterized postgresql query that users interact with through a PHP front-end.
In my Postgresql database, I have a timestamp without time zone with this value
How to make this (PostgreSQL) using Rails syntax?: SELECT fld1, fld2 FROM ( SELECT

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.