PostgreSQL: Simple C extension Development for a Novice User (and Performance Advantages)

PostgreSQL Simple C extensionOne of the great features of PostgreSQL is its extendability. My colleague and senior PostgreSQL developer Ibar has blogged about developing an extension with much broader capabilities including callback functionality. But in this blog post, I am trying to address a complete novice user who has never tried but wants to develop a simple function with business logic. Towards the end of the blog post, I want to show how lightweight the function is by doing simple benchmarking which is repeatable and should act as a strong justification for why end-users should do such development.

Generally, PostgreSQL and extension developers work on a PostgreSQL source build. For a novice user, that may not be required, instead, dev/devel packages provided for the Linux distro would be sufficient. Assuming that you have installed PostgreSQL already, the following steps can get you the additional development libraries required.

On Ubuntu/Debian

On RHEL/CentOS

The next step is to add a PostgreSQL binary path to your environment, to ensure that pg_config is there in the path. In my Ubuntu laptop, this is how:

Above mentioned paths may vary according to the environment.

Please make sure that the pg_config is executing without specifying the path:

PostgreSQL installation provides a build infrastructure for extensions, called PGXS, so that simple extension modules can be built simply against an already-installed server. It automates common build rules for simple server extension modules.

Now let’s create a directory for development. I am going to develop a simple extension addme with a function addme to add 2 numbers.

Now we need to create a Makefile which builds the extension. Luckily, we can use all PGXS macros.

MODULE specifies the shared object without file extension and EXTENSION specifies the name of the extension name. DATA defines the installation script. The reason for –0.0.1 specifying in the name is that I should match the version we specify in the control file.

Now we need a control file addme.control with the following content:

And we can prepare our function in C which will add 2 integers:

At this stage, we have only 3 files in the directory.

Now we can make the file:

For installing the extension, we need a SQL file with create function. This SQL file name should be the same as the one we specified in DATA parameter in the Makefile, which is addme–0.0.1.sql

Add the following content into this file:

And install the extension:

Now we can proceed to create the extension and test it:

Just like any function, we can use it in queries against multiple tuples.

Performance Benchmarking

Now it is important to understand the performance characteristics calling a C function in extension. For comparison, we have two  options like:
1. ‘+’ operator provided by SQL like select 1+2;
2. PLpgSQL function as below

For this test/benchmark, I am going to call the function for 1 million times!

SQL + operator

C function call

PL function call

I have performed the tests 6 times for each case and tabulated below.

Test Run

As we can see, the performance of Built in ‘+’ operator and the custom C function in the extension takes the least time with almost the same performance. But the PLpgSQL function call is slow and it shows considerable overhead. Hope this justifies why those functions, which are heavily used, need to be written as a native C extension.

Share this post

Comments (4)

  • okbobcz Reply

    PLpgSQL is designed to be glue for SQL queries – when it is used, then it is pretty fast. Your example is typical worst case and good example when don’t use plpgsql. SQL function is fast due inlining – and when inlining is possible, then it is good case for it.

    Your C example can be enhanced to use buildin function for sum of two integers instead C operator. For example, the overflow is not tested and solved in your C function.

    August 4, 2019 at 7:03 am
    • Jobin Augustine Reply

      I agree with you. PLpgSQL as a gluing language has much more scope. Just execution speed many not be a good comparison aswell. Other factors like speed of development and debugging, protection from memory leaks etc are more important from organizations.
      In this post, I was trying to address completely novice user who never tried anything on C extension side.
      Thank you for great feedback and I shall try to enhance that in upcoming posts.

      August 7, 2019 at 7:50 am
  • Tsingson Qin Reply

    cool.

    sometime we need C extension for special function/logic that use in plpgsql.

    August 8, 2019 at 2:21 am
    • Jobin Augustine Reply

      Yes Agree. Thank you for your feedback

      August 8, 2019 at 4:11 am

Leave a Reply