相关疑难解决方法(0)

表名作为PostgreSQL函数参数

我想在Postgres函数中传递一个表名作为参数.我试过这段代码:

CREATE OR REPLACE FUNCTION some_f(param character varying) RETURNS integer 
AS $$
    BEGIN
    IF EXISTS (select * from quote_ident($1) where quote_ident($1).id=1) THEN
     return 1;
    END IF;
    return 0;
    END;
$$ LANGUAGE plpgsql;

select some_f('table_name');
Run Code Online (Sandbox Code Playgroud)

我得到了这个:

ERROR:  syntax error at or near "."
LINE 4: ...elect * from quote_ident($1) where quote_ident($1).id=1)...
                                                             ^

********** Error **********

ERROR: syntax error at or near "."
Run Code Online (Sandbox Code Playgroud)

以下是更改为此时出现的错误select * from quote_ident($1) tab where tab.id=1:

ERROR:  column tab.id does not exist
LINE 1: ...T EXISTS …
Run Code Online (Sandbox Code Playgroud)

postgresql function dynamic-sql plpgsql identifier

69
推荐指数
4
解决办法
7万
查看次数

postgres_fdw:可以将数据推送到外部服务器进行加入吗?

假设我有一个类似的查询

select * from remote_table
   join local_table using(common_key)
Run Code Online (Sandbox Code Playgroud)

其中 remote_table 是一个FOREIGN TABLEwithpostgres_fdw并且local_table是一个普通表。

local_table小(100 行)和remote_table大(数百万行)。

看起来远程表被整体拉取并在本地加入,此时将较小的表发送到远程服务器并远程加入会更有效。

有没有办法让 postgres_fdw 做到这一点?

postgresql postgres-fdw postgresql-10

7
推荐指数
1
解决办法
447
查看次数

用于返回给定表的动态列集的函数

我有一个fields表来存储其他表的列信息:

CREATE TABLE public.fields (
   schema_name varchar(100), 
   table_name  varchar(100), 
   column_text varchar(100), 
   column_name varchar(100), 
   column_type varchar(100) default 'varchar(100)', 
   column_visible boolean
);
Run Code Online (Sandbox Code Playgroud)

我想创建一个函数来获取特定表的数据.刚试过这样的事:

create or replace function public.get_table(schema_name text,
                                            table_name text,
                                            active boolean default true)
  returns setof record as $$

declare 
    entity_name text default schema_name || '.' || table_name;
    r record;
begin
    for r in EXECUTE 'select * from ' || entity_name loop
        return next r;
    end loop;
    return;
end
$$
language plpgsql;
Run Code Online (Sandbox Code Playgroud)

使用此功能,我必须在调用它时指定列!

select * from public.get_table('public', 'users') as …
Run Code Online (Sandbox Code Playgroud)

postgresql function dynamic-sql plpgsql

4
推荐指数
1
解决办法
5700
查看次数