From d0db5b435a3f3066134c8c433979f86966581413 Mon Sep 17 00:00:00 2001 From: wangfeihuo Date: Tue, 8 Apr 2025 10:35:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Ddbcc(=E8=A1=A8=E5=90=8D)?= =?UTF-8?q?=E6=97=B6=E4=B8=8D=E8=BF=9B=E8=A1=8Creseed=E6=A0=87=E8=AF=86?= =?UTF-8?q?=E5=80=BC=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contrib/shark/dbcc.cpp | 9 ++ contrib/shark/expected/test_dbcc.out | 110 +++++++++++++++++- contrib/shark/shark--1.0.sql | 2 +- contrib/shark/sql/test_dbcc.sql | 45 +++++++ .../backend_parser/gram-tsql-epilogue.y.cpp | 4 +- .../src/backend_parser/gram-tsql-prologue.y.h | 2 +- .../shark/src/backend_parser/gram-tsql-rule.y | 6 +- 7 files changed, 166 insertions(+), 12 deletions(-) diff --git a/contrib/shark/dbcc.cpp b/contrib/shark/dbcc.cpp index d4e064b9f0..97b2bc893e 100644 --- a/contrib/shark/dbcc.cpp +++ b/contrib/shark/dbcc.cpp @@ -22,13 +22,22 @@ Datum dbcc_check_ident_no_reseed(PG_FUNCTION_ARGS) char result[DBCC_RESULT_MAX_LENGTH] = {0}; errno_t rc = EOK; bool withmsg = true; + bool reseed_to_max = false; if (!fcinfo->argnull[1]) { withmsg = !PG_GETARG_BOOL(1); } + if (!fcinfo->argnull[2]) { + reseed_to_max = PG_GETARG_BOOL(2); + } + get_last_value_and_max_value(txt, &last_value, ¤t_max_value); + if (reseed_to_max && last_value < current_max_value) { + get_and_reset_last_value(txt, current_max_value, true); + } + if (!withmsg) { PG_RETURN_NULL(); } diff --git a/contrib/shark/expected/test_dbcc.out b/contrib/shark/expected/test_dbcc.out index 6b41a6ea24..9389aac518 100644 --- a/contrib/shark/expected/test_dbcc.out +++ b/contrib/shark/expected/test_dbcc.out @@ -131,6 +131,106 @@ PL/pgSQL function test_procedure_test1(integer) line 2 at PERFORM drop table Employees; drop table Employees_ne; drop procedure test_procedure_test1(int); +CREATE TABLE Employees ( + EmployeeID serial , + Name VARCHAR(100) NOT NULL + ); +NOTICE: CREATE TABLE will create implicit sequence "employees_employeeid_seq" for serial column "employees.employeeid" +insert into Employees(Name) values ('zhangsan'); +insert into Employees(Name) values ('lisi'); +insert into Employees(Name) values ('wangwu'); +insert into Employees(Name) values ('heliu'); +DBCC CHECKIDENT ('Employees', NORESEED); +NOTICE: "Checking identity information: current identity value '4', current column value '4'." +CONTEXT: referenced column: dbcc_check_ident_no_reseed + dbcc_check_ident_no_reseed +-------------------------------------------------------------------------------------- + Checking identity information: current identity value '4', current column value '4'. +(1 row) + +DBCC CHECKIDENT ('Employees', RESEED, 1); +NOTICE: "Checking identity information: current identity value '4'." +CONTEXT: referenced column: dbcc_check_ident_reseed + dbcc_check_ident_reseed +------------------------------------------------------------ + Checking identity information: current identity value '4'. +(1 row) + +DBCC CHECKIDENT ('Employees', NORESEED); +NOTICE: "Checking identity information: current identity value '1', current column value '4'." +CONTEXT: referenced column: dbcc_check_ident_no_reseed + dbcc_check_ident_no_reseed +-------------------------------------------------------------------------------------- + Checking identity information: current identity value '1', current column value '4'. +(1 row) + +DBCC CHECKIDENT ('Employees'); +NOTICE: "Checking identity information: current identity value '1', current column value '4'." +CONTEXT: referenced column: dbcc_check_ident_no_reseed + dbcc_check_ident_no_reseed +-------------------------------------------------------------------------------------- + Checking identity information: current identity value '1', current column value '4'. +(1 row) + +DBCC CHECKIDENT ('Employees', NORESEED); +NOTICE: "Checking identity information: current identity value '4', current column value '4'." +CONTEXT: referenced column: dbcc_check_ident_no_reseed + dbcc_check_ident_no_reseed +-------------------------------------------------------------------------------------- + Checking identity information: current identity value '4', current column value '4'. +(1 row) + +drop table Employees; +CREATE TABLE Employees ( + EmployeeID serial , + Name VARCHAR(100) NOT NULL + ); +NOTICE: CREATE TABLE will create implicit sequence "employees_employeeid_seq" for serial column "employees.employeeid" +insert into Employees(Name) values ('zhangsan'); +insert into Employees(Name) values ('lisi'); +insert into Employees(Name) values ('wangwu'); +insert into Employees(Name) values ('heliu'); +DBCC CHECKIDENT ('Employees', NORESEED); +NOTICE: "Checking identity information: current identity value '4', current column value '4'." +CONTEXT: referenced column: dbcc_check_ident_no_reseed + dbcc_check_ident_no_reseed +-------------------------------------------------------------------------------------- + Checking identity information: current identity value '4', current column value '4'. +(1 row) + +DBCC CHECKIDENT ('Employees', RESEED, 1); +NOTICE: "Checking identity information: current identity value '4'." +CONTEXT: referenced column: dbcc_check_ident_reseed + dbcc_check_ident_reseed +------------------------------------------------------------ + Checking identity information: current identity value '4'. +(1 row) + +DBCC CHECKIDENT ('Employees', NORESEED); +NOTICE: "Checking identity information: current identity value '1', current column value '4'." +CONTEXT: referenced column: dbcc_check_ident_no_reseed + dbcc_check_ident_no_reseed +-------------------------------------------------------------------------------------- + Checking identity information: current identity value '1', current column value '4'. +(1 row) + +DBCC CHECKIDENT ('Employees', RESEED); +NOTICE: "Checking identity information: current identity value '1', current column value '4'." +CONTEXT: referenced column: dbcc_check_ident_no_reseed + dbcc_check_ident_no_reseed +-------------------------------------------------------------------------------------- + Checking identity information: current identity value '1', current column value '4'. +(1 row) + +DBCC CHECKIDENT ('Employees', NORESEED); +NOTICE: "Checking identity information: current identity value '4', current column value '4'." +CONTEXT: referenced column: dbcc_check_ident_no_reseed + dbcc_check_ident_no_reseed +-------------------------------------------------------------------------------------- + Checking identity information: current identity value '4', current column value '4'. +(1 row) + +drop table Employees; -- part1: dbcc check RESEED CREATE TABLE Employees ( EmployeeID serial , @@ -229,11 +329,11 @@ select * from Employees order by 1, 2; ALTER SEQUENCE employees_employeeid_seq MINVALUE -100; DBCC CHECKIDENT ('Employees', RESEED); -NOTICE: "Checking identity information: current identity value '3'." -CONTEXT: referenced column: dbcc_check_ident_reseed - dbcc_check_ident_reseed ------------------------------------------------------------- - Checking identity information: current identity value '3'. +NOTICE: "Checking identity information: current identity value '3', current column value '3'." +CONTEXT: referenced column: dbcc_check_ident_no_reseed + dbcc_check_ident_no_reseed +-------------------------------------------------------------------------------------- + Checking identity information: current identity value '3', current column value '3'. (1 row) DBCC CHECKIDENT ('Employees', RESEED, -2); diff --git a/contrib/shark/shark--1.0.sql b/contrib/shark/shark--1.0.sql index 7e1713921a..d1651bd592 100644 --- a/contrib/shark/shark--1.0.sql +++ b/contrib/shark/shark--1.0.sql @@ -50,7 +50,7 @@ create trusted language pltsql validator pltsql_validator; -CREATE FUNCTION dbcc_check_ident_no_reseed(varchar, boolean) RETURNS varchar as 'MODULE_PATHNAME', 'dbcc_check_ident_no_reseed' LANGUAGE C STRICT STABLE; +CREATE FUNCTION dbcc_check_ident_no_reseed(varchar, boolean, boolean) RETURNS varchar as 'MODULE_PATHNAME', 'dbcc_check_ident_no_reseed' LANGUAGE C STRICT STABLE; CREATE FUNCTION dbcc_check_ident_reseed(varchar, bigint, boolean) RETURNS varchar as 'MODULE_PATHNAME', 'dbcc_check_ident_reseed' LANGUAGE C STABLE; grant usage on language pltsql to public; diff --git a/contrib/shark/sql/test_dbcc.sql b/contrib/shark/sql/test_dbcc.sql index fc5b833750..131634e0ef 100644 --- a/contrib/shark/sql/test_dbcc.sql +++ b/contrib/shark/sql/test_dbcc.sql @@ -78,6 +78,51 @@ drop table Employees_ne; drop procedure test_procedure_test1(int); +CREATE TABLE Employees ( + EmployeeID serial , + Name VARCHAR(100) NOT NULL + ); + +insert into Employees(Name) values ('zhangsan'); +insert into Employees(Name) values ('lisi'); +insert into Employees(Name) values ('wangwu'); +insert into Employees(Name) values ('heliu'); + +DBCC CHECKIDENT ('Employees', NORESEED); + +DBCC CHECKIDENT ('Employees', RESEED, 1); + +DBCC CHECKIDENT ('Employees', NORESEED); + +DBCC CHECKIDENT ('Employees'); + +DBCC CHECKIDENT ('Employees', NORESEED); + +drop table Employees; + + +CREATE TABLE Employees ( + EmployeeID serial , + Name VARCHAR(100) NOT NULL + ); + +insert into Employees(Name) values ('zhangsan'); +insert into Employees(Name) values ('lisi'); +insert into Employees(Name) values ('wangwu'); +insert into Employees(Name) values ('heliu'); + +DBCC CHECKIDENT ('Employees', NORESEED); + +DBCC CHECKIDENT ('Employees', RESEED, 1); + +DBCC CHECKIDENT ('Employees', NORESEED); + +DBCC CHECKIDENT ('Employees', RESEED); + +DBCC CHECKIDENT ('Employees', NORESEED); + +drop table Employees; + -- part1: dbcc check RESEED CREATE TABLE Employees ( EmployeeID serial , diff --git a/contrib/shark/src/backend_parser/gram-tsql-epilogue.y.cpp b/contrib/shark/src/backend_parser/gram-tsql-epilogue.y.cpp index bf015b8692..122b724fdf 100644 --- a/contrib/shark/src/backend_parser/gram-tsql-epilogue.y.cpp +++ b/contrib/shark/src/backend_parser/gram-tsql-epilogue.y.cpp @@ -41,10 +41,10 @@ static List* make_func_call_func(List* funcname, List* args) return (list_make1(restarget)); } -static List* make_no_reseed_func(char* table_name, bool with_no_msgs) +static List* make_no_reseed_func(char* table_name, bool with_no_msgs, bool reseed_to_max) { List* funcname = list_make1(makeString("dbcc_check_ident_no_reseed")); - List* args = list_make2(makeStringConst(table_name, -1), makeBoolConst(with_no_msgs, false)); + List* args = list_make3(makeStringConst(table_name, -1), makeBoolConst(with_no_msgs, false), makeBoolConst(reseed_to_max, false)); return make_func_call_func(funcname, args); } diff --git a/contrib/shark/src/backend_parser/gram-tsql-prologue.y.h b/contrib/shark/src/backend_parser/gram-tsql-prologue.y.h index 621c88131e..53123d6cec 100644 --- a/contrib/shark/src/backend_parser/gram-tsql-prologue.y.h +++ b/contrib/shark/src/backend_parser/gram-tsql-prologue.y.h @@ -1,6 +1,6 @@ static void pgtsql_base_yyerror(YYLTYPE * yylloc, core_yyscan_t yyscanner, const char *msg); List *TsqlSystemFuncName2(char *name); -static List* make_no_reseed_func(char* table_name, bool with_no_msgs); +static List* make_no_reseed_func(char* table_name, bool with_no_msgs, bool reseed_to_max); static List* make_reseed_func(char* table_name, Node* new_seed, bool with_no_msgs); static List* make_func_call_func(List* funcname, List* args); static char* quote_identifier_wrapper(char* ident, core_yyscan_t yyscanner); \ No newline at end of file diff --git a/contrib/shark/src/backend_parser/gram-tsql-rule.y b/contrib/shark/src/backend_parser/gram-tsql-rule.y index 20ae2c4e5a..5e032592c9 100644 --- a/contrib/shark/src/backend_parser/gram-tsql-rule.y +++ b/contrib/shark/src/backend_parser/gram-tsql-rule.y @@ -517,7 +517,7 @@ DBCCCheckIdentStmt: { SelectStmt *n = makeNode(SelectStmt); n->distinctClause = NIL; - n->targetList = make_no_reseed_func(quote_identifier_wrapper($4, yyscanner), $8); + n->targetList = make_no_reseed_func(quote_identifier_wrapper($4, yyscanner), $8, false); n->intoClause = NULL; n->fromClause = NIL; n->whereClause = NULL; @@ -530,7 +530,7 @@ DBCCCheckIdentStmt: { SelectStmt *n = makeNode(SelectStmt); n->distinctClause = NIL; - n->targetList = make_no_reseed_func(quote_identifier_wrapper($4, yyscanner), $6); + n->targetList = make_no_reseed_func(quote_identifier_wrapper($4, yyscanner), $6, true); n->intoClause = NULL; n->fromClause = NIL; n->whereClause = NULL; @@ -556,7 +556,7 @@ DBCCCheckIdentStmt: { SelectStmt *n = makeNode(SelectStmt); n->distinctClause = NIL; - n->targetList = make_reseed_func(quote_identifier_wrapper($4, yyscanner), makeNullAConst(@6), $8); + n->targetList = make_no_reseed_func(quote_identifier_wrapper($4, yyscanner), $8, true); n->intoClause = NULL; n->fromClause = NIL; n->whereClause = NULL; -- Gitee