Skip to content

Commit 046f497

Browse files
committed
deallocateInvalidatedCachedStatements now runs in transactions
#1847
1 parent 8896bd6 commit 046f497

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

conn.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1354,7 +1354,7 @@ order by attnum`,
13541354
}
13551355

13561356
func (c *Conn) deallocateInvalidatedCachedStatements(ctx context.Context) error {
1357-
if c.pgConn.TxStatus() != 'I' {
1357+
if txStatus := c.pgConn.TxStatus(); txStatus != 'I' && txStatus != 'T' {
13581358
return nil
13591359
}
13601360

conn_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -1369,3 +1369,42 @@ func TestConnDeallocateInvalidatedCachedStatementsWhenCanceled(t *testing.T) {
13691369
require.EqualValues(t, 1, n)
13701370
})
13711371
}
1372+
1373+
// https://github.com/jackc/pgx/issues/1847
1374+
func TestConnDeallocateInvalidatedCachedStatementsInTransactionWithBatch(t *testing.T) {
1375+
t.Parallel()
1376+
1377+
ctx := context.Background()
1378+
1379+
connString := os.Getenv("PGX_TEST_DATABASE")
1380+
config := mustParseConfig(t, connString)
1381+
config.DefaultQueryExecMode = pgx.QueryExecModeCacheStatement
1382+
config.StatementCacheCapacity = 2
1383+
1384+
conn, err := pgx.ConnectConfig(ctx, config)
1385+
require.NoError(t, err)
1386+
1387+
tx, err := conn.Begin(ctx)
1388+
require.NoError(t, err)
1389+
defer tx.Rollback(ctx)
1390+
1391+
_, err = tx.Exec(ctx, "select $1::int + 1", 1)
1392+
require.NoError(t, err)
1393+
1394+
_, err = tx.Exec(ctx, "select $1::int + 2", 1)
1395+
require.NoError(t, err)
1396+
1397+
// This should invalidate the first cached statement.
1398+
_, err = tx.Exec(ctx, "select $1::int + 3", 1)
1399+
require.NoError(t, err)
1400+
1401+
batch := &pgx.Batch{}
1402+
batch.Queue("select $1::int + 1", 1)
1403+
err = tx.SendBatch(ctx, batch).Close()
1404+
require.NoError(t, err)
1405+
1406+
err = tx.Rollback(ctx)
1407+
require.NoError(t, err)
1408+
1409+
ensureConnValid(t, conn)
1410+
}

0 commit comments

Comments
 (0)