Skip to content

Commit

Permalink
deallocateInvalidatedCachedStatements now runs in transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
jackc committed Feb 24, 2024
1 parent 8896bd6 commit 046f497
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ order by attnum`,
}

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

Expand Down
39 changes: 39 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1369,3 +1369,42 @@ func TestConnDeallocateInvalidatedCachedStatementsWhenCanceled(t *testing.T) {
require.EqualValues(t, 1, n)
})
}

// https://github.com/jackc/pgx/issues/1847
func TestConnDeallocateInvalidatedCachedStatementsInTransactionWithBatch(t *testing.T) {
t.Parallel()

ctx := context.Background()

connString := os.Getenv("PGX_TEST_DATABASE")
config := mustParseConfig(t, connString)
config.DefaultQueryExecMode = pgx.QueryExecModeCacheStatement
config.StatementCacheCapacity = 2

conn, err := pgx.ConnectConfig(ctx, config)
require.NoError(t, err)

tx, err := conn.Begin(ctx)
require.NoError(t, err)
defer tx.Rollback(ctx)

_, err = tx.Exec(ctx, "select $1::int + 1", 1)
require.NoError(t, err)

_, err = tx.Exec(ctx, "select $1::int + 2", 1)
require.NoError(t, err)

// This should invalidate the first cached statement.
_, err = tx.Exec(ctx, "select $1::int + 3", 1)
require.NoError(t, err)

batch := &pgx.Batch{}
batch.Queue("select $1::int + 1", 1)
err = tx.SendBatch(ctx, batch).Close()
require.NoError(t, err)

err = tx.Rollback(ctx)
require.NoError(t, err)

ensureConnValid(t, conn)
}

0 comments on commit 046f497

Please sign in to comment.