Read my latest article: Announcing RailsDeveloper (posted Wed, 01 Sep 2010 17:01:00 GMT)

PL/Ruby loves RubyGems and DRb

Posted by Robby Russell Mon, 22 Aug 2005 21:09:00 GMT

33 comments Latest by zhu zhu pets Fri, 20 Aug 2010 03:31:31 GMT

I admit it. I have had a torrid love affair with procedural languages ever since I started playing with PostgreSQL. The ability to share logic amongst all the applications touching the same database server.. was…well… a breath of fresh air.

What is a procedural language in Postgresql?

PostgreSQL docs describe them as, ”…allows user-defined functions to be written in other languages besides SQL and C. “

Well, PostgreSQL has PLs for Perl, Python, Java, C, PHP… and even RUBY!


CREATE FUNCTION ruby_max(int4, int4) RETURNS int4 AS '
    if args[0].to_i > args[1].to_i
        return args[0]
    else
        return args[1]
    end
' LANGUAGE 'plruby';

PL/PGSQL is nice and all, but it’s not as fun as playing with Ruby. PL/Perl… well is perl, and PL/Python… is python. Both PL/Perl and PL/Python have untrusted variants. You see, they don’t want your PostgreSQL server to do anything harmful to the machine by being able to do stuff like system(‘cat /dev/null > /etc/passwd). But for some people, (like me) they want the flexibility of their language anyways. :-)

Note: Never do this if your system user that runs PostgreSQL has privileges to do anything harmful on your system.

The PL/Ruby documentation is minimal at the moment, but covers enough to get you started. I don’t know if many people are using it out there… but hopefully that is about to change! I’ve played with it a bit, but always wanted to be able to do stuff like require ‘rubygems’, but this is a feature of an untrusted language. I even found myself digging around in C code to see if I could figure out how to hack the plruby language to skip over those checks… but I am not a C programmer and I got lost in some header files.

Then it hit me. “Why haven’t you emailed the author?”

So I emailed the author of PL/Ruby, Guy Decoux, who responded pretty quickly with the answer to my dreams! Okay, I do have bigger dreams than this… but you get the idea.

First of all, some of you might be thinking, ”Why on Earth would you want to do this?”

Well, here is a simple example of how it could be used with RedCloth Let’s say that I want to be able to perform the following query from within SQL.

SELECT redcloth(‘strong text and emphasized text‘);

Why not do this in the application? Well, I do actually have a case where I have an older PHP application that I will be porting to Ruby in the future, but would like to give the application some access to some of the features of Ruby that I will be using, such as RedCloth.

Okay, so show me an example of one of these scary PostgreSQL functions.


CREATE FUNCTION redcloth(text) RETURNS text AS '

  require ''rubygems''
  require ''redcloth''

  content = args[0]

  rc = RedCloth.new(content)

  return rc.to_html

' LANGUAGE 'plruby';

”Wait! You said this would be scary!?”

Well, PL/Ruby allows you to write… plain ole Ruby within your functions. (do you see where I am getting here?)

PL/Ruby meets RedCloth


 rb=# SELECT redcloth('*strong text* and _emphasized text_');
                             redcloth
------------------------------------------------------------------
 <p><strong>strong text</strong> and <em>emphasized text</em></p>
(1 row)

PL/Ruby meets ShortURL


CREATE FUNCTION rubyurlize(text) RETURNS text AS '

  require ''rubygems''
  require ''shorturl''

  return ShortURL.shorten(args[0])

' LANGUAGE 'plruby';

...which allows for


 rb=# SELECT
rb-#   rubyurlize('http://www.robbyonrails.com/') as link1,
rb-#   rubyurlize('http://moulon.inra.fr/ruby/plruby.html') as link2;
          link1           |         link2
--------------------------+------------------------
 http://rubyurl.com/lyoKm | http://rubyurl.com/dTo
(1 row)

PostgreSQL meets DRb

Okay, this is one of the reasons why I wanted to play with PL/Ruby a bit more. Distributed Ruby Objects… from PostreSQL?

What is DRb?

If you don’t know already… per the description in RDOC, “dRuby is a distributed object system for Ruby. It allows an object in one Ruby process to invoke methods on an object in another Ruby process on the same or a different machine.”

It basically allows you to share an object to other machines… at the same time!

mmm…distributed objects…

DRb Object

Here is a simple ruby script that you would run from the shell. It creates a DRb object which accepts connections at localhost:9000.


#!/usr/bin/ruby

require 'drb'

class MyRemoteObject
  def say(str)
    return "You say #{str}. I say #{str.reverse.upcase}!" 
  end
end

server = MyRemoteObject.new

DRb.start_service('druby://localhost:9000', server)
DRb.thread.join

Start me up!

$ ruby mydrb.rb

Now that we have DRb running and listening for connections…we need a client to connect to it.

DRb function in PL/Ruby

Here is a very simple DRb client script and I just drop that into a PostgreSQL function.


CREATE FUNCTION drb_test(text) RETURNS text AS '

  require ''drb''

  DRb.start_service

  ro = DRbObject.new(nil, ''druby://localhost:9000'')

  return ro.say(args[0])

' LANGUAGE 'plruby';

The result?


rb=# SELECT drb_test('Potato');
           drb_test
-------------------------------
 You say Potato. I say OTATOP!
(1 row)

Are we having fun yet?

Okay, so how do I manage to get this to work? Well… for that, you will have to read my blog post, Installing untrusted PL/Ruby for PostgreSQL

Let’s all go get some coffee (or tea) and start playing with PL/Ruby today!

Subscribe to my RSS feed Enjoying the content? Be sure to subscribe to my RSS feed.
Comments

Leave a response

  1. Avatar
    Danyel Lawson Sun, 09 Mar 2008 17:09:20 GMT

    Excellent article. I like the presentation. Simple direct and to the point. Very informative. I wasn’t aware of this functionality in Ruby. I’m glad I stumbled across this article.

  2. Avatar
    parasew Tue, 01 Apr 2008 15:01:42 GMT

    since this article is from 2005 i was wondering what the status is today regarding PL/Ruby in postgreSQL? did you continue working with PL/Ruby? what is the performance when using external rubygems such as redcloth?

  3. Avatar
    William T Nelson Tue, 21 Apr 2009 18:59:39 GMT

    Postgres 8.0 and later removes the ‘make install-all-headers’ requirement.

  4. Avatar
    odpieniacze Thu, 08 Apr 2010 09:55:40 GMT

    It’s very good article. Great site with very good look and perfect information.

  5. Avatar
    szafy metalowe Thu, 08 Apr 2010 09:55:59 GMT

    It’s very good article….

  6. Avatar
    all star shoes Thu, 13 May 2010 16:34:49 GMT
  7. Avatar
    louis vuitton shorts Thu, 03 Jun 2010 01:15:22 GMT

    Beauteous white roses are blooming peacefully in the wizard’s secret garden. Someone could hear the voice of blooming whilst someone might see the sequel of fading. http://www.the-various.com/

  8. Avatar
    air jordan 23 premier Thu, 03 Jun 2010 04:52:56 GMT

    tr yrt

  9. Avatar
    Shop By Brand Mon, 07 Jun 2010 01:18:08 GMT
  10. Avatar
    nike air tailwind Mon, 07 Jun 2010 01:18:25 GMT

    try tr

  11. Avatar
    mbt chapa Thu, 10 Jun 2010 09:15:56 GMT

    rt y

  12. Avatar
    kobe basketball shoes Sat, 12 Jun 2010 09:02:08 GMT

    tr yrt

  13. Avatar
    uggs new arrival Thu, 17 Jun 2010 06:25:13 GMT

    ty u

  14. Avatar
    jordan six rings Thu, 17 Jun 2010 06:25:53 GMT

    rt r

  15. Avatar
    dunk sb mid Fri, 18 Jun 2010 07:28:21 GMT

    YTU TY

  16. Avatar
    dunk sb mid Fri, 18 Jun 2010 07:29:20 GMT

    YTU TY

  17. Avatar
    asdf Sat, 19 Jun 2010 01:47:18 GMT

    Never do this if your system nike shoes air jordansser that runs PostgreSQL has privileges to do anything harmful on your system.

  18. Avatar
    discount mbt shoes Sun, 20 Jun 2010 02:51:24 GMT

    ty uty

  19. Avatar
    mbt walk blueu Sun, 20 Jun 2010 02:51:34 GMT

    ty

  20. Avatar
    wangwei Sat, 26 Jun 2010 08:26:13 GMT
  21. Avatar
    discound p90x dvds Tue, 29 Jun 2010 04:07:18 GMT

    Thanks a lot for enjoying this beauty article with me. I am apreciating it very much! Looking forward to another great article. Good luck to the author! all the best!

  22. Avatar
    clothing stores Tue, 29 Jun 2010 04:29:49 GMT

    Never do this if your system user that runs PostgreSQL has privileges to do anything harmful on your system.

  23. Avatar
    air jordan 11 Wed, 30 Jun 2010 08:26:24 GMT

    Demonstrate a unique new conceptjordan shoesAwq2

  24. Avatar
    canadian pharmacy Wed, 14 Jul 2010 18:19:26 GMT

    People calling MySQL a “toy” is a counter-productive argument because it seems to imply that, if it works for you, your hard work must amount to a “toy” project.

  25. Avatar
    spray foam insulation Wed, 21 Jul 2010 17:56:26 GMT

    nice post

  26. Avatar
    banggood Fri, 23 Jul 2010 01:19:48 GMT

    The first commercially automated wholesale Cell phones network (the 1G generation) was launched in Japan by NTT in 1979, initially in the metropolitan area of Tokyo. Within five years, the NTT network had been expanded to cover the whole population of Japan and became the first nation-wide 1G network. In 1981, this was followed by the simultaneous launch of the Nordic wholesale Cell phones (NMT) system in Denmark, Finland, Norway and Sweden. NMT was the first mobile phone network featuring international roaming. The first 1G network launched in the USA was Chicago based Ameritech in 1983 using the Motorola DynaTAC wholesale Cell phones. Several countries then followed in the early 1980s including the UK, Mexico and Canada.

  27. Avatar
    spray foam insulation Fri, 23 Jul 2010 06:16:10 GMT

    hehe. Like this stuff.

  28. Avatar
    fiwedding Mon, 26 Jul 2010 07:57:48 GMT

    People calling MySQL a “toy” is a counter-productive argument because it seems to imply that, if it works for you, your hard work must amount to a “toy” project.

  29. Avatar
    shower door Mon, 09 Aug 2010 20:33:24 GMT

    Great!!!

  30. Avatar
    zdbjnd Wed, 11 Aug 2010 07:51:06 GMT

    buy ugg boots discount ugg boots amazonboot onuggboots UGG Stripe Cable Knit Boots UGG Classic Cardy Boots Grey Classic Argyle Knit UGG Boots Black Classic Argyle Knit UGG Boots Brown Classic Argyle Knit UGG Boots Chocolate Classic Argyle Knit UGG Boots Cream Classic Argyle Knit UGG Boots Fig Classic Argyle Knit UGG Boots Grey Classic Argyle Knit UGG Boots Classic Cardy UGG Boots Black Classic Cardy UGG Boots Chocolate Classic Cardy UGG Boots Cream Natural Classic Cardy UGG Boots Dusty Rose Classic Cardy UGG Boots Grey Classic Cardy UGG Boots Oatmeal Classic Cardy UGG Boots Stonewash Classic Cardy UGG Boots Stout Classic Cardy UGG Boots Stripe Cable Knit UGG Boots Black Cream Stripe Cable Knit UGG Boots Chocolate Cream Stripe Cable Knit UGG Boots Navy Cream Stripe Cable Knit UGG Boots Classic Mini UGG Boots Black Classic Mini UGG Boots Chestnut Classic Mini UGG Boots Chocolate Classic Mini UGG Boots Grey Classic Mini UGG Boots Classic Short UGG Boots Aqua Classic Short UGG Boots Black Classic Short UGG Boots Chestnut Classic Short UGG Boots Chocolate Classic Short UGG Boots Grey Classic Short UGG Boots Leopard Classic Short UGG Boots Pink Classic Short UGG Boots Romantic Flower Classic Short UGG Boots Sand Classic Short UGG Boots Ultramarine Classic Short UGG Boots Classic Tall UGG Boots Black Classic Tall UGG Boots Chestnut Classic Tall UGG Boots Chocolate Classic Tall UGG Boots Grey Classic Tall UGG Boots Leopard Classic Tall UGG Boots Pink Classic Tall UGG Boots Romantic Flower Classic Tall UGG Boots Sand Classic Tall UGG Boots Ugg Stripe Cable Knit Boots UGG Stripe Cable Knit Boots Navy Cream UGG Stripe Cable Knit Boots Chocolate Cream UGG Stripe Cable Knit Boots Black Cream UGG Classic Cardy Boots UGG Classic Cardy Boots Stout UGG Classic Cardy Boots Stonewash UGG Classic Cardy Boots Oatmeal UGG Classic Cardy Boots Grey UGG Classic Cardy Boots Dusty Rose UGG Classic Cardy Boots Cream Natural UGG Classic Cardy Boots Chocolate UGG Classic Cardy Boots Black Ugg Classic Argyle Knit Boots UGG Classic Argyle Knit Boots Grey UGG Classic Argyle Knit Boots Fig UGG Classic Argyle Knit Boots Cream UGG Classic Argyle Knit Boots Brown UGG Classic Argyle Knit Boots Black UUGG Classic Argyle Knit Boots Cream UGG Classic Mini Boots UGG Classic Mini Boots Sand UGG Classic Mini Boots Grey UGG Classic Mini Boots Chocolate UGG Classic Mini Boots Chestnut UGG Classic Mini Boots Black UGG Classic Tall Boots Ugg Classic Tall Boots Sand Ugg Classic Tall Boots Romantic Flower Ugg Classic Tall Boots Pink UGG Classic Mini Boots Chestnut Ugg Classic Tall Boots Grey Ugg Classic Tall Boots Chocolate Ugg Classic Tall Boots Chestnut Ugg Classic Tall Boots Black UGG Classic Short Boots UGG Classic Short Boots Aqua UGG Classic Short Boots Ultramarine UGG Classic Short Boots Sand UGG Classic Short Boots Romantic Flower UGG Classic Short Boots Pink UGG Classic Short Boots Leopard UGG Classic Short Boots Grey UGG Classic Short Boots Chocolate UGG Classic Short Boots Chestnut UGG Classic Short Boots Black

  31. Avatar
    Herve leger Thu, 12 Aug 2010 08:40:09 GMT

    We are a professional exporter and wholesaler of brand fashion products,Herve Leger Slash Dress Pink Herve Leger Red Isabelle Bandage Dress Herve Leger Black Strapless Bandage Dress Herve Leger V neck Bandage Dress Black Herve Leger Black Isabelle Bandage Dress,All products have good quality,fast and safe delivery without shipping fee. Our primary goal is to meet our clients’ requirement and establish mutually pleasant business relationships with you.If you are interested, please do not hesitate to contact us.

  32. Avatar
    Coach Handbags Fri, 13 Aug 2010 01:51:20 GMT

    We are a professional exporter and wholesaler of brand fashion products,Coach Signature Large Zoe Shoulder Bags 12674 Black Coach Signature Large Zoe Shoulder Bags 12674 Brown Coach Signature Parker Demi Shoulder Bag Tote 13408 Sand Coach Signature Zoe Shoulder Bags 12657 Khaki Mahog Coach Simple Inverted Trapezoidal Shape Handbags Black,All products have good quality,fast and safe delivery without shipping fee. Our primary goal is to meet our clients’ requirement and establish mutually pleasant business relationships with you.If you are interested, please do not hesitate to contact us.

  33. Avatar
    zhu zhu pets Fri, 20 Aug 2010 03:31:31 GMT

    Our main purpose is to help you find the hot toys . Our Animal Shaped Rubber Bands is selling fast. These animal rubber bands wholesale are made of silicone. wholesale Animal Shaped Rubber Bands are not just pretty,Animal Rubber Bands wholesale are also quite clever because they recover their original shape after each use, so you can use them over and over again. zhu zhu pets, Baby Carriers is our new product.It is one of our best seller. , power balance Pillow Pets Plush Pencil Case is hot selling. Silicone Watch They are rich-looking and most attractive and very popular with our other customers. We have business conections all over the world.Our hot toys includes: Taylormade golf, Callaway Golf, bakugan battle brawlers and something special golf equipment Animal Shaped Rubber Bands We show you the hottest toys that are out right now. Our lists are compiled using analgorithym based on the toys popularity, quality, value, safety, awards, and innovation. You will find your favorite toys in toptoys2trade.com. new bakugan include female bakugan, it is really amazing cute. Everyone loves her. Solar Powered Toys helps you enjoy low-carbon lifestyle, with the help of sun, build your interesting life. prada handbags ecco shoes d&g handbags guess handbags http://www.jerseys2trade.com/power-balance-c-142/ ">power balance

Share your thoughts... (really...I want to hear them)

Comments