Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 26993b2

Browse files
committed
AUTOCOMMIT mode is now an available backend GUC variable; setting it
to false provides more SQL-spec-compliant behavior than we had before. I am not sure that setting it false is actually a good idea yet; there is a lot of client-side code that will probably be broken by turning autocommit off. But it's a start. Loosely based on a patch by David Van Wie.
1 parent 549928d commit 26993b2

File tree

15 files changed

+126
-53
lines changed

15 files changed

+126
-53
lines changed

doc/src/sgml/release.sgml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/release.sgml,v 1.155 2002/08/30 00:28:40 tgl Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/release.sgml,v 1.156 2002/08/30 22:18:05 tgl Exp $
33
-->
44

55
<appendix id="release">
@@ -24,6 +24,7 @@ CDATA means the content is "SGML-free", so you can write without
2424
worries about funny characters.
2525
-->
2626
<literallayout><![CDATA[
27+
No-autocommit mode is available (set autocommit to off)
2728
Substantial improvements in functionality for functions returning sets
2829
Client libraries older than 6.3 no longer supported (version 0 protocol removed)
2930
PREPARE statement allows caching query plans for interactive statements

doc/src/sgml/runtime.sgml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/runtime.sgml,v 1.130 2002/08/30 16:50:49 momjian Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/runtime.sgml,v 1.131 2002/08/30 22:18:05 tgl Exp $
33
-->
44

55
<Chapter Id="runtime">
@@ -1135,6 +1135,29 @@ env PGOPTIONS='-c geqo=off' psql
11351135

11361136
<para>
11371137
<variablelist>
1138+
<varlistentry>
1139+
<term><varname>AUTOCOMMIT</varname> (<type>bool</type>)</term>
1140+
<indexterm><primary>autocommit</></>
1141+
<listitem>
1142+
<para>
1143+
If set to true, <productname>PostgreSQL</productname> will
1144+
automatically do a <command>COMMIT</> after each successful command
1145+
that is not inside an explicit transaction block (that is, unless a
1146+
<command>BEGIN</> with no matching <command>COMMIT</> has been
1147+
given).
1148+
If set to false, <productname>PostgreSQL</productname> will commit
1149+
the effects of commands only on receiving an explicit
1150+
<command>COMMIT</> command. This mode can also be thought of as
1151+
implicitly issuing <command>BEGIN</> whenever a command is received
1152+
and <productname>PostgreSQL</productname> is not already inside
1153+
a transaction block.
1154+
The default is true, for compatibility with historical
1155+
<productname>PostgreSQL</productname> behavior. But for maximum
1156+
compatibility with the SQL specification, set it to false.
1157+
</para>
1158+
</listitem>
1159+
</varlistentry>
1160+
11381161
<varlistentry>
11391162
<term><varname>AUSTRALIAN_TIMEZONES</varname> (<type>bool</type>)</term>
11401163
<indexterm><primary>Australian time zones</></>

src/backend/access/transam/xact.c

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/access/transam/xact.c,v 1.130 2002/08/06 02:36:33 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/access/transam/xact.c,v 1.131 2002/08/30 22:18:05 tgl Exp $
1212
*
1313
* NOTES
1414
* Transaction aborts can now occur two ways:
@@ -220,10 +220,15 @@ TransactionState CurrentTransactionState = &CurrentTransactionStateData;
220220
int DefaultXactIsoLevel = XACT_READ_COMMITTED;
221221
int XactIsoLevel;
222222

223+
bool autocommit = true;
224+
223225
int CommitDelay = 0; /* precommit delay in microseconds */
224226
int CommitSiblings = 5; /* number of concurrent xacts needed to
225227
* sleep */
226228

229+
230+
static bool suppressChain = false;
231+
227232
static void (*_RollbackFunc) (void *) = NULL;
228233
static void *_RollbackData = NULL;
229234

@@ -1149,13 +1154,24 @@ CleanupTransaction(void)
11491154

11501155
/* --------------------------------
11511156
* StartTransactionCommand
1157+
*
1158+
* preventChain, if true, forces autocommit behavior at the next
1159+
* CommitTransactionCommand call.
11521160
* --------------------------------
11531161
*/
11541162
void
1155-
StartTransactionCommand(void)
1163+
StartTransactionCommand(bool preventChain)
11561164
{
11571165
TransactionState s = CurrentTransactionState;
11581166

1167+
/*
1168+
* Remember if caller wants to prevent autocommit-off chaining.
1169+
* This is only allowed if not already in a transaction block.
1170+
*/
1171+
suppressChain = preventChain;
1172+
if (preventChain && s->blockState != TBLOCK_DEFAULT)
1173+
elog(ERROR, "StartTransactionCommand: can't prevent chain");
1174+
11591175
switch (s->blockState)
11601176
{
11611177
/*
@@ -1231,21 +1247,41 @@ StartTransactionCommand(void)
12311247

12321248
/* --------------------------------
12331249
* CommitTransactionCommand
1250+
*
1251+
* forceCommit = true forces autocommit behavior even when autocommit is off.
12341252
* --------------------------------
12351253
*/
12361254
void
1237-
CommitTransactionCommand(void)
1255+
CommitTransactionCommand(bool forceCommit)
12381256
{
12391257
TransactionState s = CurrentTransactionState;
12401258

12411259
switch (s->blockState)
12421260
{
12431261
/*
1244-
* if we aren't in a transaction block, we just do our usual
1245-
* transaction commit
1262+
* If we aren't in a transaction block, and we are doing
1263+
* autocommit, just do our usual transaction commit. But
1264+
* if we aren't doing autocommit, start a transaction block
1265+
* automatically by switching to INPROGRESS state. (We handle
1266+
* this choice here, and not earlier, so that an explicit BEGIN
1267+
* issued in autocommit-off mode won't issue strange warnings.)
1268+
*
1269+
* Autocommit mode is forced by either a true forceCommit parameter
1270+
* to me, or a true preventChain parameter to the preceding
1271+
* StartTransactionCommand call. This is needed so that commands
1272+
* like VACUUM can ensure that the right things happen.
12461273
*/
12471274
case TBLOCK_DEFAULT:
1248-
CommitTransaction();
1275+
if (autocommit || forceCommit || suppressChain)
1276+
CommitTransaction();
1277+
else
1278+
{
1279+
BeginTransactionBlock();
1280+
Assert(s->blockState == TBLOCK_INPROGRESS);
1281+
/* This code must match the TBLOCK_INPROGRESS case below: */
1282+
CommandCounterIncrement();
1283+
MemoryContextResetAndDeleteChildren(TransactionCommandContext);
1284+
}
12491285
break;
12501286

12511287
/*
@@ -1406,7 +1442,10 @@ BeginTransactionBlock(void)
14061442
s->blockState = TBLOCK_BEGIN;
14071443

14081444
/*
1409-
* do begin processing
1445+
* do begin processing. NOTE: if you put anything here, check that
1446+
* it behaves properly in both autocommit-on and autocommit-off modes.
1447+
* In the latter case we will already have done some work in the new
1448+
* transaction.
14101449
*/
14111450

14121451
/*

src/backend/bootstrap/bootparse.y

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $Header: /cvsroot/pgsql/src/backend/bootstrap/bootparse.y,v 1.50 2002/07/20 05:16:56 momjian Exp $
12+
* $Header: /cvsroot/pgsql/src/backend/bootstrap/bootparse.y,v 1.51 2002/08/30 22:18:05 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -55,15 +55,15 @@
5555
static void
5656
do_start()
5757
{
58-
StartTransactionCommand();
58+
StartTransactionCommand(true);
5959
elog(DEBUG3, "start transaction");
6060
}
6161

6262

6363
static void
6464
do_end()
6565
{
66-
CommitTransactionCommand();
66+
CommitTransactionCommand(true);
6767
elog(DEBUG3, "commit transaction");
6868
if (isatty(0))
6969
{

src/backend/bootstrap/bootstrap.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.138 2002/08/17 15:12:06 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.139 2002/08/30 22:18:05 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -448,7 +448,7 @@ BootstrapMain(int argc, char *argv[])
448448
SetProcessingMode(BootstrapProcessing);
449449

450450
/* clean up processing */
451-
StartTransactionCommand();
451+
StartTransactionCommand(true);
452452
cleanup();
453453

454454
/* not reached, here to make compiler happy */
@@ -821,7 +821,7 @@ cleanup()
821821
}
822822
if (boot_reldesc != NULL)
823823
closerel(NULL);
824-
CommitTransactionCommand();
824+
CommitTransactionCommand(true);
825825
proc_exit(Warnings);
826826
}
827827

src/backend/catalog/namespace.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Portions Copyright (c) 1994, Regents of the University of California
1414
*
1515
* IDENTIFICATION
16-
* $Header: /cvsroot/pgsql/src/backend/catalog/namespace.c,v 1.32 2002/08/29 00:17:02 tgl Exp $
16+
* $Header: /cvsroot/pgsql/src/backend/catalog/namespace.c,v 1.33 2002/08/30 22:18:05 tgl Exp $
1717
*
1818
*-------------------------------------------------------------------------
1919
*/
@@ -1610,11 +1610,11 @@ RemoveTempRelationsCallback(void)
16101610
{
16111611
/* Need to ensure we have a usable transaction. */
16121612
AbortOutOfAnyTransaction();
1613-
StartTransactionCommand();
1613+
StartTransactionCommand(true);
16141614

16151615
RemoveTempRelations(myTempNamespace);
16161616

1617-
CommitTransactionCommand();
1617+
CommitTransactionCommand(true);
16181618
}
16191619
}
16201620

src/backend/commands/async.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/commands/async.c,v 1.88 2002/08/05 03:29:16 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/commands/async.c,v 1.89 2002/08/30 22:18:05 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -400,9 +400,9 @@ Async_UnlistenOnExit(void)
400400
*/
401401
AbortOutOfAnyTransaction();
402402
/* Now we can do the unlisten */
403-
StartTransactionCommand();
403+
StartTransactionCommand(true);
404404
Async_UnlistenAll();
405-
CommitTransactionCommand();
405+
CommitTransactionCommand(true);
406406
}
407407

408408
/*
@@ -749,7 +749,7 @@ ProcessIncomingNotify(void)
749749

750750
notifyInterruptOccurred = 0;
751751

752-
StartTransactionCommand();
752+
StartTransactionCommand(true);
753753

754754
lRel = heap_openr(ListenerRelationName, AccessExclusiveLock);
755755
tdesc = RelationGetDescr(lRel);
@@ -803,7 +803,7 @@ ProcessIncomingNotify(void)
803803
*/
804804
heap_close(lRel, NoLock);
805805

806-
CommitTransactionCommand();
806+
CommitTransactionCommand(true);
807807

808808
/*
809809
* Must flush the notify messages to ensure frontend gets them

src/backend/commands/indexcmds.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/indexcmds.c,v 1.85 2002/08/29 15:56:20 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/indexcmds.c,v 1.86 2002/08/30 22:18:05 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -735,15 +735,16 @@ ReindexDatabase(const char *dbname, bool force, bool all)
735735
heap_close(relationRelation, AccessShareLock);
736736

737737
/* Now reindex each rel in a separate transaction */
738-
CommitTransactionCommand();
738+
CommitTransactionCommand(true);
739739
for (i = 0; i < relcnt; i++)
740740
{
741-
StartTransactionCommand();
741+
StartTransactionCommand(true);
742742
if (reindex_relation(relids[i], force))
743743
elog(NOTICE, "relation %u was reindexed", relids[i]);
744-
CommitTransactionCommand();
744+
CommitTransactionCommand(true);
745745
}
746-
StartTransactionCommand();
746+
/* Tell xact.c not to chain the upcoming commit */
747+
StartTransactionCommand(true);
747748

748749
MemoryContextDelete(private_context);
749750
}

0 commit comments

Comments
 (0)