Postgresql 如何将regclass转换为不带引号的字符串?

Postgresql 如何将regclass转换为不带引号的字符串?,postgresql,formatting,Postgresql,Formatting,我有一个存储过程: create or replace function make_bi_temporal( _tbl regclass ) returns void as $$ BEGIN execute format( 'alter table %s drop constraint if exists %s_pkey', _tbl, _tbl ); ... 但是当运行选择make\u bi\u temporal('check') 我试图将%s更改为%I: ERROR: syntax er

我有一个存储过程:

create or replace function make_bi_temporal( _tbl regclass )
returns void as $$
BEGIN
execute format( 'alter table %s drop constraint if exists %s_pkey', _tbl, _tbl );
...
但是当运行
选择make\u bi\u temporal('check')

我试图将
%s
更改为
%I

ERROR:  syntax error at or near "_pkey"
LINE 1: ...ter table "check" drop constraint if exists """check"""_pkey
%L

ERROR:  syntax error at or near "'"check"'"
LINE 1: alter table "check" drop constraint if exists '"check"'_pkey
我想获取
检查\u pkey


如何使用不带引号的格式?

似乎可以通过正则表达式找到解决方案:

regexp_replace( _tbl::text, '"', '', 'g' )


execute format( 'alter table %s drop constraint if exists %s_pkey', _tbl, regexp_replace( _tbl::text, '"', '', 'g' ) );

我将此用作解决方法,因为这似乎不是正确的方法。您的第一个错误是创建了一个名为check的表,这是一个错误

你或其他人引用它一定绕过了这一点

create table "check"( id int primary key);
因此,它将创建一个名为“check_pkey”的主键约束

您从
regclass
中剥离引号的方法听起来不是一个合适的解决方案,而是一种变通方法

由于您需要
regclass
的文本表示,因此只需查询
pg_class

create or replace function make_bi_temporal( _tbl regclass )
returns void as $$
declare
 l_relname pg_class.relname%type;
BEGIN
   select relname||'_pkey' into l_relname from pg_class where oid = $1;
   execute format( 'alter table %s drop constraint if exists %s',$1,l_relname);
END $$ language plpgsql;   

尝试
%s
\u tbl::text
@a_horse\u with_no_name:任何将regclass转换为text的操作都会给我引号=(
选择cast('check'::regclass作为text)
| | | | | | | | | | | | | | | | | | | | | |
等。你到底是如何调用函数的?在我看来还行:@a_horse|u|没有名字的124
create table "check"( id int primary key);
create or replace function make_bi_temporal( _tbl regclass )
returns void as $$
declare
 l_relname pg_class.relname%type;
BEGIN
   select relname||'_pkey' into l_relname from pg_class where oid = $1;
   execute format( 'alter table %s drop constraint if exists %s',$1,l_relname);
END $$ language plpgsql;