From 571417a1335e6474fe8c2a25f4fd50967728a39d Mon Sep 17 00:00:00 2001 From: naisila Date: Fri, 29 Jul 2022 15:44:07 +0300 Subject: [PATCH] Replace int nodes with bool nodes where needed In PG15, Boolean nodes are added. Pre PG15, internal Boolean values in Create Role commands were represented by Integer nodes. This commit replaces int nodes logic with bool nodes logic where needed. Mostly there are CREATE ROLE logic changes. Relevant PG commit: 941460fcf731a32e6a90691508d5cfa3d1f8eeaf --- src/backend/distributed/commands/role.c | 27 ++++++++++++----- .../deparser/deparse_function_stmts.c | 6 ++-- .../distributed/deparser/deparse_role_stmts.c | 30 ++++++++++--------- src/include/pg_version_compat.h | 2 ++ 4 files changed, 41 insertions(+), 24 deletions(-) diff --git a/src/backend/distributed/commands/role.c b/src/backend/distributed/commands/role.c index 74d14751e..f61b00423 100644 --- a/src/backend/distributed/commands/role.c +++ b/src/backend/distributed/commands/role.c @@ -10,6 +10,8 @@ #include "postgres.h" +#include "pg_version_compat.h" + #include "distributed/pg_version_constants.h" #include "access/heapam.h" @@ -59,6 +61,7 @@ static char * CreateCreateOrAlterRoleCommand(const char *roleName, CreateRoleStmt *createRoleStmt, AlterRoleStmt *alterRoleStmt); static DefElem * makeDefElemInt(char *name, int value); +static DefElem * makeDefElemBool(char *name, bool value); static List * GenerateRoleOptionsList(HeapTuple tuple); static List * GenerateGrantRoleStmtsFromOptions(RoleSpec *roleSpec, List *options); static List * GenerateGrantRoleStmtsOfRole(Oid roleid); @@ -454,13 +457,13 @@ GenerateRoleOptionsList(HeapTuple tuple) Form_pg_authid role = ((Form_pg_authid) GETSTRUCT(tuple)); List *options = NIL; - options = lappend(options, makeDefElemInt("superuser", role->rolsuper)); - options = lappend(options, makeDefElemInt("createdb", role->rolcreatedb)); - options = lappend(options, makeDefElemInt("createrole", role->rolcreaterole)); - options = lappend(options, makeDefElemInt("inherit", role->rolinherit)); - options = lappend(options, makeDefElemInt("canlogin", role->rolcanlogin)); - options = lappend(options, makeDefElemInt("isreplication", role->rolreplication)); - options = lappend(options, makeDefElemInt("bypassrls", role->rolbypassrls)); + options = lappend(options, makeDefElemBool("superuser", role->rolsuper)); + options = lappend(options, makeDefElemBool("createdb", role->rolcreatedb)); + options = lappend(options, makeDefElemBool("createrole", role->rolcreaterole)); + options = lappend(options, makeDefElemBool("inherit", role->rolinherit)); + options = lappend(options, makeDefElemBool("canlogin", role->rolcanlogin)); + options = lappend(options, makeDefElemBool("isreplication", role->rolreplication)); + options = lappend(options, makeDefElemBool("bypassrls", role->rolbypassrls)); options = lappend(options, makeDefElemInt("connectionlimit", role->rolconnlimit)); /* load password from heap tuple, use NULL if not set */ @@ -616,6 +619,16 @@ makeDefElemInt(char *name, int value) } +/* + * makeDefElemBool creates a DefElem with boolean typed value with -1 as location. + */ +static DefElem * +makeDefElemBool(char *name, bool value) +{ + return makeDefElem(name, (Node *) makeBoolean(value), -1); +} + + /* * GetDatabaseNameFromDbRoleSetting performs a lookup, and finds the database name * associated DbRoleSetting Tuple diff --git a/src/backend/distributed/deparser/deparse_function_stmts.c b/src/backend/distributed/deparser/deparse_function_stmts.c index 48f993425..524a1928d 100644 --- a/src/backend/distributed/deparser/deparse_function_stmts.c +++ b/src/backend/distributed/deparser/deparse_function_stmts.c @@ -196,7 +196,7 @@ AppendDefElem(StringInfo buf, DefElem *def) static void AppendDefElemStrict(StringInfo buf, DefElem *def) { - if (intVal(def->arg) == 1) + if (boolVal(def->arg)) { appendStringInfo(buf, " STRICT"); } @@ -223,7 +223,7 @@ AppendDefElemVolatility(StringInfo buf, DefElem *def) static void AppendDefElemLeakproof(StringInfo buf, DefElem *def) { - if (intVal(def->arg) == 0) + if (!boolVal(def->arg)) { appendStringInfo(buf, " NOT"); } @@ -237,7 +237,7 @@ AppendDefElemLeakproof(StringInfo buf, DefElem *def) static void AppendDefElemSecurity(StringInfo buf, DefElem *def) { - if (intVal(def->arg) == 0) + if (!boolVal(def->arg)) { appendStringInfo(buf, " SECURITY INVOKER"); } diff --git a/src/backend/distributed/deparser/deparse_role_stmts.c b/src/backend/distributed/deparser/deparse_role_stmts.c index 2af073e89..0e9b300bb 100644 --- a/src/backend/distributed/deparser/deparse_role_stmts.c +++ b/src/backend/distributed/deparser/deparse_role_stmts.c @@ -13,6 +13,8 @@ #include "postgres.h" +#include "pg_version_compat.h" + #include "distributed/citus_ruleutils.h" #include "distributed/deparser.h" #include "lib/stringinfo.h" @@ -98,59 +100,59 @@ AppendRoleOption(StringInfo buf, ListCell *optionCell) { DefElem *option = (DefElem *) lfirst(optionCell); - if (strcmp(option->defname, "superuser") == 0 && intVal(option->arg)) + if (strcmp(option->defname, "superuser") == 0 && boolVal(option->arg)) { appendStringInfo(buf, " SUPERUSER"); } - else if (strcmp(option->defname, "superuser") == 0 && !intVal(option->arg)) + else if (strcmp(option->defname, "superuser") == 0 && !boolVal(option->arg)) { appendStringInfo(buf, " NOSUPERUSER"); } - else if (strcmp(option->defname, "createdb") == 0 && intVal(option->arg)) + else if (strcmp(option->defname, "createdb") == 0 && boolVal(option->arg)) { appendStringInfo(buf, " CREATEDB"); } - else if (strcmp(option->defname, "createdb") == 0 && !intVal(option->arg)) + else if (strcmp(option->defname, "createdb") == 0 && !boolVal(option->arg)) { appendStringInfo(buf, " NOCREATEDB"); } - else if (strcmp(option->defname, "createrole") == 0 && intVal(option->arg)) + else if (strcmp(option->defname, "createrole") == 0 && boolVal(option->arg)) { appendStringInfo(buf, " CREATEROLE"); } - else if (strcmp(option->defname, "createrole") == 0 && !intVal(option->arg)) + else if (strcmp(option->defname, "createrole") == 0 && !boolVal(option->arg)) { appendStringInfo(buf, " NOCREATEROLE"); } - else if (strcmp(option->defname, "inherit") == 0 && intVal(option->arg)) + else if (strcmp(option->defname, "inherit") == 0 && boolVal(option->arg)) { appendStringInfo(buf, " INHERIT"); } - else if (strcmp(option->defname, "inherit") == 0 && !intVal(option->arg)) + else if (strcmp(option->defname, "inherit") == 0 && !boolVal(option->arg)) { appendStringInfo(buf, " NOINHERIT"); } - else if (strcmp(option->defname, "canlogin") == 0 && intVal(option->arg)) + else if (strcmp(option->defname, "canlogin") == 0 && boolVal(option->arg)) { appendStringInfo(buf, " LOGIN"); } - else if (strcmp(option->defname, "canlogin") == 0 && !intVal(option->arg)) + else if (strcmp(option->defname, "canlogin") == 0 && !boolVal(option->arg)) { appendStringInfo(buf, " NOLOGIN"); } - else if (strcmp(option->defname, "isreplication") == 0 && intVal(option->arg)) + else if (strcmp(option->defname, "isreplication") == 0 && boolVal(option->arg)) { appendStringInfo(buf, " REPLICATION"); } - else if (strcmp(option->defname, "isreplication") == 0 && !intVal(option->arg)) + else if (strcmp(option->defname, "isreplication") == 0 && !boolVal(option->arg)) { appendStringInfo(buf, " NOREPLICATION"); } - else if (strcmp(option->defname, "bypassrls") == 0 && intVal(option->arg)) + else if (strcmp(option->defname, "bypassrls") == 0 && boolVal(option->arg)) { appendStringInfo(buf, " BYPASSRLS"); } - else if (strcmp(option->defname, "bypassrls") == 0 && !intVal(option->arg)) + else if (strcmp(option->defname, "bypassrls") == 0 && !boolVal(option->arg)) { appendStringInfo(buf, " NOBYPASSRLS"); } diff --git a/src/include/pg_version_compat.h b/src/include/pg_version_compat.h index 2e242cfe1..f551085a7 100644 --- a/src/include/pg_version_compat.h +++ b/src/include/pg_version_compat.h @@ -39,6 +39,8 @@ typedef Value String; #define pgstat_init_relation(r) pgstat_initstats(r) #define pg_analyze_and_rewrite_fixedparams(a, b, c, d, e) pg_analyze_and_rewrite(a, b, c, \ d, e) +#define boolVal(v) intVal(v) +#define makeBoolean(val) makeInteger(val) static inline int64 pg_strtoint64(char *s)