2ndQuadrant is now part of EDB

Bringing together some of the world's top PostgreSQL experts.

2ndQuadrant | PostgreSQL
Mission Critical Databases
  • Contact us
  • EN
    • FR
    • IT
    • ES
    • DE
    • PT
  • Support & Services
  • Products
  • Downloads
    • Installers
      • Postgres Installer
      • 2UDA – Unified Data Analytics
    • Whitepapers
      • Business Case for PostgreSQL Support
      • Security Best Practices for PostgreSQL
    • Case Studies
      • Performance Tuning
        • BenchPrep
        • tastyworks
      • Distributed Clusters
        • ClickUp
        • European Space Agency (ESA)
        • Telefónica del Sur
        • Animal Logic
      • Database Administration
        • Agilis Systems
      • Professional Training
        • Met Office
        • London & Partners
      • Database Upgrades
        • Alfred Wegener Institute (AWI)
      • Database Migration
        • International Game Technology (IGT)
        • Healthcare Software Solutions (HSS)
        • Navionics
  • Postgres Learning Center
    • Webinars
      • Upcoming Webinars
      • Webinar Library
    • Whitepapers
      • Business Case for PostgreSQL Support
      • Security Best Practices for PostgreSQL
    • Blog
    • Training
      • Course Catalogue
    • Case Studies
      • Performance Tuning
        • BenchPrep
        • tastyworks
      • Distributed Clusters
        • ClickUp
        • European Space Agency (ESA)
        • Telefónica del Sur
        • Animal Logic
      • Database Administration
        • Agilis Systems
      • Professional Training
        • Met Office
        • London & Partners
      • Database Upgrades
        • Alfred Wegener Institute (AWI)
      • Database Migration
        • International Game Technology (IGT)
        • Healthcare Software Solutions (HSS)
        • Navionics
    • Books
      • PostgreSQL 11 Administration Cookbook
      • PostgreSQL 10 Administration Cookbook
      • PostgreSQL High Availability Cookbook – 2nd Edition
      • PostgreSQL 9 Administration Cookbook – 3rd Edition
      • PostgreSQL Server Programming Cookbook – 2nd Edition
      • PostgreSQL 9 Cookbook – Chinese Edition
    • Videos
    • Events
    • PostgreSQL
      • PostgreSQL – History
      • Who uses PostgreSQL?
      • PostgreSQL FAQ
      • PostgreSQL vs MySQL
      • The Business Case for PostgreSQL
      • Security Information
      • Documentation
  • About Us
    • About 2ndQuadrant
    • 2ndQuadrant’s Passion for PostgreSQL
    • News
    • Careers
    • Team Profile
  • Blog
  • Menu Menu
You are here: Home1 / Blog2 / Mark's PlanetPostgreSQL3 / Creating a PostgreSQL procedural language – Part 3 – Executing...
Creating a PostgreSQL procedural language – Part 3 – Executing User Code
Mark Wong

Creating a PostgreSQL procedural language – Part 3 – Executing User Code

February 25, 2020/0 Comments/in Mark's PlanetPostgreSQL /by Mark Wong

A PostgreSQL procedural language handler needs to look up the body of the user defined function or stored procedure for the code to execute.  Remember that the C function executed to handle PL/Julia user defined functions and stored procedures is defined in the SQL file. Thus the pljulia_call_handler() function needs to be updated to retrieve the body of the respective function or procedure.

The following code examples are abbreviated and broken up a bit to hopefully make it easier to follow the general flow of the code.  The complete changes are on GitHub.

The bulk of the new code is around retrieving the body of the user defined function or stored procedure that is being called.  The identifier of the function or procedure is passed to the procedural language handler as fcinfo->flinfo->fn_oid, so it is just a matter of searching the PostgreSQL system cache with that identifier and retrieving the body of the function with the following code.

 

    HeapTuple procedure_tuple;
    Datum procedure_source_datum;
    const char *procedure_code;
    bool isnull;

    procedure_tuple = SearchSysCache(PROCOID,
            ObjectIdGetDatum(fcinfo->flinfo->fn_oid), 0, 0, 0);

    procedure_source_datum = SysCacheGetAttr(PROCOID, procedure_tuple,
            Anum_pg_proc_prosrc, &isnull);

    procedure_code = DatumGetCString(DirectFunctionCall1(textout,
            procedure_source_datum));

Now we have a copy of the Julia code stored in a string variable called procedure_code.  It can be executed by passing it to Julia with the jl_eval_string() API call, which will then return the results from the code into a special variable ret.  There will be more on this special variable in a later blog.

    jl_value_t *ret;
    ret = jl_eval_string(procedure_code);

In keeping the example short, I omitted much of the error handling in the previous snippets but they do exist.  Now that we are able to execute arbitrary Julia code, it is important to note how to detect whenever there is any issue with the code.  Julia’s C API provides a function to check for exceptions and we will capture all issues into PostgreSQL log.

    if (jl_exception_occurred())
            elog(ERROR, "%s", jl_typeof_str(jl_exception_occurred()));

In order to keep the changes to the code small, we will continue to only check if the executed code from the user defined function or stored procedure is a Julia float64 data type and capture the results of the executed Julia code into the PostgreSQL log.

    if (jl_typeis(ret, jl_float64_type))
    {
        double ret_unboxed = jl_unbox_float64(ret);
        elog(DEBUG1, "ret: %e", ret_unboxed);
    }

These changes now allow us to write a PL/Julia user defined function to calculate the square root of 2, instead of hard coding it into the procedural language handler.

CREATE OR REPLACE FUNCTION test_julia()
RETURNS VOID
AS $$
    sqrt(2.0);
$$ LANGUAGE pljulia;

There is still more work to be done in order to return the results back to the SQL statement, but for now we can write functions in Julia that return floating point numbers.

Tags: julia, pljulia
Share this entry
  • Share on Facebook
  • Share on Twitter
  • Share on WhatsApp
  • Share on LinkedIn
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search

Get in touch with us!

Recent Posts

  • Random Data December 3, 2020
  • Webinar: COMMIT Without Fear – The Beauty of CAMO [Follow Up] November 13, 2020
  • Full-text search since PostgreSQL 8.3 November 5, 2020
  • Random numbers November 3, 2020
  • Webinar: Best Practices for Bulk Data Loading in PostgreSQL [Follow Up] November 2, 2020

Featured External Blogs

Tomas Vondra's Blog

Our Bloggers

  • Simon Riggs
  • Alvaro Herrera
  • Andrew Dunstan
  • Craig Ringer
  • Francesco Canovai
  • Gabriele Bartolini
  • Giulio Calacoci
  • Ian Barwick
  • Marco Nenciarini
  • Mark Wong
  • Pavan Deolasee
  • Petr Jelinek
  • Shaun Thomas
  • Tomas Vondra
  • Umair Shahid

PostgreSQL Cloud

2QLovesPG 2UDA 9.6 backup Barman BDR Business Continuity community conference database DBA development devops disaster recovery greenplum Hot Standby JSON JSONB logical replication monitoring OmniDB open source Orange performance PG12 pgbarman pglogical PG Phriday postgres Postgres-BDR postgres-xl PostgreSQL PostgreSQL 9.6 PostgreSQL10 PostgreSQL11 PostgreSQL 11 PostgreSQL 11 New Features postgresql repmgr Recovery replication security sql wal webinar webinars

Support & Services

24/7 Production Support

Developer Support

Remote DBA for PostgreSQL

PostgreSQL Database Monitoring

PostgreSQL Health Check

PostgreSQL Performance Tuning

Database Security Audit

Upgrade PostgreSQL

PostgreSQL Migration Assessment

Migrate from Oracle to PostgreSQL

Products

HA Postgres Clusters

Postgres-BDR®

2ndQPostgres

pglogical

repmgr

Barman

Postgres Cloud Manager

SQL Firewall

Postgres-XL

OmniDB

Postgres Installer

2UDA

Postgres Learning Center

Introducing Postgres

Blog

Webinars

Books

Videos

Training

Case Studies

Events

About Us

About 2ndQuadrant

What does 2ndQuadrant Mean?

News

Careers 

Team Profile

© 2ndQuadrant Ltd. All rights reserved. | Privacy Policy
  • Twitter
  • LinkedIn
  • Facebook
  • Youtube
  • Mail
Announcing New PostgreSQL Online Training Series How to use the SVM Machine Learning Model with 2UDA – PostgreSQL and Orange (Part 2) How to use the SVM Machine Learning Model with 2UDA – PostgreSQL and Orange...
Scroll to top
×