sql: unexpectedly high cpu utilization on a single query, could not be cancelled
Describe the problem
A user encountered a query that resulted in unexpectedly high CPU utilization; issuing CANCEL QUERY did not kill it.
Decoded CPU profile:
(pprof) top 40
Showing nodes accounting for 25.33s, 81.32% of 31.15s total
Dropped 727 nodes (cum <= 0.16s)
Showing top 40 nodes out of 91
flat flat% sum% cum cum%
0 0% 0% 29.14s 93.55% github.com/cockroachdb/cockroach/pkg/sql/distsqlrun.(*ProcessorBase).Run
0 0% 0% 29.13s 93.52% github.com/cockroachdb/cockroach/pkg/sql/distsqlrun.(*joinReader).Next
0 0% 0% 29.13s 93.52% github.com/cockroachdb/cockroach/pkg/sql/distsqlrun.(*orderedAggregator).Next
0 0% 0% 29.13s 93.52% github.com/cockroachdb/cockroach/pkg/sql/distsqlrun.(*orderedAggregator).accumulateRows
0 0% 0% 29.13s 93.52% github.com/cockroachdb/cockroach/pkg/sql/distsqlrun.Run
0.28s 0.9% 0.9% 29.04s 93.23% github.com/cockroachdb/cockroach/pkg/sql/distsqlrun.(*joinReader).performLookup
0.58s 1.86% 2.76% 28.07s 90.11% github.com/cockroachdb/cockroach/pkg/sql/distsqlrun.(*joinerBase).render
0.25s 0.8% 3.56% 25.40s 81.54% github.com/cockroachdb/cockroach/pkg/sql/distsqlrun.(*exprHelper).evalFilter
0.29s 0.93% 4.49% 24.09s 77.34% github.com/cockroachdb/cockroach/pkg/sql/sqlbase.RunFilter
0.93s 2.99% 7.48% 23.80s 76.40% github.com/cockroachdb/cockroach/pkg/sql/sem/tree.(*AndExpr).Eval
1.62s 5.20% 12.68% 20.07s 64.43% github.com/cockroachdb/cockroach/pkg/sql/sem/tree.(*ComparisonExpr).Eval
0.39s 1.25% 13.93% 11.99s 38.49% github.com/cockroachdb/cockroach/pkg/sql/sem/tree.(*IndexedVar).Eval
0.50s 1.61% 15.54% 11.60s 37.24% github.com/cockroachdb/cockroach/pkg/sql/distsqlrun.(*exprHelper).IndexedVarEval
0.53s 1.70% 17.24% 10.99s 35.28% github.com/cockroachdb/cockroach/pkg/sql/sqlbase.(*EncDatum).EnsureDecoded
2.08s 6.68% 23.92% 9.91s 31.81% github.com/cockroachdb/cockroach/pkg/sql/sqlbase.DecodeTableKey
2.01s 6.45% 30.37% 5.86s 18.81% runtime.getitab
1.11s 3.56% 33.93% 4.89s 15.70% runtime.assertI2I
3.55s 11.40% 45.33% 3.85s 12.36% runtime.(*itabTableType).find
1.53s 4.91% 50.24% 2.73s 8.76% github.com/cockroachdb/cockroach/pkg/util/uuid.FromBytes
0.65s 2.09% 52.33% 2.73s 8.76% runtime.convI2I
0.50s 1.61% 53.93% 2.62s 8.41% github.com/cockroachdb/cockroach/pkg/util/encoding.DecodeBytesAscending
1.15s 3.69% 57.62% 2.12s 6.81% github.com/cockroachdb/cockroach/pkg/util/encoding.decodeBytesInternal
0.53s 1.70% 59.33% 2.10s 6.74% runtime.typedslicecopy
1.86s 5.97% 65.30% 1.86s 5.97% runtime.memmove
0.72s 2.31% 67.61% 1.36s 4.37% github.com/cockroachdb/cockroach/pkg/sql/sqlbase.(*DatumAlloc).NewDUuid (inline)
0.02s 0.064% 67.67% 1.25s 4.01% runtime.systemstack
0.58s 1.86% 69.53% 1.20s 3.85% github.com/cockroachdb/cockroach/vendor/github.com/satori/go%2euuid.FromBytes
0.25s 0.8% 70.34% 1.09s 3.50% github.com/cockroachdb/cockroach/pkg/sql/sem/tree.cmpOpScalarEQFn
0.18s 0.58% 70.91% 1.05s 3.37% runtime.mallocgc
0.23s 0.74% 71.65% 0.84s 2.70% github.com/cockroachdb/cockroach/pkg/sql/sem/tree.cmpOpScalarFn
0.03s 0.096% 71.75% 0.78s 2.50% runtime.makeslice
0.29s 0.93% 72.68% 0.62s 1.99% github.com/cockroachdb/cockroach/vendor/github.com/satori/go%2euuid.(*UUID).UnmarshalBinary
0.58s 1.86% 74.54% 0.58s 1.86% runtime.indexbytebody
0.20s 0.64% 75.18% 0.56s 1.80% github.com/cockroachdb/cockroach/pkg/util/encoding.DecodeIfNull
0.47s 1.51% 76.69% 0.56s 1.80% runtime.ifaceeq
0.53s 1.70% 78.39% 0.55s 1.77% github.com/cockroachdb/cockroach/pkg/sql/sem/tree.(*EvalContext).PushIVarContainer (inline)
0.29s 0.93% 79.33% 0.54s 1.73% github.com/cockroachdb/cockroach/pkg/sql/sqlbase.(*ColumnType).ToDatumType
0 0% 79.33% 0.54s 1.73% github.com/cockroachdb/cockroach/pkg/util/stop.(*Stopper).RunWorker.func1
0.13s 0.42% 79.74% 0.52s 1.67% github.com/cockroachdb/cockroach/pkg/sql/sem/tree.(*DUuid).Compare
0.49s 1.57% 81.32% 0.50s 1.61% github.com/cockroachdb/cockroach/pkg/sql/sem/tree.(*EvalContext).PopIVarContainer (inline)
To Reproduce
This is the query that caused the issue:
SELECT count(userpositi0_.region) AS col_0_0_
FROM t1 AS userpositi0_
INNER JOIN "position" AS positionen1_ ON ((userpositi0_.position_region = positionen1_.region)
AND (userpositi0_.position_tenant_id = positionen1_.tenant_id))
AND (userpositi0_.position_id = positionen1_.uid)
WHERE (((userpositi0_.tenant_id = '<UUID>')
AND (userpositi0_.region = '<REGION>'))
AND (positionen1_.external_id = '<EXTERNAL_ID>'))
AND ((positionen1_.region,
positionen1_.tenant_id) = ('<REGION>',
'<UUID>'));
If possible, provide steps to reproduce the behavior:
- Run the Query
- Run
CANCEL QUERY <QUERY_ID> - Check the CPU usage graph and Disk Write Ops
Expected behavior The expected behaviour is that the cancel command should stop the query.
Environment:
- CockroachDB version: 2.1.x
Additional context What was the impact? CPU usage would spike up to 100% because of query not canceling.
@roncrdb we need the schema to be able to analyze this, or at least EXPLAIN and EXPLAIN(DISTSQL). How do you know it was spilling to disk?
And if you've collected a CPU profile it'll be very useful to attach it to the issue as well - the summary you provided is fine but having the profile will help analysis even further.
@jordanlewis
You can find the profile here and I've asked for the schema, but in the meantime, here is the explain
explain SELECT count(userpositi0_.region) AS col_0_0_ FROM user_position_rel AS userpositi0_ INNER JOIN "position" AS positionen1_ ON ((userpositi0_.position_region = positionen1_.region) AND (userpositi0_.position_tenant_id = positionen1_.tenant_id)) AND (userpositi0_.position_id = positionen1_.uid) WHERE (((userpositi0_.tenant_id = '<UUID>') AND (userpositi0_.region = '<region>')) AND (positionen1_.external_id = '81047842')) AND ((positionen1_.region, positionen1_.tenant_id) = ('<region>', '<UUID>'));
tree | field | description
+-----------------------+-------------+-----------------------------------------------------------------------------------------------------------------------------------------+
group | |
│ | aggregate 0 | count(region)
│ | scalar |
└── render | |
└── lookup-join | |
│ | type | inner
│ | pred | (((@5 = @8) AND (@6 = '<region>')) AND (@7 = '<UUUID>')) AND (@9 = '81047842')
├── scan | |
│ | table | user_position_rel@idx_user_position_position_region_tenant_position_id
│ | spans | /"<region>"/"\xf9\xa7\xf2\xa0\f\xb6A.\x80J\x06\xdc\xc0a\xa1\x81"-/"<region>"/"\xf9\xa7\xf2\xa0\f\xb6A.\x80J\x06\xdc\xc0a\xa1\x81"/PrefixEnd
│ | filter | (tenant_id = '<UUID>') AND (region = '<region>')
└── scan | |
| table | position@idx_position_region_tenant_id_external_id
This query doesn't spill to disk (the hash aggregator is not equipped to do so).
Zendesk ticket #3055 has been linked to this issue.
Closing as Zendesk ticket #3055 is closed.