MCPcopy
hub / github.com/jackc/pgx / TestStmtCacheInvalidationExec

Function TestStmtCacheInvalidationExec

conn_test.go:1531–1574  ·  view source on GitHub ↗

https://github.com/jackc/pgx/issues/2442

(t *testing.T)

Source from the content-addressed store, hash-verified

1529
1530// https://github.com/jackc/pgx/issues/2442
1531func TestStmtCacheInvalidationExec(t *testing.T) {
1532 ctx := context.Background()
1533
1534 conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE"))
1535 defer closeConn(t, conn)
1536
1537 if conn.PgConn().ParameterStatus("crdb_version") != "" {
1538 t.Skip("CockroachDB does not support column column type from int to bool")
1539 }
1540
1541 // create a table and fill it with some data
1542 _, err := conn.Exec(ctx, `
1543 DROP TABLE IF EXISTS drop_cols;
1544 CREATE TABLE drop_cols (
1545 id SERIAL PRIMARY KEY NOT NULL,
1546 f1 int NOT NULL,
1547 f2 int NOT NULL
1548 );
1549 `)
1550 require.NoError(t, err)
1551
1552 insertSQL := "INSERT INTO drop_cols (f1, f2) VALUES ($1, $2)"
1553 // This query will populate the statement cache.
1554 _, err = conn.Exec(ctx, insertSQL, 1, 2)
1555 require.NoError(t, err)
1556
1557 // Now, change the schema of the table out from under the statement, making it invalid.
1558 _, err = conn.Exec(ctx, "ALTER TABLE drop_cols ALTER COLUMN f1 TYPE boolean USING f1::boolean")
1559 require.NoError(t, err)
1560
1561 // We must get an error the first time we try to re-execute a bad statement.
1562 // It is up to the application to determine if it wants to try again. We punt to
1563 // the application because there is no clear recovery path in the case of failed transactions
1564 // or batch operations and because automatic retry is tricky and we don't want to get
1565 // it wrong at such an important layer of the stack.
1566 _, err = conn.Exec(ctx, insertSQL, true, 2)
1567 require.ErrorContains(t, err, "failed to encode args[0]")
1568
1569 // On retry, the statement should have been flushed from the cache.
1570 _, err = conn.Exec(ctx, insertSQL, true, 2)
1571 require.NoError(t, err)
1572
1573 ensureConnValid(t, conn)
1574}
1575
1576func TestInsertDurationInterval(t *testing.T) {
1577 ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)

Callers

nothing calls this directly

Calls 6

mustConnectStringFunction · 0.85
ParameterStatusMethod · 0.80
PgConnMethod · 0.80
closeConnFunction · 0.70
ensureConnValidFunction · 0.70
ExecMethod · 0.65

Tested by

no test coverage detected