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 1mS17L-0001H1-Jt for pgsql-sql@arkaria.postgresql.org; Sun, 19 Sep 2021 18:00:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mS17I-0000Nc-Sm for pgsql-sql@arkaria.postgresql.org; Sun, 19 Sep 2021 18:00:40 +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 1mS17I-0000NT-Ii for pgsql-sql@lists.postgresql.org; Sun, 19 Sep 2021 18:00:40 +0000 Received: from mail-io1-xd36.google.com ([2607:f8b0:4864:20::d36]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mS17F-0007Ei-Ef for pgsql-sql@lists.postgresql.org; Sun, 19 Sep 2021 18:00:39 +0000 Received: by mail-io1-xd36.google.com with SMTP id q3so19007229iot.3 for ; Sun, 19 Sep 2021 11:00:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:from:mime-version:subject:date:message-id :references:cc:in-reply-to:to; bh=DRRIq1auQRXLEvsQgjOWhoAsPe1EHwd8RiXroXpIRVQ=; b=CFNwImbGnS6PdkbcNXLWKV67cQWy0HPEowuc1/L8Mav2cRTso8W2OTkWsg2CV7et9L 7A6vMm+vcekm43cpETi5FV9IH0LxFsSP0O6i8C7vpjOGqzNs2hC5v/4qY7P+h3pmNEDb yYVHSdZIklGqyWG9bWnjGrtJ5oEdNro991cJ5TGxdbR4Jp4BypIfC0bWH4BIekZVhvrT BHNKtOwTeTvIEFdqKRbVHRP5pUihfw3h0ZveIEJVHhhovXSBrsqH+aCaJFqiSJ62Ep0u desgaV2gN2HkWpz7Zt7SHSjjdBYOxIJT/UyC8cEwJUyxbSEldZ+my1288ybn/uqBl7EZ lmHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:content-transfer-encoding:from:mime-version :subject:date:message-id:references:cc:in-reply-to:to; bh=DRRIq1auQRXLEvsQgjOWhoAsPe1EHwd8RiXroXpIRVQ=; b=TZrPuS6vYBu7jRYgPWLUIB7x1ZTyohDQlqsmaooqKchDbGWeBJ6ADfr473ddRjaiOh TNlgVUbQpykSP+cF7Ztg+rGPzQej8M+juT4hFdvxSFNoLLSXzvQBAZnfMPUTZNEJsREq 5qZyjFm/VEf+dyQ2Sz45TbkedHk+erKSfxzQdRwC/tuiw7JTf5khdhWrVw2OnpVYG6o4 S5cE4VmhcvF5e4IRsmBOj9HWuvLkXChLoFmU3wAOiTbjbOGxPaUA57+dhd/ILiNXMRVd JYKZhSCAD11hq88JKqzHZ5ZvkSBEmr/WS8P7uH4juO5Y0PHf/W+oteq1+e6z0egkqQ/B 32oQ== X-Gm-Message-State: AOAM530LYlpsItw5bD1u4aOnb1N7aW12RfKCbtlFGPef2oi5JND6KAib jM+VDqSNvK1lPJbWLAbxq94= X-Google-Smtp-Source: ABdhPJy35lOA5rP76k+qaSuKLw8VnUT1vxG6t/q0GVm0jbSQniSnXdvTKxCdO1FQVg8NWj+LG9rREQ== X-Received: by 2002:a05:6638:25cd:: with SMTP id u13mr16057645jat.114.1632074434706; Sun, 19 Sep 2021 11:00:34 -0700 (PDT) Received: from smtpclient.apple ([2601:681:5500:dde0:cdc2:4e1b:5fad:df46]) by smtp.gmail.com with ESMTPSA id m5sm7748337ila.10.2021.09.19.11.00.34 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 19 Sep 2021 11:00:34 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: Rob Sargent Mime-Version: 1.0 (1.0) Subject: Re: How to sort deleted rows with trigger. Some rows before then some rows after. Date: Sun, 19 Sep 2021 12:00:33 -0600 Message-Id: References: <0c7ae95d-6c0f-88ac-1e00-a689531e34b3@gmail.com> Cc: pgsql-sql@lists.postgresql.org In-Reply-To: <0c7ae95d-6c0f-88ac-1e00-a689531e34b3@gmail.com> To: intmail01@gmail.com X-Mailer: iPhone Mail (18G82) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Sep 19, 2021, at 11:30 AM, intmail01@gmail.com wrote: >=20 > =EF=BB=BFHi, >=20 > 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. >=20 > 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. >=20 > 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. >=20 > 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 >=20 > 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(); >=20 > 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. >=20 > Question: How to set the trigger to delete some rows before and some > other after=20 For each batch of deletes send two delete statements in a single transaction= . The first with negative values. The second with non-negative values.=20 > Thank you >=20 >=20 >=20