47

In Microsoft SQL Server and MySQL, index names need to unique within the table, but not within the database. This doesn't seem to be the case for PostgreSQL.

Here's what I'm doing: I made a copy of a table using CREATE TABLE new_table AS SELECT * FROM old_table etc and need to re-create the indexes.

Running a query like CREATE INDEX idx_column_name ON new_table USING GIST(column_name) causes ERROR: relation "idx_column_name" already exists

What's going on here?

6
  • 1
    Names are unique within the schema. (schema := namespace for tables and constraints (and functions,etc). (though cross-schema-constraints are allowed)) Commented Dec 5, 2014 at 0:04
  • BTW: is this the actual error message? ( idx_column_name is different from idx_pickup_geom ) Commented Dec 5, 2014 at 0:13
  • 3
    Indexes share the same namespace ( :=schema) with tables. (index := table). You'll need to invent another name (or omit it: the system can invent a name for you) Commented Dec 5, 2014 at 0:20
  • @wildplasser thanks for the explanation and suggestion. I gave it a +1. Want to add it as an answer? Commented Dec 5, 2014 at 0:33
  • 1
    I could. Oh well: maybe I should. For the future readers. Commented Dec 5, 2014 at 0:46

2 Answers 2

50

Indexes and tables (and views, and sequences, and...) are stored in the pg_class catalog, and they're unique per schema due to a unique key on it:

# \d pg_class
      Table "pg_catalog.pg_class"
     Column     |   Type    | Modifiers 
----------------+-----------+-----------
 relname        | name      | not null
 relnamespace   | oid       | not null
 ...
Indexes:
    "pg_class_oid_index" UNIQUE, btree (oid)
    "pg_class_relname_nsp_index" UNIQUE, btree (relname, relnamespace)

Per @wildplasser's comment, you can omit the name when creating the index, and PG will assign a unique name automatically.

Sign up to request clarification or add additional context in comments.

Comments

11
  • Names are unique within the schema. A schema is basically a namespace for {tables,constraints}, (and indexes, functions,etc).
  • cross-schema-constraints are allowed
  • Indexes share their namespace ( :=schema) with tables. (for Postgres: an index is a table).
  • (IIRC) the SQL standard does not define indexes; use constraints whenever you can (The GIST index in the question is probably an exception)
  • Ergo You'll need to invent another name.
  • or omit it: the system can invent a name if you dont supply one.
  • The downside of this: you can create multipe indices with the same definition (their names will be suffixed with _1, _2, IIRC)

4 Comments

It's not about the namespace (:= schema). If memory serves, it's about constraints on pg_class. :-)
IIRC (-;) ,the catalogs only use numeric ID. (which is PK) the namespace/class is functionally dependant. Other (candidate) keys may exist, such as {schema, table}, or even {objecttype, schema, name}
@wildplasser To postgresql an index is a relation, not a table. A relation is "most everything that has columns or is otherwise similar to a table" (from the postgresql.org/docs/9.3/static/catalog-pg-class.html doc), including indexes, views, sequences, etc.
Actually, constraint names are unique per table: "PostgreSQL does not require constraint names to be unique within a schema (but only per-table)"

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.