do $$
declare
_r record;
begin
raise notice '%', 'lpu';
delete from fed.passporttoken where lpu_id = 2873;
for _r in (select
			'update '||n.nspname||'.'||c.relname||' set '||ca.attname||' = 5584192 where '||ca.attname||' = 2873;' _sql
			, c.relname, ca.attname fk_attname, n.nspname
			from pg_class c, pg_constraint cs, pg_class p, pg_attribute ca, pg_catalog.pg_namespace n
			where ca.attrelid = c.oid
			and ca.attnum = cs.conkey[1]
			and cs.contype = 'f'
			and cs.conrelid = c.oid
			and cs.confrelid = p.oid
			and p.relname = lower('lpu')
			and n.oid = c.relnamespace
and 'update '||n.nspname||'.'||c.relname||' set '||ca.attname||' = 5584192 where '||ca.attname||' = 2873;' <> 'update dbo.lpu set lpu_id = 5584192 where lpu_id = 2873;'
			--limit 1
			)
loop
raise notice '%', _r._sql;
execute _r._sql;
end loop;
delete from lpu where lpu_id = 2873;
raise notice '%', 'org';
for _r in (select
			'update '||n.nspname||'.'||c.relname||' set '||ca.attname||' = 5584192 where '||ca.attname||' = 2873;' _sql
			, c.relname, ca.attname fk_attname, n.nspname
			from pg_class c, pg_constraint cs, pg_class p, pg_attribute ca, pg_catalog.pg_namespace n
			where ca.attrelid = c.oid
			and ca.attnum = cs.conkey[1]
			and cs.contype = 'f'
			and cs.conrelid = c.oid
			and cs.confrelid = p.oid
			and p.relname = lower('org')
			and n.oid = c.relnamespace
			--limit 1
			)
loop
raise notice '%', _r._sql;
execute _r._sql;
end loop;

delete from org where org_id = 2873;

end $$;

