Re: [ADMIN] Problems with enums after pg_upgrade
Re: [ADMIN] Problems with enums after pg_upgrade
От:
Bernhard Schrader <bernhard.schrader@innogames.de>
Дата:
Hello again,
well, still everything is working.
What information do you need to get into this issue?
Well, so far i can say, we dont use ALTER TYPE ADD VALUE. We use some more or less changed enum_add and enum_del (Which are appended at the end) to be able to change enums within transactions.
And that this happened to the beta server and not to the staging server, might be because we sometimes have to drop the whole stuff of staging, because of some failures we did, so old enum values will not be persistent in old indexes.
if you need more info, just ask. :)
regards Bernhard
SET check_function_bodies = false;
CREATE OR REPLACE FUNCTION enum_add (enum_name character varying, enum_elem character varying) RETURNS void
AS
$body$
DECLARE
_enum_typid INTEGER;
version_int INTEGER;
_highest_enumsortorder REAL;
BEGIN
-- get enumtypid
SELECT oid FROM pg_type WHERE typtype='e' AND typname=enum_name INTO _enum_typid;
SELECT INTO version_int setting FROM pg_settings WHERE name = 'server_version_num';
--postgres 9.2 or higher
IF version_int > 90200 THEN
SELECT MAX(enumsortorder) FROM pg_enum WHERE enumtypid = _enum_typid INTO _highest_enumsortorder;
-- check if elem already exists in enum
IF NOT EXISTS (SELECT * FROM pg_enum WHERE enumlabel = enum_elem AND enumtypid = _enum_typid) THEN
INSERT INTO pg_enum(enumtypid, enumlabel, enumsortorder) VALUES (
_enum_typid,
enum_elem,
_highest_enumsortorder + 1
);
END IF;
ELSE
-- check if elem already exists in enum
IF NOT EXISTS (SELECT * FROM pg_enum WHERE enumlabel = enum_elem AND enumtypid = _enum_typid) THEN
INSERT INTO pg_enum(enumtypid, enumlabel) VALUES (
_enum_typid,
enum_elem
);
END IF;
END IF;
END;
$body$
LANGUAGE plpgsql;
--
-- Definition for function enum_del:
--
CREATE OR REPLACE FUNCTION enum_del (enum_name character varying, enum_elem character varying) RETURNS void
AS
$body$
DECLARE
type_oid INTEGER;
rec RECORD;
sql VARCHAR;
ret INTEGER;
BEGIN
SELECT pg_type.oid
FROM pg_type
WHERE typtype = 'e' AND typname = enum_name
INTO type_oid;
-- check if enum exists
IF NOT EXISTS (SELECT * FROM pg_enum WHERE enumtypid = type_oid) THEN
RETURN;
END IF;
-- check if element in enum exists
IF NOT FOUND THEN
RAISE EXCEPTION 'Cannot find a enum: %', enum_name;
END IF;
-- Check column DEFAULT value references.
SELECT *
FROM
pg_attrdef
JOIN pg_attribute ON attnum = adnum AND atttypid = type_oid
JOIN pg_class ON pg_class.oid = attrelid
JOIN pg_namespace ON pg_namespace.oid = relnamespace
WHERE
adsrc = quote_literal(enum_elem) || '::' || quote_ident(enum_name)
LIMIT 1
INTO rec;
IF FOUND THEN
RAISE EXCEPTION
'Cannot delete the ENUM element %.%: column %.%.% has DEFAULT value of ''%''',
quote_ident(enum_name), quote_ident(enum_elem),
quote_ident(rec.nspname), quote_ident(rec.relname),
rec.attname, quote_ident(enum_elem);
END IF;
-- Check data references.
FOR rec IN
SELECT *
FROM
pg_attribute
JOIN pg_class ON pg_class.oid = attrelid
JOIN pg_namespace ON pg_namespace.oid = relnamespace
WHERE
atttypid = type_oid
AND relkind = 'r'
LOOP
sql :=
'SELECT 1 FROM ONLY '
|| quote_ident(rec.nspname) || '.'
|| quote_ident(rec.relname) || ' '
|| ' WHERE '
|| quote_ident(rec.attname) || ' = '
|| quote_literal(enum_elem)
|| ' LIMIT 1';
EXECUTE sql INTO ret;
IF ret IS NOT NULL THEN
RAISE EXCEPTION
'Cannot delete the ENUM element %.%: column %.%.% contains references',
quote_ident(enum_name), quote_ident(enum_elem),
quote_ident(rec.nspname), quote_ident(rec.relname),
rec.attname;
END IF;
END LOOP;
-- OK. We may delete.
DELETE FROM pg_enum WHERE enumtypid = type_oid AND enumlabel = enum_elem;
END;
$body$
LANGUAGE plpgsql;-- Bernhard Schrader System Administration InnoGames GmbH Harburger Schloßstraße 28 (Channel 4) - 21079 Hamburg - Germany Tel +49 40 7889335-53 Fax +49 40 7889335-22 Managing Directors: Hendrik Klindworth, Eike Klindworth, Michael Zillmer VAT-ID: DE264068907 Amtsgericht Hamburg, HRB 108973 http://www.innogames.com – bernhard.schrader@innogames.de
Re: [ADMIN] Problems with enums after pg_upgrade
От:
Andres Freund <andres@2ndquadrant.com>
Дата:
On 2012-12-19 16:51:32 +0100, Bernhard Schrader wrote: > Hello again, > > well, still everything is working. > > What information do you need to get into this issue? > > Well, so far i can say, we dont use ALTER TYPE ADD VALUE. We use some more > or less changed enum_add and enum_del (Which are appended at the end) to be > able to change enums within transactions. That explains everything. You *CANNOT* do that. There are some pretty fundamental reasons why you are not allowed to add enums in a transaction. And even more reasons why deleting from enums isn't allowed at all. Greetings, Andres Freund --Andres Freund http://www.2ndQuadrant.com/PostgreSQL Development, 24x7 Support, Training & Services
Re: [ADMIN] Problems with enums after pg_upgrade
От:
Andrew Dunstan <andrew@dunslane.net>
Дата:
On 12/19/2012 10:56 AM, Andres Freund wrote: > On 2012-12-19 16:51:32 +0100, Bernhard Schrader wrote: >> Hello again, >> >> well, still everything is working. >> >> What information do you need to get into this issue? >> >> Well, so far i can say, we dont use ALTER TYPE ADD VALUE. We use some more >> or less changed enum_add and enum_del (Which are appended at the end) to be >> able to change enums within transactions. > That explains everything. You *CANNOT* do that. There are some pretty > fundamental reasons why you are not allowed to add enums in a > transaction. And even more reasons why deleting from enums isn't allowed > at all. > Yes, this is exactly what I referred to in my recent reply to Tom. This is a recipe for database corruption. Hacking the catalog generally is something to be done only with the most extreme caution, IMNSHO. cheers andrew
Re: [ADMIN] Problems with enums after pg_upgrade
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Andres Freund writes: > On 2012-12-19 16:51:32 +0100, Bernhard Schrader wrote: >> Well, so far i can say, we dont use ALTER TYPE ADD VALUE. We use some more >> or less changed enum_add and enum_del (Which are appended at the end) to be >> able to change enums within transactions. > That explains everything. You *CANNOT* do that. Yeah. So this was not pg_upgrade's fault at all: that code would have created problems in 9.1 or later even without using pg_upgrade. For the record, the reason you can't safely do this is exactly what we saw here: it's possible for deleted/never-committed values of the type to remain behind in upper levels of btree indexes. Since we now need to be able to consult pg_enum to know how to compare values of an enum type, deleted values are uncomparable. enum_add is all right as long as you are careful to commit its transaction before inserting the new value anywhere. enum_del is quite unsafe unless you REINDEX all indexes on columns of the type after making sure the value is gone from the tables. regards, tom lane
Re: [ADMIN] Problems with enums after pg_upgrade
От:
Andrew Dunstan <andrew@dunslane.net>
Дата:
On 12/19/2012 11:31 AM, Tom Lane wrote: > enum_add is all right as long as you are careful to commit its > transaction before inserting the new value anywhere. > > It's not really all right for post-9.0 versions. For example, this is wrong: > --postgres 9.2 or higher > IF version_int > 90200 THEN It should really be "IF version_int >= 90100 THEN" what is even worse is that this procedure doesn't take any care at all of the ordering rule for even numbered enum oids. We could have oid wraparound to an even numbered oid and it would break the rule. cheers andrew
Re: [ADMIN] Problems with enums after pg_upgrade
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Andrew Dunstan writes: > what is even worse is that this procedure doesn't take any care at all > of the ordering rule for even numbered enum oids. Good point. You really should use ALTER TYPE ADD VALUE, on versions where that's available. regards, tom lane