Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit fb32748

Browse files
committed
Switch SQLValueFunction on "name" to use COERCE_SQL_SYNTAX
This commit changes six SQL keywords to use COERCE_SQL_SYNTAX rather than relying on SQLValueFunction: - CURRENT_ROLE - CURRENT_USER - USER - SESSION_USER - CURRENT_CATALOG - CURRENT_SCHEMA Among the six, "user", "current_role" and "current_catalog" require specific SQL functions to allow ruleutils.c to map them to the SQL keywords these require when using COERCE_SQL_SYNTAX. Having pg_proc.proname match with the keyword ensures that the compatibility remains the same when projecting any of these keywords in a FROM clause to an attribute name when an alias is not specified. This is covered by the tests added in 2e0d80c, making sure that a correct mapping happens with each SQL keyword. The three others (current_schema, session_user and current_user) already have pg_proc entries for this job, so this brings more consistency between the way such keywords are treated in the parser, the executor and ruleutils.c. SQLValueFunction is reduced to half its contents after this change, simplifying its logic a bit as there is no need to enforce a C collation anymore for the entries returning a name as a result. I have made a few performance tests, with a million-ish calls to these keywords without seeing a difference in run-time or in perf profiles (ExecEvalSQLValueFunction() is removed from the profiles). The remaining SQLValueFunctions are now related to timestamps and dates. Bump catalog version. Reviewed-by: Corey Huinker Discussion: https://postgr.es/m/YzaG3MoryCguUOym@paquier.xyz
1 parent ed1d313 commit fb32748

File tree

9 files changed

+56
-93
lines changed

9 files changed

+56
-93
lines changed

src/backend/executor/execExprInterp.c

-27
Original file line numberDiff line numberDiff line change
@@ -2495,15 +2495,10 @@ ExecEvalParamExtern(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
24952495
void
24962496
ExecEvalSQLValueFunction(ExprState *state, ExprEvalStep *op)
24972497
{
2498-
LOCAL_FCINFO(fcinfo, 0);
24992498
SQLValueFunction *svf = op->d.sqlvaluefunction.svf;
25002499

25012500
*op->resnull = false;
25022501

2503-
/*
2504-
* Note: current_schema() can return NULL. current_user() etc currently
2505-
* cannot, but might as well code those cases the same way for safety.
2506-
*/
25072502
switch (svf->op)
25082503
{
25092504
case SVFOP_CURRENT_DATE:
@@ -2525,28 +2520,6 @@ ExecEvalSQLValueFunction(ExprState *state, ExprEvalStep *op)
25252520
case SVFOP_LOCALTIMESTAMP_N:
25262521
*op->resvalue = TimestampGetDatum(GetSQLLocalTimestamp(svf->typmod));
25272522
break;
2528-
case SVFOP_CURRENT_ROLE:
2529-
case SVFOP_CURRENT_USER:
2530-
case SVFOP_USER:
2531-
InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
2532-
*op->resvalue = current_user(fcinfo);
2533-
*op->resnull = fcinfo->isnull;
2534-
break;
2535-
case SVFOP_SESSION_USER:
2536-
InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
2537-
*op->resvalue = session_user(fcinfo);
2538-
*op->resnull = fcinfo->isnull;
2539-
break;
2540-
case SVFOP_CURRENT_CATALOG:
2541-
InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
2542-
*op->resvalue = current_database(fcinfo);
2543-
*op->resnull = fcinfo->isnull;
2544-
break;
2545-
case SVFOP_CURRENT_SCHEMA:
2546-
InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
2547-
*op->resvalue = current_schema(fcinfo);
2548-
*op->resnull = fcinfo->isnull;
2549-
break;
25502523
}
25512524
}
25522525

src/backend/nodes/nodeFuncs.c

+3-8
Original file line numberDiff line numberDiff line change
@@ -917,11 +917,8 @@ exprCollation(const Node *expr)
917917
coll = ((const MinMaxExpr *) expr)->minmaxcollid;
918918
break;
919919
case T_SQLValueFunction:
920-
/* Returns either NAME or a non-collatable type */
921-
if (((const SQLValueFunction *) expr)->type == NAMEOID)
922-
coll = C_COLLATION_OID;
923-
else
924-
coll = InvalidOid;
920+
/* Returns a non-collatable type */
921+
coll = InvalidOid;
925922
break;
926923
case T_XmlExpr:
927924

@@ -1144,9 +1141,7 @@ exprSetCollation(Node *expr, Oid collation)
11441141
((MinMaxExpr *) expr)->minmaxcollid = collation;
11451142
break;
11461143
case T_SQLValueFunction:
1147-
Assert((((SQLValueFunction *) expr)->type == NAMEOID) ?
1148-
(collation == C_COLLATION_OID) :
1149-
(collation == InvalidOid));
1144+
Assert(collation == InvalidOid);
11501145
break;
11511146
case T_XmlExpr:
11521147
Assert((((XmlExpr *) expr)->op == IS_XMLSERIALIZE) ?

src/backend/parser/gram.y

+24-6
Original file line numberDiff line numberDiff line change
@@ -15231,15 +15231,24 @@ func_expr_common_subexpr:
1523115231
}
1523215232
| CURRENT_ROLE
1523315233
{
15234-
$$ = makeSQLValueFunction(SVFOP_CURRENT_ROLE, -1, @1);
15234+
$$ = (Node *) makeFuncCall(SystemFuncName("current_role"),
15235+
NIL,
15236+
COERCE_SQL_SYNTAX,
15237+
@1);
1523515238
}
1523615239
| CURRENT_USER
1523715240
{
15238-
$$ = makeSQLValueFunction(SVFOP_CURRENT_USER, -1, @1);
15241+
$$ = (Node *) makeFuncCall(SystemFuncName("current_user"),
15242+
NIL,
15243+
COERCE_SQL_SYNTAX,
15244+
@1);
1523915245
}
1524015246
| SESSION_USER
1524115247
{
15242-
$$ = makeSQLValueFunction(SVFOP_SESSION_USER, -1, @1);
15248+
$$ = (Node *) makeFuncCall(SystemFuncName("session_user"),
15249+
NIL,
15250+
COERCE_SQL_SYNTAX,
15251+
@1);
1524315252
}
1524415253
| SYSTEM_USER
1524515254
{
@@ -15250,15 +15259,24 @@ func_expr_common_subexpr:
1525015259
}
1525115260
| USER
1525215261
{
15253-
$$ = makeSQLValueFunction(SVFOP_USER, -1, @1);
15262+
$$ = (Node *) makeFuncCall(SystemFuncName("user"),
15263+
NIL,
15264+
COERCE_SQL_SYNTAX,
15265+
@1);
1525415266
}
1525515267
| CURRENT_CATALOG
1525615268
{
15257-
$$ = makeSQLValueFunction(SVFOP_CURRENT_CATALOG, -1, @1);
15269+
$$ = (Node *) makeFuncCall(SystemFuncName("current_catalog"),
15270+
NIL,
15271+
COERCE_SQL_SYNTAX,
15272+
@1);
1525815273
}
1525915274
| CURRENT_SCHEMA
1526015275
{
15261-
$$ = makeSQLValueFunction(SVFOP_CURRENT_SCHEMA, -1, @1);
15276+
$$ = (Node *) makeFuncCall(SystemFuncName("current_schema"),
15277+
NIL,
15278+
COERCE_SQL_SYNTAX,
15279+
@1);
1526215280
}
1526315281
| CAST '(' a_expr AS Typename ')'
1526415282
{ $$ = makeTypeCast($3, $5, @1); }

src/backend/parser/parse_expr.c

-8
Original file line numberDiff line numberDiff line change
@@ -2231,14 +2231,6 @@ transformSQLValueFunction(ParseState *pstate, SQLValueFunction *svf)
22312231
svf->type = TIMESTAMPOID;
22322232
svf->typmod = anytimestamp_typmod_check(false, svf->typmod);
22332233
break;
2234-
case SVFOP_CURRENT_ROLE:
2235-
case SVFOP_CURRENT_USER:
2236-
case SVFOP_USER:
2237-
case SVFOP_SESSION_USER:
2238-
case SVFOP_CURRENT_CATALOG:
2239-
case SVFOP_CURRENT_SCHEMA:
2240-
svf->type = NAMEOID;
2241-
break;
22422234
}
22432235

22442236
return (Node *) svf;

src/backend/parser/parse_target.c

-18
Original file line numberDiff line numberDiff line change
@@ -1895,24 +1895,6 @@ FigureColnameInternal(Node *node, char **name)
18951895
case SVFOP_LOCALTIMESTAMP_N:
18961896
*name = "localtimestamp";
18971897
return 2;
1898-
case SVFOP_CURRENT_ROLE:
1899-
*name = "current_role";
1900-
return 2;
1901-
case SVFOP_CURRENT_USER:
1902-
*name = "current_user";
1903-
return 2;
1904-
case SVFOP_USER:
1905-
*name = "user";
1906-
return 2;
1907-
case SVFOP_SESSION_USER:
1908-
*name = "session_user";
1909-
return 2;
1910-
case SVFOP_CURRENT_CATALOG:
1911-
*name = "current_catalog";
1912-
return 2;
1913-
case SVFOP_CURRENT_SCHEMA:
1914-
*name = "current_schema";
1915-
return 2;
19161898
}
19171899
break;
19181900
case T_XmlExpr:

src/backend/utils/adt/ruleutils.c

+18-18
Original file line numberDiff line numberDiff line change
@@ -9169,24 +9169,6 @@ get_rule_expr(Node *node, deparse_context *context,
91699169
appendStringInfo(buf, "LOCALTIMESTAMP(%d)",
91709170
svf->typmod);
91719171
break;
9172-
case SVFOP_CURRENT_ROLE:
9173-
appendStringInfoString(buf, "CURRENT_ROLE");
9174-
break;
9175-
case SVFOP_CURRENT_USER:
9176-
appendStringInfoString(buf, "CURRENT_USER");
9177-
break;
9178-
case SVFOP_USER:
9179-
appendStringInfoString(buf, "USER");
9180-
break;
9181-
case SVFOP_SESSION_USER:
9182-
appendStringInfoString(buf, "SESSION_USER");
9183-
break;
9184-
case SVFOP_CURRENT_CATALOG:
9185-
appendStringInfoString(buf, "CURRENT_CATALOG");
9186-
break;
9187-
case SVFOP_CURRENT_SCHEMA:
9188-
appendStringInfoString(buf, "CURRENT_SCHEMA");
9189-
break;
91909172
}
91919173
}
91929174
break;
@@ -10288,6 +10270,24 @@ get_func_sql_syntax(FuncExpr *expr, deparse_context *context)
1028810270
appendStringInfoChar(buf, ')');
1028910271
return true;
1029010272

10273+
case F_CURRENT_CATALOG:
10274+
appendStringInfoString(buf, "CURRENT_CATALOG");
10275+
return true;
10276+
case F_CURRENT_ROLE:
10277+
appendStringInfoString(buf, "CURRENT_ROLE");
10278+
return true;
10279+
case F_CURRENT_SCHEMA:
10280+
appendStringInfoString(buf, "CURRENT_SCHEMA");
10281+
return true;
10282+
case F_CURRENT_USER:
10283+
appendStringInfoString(buf, "CURRENT_USER");
10284+
return true;
10285+
case F_USER:
10286+
appendStringInfoString(buf, "USER");
10287+
return true;
10288+
case F_SESSION_USER:
10289+
appendStringInfoString(buf, "SESSION_USER");
10290+
return true;
1029110291
case F_SYSTEM_USER:
1029210292
appendStringInfoString(buf, "SYSTEM_USER");
1029310293
return true;

src/include/catalog/catversion.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
*/
5858

5959
/* yyyymmddN */
60-
#define CATALOG_VERSION_NO 202211181
60+
#define CATALOG_VERSION_NO 202211201
6161

6262
#endif

src/include/catalog/pg_proc.dat

+9
Original file line numberDiff line numberDiff line change
@@ -1505,6 +1505,15 @@
15051505
{ oid => '745', descr => 'current user name',
15061506
proname => 'current_user', provolatile => 's', prorettype => 'name',
15071507
proargtypes => '', prosrc => 'current_user' },
1508+
{ oid => '9695', descr => 'current role name',
1509+
proname => 'current_role', provolatile => 's', prorettype => 'name',
1510+
proargtypes => '', prosrc => 'current_user' },
1511+
{ oid => '9696', descr => 'user name',
1512+
proname => 'user', provolatile => 's', prorettype => 'name',
1513+
proargtypes => '', prosrc => 'current_user' },
1514+
{ oid => '9697', descr => 'name of the current database',
1515+
proname => 'current_catalog', provolatile => 's', prorettype => 'name',
1516+
proargtypes => '', prosrc => 'current_database' },
15081517
{ oid => '746', descr => 'session user name',
15091518
proname => 'session_user', provolatile => 's', prorettype => 'name',
15101519
proargtypes => '', prosrc => 'session_user' },

src/include/nodes/primnodes.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -1313,13 +1313,7 @@ typedef enum SQLValueFunctionOp
13131313
SVFOP_LOCALTIME,
13141314
SVFOP_LOCALTIME_N,
13151315
SVFOP_LOCALTIMESTAMP,
1316-
SVFOP_LOCALTIMESTAMP_N,
1317-
SVFOP_CURRENT_ROLE,
1318-
SVFOP_CURRENT_USER,
1319-
SVFOP_USER,
1320-
SVFOP_SESSION_USER,
1321-
SVFOP_CURRENT_CATALOG,
1322-
SVFOP_CURRENT_SCHEMA
1316+
SVFOP_LOCALTIMESTAMP_N
13231317
} SQLValueFunctionOp;
13241318

13251319
typedef struct SQLValueFunction

0 commit comments

Comments
 (0)