</para>
<para>
- Issuing <command>ABORT</> when not inside a transaction does
- no harm, but it will provoke a warning message.
+ Issuing <command>ABORT</> outside of a transaction block has no effect.
</para>
</refsect1>
</para>
<para>
- Issuing <command>ROLLBACK</> when not inside a transaction does
- no harm, but it will provoke a warning message.
+ Issuing <command>ROLLBACK</> outside of a transaction
+ block has no effect.
</para>
</refsect1>
<para>
Specifies that the command takes effect for only the current
transaction. After <command>COMMIT</> or <command>ROLLBACK</>,
- the session-level setting takes effect again.
- <productname>PostgreSQL</productname> reports an error if
- <command>SET LOCAL</> is used outside a transaction block.
+ the session-level setting takes effect again. This has no effect
+ outside of a transaction block.
</para>
</listitem>
</varlistentry>
<para>
This command only alters the behavior of constraints within the
- current transaction. Thus, if you execute this command outside of a
- transaction block
- (<command>BEGIN</command>/<command>COMMIT</command> pair), it will
- generate an error.
+ current transaction. This has no effect outside of a transaction block.
</para>
</refsect1>
<para>
If <command>SET TRANSACTION</command> is executed without a prior
<command>START TRANSACTION</command> or <command>BEGIN</command>,
- it will generate an error.
+ it will have no effect.
</para>
<para>
SubTransactionId mySubid,
SubTransactionId parentSubid);
static void CleanupTransaction(void);
+static void CheckTransactionChain(bool isTopLevel, bool throwError,
+ const char *stmtType);
static void CommitTransaction(void);
static TransactionId RecordTransactionAbort(bool isSubXact);
static void StartTransaction(void);
/* all okay */
}
+/*
+ * These two functions allow for warnings or errors if a command is
+ * executed outside of a transaction block.
+ *
+ * While top-level transaction control commands (BEGIN/COMMIT/ABORT) and
+ * SET that have no effect issue warnings, all other no-effect commands
+ * generate errors.
+ */
+void
+WarnNoTransactionChain(bool isTopLevel, const char *stmtType)
+{
+ CheckTransactionChain(isTopLevel, false, stmtType);
+}
+
+void
+RequireTransactionChain(bool isTopLevel, const char *stmtType)
+{
+ CheckTransactionChain(isTopLevel, true, stmtType);
+}
+
/*
* RequireTransactionChain
*
* is presumably an error). DECLARE CURSOR is an example.
*
* If we appear to be running inside a user-defined function, we do not
- * issue an error, since the function could issue more commands that make
+ * issue anything, since the function could issue more commands that make
* use of the current statement's results. Likewise subtransactions.
* Thus this is an inverse for PreventTransactionChain.
*
* isTopLevel: passed down from ProcessUtility to determine whether we are
* inside a function.
- * stmtType: statement type name, for error messages.
+ * stmtType: statement type name, for warning or error messages.
*/
-void
-RequireTransactionChain(bool isTopLevel, const char *stmtType)
+static void
+CheckTransactionChain(bool isTopLevel, bool throwError, const char *stmtType)
{
/*
* xact block already started?
if (!isTopLevel)
return;
- ereport(ERROR,
+ ereport(throwError ? ERROR : WARNING,
(errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
/* translator: %s represents an SQL statement name */
errmsg("%s can only be used in transaction blocks",
stmtType)));
+ return;
}
/*
/*
* The user issued ABORT when not inside a transaction. Issue a
- * NOTICE and go to abort state. The upcoming call to
+ * WARNING and go to abort state. The upcoming call to
* CommitTransactionCommand() will then put us back into the
* default state.
*/
case TBLOCK_STARTED:
- ereport(NOTICE,
+ ereport(WARNING,
(errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
errmsg("there is no transaction in progress")));
s->blockState = TBLOCK_ABORT_PENDING;
break;
case T_ConstraintsSetStmt:
- RequireTransactionChain(isTopLevel, "SET CONSTRAINTS");
+ WarnNoTransactionChain(isTopLevel, "SET CONSTRAINTS");
AfterTriggerSetState((ConstraintsSetStmt *) parsetree);
break;
case VAR_SET_VALUE:
case VAR_SET_CURRENT:
if (stmt->is_local)
- RequireTransactionChain(isTopLevel, "SET LOCAL");
+ WarnNoTransactionChain(isTopLevel, "SET LOCAL");
(void) set_config_option(stmt->name,
ExtractSetVariableArgs(stmt),
(superuser() ? PGC_SUSET : PGC_USERSET),
{
ListCell *head;
- RequireTransactionChain(isTopLevel, "SET TRANSACTION");
+ WarnNoTransactionChain(isTopLevel, "SET TRANSACTION");
foreach(head, stmt->args)
{
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("SET LOCAL TRANSACTION SNAPSHOT is not implemented")));
- RequireTransactionChain(isTopLevel, "SET TRANSACTION");
+ WarnNoTransactionChain(isTopLevel, "SET TRANSACTION");
Assert(IsA(con, A_Const));
Assert(nodeTag(&con->val) == T_String);
ImportSnapshot(strVal(&con->val));
break;
case VAR_SET_DEFAULT:
if (stmt->is_local)
- RequireTransactionChain(isTopLevel, "SET LOCAL");
+ WarnNoTransactionChain(isTopLevel, "SET LOCAL");
/* fall through */
case VAR_RESET:
if (strcmp(stmt->name, "transaction_isolation") == 0)
- RequireTransactionChain(isTopLevel, "RESET TRANSACTION");
+ WarnNoTransactionChain(isTopLevel, "RESET TRANSACTION");
(void) set_config_option(stmt->name,
NULL,
extern void AbortOutOfAnyTransaction(void);
extern void PreventTransactionChain(bool isTopLevel, const char *stmtType);
extern void RequireTransactionChain(bool isTopLevel, const char *stmtType);
+extern void WarnNoTransactionChain(bool isTopLevel, const char *stmtType);
extern bool IsInTransactionChain(bool isTopLevel);
extern void RegisterXactCallback(XactCallback callback, void *arg);
extern void UnregisterXactCallback(XactCallback callback, void *arg);
-- TRANSACTION STUFF
-- not in a xact
abort;
-NOTICE: there is no transaction in progress
+WARNING: there is no transaction in progress
-- not in a xact
end;
WARNING: there is no transaction in progress
-- SET LOCAL has no effect outside of a transaction
SET LOCAL vacuum_cost_delay TO 50;
-ERROR: SET LOCAL can only be used in transaction blocks
+WARNING: SET LOCAL can only be used in transaction blocks
SHOW vacuum_cost_delay;
vacuum_cost_delay
-------------------
(1 row)
SET LOCAL datestyle = 'SQL';
-ERROR: SET LOCAL can only be used in transaction blocks
+WARNING: SET LOCAL can only be used in transaction blocks
SHOW datestyle;
DateStyle
-----------