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 1jsomX-0007m9-T0 for pgsql-general@arkaria.postgresql.org; Tue, 07 Jul 2020 14:41:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jsomV-0002x0-Kc for pgsql-general@arkaria.postgresql.org; Tue, 07 Jul 2020 14:41:11 +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 1jsomV-0002wt-6n for pgsql-general@lists.postgresql.org; Tue, 07 Jul 2020 14:41:11 +0000 Received: from mail-wr1-x429.google.com ([2a00:1450:4864:20::429]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jsomR-0003sF-Qc for pgsql-general@lists.postgresql.org; Tue, 07 Jul 2020 14:41:10 +0000 Received: by mail-wr1-x429.google.com with SMTP id z15so34132777wrl.8 for ; Tue, 07 Jul 2020 07:41:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=4LQpCgkixk7Jk5Z72iBnh3+e0hens9b3kOedZg9T5xQ=; b=iIgMn/gKBrSozgIeEk/7AtrmJUfD2NBe/qrZxndcKanKyNWb5IR3+skrTegZOI8wu7 4gmPvvjzze1x9xQ1ZOk8pOYEzsMzGnlgFeIj9lVLmMJ4ngzAUhPCLwMqJOK7WZfjA5lJ IwZvJv8QjcCaml/mdKwIdD8X3TncMkNaJzvDhlXaYODYwnsotQ6wbF7QTGkwPTTl8m6U 0QG3sOOoYevS2JFsGJRxoXRW2RW9zGSmiopSUxkgeR/8qeLhdZe91p3TlYdISes2uq/t Ikeuifsp2bVEXosWvihEfjnuZFKq8rDPuOwqtF21qfjepbz4zd9aU2uZSTuMRV9fGzkt oqXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=4LQpCgkixk7Jk5Z72iBnh3+e0hens9b3kOedZg9T5xQ=; b=rgxKYTgoQrBVghFZAtHADZ8Nyfx+SoRa1wrxiigO5miMkI958qJFnMzitmoKCCZak0 nLwyeiZE5bL5G2vs+azGmhNiXAcRg1Pvqxcz0kuumZJI7ppBQIe9889dKAlKyiT9xYDU 8IFr0vJ53ChjWjDB002G3X9cplmzYWJPHGXa2cEgtsstoNxfh7XYXx8pFyL23xOiP+AH muN9GiDolkHhgCTGxy19pkljZkeN3cHb6ky7JMir/YmilWp7ykAgXTs3uWqspxHK+PJq Zu8psrnt4w3rL402iUn6BzRGsikRSvHoiSNkzbz5Uwiq+4R3lnn4aAQXr7slFXXN5sm7 hWnw== X-Gm-Message-State: AOAM532aod0+hzi3cLlVHNif0SFyhpePr+O+5qO73Lh9aM8OmiedHLpx XLnoBHfnd2oNuwL01qHL4AljMQ== X-Google-Smtp-Source: ABdhPJwKgjghka/T3HUcXzTgUtPAaPKnOI1RYdDv2HNabooISb5s76HD4WC9bx51xs22yhuFqF8Fpg== X-Received: by 2002:a5d:56c7:: with SMTP id m7mr53545463wrw.223.1594132866389; Tue, 07 Jul 2020 07:41:06 -0700 (PDT) Received: from localhost.localdomain (217-149-172-183.nat.highway.telekom.at. [217.149.172.183]) by smtp.gmail.com with ESMTPSA id f186sm1355035wmf.29.2020.07.07.07.41.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jul 2020 07:41:05 -0700 (PDT) Message-ID: <265c0fb3098df1da73c9d091fd9cde5389345e27.camel@cybertec.at> Subject: Re: Basic question about structuring SQL From: Laurenz Albe To: Robert Inder , pgsql-general@lists.postgresql.org Date: Tue, 07 Jul 2020 16:41:04 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.32.5 (3.32.5-1.fc30) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Tue, 2020-07-07 at 12:40 +0100, Robert Inder wrote: > NOW, I want to do the same for a BIGGER task. > BUT I realise that if I create bigger-task.sql like this... > BEGIN; > <> > \i do-something-useful.sql > <> > COMMIT; > ...the COMMIT inside "do-something-useful.sql" closes the transaction started in "bigger-task.sql" > So I can get some things committed even if other things (in tidy-up) fail. > > So how SHOULD I tackle this? > PostgreSQL does not do nested transactions (right?) You can use subtransactions. In the inner scripts, don't use BEGIN, but SAVEPOINT . Instead of ROLLBACK in the inner script, use ROLLBACK TO SAVEPOINT . Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com