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 1fTvFy-00032n-O3 for pgsql-hackers@arkaria.postgresql.org; Fri, 15 Jun 2018 20:23:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fTvFw-0006Um-TG for pgsql-hackers@arkaria.postgresql.org; Fri, 15 Jun 2018 20:23:36 +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 1fTvFw-0006Uc-Dc for pgsql-hackers@lists.postgresql.org; Fri, 15 Jun 2018 20:23:36 +0000 Received: from mail-qk0-x242.google.com ([2607:f8b0:400d:c09::242]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fTvFs-0002V1-PG for pgsql-hackers@postgresql.org; Fri, 15 Jun 2018 20:23:34 +0000 Received: by mail-qk0-x242.google.com with SMTP id 185-v6so6331265qkk.11 for ; Fri, 15 Jun 2018 13:23:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=s83nXtpnGJx1qrotMLt1ktxBUQ3DOws27s74aMaZYuQ=; b=AJSxQYw5VcGUKzXREIKcrrAlHRLGNRPiA1YuDtsXNWCbyS1AN+5fs+6vGuYN0Swg4h 5xUdkeJaK/mWmKQvReJMpta04BgsLMA0R2mo4DFnx1NG09YZ6a+YD5fezpGBwtDdKfhk xKRXTKhbHrSFpIqcei97/vu6uE4frJk4PVO7lEwTp/I5pFqmW8BAMmxaqg++HS/snfQw ZKeuV93IzjzjUz9UIwmnCcEVZM5qWUcv35aPYGTccaikM4nJYNmYQnYWeUuMTOeKvCOJ wg8aVfrtdyigMW5Cs5T/nmPlcK9wg+skPNxj5TEEKyNOxiY9e/avF/ry+ujeawJPgAPY UwOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=s83nXtpnGJx1qrotMLt1ktxBUQ3DOws27s74aMaZYuQ=; b=me59FTjKf7JEZ+4HxmAM85U4O3Vt2Ka10q6nR5X2znXSALW+eG0+OWLgtrF0FnLCGI ENFbnqNzvBuX2Agc+gxmxZ85I/wJTOj0jX28HMyqo1zBou92HeFpH8rbzlR5H2rp+jm/ 0fqfyyIYgFHIlQCk1Lnjps7yHli0Doynh6IiKinKw+bstLdSojSFT5DUnsChCTnDkM+B sfEQWFDyC+s8ryL8kKjMd91Eq2h+S0nwiNV600X/v53Z6AfHfRrOQnRCJHs48wbXEl4u jb/esfSD974yfM28mM6oY7sFYls8bBWsEOwF5NDC8DpeEQ6+RUc770An3nf1Le7f7X/b b7dg== X-Gm-Message-State: APt69E2Vyr5gpehdIHk54KF6IK1TrCzOaev2TI6s0yxjLMBs319Hh5XL 5NX2j5d/Hlorgukd/ws2b7fOEA== X-Google-Smtp-Source: ADUXVKIgBhbmwVU1GbOnr390Pnj0sbFPu7TEZvnWkFlh91AVHTLfjp3pjXnJmZEx2M9CcxO10l3j2Q== X-Received: by 2002:a37:8483:: with SMTP id g125-v6mr2552750qkd.392.1529094211818; Fri, 15 Jun 2018 13:23:31 -0700 (PDT) Received: from alvin.alvh.no-ip.org ([190.121.29.3]) by smtp.gmail.com with ESMTPSA id d11-v6sm5864253qto.41.2018.06.15.13.23.30 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 15 Jun 2018 13:23:30 -0700 (PDT) Received: by alvin.alvh.no-ip.org (Postfix, from userid 1000) id 89EC19F7; Fri, 15 Jun 2018 16:23:28 -0400 (-04) Date: Fri, 15 Jun 2018 16:23:28 -0400 From: Alvaro Herrera To: "Tsunakawa, Takayuki" Cc: 'Simon Riggs' , Peter Eisentraut , MauMau , PostgreSQL-development Subject: Re: [HACKERS] Statement-level rollback Message-ID: <20180615202328.7m46qo46v5a5wkd2@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <0A3221C70F24FB45833433255569204D1F810A6B@G01JPEXMBYT05> User-Agent: NeoMutt/20170306-137-4415bd-dirty (1.8.0) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2017-Nov-06, Tsunakawa, Takayuki wrote: > From: Simon Riggs > > A backend-based solution is required for PL procedures and functions. > > > > We could put this as an option into PL/pgSQL, but it seems like it is > > a function of the transaction manager rather than the driver. > > Exactly. Thanks. I've been looking at re-implementing this feature recently, using Tsunakawa's proposed UI of a GUC transaction_rollback_scope that can take values "transaction" (default, current behavior) and "statement". I didn't take other parts of his patch though; see below. I think the main objectionable point is that of making servers behave in a way that could lose data, if applications assume that transactions behave in the way they do today. I propose that we solve this by allowing this feature to be enabled only via one of: * a PGOPTIONS connection-time option * ALTER USER SET (transaction_rollback_scope) but it can be *disabled* normally via SET. In other words, changing the scope from transaction to statement in a running session is forbidden, but changing it the other way around is allowed (if app is unsure whether env is unsafe, it can set the scope to "transaction" to ensure it's safe from that point onwards). Changing the scope in postgresql.conf is forbidden, so a server is never unsafe as a whole. Drivers such as JDBC can easily use this mode, for example a connection option such as "AUTOSAVE=SERVER" can automatically add the transaction_rollback_scope option. (Naturally, if the server does not support transaction_rollback_scope and the user gave that option, this causes an exception to be raised -- NOT to fallback to the standard transaction behavior!) Tsunakawa's implementation puts the feature in postgres.c's client loop. I think a better way to implement this is to change xact.c to have a new TBLOCK state which indicates when to start a new internal subtransaction; StartTransactionCommand pushes a new element into the transaction stack and puts it in the new state; a subsequent operation actually starts the new subtransaction. (This design decision allows things like SAVEPOINT to work correctly by having the subtrasaction-for-savepoint appear *before* the internal subtransaction, so a subsequent "SELECT 0/0" doesn't remove the user declared savepoint.) I have a PoC implementation that's slightly different: it adds more code to a few xact.c low-level routines (StartTransactionCommand creates the internal savepoint itself). It's not as nice because SAVEPOINT has to close the internal subtransaction, then create the savepoint, then create the internal subtransaction again. And it doesn't handle RELEASE. But as a PoC it's quite nice. I measured the performance overhead to be about 2% - 3% loss of the current mode, which seems acceptable. I would like to hear opinions on whether the protections I propose are sufficient to appease the objections. In my opinion they are, and we should press forward with this, which seems to be one of the frequently requested features from people porting from other DBMSs. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services