Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: prepared statements over-account for memory on query cache hits #98071

Open
DrewKimball opened this issue Mar 6, 2023 · 1 comment
Open
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-support Would prevent or help troubleshoot a customer escalation - bugs, missing observability/tooling, docs P-3 Issues/test failures with no fix SLA T-sql-queries SQL Queries Team

Comments

@DrewKimball
Copy link
Collaborator

DrewKimball commented Mar 6, 2023

Cached prepared statements track their own memory usage, since they are potentially long-lived. This memory accounting includes the metadata needed for the prepared statement (e.g. SQL string, AST etc.) as well as the Memo data structured used by the optimizer to store the query plan. It is possible for the plan for a prepared statement to be kept in the query cache, in which case preparing the statement does not require construction of a new Memo data structure. However, the memory usage of this Memo is still registered with the prepared statement's memory accounting, which can cause a significant overestimate of SQL memory usage - in one customer issue, the reported SQL memory was 3x higher than the actual hardware memory usage.

See also #72581

Jira issue: CRDB-25064

@DrewKimball DrewKimball added the C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. label Mar 6, 2023
@blathers-crl blathers-crl bot added the T-sql-queries SQL Queries Team label Mar 6, 2023
@DrewKimball DrewKimball added the O-support Would prevent or help troubleshoot a customer escalation - bugs, missing observability/tooling, docs label Mar 6, 2023
@exalate-issue-sync exalate-issue-sync bot removed O-support Would prevent or help troubleshoot a customer escalation - bugs, missing observability/tooling, docs T-sql-queries SQL Queries Team labels Mar 6, 2023
@michae2
Copy link
Collaborator

michae2 commented Mar 18, 2023

Another (small) source of over-accounting:

// resetTo resets a namespace to equate another one (`to`). All the receiver's
// references are released and all the to's references are duplicated.
//
// An empty `to` can be passed in to deallocate everything.
//
// It can only return an error if we've reached the memory limit and had to make
// a copy of portals.
func (ns *prepStmtNamespace) resetTo(
ctx context.Context, to prepStmtNamespace, prepStmtsNamespaceMemAcc *mon.BoundAccount,
) error {
for name, p := range ns.prepStmts {
p.decRef(ctx)
delete(ns.prepStmts, name)
}
for name, p := range ns.portals {
p.close(ctx, prepStmtsNamespaceMemAcc, name)
delete(ns.portals, name)
}
for name, ps := range to.prepStmts {
ps.incRef(ctx)
ns.prepStmts[name] = ps
}
for name, p := range to.portals {
if err := p.accountForCopy(ctx, prepStmtsNamespaceMemAcc, name); err != nil {
return err
}
ns.portals[name] = p
}
return nil
}
does not un-account for prepared statements being rewound, so if we automatically retry a transaction with a PREPARE its memory will get counted multiple times.
EDIT: never mind, this is wrong

@michae2 michae2 removed their assignment Mar 28, 2023
@mgartner mgartner moved this to New Backlog in SQL Queries Jul 24, 2023
@michae2 michae2 added the O-support Would prevent or help troubleshoot a customer escalation - bugs, missing observability/tooling, docs label Dec 4, 2023
@lunevalex lunevalex added the T-sql-queries SQL Queries Team label Jan 10, 2024
@mgartner mgartner added the P-3 Issues/test failures with no fix SLA label Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-support Would prevent or help troubleshoot a customer escalation - bugs, missing observability/tooling, docs P-3 Issues/test failures with no fix SLA T-sql-queries SQL Queries Team
Projects
Status: Backlog
Development

No branches or pull requests

4 participants