diff --git a/conn.go b/conn.go index 72154325..5819611a 100644 --- a/conn.go +++ b/conn.go @@ -740,7 +740,7 @@ optionLoop: rows := c.getRows(ctx, sql, args) var err error - sd := c.preparedStatements[sql] + sd, explicitPreparedStatement := c.preparedStatements[sql] if sd != nil || mode == QueryExecModeCacheStatement || mode == QueryExecModeCacheDescribe || mode == QueryExecModeDescribeExec { if sd == nil { switch mode { @@ -806,7 +806,7 @@ optionLoop: resultFormats = c.eqb.resultFormats } - if mode == QueryExecModeCacheDescribe { + if !explicitPreparedStatement && mode == QueryExecModeCacheDescribe { rows.resultReader = c.pgConn.ExecParams(ctx, sql, c.eqb.paramValues, sd.ParamOIDs, c.eqb.paramFormats, resultFormats) } else { rows.resultReader = c.pgConn.ExecPrepared(ctx, sd.Name, c.eqb.paramValues, c.eqb.paramFormats, resultFormats) diff --git a/conn_test.go b/conn_test.go index 675fba17..5115d76e 100644 --- a/conn_test.go +++ b/conn_test.go @@ -472,6 +472,20 @@ func TestPrepareIdempotency(t *testing.T) { } } +func TestPrepareStatementCacheModes(t *testing.T) { + t.Parallel() + + pgxtest.RunWithQueryExecModes(context.Background(), t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) { + _, err := conn.Prepare(context.Background(), "test", "select $1::text") + require.NoError(t, err) + + var s string + err = conn.QueryRow(context.Background(), "test", "hello").Scan(&s) + require.NoError(t, err) + require.Equal(t, "hello", s) + }) +} + func TestListenNotify(t *testing.T) { t.Parallel()