Creating an extension test in postgresql

I want to create an extension test in postgres (using PostGis), so I want to take the following steps:

1.- Edit the file btree_interval.cfrom btree_gist like this:

gbt_intvkey_cmp(const void *a, const void *b)
{
    intvKEY    *ia = (intvKEY *) (((const Nsrt *) a)->t);
    intvKEY    *ib = (intvKEY *) (((const Nsrt *) b)->t);
    int         res;
  ......
  ......

  printf("Test for PostGis\n");

    return res;
}

Add only printfbecause I just want to check a little

2.- Run the following command:

gcc -shared -o btree_gist_test.so -fPIC btree_gist.c

My doubts:

1.- I do not know where I can find the file btree_gist.cafter installing postgresql, and then execute the command above.

If you ask me: "Why don't you just download the source code?" Well, because when I did this, I received an error message:

 #include "postgres.h"
                      ^
compilation terminated

So, I thought it was better to do this in the same folder where postgresql is already installed.

2.- btree_gist_test.so, , /usr/lib/postgresql/lib/, , .

+3
2

, , postgresql-server ubuntu

extension.c

/* Postgres headers */
#include <postgres.h>
#include <utils/rel.h>

#include <stdio.h>
#include <string.h>

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

static char *
extract_string(text *word)
{
    char *head;
    char *tail;

    if (word == NULL)
        return NULL;

    head = VARDATA(word);
    tail = head + VARSIZE(word) - VARHDRSZ;
    tail[0] = '\0';

    return head;
}

PG_FUNCTION_INFO_V1(compare_strings);
Datum
compare_strings(PG_FUNCTION_ARGS)
{
    char *lhs;
    char *rhs;

    lhs = extract_string(PG_GETARG_TEXT_P(0));
    rhs = extract_string(PG_GETARG_TEXT_P(1));

    PG_RETURN_BOOL(strcmp(lhs, rhs) == 0);
}

Makefile
Makefile, , .

CC     = gcc
OBJECT = extension.o
NAME   = my-extension.so
CFLAGS = -Wall -Werror -g3 -O0 -I$(shell pg_config --includedir-server)

all: $(OBJECT)
    $(CC) -shared -o $(NAME) $(OBJECT)

%.o: %.c
    $(CC) -c -fPIC $(CFLAGS) $<

install: all
    @install -Dv -m755 $(NAME) $(shell pg_config --pkglibdir)/$(NAME)
    @psql -U postgres -f create-function.sql

clean:
    @rm -fv *.o *.so

-function.sql
script

CREATE OR REPLACE FUNCTION 
    compare_strings(VARCHAR,VARCHAR) RETURNS integer AS 'my-extension' 
LANGUAGE C STRICT;

, , , .

+3

:

PostgreSQL, PostgreSQL :

  • cd
  • make
  • make install

... , PostgreSQL PostgreSQL, PGXS.

, :

  • , , pg_config, PATH
  • make USE_PGXS=1
  • , sudo make USE_PGXS=1 install

:

0

Source: https://habr.com/ru/post/1610472/


All Articles