数据库升级,给某张表增加字段,防止重复升级时sql脚本报错

create or replace function func_add_column() returns int as
$body$
begin
perform * from pg_tables where tablename = ‘表名‘;
if found then
perform * from pg_attribute where attrelid = ‘表名‘::regclass and attname = ‘字段名‘;
if not found then
alter table 表名 add column 字段名 ;
end if;

end if;

return 0 ;
end;
$body$ language plpgsql;
select func_add_column();

相关文章