PostgreSQL如何实现输出
短信预约 -IT技能 免费直播动态提醒
这篇文章主要介绍了PostgreSQL如何实现输出,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
一、输出
参照current_date函数,首先通过日志跟踪其parse tree,重点关注targetList
2019-04-19 17:06:15.591 CST,"xdb","testdb",1693,"[local]",5cb98b16.69d,3,"SELECT",2019-04-19 16:47:18 CST,3/4,0,LOG,00000,"parse tree:"," {QUERY
:commandType 1
...
:targetList (
{TARGETENTRY
:expr
{SQLVALUEFUNCTION
:op 0
:type 1082
:typmod -1
:location 7
}
:resno 1
:resname current_date
:ressortgroupref 0
:resorigtbl 0
:resorigcol 0
:resjunk false
}
)
...
:stmt_len 27
}
分析源代码,找到与输出相关的文件是class="lazy" data-src/backend/parser/parse_target.c,相关的函数为transformTargetEntry,跟踪此函数,打印调用栈:
(gdb) bt
#0 transformTargetEntry (pstate=0x16afae8, node=0x16af770, expr=0x0, exprKind=EXPR_KIND_SELECT_TARGET, colname=0x0,
resjunk=false) at parse_target.c:93
#1 0x000000000060b1f9 in transformTargetList (pstate=0x16afae8, targetlist=0x16af828, exprKind=EXPR_KIND_SELECT_TARGET)
at parse_target.c:191
#2 0x00000000005b1e3a in transformSelectStmt (pstate=0x16afae8, stmt=0x16af938) at analyze.c:1243
#3 0x00000000005b03db in transformStmt (pstate=0x16afae8, parseTree=0x16af938) at analyze.c:301
#4 0x00000000005b02b6 in transformOptionalSelectInto (pstate=0x16afae8, parseTree=0x16af938) at analyze.c:246
#5 0x00000000005b0174 in transformTopLevelStmt (pstate=0x16afae8, parseTree=0x16afa50) at analyze.c:196
#6 0x00000000005affcc in parse_analyze (parseTree=0x16afa50, sourceText=0x16aed78 "select current_date from t1;",
paramTypes=0x0, numParams=0, queryEnv=0x0) at analyze.c:116
#7 0x00000000008bb78c in pg_analyze_and_rewrite (parsetree=0x16afa50,
query_string=0x16aed78 "select current_date from t1;", paramTypes=0x0, numParams=0, queryEnv=0x0) at postgres.c:689
#8 0x00000000008bbddd in exec_simple_query (query_string=0x16aed78 "select current_date from t1;") at postgres.c:1070
#9 0x00000000008c01f3 in PostgresMain (argc=1, argv=0x16dcd28, dbname=0x16dcb90 "testdb", username=0x16aba98 "xdb")
at postgres.c:4182
#10 0x000000000081e0ce in BackendRun (port=0x16d0b50) at postmaster.c:4361
#11 0x000000000081d841 in BackendStartup (port=0x16d0b50) at postmaster.c:4033
#12 0x0000000000819c3b in ServerLoop () at postmaster.c:1706
#13 0x00000000008194f1 in PostmasterMain (argc=1, argv=0x16a9a50) at postmaster.c:1379
#14 0x0000000000742993 in main (argc=1, argv=0x16a9a50) at main.c:228
其中FigureColnameInternal函数为current_date设置输出列名,添加以下代码,则实现输出sysdate:
//Hacker : 添加系统列
case SVFOP_ZZ_SYSDATE:
*name = "sysdate";//zz_sysdate -> sysdate
return 2;
相关源码解读
TargetEntry *
transformTargetEntry(ParseState *pstate,
Node *node,
Node *expr,
ParseExprKind exprKind,
char *colname,
bool resjunk)
{
//expr为NULL,则转换之
if (expr == NULL)
{
if (exprKind == EXPR_KIND_UPDATE_SOURCE && IsA(node, SetToDefault))
expr = node;
else
expr = transformExpr(pstate, node, exprKind);
}
if (colname == NULL && !resjunk)
{
colname = FigureColname(node);
}
//返回TargetEntry
return makeTargetEntry((Expr *) expr,
(AttrNumber) pstate->p_next_resno++,
colname,
resjunk);
}
char *
FigureColname(Node *node)
{
char *name = NULL;
(void) FigureColnameInternal(node, &name);
if (name != NULL)
return name;
return "?column?";
}
static int
FigureColnameInternal(Node *node, char **name)
{
int strength = 0;
if (node == NULL)
return strength;
switch (nodeTag(node))
{
...
case T_SQLValueFunction:
switch (((SQLValueFunction *) node)->op)
{
case SVFOP_CURRENT_DATE:
*name = "current_date";
return 2;
...
//Hacker : 添加系统列
case SVFOP_ZZ_SYSDATE:
*name = "sysdate";//zz_sysdate -> sysdate
return 2;
}
break;
...
}
}
感谢你能够认真阅读完这篇文章,希望小编分享的“PostgreSQL如何实现输出”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341