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 1olFDs-00074U-I2 for pgsql-general@arkaria.postgresql.org; Wed, 19 Oct 2022 19:59:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1olFDr-0002Qk-Da for pgsql-general@arkaria.postgresql.org; Wed, 19 Oct 2022 19:59:27 +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 1olFDr-0002Qa-0T for pgsql-general@lists.postgresql.org; Wed, 19 Oct 2022 19:59:27 +0000 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1olFDo-0001x4-Rf for pgsql-general@lists.postgresql.org; Wed, 19 Oct 2022 19:59:26 +0000 Received: by mail-pl1-x632.google.com with SMTP id c24so18315169plo.3 for ; Wed, 19 Oct 2022 12:59:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yugabyte.com; s=google; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=9tJtu88ZqBNO+isUlPklQM1RZlOn9WXVPmsDSfiudP0=; b=UjbCp5ywCaXRnZ2FWNawoflulniZJ/M32xouNI9iOT2p09oRciDFGQlQBl33jvKI+g wRih29rvUIHEUUv/qMnrGUF4W64woHyVIf8poCmMmSIOlLRvuv0+IC8cXAn3EsiSxLus y030idaWv7t6hkGSWsUZctANZ3AVniMmSMgiA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9tJtu88ZqBNO+isUlPklQM1RZlOn9WXVPmsDSfiudP0=; b=P6agJ+++xB0ENyYl2iCu8fV82jRSONsb4+qrrXsEe3sb+/7x8XrxkOLNJUAh61l5eh 1F31q3f2JmiyvJs3EGZbrUSjYg8WGZsTp7E2ZQYPE0+FxRy/isysTdxF/DlQCm0QXoPC bTLtB334hxMQb8IADij2HAoTnj7TTpbt6auDu090/W0Rd20GiWkCjpnUXZZ6Aq1m6rmD 14mTzs+QSjA1oG1qVBgfQr3LfZWpXgSFacisuM+lsYwNHH8qOFrJ3a4rAuvGrpKmsTrl PrRzRl3g6sAdXFcPLLWEuoN53XPl0owso4va07NzY+BcMIozQWADzjgZxA6ikdCdIHRZ Td/A== X-Gm-Message-State: ACrzQf2hjdcQtHZHMevDci/lSB73j2AkTPDcmw/3kOHTZF5+PUfpFh/Z 0KuKgipRGRozftLQLhFp2LZ5AdzjVvgZFg== X-Google-Smtp-Source: AMsMyM7HpaOuuQ7gPNthqBFEgwPyawPBQg1A7+6PerjeXQ5XT2rlK1KvtqrYQ5pbV9bHOpZ/XMLXsw== X-Received: by 2002:a17:90b:4b02:b0:20d:3937:7ad with SMTP id lx2-20020a17090b4b0200b0020d393707admr46917890pjb.145.1666209562286; Wed, 19 Oct 2022 12:59:22 -0700 (PDT) Received: from smtpclient.apple ([2601:646:101:7fda:14fd:bf75:5b5f:5b59]) by smtp.gmail.com with ESMTPSA id j8-20020a170902da8800b0017305e99f00sm11185626plx.107.2022.10.19.12.59.21 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 19 Oct 2022 12:59:21 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) Subject: Re: COMMIT IN STORED PROCEDURE WHILE IN A LOOP From: Bryn Llewellyn In-Reply-To: <1c4cb79ca48fa14246fbe78374da4ef9ac435a0a.camel@gmail.com> Date: Wed, 19 Oct 2022 12:59:20 -0700 Cc: Christophe Pettus , Ravi Krishna , gogala.mladen@gmail.com, Tom Lane PostgreSQL , Laurenz Albe Content-Transfer-Encoding: quoted-printable Message-Id: References: <353091661.109766.1666124045054.ref@mail.yahoo.com> <353091661.109766.1666124045054@mail.yahoo.com> <72BEC008-DC6C-4D80-80A6-E1634CFFA656@thebuild.com> <80B77102-84D3-4AD8-B138-1C0C3341855B@thebuild.com> <2b93e12697bb273c493b47e04c762b39a66910ab.camel@gmail.com> <1c4cb79ca48fa14246fbe78374da4ef9ac435a0a.camel@gmail.com> To: pgsql-general list X-Mailer: Apple Mail (2.3654.120.0.1.13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk It seems that I made a thread-discipline error when I asked a question = that had nothing to do with the frequency, or the cost, of committing = when I saw this reply (paraphrased for brevity here) from Christophe: > You [cannot] commit in [a] BEGIN / END [block statement] that has an = exception handler [because] that creates a subtransaction for the = duration of the [block statement]. I asked this in response (again, paraphrased for brevity): > Could the limitation be lifted...? [Or is the limitation] rooted in = profoundly deep features of the architecture? Sorry that I caused some distraction. Anyway, Tom replied immediately. = He said: > BEGIN with an exception block is a subtransaction because it's defined = to roll back to the database state as of the start of the block if an = exception occurs. COMMIT in the middle fundamentally conflicts with = that, I should think. Thanks, Tom. It's clear to me now that the present PG paradigm will = never, ever change. So my conclusion stands for this use case: I'm using "serializable" = isolation (which luxury ORCL doesn't afford me); I know that I can get a = "cannot serialize" error at "commit" time. Here, I cannot take = appropriate action within my PL/pgSQL code and hide the whole story of = what this is about from client code. Rather, I must explain the business = to the authors of the next tier, and teach them when, and how, retry is = appropriate. Tom asked, too, if ORCL has a different paradigm... Briefly, yes=E2=80=94a= nd radically so. But (all of you) do please feel free to skip over my = sketch here if it doesn't interest you. =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 = =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 = =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 = =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 =E2=80=94 = =E2=80=94 =E2=80=94=20 =C2=AB The real account of which this is a sketch is of no interest at all if = you work only with PG and have never used ORCL. But if you need to = switch, intellectually, from one to the other=E2=80=94and maybe need to = do this all the time because your job duties span both systems=E2=80=94the= n it *is* interesting. ORCL's PL/SQL is compiled (and optimized) at "create procedure" time. = The new source code replaces the old in the catalog. And if it failed to = compile, then you're left with an invalid unit that you cannot execute. = Compilation errors are recorded in the catalog too. Further, static = dependencies (proc upon proc, proc upon table, etc) are also recorded in = the catalog. This is intimately connected with the hard distinction = between static and dynamic SQL. The latter simply passes the text on "as = is" into the byte code to be dealt with at run-time. Only statements = like "select", "insert", "update", delete" and a few others can be = static SQL. Table creation and the like must be dynamic SQL. This = probably shocks those who move from PG to ORCL because you cannot, for = example, create a table and then operate on it with static SQL in the = same procedure. In particular, for the present discussion, the PL/SQL block statement is = a pure lexical device. (This is the case in PL/SQL's progenitor, ADA. = And that's where all that stuff about DIANA, that the PL/SQL programmer = eventually comes to hear about, comes from.) All memory that you had a = block statement in the source is lost in the compiled so-called byte = code that gets interpreted at run time. On the other hand, every call = from PL/SQL to SQL is done in its own subtransaction=E2=80=94and if it = fails, then that single statement is atomically rolled back. The effect = of all the SQLs to date, at this moment, remains intact=E2=80=94but = uncommitted. (Of course, you might have issued "commit"(s) = programmatically. So I'm talking about SQLs that were done since the = most recent "commit".)=20 Significantly, the failure of a call from PL/SQL to SQL raises an = exception=E2=80=94so (as well as the single-statement rollback) you now = have an in-flight exception that flies up through successive scopes in = search of a matching handler. If it remains unhandled at the last moment = before the top-level PL/SQL "call" is due to finish, then a "rollback" = is automatically issued. But if a handler *is* found, well... the = exception is dead and you can carry on. Like everything else in = programming, the code author must work out what "safe" is. (It could be = to turn an insert that fails 'cos a unique key is violated into an = update.) In ORCL, just as in PG, writing "when others than null" is held = to be stupid. And code examples that do this are deemed to be not worthy = of discussion. Though the paradigms are different, each allows you properly to = implement mission-critical applications. It's rather like English and = Chinese. Astonishingly different. But each supports all that you need to = let people communicate about mundane daily business, science, = philosophy, epistemology, and so on.) =C2=BB