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

Function TestStmtCacheInvalidationConn

conn_test.go:1218–1278  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1216}
1217
1218func TestStmtCacheInvalidationConn(t *testing.T) {
1219 ctx := context.Background()
1220
1221 conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE"))
1222 defer closeConn(t, conn)
1223
1224 // create a table and fill it with some data
1225 _, err := conn.Exec(ctx, `
1226 DROP TABLE IF EXISTS drop_cols;
1227 CREATE TABLE drop_cols (
1228 id SERIAL PRIMARY KEY NOT NULL,
1229 f1 int NOT NULL,
1230 f2 int NOT NULL
1231 );
1232 `)
1233 require.NoError(t, err)
1234 _, err = conn.Exec(ctx, "INSERT INTO drop_cols (f1, f2) VALUES (1, 2)")
1235 require.NoError(t, err)
1236
1237 getSQL := "SELECT * FROM drop_cols WHERE id = $1"
1238
1239 // This query will populate the statement cache. We don't care about the result.
1240 rows, err := conn.Query(ctx, getSQL, 1)
1241 require.NoError(t, err)
1242 rows.Close()
1243 require.NoError(t, rows.Err())
1244
1245 // Now, change the schema of the table out from under the statement, making it invalid.
1246 _, err = conn.Exec(ctx, "ALTER TABLE drop_cols DROP COLUMN f1")
1247 require.NoError(t, err)
1248
1249 // We must get an error the first time we try to re-execute a bad statement.
1250 // It is up to the application to determine if it wants to try again. We punt to
1251 // the application because there is no clear recovery path in the case of failed transactions
1252 // or batch operations and because automatic retry is tricky and we don't want to get
1253 // it wrong at such an important layer of the stack.
1254 rows, err = conn.Query(ctx, getSQL, 1)
1255 require.NoError(t, err)
1256 rows.Next()
1257 nextErr := rows.Err()
1258 rows.Close()
1259 for _, err := range []error{nextErr, rows.Err()} {
1260 if err == nil {
1261 t.Fatal(`expected "cached plan must not change result type": no error`)
1262 }
1263 if !strings.Contains(err.Error(), "cached plan must not change result type") {
1264 t.Fatalf(`expected "cached plan must not change result type", got: "%s"`, err.Error())
1265 }
1266 }
1267
1268 // On retry, the statement should have been flushed from the cache.
1269 rows, err = conn.Query(ctx, getSQL, 1)
1270 require.NoError(t, err)
1271 rows.Next()
1272 err = rows.Err()
1273 require.NoError(t, err)
1274 rows.Close()
1275 require.NoError(t, rows.Err())

Callers

nothing calls this directly

Calls 9

mustConnectStringFunction · 0.85
closeConnFunction · 0.70
ensureConnValidFunction · 0.70
ExecMethod · 0.65
QueryMethod · 0.65
CloseMethod · 0.65
ErrMethod · 0.65
NextMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected