Switch query fixing aclitems in ~15 from O(N^2) to O(N) in upgrade_adapt.sql

f4f2f2b was doing a sequential scan of pg_class before checking if a
relation had attributes dependent on aclitem as data typewhen building
the set of ALTER TABLE queries, but it would be costly on a regression
database.

While on it, make the query style more consistent with the rest.

Reported-by: Justin Pryzby
Discussion: https://postgr.es/m/20221223032724.GQ1153@telsasoft.com
This commit is contained in:
Michael Paquier 2022-12-26 08:00:55 +09:00
parent 442e25d248
commit d3c0cc4447

View File

@ -95,25 +95,21 @@ DROP OPERATOR public.#@%# (pg_catalog.int8, NONE);
-- The internal format of "aclitem" has changed in 16, so replace it with -- The internal format of "aclitem" has changed in 16, so replace it with
-- text type in tables. -- text type in tables.
\if :oldpgversion_le15 \if :oldpgversion_le15
DO $$ DO $stmt$
DECLARE DECLARE
rec text; rec record;
col text;
BEGIN BEGIN
FOR rec in FOR rec in
SELECT oid::regclass::text SELECT oid::regclass::text as rel, attname as col
FROM pg_class FROM pg_class c, pg_attribute a
WHERE relname !~ '^pg_' WHERE c.relname !~ '^pg_'
AND relkind IN ('r') AND c.relkind IN ('r')
AND a.attrelid = c.oid
AND a.atttypid = 'aclitem'::regtype
ORDER BY 1 ORDER BY 1
LOOP LOOP
FOR col in SELECT attname FROM pg_attribute EXECUTE 'ALTER TABLE ' || quote_ident(rec.rel) || ' ALTER COLUMN ' ||
WHERE attrelid::regclass::text = rec quote_ident(rec.col) || ' SET DATA TYPE text';
AND atttypid = 'aclitem'::regtype
LOOP
EXECUTE 'ALTER TABLE ' || quote_ident(rec) || ' ALTER COLUMN ' ||
quote_ident(col) || ' SET DATA TYPE text';
END LOOP;
END LOOP; END LOOP;
END; $$; END; $stmt$;
\endif \endif