1.34. determineattkindserial( text )

関数特性

言語: PLPGSQL

戻り値: text

determineAttKindSerial (tab_fqname) レプリケーションに主キーを持たなく指定されたテーブルの追加。事前にt tableAddKey() が呼ばれ、シリアル列の作成が完了する事を仮定します。それに基づいて attkind を戻します。

declare
	p_tab_fqname            alias for $1;
	v_tab_fqname_quoted	text default '';
	v_attkind		text default '';
	v_attrow		record;
	v_have_serial		bool default 'f';
begin
	v_tab_fqname_quoted := slon_quote_input(p_tab_fqname);
	--
	-- もし Slony-I の特別なシリアル列が発見されれば、
	-- このリレーションの属性上でループし、そして
	-- 全てのユーザ列に "v" と "k" を追加します。

	--
	for v_attrow in select PGA.attnum, PGA.attname
			from "pg_catalog".pg_class PGC,
			    "pg_catalog".pg_namespace PGN,
				"pg_catalog".pg_attribute PGA
			where slon_quote_brute(PGN.nspname) || '.' ||
			    slon_quote_brute(PGC.relname) = v_tab_fqname_quoted
				and PGN.oid = PGC.relnamespace
				and PGA.attrelid = PGC.oid
				and not PGA.attisdropped
				and PGA.attnum > 0
			order by attnum
	loop
		if v_attrow.attname = '_Slony-I_schemadoc_rowID' then
		    v_attkind := v_attkind || 'k';
			v_have_serial := 't';
		else
			v_attkind := v_attkind || 'v';
		end if;
	end loop;
	
	--
	-- テーブルには少なくとも 1 つの属性が必要で、
	-- 属性が見つからない場合そのテーブルは存在しないことになります。
	--
	if not found then
		raise exception 'Slony-I: table % not found', v_tab_fqname_quoted;
	end if;

	--
	-- If it does not have the special serial column, we
	-- should not have been called in the first place.
	--
	if not v_have_serial then
		raise exception 'Slony-I: table % does not have the serial key',
				v_tab_fqname_quoted;
	end if;

	execute 'update ' || v_tab_fqname_quoted ||
		' set "_Slony-I_schemadoc_rowID" =' ||
		' "pg_catalog".nextval(''sl_rowid_seq'');';
	execute 'alter table only ' || v_tab_fqname_quoted ||
		' add unique ("_Slony-I_schemadoc_rowID");';
	execute 'alter table only ' || v_tab_fqname_quoted ||
		' alter column "_Slony-I_schemadoc_rowID" ' ||
		' set not null;';

	--
	-- 結果の Slony-I attkind を返します。
	--
	return v_attkind;
end;