関数特性
言語: PLPGSQL
戻り値: integer
alterTableForReplication(tab_id) 複製のためテーブルを設定します。これはオリジンノード上で、テーブルのトリガー、 "logTrigger()" を追加するのに係わります。 購読ノード上で、これはトリガーとルールを無効にし、複製されたテーブルに対する書き込みアクセスを拒否するトリガーを追加します。declare
p_tab_id alias for $1;
v_no_id int4;
v_tab_row record;
v_tab_fqname text;
v_tab_attkind text;
v_n int4;
begin
-- ----
-- 中枢構成にロックを取得
-- ----
lock table sl_config_lock;
-- ----
-- 自らのローカルノード識別子の取得
-- ----
v_no_id := getLocalNodeId('_schemadoc');
-- ----
-- sl_table 行とテーブルの現在のオリジンノードの取得
-- テーブルが現在変更された状態でない事の検証
-- ----
select T.tab_reloid, T.tab_set, T.tab_idxname, T.tab_altered,
S.set_origin, PGX.indexrelid,
slon_quote_brute(PGN.nspname) || '.' ||
slon_quote_brute(PGC.relname) as tab_fqname
into v_tab_row
from sl_table T, sl_set S,
"pg_catalog".pg_class PGC, "pg_catalog".pg_namespace PGN,
"pg_catalog".pg_index PGX, "pg_catalog".pg_class PGXC
where T.tab_id = p_tab_id
and T.tab_set = S.set_id
and T.tab_reloid = PGC.oid
and PGC.relnamespace = PGN.oid
and PGX.indrelid = T.tab_reloid
and PGX.indexrelid = PGXC.oid
and PGXC.relname = T.tab_idxname
for update;
if not found then
raise exception 'Slony-I: Table with id % not found', p_tab_id;
end if;
v_tab_fqname = v_tab_row.tab_fqname;
if v_tab_row.tab_altered then
raise exception 'Slony-I: Table % is already in altered state',
v_tab_fqname;
end if;
v_tab_attkind := determineAttKindUnique(v_tab_row.tab_fqname,
v_tab_row.tab_idxname);
execute 'lock table ' || v_tab_fqname || ' in access exclusive mode';
-- ----
-- オリジンノードと購読ノードでは手順が異なる
-- ----
if v_no_id = v_tab_row.set_origin then
-- ----
-- オリジンノード上でテーブルにログトリガーを追加し、完了
-- ----
execute 'create trigger "_schemadoc_logtrigger_' ||
p_tab_id || '" after insert or update or delete on ' ||
v_tab_fqname || ' for each row execute procedure
logTrigger (''_schemadoc'', ''' ||
p_tab_id || ''', ''' ||
v_tab_attkind || ''');';
else
-- ----
-- 購読ノード上で物事はちょっとだけ扱いにくくなります。
-- 全てのユーザに対し外部キートリガーとルールを無効にします。
-- ----
-- ----
-- 全ての既存のトリガーを無効にします。
-- ----
update "pg_catalog".pg_trigger
set tgrelid = v_tab_row.indexrelid
where tgrelid = v_tab_row.tab_reloid
and not exists (
select true from sl_table TAB,
sl_trigger TRIG
where TAB.tab_reloid = tgrelid
and TAB.tab_id = TRIG.trig_tabid
and TRIG.trig_tgname = tgname
);
get diagnostics v_n = row_count;
if v_n > 0 then
update "pg_catalog".pg_class
set reltriggers = reltriggers - v_n
where oid = v_tab_row.tab_reloid;
end if;
-- ----
-- 全ての既存のルールを無効にします。
-- ----
update "pg_catalog".pg_rewrite
set ev_class = v_tab_row.indexrelid
where ev_class = v_tab_row.tab_reloid;
get diagnostics v_n = row_count;
if v_n > 0 then
update "pg_catalog".pg_class
set relhasrules = false
where oid = v_tab_row.tab_reloid;
end if;
-- ----
-- 複製されたテーブルへの書き込みアクセスを拒否するトリガーを追加します。
-- ----
execute 'create trigger "_schemadoc_denyaccess_' ||
p_tab_id || '" before insert or update or delete on ' ||
v_tab_fqname || ' for each row execute procedure
denyAccess (''_schemadoc'');';
end if;
-- ----
-- この構成で変更されたテーブルに印を付けます。
-- ----
update sl_table
set tab_altered = true where tab_id = p_tab_id;
return p_tab_id;
end;