Get the App
SLTechnology News&Howtos  ›  Database  › 

How to implement functions similar to CURRENT_DATE functions in PostgreSQL

Shulou Source: shulou.com Published: 2022-05-31 15:21:25 12月02日 Update

Editor to share with you how PostgreSQL to achieve similar CURRENT_DATE function functions, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!

I. realization

The implementation part involves transforming SQLValueFunction (setting the new column zz_sysdate to SQLValueFunction in gram.y), parsing SQLValueFunction and some internal processing of PG.

Convert SQLValueFunction

The SQLValueFunction conversion is in the file src/backend/parser/parse_expr.c, and the corresponding function is transformSQLValueFunction. The new / modified codes are as follows:

Static Node * transformSQLValueFunction (ParseState * pstate, SQLValueFunction * svf) {switch (svf- > op) {. Case SVFOP_ZZ_SYSDATE: svf- > type = DATEOID; break

Parsing SQLValueFunction

Parsing SQLValueFunction in the file src/backend/executor/execExprInterp.c, the corresponding function is ExecEvalSQLValueFunction, and the related code is as follows:

VoidExecEvalSQLValueFunction (ExprState * state, ExprEvalStep * op) {switch (svf- > op) {... Case SVFOP_ZZ_SYSDATE: * op- > resvalue = DateADTGetDatum (GetSQLCurrentDate ()); break

Internal processing

The internal processing of PG is in the file src/backend/utils/adt/ruleutils.c, and the corresponding function is get_rule_expr. The related code is as follows:

Static voidget_rule_expr (Node * node, deparse_context * context, bool showimplicit) {. Case T_SQLValueFunction: {SQLValueFunction * svf = (SQLValueFunction *) node; / * * Note: this code knows that typmod for time, timestamp, and * timestamptz just prints as integer. * / switch (svf- > op) {... Case SVFOP_ZZ_SYSDATE: appendStringInfoString (buf, "ZZ_SYSDATE"); break; II, source code interpretation

Parse_expr.c: handle expressions in parser, processing expressions in parsers

ExecExprInterp.c: analytic evaluation of expressions (steps)

Ruleutils.c: Functions to convert stored expressions/querytrees back to source text, transform and store expression / query tree to source text.

These are all the contents of the article "how to achieve functions similar to CURRENT_DATE functions in PostgreSQL". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Functions processing code files articles expressions functions content not much most text more steps source code knowledge industry information information channels parts channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Xiaomi vpn MySQL Shulou Information