I'm upgrading PostgreSQL from 9.6 to 13. Following script works on 9.6:
BEGIN
CREATE TYPE x AS ENUM ('foo', 'bar');
ALTER TYPE x ADD VALUE 'baz';
SELECT 'baz'::x;
END;
But on 13 ends up with:
ERROR: unsafe use of new value "baz" of enum type x
LINE 1: SELECT 'baz'::x;
^
HINT: New enum values must be committed before they can be used.
Googling suggests this is earlier than 13, but I don't know which version exactly.
I'm pretty sure I have a good reason to create enum, add value and use this value in the same transaction. Details in the end of the question.
Is there any known clean workaround?
[EDIT] - why I want to do this
I have a set of SQL files
v_0.01.sql
v_0.02.sql
v_0.03.sql
...
that are incremental, i.e. "database version X" is contained in "all SQL files up to X", e.g. version 0.02 is installed with
cat v_0.01.sql v_0.02.sql | psql -1
Some of those files contain CREATE TYPE
s, some other ALTER TYPE
s. I'll probably add more of those in the future files.
This is convenient, because in any given moment I can easily:
- install a fresh database in any desired version
- upgrade database from any version to any other version
- check the difference between versions
On 9.6, the second dot required some more effort - namely performing any ALTER TYPE
s outside a transaction. On 13 this is required also for the first operation and this is inconvenient, because I do this much more often and also there is more-total-SQL, so more ALTER TYPE
s.
I'd like to keep:
- current files structure
- easy installation of fresh databases
- single-transaction installations (
psql -1
) - this way I never confuse correct installation with a failed one