Skip to content

Building PostgreSQL C Extension with Ziglang : libmathtest.so.0: cannot open shared object file: No such file or directory #3617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ranjanprj opened this issue Nov 6, 2019 · 4 comments

Comments

@ranjanprj
Copy link

ranjanprj commented Nov 6, 2019

This is a great project and I'm very excited about this. My interest lies in building high perf PostgreSQL extensions with Ziglang. Here is my workflow

  1. Compile mathtest.zig as shared dynamic library
  2. Embed the add function in C file for postgresql
  3. Compile using Makefile for Postgresql
  4. Try to execute the file

Steps

  1. mathtest.zig
export fn add(a: i32, b: i32) i32 {
    return a + b;
}
  1. zig build-lib mathtest.zig

  2. funcs.c

#include "postgres.h"
#include <string.h>
#include "fmgr.h"
#include "utils/geo_decls.h"
#include "mathtest.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

/* by value */

PG_FUNCTION_INFO_V1(add_one);

Datum
add_one(PG_FUNCTION_ARGS)
{
    int32   arg = PG_GETARG_INT32(0);

    PG_RETURN_INT32(add(arg,arg));
}
  1. Makefile
MODULES = funcs

PG_CONFIG = pg_config
PGXS = $(shell $(PG_CONFIG) --pgxs)
INCLUDEDIR = $(shell $(PG_CONFIG) --includedir-server)
include $(PGXS)


funcs.so: funcs.o
        cc  -shared -o funcs.so funcs.o /home/ranjanprj/zig/libmathtest.so.0.0.0

funcs.o: funcs.c
        cc -o funcs.o -c funcs.c $(CFLAGS) -I$(INCLUDEDIR)
  1. sudo make && sudo make install
  2. In postgresql create the extension
drop function if exists add_zig(integer);

CREATE or replace FUNCTION add_zig(integer) RETURNS integer
     AS '/home/ranjanprj/zig/funcs.so', 'add_one'
     LANGUAGE C STRICT;
  1. ERROR
drop function if exists add_zig(integer);

CREATE or replace FUNCTION add_zig(integer) RETURNS integer
     AS '/home/ranjanprj/zig/funcs.so', 'add_one'
     LANGUAGE C STRICT;
  1. Apparently it copies libmathtest.so.0 in /usr/lib/postgresql/11/lib directory, I tried to change the name in my current directory of libmathtest.so.0.0.0 to libmathtest.so.0 but it still didn't work
    neither did copying libmathtest.so.0.0.0 to /usr/lib/postgresql/11/lib directory work.

Please note that same workflow works fine with rustlang shared lib by just replacing the .so file path in Makefile

Any help appreciated !

@daurnimator
Copy link
Contributor

I think this is a dupe of #2230 and #2231?

See also #3462

@ranjanprj
Copy link
Author

I think this is a dupe of #2230 and #2231?

See also #3462

Yes, I think I was looking for a way to skip the versioning generation stuff, is there a way that it can be skipped ? It would be cool we could build high perf extensions to PostgreSQL using Ziglang, as it has the right properties exactly for this use case.

@andrewrk
Copy link
Member

OK looks like this is a dupe of #2230 and #2231, both of which have been accepted, and are contributor friendly issues.

@carwyn
Copy link

carwyn commented Mar 23, 2024

May be of interest for those interested in using Zig for PostgreSQL extensions: https://github.com/xataio/pgzx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants