2014-02-24 18 views
3

我使用postgres模式來分組視圖和功能,並保持它們在數據庫中的多個版本(有時需要向後兼容),因爲有不同模式中的同一個函數的多個版本,我不能簡單地引用它的名稱,但需要編寫完整的限定名稱「schema.funcname」來訪問它。postgres本地模式限定符

當從schemaA中的另一個函數引用schemaA中的函數時,我總是必須編寫schemaA.funcname。當我稍後重新命名模式時,這會讓我感到不舒服 - 是否有限定符表示同一模式中的函數可以使用?也許就像java中的「this」限定符一樣?

希望它可以理解什麼,我的意思

THX

回答

0

您可以使用set schema改變搜索路徑當前會話:

create schema test1; 
create schema test2; 

create function test1.f() returns int as $body$ begin return 1; end; $body$ language plpgsql; 
create function test2.f() returns int as $body$ begin return 2; end; $body$ language plpgsql; 

set schema 'test1'; 
select f(); 

set schema 'test2'; 
select f(); 

您還可以添加search_path到函數的定義:

create or replace function test1.x() returns int as $body$ begin return 1; end; $body$ language plpgsql; 
create or replace function test1.f() returns int as $body$ begin return x(); end; $body$ language plpgsql set search_path = 'test1'; 

create or replace function test2.x() returns int as $body$ begin return 2; end; $body$ language plpgsql; 
create or replace function test2.f() returns int as $body$ begin return x(); end; $body$ language plpgsql set search_path = 'test2'; 

select test1.f(); 

select test2.f(); 
+0

thx爲答案 - 我知道設置架構,但它不是我想要做的,因爲它會影響整個會話。從test1中的函數調用f將調用與test2中的函數調用f相同的函數。我想函數test1調用test1.f()和test2中的函數來調用test2.f() – user3347114

+0

您還可以將'search_path'添加到函數定義中,查看我的更新答案。這是你需要的嗎?如果不是,那我就沒有想法。 –

+0

在某種程度上 - 它不是我正在尋找的,但它可能是有用的,我會考慮它。無論如何感謝提示 – user3347114