Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fO1Hw-0003mN-Cg for pgsql-general@arkaria.postgresql.org; Wed, 30 May 2018 13:37:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fO1Hv-00068A-3F for pgsql-general@arkaria.postgresql.org; Wed, 30 May 2018 13:37:15 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fO1Hu-000680-O0 for pgsql-general@lists.postgresql.org; Wed, 30 May 2018 13:37:14 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fO1Hr-0004mg-UT for pgsql-general@postgresql.org; Wed, 30 May 2018 13:37:13 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 431A1214AC; Wed, 30 May 2018 09:37:11 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Wed, 30 May 2018 09:37:11 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=92wXhOfZ7R0kKqq0PvDTxBl8+VuaJ q5JcvR6CNrOILM=; b=T0cNZr62TgCPvUKunmDdtstu0sV/rgW/ZYSoCA05sRHwM 3wERlyOKOJYzyQNnJ7KZQvufQB7OBzdEvtW1Y6Gtw1gk5zWvIKF7DODMjd2VuC33 be4F+nLt51jXQOPn+5JrFcwxYHZWtf9z06jSwirXyBc9n6iIR6yfReKvA+4ZMyEB oOmXoXpYgAIcTcTzhCXun/WMjE8iZ/s0Ylo/GZwYTwWy6o1tFdhH1/YvhBh/NjiZ 6WFu8B7sdmKeyyBN1tCbpeMNgP1f+EwjzA5oLvsypXbf8lRw2KhXo8ejHekAapYG KkRhS06zYSWwOWUUk7nD/lFLKCkUQXnNnPXVfZnhA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=92wXhO fZ7R0kKqq0PvDTxBl8+VuaJq5JcvR6CNrOILM=; b=TecBJ7k46/Bvo05hgZglWo smbsVUNGp8dMJ64DHCZ18CcZrIWCEPV/OFz187ZoZmbXVuEKDAhlbRts1ipQkuUu kUzNREwBQAyVCTchkM8xTd4OAd/JPfJlxw4hQU6BsAM1rSjmbRbc1kNHjScxD9o2 5j7yz0ttARV1sT9GqGFpMUlkWwhKtgM7M2ySHwu+T6nMpn8SqRw4/GRejgzUlLu/ gVaNPKcekZQSoTDpE36ESFyiRt3ILKc+7QC/vBEJfzCXEEJ8EPkFcTRffnq8zEhE kbP2iYcin4XXz/OX85Bs1QS9EeyJD9HEoGiU4jVOyVz+hATlOqrnRuGbhwxmUAsw == X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Sender: Received: from [192.168.1.3] (unknown [71.212.159.209]) by mail.messagingengine.com (Postfix) with ESMTPA id 9C9731028C; Wed, 30 May 2018 09:37:10 -0400 (EDT) Subject: Re: Update rules on views To: =?UTF-8?B?TWFyb8WhIEtvbGzDoXI=?= , pgsql-general@postgresql.org References: From: Adrian Klaver Message-ID: <141d6b62-5b77-7ae9-831e-cd036b52cada@aklaver.com> Date: Wed, 30 May 2018 06:37:09 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 05/26/2018 11:32 AM, Maroš Kollár wrote: > Hello, > > I am currently evaluating multiple ways of denying certain updates on > record AND indicating whether an update was denied because it did not > match some criteria or if it simply was not found. > > One of these methods is using the rule system and behaves in an odd > way.The unexpected results are indicated below. > > -- Function for debugging > CREATE OR REPLACE FUNCTION public.test_debug( > message text > ) RETURNS text > LANGUAGE plpgsql > AS $function$ > BEGIN > RAISE NOTICE 'Calling test_debug: %', message; > RETURN message; > END; $function$; > > DROP TABLE test CASCADE ; > -- Main table > CREATE TABLE test(id int primary key, animal text NOT NULL, sound text > NOT NULL); > -- Populate table > INSERT into test VALUES (1,'rabbit','purr'),(2,'fox','shriek'); > -- Create simple view > CREATE VIEW test_view AS SELECT * FROM test; > -- Unconditional fallback update rule > CREATE OR REPLACE RULE test_fallback AS ON UPDATE TO test_view > DO INSTEAD NOTHING; > -- Rule to deny update if animal is the same > CREATE RULE test_deny AS ON UPDATE TO test_view > WHERE NEW.animal = OLD.animal > DO INSTEAD (SELECT test_debug('deny')); > -- Rule to allow update if animal was changed > CREATE OR REPLACE RULE test_allow AS ON UPDATE TO test_view > WHERE NEW.animal <> OLD.animal > DO INSTEAD ( > UPDATE test SET > animal = NEW.animal, > sound = NEW.sound > WHERE id = OLD.id; > SELECT test_debug('allow'); > ); > > -- Test an update that should be denied by the rule since animal is the same > UPDATE test_view SET animal = 'rabbit', sound = 'bark' WHERE id = 1; > -- NOTICE: Calling test_debug: deny > -- test_debug > -- -------- > -- deny > -- (1 row) > -- > -- UPDATE 0 > > -- Check if the record was not altered > SELECT * FROM test WHERE id = 1; > -- id | animal | sound > -- ----+--------+------- > -- 1 | rabbit | purr > -- (1 row) > > -- Test an update that should be accepted by the rule since animal is different > UPDATE test_view SET animal = 'bear',sound = 'roar' WHERE id = 2; > -- NOTICE: Calling test_debug: deny <--- expecting allow instead ! > -- test_debug > -- ------------ > -- deny <--- expecting allow instead ! > -- (1 row) > -- > -- UPDATE 1 > > -- Check if the record was altered > SELECT * FROM test WHERE id = 2; > -- id | animal | sound > -- ----+--------+------- > -- 2 | bear | roar <--- record was updated as expected, > although it returned 'deny' > -- (1 row) > > -- Test an update on a record that does not exist > UPDATE test_view SET animal = 'dog',sound = 'bark' WHERE id = 3; > -- test_debug > -- ------------ > -- (0 rows) > -- > -- UPDATE 0 > > Why do I see 'deny' on an update that was handled by the 'test_allow' rule? RULEs are weird? I have not used any in a long time. If you want to continue with this then: https://www.postgresql.org/docs/9.6/static/sql-createrule.html "If you want to handle all the useful cases in conditional rules, add an unconditional DO INSTEAD NOTHING rule to ensure that the system understands it will never be called on to update the dummy table. Then make the conditional rules non-INSTEAD; in the cases where they are applied, they add to the default INSTEAD NOTHING action. (This method does not currently work to support RETURNING queries, however.)" I would go with triggers though: https://www.postgresql.org/docs/9.6/static/sql-createtrigger.html " INSTEAD OF INSERT/UPDATE/DELETE Views — TRUNCATE — — " Their behavior is more predictable. > > All tests were run on postgres 9.6.7. > > Cheers > Maroš > > -- Adrian Klaver adrian.klaver@aklaver.com