How to create a custom built-in function for PostgreSQL

Source: Internet
Author: User

  1. function version (), without parameters, as an example is concise and straightforward. If you need parameters, you can look at other function definitions under ADT, which is a very simple thing, which is the knowledge storehouse that open source brings to us;

    1. Postgres=# select version ();                                                        version                                                        -------------------------------------------------------------------------------- --------------------------------- PostgreSQL 9.5devel on x86_64-unknown-linux-gnu,  compiled by gcc  (GCC)  4.9.2 20150212  (red hat 4.9.2-6),  64-bit (1  row) postgres=#
    2. Defined inside the SRC/BACKEND/UTILS/ADT/VERSION.C:

        1. Datumpgsql_version (Pg_function_args) {pg_return_text_p (Cstring_to_text (PG_VERSION_STR));}
    3. Other documents within the definition,/src/include/utils/builtins:

        1. /* VERSION.C */extern Datum pgsql_version (Pg_function_args);
    4. The most critical definition, let the user can see,/src/include/catalog/proc.h, this file is compiled before the script will be processed to generate the BKI (self-lookup document) script, the initialization of the database in Pg_proc inside the definition of functions, type internal, This type is different from other types (such as C, defined in the external library), the meaning of these definitions, space is limited please learn it yourself:

        1. DATA (Insert OID = PGNSP pguid 1 0 0 0 F f f f t F S 0 0 "" "_null_ _null_ _null_ _null_ Pgsql_versio n _null_ _null_ _null_));
  2. Define our own functions:

    1. Datumquanzl_version (Pg_function_args) {pg_return_text_p (Cstring_to_text ("Quan ' s Database");}
    2. Builtins.h add a definition under Pgsql_version:

        1. extern Datum quanzl_version (Pg_function_args);
    3. Proc.h:

        1. DATA (Insert OID = quan_version pgnsp pguid 1 0 0 0 F f f f t F S 0 0 "" _null_ _null_ _null_ _null_ quanzl_ Version _null_ _null_ _null_));
        2. The OID = 90 here is very important, not random selection, must be unused, how to find, PG provides us with a script, under the src/include/catalog/unused_oids, easy to know which is available. (There's also a duplicate_oids for troubleshooting)

  3. Compile, run, it's so simple

  4. Time relationship, the above process is unverified, there may be errors, but the approximate steps are so.








How to create a custom built-in function for PostgreSQL

Related Article

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.