psql's \d display of unique index vs. constraint

Поиск
Список
Период
Сортировка
От Josh Kupershmidt
Тема psql's \d display of unique index vs. constraint
Дата
Msg-id t2i4ec1cf761004091501r6b1a27e0q7d6dd63509cd7744@mail.gmail.com
обсуждение исходный текст
Ответы Re: psql's \d display of unique index vs. constraint  (Peter Eisentraut <peter_e@gmx.net>)
Список pgsql-general
Hi all,

I often come across tables with either a unique index or a unique
constraint on them, and psql isn't helpful at showing the difference
between the two. Normally, I don't care which is which, except for
when I have to manually drop and recreate the index or constraint to
speed up a bulk load.

Consider the following two tables, con_tbl and idx_tbl:
    CREATE TABLE con_tbl (
       pkid       int PRIMARY KEY,
       identifier text
    );
    ALTER TABLE con_tbl
        ADD CONSTRAINT "con_tbl_identifier_key" UNIQUE ("identifier");

    CREATE TABLE idx_tbl (
      pkid        int PRIMARY KEY,
      identifier  text
    );
    CREATE UNIQUE INDEX "idx_tbl_identifier_key" ON "idx_tbl" ("identifier");

If you use psql's \d command to describe the two tables, you'll see:
<snip>
Indexes:
    "idx_tbl_pkey" PRIMARY KEY, btree (pkid)
    "idx_tbl_identifier_key" UNIQUE, btree (identifier)
<snip>
Indexes:
    "con_tbl_pkey" PRIMARY KEY, btree (pkid)
    "con_tbl_identifier_key" UNIQUE, btree (identifier)

These two displays are exactly the same, except for the names I chose.
However, if you try either of:
  DROP INDEX "con_tbl_identifier_key";
  ALTER TABLE "idx_tbl"
      DROP CONSTRAINT "idx_tbl_identifier_key";

you'll see both of these commands fail -- you have to know which was
declared as a constraint, and which as an index, in order to know to
use:
    ALTER TABLE "con_tbl"
        DROP CONSTRAINT "con_tbl_identifier_key";
    DROP INDEX "idx_tbl_identifier_key";

Unless there's a simple psql command or display option I'm missing
(\d+ doesn't help), I think it would be better if the \d display of
"idx_tbl_identifier_key" was kept as-is, and the display for
"con_tbl_identifier_key" was presented under a separate "Unique
Constraints" section or a similar heading. Even better would be
allowing either ALTER TABLE ... DROP CONSTRAINT or DROP INDEX to work
regardless of how the unique index or constraint was declared, though
perhaps that would be more work.

Tested with a recent psql:
$ psql -V
psql (PostgreSQL) 9.0devel
contains support for command-line editing


Thanks
Josh

В списке pgsql-general по дате отправления:

Предыдущее
От: Sam Mason
Дата:
Сообщение: Re: Can not connect remotely
Следующее
От: Allan Kamau
Дата:
Сообщение: @> and <@ (contains and is contained by) operations on large arrays