This is an automated email from the ASF dual-hosted git repository.

yjhjstz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit b9cb020e27ebc09577e26846f2413831aa306106
Author: airfan <[email protected]>
AuthorDate: Mon Jan 9 09:54:32 2023 +0800

    Fix resqueue does not work when using jdbc extend protocol.
    
    After GPDB merge to Postgres V12 and RawStmt is introduced,
    CachedPlanSource->sourceTag is set mistakely in some places.
    This causes resqueue does not work when using jdbc extend
    protocol and some other problem.
---
 src/backend/executor/spi.c                    |  4 +-
 src/backend/tcop/postgres.c                   |  2 +-
 src/test/regress/.gitignore                   |  1 +
 src/test/regress/GNUmakefile                  |  4 +-
 src/test/regress/expected/resource_queue.out  | 21 ++++++++
 src/test/regress/extended_protocol_resqueue.c | 71 +++++++++++++++++++++++++++
 src/test/regress/sql/resource_queue.sql       | 20 ++++++++
 7 files changed, 119 insertions(+), 4 deletions(-)

diff --git a/src/backend/executor/spi.c b/src/backend/executor/spi.c
index b7fa9ca963..a23a6a129f 100644
--- a/src/backend/executor/spi.c
+++ b/src/backend/executor/spi.c
@@ -2354,7 +2354,7 @@ _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
                CompleteCachedPlan(plansource,
                                                   stmt_list,
                                                   NULL,
-                                                  nodeTag(parsetree),
+                                                  nodeTag(parsetree->stmt),
                                                   plan->argtypes,
                                                   plan->nargs,
                                                   plan->parserSetup,
@@ -2605,7 +2605,7 @@ _SPI_execute_plan(SPIPlanPtr plan, const 
SPIExecuteOptions *options,
                        CompleteCachedPlan(plansource,
                                                           stmt_list,
                                                           NULL,
-                                                          nodeTag(parsetree),
+                                                          
nodeTag(parsetree->stmt),
                                                           plan->argtypes,
                                                           plan->nargs,
                                                           plan->parserSetup,
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index d996a14ec5..bcd76cd6a8 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -2237,7 +2237,7 @@ exec_parse_message(const char *query_string,      /* 
string to execute */
                if (parsetree_list)
                {
                        Node       *parsetree = (Node *) 
linitial(parsetree_list);
-                       sourceTag = nodeTag(parsetree);
+                       sourceTag = IsA(parsetree, RawStmt) ? nodeTag(((RawStmt 
*)parsetree)->stmt) : nodeTag(parsetree);
                }
 
                /* Done with the snapshot used for parsing */
diff --git a/src/test/regress/.gitignore b/src/test/regress/.gitignore
index 5990a6bade..6788d5711f 100644
--- a/src/test/regress/.gitignore
+++ b/src/test/regress/.gitignore
@@ -1,5 +1,6 @@
 # Local binaries
 /pg_regress
+/extended_protocol_resqueue
 
 # Generated subdirectories
 /tmp_check/
diff --git a/src/test/regress/GNUmakefile b/src/test/regress/GNUmakefile
index 57538ffac9..bce6d3c7cb 100644
--- a/src/test/regress/GNUmakefile
+++ b/src/test/regress/GNUmakefile
@@ -41,10 +41,12 @@ EXTRADEFS = '-DHOST_TUPLE="$(host_tuple)"' \
 
 # Build regression test driver
 
-all: pg_regress$(X) twophase_pqexecparams
+all: pg_regress$(X) twophase_pqexecparams extended_protocol_resqueue
 
 pg_regress$(X): pg_regress.o pg_regress_main.o $(WIN32RES) | submake-libpgport
        $(CC) $(CFLAGS) $^ $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@
+extended_protocol_resqueue: extended_protocol_resqueue.c
+       $(CC) $(CPPFLAGS) -I../../../src/interfaces/libpq -L$(GPHOME)/lib 
-L../../../src/interfaces/libpq  -o $@ $< -lpq
 
 # dependencies ensure that path changes propagate
 pg_regress.o: pg_regress.c $(top_builddir)/src/port/pg_config_paths.h
diff --git a/src/test/regress/expected/resource_queue.out 
b/src/test/regress/expected/resource_queue.out
index a39e600b82..e3b3c7c4cf 100755
--- a/src/test/regress/expected/resource_queue.out
+++ b/src/test/regress/expected/resource_queue.out
@@ -624,3 +624,24 @@ SELECT * FROM rq_test_oosm_table;
 DROP TABLE rq_test_oosm_table;
 RESET ROLE;
 DROP ROLE rq_test_oosm_role;
+-- test for extended queries
+-- create a role that only use in this test will drop it later
+-- later we will use username to identify the backend process
+create role extend_protocol_requeue_role with login;
+NOTICE:  resource queue required -- using default resource queue "pg_default"
+-- start_matchsubs
+--
+-- m/NOTICE:  query requested \d+/
+-- s/NOTICE:  query requested \d+/NOTICE:  query requested XXX/g
+--
+-- m/NOTICE:  SPI memory reservation \d+/
+-- s/NOTICE:  SPI memory reservation \d+/NOTICE:  SPI memory reservation /g
+--
+-- end_matchsubs
+-- run query using non_superuser role so that it can be
+-- controled by resource queue
+\! ./extended_protocol_resqueue dbname=regression extend_protocol_requeue_role;
+NOTICE:  query requested 9128000KB
+NOTICE:  query requested 9128000KB of memory
+NOTICE:  SPI memory reservation 1931072000
+drop role extend_protocol_requeue_role;
diff --git a/src/test/regress/extended_protocol_resqueue.c 
b/src/test/regress/extended_protocol_resqueue.c
new file mode 100644
index 0000000000..1932f28138
--- /dev/null
+++ b/src/test/regress/extended_protocol_resqueue.c
@@ -0,0 +1,71 @@
+/*
+ * extended_protocol_resqueue.c
+ *
+ * This program is to test whether exetend-queries can be controled properly 
+ * by resource queue.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include "libpq-fe.h"
+
+/* for ntohl/htonl */
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+
+static void
+exit_nicely(PGconn *conn)
+{
+       PQfinish(conn);
+       exit(1);
+}
+
+int
+main(int argc, char **argv)
+{
+       const char *conninfo;
+       const char *execrole;
+       PGconn     *conn;
+       PGresult   *res;
+       const char *paramValues[1];
+       char            buf[80];
+
+       conninfo = argv[1];
+       execrole = argv[2];
+
+       /* Make a connection to the database */
+       conn = PQconnectdb(conninfo);
+
+       /* Check to see that the backend connection was successfully made */
+       if (PQstatus(conn) != CONNECTION_OK)
+       {
+               fprintf(stderr, "Connection to database failed: %s",
+                               PQerrorMessage(conn));
+               exit_nicely(conn);
+       }
+
+       /* Change session role to non_superuser */
+       snprintf(buf, 80, "SET SESSION AUTHORIZATION %s;", execrole);
+       PQexec(conn, buf);
+       
+       /* Set guc to print memory requested when resource queue is enabled */
+       PQexec(conn, "set gp_log_resqueue_memory = 1;");
+       
+       res = PQprepare(conn, "extend_query_cursor", "select pg_sleep(1);", 0, 
NULL);
+       if (PQresultStatus(res) != PGRES_COMMAND_OK)
+       {
+               printf("%s:%d: Execution failed: %s", __FILE__, __LINE__, 
PQresultErrorMessage(res));
+               exit_nicely(conn);
+       }
+       PQclear(res);
+       
+       PQexecPrepared(conn, "extend_query_cursor", 0, NULL, NULL, NULL, 0);
+       
+       PQfinish(conn);
+       
+       return 0;
+}
diff --git a/src/test/regress/sql/resource_queue.sql 
b/src/test/regress/sql/resource_queue.sql
index e2b8d3089c..7dc6611f93 100644
--- a/src/test/regress/sql/resource_queue.sql
+++ b/src/test/regress/sql/resource_queue.sql
@@ -415,3 +415,23 @@ SELECT * FROM rq_test_oosm_table;
 DROP TABLE rq_test_oosm_table;
 RESET ROLE;
 DROP ROLE rq_test_oosm_role;
+-- test for extended queries
+-- create a role that only use in this test will drop it later
+-- later we will use username to identify the backend process
+create role extend_protocol_requeue_role with login;
+
+-- start_matchsubs
+--
+-- m/NOTICE:  query requested \d+/
+-- s/NOTICE:  query requested \d+/NOTICE:  query requested XXX/g
+--
+-- m/NOTICE:  SPI memory reservation \d+/
+-- s/NOTICE:  SPI memory reservation \d+/NOTICE:  SPI memory reservation /g
+--
+-- end_matchsubs
+
+-- run query using non_superuser role so that it can be
+-- controled by resource queue
+\! ./extended_protocol_resqueue dbname=regression extend_protocol_requeue_role;
+
+drop role extend_protocol_requeue_role;


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to