-- This function takes a table and column and will set the column
-- to allow NULLs.
--
-- $Id$
--
DROP FUNCTION kl_setnull(name, name);
CREATE FUNCTION kl_setnull(name, name) RETURNS boolean AS '
DECLARE
	tablename ALIAS FOR $1;
	colname ALIAS FOR $2;
	rec_affected int;
BEGIN
	-- If any params are NULL, return NULL - this means function
	-- can be defined isstrict.
	IF tablename IS NULL OR colname IS NULL THEN 
		RETURN NULL;
	END IF;

	-- Lock table with standard ALTER TABLE locks
	EXECUTE ''LOCK TABLE '' || quote_ident(tablename) || '' IN ACCESS EXCLUSIVE MODE'';

	-- Update the system catalogs
	EXECUTE ''UPDATE pg_attribute SET attnotnull = false WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '' || quote_literal(tablename) || '') AND attname = '' || quote_literal(colname);

	-- Get number of rows modified
	GET DIAGNOSTICS rec_affected = ROW_COUNT;

	-- Return number of rows modified
	RETURN (rec_affected = 1);
	
END;
' LANGUAGE 'plpgsql'
WITH (isstrict);
