Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mS0do-000088-1O for pgsql-sql@arkaria.postgresql.org; Sun, 19 Sep 2021 17:30:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mS0dl-0005ZV-8V for pgsql-sql@arkaria.postgresql.org; Sun, 19 Sep 2021 17:30:09 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mS0dk-0005UM-UH for pgsql-sql@lists.postgresql.org; Sun, 19 Sep 2021 17:30:09 +0000 Received: from mail-ed1-x52e.google.com ([2a00:1450:4864:20::52e]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mS0dh-00070q-JH for pgsql-sql@lists.postgresql.org; Sun, 19 Sep 2021 17:30:07 +0000 Received: by mail-ed1-x52e.google.com with SMTP id c22so51370777edn.12 for ; Sun, 19 Sep 2021 10:30:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=reply-to:to:from:subject:message-id:disposition-notification-to :date:user-agent:mime-version:content-transfer-encoding :content-language; bh=KPjmt7nqp1HeNSy8BGf5yHzB+rZ6+rVACxXzK8sOP1c=; b=KPkTHv+nSvOt53q8Zrftx7q+JKtDhAowOnF+35NYMbQsvyHKoGi3DWh+0auQ5gdn0p cLGTlYAI0/dbbXx0eabVIPlgF3EVLFAnxoHnVeanUPEfPChnYUsbgZ4/F/PhQY2XTNqy DbBJUQvpVLMqQR48kNZR8AnjFU8PkMnoJGDE6EdDyExhyiIXWKfCrh2ukfbXkakTGy3T wEgikhzGNcDJz/ztkm9kTPMs525Q+1hRs/pXG7NWlwMRQ5qc2PHxgRCm8BMSs0SbfEE0 wtUYnktGtZPtQEr/Kmy3iuJJTuGxzoskK0hv6EYH0OEUYtR0gfwoYdXCDxZ7vYGqf2Ub Bc2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:reply-to:to:from:subject:message-id :disposition-notification-to:date:user-agent:mime-version :content-transfer-encoding:content-language; bh=KPjmt7nqp1HeNSy8BGf5yHzB+rZ6+rVACxXzK8sOP1c=; b=vc3friKlvQtjNR2boliuTV7dLQQ9n12ahscWx92biShHQSH5RWOHlIzil53XYSM+xK mpRhSV9FGIwswGE3Pl0ljCfkA0GpimvK40PNGiGqBNqBIygZtPMACduowbwssYxOhzlE BV9khh3wqw1gjuD9HuNUCj+uDVIH3SuDh3pQBsEwsGB/zG51QoYzHAHprtFyBlX150Ee 0h4pXoHM8NvfNyxHXryphQcMMjsmkY7K+MNleg1jC3/36/qlvhaT4bskMAlMw1YLZdhN 8R7o7CS9Xq+/g/qLMjrGnImR1S9Xmk7MEr9yHjtUitRA5pskTH1M1SfYTjtDk3+Km0Rt jlGQ== X-Gm-Message-State: AOAM5309S7v6zNWHEskDZM92bzBW/avrdmevvi76zfT9hQCJbMvlc63I pToG1ErARmSrMyk1G60Xm2W5SOqPkGw= X-Google-Smtp-Source: ABdhPJwUsBTeorw45JNLIWkafR5OF2HcHvMGE9zrUvymmOW+Rki3yvCocQtEDypJlKVYUpARV4QEZA== X-Received: by 2002:a05:6402:31eb:: with SMTP id dy11mr25586312edb.17.1632072604053; Sun, 19 Sep 2021 10:30:04 -0700 (PDT) Received: from [10.0.0.4] ([41.77.19.27]) by smtp.gmail.com with ESMTPSA id o23sm5884440eds.75.2021.09.19.10.30.02 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 19 Sep 2021 10:30:03 -0700 (PDT) Reply-To: intmail01@gmail.com To: pgsql-sql@lists.postgresql.org From: "intmail01@gmail.com" Subject: How to sort deleted rows with trigger. Some rows before then some rows after. Message-ID: <0c7ae95d-6c0f-88ac-1e00-a689531e34b3@gmail.com> Disposition-Notification-To: "intmail01@gmail.com" Date: Sun, 19 Sep 2021 17:31:04 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Content-Language: fr List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, Deleting some rows in my table require some rules. Some kind of row must be deleted before others if not error occurs. It is a stock management. Calculated the remains stock must be always positive never negative. If I delete all rows that is marked as an positive input quantity then the stock will be negative. Triggers calculate the remaining stock each time one row is deleted. It uses "FOR EACH ROW" option. If someone have to delete with GUi many rows and want to avoid error, he will be forced to delete negative before then positive after. It is a wast of time because when the number of rows grows the chance to redo the task many times due to errors. Below is an example. If user select all rows then delete them, an error happen. After deleting the input quantity of 20, the first row will be with a stock of -5. TABLE: t_stock Date:         Qty:     Stock: 2021/09/19    20    20 2021/09/20    -5    15 2021/09/21    10    25 2021/09/22    -8    17 I try to use two triggers but it does not work, the deletion start always with the positive quantity 20 not a negative one: CREATE TRIGGER delete_1 BEFORE DELETE ON t_stock FOR EACH ROW WHEN (old.qty<0) EXECUTE FUNCTION mainfunction(); CREATE TRIGGER delete_2 BEFORE DELETE ON t_stock FOR EACH ROW WHEN (old.qty>0) EXECUTE FUNCTION mainfunction(); If a use "FOR EACH STATEMENT" with the Transition Tables which can help to list all rows to be deleted but it is only available with "AFTER" operation. Question: How to set the trigger to delete some rows before and some other after ? Thank you