根据条件判断是否需要执行
DO
$do$
begin
if not exists
(SELECT indexname from PG_INDEXES where "schemaname"='public' and "table_name"='my_table' and indexname='my_index') then
create index my_index on public.my_table("my_column");
end if;
END;
$do$
;
-- $do$可以换成一个单引号,内容部分的单引号改成2个单引号进行转义
-- 查询表或字段
select * from information_schema.columns where table_schema='public' and table_name ='my_table' and column_name='my_column'