Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a6542a4

Browse filesBrowse files
committed
Change SET LOCAL/CONSTRAINTS/TRANSACTION and ABORT behavior
Change SET LOCAL/CONSTRAINTS/TRANSACTION behavior outside of a transaction block from error (post-9.3) to warning. (Was nothing in <= 9.3.) Also change ABORT outside of a transaction block from notice to warning.
1 parent 05b476c commit a6542a4
Copy full SHA for a6542a4

File tree

Expand file treeCollapse file tree

11 files changed

+47
-28
lines changed
Filter options
Expand file treeCollapse file tree

11 files changed

+47
-28
lines changed

‎doc/src/sgml/ref/abort.sgml

Copy file name to clipboardExpand all lines: doc/src/sgml/ref/abort.sgml
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ ABORT [ WORK | TRANSACTION ]
6363
</para>
6464

6565
<para>
66-
Issuing <command>ABORT</> when not inside a transaction does
67-
no harm, but it will provoke a warning message.
66+
Issuing <command>ABORT</> outside of a transaction block has no effect.
6867
</para>
6968
</refsect1>
7069

‎doc/src/sgml/ref/rollback.sgml

Copy file name to clipboardExpand all lines: doc/src/sgml/ref/rollback.sgml
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ ROLLBACK [ WORK | TRANSACTION ]
5959
</para>
6060

6161
<para>
62-
Issuing <command>ROLLBACK</> when not inside a transaction does
63-
no harm, but it will provoke a warning message.
62+
Issuing <command>ROLLBACK</> outside of a transaction
63+
block has no effect.
6464
</para>
6565
</refsect1>
6666

‎doc/src/sgml/ref/set.sgml

Copy file name to clipboardExpand all lines: doc/src/sgml/ref/set.sgml
+2-3Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,8 @@ SET [ SESSION | LOCAL ] TIME ZONE { <replaceable class="PARAMETER">timezone</rep
110110
<para>
111111
Specifies that the command takes effect for only the current
112112
transaction. After <command>COMMIT</> or <command>ROLLBACK</>,
113-
the session-level setting takes effect again.
114-
<productname>PostgreSQL</productname> reports an error if
115-
<command>SET LOCAL</> is used outside a transaction block.
113+
the session-level setting takes effect again. This has no effect
114+
outside of a transaction block.
116115
</para>
117116
</listitem>
118117
</varlistentry>

‎doc/src/sgml/ref/set_constraints.sgml

Copy file name to clipboardExpand all lines: doc/src/sgml/ref/set_constraints.sgml
+1-4Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,7 @@ SET CONSTRAINTS { ALL | <replaceable class="parameter">name</replaceable> [, ...
9999

100100
<para>
101101
This command only alters the behavior of constraints within the
102-
current transaction. Thus, if you execute this command outside of a
103-
transaction block
104-
(<command>BEGIN</command>/<command>COMMIT</command> pair), it will
105-
generate an error.
102+
current transaction. This has no effect outside of a transaction block.
106103
</para>
107104
</refsect1>
108105

‎doc/src/sgml/ref/set_transaction.sgml

Copy file name to clipboardExpand all lines: doc/src/sgml/ref/set_transaction.sgml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ SET SESSION CHARACTERISTICS AS TRANSACTION <replaceable class="parameter">transa
185185
<para>
186186
If <command>SET TRANSACTION</command> is executed without a prior
187187
<command>START TRANSACTION</command> or <command>BEGIN</command>,
188-
it will generate an error.
188+
it will have no effect.
189189
</para>
190190

191191
<para>

‎src/backend/access/transam/xact.c

Copy file name to clipboardExpand all lines: src/backend/access/transam/xact.c
+30-7Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ static void CallSubXactCallbacks(SubXactEvent event,
265265
SubTransactionId mySubid,
266266
SubTransactionId parentSubid);
267267
static void CleanupTransaction(void);
268+
static void CheckTransactionChain(bool isTopLevel, bool throwError,
269+
const char *stmtType);
268270
static void CommitTransaction(void);
269271
static TransactionId RecordTransactionAbort(bool isSubXact);
270272
static void StartTransaction(void);
@@ -2948,6 +2950,26 @@ PreventTransactionChain(bool isTopLevel, const char *stmtType)
29482950
/* all okay */
29492951
}
29502952

2953+
/*
2954+
* These two functions allow for warnings or errors if a command is
2955+
* executed outside of a transaction block.
2956+
*
2957+
* While top-level transaction control commands (BEGIN/COMMIT/ABORT) and
2958+
* SET that have no effect issue warnings, all other no-effect commands
2959+
* generate errors.
2960+
*/
2961+
void
2962+
WarnNoTransactionChain(bool isTopLevel, const char *stmtType)
2963+
{
2964+
CheckTransactionChain(isTopLevel, false, stmtType);
2965+
}
2966+
2967+
void
2968+
RequireTransactionChain(bool isTopLevel, const char *stmtType)
2969+
{
2970+
CheckTransactionChain(isTopLevel, true, stmtType);
2971+
}
2972+
29512973
/*
29522974
* RequireTransactionChain
29532975
*
@@ -2957,16 +2979,16 @@ PreventTransactionChain(bool isTopLevel, const char *stmtType)
29572979
* is presumably an error). DECLARE CURSOR is an example.
29582980
*
29592981
* If we appear to be running inside a user-defined function, we do not
2960-
* issue an error, since the function could issue more commands that make
2982+
* issue anything, since the function could issue more commands that make
29612983
* use of the current statement's results. Likewise subtransactions.
29622984
* Thus this is an inverse for PreventTransactionChain.
29632985
*
29642986
* isTopLevel: passed down from ProcessUtility to determine whether we are
29652987
* inside a function.
2966-
* stmtType: statement type name, for error messages.
2988+
* stmtType: statement type name, for warning or error messages.
29672989
*/
2968-
void
2969-
RequireTransactionChain(bool isTopLevel, const char *stmtType)
2990+
static void
2991+
CheckTransactionChain(bool isTopLevel, bool throwError, const char *stmtType)
29702992
{
29712993
/*
29722994
* xact block already started?
@@ -2986,11 +3008,12 @@ RequireTransactionChain(bool isTopLevel, const char *stmtType)
29863008
if (!isTopLevel)
29873009
return;
29883010

2989-
ereport(ERROR,
3011+
ereport(throwError ? ERROR : WARNING,
29903012
(errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
29913013
/* translator: %s represents an SQL statement name */
29923014
errmsg("%s can only be used in transaction blocks",
29933015
stmtType)));
3016+
return;
29943017
}
29953018

29963019
/*
@@ -3425,12 +3448,12 @@ UserAbortTransactionBlock(void)
34253448

34263449
/*
34273450
* The user issued ABORT when not inside a transaction. Issue a
3428-
* NOTICE and go to abort state. The upcoming call to
3451+
* WARNING and go to abort state. The upcoming call to
34293452
* CommitTransactionCommand() will then put us back into the
34303453
* default state.
34313454
*/
34323455
case TBLOCK_STARTED:
3433-
ereport(NOTICE,
3456+
ereport(WARNING,
34343457
(errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
34353458
errmsg("there is no transaction in progress")));
34363459
s->blockState = TBLOCK_ABORT_PENDING;

‎src/backend/tcop/utility.c

Copy file name to clipboardExpand all lines: src/backend/tcop/utility.c
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ standard_ProcessUtility(Node *parsetree,
754754
break;
755755

756756
case T_ConstraintsSetStmt:
757-
RequireTransactionChain(isTopLevel, "SET CONSTRAINTS");
757+
WarnNoTransactionChain(isTopLevel, "SET CONSTRAINTS");
758758
AfterTriggerSetState((ConstraintsSetStmt *) parsetree);
759759
break;
760760

‎src/backend/utils/misc/guc.c

Copy file name to clipboardExpand all lines: src/backend/utils/misc/guc.c
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6274,7 +6274,7 @@ ExecSetVariableStmt(VariableSetStmt *stmt, bool isTopLevel)
62746274
case VAR_SET_VALUE:
62756275
case VAR_SET_CURRENT:
62766276
if (stmt->is_local)
6277-
RequireTransactionChain(isTopLevel, "SET LOCAL");
6277+
WarnNoTransactionChain(isTopLevel, "SET LOCAL");
62786278
(void) set_config_option(stmt->name,
62796279
ExtractSetVariableArgs(stmt),
62806280
(superuser() ? PGC_SUSET : PGC_USERSET),
@@ -6295,7 +6295,7 @@ ExecSetVariableStmt(VariableSetStmt *stmt, bool isTopLevel)
62956295
{
62966296
ListCell *head;
62976297

6298-
RequireTransactionChain(isTopLevel, "SET TRANSACTION");
6298+
WarnNoTransactionChain(isTopLevel, "SET TRANSACTION");
62996299

63006300
foreach(head, stmt->args)
63016301
{
@@ -6346,7 +6346,7 @@ ExecSetVariableStmt(VariableSetStmt *stmt, bool isTopLevel)
63466346
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
63476347
errmsg("SET LOCAL TRANSACTION SNAPSHOT is not implemented")));
63486348

6349-
RequireTransactionChain(isTopLevel, "SET TRANSACTION");
6349+
WarnNoTransactionChain(isTopLevel, "SET TRANSACTION");
63506350
Assert(IsA(con, A_Const));
63516351
Assert(nodeTag(&con->val) == T_String);
63526352
ImportSnapshot(strVal(&con->val));
@@ -6357,11 +6357,11 @@ ExecSetVariableStmt(VariableSetStmt *stmt, bool isTopLevel)
63576357
break;
63586358
case VAR_SET_DEFAULT:
63596359
if (stmt->is_local)
6360-
RequireTransactionChain(isTopLevel, "SET LOCAL");
6360+
WarnNoTransactionChain(isTopLevel, "SET LOCAL");
63616361
/* fall through */
63626362
case VAR_RESET:
63636363
if (strcmp(stmt->name, "transaction_isolation") == 0)
6364-
RequireTransactionChain(isTopLevel, "RESET TRANSACTION");
6364+
WarnNoTransactionChain(isTopLevel, "RESET TRANSACTION");
63656365

63666366
(void) set_config_option(stmt->name,
63676367
NULL,

‎src/include/access/xact.h

Copy file name to clipboardExpand all lines: src/include/access/xact.h
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ extern char TransactionBlockStatusCode(void);
245245
extern void AbortOutOfAnyTransaction(void);
246246
extern void PreventTransactionChain(bool isTopLevel, const char *stmtType);
247247
extern void RequireTransactionChain(bool isTopLevel, const char *stmtType);
248+
extern void WarnNoTransactionChain(bool isTopLevel, const char *stmtType);
248249
extern bool IsInTransactionChain(bool isTopLevel);
249250
extern void RegisterXactCallback(XactCallback callback, void *arg);
250251
extern void UnregisterXactCallback(XactCallback callback, void *arg);

‎src/test/regress/expected/errors.out

Copy file name to clipboardExpand all lines: src/test/regress/expected/errors.out
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ ERROR: column name "oid" conflicts with a system column name
114114
-- TRANSACTION STUFF
115115
-- not in a xact
116116
abort;
117-
NOTICE: there is no transaction in progress
117+
WARNING: there is no transaction in progress
118118
-- not in a xact
119119
end;
120120
WARNING: there is no transaction in progress

‎src/test/regress/expected/guc.out

Copy file name to clipboardExpand all lines: src/test/regress/expected/guc.out
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ SELECT '2006-08-13 12:34:56'::timestamptz;
2929

3030
-- SET LOCAL has no effect outside of a transaction
3131
SET LOCAL vacuum_cost_delay TO 50;
32-
ERROR: SET LOCAL can only be used in transaction blocks
32+
WARNING: SET LOCAL can only be used in transaction blocks
3333
SHOW vacuum_cost_delay;
3434
vacuum_cost_delay
3535
-------------------
3636
40ms
3737
(1 row)
3838

3939
SET LOCAL datestyle = 'SQL';
40-
ERROR: SET LOCAL can only be used in transaction blocks
40+
WARNING: SET LOCAL can only be used in transaction blocks
4141
SHOW datestyle;
4242
DateStyle
4343
-----------

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.