Postgres the process of creating a table and some source code analysis

Source: Internet
Author: User

Background: Modifying the PG kernel, when creating a table, the table name cannot have the same name as the current user name.

First we know that definerelation this function is the function that eventually creates the table structure, the main parameter is CREATESTMT this structure, the structure is as follows

typedef struct CREATESTMT{NODETAGTYPE; Rangevar   *relation;/* relation to create */list   *tableelts;/* column definitions (List of columndef) */list   *inhrelations;/* relations to inherit from (List of * inhrelation) */typename   *oftypename;/* of TypeName */list   *c onstraints;/* constraints (List of Constraint nodes) */list   *options;/* options from with clause */oncommitaction Onco mmit;/* What does we do at COMMIT? */char   *tablespacename;/* table space to use, or NULL */boolif_not_exists;/* just does nothing if it already exists? */ } createstmt;

The structure of the relation contained in the Catalogname,schemaname,relname at this time relname can be smoothly obtained.

Tableelts This list defines all the column names in the table structure, and if you want to add a non-hidden column, you can append it in.

is to create a simple table, the calling procedure for the PG intrinsic function:

In the PG backend typed SQL, the entry function is Exec_simple_query, this string of SQL parsing, rewrite the production execution plan.

Comes with a function removed by the PG kernel, which parses the query structure out of the SQL function

Char *deparse_query_def (query *query) {stringinfodata buf;initstringinfo (&buf); get_query_def (query, &buf, NIL, Null,prettyflag_indent, Wrap_column_default, 0); return buf.data;}

You can compile this function into the PG kernel if you want to get a good look at the query and what tasks subquery rewrite later.

In the process of processutility hook function can be hooked up, according to the type of Nodetag (Parsetree) can be processed separately, such as CSTORE_FDW in t_dropstmt this operation, the physical file is deleted

The use of this hook can also control certain operations to add the content you want.

We can make kernel modifications in the Processutilityslow function, add to the current user name, and compare it from the relname in the CREATESTMT structure, and then control whether or not to create a table or make an error message.

Get the current user name as follows

Datumcurrent_user (Pg_function_args) {pg_return_datum (DirectFunctionCall1 (Namein, Cstringgetdatum ( Getusernamefromid (GetUserId ()))));}

This function is the intrinsic function of the PG, using the effect:

postgres=# Select Current_User; Current_User--------------Postgres (1 row)

In this way, you can complete the context of the content.

PS: There are usually several collocation functions using->definerelation->commandcounterincrement->transformreloptions

->heap_reloptions->newrelationcreatetoasttable[altertablecreatetoasttable]

Note: Without consent, no reprint!

  

Postgres the process of creating a table and some source code analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.