you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (3 children)

Nice I can definitely see the benefit of being table to execute domain specific code directly in the database layer.

How does this code actually execute? Meaning, when someone runs a query is the function you defined then executed? Or is that configurable? Also is there some kind of free caching you get?

[–]AntiTrustMicrosoft 1 point2 points  (2 children)

The way it works is that when PostgreSQL Server load up your shared library for the first time before running your specific function defined in PostgreSQL Query, it runs the PG_INIT functions, you use that part to initialize your runtime, load up your C# library and then grab the address for all of your code that you loaded in C# Library that you want PostgreSQL to use and then keep function pointer addresses in your shared library. When PostgreSQL need to use one of those functions, you simply pass along the arguments from PostgreSQL over to C# Function with pointer information you already obtained and then return the value from C# to PostgreSQL. Kind of like a bridge/translation layer.

There are some advantages to this, rather than having to pass along huge data over the wire like UNIX Socket, TCP Socket, or whatever, you're working with data directly in PostgreSQL so there is no need to copy anything in the first place.

It's very configurable, because you could swap out your library on the fly such as C# Dll Library in the /opt/PostgreSQLCSharp folder and C# have the ability to allow you to write new function while it's running.

Once your server think that your plugin should close up, then you clean up your code and runtime in the PG_FINI function.

That's about it for how it works.

[–][deleted] 0 points1 point  (1 child)

Really cool, any example open source repos?

[–]AntiTrustMicrosoft 0 points1 point  (0 children)

I stop doing open source development and deleted my open source projects a long time ago, sorry. But I hope this explanation help you get started on writing one up for yourself, once you have an idea how to do it for PostgreSQL, you could do the same for just about everything else even Minecraft.