Skip to content

Commit

Permalink
pgwire: add tests for multiple active portals
Browse files Browse the repository at this point in the history
Release note: None
  • Loading branch information
ZhouXing19 committed Mar 15, 2023
1 parent 4197e9b commit cee26e7
Show file tree
Hide file tree
Showing 2 changed files with 390 additions and 2 deletions.
388 changes: 388 additions & 0 deletions pkg/sql/pgwire/testdata/pgtest/multiple_active_portals
Original file line number Diff line number Diff line change
@@ -0,0 +1,388 @@
send crdb_only
Query {"String": "SET CLUSTER SETTING sql.defaults.multiple_active_portals.enabled = true"}
----

# TODO(janexing): is there a way to ignore the notice for cluster setting?
until crdb_only
ReadyForQuery
----
{"Severity":"NOTICE","SeverityUnlocalized":"NOTICE","Code":"00000","Message":"setting global default sql.defaults.multiple_active_portals.enabled is not recommended","Detail":"","Hint":"use the `ALTER ROLE ... SET` syntax to control session variable defaults at a finer-grained level. See: https://www.cockroachlabs.com/docs/v23.1/alter-role.html#set-default-session-variable-values-for-a-role","Position":0,"InternalPosition":0,"InternalQuery":"","Where":"","SchemaName":"","TableName":"","ColumnName":"","DataTypeName":"","ConstraintName":"","File":"set_cluster_setting.go","Line":248,"Routine":"startExec","UnknownFields":null}
{"Type":"CommandComplete","CommandTag":"SET CLUSTER SETTING"}
{"Type":"ReadyForQuery","TxStatus":"I"}

subtest select_from_individual_resources_q1_q2

send
Query {"String": "BEGIN"}
Parse {"Name": "q1", "Query": "SELECT * FROM generate_series(1,20)"}
Parse {"Name": "q2", "Query": "SELECT * FROM generate_series(1,20)"}
Bind {"DestinationPortal": "p1", "PreparedStatement": "q1"}
Bind {"DestinationPortal": "p2", "PreparedStatement": "q2"}
Execute {"Portal": "p1", "MaxRows": 1}
Execute {"Portal": "p2", "MaxRows": 1}
Execute {"Portal": "p1", "MaxRows": 1}
Execute {"Portal": "p2", "MaxRows": 1}
Sync
----

until
ReadyForQuery
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"BEGIN"}
{"Type":"ReadyForQuery","TxStatus":"T"}
{"Type":"ParseComplete"}
{"Type":"ParseComplete"}
{"Type":"BindComplete"}
{"Type":"BindComplete"}
{"Type":"DataRow","Values":[{"text":"1"}]}
{"Type":"PortalSuspended"}
{"Type":"DataRow","Values":[{"text":"1"}]}
{"Type":"PortalSuspended"}
{"Type":"DataRow","Values":[{"text":"2"}]}
{"Type":"PortalSuspended"}
{"Type":"DataRow","Values":[{"text":"2"}]}
{"Type":"PortalSuspended"}
{"Type":"ReadyForQuery","TxStatus":"T"}

send
Query {"String": "COMMIT"}
----

until
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"COMMIT"}
{"Type":"ReadyForQuery","TxStatus":"I"}

subtest end

subtest select_from_same_table_q3_q4
send
Query {"String": "BEGIN"}
Query {"String": "CREATE TABLE mytable (x int)"}
Query {"String": "INSERT INTO mytable VALUES (1),(2),(3)"}
Parse {"Name": "q3", "Query": "SELECT * FROM mytable"}
Bind {"DestinationPortal": "p3", "PreparedStatement": "q3"}
Parse {"Name": "q4", "Query": "SELECT * FROM mytable"}
Bind {"DestinationPortal": "p4", "PreparedStatement": "q4"}
Execute {"Portal": "p3", "MaxRows": 1}
Execute {"Portal": "p4", "MaxRows": 1}
Execute {"Portal": "p4", "MaxRows": 1}
Execute {"Portal": "p3", "MaxRows": 1}
Sync
----


until
ReadyForQuery
ReadyForQuery
ReadyForQuery
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"BEGIN"}
{"Type":"ReadyForQuery","TxStatus":"T"}
{"Type":"CommandComplete","CommandTag":"CREATE TABLE"}
{"Type":"ReadyForQuery","TxStatus":"T"}
{"Type":"CommandComplete","CommandTag":"INSERT 0 3"}
{"Type":"ReadyForQuery","TxStatus":"T"}
{"Type":"ParseComplete"}
{"Type":"BindComplete"}
{"Type":"ParseComplete"}
{"Type":"BindComplete"}
{"Type":"DataRow","Values":[{"text":"1"}]}
{"Type":"PortalSuspended"}
{"Type":"DataRow","Values":[{"text":"1"}]}
{"Type":"PortalSuspended"}
{"Type":"DataRow","Values":[{"text":"2"}]}
{"Type":"PortalSuspended"}
{"Type":"DataRow","Values":[{"text":"2"}]}
{"Type":"PortalSuspended"}
{"Type":"ReadyForQuery","TxStatus":"T"}

subtest end

subtest bind_to_an_existing_active_portal

send
Parse {"Name": "q5", "Query": "SELECT * FROM mytable"}
Bind {"DestinationPortal": "p3", "PreparedStatement": "q5"}
Execute {"Portal": "p3", "MaxRows": 2}
Sync
----

# cursor \"p3\" already exists
until
ErrorResponse
----
{"Type":"ParseComplete"}
{"Type":"ErrorResponse","Code":"42P03"}

send
Query {"String": "COMMIT"}
Sync
----

# Rollback
until
ReadyForQuery
ReadyForQuery
----
{"Type":"ReadyForQuery","TxStatus":"E"}
{"Type":"CommandComplete","CommandTag":"ROLLBACK"}
{"Type":"ReadyForQuery","TxStatus":"I"}

subtest end


subtest not_in_explicit_transaction_q6_q7

send
Parse {"Name": "q6", "Query": "SELECT * FROM generate_series(1,20)"}
Bind {"DestinationPortal": "p6", "PreparedStatement": "q6"}
Parse {"Name": "q7", "Query": "SELECT * FROM generate_series(1,20)"}
Bind {"DestinationPortal": "p7", "PreparedStatement": "q7"}
Execute {"Portal": "p7", "MaxRows": 1}
Execute {"Portal": "p6", "MaxRows": 1}
Execute {"Portal": "p7", "MaxRows": 1}
Execute {"Portal": "p6", "MaxRows": 1}
Sync
----


until
ReadyForQuery
ReadyForQuery
----
{"Type":"ReadyForQuery","TxStatus":"I"}
{"Type":"ParseComplete"}
{"Type":"BindComplete"}
{"Type":"ParseComplete"}
{"Type":"BindComplete"}
{"Type":"DataRow","Values":[{"text":"1"}]}
{"Type":"PortalSuspended"}
{"Type":"DataRow","Values":[{"text":"1"}]}
{"Type":"PortalSuspended"}
{"Type":"DataRow","Values":[{"text":"2"}]}
{"Type":"PortalSuspended"}
{"Type":"DataRow","Values":[{"text":"2"}]}
{"Type":"PortalSuspended"}
{"Type":"ReadyForQuery","TxStatus":"I"}


send
Execute {"Portal": "p7", "MaxRows": 2}
Sync
----

# p7 doesn't exist, as it is closed when the implicit txn is committed.
until
ErrorResponse
ReadyForQuery
----
{"Type":"ErrorResponse","Code":"34000"}
{"Type":"ReadyForQuery","TxStatus":"I"}


subtest end


subtest drop_table_when_there_are_dependent_active_portals

send
Query {"String": "BEGIN"}
Query {"String": "CREATE TABLE mytable (x int)"}
Query {"String": "INSERT INTO mytable VALUES (1),(2),(3)"}
Parse {"Name": "q8", "Query": "SELECT * FROM mytable"}
Bind {"DestinationPortal": "p8", "PreparedStatement": "q8"}
Execute {"Portal": "p8", "MaxRows": 1}
Sync
----


until
ReadyForQuery
ReadyForQuery
ReadyForQuery
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"BEGIN"}
{"Type":"ReadyForQuery","TxStatus":"T"}
{"Type":"CommandComplete","CommandTag":"CREATE TABLE"}
{"Type":"ReadyForQuery","TxStatus":"T"}
{"Type":"CommandComplete","CommandTag":"INSERT 0 3"}
{"Type":"ReadyForQuery","TxStatus":"T"}
{"Type":"ParseComplete"}
{"Type":"BindComplete"}
{"Type":"DataRow","Values":[{"text":"1"}]}
{"Type":"PortalSuspended"}
{"Type":"ReadyForQuery","TxStatus":"T"}

send
Query {"String": "DROP TABLE mytable"}
----

# cannot DROP TABLE \"mytable\" because it is being used by active queries in this session
until noncrdb_only
ErrorResponse
ReadyForQuery
----
{"Type":"ErrorResponse","Code":"55006"}
{"Type":"ReadyForQuery","TxStatus":"E"}

# For cursor we have `cannot run schema change in a transaction with open DECLARE cursors`.
# We should have something similar for portals as well.
until crdb_only
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"DROP TABLE"}
{"Type":"ReadyForQuery","TxStatus":"T"}

send
Query {"String": "COMMIT"}
----

until noncrdb_only
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"ROLLBACK"}
{"Type":"ReadyForQuery","TxStatus":"I"}

until crdb_only
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"COMMIT"}
{"Type":"ReadyForQuery","TxStatus":"I"}

subtest end


subtest different_portals_bind_to_the_same_statement

send
Query {"String": "BEGIN"}
Parse {"Name": "q9", "Query": "SELECT * FROM generate_series(1,20)"}
Bind {"DestinationPortal": "p9", "PreparedStatement": "q9"}
Bind {"DestinationPortal": "p10", "PreparedStatement": "q9"}
Execute {"Portal": "p9", "MaxRows": 1}
Execute {"Portal": "p10", "MaxRows": 1}
Sync
----

until
ReadyForQuery
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"BEGIN"}
{"Type":"ReadyForQuery","TxStatus":"T"}
{"Type":"ParseComplete"}
{"Type":"BindComplete"}
{"Type":"BindComplete"}
{"Type":"DataRow","Values":[{"text":"1"}]}
{"Type":"PortalSuspended"}
{"Type":"DataRow","Values":[{"text":"1"}]}
{"Type":"PortalSuspended"}
{"Type":"ReadyForQuery","TxStatus":"T"}


send
Execute {"Portal": "p9", "MaxRows": 1}
Sync
----

until
ReadyForQuery
----
{"Type":"DataRow","Values":[{"text":"2"}]}
{"Type":"PortalSuspended"}
{"Type":"ReadyForQuery","TxStatus":"T"}


send
Query {"String": "COMMIT"}
Sync
----

until
ReadyForQuery
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"COMMIT"}
{"Type":"ReadyForQuery","TxStatus":"I"}
{"Type":"ReadyForQuery","TxStatus":"I"}

subtest end


subtest more_complicated_stmts
send
Query {"String": "BEGIN; CREATE TABLE ta (x int, y int); CREATE TABLE tb (x int, z int); INSERT INTO ta VALUES (1,1), (2,2), (3,3); INSERT INTO tb VALUES (1,2), (2,3), (3,4)"}
Parse {"Name": "q11", "Query": "SELECT z as myz FROM ta JOIN tb ON ta.x = tb.x ORDER BY myz"}
Bind {"DestinationPortal": "p11", "PreparedStatement": "q11"}
Execute {"Portal": "p11", "MaxRows": 1}
Sync
----

until
ReadyForQuery
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"BEGIN"}
{"Type":"CommandComplete","CommandTag":"CREATE TABLE"}
{"Type":"CommandComplete","CommandTag":"CREATE TABLE"}
{"Type":"CommandComplete","CommandTag":"INSERT 0 3"}
{"Type":"CommandComplete","CommandTag":"INSERT 0 3"}
{"Type":"ReadyForQuery","TxStatus":"T"}
{"Type":"ParseComplete"}
{"Type":"BindComplete"}
{"Type":"DataRow","Values":[{"text":"2"}]}
{"Type":"PortalSuspended"}
{"Type":"ReadyForQuery","TxStatus":"T"}

send
Query {"String": "COMMIT"}
Sync
----

until
ReadyForQuery
ReadyForQuery
----
{"Type":"CommandComplete","CommandTag":"COMMIT"}
{"Type":"ReadyForQuery","TxStatus":"I"}
{"Type":"ReadyForQuery","TxStatus":"I"}

subtest end


subtest subqueries
send
Query {"String": "BEGIN; DROP TABLE IF EXISTS t; CREATE TABLE t (x int)"}
Parse {"Name": "q12", "Query": "WITH t AS (INSERT INTO t(x) VALUES (1), (2), (3) RETURNING x) SELECT * FROM t;"}
Bind {"DestinationPortal": "p12", "PreparedStatement": "q12"}
Bind {"DestinationPortal": "p13", "PreparedStatement": "q12"}
Execute {"Portal": "p12", "MaxRows": 1}
Execute {"Portal": "p12", "MaxRows": 1}
Execute {"Portal": "p13", "MaxRows": 1}
Sync
----

# multiple acitve portal
until keepErrMessage
ReadyForQuery
ErrorResponse
----
{"Type":"CommandComplete","CommandTag":"BEGIN"}
{"Type":"CommandComplete","CommandTag":"DROP TABLE"}
{"Type":"CommandComplete","CommandTag":"CREATE TABLE"}
{"Type":"ReadyForQuery","TxStatus":"T"}
{"Type":"ParseComplete"}
{"Type":"BindComplete"}
{"Type":"BindComplete"}
{"Type":"DataRow","Values":[{"text":"1"}]}
{"Type":"PortalSuspended"}
{"Type":"DataRow","Values":[{"text":"2"}]}
{"Type":"PortalSuspended"}
{"Type":"ErrorResponse","Code":"0A000","Message":"unimplemented: multiple active portals not supported, please set sql.defaults.multiple_active_portals.enabled to true. (Note this feature is in preview)"}

subtest end
Loading

0 comments on commit cee26e7

Please sign in to comment.