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 5 – Returning...
Creating a PostgreSQL procedural language – Part 5 – Returning Results
Mark Wong

Creating a PostgreSQL procedural language – Part 5 – Returning Results

March 15, 2020/0 Comments/in Mark's PlanetPostgreSQL /by Mark Wong

This example will just be focusing on returning data from user defined functions, specifically returning a value as opposed to using OUT parameters.  The full code is on Github.

To quickly review, PL/Julia calls jl_eval_string() to execute Julia code and captures the returning jl_value_t data structure, which contains the result of the Julia code executed.  The result needs to be extracted from the jl_value_t data structure and then handled before returning the value to the calling SQL statement.

First we will create a helper function to convert a C string into the data type specified by the RETURN clause of the CREATE FUNCTION statement.

static Datum
cstring_to_type(char * input, Oid typeoid)
{
    HeapTuple typetuple;
    Form_pg_type pg_type_entry;
    Datum ret;

    typetuple = SearchSysCache(TYPEOID, ObjectIdGetDatum(typeoid), 0, 0, 0);
    pg_type_entry = (Form_pg_type) GETSTRUCT(typetuple);
    ret = OidFunctionCall3(pg_type_entry->typinput,
            CStringGetDatum(input), 0, -1);
    ReleaseSysCache(typetuple);
    PG_RETURN_DATUM(ret);
}

Let’s modify the user defined function test_julia() from before to now return a FLOAT4 value:

CREATE OR REPLACE FUNCTION test_julia(val INTEGER)
RETURNS FLOAT4 AS $$
    sqrt(val);
$$ LANGUAGE pljulia;

Now we will add some new code after calling jl_eval_string(). The results of the Julia code is captured and the jl_value_t structure needs to be examined to determine what kind of data is in it.  In this example, I only demonstrate how to check if the return type is floating point number.  If the returned data is verified to be a floating point (jl_float64_type), then convert it to a C string called buffer using a snprintf() call.

jl_value_t *ret;
ret = jl_eval_string(compiled_code);
if (jl_typeis(ret, jl_float64_type))
{
    double ret_unboxed = jl_unbox_float64(ret);

    buffer = (char *) palloc0((DOUBLE_LEN + 1) * sizeof(char));
    snprintf(buffer, DOUBLE_LEN, "%f", ret_unboxed);
}

We can now call the cstring_to_type() function with the results of jl_eval_string() copied into buffer and finally return from the user defined function.

PG_RETURN_DATUM(cstring_to_type(buffer, procedure_struct->prorettype));

Try executing the latest test_julia() user defined function again:

julia=# select test_julia(8);

test_julia
------------
2.828427
(1 row)

This is where I’ll stop blogging about PL/Julia development for the time being.  There are still a number of things left to do to make PL/Julia more robust and fully functional.  You may have also noticed various WARNING messages if you’ve been trying the code.  Please take a look at the issues list and feel free to try lending a hand.

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
Webinar: State of Full Text Search in PostgreSQL 12 [Follow Up] Full Text Search Feature in PostgreSQL WITH a little help from my friends
Scroll to top
×