41

I am trying to add new type value to my existing types in PostgreSQL. But I get the following error

error: ALTER TYPE ... ADD cannot run inside a transaction block

The query I used to add a new value to the type is

ALTER TYPE public.request_type ADD VALUE "Check";

I am actually running above query in migrations file which is created using node-pg-migrate

Here public is my schema.

Any idea why this is failing?

Edit:

The below query executes fine when execute it in pgadmin

ALTER TYPE public.request_type ADD VALUE "Check";

But when I run above command through node-pg-migrate migrations it fails and throws above error

Hemadri Dasari
  • 32,666
  • 37
  • 119
  • 162

6 Answers6

72

As it was mentioned above you can't edit enum within transaction block. But you can create the new one. Here are the steps:

  1. Change type from request_type to varchar for all columns/tables which use this type:
ALTER TABLE table_name
    ALTER COLUMN column_name TYPE VARCHAR(255);
  1. Drop and create again request_type enum:
DROP TYPE IF EXISTS request_type;
CREATE TYPE request_type AS ENUM (
    'OLD_VALUE_1',
    'OLD_VALUE_2',
    'NEW_VALUE_1',
    'NEW_VALUE_2'
);
  1. Revert type from varchar to request_type for all columns/tables (revert step one):
ALTER TABLE table_name
    ALTER COLUMN column_name TYPE request_type
    USING (column_name::request_type);
anulaibar
  • 310
  • 2
  • 9
Nasar Kushnir
  • 721
  • 5
  • 3
  • 2
    This is actually great to circumvent the transaction issue, plus you can use a similar method to remove enum values (which is not supported directly by postgres). Thanks! – GavinoGrifoni Jul 18 '19 at 06:35
  • Killer solution. Love the casting abilities of PG! – mattdlockyer Sep 11 '19 at 23:03
  • 2
    This is a lovely solution; request_type is being used as a column name and the type; perhaps rename them to remove ambiquity. – rdsoze Nov 26 '19 at 14:55
  • Perfect! I also found I had to temporarily change the default value for the column as well as it's type because that also referenced the old ENUM type and so prevented DROP. – TomG Dec 05 '19 at 14:28
  • 1
    it's a bit confusing that you're rusing request_type for both the COLUMN and the TYPE. In step 3, which ones represent the column and which represent the type? request_type is mentioned 4 times in the same line – Guus Dec 17 '20 at 14:35
  • 1
    Dropping type will lose the existing data here. – Naveen Kumar Madipally Jan 22 '21 at 09:19
27

The reason is given in the following comment in AlterEnum in src/backend/commands/typecmds.c:

/*
 * Ordinarily we disallow adding values within transaction blocks,
 * because we can't cope with enum OID values getting into indexes and
 * then having their defining pg_enum entries go away.  However, it's
 * okay if the enum type was created in the current transaction, since
 * then there can be no such indexes that wouldn't themselves go away
 * on rollback.  (We support this case because pg_dump
 * --binary-upgrade needs it.)

Note that this restriction has been removed in commit 212fab99; the commit message reads:

To prevent possibly breaking indexes on enum columns, we must keep
uncommitted enum values from getting stored in tables, unless we
can be sure that any such column is new in the current transaction.

Formerly, we enforced this by disallowing ALTER TYPE ... ADD VALUE
from being executed at all in a transaction block, unless the target
enum type had been created in the current transaction.  This patch
removes that restriction, and instead insists that an uncommitted enum
value can't be referenced unless it belongs to an enum type created
in the same transaction as the value.  Per discussion, this should be
a bit less onerous.  It does require each function that could possibly
return a new enum value to SQL operations to check this restriction,
but there aren't so many of those that this seems unmaintainable.

So you might want to upgrade to PostgreSQL v12 some time soon :^)

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
  • Hi Sorry for late reply from my side and thank you for detailed explanation. Is there any way for me to overcome this issue without migrating it to v12? – Hemadri Dasari Nov 05 '18 at 14:18
  • Hi I have updated my question. Could you please help. The issue happens only when I run migrations but when I excecute that query manually in pgadmin it works fine – Hemadri Dasari Nov 05 '18 at 14:41
  • 1
    The node.js function probably runs inside a transaction. No idea how to change that. Perhaps it would be better to avoid enums. They are not a good idea if the values can change. – Laurenz Albe Nov 05 '18 at 19:51
  • Aurora AWS doesn't support PostgreSQL v12 :( – Yitzchak Nov 23 '20 at 18:43
  • @Yitzchak Choose a better provider or run your own databases... – Laurenz Albe Nov 24 '20 at 03:01
  • 1
    @Yitzchak it does now! https://aws.amazon.com/about-aws/whats-new/2021/01/amazon-aurora-supports-postgresql-12/ – Bryan McGrane May 24 '21 at 16:07
6

Workaround for earlier versions of PostgreSQL shown here:

Note this will require special permissions because it changes a system table.

  • Replace 'NEW_ENUM_VALUE' with the value you want.
  • Replace'type_egais_units' with the oid of the enum you want to change. (Use SELECT * FROM pg_enum to find the enum you want to update, in my case it was a 5-digit number like '19969')

The statement:

INSERT INTO pg_enum (
    enumtypid, 
    enumlabel, 
    enumsortorder
)
SELECT 
    'type_egais_units'::regtype::oid, 
    'NEW_ENUM_VALUE', 
    (SELECT MAX(enumsortorder) + 1 FROM pg_enum WHERE enumtypid = 'type_egais_units'::regtype)

Of course , upgrading PostgreSQL as suggested in the accepted answer, is probably the best.

Does anyone know how to avoid using transactions when running queries from pgAdmin Version 3.5? (i.e. when executing with F5?)

Nate Anderson
  • 18,334
  • 18
  • 100
  • 135
5

You can change your query to

COMMIT;
ALTER TYPE public.request_type ADD VALUE "Check";
VAG
  • 69
  • 1
  • 3
  • I guess it'll work, but it doesn't sounds like a good practice. in my case (and also in the case of the question) we're in the middle of a migration script. The migration is a transaction for free - this is how it works without us starting a transaction and it has a reason: rollback in case of a failure. If we commit we can make real bad issues... – Yitzchak Nov 23 '20 at 18:40
0

Specifically how to do this with node-pg-migrate is to disable transactions for the migration:

exports.up = (pgm) => {
  pgm.noTransaction()
  pgm.addTypeValue('foo', 'BAR', { ifNotExists: true })
}
vesse
  • 4,871
  • 26
  • 35
0

If you changelog in xml format, use <changeSet id="name" author="author" runInTransaction="false">