使用 $$ 时 creat 出现 Postgres 语法错误

Postgres syntax error with creat when use $$

我要执行下一个脚本迁移。 但我发现语法错误。 我该如何解决?

脚本示例:

CREATE OR REPLACE FUNCTION __execute(TEXT) RETURNS VOID AS $$
BEGIN EXECUTE ; END;
$$ LANGUAGE plpgsql STRICT;

CREATE OR REPLACE FUNCTION __index_exists(TEXT, TEXT, TEXT) RETURNS bool as $$
SELECT exists(SELECT 1 FROM pg_indexes WHERE (schemaname, tablename, indexname) = (, , ));
$$ language sql STRICT;

DO $$ BEGIN IF __my_function('true') THEN
SELECT __execute($$
    CREATE INDEX "test_idx"
    ON test_table
    USING btree (column_name);
    $$)
WHERE NOT __index_exists('public', 'test_table', 'test_idx');
END IF; END; $$;

错误:

SQL State  : 42601
Error Code : 0
Message    : ERROR: syntax error at or near "CREATE"

我认为 postgresql 对函数内部的美元引用感到困惑,如果你给它一个标签名称可能会解决这个问题

DO $$ BEGIN IF __my_function('true') THEN
perform __execute($param$ CREATE INDEX "test_idx"
    ON test_table
    USING btree (column_name);$param$)
WHERE NOT __index_exists('public', 'test_table', 'test_idx');
END IF; END; $$;