i'm trying to fix database issue related to creating missing sequence of the table after moving database from lower to higher version but i face 2 issues
first here is what i tried so far:
DO $$
DECLARE
i TEXT;
BEGIN
FOR i IN (select table_name from information_schema.tables where table_catalog='cst_sh' and table_schema='public') LOOP
IF EXISTS (SELECT count(*) FROM information_schema.columns WHERE table_name=table_name and column_name='id') THEN
EXECUTE 'CREATE SEQUENCE IF NOT EXISTS '''||i||'_id_seq''';
EXECUTE 'Select setval('''||i||'_id_seq'', (SELECT max(id) as a FROM ' || i ||')+1,true);';
end if;
END LOOP;
END$$;
1st problem is that the condition doesn't seem to work. First i check if the table has column called id then i start to create the sequence if exist and then set the value for it but some tables doesn't have id column so the second query fail.
2nd problem is with the 1st query that i use to create the sequence if it doesn't exist, it fails every time and i dont know why
the error is :
QUERY: CREATE SEQUENCE IF NOT EXISTS 'xxxx'
CONTEXT: PL/pgSQL function inline_code_block line 7 at EXECUTE