2

I have this table on PostgreSQL:


example_dev=# \d products
 Table "public.products"
 Column | Type | Collation | Nullable | Default 
--------+------------------------+-----------+----------+--------------------------------------
 id | bigint | | not null | nextval('products_id_seq'::regclass)
 name | character varying(255) | | | 
 price | integer | | | 
 sku | character varying(255) | | | 
Indexes:
 "products_pkey" PRIMARY KEY, btree (id)
 "name_price_unique_index" UNIQUE, btree (name, price)

As you see, I have a combined unique key name_price_unique_index on the columns name and price.

What I need to do is to modify this because I need to add sku column too. So I'd be needing a combination of the three columns.

Somthing like this:

"name_price_sku_unique_index" UNIQUE, btree (name, price, sku)

I've been looking for info on how to do this but I haven't found any.

Does anyone know how to do this?

asked Oct 20, 2021 at 17:18

1 Answer 1

4

You need to create a new index and replace the old one:

CREATE UNIQUE INDEX CONCURRENTLY newidx ON tab (name, price, sku);
DROP INDEX name_price_sku_unique_index;
ALTER INDEX newidx RENAME TO name_price_sku_unique_index;
answered Oct 20, 2021 at 17:29
2
  • why not removing index first ? Commented Apr 15, 2024 at 13:02
  • 1
    @gstackoverflow Your performance might suffer while there is no index. If that is irrelevant, then you might as well drop the index first. Commented Apr 15, 2024 at 14:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.