determineattkindserial( text )

1.43. determineattkindserial( text )

Function Properties

Language: PLPGSQL

Return Type: text

determineAttKindSerial (tab_fqname) A table was that was specified without a primary key is added to the replication. Assume that tableAddKey() was called before and finish the creation of the serial column. The return an attkind according to that.

	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';
	v_tab_fqname_quoted := slon_quote_input(p_tab_fqname);
	-- Loop over the attributes of this relation
	-- and add a "v" for every user column, and a "k"
	-- if we find the Slony-I special serial column.
	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
		if v_attrow.attname = '_Slony-I_schemadoc_rowID' then
		    v_attkind := v_attkind || 'k';
			v_have_serial := 't';
			v_attkind := v_attkind || 'v';
		end if;
	end loop;
	-- A table must have at least one attribute, so not finding
	-- anything means the table does not exist.
	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',
	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;';

	-- Return the resulting Slony-I attkind
	return v_attkind;