2015-05-27 92 views
0

我有功能的功能,即对于某些表(约400的表)生成的uniq的ID,例如:替换用作默认表

CREATE TABLE my_table (
    id bigint DEFAULT get_unique_id(), 
    ... 
) 

我需要可选的属性添加到该方法。 现在我可以创建具有相同名称和可选attiribute

CREATE OR REPLACE FUNCTION get_unique_id(swithattribute integer DEFAULT (-1)) ... 

新的方法,但我想只有一个使用此名称的方法。

所以我需要更换原单的,但如果我尝试删除原来的功能get_unique_id(),Postgres的把我和错误:

ERROR: cannot drop function get_unique_id() because other objects depend on it 
DETAIL: default for table my_table column id depends on function get_unique_id() 
default for table my_table2 column id depends on function get_unique_id() 
... 

有没有简单的方法如何添加可选属性变成已有的功能?

回答

0

您可以运行:需要

begin; 
alter table my_table alter column id set default -1; 
create or replace function get_unique_id...; --insert new function definition here 
alter table my_table alter column id set default get_unique_id(); 
commit; 

,但可能表排它锁,因此它可以阻断了一段时间的查询。

如果有很多表,你可以使用查询:

select 'alter table '||quote_ident(table_schema)||'.'||quote_ident(table_name)||' alter column '||quote_ident(column_name)||' set default -1;' from information_schema.columns where column_default='get_unique_id()'; 

来产生所需alter语句。

+0

我在想这个解决方案,但是有大约400个表使用这个函数。 –

+0

@JakubMichalko,我添加了声明来生成需要的'ddl'。我想你正在寻找更优雅的东西,但我怀疑是否有其他解决方案。 –

0

我发明了一些黑客,但优雅和工作的解决方案。我创建所需要的功能为临时

CREATE OR REPLACE FUNCTION get_unique_tmp(arg int default 0) ... 

,然后我这个临时的功能

update pg_catalog.pg_proc 
set proargtypes=x.proargtypes, proargnames=x.proargnames, proargdefaults=x.proargdefaults,prosrc=x.prosrc,pronargs=x.pronargs,pronargdefaults=x.pronargdefaults 
from (select * from pg_catalog.pg_proc p where p.proname='get_unique_tmp') x 
where pg_catalog.pg_proc.proname='get_unique_id' 

改变了PG目录原来的(参数和SRC),但我不知道它的正确性。