X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (neptune.hub.org [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 2DC63D1B439 for ; Wed, 26 Nov 2003 19:52:02 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 01728-04 for ; Wed, 26 Nov 2003 15:51:38 -0400 (AST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by svr1.postgresql.org (Postfix) with ESMTP id 4FE62D1B455 for ; Wed, 26 Nov 2003 15:51:31 -0400 (AST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.12.9/8.12.9) with ESMTP id hAQJpZNu021254 for ; Wed, 26 Nov 2003 19:51:35 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.12.9/8.12.9/Submit) id hAQJRgJn016959 for pgsql-sql@postgresql.org; Wed, 26 Nov 2003 19:27:42 GMT From: "Mujdat Pakkan" X-Newsgroups: comp.databases.postgresql.sql Subject: rules and return values question Date: Wed, 26 Nov 2003 11:29:33 -0800 Organization: Hub.Org Networking Services Lines: 41 Message-ID: X-Complaints-To: usenet@news.hub.org X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200311/323 X-Sequence-Number: 16138 We have an interesting case where we want to use Postgres both as a database and a front end to a proprietary database. For the latter, we wrote functions that access the proprietary database. Then we defined views on the proprietary database and wrote rules for insert/update/delete on those views using the functions. The problem is that we cannot find a way to return reasonable error values from the access functions. The rules always return the result of the last query executed and 0 if the query is not of the same type as the original query in the rule. CREATE FUNCTION old_db_view_func(int, int, int) RETURNS int AS '...', 'old_db_view_func' LANGUAGE 'C' VOLATILE STRICT; // this accesses the proprietary database and retrieves all rows CREATE FUNCTION old_db_del_func(int) RETURNS int AS '...', 'old_db_del_func', LANGUAGE 'C' VOLATILE STRICT; // this accesses the proprietary database and deletes a row CREATE VIEW old_db_view AS SELECT t.a, t.b, t.c FROM old_db_view_func() AS t; CREATE TABLE old_db_log (op text, a int, b, int, c int, res int); CREATE RULE old_db_del AS ON DELETE TO old_db_view DO INSTEAD INSERT INTO old_db_log VALUES ('delete', OLD.a, OLD.b, OLD.c, old_db_del_func(OLD.a)); Now when you execute: DELETE FROM old_db_view WHERE a=1; you always get 0 as a result whether there is a row with a=1 or not, since the last query is an INSERT (into old_db_log) and is not of the same type as the original query (DELETE). And even if you do an INSERT INTO old_db_view VALUES (1,2,3); where there already exists a row with a =1 and the function returns an error, the rule returns 1. We do not really need the old_db_log but you cannot invoke functions directly from the rules. You need to give a query. We could have dummy tables but it introduces too much overhead. In any case, we need the return value (or an exception) from the function but there does not seem a way to return it. Anybody had a similar requirement before?