2
0

StatementErrored does not need context nor return an error

This commit is contained in:
Jack Christensen
2020-11-11 15:52:59 -06:00
parent 426124b32f
commit cba610c245
3 changed files with 11 additions and 20 deletions
+4 -10
View File
@@ -88,24 +88,18 @@ func (c *LRU) Clear(ctx context.Context) error {
return nil return nil
} }
func (c *LRU) StatementErrored(ctx context.Context, sql string, err error) error { func (c *LRU) StatementErrored(sql string, err error) {
pgErr, ok := err.(*pgconn.PgError) pgErr, ok := err.(*pgconn.PgError)
if !ok { if !ok {
// we don't know how to handle this error return
return nil
} }
isInvalidCachedPlanError := pgErr.Severity == "ERROR" && isInvalidCachedPlanError := pgErr.Severity == "ERROR" &&
pgErr.Code == "0A000" && pgErr.Code == "0A000" &&
pgErr.Message == "cached plan must not change result type" pgErr.Message == "cached plan must not change result type"
if !isInvalidCachedPlanError { if isInvalidCachedPlanError {
// only flush if a plan has been changed out from under us c.stmtsToClear = append(c.stmtsToClear, sql)
return nil
} }
c.stmtsToClear = append(c.stmtsToClear, sql)
return nil
} }
func (c *LRU) clearStmt(ctx context.Context, sql string) error { func (c *LRU) clearStmt(ctx context.Context, sql string) error {
+3 -4
View File
@@ -89,8 +89,7 @@ func TestLRUStmtInvalidation(t *testing.T) {
require.EqualValues(t, 1, cache.Len()) require.EqualValues(t, 1, cache.Len())
require.ElementsMatch(t, []string{"select 1"}, fetchServerStatements(t, ctx, conn)) require.ElementsMatch(t, []string{"select 1"}, fetchServerStatements(t, ctx, conn))
err = cache.StatementErrored(ctx, "select 1", fakeInvalidCachePlanError) cache.StatementErrored("select 1", fakeInvalidCachePlanError)
require.NoError(t, err)
_, err = cache.Get(ctx, "select 2") _, err = cache.Get(ctx, "select 2")
require.NoError(t, err) require.NoError(t, err)
require.EqualValues(t, 1, cache.Len()) require.EqualValues(t, 1, cache.Len())
@@ -117,7 +116,7 @@ func TestLRUStmtInvalidation(t *testing.T) {
require.Error(t, res.Close()) require.Error(t, res.Close())
require.Equal(t, byte('E'), conn.TxStatus()) require.Equal(t, byte('E'), conn.TxStatus())
err = cache.StatementErrored(ctx, "select 1", fakeInvalidCachePlanError) cache.StatementErrored("select 1", fakeInvalidCachePlanError)
require.EqualValues(t, 1, cache.Len()) require.EqualValues(t, 1, cache.Len())
res = conn.Exec(ctx, "rollback") res = conn.Exec(ctx, "rollback")
@@ -156,7 +155,7 @@ func TestLRUStmtInvalidationIntegration(t *testing.T) {
result = conn.ExecPrepared(ctx, sd1.Name, nil, nil, nil).Read() result = conn.ExecPrepared(ctx, sd1.Name, nil, nil, nil).Read()
require.EqualError(t, result.Err, "ERROR: cached plan must not change result type (SQLSTATE 0A000)") require.EqualError(t, result.Err, "ERROR: cached plan must not change result type (SQLSTATE 0A000)")
cache.StatementErrored(ctx, sql, result.Err) cache.StatementErrored(sql, result.Err)
sd2, err := cache.Get(ctx, sql) sd2, err := cache.Get(ctx, sql)
require.NoError(t, err) require.NoError(t, err)
+4 -6
View File
@@ -21,12 +21,10 @@ type Cache interface {
Clear(ctx context.Context) error Clear(ctx context.Context) error
// StatementErrored informs the cache that the given statement resulted in an error when it // StatementErrored informs the cache that the given statement resulted in an error when it
// was last used against the database. In some cases, this will cause the cache to flush // was last used against the database. In some cases, this will cause the cache to maer that
// the statement from the cache. It will only do so when the underlying `*pgconn.PgConn` // statement as bad. The bad statement will instead be flushed during the next call to Get
// is not currently in a transaction. If the connection is in the middle of a transaction, // that occurs outside of a failed transaction.
// the bad statement will instead be flushed during the next call to Get that occurrs outside StatementErrored(sql string, err error)
// of a transaction.
StatementErrored(ctx context.Context, sql string, err error) error
// Len returns the number of cached prepared statement descriptions. // Len returns the number of cached prepared statement descriptions.
Len() int Len() int