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: Auto-commit before DDL in BIND #142034

Conversation

spilchen
Copy link
Contributor

Several parts of conn_executor_*go already auto-commit transactions before handling DDL commands. However, we were missing the auto-commit for the BIND command. Without it, binding a prepared statement could trigger the auto-commit after the bind, which closes the portal and leads to unknown portal errors.

Epic: none
Release note (bug fix): Fixed a bug when running with autocommit_before_ddl that could cause a runtime error when binding a previously prepared DDL statement.

Fixes #141576
Fixes #141881

@spilchen spilchen self-assigned this Feb 26, 2025
@spilchen spilchen requested review from a team as code owners February 26, 2025 14:55
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @spilchen)


pkg/sql/pgwire/testdata/pgtest/portals_crbugs line 383 at r1 (raw file):

{"Type":"ReadyForQuery","TxStatus":"I"}

send

super nit: this portals_crbugs file is intended to test expected differences from PG in how CRDB handles portal exhaustion.

i think a more specific test file for this would be pkg/sql/pgwire/testdata/pgtest/schema_changes_implicit_txn_with_bind (though we could rename the file slightly so it's not too specific/verbose).


pkg/sql/pgwire/testdata/pgtest/portals_crbugs line 432 at r1 (raw file):

----

until

nit: we could have two different until sections: one for postgres and one for cockroachdb. they can be written like this:

this allows the test to pass against postgres (run with ./dev test pkg/sql/pgwire -f=TestPGTest --test-args='-addr localhost:5432 -user user_name') as well as cockroachdb.

(this isn't needed if the test is defined in this file, since the file header has only crdb at the top, but this will be useful if we move this test to a different file)

until noncrdb_only
ReadyForQuery
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"BEGIN"}
{"Type":"ReadyForQuery","TxStatus":"T"}
{"Type":"BindComplete"}
{"Type":"CommandComplete","CommandTag":"DROP TABLE"}
{"Type":"ReadyForQuery","TxStatus":"T"}

until crdb_only
ReadyForQuery
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"BEGIN"}
{"Type":"ReadyForQuery","TxStatus":"T"}
{"Severity":"NOTICE","SeverityUnlocalized":"NOTICE","Code":"00000","Message":"auto-committing transaction before processing DDL due to autocommit_before_ddl setting","Detail":"","Hint":"","Position":0,"InternalPosition":0,"InternalQuery":"","Where":"","SchemaName":"","TableName":"","ColumnName":"","DataTypeName":"","ConstraintName":"","File":"conn_executor_ddl.go","Line":0,"Routine":"maybeAutoCommitBeforeDDL","UnknownFields":null}
{"Type":"BindComplete"}
{"Type":"CommandComplete","CommandTag":"DROP TABLE"}
{"Type":"ReadyForQuery","TxStatus":"I"}

Several parts of conn_executor_*go already auto-commit transactions
before handling DDL commands. However, we were missing the auto-commit
for the BIND command. Without it, binding a prepared statement could
trigger the auto-commit after the bind, which closes the portal and
leads to unknown portal errors.

Epic: none
Release note (bug fix): Fixed a bug when running with
autocommit_before_ddl that could cause a runtime error when binding a
previously prepared DDL statement.

Fixes cockroachdb#141576
Fixes cockroachdb#141881
@spilchen spilchen force-pushed the gh-141881/250226/1042/auto-commit-bind/pr-ready branch from a603260 to 1b6191d Compare February 26, 2025 16:21
Copy link
Contributor Author

@spilchen spilchen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @rafiss and @spilchen)


pkg/sql/pgwire/testdata/pgtest/portals_crbugs line 383 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

super nit: this portals_crbugs file is intended to test expected differences from PG in how CRDB handles portal exhaustion.

i think a more specific test file for this would be pkg/sql/pgwire/testdata/pgtest/schema_changes_implicit_txn_with_bind (though we could rename the file slightly so it's not too specific/verbose).

Good point. I moved it to schema_changes_implicit_txn_with_bind, and renamed the testcase to schema_changes_implicit_txn


pkg/sql/pgwire/testdata/pgtest/portals_crbugs line 432 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

nit: we could have two different until sections: one for postgres and one for cockroachdb. they can be written like this:

this allows the test to pass against postgres (run with ./dev test pkg/sql/pgwire -f=TestPGTest --test-args='-addr localhost:5432 -user user_name') as well as cockroachdb.

(this isn't needed if the test is defined in this file, since the file header has only crdb at the top, but this will be useful if we move this test to a different file)

until noncrdb_only
ReadyForQuery
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"BEGIN"}
{"Type":"ReadyForQuery","TxStatus":"T"}
{"Type":"BindComplete"}
{"Type":"CommandComplete","CommandTag":"DROP TABLE"}
{"Type":"ReadyForQuery","TxStatus":"T"}

until crdb_only
ReadyForQuery
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"BEGIN"}
{"Type":"ReadyForQuery","TxStatus":"T"}
{"Severity":"NOTICE","SeverityUnlocalized":"NOTICE","Code":"00000","Message":"auto-committing transaction before processing DDL due to autocommit_before_ddl setting","Detail":"","Hint":"","Position":0,"InternalPosition":0,"InternalQuery":"","Where":"","SchemaName":"","TableName":"","ColumnName":"","DataTypeName":"","ConstraintName":"","File":"conn_executor_ddl.go","Line":0,"Routine":"maybeAutoCommitBeforeDDL","UnknownFields":null}
{"Type":"BindComplete"}
{"Type":"CommandComplete","CommandTag":"DROP TABLE"}
{"Type":"ReadyForQuery","TxStatus":"I"}

I didn't know you could do this with those tests. Since I moved the test that isn't crdb_only, I added the separate until sections and confirmed it ran on a postgres instance.

@spilchen
Copy link
Contributor Author

TFTR!

bors r+

@craig craig bot merged commit 62f4972 into cockroachdb:master Feb 26, 2025
24 checks passed
@spilchen
Copy link
Contributor Author

I will backport this in #141851 and #141987

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

roachtest: pgjdbc failed "unknown portal" error under CockroachDB 25.1 after multiple DDL operations
3 participants