Thread: Create index on user defined type

Create index on user defined type

From
aditya desai
Date:
Hi,
Is there any way to create index on user defined type in Postgresql? Need to create index on bug_status in bug table.

CREATE TYPE bug_status AS ENUM ('new', 'open', 'closed');

CREATE TABLE bug (
    id serial,
    description text,
    status bug_status
);

Regards,
Aditya.

Re: Create index on user defined type

From
Christophe Pettus
Date:
> On Apr 19, 2022, at 09:27, aditya desai <admad123@gmail.com> wrote:
>
> Hi,
> Is there any way to create index on user defined type in Postgresql? Need to create index on bug_status in bug table.
>
> CREATE TYPE bug_status AS ENUM ('new', 'open', 'closed');
>
> CREATE TABLE bug (
>     id serial,
>     description text,
>     status bug_status
> );

It works right out of the box:

xof=# CREATE TYPE bug_status AS ENUM ('new', 'open', 'closed');
CREATE TYPE
xof=# CREATE TABLE bug (
    id serial,
    description text,
    status bug_status
);
CREATE TABLE
xof=# create index on bug(status);
CREATE INDEX
xof=#




Re: Create index on user defined type

From
aditya desai
Date:
Thanks! Apologies! Looks like I was having issues with NULL values and UNIQUE index on user defined column.

On Tue, Apr 19, 2022 at 10:00 PM Christophe Pettus <xof@thebuild.com> wrote:

> On Apr 19, 2022, at 09:27, aditya desai <admad123@gmail.com> wrote:
>
> Hi,
> Is there any way to create index on user defined type in Postgresql? Need to create index on bug_status in bug table.
>
> CREATE TYPE bug_status AS ENUM ('new', 'open', 'closed');
>
> CREATE TABLE bug (
>     id serial,
>     description text,
>     status bug_status
> );

It works right out of the box:

xof=# CREATE TYPE bug_status AS ENUM ('new', 'open', 'closed');
CREATE TYPE
xof=# CREATE TABLE bug (
    id serial,
    description text,
    status bug_status
);
CREATE TABLE
xof=# create index on bug(status);
CREATE INDEX
xof=#

Re: Create index on user defined type

From
Rob Sargent
Date:
On 4/19/22 11:03, aditya desai wrote:
Thanks! Apologies! Looks like I was having issues with NULL values and UNIQUE index on user defined column.

On Tue, Apr 19, 2022 at 10:00 PM Christophe Pettus <xof@thebuild.com> wrote:

> On Apr 19, 2022, at 09:27, aditya desai <admad123@gmail.com> wrote:
>
> Hi,
> Is there any way to create index on user defined type in Postgresql? Need to create index on bug_status in bug table.
>
> CREATE TYPE bug_status AS ENUM ('new', 'open', 'closed');
>
> CREATE TABLE bug (
>     id serial,
>     description text,
>     status bug_status
> );

It works right out of the box:

xof=# CREATE TYPE bug_status AS ENUM ('new', 'open', 'closed');
CREATE TYPE
xof=# CREATE TABLE bug (
    id serial,
    description text,
    status bug_status
);
CREATE TABLE
xof=# create index on bug(status);
CREATE INDEX
xof=#

I would have to wonder at the effectiveness an index on such a small number of possible values.