get rid of {Push/Pop}OverrideSearchPath (#7145)

pull/7170/head
zhjwpku 2023-09-05 23:40:22 +08:00 committed by GitHub
parent 8eb3360017
commit 205b159606
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 80 additions and 98 deletions

View File

@ -53,6 +53,7 @@
#include "distributed/multi_executor.h"
#include "distributed/multi_logical_planner.h"
#include "distributed/multi_partitioning_utils.h"
#include "distributed/namespace_utils.h"
#include "distributed/reference_table_utils.h"
#include "distributed/relation_access_tracking.h"
#include "distributed/replication_origin_session_utils.h"
@ -1764,10 +1765,7 @@ CreateMaterializedViewDDLCommand(Oid matViewOid)
* Set search_path to NIL so that all objects outside of pg_catalog will be
* schema-prefixed.
*/
OverrideSearchPath *overridePath = GetOverrideSearchPath(CurrentMemoryContext);
overridePath->schemas = NIL;
overridePath->addCatalog = true;
PushOverrideSearchPath(overridePath);
int saveNestLevel = PushEmptySearchPath();
/*
* Push the transaction snapshot to be able to get vief definition with pg_get_viewdef
@ -1779,7 +1777,7 @@ CreateMaterializedViewDDLCommand(Oid matViewOid)
char *viewDefinition = TextDatumGetCString(viewDefinitionDatum);
PopActiveSnapshot();
PopOverrideSearchPath();
PopEmptySearchPath(saveNestLevel);
appendStringInfo(query, "AS %s", viewDefinition);

View File

@ -50,7 +50,7 @@ static List * GetAllViews(void);
static bool ShouldPropagateExtensionCommand(Node *parseTree);
static bool IsAlterExtensionSetSchemaCitus(Node *parseTree);
static Node * RecreateExtensionStmt(Oid extensionOid);
static List * GenerateGrantCommandsOnExtesionDependentFDWs(Oid extensionId);
static List * GenerateGrantCommandsOnExtensionDependentFDWs(Oid extensionId);
/*
@ -985,7 +985,7 @@ CreateExtensionDDLCommand(const ObjectAddress *extensionAddress)
/* any privilege granted on FDWs that belong to the extension should be included */
List *FDWGrants =
GenerateGrantCommandsOnExtesionDependentFDWs(extensionAddress->objectId);
GenerateGrantCommandsOnExtensionDependentFDWs(extensionAddress->objectId);
ddlCommands = list_concat(ddlCommands, FDWGrants);
@ -1048,11 +1048,11 @@ RecreateExtensionStmt(Oid extensionOid)
/*
* GenerateGrantCommandsOnExtesionDependentFDWs returns a list of commands that GRANTs
* GenerateGrantCommandsOnExtensionDependentFDWs returns a list of commands that GRANTs
* the privileges on FDWs that are depending on the given extension.
*/
static List *
GenerateGrantCommandsOnExtesionDependentFDWs(Oid extensionId)
GenerateGrantCommandsOnExtensionDependentFDWs(Oid extensionId)
{
List *commands = NIL;
List *FDWOids = GetDependentFDWsToExtension(extensionId);

View File

@ -895,7 +895,7 @@ GetForeignConstraintCommandsInternal(Oid relationId, int flags)
List *foreignKeyCommands = NIL;
PushOverrideEmptySearchPath(CurrentMemoryContext);
int saveNestLevel = PushEmptySearchPath();
Oid foreignKeyOid = InvalidOid;
foreach_oid(foreignKeyOid, foreignKeyOids)
@ -906,7 +906,7 @@ GetForeignConstraintCommandsInternal(Oid relationId, int flags)
}
/* revert back to original search_path */
PopOverrideSearchPath();
PopEmptySearchPath(saveNestLevel);
return foreignKeyCommands;
}

View File

@ -909,15 +909,14 @@ GetFunctionDDLCommand(const RegProcedure funcOid, bool useCreateOrReplace)
else
{
Datum sqlTextDatum = (Datum) 0;
PushOverrideEmptySearchPath(CurrentMemoryContext);
int saveNestLevel = PushEmptySearchPath();
sqlTextDatum = DirectFunctionCall1(pg_get_functiondef,
ObjectIdGetDatum(funcOid));
createFunctionSQL = TextDatumGetCString(sqlTextDatum);
/* revert back to original search_path */
PopOverrideSearchPath();
PopEmptySearchPath(saveNestLevel);
}
return createFunctionSQL;

View File

@ -530,7 +530,7 @@ GetExplicitStatisticsCommandList(Oid relationId)
RelationClose(relation);
/* generate fully-qualified names */
PushOverrideEmptySearchPath(CurrentMemoryContext);
int saveNestLevel = PushEmptySearchPath();
Oid statisticsId = InvalidOid;
foreach_oid(statisticsId, statisticsIdList)
@ -579,7 +579,7 @@ GetExplicitStatisticsCommandList(Oid relationId)
}
/* revert back to original search_path */
PopOverrideSearchPath();
PopEmptySearchPath(saveNestLevel);
return explicitStatisticsCommandList;
}

View File

@ -74,7 +74,7 @@ GetExplicitTriggerCommandList(Oid relationId)
{
List *createTriggerCommandList = NIL;
PushOverrideEmptySearchPath(CurrentMemoryContext);
int saveNestLevel = PushEmptySearchPath();
List *triggerIdList = GetExplicitTriggerIdList(relationId);
@ -116,7 +116,7 @@ GetExplicitTriggerCommandList(Oid relationId)
}
/* revert back to original search_path */
PopOverrideSearchPath();
PopEmptySearchPath(saveNestLevel);
return createTriggerCommandList;
}

View File

@ -479,10 +479,7 @@ AppendViewDefinitionToCreateViewCommand(StringInfo buf, Oid viewOid)
* Set search_path to NIL so that all objects outside of pg_catalog will be
* schema-prefixed.
*/
OverrideSearchPath *overridePath = GetOverrideSearchPath(CurrentMemoryContext);
overridePath->schemas = NIL;
overridePath->addCatalog = true;
PushOverrideSearchPath(overridePath);
int saveNestLevel = PushEmptySearchPath();
/*
* Push the transaction snapshot to be able to get vief definition with pg_get_viewdef
@ -494,7 +491,7 @@ AppendViewDefinitionToCreateViewCommand(StringInfo buf, Oid viewOid)
char *viewDefinition = TextDatumGetCString(viewDefinitionDatum);
PopActiveSnapshot();
PopOverrideSearchPath();
PopEmptySearchPath(saveNestLevel);
appendStringInfo(buf, "AS %s ", viewDefinition);
}

View File

@ -818,7 +818,7 @@ deparse_shard_index_statement(IndexStmt *origStmt, Oid distrelid, int64 shardid,
* Switch to empty search_path to deparse_index_columns to produce fully-
* qualified names in expressions.
*/
PushOverrideEmptySearchPath(CurrentMemoryContext);
int saveNestLevel = PushEmptySearchPath();
/* index column or expression list begins here */
appendStringInfoChar(buffer, '(');
@ -855,7 +855,7 @@ deparse_shard_index_statement(IndexStmt *origStmt, Oid distrelid, int64 shardid,
}
/* revert back to original search_path */
PopOverrideSearchPath();
PopEmptySearchPath(saveNestLevel);
}

View File

@ -345,9 +345,9 @@ AppendAlterDomainStmtSetDefault(StringInfo buf, AlterDomainStmt *stmt)
expr = TransformDefaultExpr(expr, stmt->typeName, baseTypeName);
/* deparse while the searchpath is cleared to force qualification of identifiers */
PushOverrideEmptySearchPath(CurrentMemoryContext);
int saveNestLevel = PushEmptySearchPath();
char *exprSql = deparse_expression(expr, NIL, true, true);
PopOverrideSearchPath();
PopEmptySearchPath(saveNestLevel);
appendStringInfo(buf, "SET DEFAULT %s", exprSql);
}
@ -443,9 +443,9 @@ AppendConstraint(StringInfo buf, Constraint *constraint, List *domainName,
elog(ERROR, "missing expression for domain constraint");
}
PushOverrideEmptySearchPath(CurrentMemoryContext);
int saveNestLevel = PushEmptySearchPath();
char *exprSql = deparse_expression(expr, NIL, true, true);
PopOverrideSearchPath();
PopEmptySearchPath(saveNestLevel);
appendStringInfo(buf, " CHECK (%s)", exprSql);
return;
@ -469,9 +469,9 @@ AppendConstraint(StringInfo buf, Constraint *constraint, List *domainName,
elog(ERROR, "missing expression for domain default");
}
PushOverrideEmptySearchPath(CurrentMemoryContext);
int saveNestLevel = PushEmptySearchPath();
char *exprSql = deparse_expression(expr, NIL, true, true);
PopOverrideSearchPath();
PopEmptySearchPath(saveNestLevel);
appendStringInfo(buf, " DEFAULT %s", exprSql);
return;

View File

@ -307,11 +307,11 @@ AppendWhereClauseExpression(StringInfo buf, RangeVar *tableName,
List *relationContext = deparse_context_for(tableName->relname, relation->rd_id);
PushOverrideEmptySearchPath(CurrentMemoryContext);
int saveNestLevel = PushEmptySearchPath();
char *whereClauseString = deparse_expression(whereClause,
relationContext,
true, true);
PopOverrideSearchPath();
PopEmptySearchPath(saveNestLevel);
appendStringInfoString(buf, whereClauseString);

View File

@ -562,9 +562,9 @@ DeparseRawExprForColumnDefault(Oid relationId, Oid columnTypeId, int32 columnTyp
List *deparseContext = deparse_context_for(get_rel_name(relationId), relationId);
PushOverrideEmptySearchPath(CurrentMemoryContext);
int saveNestLevel = PushEmptySearchPath();
char *defaultExprStr = deparse_expression(defaultExpr, deparseContext, false, false);
PopOverrideSearchPath();
PopEmptySearchPath(saveNestLevel);
RelationClose(relation);

View File

@ -53,6 +53,7 @@
#include "common/keywords.h"
#include "distributed/citus_nodefuncs.h"
#include "distributed/citus_ruleutils.h"
#include "distributed/namespace_utils.h"
#include "executor/spi.h"
#include "foreign/foreign.h"
#include "funcapi.h"
@ -610,18 +611,14 @@ pg_get_rule_expr(Node *expression)
{
bool showImplicitCasts = true;
deparse_context context;
OverrideSearchPath *overridePath = NULL;
StringInfo buffer = makeStringInfo();
/*
* Set search_path to NIL so that all objects outside of pg_catalog will be
* schema-prefixed. pg_catalog will be added automatically when we call
* PushOverrideSearchPath(), since we set addCatalog to true;
* PushEmptySearchPath().
*/
overridePath = GetOverrideSearchPath(CurrentMemoryContext);
overridePath->schemas = NIL;
overridePath->addCatalog = true;
PushOverrideSearchPath(overridePath);
int saveNestLevel = PushEmptySearchPath();
context.buf = buffer;
context.namespaces = NIL;
@ -638,7 +635,7 @@ pg_get_rule_expr(Node *expression)
get_rule_expr(expression, &context, showImplicitCasts);
/* revert back to original search_path */
PopOverrideSearchPath();
PopEmptySearchPath(saveNestLevel);
return buffer->data;
}
@ -1955,8 +1952,6 @@ get_query_def_extended(Query *query, StringInfo buf, List *parentnamespace,
deparse_context context;
deparse_namespace dpns;
OverrideSearchPath *overridePath = NULL;
/* Guard against excessively long or deeply-nested queries */
CHECK_FOR_INTERRUPTS();
check_stack_depth();
@ -1975,12 +1970,9 @@ get_query_def_extended(Query *query, StringInfo buf, List *parentnamespace,
/*
* Set search_path to NIL so that all objects outside of pg_catalog will be
* schema-prefixed. pg_catalog will be added automatically when we call
* PushOverrideSearchPath(), since we set addCatalog to true;
* PushEmptySearchPath().
*/
overridePath = GetOverrideSearchPath(CurrentMemoryContext);
overridePath->schemas = NIL;
overridePath->addCatalog = true;
PushOverrideSearchPath(overridePath);
int saveNestLevel = PushEmptySearchPath();
context.buf = buf;
context.namespaces = lcons(&dpns, list_copy(parentnamespace));
@ -2031,7 +2023,7 @@ get_query_def_extended(Query *query, StringInfo buf, List *parentnamespace,
}
/* revert back to original search_path */
PopOverrideSearchPath();
PopEmptySearchPath(saveNestLevel);
}
/* ----------

View File

@ -54,6 +54,7 @@
#include "distributed/citus_nodefuncs.h"
#include "distributed/citus_ruleutils.h"
#include "distributed/multi_router_planner.h"
#include "distributed/namespace_utils.h"
#include "executor/spi.h"
#include "foreign/foreign.h"
#include "funcapi.h"
@ -624,18 +625,14 @@ pg_get_rule_expr(Node *expression)
{
bool showImplicitCasts = true;
deparse_context context;
OverrideSearchPath *overridePath = NULL;
StringInfo buffer = makeStringInfo();
/*
* Set search_path to NIL so that all objects outside of pg_catalog will be
* schema-prefixed. pg_catalog will be added automatically when we call
* PushOverrideSearchPath(), since we set addCatalog to true;
* PushEmptySearchPath(), since we set addCatalog to true;
*/
overridePath = GetOverrideSearchPath(CurrentMemoryContext);
overridePath->schemas = NIL;
overridePath->addCatalog = true;
PushOverrideSearchPath(overridePath);
int saveNestLevel = PushEmptySearchPath();
context.buf = buffer;
context.namespaces = NIL;
@ -652,7 +649,7 @@ pg_get_rule_expr(Node *expression)
get_rule_expr(expression, &context, showImplicitCasts);
/* revert back to original search_path */
PopOverrideSearchPath();
PopEmptySearchPath(saveNestLevel);
return buffer->data;
}
@ -2038,8 +2035,6 @@ get_query_def_extended(Query *query, StringInfo buf, List *parentnamespace,
deparse_context context;
deparse_namespace dpns;
OverrideSearchPath *overridePath = NULL;
/* Guard against excessively long or deeply-nested queries */
CHECK_FOR_INTERRUPTS();
check_stack_depth();
@ -2058,12 +2053,9 @@ get_query_def_extended(Query *query, StringInfo buf, List *parentnamespace,
/*
* Set search_path to NIL so that all objects outside of pg_catalog will be
* schema-prefixed. pg_catalog will be added automatically when we call
* PushOverrideSearchPath(), since we set addCatalog to true;
* PushEmptySearchPath().
*/
overridePath = GetOverrideSearchPath(CurrentMemoryContext);
overridePath->schemas = NIL;
overridePath->addCatalog = true;
PushOverrideSearchPath(overridePath);
int saveNestLevel = PushEmptySearchPath();
context.buf = buf;
context.namespaces = lcons(&dpns, list_copy(parentnamespace));
@ -2118,7 +2110,7 @@ get_query_def_extended(Query *query, StringInfo buf, List *parentnamespace,
}
/* revert back to original search_path */
PopOverrideSearchPath();
PopEmptySearchPath(saveNestLevel);
}
/* ----------

View File

@ -54,6 +54,7 @@
#include "distributed/citus_nodefuncs.h"
#include "distributed/citus_ruleutils.h"
#include "distributed/multi_router_planner.h"
#include "distributed/namespace_utils.h"
#include "executor/spi.h"
#include "foreign/foreign.h"
#include "funcapi.h"
@ -641,18 +642,14 @@ pg_get_rule_expr(Node *expression)
{
bool showImplicitCasts = true;
deparse_context context;
OverrideSearchPath *overridePath = NULL;
StringInfo buffer = makeStringInfo();
/*
* Set search_path to NIL so that all objects outside of pg_catalog will be
* schema-prefixed. pg_catalog will be added automatically when we call
* PushOverrideSearchPath(), since we set addCatalog to true;
* PushEmptySearchPath().
*/
overridePath = GetOverrideSearchPath(CurrentMemoryContext);
overridePath->schemas = NIL;
overridePath->addCatalog = true;
PushOverrideSearchPath(overridePath);
int saveNestLevel = PushEmptySearchPath();
context.buf = buffer;
context.namespaces = NIL;
@ -669,7 +666,7 @@ pg_get_rule_expr(Node *expression)
get_rule_expr(expression, &context, showImplicitCasts);
/* revert back to original search_path */
PopOverrideSearchPath();
PopEmptySearchPath(saveNestLevel);
return buffer->data;
}
@ -2052,8 +2049,6 @@ get_query_def_extended(Query *query, StringInfo buf, List *parentnamespace,
deparse_context context;
deparse_namespace dpns;
OverrideSearchPath *overridePath = NULL;
/* Guard against excessively long or deeply-nested queries */
CHECK_FOR_INTERRUPTS();
check_stack_depth();
@ -2072,12 +2067,9 @@ get_query_def_extended(Query *query, StringInfo buf, List *parentnamespace,
/*
* Set search_path to NIL so that all objects outside of pg_catalog will be
* schema-prefixed. pg_catalog will be added automatically when we call
* PushOverrideSearchPath(), since we set addCatalog to true;
* PushEmptySearchPath().
*/
overridePath = GetOverrideSearchPath(CurrentMemoryContext);
overridePath->schemas = NIL;
overridePath->addCatalog = true;
PushOverrideSearchPath(overridePath);
int saveNestLevel = PushEmptySearchPath();
context.buf = buf;
context.namespaces = lcons(&dpns, list_copy(parentnamespace));
@ -2132,7 +2124,7 @@ get_query_def_extended(Query *query, StringInfo buf, List *parentnamespace,
}
/* revert back to original search_path */
PopOverrideSearchPath();
PopEmptySearchPath(saveNestLevel);
}
/* ----------

View File

@ -612,7 +612,7 @@ GetPreLoadTableCreationCommands(Oid relationId,
{
List *tableDDLEventList = NIL;
PushOverrideEmptySearchPath(CurrentMemoryContext);
int saveNestLevel = PushEmptySearchPath();
/* fetch table schema and column option definitions */
char *tableSchemaDef = pg_get_tableschemadef_string(relationId,
@ -665,7 +665,7 @@ GetPreLoadTableCreationCommands(Oid relationId,
tableDDLEventList = list_concat(tableDDLEventList, policyCommands);
/* revert back to original search_path */
PopOverrideSearchPath();
PopEmptySearchPath(saveNestLevel);
return tableDDLEventList;
}
@ -754,7 +754,7 @@ GatherIndexAndConstraintDefinitionList(Form_pg_index indexForm, List **indexDDLE
int indexFlags)
{
/* generate fully-qualified names */
PushOverrideEmptySearchPath(CurrentMemoryContext);
int saveNestLevel = PushEmptySearchPath();
Oid indexId = indexForm->indexrelid;
bool indexImpliedByConstraint = IndexImpliedByAConstraint(indexForm);
@ -805,7 +805,7 @@ GatherIndexAndConstraintDefinitionList(Form_pg_index indexForm, List **indexDDLE
}
/* revert back to original search_path */
PopOverrideSearchPath();
PopEmptySearchPath(saveNestLevel);
}

View File

@ -158,7 +158,7 @@ NoneDistTableDropCoordinatorPlacementTable(Oid noneDistTableId)
* local session because changes made to shards are allowed for Citus internal
* backends anyway.
*/
int save_nestlevel = NewGUCNestLevel();
int saveNestLevel = NewGUCNestLevel();
SetLocalEnableLocalReferenceForeignKeys(false);
SetLocalEnableManualChangesToShard(true);
@ -184,7 +184,7 @@ NoneDistTableDropCoordinatorPlacementTable(Oid noneDistTableId)
bool localExecutionSupported = true;
ExecuteUtilityTaskList(list_make1(task), localExecutionSupported);
AtEOXact_GUC(true, save_nestlevel);
AtEOXact_GUC(true, saveNestLevel);
}

View File

@ -11,22 +11,33 @@
#include "postgres.h"
#include "catalog/namespace.h"
#include "distributed/citus_ruleutils.h"
#include "distributed/namespace_utils.h"
#include "utils/guc.h"
#include "utils/regproc.h"
/*
* PushOverrideEmptySearchPath pushes search_path to be NIL and sets addCatalog to
* true so that all objects outside of pg_catalog will be schema-prefixed.
* Afterwards, PopOverrideSearchPath can be used to revert the search_path back.
* We use the equivalent of a function SET option to allow the setting to
* persist for the exact duration of the transaction, guc.c takes care of
* undoing the setting on error.
*
* We set search_path to "pg_catalog" instead of "" to expose useful utilities.
*/
int
PushEmptySearchPath()
{
int saveNestLevel = NewGUCNestLevel();
(void) set_config_option("search_path", "pg_catalog",
PGC_USERSET, PGC_S_SESSION,
GUC_ACTION_SAVE, true, 0, false);
return saveNestLevel;
}
/*
* Restore the GUC variable search_path we set in PushEmptySearchPath
*/
void
PushOverrideEmptySearchPath(MemoryContext memoryContext)
PopEmptySearchPath(int saveNestLevel)
{
OverrideSearchPath *overridePath = GetOverrideSearchPath(memoryContext);
overridePath->schemas = NIL;
overridePath->addCatalog = true;
PushOverrideSearchPath(overridePath);
AtEOXact_GUC(true, saveNestLevel);
}

View File

@ -10,6 +10,7 @@
#ifndef NAMESPACE_UTILS_H
#define NAMESPACE_UTILS_H
extern void PushOverrideEmptySearchPath(MemoryContext memoryContext);
extern int PushEmptySearchPath(void);
extern void PopEmptySearchPath(int saveNestLevel);
#endif /* NAMESPACE_UTILS_H */