1.37. determineidxnameunique( text, name )

関数特性

言語: PLPGSQL

戻り値: name

FUNCTION determineIdxnameUnique (tab_fqname, indexname) tablename と tab_fqname を仮定し、その一意インデックス、インデックス名、出口を検査するか、もしくはテーブルの主キーインデックス名を返します。一意のインデックスが無い場合、例外を生じさせます。

declare
	p_tab_fqname    	alias for $1;
	v_tab_fqname_quoted	text default '';
	p_idx_name		alias for $2;
	v_idxrow		record;
begin
	v_tab_fqname_quoted := slon_quote_input(p_tab_fqname);
	--
	-- テーブルが存在するかの確証
	--
	if (select PGC.relname
				from "pg_catalog".pg_class PGC,
					"pg_catalog".pg_namespace PGN
				where slon_quote_brute(PGN.nspname) || '.' ||
					slon_quote_brute(PGC.relname) = v_tab_fqname_quoted
					and PGN.oid = PGC.relnamespace) is null then
		raise exception 'Slony-I: table % not found', v_tab_fqname_quoted;
	end if;

	--
	-- テーブル主キーもしくは特定の一意インデックスの参照
	--
	if p_idx_name isnull then
		select PGXC.relname
				into v_idxrow
				from "pg_catalog".pg_class PGC,
					"pg_catalog".pg_namespace PGN,
					"pg_catalog".pg_index PGX,
					"pg_catalog".pg_class PGXC
				where slon_quote_brute(PGN.nspname) || '.' ||
					slon_quote_brute(PGC.relname) = v_tab_fqname_quoted
					and PGN.oid = PGC.relnamespace
					and PGX.indrelid = PGC.oid
					and PGX.indexrelid = PGXC.oid
					and PGX.indisprimary;
		if not found then
			raise exception 'Slony-I: table % has no primary key',
					v_tab_fqname_quoted;
		end if;
	else
		select PGXC.relname
				into v_idxrow
				from "pg_catalog".pg_class PGC,
					"pg_catalog".pg_namespace PGN,
					"pg_catalog".pg_index PGX,
					"pg_catalog".pg_class PGXC
				where slon_quote_brute(PGN.nspname) || '.' ||
					slon_quote_brute(PGC.relname) = v_tab_fqname_quoted
					and PGN.oid = PGC.relnamespace
					and PGX.indrelid = PGC.oid
					and PGX.indexrelid = PGXC.oid
					and PGX.indisunique
					and slon_quote_brute(PGXC.relname) = slon_quote_input(p_idx_name);
		if not found then
			raise exception 'Slony-I: table % has no unique index %',
					v_tab_fqname_quoted, p_idx_name;
		end if;
	end if;

	--
	-- 見つかったインデックス名を返します。
	--
	return v_idxrow.relname;
end;