Skip to content

sql: assertion failure caused by routine with exception handler #156412

@stevendanna

Description

@stevendanna

Describe the problem

The following is a slightly modified case from plpqsql_block:

CREATE TABLE t122278 (pk INT PRIMARY KEY);
CREATE FUNCTION f(n INT) RETURNS INT AS $$
  BEGIN
    BEGIN
      IF n = 0 THEN
        RETURN 1 // 0;
      END IF;
    EXCEPTION
      WHEN division_by_zero THEN
        RETURN (SELECT 100 + count(*) FROM t122278);
    END;
    RETURN 1 // 0;
  EXCEPTION
    WHEN division_by_zero THEN
      RETURN (SELECT 200 + count(*) FROM t122278);
  END
$$ LANGUAGE PLpgSQL;

BEGIN;
INSERT INTO t122278 values (1);
SELECT f(0);
COMMIT;

On master, it will fail with the following assertion failed error:

ERROR: internal error: read sequence number 3 but sequence number is ignored [{3 3}] after savepoint rollback
SQLSTATE: XX000
DETAIL: stack trace:
pkg/kv/kvclient/kvcoord/txn_interceptor_seq_num_allocator.go:185: checkReadSeqNotIgnoredLocked()
pkg/kv/kvclient/kvcoord/txn_interceptor_seq_num_allocator.go:82: SendLocked()
pkg/kv/kvclient/kvcoord/txn_interceptor_heartbeater.go:266: SendLocked()
pkg/kv/kvclient/kvcoord/txn_coord_sender.go:570: Send()
pkg/kv/db.go:1167: sendUsingSender()
pkg/kv/txn.go:1381: Send()
pkg/sql/row/kv_batch_fetcher.go:283: func2()
pkg/sql/row/kv_batch_fetcher.go:646: fetch()
pkg/sql/row/kv_batch_fetcher.go:934: nextBatch()
pkg/sql/row/kv_batch_fetcher.go:801: NextBatch()
pkg/sql/row/kv_fetcher.go:314: nextKV()
pkg/sql/row/kv_fetcher.go:335: NextKV()
pkg/sql/colfetcher/cfetcher.go:775: NextBatch()
pkg/sql/colfetcher/colbatch_scan.go:240: Next()
pkg/sql/colexec/colexecutils/cancel_checker.go:54: Next()
pkg/sql/colexec/count.go:46: Next()
pkg/sql/colexec/colexecutils/operator.go:152: Next()
bazel-out/darwin_arm64-fastbuild/bin/pkg/sql/colexec/colexecprojconst/proj_const_right_ops.eg.go:4037: Next()
pkg/sql/colexec/colexecbase/simple_project.go:113: Next()
pkg/sql/colexec/materializer.go:246: next()
pkg/sql/colexec/materializer.go:271: nextAdapter()
pkg/sql/colexecerror/error.go:162: CatchVectorizedRuntimeError()
pkg/sql/colexec/materializer.go:277: Next()
pkg/sql/colflow/flow_coordinator.go:119: next()
pkg/sql/colflow/flow_coordinator.go:136: nextAdapter()
pkg/sql/colexecerror/error.go:162: CatchVectorizedRuntimeError()
pkg/sql/colflow/flow_coordinator.go:141: Next()
pkg/sql/execinfra/base.go:193: Run()
pkg/sql/execinfra/processorsbase.go:752: Run()
pkg/sql/flowinfra/flow.go:574: Run()
pkg/sql/colflow/vectorized_flow.go:301: Run()
pkg/sql/distsql_running.go:1072: Run()

HINT: You have encountered an unexpected error.

Please check the public issue tracker to check whether this problem is
already tracked. If you cannot find it there, please report the error
with details by creating a new issue.

If you would rather not post publicly, please contact us directly
using the support form.

We appreciate your feedback

The most proximate cause is that when Stepping is enabled, RollbackSavepoint puts the transaction in a state where the readSeq is part of the ignored set, expecting that the caller will Step() the transaction.

However, in this case, no future step is going to happen before we try to send another batch using the transaction.

It isn't clear to me is what we should be doing in this case, so I've assigned this T-sql-queries for their opinion.

Related: #156366 which would make it even easier to hit this case and is where I encountered the bug initially.

Jira issue: CRDB-55988

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCode not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.P-3Issues/test failures with no fix SLAT-sql-queriesSQL Queries Teambranch-masterFailures and bugs on the master branch.

    Type

    No type

    Projects

    Status

    Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions