pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: TalGloz <glozmantal@gmail.com>
To: pgsql-general@postgresql.org
Subject: Re: PostgreSQL C Language Extension with C++ Code
Date: Sun, 12 Aug 2018 13:55:30 -0700 (MST)
Message-ID: <1534107330015-0.post@n3.nabble.com> (raw)
In-Reply-To: <1607.1534101134@sss.pgh.pa.us>
References: <1534093538827-0.post@n3.nabble.com>
	<CA+FnnTzH1gZ8o4a_WTD79kk7CNUzFu0NHUmbs03Gax0Su4Lgzw@mail.gmail.com>
	<1534097342417-0.post@n3.nabble.com>
	<31921.1534098408@sss.pgh.pa.us>
	<1534099202325-0.post@n3.nabble.com>
	<1607.1534101134@sss.pgh.pa.us>

OK It worked. This is how I did it, hopefully it is right

extern "C" {
#include <postgres.h>
#include <utils/rel.h>
#include <fmgr.h>
#include <utils/array.h>
#include <utils/builtins.h>
#include <catalog/pg_type.h>
#include <stdlib.h>
#include <stdint.h>

PG_MODULE_MAGIC;
}

#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <seal/seal.h> // external compiled c++ library linked on running
'make'


extern "C" {
Datum sum_of_numbers(PG_FUNCTION_ARGS){
        std::vector<int> numbers {23, 445, 64};
                int sum = 0;
                for (auto &item : numbers){
                        sum += item;
                }
                return sum;
};
PG_FUNCTION_INFO_V1(sum_of_numbers);
}

I've managed to create and execute the function in my PostgreSQL database.

So basically I can execute any CPP code as long as I declare my functions
like this:

extern "C" {
    Datum function_name(PG_FUNCTION_ARGS){
         // CPP code here
    };

    PG_FUNCTION_INFO_V1(function_name);
}

In addition tho thath, all the C headers should be * inside a extern "C" {
}* block and all the CPP headers *outside the extern "C" { }* block, did I
get it right? 

Thanks,
Tal



--
Sent from: http://www.postgresql-archive.org/PostgreSQL-general-f1843780.html




view thread (10+ messages)

Message-ID: <1534107330015-0.post@n3.nabble.com>
Permalink:  ../1534107330015-0.post@n3.nabble.com/
Also on:    postgresql.org/message-id/1534107330015-0.post@n3.nabble.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: glozmantal@gmail.com
  Subject: Re: PostgreSQL C Language Extension with C++ Code
  In-Reply-To: <1534107330015-0.post@n3.nabble.com>
  References: <1534093538827-0.post@n3.nabble.com> <CA+FnnTzH1gZ8o4a_WTD79kk7CNUzFu0NHUmbs03Gax0Su4Lgzw@mail.gmail.com> <1534097342417-0.post@n3.nabble.com> <31921.1534098408@sss.pgh.pa.us> <1534099202325-0.post@n3.nabble.com> <1607.1534101134@sss.pgh.pa.us> <1534107330015-0.post@n3.nabble.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox