160

I want to add indexes to some of the columns in a table on creation. Is there are way to add them to the CREATE TABLE definition or do I have to add them afterward with another query?

CREATE INDEX reply_user_id ON reply USING btree (user_id);

3 Answers 3

167

There doesn't seem to be any way of specifying an index in the CREATE TABLE syntax. PostgreSQL does however create an index for unique constraints and primary keys by default, as described in this note:

PostgreSQL automatically creates an index for each unique constraint and primary key constraint to enforce uniqueness.

Other than that, if you want a non-unique index, you will need to create it yourself in a separate CREATE INDEX query.

2
  • 1
    Thanks, I didn't know about the unique constraint creating an index.
    – Xeoncross
    Jun 4, 2011 at 22:37
  • 2
    Note that PostgreSQL supports transactional schema updates - it's a good idea to BEGIN/COMMIT around your CREATE TABLE and CREATE INDEX statements, if you want the net overall table creation to succeed or fail as a whole. May 20, 2019 at 12:43
30

No.

However, you can create unique indexes in the create, but that's because they are classed as constraints. You can't create a "general" index.

27
+50

Peter Krauss is looking for a canonical answer:

There are a MODERN SYNTAX (year 2020), so please explain and show examples, compatible with postgresql.org/docs/current/sql-createtable.html

You are searching for inline index definition, which is not available for PostgreSQL up to current version 12. Except UNIQUE/PRIMARY KEY constraint, that creates underlying index for you.

CREATE TABLE

[ CONSTRAINT constraint_name ] { CHECK ( expression ) [ NO INHERIT ] | UNIQUE ( column_name [, ... ] ) index_parameters | PRIMARY KEY ( column_name [, ... ] ) index_parameters |


The sample syntax of inline column definition(here SQL Server):

CREATE TABLE tab(
  id INT PRIMARY KEY,                            -- constraint
  c INT INDEX filtered (c) WHERE c > 10,         -- filtered index
  b VARCHAR(10) NOT NULL INDEX idx_tab_b,        -- index on column
  d VARCHAR(20) NOT NULL,
  INDEX my_index NONCLUSTERED(d)                 -- index on column as separate entry
);

db<>fiddle demo

The rationale behind introducing them is quite interesting What are Inline Indexes? by Phil Factor

4
  • Hi, solved (!). Can you add something about more complex case, for example b VARCHAR(10) NOT NULL INDEX idx_tab_b gin (b gin_trgm_ops) .. how to add more parameters without repeat column name? Mar 20, 2020 at 2:30
  • @PeterKrauss The example I provided is for SQL Server which does not support gin_trgm_ops. Mar 21, 2020 at 8:28
  • Hi @LukaszSzozda For syntax, perhaps you can try to transform any kind of complex CREATE INDEX idxName ON tableName USING MethodName (fieldName optionName); into syntax of inline index definition. PS: this question is about PostgreSQL not Microsoft-SQL-Server (see tags). Mar 21, 2020 at 17:55
  • @PeterKrauss I am fully aware that this question is about PostgreSQL. I just gave you a proper name of this feature and show example how this could look like. Let's hope more RDBMS will support inlinde indexes in the future :) Mar 21, 2020 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.