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 (redux).
Originally committed as15bc038f
(plus some follow-ups), this was reverted in28e07270
due to a problem discovered in parallel workers. This new version corrects that problem by sending the list of uncommitted enum values to parallel workers. Here follows the original commit message describing the change: 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. Author: Andrew Dunstan and Tom Lane, with parallel query fix by Thomas Munro Reviewed-by: Tom Lane Discussion: https://postgr.es/m/CAEepm%3D0Ei7g6PaNTbcmAh9tCRahQrk%3Dr5ZWLD-jr7hXweYX3yg%40mail.gmail.com Discussion: https://postgr.es/m/4075.1459088427%40sss.pgh.pa.us
This commit is contained in:
@@ -273,19 +273,34 @@ ALTER TYPE rainbow RENAME VALUE 'blue' TO 'green';
|
||||
--
|
||||
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;
|
||||
|
||||
-- but ALTER TYPE RENAME VALUE is safe in a transaction
|
||||
-- but a renamed value is safe to use later in same transaction
|
||||
BEGIN;
|
||||
ALTER TYPE bogus RENAME VALUE 'good' to 'bad';
|
||||
SELECT 'bad'::bogus;
|
||||
@@ -293,12 +308,21 @@ 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 values created during CREATE TYPE can be used in any case
|
||||
BEGIN;
|
||||
CREATE TYPE bogus AS ENUM();
|
||||
ALTER TYPE bogus ADD VALUE 'good';
|
||||
ALTER TYPE bogus ADD VALUE 'ugly';
|
||||
CREATE TYPE bogus AS ENUM('good','bad','ugly');
|
||||
ALTER TYPE bogus RENAME TO bogon;
|
||||
select enum_range(null::bogon);
|
||||
ROLLBACK;
|
||||
|
||||
-- ideally, we'd allow this usage; but it requires keeping track of whether
|
||||
-- the enum type was created in the current transaction, which is expensive
|
||||
BEGIN;
|
||||
CREATE TYPE bogus AS ENUM('good');
|
||||
ALTER TYPE bogus RENAME TO bogon;
|
||||
ALTER TYPE bogon ADD VALUE 'bad';
|
||||
ALTER TYPE bogon ADD VALUE 'ugly';
|
||||
select enum_range(null::bogon); -- fails
|
||||
ROLLBACK;
|
||||
|
||||
--
|
||||
|
Reference in New Issue
Block a user