Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e9afA-0004KJ-ES for pgsql-hackers@arkaria.postgresql.org; Tue, 31 Oct 2017 17:49:20 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1e9afA-0001TL-0z for pgsql-hackers@arkaria.postgresql.org; Tue, 31 Oct 2017 17:49:20 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e9adV-000709-R6 for pgsql-hackers@postgresql.org; Tue, 31 Oct 2017 17:47:37 +0000 Received: from mail-pg0-x22a.google.com ([2607:f8b0:400e:c05::22a]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1e9adO-00030X-HE for pgsql-hackers@postgresql.org; Tue, 31 Oct 2017 17:47:37 +0000 Received: by mail-pg0-x22a.google.com with SMTP id s75so15290021pgs.0 for ; Tue, 31 Oct 2017 10:47:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:from:to:cc:references:in-reply-to:subject:date :mime-version:importance; bh=GIKc94Nr5YkSzW34u0X4m+lq9BKq3rDj0/ZPFkagSaY=; b=eh5QW/FcQSaO8IJO0AN0IXkcNcw0JFQeBgqw0PrhYxxXusSQLRYDyyt5bk1U7yUyGg Ft9n6ODJWy4iPZ+6Iu0GgJNwlvMxc0c9en3I1yhLxNx9ZUYRRUtuWCTePkz5gc2fznr4 8Ktkt7fK35T/+DcmJvZoJG/9vgXpm9Pt481sfWAoxl14dOpDYqTr3F+RNsJfNgGth6/I QKUjTAWOyk5dh5+XVOIUMxF/Defxu5Tju3tyxGjIT8p1gYT49bCKsbkJBMjJJV5HtMWV m/Evw+3D9BH5jWW1ebbgUE9WBEQgEXYn9aFkd7bBFUoGha5PFqLByY8VQCQl+J4GywG4 zIew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:from:to:cc:references:in-reply-to :subject:date:mime-version:importance; bh=GIKc94Nr5YkSzW34u0X4m+lq9BKq3rDj0/ZPFkagSaY=; b=ksIUjG5es9+BVB0b98DoNe5y8TIKp/G7hUx7R01G9lZYBIGAKLFJ/jWlElPnv7rDrt xHsyZlOe5umjtxdkDTTh+KCf/uKS4izKCI5+632D2P9OdqVfTF8pweLwyTCha70Cn5ev 9v3GOwnAU4ulhXPAVrq/QjXTFBGycdIdcKTUNhL3wpMXZ5P2sXRCVaplX9lSGbvr2y/U a1hU5XitZILQm9fwTKE8psJR/muSgxG8TuifyIZbNIlf9VsvTpFMcUzhwvtj0At7lczV w9kredMlwHIHmzs9qRvnWfq/n+gvegfnc+GTMXV3alKIHfi/QorWL2l1vrs10Z9mDETX OkRw== X-Gm-Message-State: AMCzsaXmiuxFm6YE3rToxnMsGwpsPKL8knZnvNXT3y7p2NoNUW7O3+oz /H4lSNdJVYzqYQChNJ17a4s= X-Google-Smtp-Source: ABhQp+Tk/gtCchkWxkYNwB0fXYUQokZ17nT/WL4HIR+v0bedhES/7JZtAgft2hCpnKCwzTafGYhINA== X-Received: by 10.159.195.69 with SMTP id z5mr2496243pln.183.1509472048347; Tue, 31 Oct 2017 10:47:28 -0700 (PDT) Received: from tunaPC ([2400:4122:98e9:be00:9e1:235c:c8fc:a513]) by smtp.gmail.com with ESMTPSA id d76sm4434786pfk.69.2017.10.31.10.47.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 31 Oct 2017 10:47:27 -0700 (PDT) Message-ID: From: "MauMau" To: "Simon Riggs" , "Peter Eisentraut" Cc: "Tsunakawa, Takayuki" , References: <0A3221C70F24FB45833433255569204D1F6A9286@G01JPEXMBYT05> In-Reply-To: Subject: Re: Statement-level rollback Date: Wed, 1 Nov 2017 02:47:25 +0900 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_0003_01D352BB.BFF65430" X-Priority: 3 X-MSMail-Priority: Normal Importance: Normal X-Mailer: Microsoft Windows Live Mail 16.4.3528.331 X-MimeOLE: Produced By Microsoft MimeOLE V16.4.3528.331 List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org This is a multi-part message in MIME format. ------=_NextPart_000_0003_01D352BB.BFF65430 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit From: Simon Riggs On 14 August 2017 at 23:58, Peter Eisentraut wrote: > On 2/28/17 02:39, Tsunakawa, Takayuki wrote: >> The code for stored functions is not written yet, but I'd like your feedback for the specification and design based on the current patch. I'll add this patch to CommitFest 2017-3. > > This patch needs to be rebased for the upcoming commit fest. I'm willing to review this if the patch is going to be actively worked on. I'm very sorry I couldn't reply to your kind offer. I rebased the patch and will add it to CF 2017/11. I hope I will complete the patch in this CF. Regards Takayuki Tsunakawa ------=_NextPart_000_0003_01D352BB.BFF65430 Content-Type: application/octet-stream; name="stmt_rollback_v2.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="stmt_rollback_v2.patch" diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml=0A= index d360fc4d58..68098ed5d5 100644=0A= --- a/doc/src/sgml/config.sgml=0A= +++ b/doc/src/sgml/config.sgml=0A= @@ -6379,6 +6379,32 @@ COPY postgres_log FROM '/full/path/to/logfile.csv' W= ITH csv;=0A= =0A= =0A= =20=0A= + =0A= + default_transaction_rollback_scope (e= num)=0A= + =0A= + transaction rollback scope=0A= + setting default=0A= + =0A= + =0A= + default_transaction_rollback_scope configurati= on parameter=0A= + =0A= + =0A= + =0A= + =0A= + This parameter controls which range of operations=0A= + to roll back when an SQL statement fails.=0A= + transaction rolls back the entire=0A= + transaction or current subtransaction.=0A= + statement rolls back the failed SQL statement.=0A= + The default is transaction.=0A= + =0A= +=0A= + =0A= + Consult for more informatio= n.=0A= + =0A= + =0A= + =0A= +=0A= =0A= default_transaction_read_only (boolea= n)=0A= =0A= diff --git a/doc/src/sgml/ref/set_transaction.sgml b/doc/src/sgml/ref/set_t= ransaction.sgml=0A= index 3ab1e6f771..e32914525a 100644=0A= --- a/doc/src/sgml/ref/set_transaction.sgml=0A= +++ b/doc/src/sgml/ref/set_transaction.sgml=0A= @@ -10,6 +10,11 @@=0A= =0A= =20=0A= =0A= + transaction rollback scope=0A= + setting=0A= + =0A= +=0A= + =0A= read-only transaction=0A= setting=0A= =0A= @@ -39,6 +44,7 @@ SET SESSION CHARACTERISTICS AS TRANSACTION transa=0A= where transaction_mode is one of:=0A= =20=0A= ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED | RE= AD UNCOMMITTED }=0A= + ROLLBACK SCOPE { TRANSACTION | STATEMENT }=0A= READ WRITE | READ ONLY=0A= [ NOT ] DEFERRABLE=0A= =0A= @@ -59,8 +65,8 @@ SET SESSION CHARACTERISTICS AS TRANSACTION transa=0A= =20=0A= =0A= The available transaction characteristics are the transaction=0A= - isolation level, the transaction access mode (read/write or=0A= - read-only), and the deferrable mode.=0A= + isolation level, the rollback scope,=0A= + the transaction access mode (read/write or read-only), and the deferrab= le mode.=0A= In addition, a snapshot can be selected, though only for the current=0A= transaction, not as a session default.=0A= =0A= @@ -123,6 +129,36 @@ SET SESSION CHARACTERISTICS AS TRANSACTION transa=0A= isolation and concurrency control.=0A= =0A= =20=0A= +tunatuna=0A= + =0A= + The isolation level of a transaction determines what data the=0A= + The rollback scope of a transaction determines which range of=0A= + operations to roll back when an SQL statement fails:=0A= +=0A= + =0A= + =0A= + TRANSACTION=0A= + =0A= + =0A= + A statement can only see rows committed before it began. This=0A= + Rolls back the entire transaction or current subtransaction.=0A= + This is the default.=0A= + =0A= + =0A= + =0A= +=0A= + =0A= + STATEMENT=0A= + =0A= + =0A= + All statements of the current transaction can only see rows committ= ed=0A= + Rolls back the failed SQL statement.=0A= + =0A= + =0A= + =0A= + =0A= + =0A= +=0A= =0A= The transaction access mode determines whether the transaction is=0A= read/write or read-only. Read/write is the default. When a=0A= @@ -200,6 +236,7 @@ SET SESSION CHARACTERISTICS AS TRANSACTION transa=0A= =0A= The session default transaction modes can also be set by setting the=0A= configuration parameters ,=0A= + ,=0A= , and=0A= .=0A= (In fact SET SESSION CHARACTERISTICS is just a=0A= diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam= /xact.c=0A= index 8203388fa8..a4ddba4c4c 100644=0A= --- a/src/backend/access/transam/xact.c=0A= +++ b/src/backend/access/transam/xact.c=0A= @@ -73,6 +73,9 @@=0A= int DefaultXactIsoLevel =3D XACT_READ_COMMITTED;=0A= int XactIsoLevel;=0A= =20=0A= +int DefaultXactRollbackScope =3D XACT_SCOPE_XACT;=0A= +int XactRollbackScope;=0A= +=0A= bool DefaultXactReadOnly =3D false;=0A= bool XactReadOnly;=0A= =20=0A= @@ -1839,6 +1842,7 @@ StartTransaction(void)=0A= }=0A= XactDeferrable =3D DefaultXactDeferrable;=0A= XactIsoLevel =3D DefaultXactIsoLevel;=0A= + XactRollbackScope =3D DefaultXactRollbackScope;=0A= forceSyncCommit =3D false;=0A= MyXactFlags =3D 0;=0A= =20=0A= diff --git a/src/backend/commands/variable.c b/src/backend/commands/variabl= e.c=0A= index 3ed1c56e82..ac8eafd90b 100644=0A= --- a/src/backend/commands/variable.c=0A= +++ b/src/backend/commands/variable.c=0A= @@ -608,6 +608,58 @@ show_XactIsoLevel(void)=0A= }=0A= =20=0A= /*=0A= + * SET TRANSACTION ROLLBACK SCOPE=0A= + */=0A= +bool=0A= +check_XactRollbackScope(char **newval, void **extra, GucSource source)=0A= +{=0A= + int newXactRollbackScope;=0A= +=0A= + if (strcmp(*newval, "transaction") =3D=3D 0)=0A= + {=0A= + newXactRollbackScope =3D XACT_SCOPE_XACT;=0A= + }=0A= + else if (strcmp(*newval, "statement") =3D=3D 0)=0A= + {=0A= + newXactRollbackScope =3D XACT_SCOPE_STMT;=0A= + }=0A= + else if (strcmp(*newval, "default") =3D=3D 0)=0A= + {=0A= + newXactRollbackScope =3D DefaultXactRollbackScope;=0A= + }=0A= + else=0A= + return false;=0A= +=0A= + *extra =3D malloc(sizeof(int));=0A= + if (!*extra)=0A= + return false;=0A= + *((int *) *extra) =3D newXactRollbackScope;=0A= +=0A= + return true;=0A= +}=0A= +=0A= +void=0A= +assign_XactRollbackScope(const char *newval, void *extra)=0A= +{=0A= + XactRollbackScope =3D *((int *) extra);=0A= +}=0A= +=0A= +const char *=0A= +show_XactRollbackScope(void)=0A= +{=0A= + /* We need this because we don't want to show "default". */=0A= + switch (XactRollbackScope)=0A= + {=0A= + case XACT_SCOPE_XACT:=0A= + return "transaction";=0A= + case XACT_SCOPE_STMT:=0A= + return "statement";=0A= + default:=0A= + return "bogus";=0A= + }=0A= +}=0A= +=0A= +/*=0A= * SET TRANSACTION [NOT] DEFERRABLE=0A= */=0A= =20=0A= diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y=0A= index 4c83a63f7d..db71c764dd 100644=0A= --- a/src/backend/parser/gram.y=0A= +++ b/src/backend/parser/gram.y=0A= @@ -352,7 +352,7 @@ static Node *makeRecursiveViewSelect(char *relname, Lis= t *aliases, Node *query);=0A= %type RowSecurityOptionalWithCheck RowSecurityOptionalExpr=0A= %type RowSecurityDefaultToRole RowSecurityOptionalToRole=0A= =20=0A= -%type iso_level opt_encoding=0A= +%type iso_level rollback_scope opt_encoding=0A= %type grantee=0A= %type grantee_list=0A= %type privilege=0A= @@ -668,7 +668,7 @@ static Node *makeRecursiveViewSelect(char *relname, Lis= t *aliases, Node *query);=0A= RESET RESTART RESTRICT RETURNING RETURNS REVOKE RIGHT ROLE ROLLBACK ROLLU= P=0A= ROW ROWS RULE=0A= =20=0A= - SAVEPOINT SCHEMA SCHEMAS SCROLL SEARCH SECOND_P SECURITY SELECT SEQUENCE = SEQUENCES=0A= + SAVEPOINT SCHEMA SCHEMAS SCOPE SCROLL SEARCH SECOND_P SECURITY SELECT SEQ= UENCE SEQUENCES=0A= SERIALIZABLE SERVER SESSION SESSION_USER SET SETS SETOF SHARE SHOW=0A= SIMILAR SIMPLE SKIP SMALLINT SNAPSHOT SOME SQL_P STABLE STANDALONE_P=0A= START STATEMENT STATISTICS STDIN STDOUT STORAGE STRICT_P STRIP_P=0A= @@ -1557,6 +1557,10 @@ iso_level: READ UNCOMMITTED { $$ =3D "read unco= mmitted"; }=0A= | SERIALIZABLE { $$ =3D "serializable"; }=0A= ;=0A= =20=0A= +rollback_scope: TRANSACTION { $$ =3D "transaction"; }=0A= + | STATEMENT { $$ =3D "statement"; }=0A= + ;=0A= +=0A= opt_boolean_or_string:=0A= TRUE_P { $$ =3D "true"; }=0A= | FALSE_P { $$ =3D "false"; }=0A= @@ -9596,6 +9600,9 @@ transaction_mode_item:=0A= ISOLATION LEVEL iso_level=0A= { $$ =3D makeDefElem("transaction_isolation",=0A= makeStringConst($3, @3), @1); }=0A= + | ROLLBACK SCOPE rollback_scope=0A= + { $$ =3D makeDefElem("transaction_rollback_scope",=0A= + makeStringConst($3, @3), @1); }=0A= | READ ONLY=0A= { $$ =3D makeDefElem("transaction_read_only",=0A= makeIntConst(TRUE, @1), @1); }=0A= @@ -14807,6 +14814,7 @@ unreserved_keyword:=0A= | SAVEPOINT=0A= | SCHEMA=0A= | SCHEMAS=0A= + | SCOPE=0A= | SCROLL=0A= | SEARCH=0A= | SECOND_P=0A= diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c=0A= index 2c7260e564..fe2ad5da2e 100644=0A= --- a/src/backend/tcop/postgres.c=0A= +++ b/src/backend/tcop/postgres.c=0A= @@ -149,6 +149,13 @@ static bool ignore_till_sync =3D false;=0A= static bool stmt_timeout_active =3D false;=0A= =20=0A= /*=0A= + * Flag to keep track of whether we have started a transaction.=0A= + * Flag to keep track of whether we have created a savepoint=0A= + * for statement rollback.=0A= + */=0A= +static bool stmt_savepoint_created =3D false;=0A= +=0A= +/*=0A= * If an unnamed prepared statement exists, it's stored here.=0A= * We keep it separate from the hashtable kept by commands/prepare.c=0A= * in order to reduce overhead for short-lived queries.=0A= @@ -923,6 +930,16 @@ exec_simple_query(const char *query_string)=0A= start_xact_command();=0A= =20=0A= /*=0A= + * Create a savepoint for statement rollback.=0A= + */=0A= + if (XactRollbackScope =3D=3D XACT_SCOPE_STMT &&=0A= + IsTransactionBlock())=0A= + {=0A= + BeginInternalSubTransaction(NULL);=0A= + stmt_savepoint_created =3D true;=0A= + }=0A= +=0A= + /*=0A= * Zap any pre-existing unnamed statement. (While not strictly necessary= ,=0A= * it seems best to define simple-Query mode as if it used the unnamed=0A= * statement and portal; this ensures we recover any storage used by prio= r=0A= @@ -1022,6 +1039,17 @@ exec_simple_query(const char *query_string)=0A= if (use_implicit_block)=0A= BeginImplicitTransactionBlock();=0A= =20=0A= + /*=0A= + * Create a savepoint for statement rollback.=0A= + */=0A= + if (XactRollbackScope =3D=3D XACT_SCOPE_STMT &&=0A= + !stmt_savepoint_created &&=0A= + IsTransactionBlock())=0A= + {=0A= + BeginInternalSubTransaction(NULL);=0A= + stmt_savepoint_created =3D true;=0A= + }=0A= +=0A= /* If we got a cancel signal in parsing or prior command, quit */=0A= CHECK_FOR_INTERRUPTS();=0A= =20=0A= @@ -1143,6 +1171,7 @@ exec_simple_query(const char *query_string)=0A= if (use_implicit_block)=0A= EndImplicitTransactionBlock();=0A= finish_xact_command();=0A= + stmt_savepoint_created =3D false;=0A= }=0A= else if (IsA(parsetree->stmt, TransactionStmt))=0A= {=0A= @@ -1159,6 +1188,15 @@ exec_simple_query(const char *query_string)=0A= * those that start or end a transaction block.=0A= */=0A= CommandCounterIncrement();=0A= +=0A= + /*=0A= + * Delete the savepoint for statement rollback.=0A= + */=0A= + if (stmt_savepoint_created)=0A= + {=0A= + ReleaseCurrentSubTransaction();=0A= + stmt_savepoint_created =3D false;=0A= + }=0A= }=0A= =20=0A= /*=0A= @@ -1171,6 +1209,15 @@ exec_simple_query(const char *query_string)=0A= } /* end loop over parsetrees */=0A= =20=0A= /*=0A= + * Delete the savepoint for statement rollback.=0A= + */=0A= + if (stmt_savepoint_created)=0A= + {=0A= + ReleaseCurrentSubTransaction();=0A= + stmt_savepoint_created =3D false;=0A= + }=0A= +=0A= + /*=0A= * Close down transaction statement, if one is open. (This will only do= =0A= * something if the parsetree list was empty; otherwise the last loop=0A= * iteration already did it.)=0A= @@ -1258,6 +1305,16 @@ exec_parse_message(const char *query_string, /* stri= ng to execute */=0A= start_xact_command();=0A= =20=0A= /*=0A= + * Create a savepoint for statement rollback.=0A= + */=0A= + if (XactRollbackScope =3D=3D XACT_SCOPE_STMT &&=0A= + IsTransactionBlock())=0A= + {=0A= + BeginInternalSubTransaction(NULL);=0A= + stmt_savepoint_created =3D true;=0A= + }=0A= +=0A= + /*=0A= * Switch to appropriate context for constructing parsetrees.=0A= *=0A= * We have two strategies depending on whether the prepared statement is= =0A= @@ -1435,6 +1492,12 @@ exec_parse_message(const char *query_string, /* stri= ng to execute */=0A= MemoryContextSwitchTo(oldcontext);=0A= =20=0A= /*=0A= + * Delete the savepoint for statement rollback.=0A= + */=0A= + ReleaseCurrentSubTransaction();=0A= + stmt_savepoint_created =3D false;=0A= +=0A= + /*=0A= * We do NOT close the open transaction command here; that only happens= =0A= * when the client sends Sync. Instead, do CommandCounterIncrement just= =0A= * in case something happened during parse/plan.=0A= @@ -1546,6 +1609,16 @@ exec_bind_message(StringInfo input_message)=0A= */=0A= start_xact_command();=0A= =20=0A= + /*=0A= + * Create a savepoint for statement rollback.=0A= + */=0A= + if (XactRollbackScope =3D=3D XACT_SCOPE_STMT &&=0A= + IsTransactionBlock())=0A= + {=0A= + BeginInternalSubTransaction(NULL);=0A= + stmt_savepoint_created =3D true;=0A= + }=0A= +=0A= /* Switch back to message context */=0A= MemoryContextSwitchTo(MessageContext);=0A= =20=0A= @@ -1823,6 +1896,12 @@ exec_bind_message(StringInfo input_message)=0A= PortalSetResultFormat(portal, numRFormats, rformats);=0A= =20=0A= /*=0A= + * Delete the savepoint for statement rollback.=0A= + */=0A= + ReleaseCurrentSubTransaction();=0A= + stmt_savepoint_created =3D false;=0A= +=0A= + /*=0A= * Send BindComplete.=0A= */=0A= if (whereToSendOutput =3D=3D DestRemote)=0A= @@ -1884,6 +1963,16 @@ exec_execute_message(const char *portal_name, long m= ax_rows)=0A= if (dest =3D=3D DestRemote)=0A= dest =3D DestRemoteExecute;=0A= =20=0A= + /*=0A= + * Create a savepoint for statement rollback.=0A= + */=0A= + if (XactRollbackScope =3D=3D XACT_SCOPE_STMT &&=0A= + IsTransactionBlock())=0A= + {=0A= + BeginInternalSubTransaction(NULL);=0A= + stmt_savepoint_created =3D true;=0A= + }=0A= +=0A= portal =3D GetPortalByName(portal_name);=0A= if (!PortalIsValid(portal))=0A= ereport(ERROR,=0A= @@ -2026,6 +2115,7 @@ exec_execute_message(const char *portal_name, long ma= x_rows)=0A= * will start a new xact command for the next command (if any).=0A= */=0A= finish_xact_command();=0A= + stmt_savepoint_created =3D false;=0A= }=0A= else=0A= {=0A= @@ -2050,6 +2140,15 @@ exec_execute_message(const char *portal_name, long m= ax_rows)=0A= }=0A= =20=0A= /*=0A= + * Delete the savepoint for statement rollback.=0A= + */=0A= + if (stmt_savepoint_created)=0A= + {=0A= + ReleaseCurrentSubTransaction();=0A= + stmt_savepoint_created =3D false;=0A= + }=0A= +=0A= + /*=0A= * Emit duration logging if appropriate.=0A= */=0A= switch (check_log_duration(msec_str, was_logged))=0A= @@ -2327,6 +2426,16 @@ exec_describe_statement_message(const char *stmt_nam= e)=0A= */=0A= start_xact_command();=0A= =20=0A= + /*=0A= + * Create a savepoint for statement rollback.=0A= + */=0A= + if (XactRollbackScope =3D=3D XACT_SCOPE_STMT &&=0A= + IsTransactionBlock())=0A= + {=0A= + BeginInternalSubTransaction(NULL);=0A= + stmt_savepoint_created =3D true;=0A= + }=0A= +=0A= /* Switch back to message context */=0A= MemoryContextSwitchTo(MessageContext);=0A= =20=0A= @@ -2368,6 +2477,12 @@ exec_describe_statement_message(const char *stmt_nam= e)=0A= "commands ignored until end of transaction block"),=0A= errdetail_abort()));=0A= =20=0A= + /*=0A= + * Delete the savepoint for statement rollback.=0A= + */=0A= + ReleaseCurrentSubTransaction();=0A= + stmt_savepoint_created =3D false;=0A= +=0A= if (whereToSendOutput !=3D DestRemote)=0A= return; /* can't actually do anything... */=0A= =20=0A= @@ -2422,6 +2537,16 @@ exec_describe_portal_message(const char *portal_name= )=0A= */=0A= start_xact_command();=0A= =20=0A= + /*=0A= + * Create a savepoint for statement rollback.=0A= + */=0A= + if (XactRollbackScope =3D=3D XACT_SCOPE_STMT &&=0A= + IsTransactionBlock())=0A= + {=0A= + BeginInternalSubTransaction(NULL);=0A= + stmt_savepoint_created =3D true;=0A= + }=0A= +=0A= /* Switch back to message context */=0A= MemoryContextSwitchTo(MessageContext);=0A= =20=0A= @@ -2447,6 +2572,12 @@ exec_describe_portal_message(const char *portal_name= )=0A= "commands ignored until end of transaction block"),=0A= errdetail_abort()));=0A= =20=0A= + /*=0A= + * Delete the savepoint for statement rollback.=0A= + */=0A= + ReleaseCurrentSubTransaction();=0A= + stmt_savepoint_created =3D false;=0A= +=0A= if (whereToSendOutput !=3D DestRemote)=0A= return; /* can't actually do anything... */=0A= =20=0A= @@ -3929,6 +4060,13 @@ PostgresMain(int argc, char *argv[],=0A= */=0A= AbortCurrentTransaction();=0A= =20=0A= + /* Roll back the failed statement, if requested. */=0A= + if (stmt_savepoint_created)=0A= + {=0A= + RollbackAndReleaseCurrentSubTransaction();=0A= + stmt_savepoint_created =3D false;=0A= + }=0A= +=0A= if (am_walsender)=0A= WalSndErrorCleanup();=0A= =20=0A= diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c=0A= index 82a707af7b..c833f53665 100644=0A= --- a/src/backend/tcop/utility.c=0A= +++ b/src/backend/tcop/utility.c=0A= @@ -420,6 +420,10 @@ standard_ProcessUtility(PlannedStmt *pstmt,=0A= SetPGVariable("transaction_isolation",=0A= list_make1(item->arg),=0A= true);=0A= + if (strcmp(item->defname, "transaction_rollback_scope") =3D=3D 0)= =0A= + SetPGVariable("transaction_rollback_scope",=0A= + list_make1(item->arg),=0A= + true);=0A= else if (strcmp(item->defname, "transaction_read_only") =3D=3D 0)= =0A= SetPGVariable("transaction_read_only",=0A= list_make1(item->arg),=0A= diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c=0A= index 65372d7cc5..a2c485d11f 100644=0A= --- a/src/backend/utils/misc/guc.c=0A= +++ b/src/backend/utils/misc/guc.c=0A= @@ -278,6 +278,12 @@ static const struct config_enum_entry isolation_level_= options[] =3D {=0A= {NULL, 0}=0A= };=0A= =20=0A= +static const struct config_enum_entry rollback_scope_options[] =3D {=0A= + {"transaction", XACT_SCOPE_XACT, false},=0A= + {"statement", XACT_SCOPE_STMT, false},=0A= + {NULL, 0}=0A= +};=0A= +=0A= static const struct config_enum_entry session_replication_role_options[] = =3D {=0A= {"origin", SESSION_REPLICATION_ROLE_ORIGIN, false},=0A= {"replica", SESSION_REPLICATION_ROLE_REPLICA, false},=0A= @@ -505,6 +511,7 @@ static char *timezone_string;=0A= static char *log_timezone_string;=0A= static char *timezone_abbreviations_string;=0A= static char *XactIsoLevel_string;=0A= +static char *XactRollbackScope_string;=0A= static char *data_directory;=0A= static char *session_authorization_string;=0A= static int max_function_args;=0A= @@ -3407,6 +3414,17 @@ static struct config_string ConfigureNamesString[] = =3D=0A= },=0A= =20=0A= {=0A= + {"transaction_rollback_scope", PGC_USERSET, CLIENT_CONN_STATEMENT,=0A= + gettext_noop("Sets the scope of rollback when the current transaction a= borts."),=0A= + NULL,=0A= + GUC_NO_RESET_ALL | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE=0A= + },=0A= + &XactRollbackScope_string,=0A= + "default",=0A= + check_XactRollbackScope, assign_XactRollbackScope, show_XactRollbackScop= e=0A= + },=0A= +=0A= + {=0A= {"unix_socket_group", PGC_POSTMASTER, CONN_AUTH_SETTINGS,=0A= gettext_noop("Sets the owning group of the Unix-domain socket."),=0A= gettext_noop("The owning user of the socket is always the user "=0A= @@ -3710,6 +3728,16 @@ static struct config_enum ConfigureNamesEnum[] =3D= =0A= },=0A= =20=0A= {=0A= + {"default_transaction_rollback_scope", PGC_USERSET, CLIENT_CONN_STATEMEN= T,=0A= + gettext_noop("Sets the scope of rollback when the current transaction a= borts."),=0A= + NULL=0A= + },=0A= + &DefaultXactRollbackScope,=0A= + XACT_SCOPE_XACT, rollback_scope_options,=0A= + NULL, NULL, NULL=0A= + },=0A= +=0A= + {=0A= {"IntervalStyle", PGC_USERSET, CLIENT_CONN_LOCALE,=0A= gettext_noop("Sets the display format for interval values."),=0A= NULL,=0A= @@ -4507,6 +4535,8 @@ InitializeGUCOptions(void)=0A= */=0A= SetConfigOption("transaction_isolation", "default",=0A= PGC_POSTMASTER, PGC_S_OVERRIDE);=0A= + SetConfigOption("transaction_rollback_scope", "default",=0A= + PGC_POSTMASTER, PGC_S_OVERRIDE);=0A= SetConfigOption("transaction_read_only", "no",=0A= PGC_POSTMASTER, PGC_S_OVERRIDE);=0A= SetConfigOption("transaction_deferrable", "no",=0A= @@ -7337,6 +7367,9 @@ ExecSetVariableStmt(VariableSetStmt *stmt, bool isTop= Level)=0A= if (strcmp(item->defname, "transaction_isolation") =3D=3D 0)=0A= SetPGVariable("transaction_isolation",=0A= list_make1(item->arg), stmt->is_local);=0A= + else if (strcmp(item->defname, "transaction_rollback_scope") =3D=3D 0= )=0A= + SetPGVariable("transaction_isolation",=0A= + list_make1(item->arg), stmt->is_local);=0A= else if (strcmp(item->defname, "transaction_read_only") =3D=3D 0)=0A= SetPGVariable("transaction_read_only",=0A= list_make1(item->arg), stmt->is_local);=0A= @@ -7359,6 +7392,9 @@ ExecSetVariableStmt(VariableSetStmt *stmt, bool isTop= Level)=0A= if (strcmp(item->defname, "transaction_isolation") =3D=3D 0)=0A= SetPGVariable("default_transaction_isolation",=0A= list_make1(item->arg), stmt->is_local);=0A= + else if (strcmp(item->defname, "transaction_rollback_scope") =3D=3D 0= )=0A= + SetPGVariable("default_transaction_isolation",=0A= + list_make1(item->arg), stmt->is_local);=0A= else if (strcmp(item->defname, "transaction_read_only") =3D=3D 0)=0A= SetPGVariable("default_transaction_read_only",=0A= list_make1(item->arg), stmt->is_local);=0A= diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/ut= ils/misc/postgresql.conf.sample=0A= index 368b280c8a..a3aa64067f 100644=0A= --- a/src/backend/utils/misc/postgresql.conf.sample=0A= +++ b/src/backend/utils/misc/postgresql.conf.sample=0A= @@ -545,6 +545,7 @@=0A= # only default tablespace=0A= #check_function_bodies =3D on=0A= #default_transaction_isolation =3D 'read committed'=0A= +#default_transaction_rollback_scope =3D 'transaction'=0A= #default_transaction_read_only =3D off=0A= #default_transaction_deferrable =3D off=0A= #session_replication_role =3D 'origin'=0A= diff --git a/src/include/access/xact.h b/src/include/access/xact.h=0A= index f2c10f905f..cb7ab74376 100644=0A= --- a/src/include/access/xact.h=0A= +++ b/src/include/access/xact.h=0A= @@ -43,6 +43,15 @@ extern PGDLLIMPORT int XactIsoLevel;=0A= #define IsolationUsesXactSnapshot() (XactIsoLevel >=3D XACT_REPEATABLE_REA= D)=0A= #define IsolationIsSerializable() (XactIsoLevel =3D=3D XACT_SERIALIZABLE)= =0A= =20=0A= +/*=0A= + * Xact rollback scopes=0A= + */=0A= +#define XACT_SCOPE_XACT 0=0A= +#define XACT_SCOPE_STMT 1=0A= +=0A= +extern int DefaultXactRollbackScope;=0A= +extern PGDLLIMPORT int XactRollbackScope;=0A= +=0A= /* Xact read-only state */=0A= extern bool DefaultXactReadOnly;=0A= extern bool XactReadOnly;=0A= diff --git a/src/include/commands/variable.h b/src/include/commands/variabl= e.h=0A= index 575339a6d8..69dd0c07fd 100644=0A= --- a/src/include/commands/variable.h=0A= +++ b/src/include/commands/variable.h=0A= @@ -25,6 +25,9 @@ extern bool check_transaction_read_only(bool *newval, voi= d **extra, GucSource so=0A= extern bool check_XactIsoLevel(char **newval, void **extra, GucSource sour= ce);=0A= extern void assign_XactIsoLevel(const char *newval, void *extra);=0A= extern const char *show_XactIsoLevel(void);=0A= +extern bool check_XactRollbackScope(char **newval, void **extra, GucSource= source);=0A= +extern void assign_XactRollbackScope(const char *newval, void *extra);=0A= +extern const char *show_XactRollbackScope(void);=0A= extern bool check_transaction_deferrable(bool *newval, void **extra, GucSo= urce source);=0A= extern bool check_random_seed(double *newval, void **extra, GucSource sour= ce);=0A= extern void assign_random_seed(double newval, void *extra);=0A= diff --git a/src/include/parser/kwlist.h b/src/include/parser/kwlist.h=0A= index f50e45e886..db3bf834d2 100644=0A= --- a/src/include/parser/kwlist.h=0A= +++ b/src/include/parser/kwlist.h=0A= @@ -346,6 +346,7 @@ PG_KEYWORD("rule", RULE, UNRESERVED_KEYWORD)=0A= PG_KEYWORD("savepoint", SAVEPOINT, UNRESERVED_KEYWORD)=0A= PG_KEYWORD("schema", SCHEMA, UNRESERVED_KEYWORD)=0A= PG_KEYWORD("schemas", SCHEMAS, UNRESERVED_KEYWORD)=0A= +PG_KEYWORD("scope", SCOPE, UNRESERVED_KEYWORD)=0A= PG_KEYWORD("scroll", SCROLL, UNRESERVED_KEYWORD)=0A= PG_KEYWORD("search", SEARCH, UNRESERVED_KEYWORD)=0A= PG_KEYWORD("second", SECOND_P, UNRESERVED_KEYWORD)=0A= ------=_NextPart_000_0003_01D352BB.BFF65430 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers ------=_NextPart_000_0003_01D352BB.BFF65430--