Adds alter database set option (#7181)

DESCRIPTION: Adds support for ALTER DATABASE <db_name> SET .. statement
propagation
SET statements in Postgres has a common structure which is already being
used in Alter Function
statement. 
In this PR, I added a util file; citus_setutils and made it usable for
both for
alter database<db_name>set .. and alter function ... set ... statements.
With this PR, below statements will be propagated
```sql
ALTER DATABASE name SET configuration_parameter { TO | = } { value | DEFAULT }
ALTER DATABASE name SET configuration_parameter FROM CURRENT
ALTER DATABASE name RESET configuration_parameter
ALTER DATABASE name RESET ALL
```
Additionally, there was a bug in processing float values in the common
code block.
I fixed this one as well

Previous
```C
case T_Float:
			{
				appendStringInfo(buf, " %s", strVal(value));
				break;
			}
```
Now
```C
case T_Float:
			{
				appendStringInfo(buf, " %s", nodeToString(value));
				break;
			}
```
pull/7201/head
Gürkan İndibay 2023-09-14 16:29:16 +03:00 committed by GitHub
parent 26dc407f4a
commit 7c0b289761
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 411 additions and 159 deletions

View File

@ -212,3 +212,33 @@ PreprocessAlterDatabaseRefreshCollStmt(Node *node, const char *queryString,
#endif
/*
* PreprocessAlterDatabaseSetStmt is executed before the statement is applied to the local
* postgres instance.
*
* In this stage we can prepare the commands that need to be run on all workers to grant
* on databases.
*/
List *
PreprocessAlterDatabaseSetStmt(Node *node, const char *queryString,
ProcessUtilityContext processUtilityContext)
{
if (!ShouldPropagate())
{
return NIL;
}
AlterDatabaseSetStmt *stmt = castNode(AlterDatabaseSetStmt, node);
EnsureCoordinator();
char *sql = DeparseTreeNode((Node *) stmt);
List *commands = list_make3(DISABLE_DDL_PROPAGATION,
(void *) sql,
ENABLE_DDL_PROPAGATION);
return NodeDDLTaskList(NON_COORDINATOR_NODES, commands);
}

View File

@ -468,6 +468,18 @@ static DistributeObjectOps Database_RefreshColl = {
};
#endif
static DistributeObjectOps Database_Set = {
.deparse = DeparseAlterDatabaseSetStmt,
.qualify = NULL,
.preprocess = PreprocessAlterDatabaseSetStmt,
.postprocess = NULL,
.objectType = OBJECT_DATABASE,
.operationType = DIST_OPS_ALTER,
.address = NULL,
.markDistributed = false,
};
static DistributeObjectOps Domain_Alter = {
.deparse = DeparseAlterDomainStmt,
.qualify = QualifyAlterDomainStmt,
@ -1318,6 +1330,13 @@ GetDistributeObjectOps(Node *node)
}
#endif
case T_AlterDatabaseSetStmt:
{
return &Database_Set;
}
case T_AlterDomainStmt:
{
return &Domain_Alter;

View File

@ -0,0 +1,174 @@
#include "postgres.h"
#include "pg_version_compat.h"
#include "catalog/namespace.h"
#include "lib/stringinfo.h"
#include "nodes/parsenodes.h"
#include "utils/builtins.h"
#include "distributed/deparser.h"
#include "distributed/citus_ruleutils.h"
#include "commands/defrem.h"
#include "distributed/log_utils.h"
#include "parser/parse_type.h"
#include "nodes/print.h"
void AppendVarSetValue(StringInfo buf, VariableSetStmt *setStmt);
/*
* AppendVarSetValueDb deparses a VariableSetStmt with VAR_SET_VALUE kind.
* It takes from flatten_set_variable_args in postgres's utils/misc/guc.c,
* however flatten_set_variable_args does not apply correct quoting.
*/
void
AppendVarSetValue(StringInfo buf, VariableSetStmt *setStmt)
{
ListCell *varArgCell = NULL;
ListCell *firstCell = list_head(setStmt->args);
Assert(setStmt->kind == VAR_SET_VALUE);
foreach(varArgCell, setStmt->args)
{
Node *varArgNode = lfirst(varArgCell);
A_Const *varArgConst = NULL;
TypeName *typeName = NULL;
if (IsA(varArgNode, A_Const))
{
varArgConst = (A_Const *) varArgNode;
}
else if (IsA(varArgNode, TypeCast))
{
TypeCast *varArgTypeCast = (TypeCast *) varArgNode;
varArgConst = castNode(A_Const, varArgTypeCast->arg);
typeName = varArgTypeCast->typeName;
}
else
{
elog(ERROR, "unrecognized node type: %d", varArgNode->type);
}
/* don't know how to start SET until we inspect first arg */
if (varArgCell != firstCell)
{
appendStringInfoChar(buf, ',');
}
else if (typeName != NULL)
{
appendStringInfoString(buf, " SET TIME ZONE");
}
else
{
appendStringInfo(buf, " SET %s =", quote_identifier(setStmt->name));
}
Node *value = (Node *) &varArgConst->val;
switch (value->type)
{
case T_Integer:
{
appendStringInfo(buf, " %d", intVal(value));
break;
}
case T_Float:
{
appendStringInfo(buf, " %s", nodeToString(value));
break;
}
case T_String:
{
if (typeName != NULL)
{
/*
* Must be a ConstInterval argument for TIME ZONE. Coerce
* to interval and back to normalize the value and account
* for any typmod.
*/
Oid typoid = InvalidOid;
int32 typmod = -1;
typenameTypeIdAndMod(NULL, typeName, &typoid, &typmod);
Assert(typoid == INTERVALOID);
Datum interval =
DirectFunctionCall3(interval_in,
CStringGetDatum(strVal(value)),
ObjectIdGetDatum(InvalidOid),
Int32GetDatum(typmod));
char *intervalout =
DatumGetCString(DirectFunctionCall1(interval_out,
interval));
appendStringInfo(buf, " INTERVAL '%s'", intervalout);
}
else
{
appendStringInfo(buf, " %s", quote_literal_cstr(strVal(value)));
}
break;
}
default:
{
elog(ERROR, "Unexpected Value type in VAR_SET_VALUE arguments.");
break;
}
}
}
}
/*
* AppendVariableSetDb appends a string representing the VariableSetStmt to a buffer
*/
void
AppendVariableSet(StringInfo buf, VariableSetStmt *setStmt)
{
switch (setStmt->kind)
{
case VAR_SET_VALUE:
{
AppendVarSetValue(buf, setStmt);
break;
}
case VAR_SET_CURRENT:
{
appendStringInfo(buf, " SET %s FROM CURRENT", quote_identifier(
setStmt->name));
break;
}
case VAR_SET_DEFAULT:
{
appendStringInfo(buf, " SET %s TO DEFAULT", quote_identifier(setStmt->name));
break;
}
case VAR_RESET:
{
appendStringInfo(buf, " RESET %s", quote_identifier(setStmt->name));
break;
}
case VAR_RESET_ALL:
{
appendStringInfoString(buf, " RESET ALL");
break;
}
/* VAR_SET_MULTI is a special case for SET TRANSACTION that should not occur here */
case VAR_SET_MULTI:
default:
{
ereport(ERROR, (errmsg("Unable to deparse SET statement")));
break;
}
}
}

View File

@ -23,9 +23,12 @@
#include "commands/defrem.h"
#include "distributed/deparser.h"
#include "distributed/log_utils.h"
#include "parser/parse_type.h"
static void AppendAlterDatabaseOwnerStmt(StringInfo buf, AlterOwnerStmt *stmt);
static void AppendAlterDatabaseStmt(StringInfo buf, AlterDatabaseStmt *stmt);
static void AppendDefElemConnLimit(StringInfo buf, DefElem *def);
char *
DeparseAlterDatabaseOwnerStmt(Node *node)
@ -178,3 +181,27 @@ DeparseAlterDatabaseRefreshCollStmt(Node *node)
#endif
static void
AppendAlterDatabaseSetStmt(StringInfo buf, AlterDatabaseSetStmt *stmt)
{
appendStringInfo(buf, "ALTER DATABASE %s", quote_identifier(stmt->dbname));
VariableSetStmt *varSetStmt = castNode(VariableSetStmt, stmt->setstmt);
AppendVariableSet(buf, varSetStmt);
}
char *
DeparseAlterDatabaseSetStmt(Node *node)
{
AlterDatabaseSetStmt *stmt = castNode(AlterDatabaseSetStmt, node);
StringInfoData str = { 0 };
initStringInfo(&str);
AppendAlterDatabaseSetStmt(&str, stmt);
return str.data;
}

View File

@ -61,7 +61,6 @@ static void AppendDefElemRows(StringInfo buf, DefElem *def);
static void AppendDefElemSet(StringInfo buf, DefElem *def);
static void AppendDefElemSupport(StringInfo buf, DefElem *def);
static void AppendVarSetValue(StringInfo buf, VariableSetStmt *setStmt);
static void AppendRenameFunctionStmt(StringInfo buf, RenameStmt *stmt);
static void AppendAlterFunctionSchemaStmt(StringInfo buf, AlterObjectSchemaStmt *stmt);
static void AppendAlterFunctionOwnerStmt(StringInfo buf, AlterOwnerStmt *stmt);
@ -300,164 +299,6 @@ AppendDefElemSupport(StringInfo buf, DefElem *def)
}
/*
* AppendVariableSet appends a string representing the VariableSetStmt to a buffer
*/
void
AppendVariableSet(StringInfo buf, VariableSetStmt *setStmt)
{
switch (setStmt->kind)
{
case VAR_SET_VALUE:
{
AppendVarSetValue(buf, setStmt);
break;
}
case VAR_SET_CURRENT:
{
appendStringInfo(buf, " SET %s FROM CURRENT", quote_identifier(
setStmt->name));
break;
}
case VAR_SET_DEFAULT:
{
appendStringInfo(buf, " SET %s TO DEFAULT", quote_identifier(setStmt->name));
break;
}
case VAR_RESET:
{
appendStringInfo(buf, " RESET %s", quote_identifier(setStmt->name));
break;
}
case VAR_RESET_ALL:
{
appendStringInfoString(buf, " RESET ALL");
break;
}
/* VAR_SET_MULTI is a special case for SET TRANSACTION that should not occur here */
case VAR_SET_MULTI:
default:
{
ereport(ERROR, (errmsg("Unable to deparse SET statement")));
break;
}
}
}
/*
* AppendVarSetValue deparses a VariableSetStmt with VAR_SET_VALUE kind.
* It takes from flatten_set_variable_args in postgres's utils/misc/guc.c,
* however flatten_set_variable_args does not apply correct quoting.
*/
static void
AppendVarSetValue(StringInfo buf, VariableSetStmt *setStmt)
{
ListCell *varArgCell = NULL;
ListCell *firstCell = list_head(setStmt->args);
Assert(setStmt->kind == VAR_SET_VALUE);
foreach(varArgCell, setStmt->args)
{
Node *varArgNode = lfirst(varArgCell);
A_Const *varArgConst = NULL;
TypeName *typeName = NULL;
if (IsA(varArgNode, A_Const))
{
varArgConst = (A_Const *) varArgNode;
}
else if (IsA(varArgNode, TypeCast))
{
TypeCast *varArgTypeCast = (TypeCast *) varArgNode;
varArgConst = castNode(A_Const, varArgTypeCast->arg);
typeName = varArgTypeCast->typeName;
}
else
{
elog(ERROR, "unrecognized node type: %d", varArgNode->type);
}
/* don't know how to start SET until we inspect first arg */
if (varArgCell != firstCell)
{
appendStringInfoChar(buf, ',');
}
else if (typeName != NULL)
{
appendStringInfoString(buf, " SET TIME ZONE");
}
else
{
appendStringInfo(buf, " SET %s =", quote_identifier(setStmt->name));
}
Node *value = (Node *) &varArgConst->val;
switch (value->type)
{
case T_Integer:
{
appendStringInfo(buf, " %d", intVal(value));
break;
}
case T_Float:
{
appendStringInfo(buf, " %s", strVal(value));
break;
}
case T_String:
{
if (typeName != NULL)
{
/*
* Must be a ConstInterval argument for TIME ZONE. Coerce
* to interval and back to normalize the value and account
* for any typmod.
*/
Oid typoid = InvalidOid;
int32 typmod = -1;
typenameTypeIdAndMod(NULL, typeName, &typoid, &typmod);
Assert(typoid == INTERVALOID);
Datum interval =
DirectFunctionCall3(interval_in,
CStringGetDatum(strVal(value)),
ObjectIdGetDatum(InvalidOid),
Int32GetDatum(typmod));
char *intervalout =
DatumGetCString(DirectFunctionCall1(interval_out,
interval));
appendStringInfo(buf, " INTERVAL '%s'", intervalout);
}
else
{
appendStringInfo(buf, " %s", quote_literal_cstr(strVal(
value)));
}
break;
}
default:
{
elog(ERROR, "Unexpected Value type in VAR_SET_VALUE arguments.");
break;
}
}
}
}
/*
* DeparseRenameFunctionStmt builds and returns a string representing the RenameStmt
*/

View File

@ -231,6 +231,10 @@ extern List * PreprocessAlterDatabaseRefreshCollStmt(Node *node, const char *que
processUtilityContext);
extern List * PreprocessAlterDatabaseSetStmt(Node *node, const char *queryString,
ProcessUtilityContext processUtilityContext);
/* domain.c - forward declarations */
extern List * CreateDomainStmtObjectAddress(Node *node, bool missing_ok, bool
isPostprocess);

View File

@ -225,6 +225,8 @@ extern char * DeparseAlterDatabaseOwnerStmt(Node *node);
extern char * DeparseGrantOnDatabaseStmt(Node *node);
extern char * DeparseAlterDatabaseStmt(Node *node);
extern char * DeparseAlterDatabaseRefreshCollStmt(Node *node);
extern char * DeparseAlterDatabaseSetStmt(Node *node);
/* forward declaration for deparse_publication_stmts.c */
extern char * DeparseCreatePublicationStmt(Node *stmt);

View File

@ -33,4 +33,118 @@ DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
-- this statement will get error since we don't have a multiple database support for now
alter database regression rename to regression2;
ERROR: current database cannot be renamed
alter database regression set default_transaction_read_only = true;
NOTICE: issuing ALTER DATABASE regression SET default_transaction_read_only = 'true'
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
NOTICE: issuing ALTER DATABASE regression SET default_transaction_read_only = 'true'
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
set default_transaction_read_only = false;
alter database regression set default_transaction_read_only from current;
NOTICE: issuing ALTER DATABASE regression SET default_transaction_read_only FROM CURRENT
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
NOTICE: issuing ALTER DATABASE regression SET default_transaction_read_only FROM CURRENT
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
alter database regression set default_transaction_read_only to DEFAULT;
NOTICE: issuing ALTER DATABASE regression SET default_transaction_read_only TO DEFAULT
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
NOTICE: issuing ALTER DATABASE regression SET default_transaction_read_only TO DEFAULT
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
alter database regression RESET default_transaction_read_only;
NOTICE: issuing ALTER DATABASE regression RESET default_transaction_read_only
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
NOTICE: issuing ALTER DATABASE regression RESET default_transaction_read_only
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
alter database regression SET TIME ZONE '-7';
NOTICE: issuing ALTER DATABASE regression SET timezone = '-7'
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
NOTICE: issuing ALTER DATABASE regression SET timezone = '-7'
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
alter database regression set TIME ZONE LOCAL;
NOTICE: issuing ALTER DATABASE regression SET timezone TO DEFAULT
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
NOTICE: issuing ALTER DATABASE regression SET timezone TO DEFAULT
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
alter database regression set TIME ZONE DEFAULT;
NOTICE: issuing ALTER DATABASE regression SET timezone TO DEFAULT
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
NOTICE: issuing ALTER DATABASE regression SET timezone TO DEFAULT
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
alter database regression RESET TIME ZONE;
NOTICE: issuing ALTER DATABASE regression RESET timezone
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
NOTICE: issuing ALTER DATABASE regression RESET timezone
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
alter database regression SET TIME ZONE INTERVAL '-08:00' HOUR TO MINUTE;
NOTICE: issuing ALTER DATABASE regression SET TIME ZONE INTERVAL '@ 8 hours ago'
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
NOTICE: issuing ALTER DATABASE regression SET TIME ZONE INTERVAL '@ 8 hours ago'
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
alter database regression RESET TIME ZONE;
NOTICE: issuing ALTER DATABASE regression RESET timezone
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
NOTICE: issuing ALTER DATABASE regression RESET timezone
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
alter database regression set default_transaction_isolation = 'serializable';
NOTICE: issuing ALTER DATABASE regression SET default_transaction_isolation = 'serializable'
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
NOTICE: issuing ALTER DATABASE regression SET default_transaction_isolation = 'serializable'
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
set default_transaction_isolation = 'read committed';
alter database regression set default_transaction_isolation from current;
NOTICE: issuing ALTER DATABASE regression SET default_transaction_isolation FROM CURRENT
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
NOTICE: issuing ALTER DATABASE regression SET default_transaction_isolation FROM CURRENT
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
alter database regression set default_transaction_isolation to DEFAULT;
NOTICE: issuing ALTER DATABASE regression SET default_transaction_isolation TO DEFAULT
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
NOTICE: issuing ALTER DATABASE regression SET default_transaction_isolation TO DEFAULT
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
alter database regression RESET default_transaction_isolation;
NOTICE: issuing ALTER DATABASE regression RESET default_transaction_isolation
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
NOTICE: issuing ALTER DATABASE regression RESET default_transaction_isolation
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
alter database regression set statement_timeout = 1000;
NOTICE: issuing ALTER DATABASE regression SET statement_timeout = 1000
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
NOTICE: issuing ALTER DATABASE regression SET statement_timeout = 1000
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
set statement_timeout = 2000;
alter database regression set statement_timeout from current;
NOTICE: issuing ALTER DATABASE regression SET statement_timeout FROM CURRENT
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
NOTICE: issuing ALTER DATABASE regression SET statement_timeout FROM CURRENT
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
alter database regression set statement_timeout to DEFAULT;
NOTICE: issuing ALTER DATABASE regression SET statement_timeout TO DEFAULT
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
NOTICE: issuing ALTER DATABASE regression SET statement_timeout TO DEFAULT
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
alter database regression RESET statement_timeout;
NOTICE: issuing ALTER DATABASE regression RESET statement_timeout
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
NOTICE: issuing ALTER DATABASE regression RESET statement_timeout
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
alter database regression set lock_timeout = 1201.5;
NOTICE: issuing ALTER DATABASE regression SET lock_timeout = 1201.5
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
NOTICE: issuing ALTER DATABASE regression SET lock_timeout = 1201.5
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
set lock_timeout = 1202.5;
alter database regression set lock_timeout from current;
NOTICE: issuing ALTER DATABASE regression SET lock_timeout FROM CURRENT
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
NOTICE: issuing ALTER DATABASE regression SET lock_timeout FROM CURRENT
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
alter database regression set lock_timeout to DEFAULT;
NOTICE: issuing ALTER DATABASE regression SET lock_timeout TO DEFAULT
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
NOTICE: issuing ALTER DATABASE regression SET lock_timeout TO DEFAULT
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
alter database regression RESET lock_timeout;
NOTICE: issuing ALTER DATABASE regression RESET lock_timeout
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
NOTICE: issuing ALTER DATABASE regression RESET lock_timeout
DETAIL: on server postgres@localhost:xxxxx connectionId: xxxxxxx
set citus.log_remote_commands = false;

View File

@ -1,6 +1,7 @@
set citus.log_remote_commands = true;
set citus.grep_remote_commands = '%ALTER DATABASE%';
-- since ALLOW_CONNECTIONS alter option should be executed in a different database
-- and since we don't have a multiple database support for now,
-- this statement will get error
@ -15,4 +16,44 @@ alter database regression with IS_TEMPLATE false;
-- this statement will get error since we don't have a multiple database support for now
alter database regression rename to regression2;
alter database regression set default_transaction_read_only = true;
set default_transaction_read_only = false;
alter database regression set default_transaction_read_only from current;
alter database regression set default_transaction_read_only to DEFAULT;
alter database regression RESET default_transaction_read_only;
alter database regression SET TIME ZONE '-7';
alter database regression set TIME ZONE LOCAL;
alter database regression set TIME ZONE DEFAULT;
alter database regression RESET TIME ZONE;
alter database regression SET TIME ZONE INTERVAL '-08:00' HOUR TO MINUTE;
alter database regression RESET TIME ZONE;
alter database regression set default_transaction_isolation = 'serializable';
set default_transaction_isolation = 'read committed';
alter database regression set default_transaction_isolation from current;
alter database regression set default_transaction_isolation to DEFAULT;
alter database regression RESET default_transaction_isolation;
alter database regression set statement_timeout = 1000;
set statement_timeout = 2000;
alter database regression set statement_timeout from current;
alter database regression set statement_timeout to DEFAULT;
alter database regression RESET statement_timeout;
alter database regression set lock_timeout = 1201.5;
set lock_timeout = 1202.5;
alter database regression set lock_timeout from current;
alter database regression set lock_timeout to DEFAULT;
alter database regression RESET lock_timeout;
set citus.log_remote_commands = false;