tableaddkey( text )

1.126. tableaddkey( text )

Function Properties

Language: PLPGSQL

Return Type: text

tableAddKey (tab_fqname) - if the table has not got a column of the form _Slony-I_<clustername>_rowID, then add it as a bigint, defaulted to nextval() for a sequence created for the cluster.

	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: tableAddKey(): table % not found', v_tab_fqname_quoted;
	end if;

	-- If it does not have the special serial column, we
	-- have to add it. This will be only half way done.
	-- The function to add the table to the set must finish
	-- these definitions with NOT NULL and UNIQUE after
	-- updating all existing rows.
	if not v_have_serial then
		execute 'lock table ' || v_tab_fqname_quoted ||
			' in access exclusive mode';
		execute 'alter table only ' || v_tab_fqname_quoted ||
			' add column "_Slony-I_schemadoc_rowID" bigint;';
		execute 'alter table only ' || v_tab_fqname_quoted ||
			' alter column "_Slony-I_schemadoc_rowID" ' ||
			' set default "pg_catalog".nextval(''sl_rowid_seq'');';

		v_attkind := v_attkind || 'k';
	end if;

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