mirror of
https://github.com/postgres/postgres.git
synced 2025-09-09 13:09:39 +03:00
Relax transactional restrictions on ALTER TYPE ... ADD VALUE.
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. Andrew Dunstan and Tom Lane Discussion: <4075.1459088427@sss.pgh.pa.us>
This commit is contained in:
@@ -262,26 +262,42 @@ DROP TYPE bogus;
|
||||
--
|
||||
CREATE TYPE bogus AS ENUM('good');
|
||||
|
||||
-- check that we can't add new values to existing enums in a transaction
|
||||
-- check that we can add new values to existing enums in a transaction
|
||||
-- but we can't use them
|
||||
BEGIN;
|
||||
ALTER TYPE bogus ADD VALUE 'bad';
|
||||
ALTER TYPE bogus ADD VALUE 'new';
|
||||
SAVEPOINT x;
|
||||
SELECT 'new'::bogus; -- unsafe
|
||||
ROLLBACK TO x;
|
||||
SELECT enum_first(null::bogus); -- safe
|
||||
SELECT enum_last(null::bogus); -- unsafe
|
||||
ROLLBACK TO x;
|
||||
SELECT enum_range(null::bogus); -- unsafe
|
||||
ROLLBACK TO x;
|
||||
COMMIT;
|
||||
SELECT 'new'::bogus; -- now safe
|
||||
SELECT enumlabel, enumsortorder
|
||||
FROM pg_enum
|
||||
WHERE enumtypid = 'bogus'::regtype
|
||||
ORDER BY 2;
|
||||
|
||||
-- check that we recognize the case where the enum already existed but was
|
||||
-- modified in the current txn
|
||||
-- modified in the current txn; this should not be considered safe
|
||||
BEGIN;
|
||||
ALTER TYPE bogus RENAME TO bogon;
|
||||
ALTER TYPE bogon ADD VALUE 'bad';
|
||||
SELECT 'bad'::bogon;
|
||||
ROLLBACK;
|
||||
|
||||
DROP TYPE bogus;
|
||||
|
||||
-- check that we *can* add new values to existing enums in a transaction,
|
||||
-- if the type is new as well
|
||||
-- check that we can add new values to existing enums in a transaction
|
||||
-- and use them, if the type is new as well
|
||||
BEGIN;
|
||||
CREATE TYPE bogus AS ENUM();
|
||||
ALTER TYPE bogus ADD VALUE 'good';
|
||||
CREATE TYPE bogus AS ENUM('good');
|
||||
ALTER TYPE bogus ADD VALUE 'bad';
|
||||
ALTER TYPE bogus ADD VALUE 'ugly';
|
||||
SELECT enum_range(null::bogus);
|
||||
ROLLBACK;
|
||||
|
||||
--
|
||||
|
Reference in New Issue
Block a user