Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfb32748

Browse files
committed
Switch SQLValueFunction on "name" to use COERCE_SQL_SYNTAX
This commit changes six SQL keywords to use COERCE_SQL_SYNTAX ratherthan relying on SQLValueFunction:- CURRENT_ROLE- CURRENT_USER- USER- SESSION_USER- CURRENT_CATALOG- CURRENT_SCHEMAAmong the six, "user", "current_role" and "current_catalog" requirespecific SQL functions to allow ruleutils.c to map them to the SQLkeywords these require when using COERCE_SQL_SYNTAX. Havingpg_proc.proname match with the keyword ensures that the compatibilityremains the same when projecting any of these keywords in a FROM clauseto an attribute name when an alias is not specified. This is covered bythe tests added in2e0d80c, making sure that a correct mapping happenswith each SQL keyword. The three others (current_schema, session_userand current_user) already have pg_proc entries for this job, so thisbrings more consistency between the way such keywords are treated in theparser, 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 collationanymore for the entries returning a name as a result. I have made a fewperformance tests, with a million-ish calls to these keywords withoutseeing a difference in run-time or in perf profiles(ExecEvalSQLValueFunction() is removed from the profiles). Theremaining SQLValueFunctions are now related to timestamps and dates.Bump catalog version.Reviewed-by: Corey HuinkerDiscussion:https://postgr.es/m/YzaG3MoryCguUOym@paquier.xyz
1 parented1d313 commitfb32748

File tree

9 files changed

+56
-93
lines changed

9 files changed

+56
-93
lines changed

‎src/backend/executor/execExprInterp.c

Lines changed: 0 additions & 27 deletions
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
caseSVFOP_CURRENT_DATE:
@@ -2525,28 +2520,6 @@ ExecEvalSQLValueFunction(ExprState *state, ExprEvalStep *op)
25252520
caseSVFOP_LOCALTIMESTAMP_N:
25262521
*op->resvalue=TimestampGetDatum(GetSQLLocalTimestamp(svf->typmod));
25272522
break;
2528-
caseSVFOP_CURRENT_ROLE:
2529-
caseSVFOP_CURRENT_USER:
2530-
caseSVFOP_USER:
2531-
InitFunctionCallInfoData(*fcinfo,NULL,0,InvalidOid,NULL,NULL);
2532-
*op->resvalue=current_user(fcinfo);
2533-
*op->resnull=fcinfo->isnull;
2534-
break;
2535-
caseSVFOP_SESSION_USER:
2536-
InitFunctionCallInfoData(*fcinfo,NULL,0,InvalidOid,NULL,NULL);
2537-
*op->resvalue=session_user(fcinfo);
2538-
*op->resnull=fcinfo->isnull;
2539-
break;
2540-
caseSVFOP_CURRENT_CATALOG:
2541-
InitFunctionCallInfoData(*fcinfo,NULL,0,InvalidOid,NULL,NULL);
2542-
*op->resvalue=current_database(fcinfo);
2543-
*op->resnull=fcinfo->isnull;
2544-
break;
2545-
caseSVFOP_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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -917,11 +917,8 @@ exprCollation(const Node *expr)
917917
coll= ((constMinMaxExpr*)expr)->minmaxcollid;
918918
break;
919919
caseT_SQLValueFunction:
920-
/* Returns either NAME or a non-collatable type */
921-
if (((constSQLValueFunction*)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
caseT_XmlExpr:
927924

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

‎src/backend/parser/gram.y

Lines changed: 24 additions & 6 deletions
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_exprASTypename')'
1526415282
{$$ = makeTypeCast($3,$5,@1); }

‎src/backend/parser/parse_expr.c

Lines changed: 0 additions & 8 deletions
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-
caseSVFOP_CURRENT_ROLE:
2235-
caseSVFOP_CURRENT_USER:
2236-
caseSVFOP_USER:
2237-
caseSVFOP_SESSION_USER:
2238-
caseSVFOP_CURRENT_CATALOG:
2239-
caseSVFOP_CURRENT_SCHEMA:
2240-
svf->type=NAMEOID;
2241-
break;
22422234
}
22432235

22442236
return (Node*)svf;

‎src/backend/parser/parse_target.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,24 +1895,6 @@ FigureColnameInternal(Node *node, char **name)
18951895
caseSVFOP_LOCALTIMESTAMP_N:
18961896
*name="localtimestamp";
18971897
return2;
1898-
caseSVFOP_CURRENT_ROLE:
1899-
*name="current_role";
1900-
return2;
1901-
caseSVFOP_CURRENT_USER:
1902-
*name="current_user";
1903-
return2;
1904-
caseSVFOP_USER:
1905-
*name="user";
1906-
return2;
1907-
caseSVFOP_SESSION_USER:
1908-
*name="session_user";
1909-
return2;
1910-
caseSVFOP_CURRENT_CATALOG:
1911-
*name="current_catalog";
1912-
return2;
1913-
caseSVFOP_CURRENT_SCHEMA:
1914-
*name="current_schema";
1915-
return2;
19161898
}
19171899
break;
19181900
caseT_XmlExpr:

‎src/backend/utils/adt/ruleutils.c

Lines changed: 18 additions & 18 deletions
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-
caseSVFOP_CURRENT_ROLE:
9173-
appendStringInfoString(buf,"CURRENT_ROLE");
9174-
break;
9175-
caseSVFOP_CURRENT_USER:
9176-
appendStringInfoString(buf,"CURRENT_USER");
9177-
break;
9178-
caseSVFOP_USER:
9179-
appendStringInfoString(buf,"USER");
9180-
break;
9181-
caseSVFOP_SESSION_USER:
9182-
appendStringInfoString(buf,"SESSION_USER");
9183-
break;
9184-
caseSVFOP_CURRENT_CATALOG:
9185-
appendStringInfoString(buf,"CURRENT_CATALOG");
9186-
break;
9187-
caseSVFOP_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+
caseF_CURRENT_CATALOG:
10274+
appendStringInfoString(buf,"CURRENT_CATALOG");
10275+
return true;
10276+
caseF_CURRENT_ROLE:
10277+
appendStringInfoString(buf,"CURRENT_ROLE");
10278+
return true;
10279+
caseF_CURRENT_SCHEMA:
10280+
appendStringInfoString(buf,"CURRENT_SCHEMA");
10281+
return true;
10282+
caseF_CURRENT_USER:
10283+
appendStringInfoString(buf,"CURRENT_USER");
10284+
return true;
10285+
caseF_USER:
10286+
appendStringInfoString(buf,"USER");
10287+
return true;
10288+
caseF_SESSION_USER:
10289+
appendStringInfoString(buf,"SESSION_USER");
10290+
return true;
1029110291
caseF_SYSTEM_USER:
1029210292
appendStringInfoString(buf,"SYSTEM_USER");
1029310293
return true;

‎src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
*/
5858

5959
/*yyyymmddN */
60-
#defineCATALOG_VERSION_NO202211181
60+
#defineCATALOG_VERSION_NO202211201
6161

6262
#endif

‎src/include/catalog/pg_proc.dat

Lines changed: 9 additions & 0 deletions
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

Lines changed: 1 addition & 7 deletions
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
typedefstructSQLValueFunction

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp