【发布时间】:2013-12-22 22:55:14
【问题描述】:
我正在编写一个简单的 plpgsql 函数来测试我必须使用 information_schema.columns 表在各种表上动态运行指标的想法。该函数工作正常,但是当我使用 information_schema 表中的 info 生成要传递给我的函数的表名时,我在标题中收到错误消息:
ERROR: function cannot execute on segment because it accesses relation "my_table"
这是一个简单的函数(原理证明):
create or replace function count_rows(table_name text, column_name text)
returns bigint as $$
declare
n bigint;
BEGIN
execute 'select count(*) from (select ' || column_name || ' from ' || table_name || ') as t' into n;
return n;
END;
$$ language 'plpgsql';
这个查询(以及函数)工作正常:
select * from count_rows('my_table','my_column'); -- works correctly!
但是使用来自 information_schema.columns 表的输入的查询失败并出现上述错误:
select table_name, column_name, count_rows(table_name, column_name) as num_rows
from information_schema.columns where table_name = 'my_table'; -- doesnt work
这个错误信息是什么意思?为什么不能这样查询information_schema中列出的表?
【问题讨论】:
-
您使用的是哪个数据库版本?我对 8.2、9.2 和 9.3 进行了快速检查,但它们都没有给我那个错误(并且通过代码搜索没有找到那个错误字符串)。此外,您应该使用 quote_ident() 或 format() - 如果您的任何列名包含奇怪的字符,您可能会传递一个棘手的查询来执行。
-
语言名称是 SQL 标识符,不应放在单引号中。你应该改用
plpgsql。 -
我在 PostgreSQL 8.2.15 上。我不确定为什么这个错误在这些版本中不可重复。如果我将语言名称(plpgsql)的引号去掉,我会得到同样的错误。
标签: subquery plpgsql information-schema greenplum