Skip to content

Commit 5256f3d

Browse files
authored
Merge pull request #5388 from sysown/v3.0_fix-pgsql-extended-query-routing_5387
Fix: Extended query Routing and Statistics Tracking
2 parents 10ff132 + 1f0756b commit 5256f3d

File tree

6 files changed

+546
-4
lines changed

6 files changed

+546
-4
lines changed

lib/PgSQL_Connection.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,13 +726,24 @@ PG_ASYNC_ST PgSQL_Connection::handler(short event) {
726726
break;
727727

728728
case ASYNC_STMT_DESCRIBE_START:
729+
{
729730
stmt_describe_start();
731+
__sync_fetch_and_add(&parent->queries_sent, 1);
732+
size_t bytes_sent = 7 + 5; // 7 for DESCRIBE header, 5 for SYNC/FLUSH
733+
if (query.extended_query_info->stmt_type == 'P') {
734+
bytes_sent += query.extended_query_info->stmt_client_portal_name ? (strlen(query.extended_query_info->stmt_client_portal_name) + 1) : 0;
735+
} else {
736+
bytes_sent += query.backend_stmt_name ? (strlen(query.backend_stmt_name) + 1) : 0;
737+
}
738+
update_bytes_sent(bytes_sent);
739+
statuses.questions++;
730740
if (async_exit_status) {
731741
next_event(ASYNC_STMT_DESCRIBE_CONT);
732742
} else {
733743
NEXT_IMMEDIATE(ASYNC_STMT_DESCRIBE_END);
734744
}
735-
break;
745+
}
746+
break;
736747
case ASYNC_STMT_DESCRIBE_CONT:
737748
if (event) {
738749
stmt_describe_cont(event);
@@ -750,6 +761,9 @@ PG_ASYNC_ST PgSQL_Connection::handler(short event) {
750761

751762
case ASYNC_STMT_EXECUTE_START:
752763
stmt_execute_start();
764+
__sync_fetch_and_add(&parent->queries_sent, 1);
765+
update_bytes_sent(query.extended_query_info->bind_msg->get_raw_pkt().size + 5);
766+
statuses.questions++;
753767
if (async_exit_status) {
754768
next_event(ASYNC_STMT_EXECUTE_CONT);
755769
} else {
@@ -808,10 +822,11 @@ PG_ASYNC_ST PgSQL_Connection::handler(short event) {
808822

809823
case ASYNC_RESYNC_START:
810824
if (PQpipelineStatus(pgsql_conn) == PQ_PIPELINE_OFF) {
811-
proxy_warning("Resync not required — connection already synchronized.\n");
825+
proxy_warning("Resync not required - connection already synchronized.\n");
812826
NEXT_IMMEDIATE(ASYNC_RESYNC_END);
813827
}
814828
resync_start();
829+
update_bytes_sent(5); // SYNC message
815830
if (async_exit_status) {
816831
next_event(ASYNC_RESYNC_CONT);
817832
} else {

lib/PgSQL_Session.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4470,8 +4470,13 @@ bool PgSQL_Session::handler___status_WAITING_CLIENT_DATA___STATE_SLEEP___PGSQL_Q
44704470
next_query_flagIN = qpo->next_query_flagIN;
44714471
}
44724472

4473-
if (qpo->destination_hostgroup >= 0 && transaction_persistent_hostgroup == -1) {
4474-
current_hostgroup = qpo->destination_hostgroup;
4473+
if (transaction_persistent_hostgroup == -1) {
4474+
if (qpo->destination_hostgroup >= 0) {
4475+
current_hostgroup = qpo->destination_hostgroup;
4476+
} else {
4477+
// No query rule matched - use default hostgroup
4478+
current_hostgroup = default_hostgroup;
4479+
}
44754480
}
44764481

44774482
// Hostgroup locking check

test/tap/groups/groups.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@
225225
"mysql-watchdog_test-t": [ "default-g4", "mysql-auto_increment_delay_multiplex=0-g4", "mysql-multiplexing=false-g4", "mysql-query_digests=0-g4", "mysql-query_digests_keep_comment=1-g4" ],
226226
"pgsql-extended_query_protocol_query_rules_test-t": [ "default-g4", "mysql-auto_increment_delay_multiplex=0-g4", "mysql-multiplexing=false-g4", "mysql-query_digests=0-g4", "mysql-query_digests_keep_comment=1-g4" ],
227227
"pgsql-extended_query_protocol_test-t": [ "default-g4", "mysql-auto_increment_delay_multiplex=0-g4", "mysql-multiplexing=false-g4", "mysql-query_digests=0-g4", "mysql-query_digests_keep_comment=1-g4" ],
228+
"pgsql-query_rules_routing-t": [ "pgsql17-repl-g4" ],
228229
"pgsql-multiplex_status_test-t": [ "default-g4", "mysql-auto_increment_delay_multiplex=0-g4", "mysql-multiplexing=false-g4", "mysql-query_digests=0-g4", "mysql-query_digests_keep_comment=1-g4" ],
229230
"pgsql-proxysql_cmd_test-t": [ "default-g4", "mysql-auto_increment_delay_multiplex=0-g4", "mysql-multiplexing=false-g4", "mysql-query_digests=0-g4", "mysql-query_digests_keep_comment=1-g4" ],
230231
"pgsql-query_cancel_session_termination_test-t": [ "default-g4", "mysql-auto_increment_delay_multiplex=0-g4", "mysql-multiplexing=false-g4", "mysql-query_digests=0-g4", "mysql-query_digests_keep_comment=1-g4" ],
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# PostgreSQL 17 Replication Test Group
2+
#
3+
# Tests for replication, query routing, read-only detection
4+
5+
export TEST_PY_TAP_INCL="pgsql-query_rules_routing-t"
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env bash
2+
#
3+
# change infra config for PostgreSQL 17 replication
4+
# inherits env from tester script
5+
#
6+
7+
INFRA=infra-$(basename $(dirname "$0") | sed 's/-g[0-9]//' | sed 's/_.*//')
8+
9+
# destroy running infras
10+
$JENKINS_SCRIPTS_PATH/infra-default/docker-compose-destroy.bash
11+
12+
# cleanup ProxySQL before starting new infra
13+
psql -h127.0.0.1 -p6132 -Uadmin -dadmin -c " \
14+
DELETE FROM pgsql_users; \
15+
LOAD PGSQL USERS TO RUNTIME; \
16+
SAVE PGSQL USERS TO DISK; \
17+
DELETE FROM pgsql_servers; \
18+
DELETE FROM pgsql_hostgroup_parameters; \
19+
DELETE FROM pgsql_replication_hostgroups; \
20+
LOAD PGSQL SERVERS TO RUNTIME; \
21+
SAVE PGSQL SERVERS TO DISK; \
22+
DELETE FROM pgsql_query_rules; \
23+
LOAD PGSQL QUERY RULES TO RUNTIME; \
24+
SAVE PGSQL QUERY RULES TO DISK; \
25+
DELETE FROM mysql_users; \
26+
LOAD MYSQL USERS TO RUNTIME; \
27+
SAVE MYSQL USERS TO DISK; \
28+
DELETE FROM mysql_servers; \
29+
DELETE FROM mysql_replication_hostgroups; \
30+
DELETE FROM mysql_group_replication_hostgroups; \
31+
DELETE FROM mysql_galera_hostgroups; \
32+
LOAD MYSQL SERVERS TO RUNTIME; \
33+
SAVE MYSQL SERVERS TO DISK; \
34+
DELETE FROM mysql_query_rules; \
35+
LOAD MYSQL QUERY RULES TO RUNTIME; \
36+
SAVE MYSQL QUERY RULES TO DISK; \
37+
" 2>&1
38+
39+
# load environment for infra
40+
source $JENKINS_SCRIPTS_PATH/${INFRA}/.env
41+
42+
# Start infra (this will configure ProxySQL via docker-proxy-post.bash)
43+
$JENKINS_SCRIPTS_PATH/infra-docker-hoster/docker-compose-init.bash
44+
$JENKINS_SCRIPTS_PATH/${INFRA}/docker-compose-init.bash
45+
46+
# wait for infra to stabilize
47+
sleep 10

0 commit comments

Comments
 (0)