1. Background knowledge
1) flex:the Fast Lexical Analyzer
2) bison:a General-purpose parser generator
3) C language
The first two please check the documentation yourself, the Chinese materials are also many, Oschina on the search can see their introduction
2. Process
First of all, some languages are differentiated between functions and processes, some do not, but they are actually not essential differences, or a thing. For PG, the process is returns void function, so create procedure is actually creating a synonym. Let's look at Create FUNCTION (SRC/BACKEND/PASER/GRAM.Y):
Createfunctionstmt:create opt_or_replace function func_name func_args_with_defaultsreturns func_return createfunc_opt_list opt_definition{createfunctionstmt *n = makenode (createfunctionstmt); n->replace = $2;n->funcname = $4;n->parameters = $ 5;n->returntype = $7;n->options = $8;n->withclause = $9;$$ = (node *) n;}| create opt_or_replace function func_name func_args_with_defaults returns TABLE ' (' table_func_column_list ') ' createfunc_opt_list opt_definition{ Createfunctionstmt *n = makenode (createfunctionstmt);n->replace = $2;n-> Funcname = $4;n->parameters = mergetablefuncparameters ($5, $9); n->returnType = tablefunctypename ($9);n->returntype->location = @7;n->options = $11; N->withclause = $12;$$ = (node *) n;}| CREATE opt_or_replace FUNCTION func_name func_args_with_defaults Createfunc_opt_list opt_definition{createfunctionstmt *n = makenode (CreateFunctionStmt); n- >replace = $2;n->funcname = $4;n->parameters = $5;n->returntype = NULL;n->options = $6;n->withClause = $7;$$ = (node *) n;};
3. Description of function syntax
It can be seen that we need to do very little, the first is to define the procedure keyword, the second is to find the void parameter definition
1) define the PROCEDURE keyword, we can see from the program that it has already been defined, and function with the non-reserved keywords, the impression that v9.2 has no such definition. PG keyword Classification can read the document on its own, in addition to the definition of gram.y there are a lot of attention, there are many places to pay attention to;
2) No return value is a special system type: void, which we can specify directly when creating a function;
3) Function syntax we only need the first one, because the latter two have return values;
4. Our process syntax is:
Createprocedurestmt:create opt_or_replace PROCEDURE func_name func_args_with_defaultscreatefunc_opt_list opt_ definition{createfunctionstmt *n = Makenode (createfunctionstmt); n->replace = $2;n->funcname = $4;n-> Parameters = $5;n->returntype = Maketypename ("void"), n->options = $6;n->withclause = $7;$$ = (Node *) n;};
Of course, you also need to add createprocedurestmt in the stmt definition and add createprocedurestmt definition inside the%type<node> .
These learning bison or YACC grammar can know what is going on, the simplest is to find a similar syntax like this divert.
5, make & do install, create a no return value function test:
postgres=# CREATE PROCEDURE Check_proc () as postgres-# $ $postgres $# DECLARE passed boolean;postgres$# beginpostgres$# SELECT true into passed;postgres$# end;postgres$# $$ LANGUAGE plpgsql; CREATE functionpostgres=#
At this point, as a syntax structure, it can already run, and of course many details need to be dealt with, as an example here is enough to explain the problem, no longer continue.
Syntax Creation Example--create PROCEDURE