I've written a stored procedure that performs an insert, and then another insert. See below. The foreign key constraint of the DeclarationBodies table always complains, meaning the declaration_id is not present in the Declarations table... But it should be, given that the select query finds it...

Any Ideas?

Code:
CREATE TABLE Declarations (
  "declaration_id" SERIAL NOT NULL, 
  "name" VARCHAR(100) NOT NULL, 
  "theory_id" INTEGER NOT NULL,
  "version" VARCHAR(50) DEFAULT (''),
  "pvs_version" VARCHAR(50) DEFAULT (''), 
  CONSTRAINT pk_Declarations_declaration_id
  	PRIMARY KEY("declaration_id"), 
  CONSTRAINT fk_Declarations_theory_id
  	FOREIGN KEY ("theory_id") REFERENCES Theories("theory_id")
		ON DELETE CASCADE ON UPDATE CASCADE NOT DEFERRABLE
);

CREATE TABLE DeclarationBodies (
  "declaration_id" INTEGER NOT NULL, 
  "body" TEXT, 
  CONSTRAINT fk_Declaration_Bodies_declaration_id
  	FOREIGN KEY ("declaration_id") REFERENCES Declarations("declaration_id")
		ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE
);

CREATE TABLE Functions(
  "signature" VARCHAR(100),
  CONSTRAINT pk_Functions_declaration_id
  	PRIMARY KEY("declaration_id"), 
  CONSTRAINT fk_Functions_theory_id
  	FOREIGN KEY ("theory_id") REFERENCES Theories("theory_id")
		ON DELETE CASCADE ON UPDATE CASCADE NOT DEFERRABLE
) INHERITS(Declarations);

CREATE OR REPLACE FUNCTION fn_insert_function(integer,varchar,integer,varchar,text) RETURNS integer AS'
DECLARE
  in_received_id ALIAS FOR $1;
  in_name ALIAS FOR $2;
  in_theory_id ALIAS FOR $3;
  in_signature ALIAS FOR $4;
  in_body ALIAS FOR $5;
  res_oid INTEGER;
  d_id INTEGER;
--  r_row record;  
BEGIN
--  SELECT INTO r_row R.version, R.pvs_version
--    FROM Received R
--    WHERE R.received_id = in_received_id;
  
--  IF NOT FOUND THEN
--    RAISE EXCEPTION ''Received id [%] not found!  Aborting insert of [%].'',in_received_id,in_name;
--    RETURN -1;  
--  END IF;

  INSERT INTO Functions (name, theory_id, signature)
    VALUES (in_name, in_theory_id, in_signature);
  
  GET DIAGNOSTICS res_oid = RESULT_OID;
  
  SELECT INTO d_id F.declaration_id 
    FROM Functions F
    WHERE F.oid = res_oid;
    
  RAISE NOTICE ''Declaration ID: %'',d_id;
  
  INSERT INTO DeclarationBodies (declaration_id, body) VALUES (d_id, in_body);

  RETURN d_id;
END;
'LANGUAGE 'plpgsql' STABLE RETURNS NULL ON NULL INPUT SECURITY INVOKER;
Thanks.