From 1a26a438474b43c60cd87bef17eba879767a8ead Mon Sep 17 00:00:00 2001 From: chenxiaobin19 <1025221611@qq.com> Date: Tue, 15 Apr 2025 11:49:02 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=99=AE=E9=80=9A=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E9=80=9A=E8=BF=87gs=5Fclean=E7=BA=BF=E7=A8=8B=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gausskernel/process/tcop/postgres.cpp | 97 ++++++++++++++++++++++- 1 file changed, 95 insertions(+), 2 deletions(-) diff --git a/src/gausskernel/process/tcop/postgres.cpp b/src/gausskernel/process/tcop/postgres.cpp index e9ac1d9145..8dcc93d548 100755 --- a/src/gausskernel/process/tcop/postgres.cpp +++ b/src/gausskernel/process/tcop/postgres.cpp @@ -7696,6 +7696,98 @@ void RemoveTempNamespace() #if (!defined(ENABLE_MULTIPLE_NODES)) && (!defined(ENABLE_PRIVATEGAUSS)) #define INITIAL_USER_ID 10 +#define GS_CLEAN "gs_clean" + +static PGconn* LoginDatabase(char* host, int port, char* user, char* password, + char* dbname, const char* progname, char* encoding) +{ + PGconn* conn = NULL; + char portValue[32]; +#define PARAMS_ARRAY_SIZE 10 + const char* keywords[PARAMS_ARRAY_SIZE]; + const char* values[PARAMS_ARRAY_SIZE]; + int count = 0; + int retryNum = 10; + int rc; + + rc = sprintf_s(portValue, sizeof(portValue), "%d", port); + securec_check_ss_c(rc, "\0", "\0"); + + keywords[0] = "host"; + values[0] = host; + keywords[1] = "port"; + values[1] = portValue; + keywords[2] = "user"; + values[2] = user; + keywords[3] = "password"; + values[3] = password; + keywords[4] = "dbname"; + values[4] = dbname; + keywords[5] = "fallback_application_name"; + values[5] = progname; + keywords[6] = "client_encoding"; + values[6] = encoding; + keywords[7] = "connect_timeout"; + values[7] = "5"; + keywords[8] = "options"; + /* this mode: remove timeout */ + values[8] = "-c xc_maintenance_mode=on"; + keywords[9] = NULL; + values[9] = NULL; + +retry: + /* try to connect to database */ + conn = PQconnectdbParams(keywords, values, true); + if (PQstatus(conn) != CONNECTION_OK) { + if (++count < retryNum) { + ereport(LOG, (errmsg("Could not connect to the %s, the connection info : %s", + dbname, PQerrorMessage(conn)))); + PQfinish(conn); + conn = NULL; + + /* sleep 0.1 s */ + pg_usleep(100000L); + goto retry; + } + + char connErrorMsg[MAX_ERRMSG_LENGTH] = {0}; + errno_t rc; + rc = snprintf_s(connErrorMsg, MAX_ERRMSG_LENGTH, MAX_ERRMSG_LENGTH - 1, + "%s", PQerrorMessage(conn)); + securec_check_ss(rc, "\0", "\0"); + + PQfinish(conn); + conn = NULL; + ereport(ERROR, (errcode(ERRCODE_CONNECTION_TIMED_OUT), + (errmsg("Could not connect to the %s, " + "we have tried %d times, the connection info: %s", + dbname, count, connErrorMsg)))); + } + + return (conn); +} + +static void start_gs_clean_load_dolphin() +{ + char* dbname = get_database_name(u_sess->proc_cxt.MyDatabaseId); + /* start gs_clean to load dolphin while connecting */ + PGconn* conn = LoginDatabase("localhost", g_instance.attr.attr_network.PostPortNumber, + NULL, NULL, dbname, GS_CLEAN, "auto"); + + if (conn == NULL) { + ereport(ERROR, (errcode(ERRCODE_INVALID_STATUS), + (errmsg("Could not connect to the database %s.", + dbname)))); + } + PQfinish(conn); + conn = NULL; + + start_xact_command(); + u_sess->attr.attr_sql.dolphin = CheckIfExtensionExists("dolphin"); + finish_xact_command(); + InitBSqlPluginHookIfNeeded(); +} + /* * IMPORTANT: * 1. load plugin should call after process is normal, cause heap_create_with_catalog will check it. @@ -7710,8 +7802,9 @@ void LoadSqlPlugin() if (!u_sess->attr.attr_sql.dolphin && u_sess->attr.attr_common.upgrade_mode == 0) { Oid userId = GetUserId(); if (userId != INITIAL_USER_ID) { - ereport(ERROR, (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), - errmsg("Please use the original role to connect B-compatibility database first, to load extension dolphin"))); + /* dolphin need to be created by initial user, if not, start one. */ + start_gs_clean_load_dolphin(); + return; } /* Creating extension dolphin must init mask_password_mem_cxt before */ -- Gitee From cc6e0dd5d6258867cdb57d950717599394bdda6a Mon Sep 17 00:00:00 2001 From: chenxiaobin19 <1025221611@qq.com> Date: Wed, 16 Apr 2025 15:46:02 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AE=BE=E7=BD=AEb=5Ffor?= =?UTF-8?q?mat=5Fbehavior=5Fcompat=5Foptions=E8=BF=9E=E6=8E=A5B=E5=BA=93co?= =?UTF-8?q?redump=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/backend/utils/misc/guc.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/common/backend/utils/misc/guc.cpp b/src/common/backend/utils/misc/guc.cpp index 1bd1373a2f..01b17c00c9 100755 --- a/src/common/backend/utils/misc/guc.cpp +++ b/src/common/backend/utils/misc/guc.cpp @@ -6709,6 +6709,8 @@ void BeginReportingGUCOptions(void) u_sess->utils_cxt.reporting_enabled = true; + /* start transaction to allow calling SearchSysCache such as show_collation_connection */ + start_xact_command(); /* Transmit initial values of interesting variables */ for (i = 0; i < u_sess->num_guc_variables; i++) { struct config_generic* conf = u_sess->guc_variables[i]; @@ -6716,7 +6718,7 @@ void BeginReportingGUCOptions(void) if (conf->flags & GUC_REPORT) ReportGUCOption(conf); } - + finish_xact_command(); } /* -- Gitee