Re: ALTER TABLE fails when changing column type due to index with bit_ops opclass

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Manuel Rigger <rigger(dot)manuel(at)gmail(dot)com>
Cc: PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: ALTER TABLE fails when changing column type due to index with bit_ops opclass
Date: 2019-11-20 18:16:22
Message-ID: 17980.1574273782@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Manuel Rigger <rigger(dot)manuel(at)gmail(dot)com> writes:
> Consider the following statements:

> CREATE TABLE t0(c0 BIT VARYING(1));
> CREATE INDEX i0 ON t0(c0 bit_ops);
> ALTER TABLE t0 ALTER c0 TYPE TEXT; -- ERROR: operator class "bit_ops"
> does not accept data type text

> Altering the column type fails, which is somewhat unexpected, since it
> does not seem to cause problems for other opclasses. For example, the
> following executes without errors:

> CREATE TABLE t0(c0 TEXT);
> CREATE INDEX i0 ON t0(c0 text_ops);
> ALTER TABLE t0 ALTER c0 TYPE BIT VARYING(1) USING c0::bit varying(1);

> Is this a bug or expected?

I think this is expected behavior, more or less. The critical difference
is that you specified a non-default opclass in the first example (the
default choice for that column datatype is varbit_ops not bit_ops).
ALTER TABLE figures that it's okay to replace the default opclass for
the original type with the default opclass for the new type, but it's
not willing to guess about what you want if the index has a non-default
opclass. So the conversion only goes through if the specified opclass
also accepts the new datatype, which typically it wouldn't.

This is probably not documented anyplace. Should it be? If so, what
should we say and where?

regards, tom lane

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2019-11-21 01:14:18 BUG #16129: Segfault in tts_virtual_materialize in logical replication worker
Previous Message Tomas Vondra 2019-11-20 16:19:09 Re: BUG #16127: PostgreSQL 12.1 on Windows 2008 R2copy table from ‘large 2GB csv’report “Unknown error”