Skip to content

Commit bcaca8e

Browse files
author
Konstantin Knizhnik
committed
Add support of dedicated backends
1 parent 46f9b96 commit bcaca8e

File tree

9 files changed

+51
-1
lines changed

9 files changed

+51
-1
lines changed

src/backend/access/transam/xact.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ bool XactDeferrable;
8484

8585
int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
8686

87+
/* Backend needs to preserve session context */
88+
bool is_dedicated_backend;
89+
bool allow_dedicated_backends; /* GUC */
90+
8791
/*
8892
* CheckXidAlive is a xid value pointing to a possibly ongoing (sub)
8993
* transaction. Currently, it is used in logical decoding. It's possible
@@ -4952,7 +4956,7 @@ TransactionBlockStatusCode(void)
49524956
{
49534957
case TBLOCK_DEFAULT:
49544958
case TBLOCK_STARTED:
4955-
return 'I'; /* idle --- not in transaction */
4959+
return is_dedicated_backend && allow_dedicated_backends ? 'T' : 'I'; /* idle --- not in transaction */
49564960
case TBLOCK_BEGIN:
49574961
case TBLOCK_SUBBEGIN:
49584962
case TBLOCK_INPROGRESS:

src/backend/commands/portalcmds.c

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ PerformCursorOpen(ParseState *pstate, DeclareCursorStmt *cstmt, ParamListInfo pa
5959
(errcode(ERRCODE_INVALID_CURSOR_NAME),
6060
errmsg("invalid cursor name: must not be empty")));
6161

62+
if (cstmt->options & CURSOR_OPT_HOLD)
63+
is_dedicated_backend = true; /* cursors are not compatible with builtin connection pooler */
64+
6265
/*
6366
* If this is a non-holdable cursor, we require that this statement has
6467
* been executed inside a transaction block (or else, it would have no

src/backend/commands/sequence.c

+15
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,19 @@ DefineSequence(ParseState *pstate, CreateSeqStmt *seq)
245245
heap_freetuple(tuple);
246246
table_close(rel, RowExclusiveLock);
247247

248+
/*
249+
* TODO:
250+
* Using currval() may cause incorrect behavior with connection pooler.
251+
* Unfortunately marking backend as tainted in currval() is too late.
252+
* This is why it is done in nextval(), although it is not strictly required, because
253+
* nextval() may be not followed by currval().
254+
* But currval() may be not preceded by nextval().
255+
* To make regression tests passed, backend is also marker as tainted when it creates
256+
* sequence. Certainly it is just temporary workaround, because sequence may be created
257+
* in one backend and accessed in another.
258+
*/
259+
is_dedicated_backend = true; /* in case of using currval() */
260+
248261
return address;
249262
}
250263

@@ -611,6 +624,8 @@ nextval(PG_FUNCTION_ARGS)
611624
*/
612625
relid = RangeVarGetRelid(sequence, NoLock, false);
613626

627+
is_dedicated_backend = true; /* in case of using currval() */
628+
614629
PG_RETURN_INT64(nextval_internal(relid, true));
615630
}
616631

src/backend/commands/tablecmds.c

+4
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,10 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
733733
(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
734734
errmsg("ON COMMIT can only be used on temporary tables")));
735735

736+
if (stmt->relation->relpersistence == RELPERSISTENCE_TEMP
737+
&& stmt->oncommit != ONCOMMIT_DROP)
738+
is_dedicated_backend = true;
739+
736740
if (stmt->partspec != NULL)
737741
{
738742
if (relkind != RELKIND_RELATION)

src/backend/storage/lmgr/lock.c

+3
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,10 @@ LockAcquireExtended(const LOCKTAG *locktag,
822822

823823
/* Identify owner for lock */
824824
if (sessionLock)
825+
{
825826
owner = NULL;
827+
is_dedicated_backend = true;
828+
}
826829
else
827830
owner = CurrentResourceOwner;
828831

src/backend/utils/adt/lockfuncs.c

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "postgres.h"
1414

1515
#include "access/htup_details.h"
16+
#include "access/xact.h"
1617
#include "funcapi.h"
1718
#include "miscadmin.h"
1819
#include "storage/predicate_internals.h"
@@ -611,12 +612,14 @@ pg_safe_snapshot_blocking_pids(PG_FUNCTION_ARGS)
611612
* field4: 1 if using an int8 key, 2 if using 2 int4 keys
612613
*/
613614
#define SET_LOCKTAG_INT64(tag, key64) \
615+
is_dedicated_backend = true; \
614616
SET_LOCKTAG_ADVISORY(tag, \
615617
MyDatabaseId, \
616618
(uint32) ((key64) >> 32), \
617619
(uint32) (key64), \
618620
1)
619621
#define SET_LOCKTAG_INT32(tag, key1, key2) \
622+
is_dedicated_backend = true; \
620623
SET_LOCKTAG_ADVISORY(tag, MyDatabaseId, key1, key2, 2)
621624

622625
/*

src/backend/utils/misc/guc_funcs.c

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ ExecSetVariableStmt(VariableSetStmt *stmt, bool isTopLevel)
5353
(errcode(ERRCODE_INVALID_TRANSACTION_STATE),
5454
errmsg("cannot set parameters during a parallel operation")));
5555

56+
if (!stmt->is_local)
57+
is_dedicated_backend = true;
58+
5659
switch (stmt->kind)
5760
{
5861
case VAR_SET_VALUE:

src/backend/utils/misc/guc_tables.c

+12
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,18 @@ struct config_bool ConfigureNamesBool[] =
802802
true,
803803
NULL, NULL, NULL
804804
},
805+
806+
{
807+
{"allow_dedicated_backends", PGC_USERSET, CLIENT_CONN_OTHER,
808+
gettext_noop("Prevent connection pooler from reusing backends requiring session semantic."),
809+
NULL,
810+
GUC_EXPLAIN
811+
},
812+
&allow_dedicated_backends,
813+
true,
814+
NULL, NULL, NULL
815+
},
816+
805817
{
806818
{"enable_seqscan", PGC_USERSET, QUERY_TUNING_METHOD,
807819
gettext_noop("Enables the planner's use of sequential-scan plans."),

src/include/access/xact.h

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ extern PGDLLIMPORT bool XactReadOnly;
5858
/* flag for logging statements in this transaction */
5959
extern PGDLLIMPORT bool xact_is_sampled;
6060

61+
/* Backend needs to preserve session context */
62+
extern PGDLLIMPORT bool is_dedicated_backend;
63+
extern bool allow_dedicated_backends;
6164
/*
6265
* Xact is deferrable -- only meaningful (currently) for read only
6366
* SERIALIZABLE transactions

0 commit comments

Comments
 (0)