pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Dunstan <andrew.dunstan@2ndquadrant.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-hackers@postgreSQL.org, Jan Wieck <jan@wi3ck.info>
Subject: Re: I propose killing PL/Tcl's "modules" infrastructure
Date: Sat, 25 Feb 2017 13:55:26 -0500
Message-ID: <33fe0208-74df-1a03-08af-9a4f0d1ed14d@2ndQuadrant.com> (raw)
In-Reply-To: <23184.1488048288@sss.pgh.pa.us>
References: <22067.1488046447@sss.pgh.pa.us>
	<234c68e3-47ee-4db1-fe6d-6a5de56575b6@2ndQuadrant.com>
	<23184.1488048288@sss.pgh.pa.us>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-hackers>



On 02/25/2017 01:44 PM, Tom Lane wrote:
> Andrew Dunstan <andrew.dunstan@2ndquadrant.com> writes:
>> On 02/25/2017 01:14 PM, Tom Lane wrote:
>>> Now, we could try to fix this bug, and add the regression test coverage
>>> that the code clearly lacks, and upgrade the documentation about it from
>>> its currently very sad state.  But I think the right answer is just to
>>> remove the feature altogether.  It's evidently not being used, and it's
>>> kind of insecure by design, and it would not be that hard for someone
>>> to provide equivalent functionality entirely in userland if they really
>>> wanted it.
>> In PLv8 we added a parameter plv8.start_proc that names a parameterless
>> function that's executed when plv8 is first called in each session. It
>> can be used quite easily to implement something like a modules
>> infrastructure - in fact I have used it to good effect for exactly that.
>> Maybe something similar for pltcl would be a good thing.
> Yeah, the only part that's even a bit hard to replicate in userland is
> initializing the autoloading mechanism in each session.  It would be
> cleaner to provide a feature similar to what you describe that could
> be used for that purpose as well as others.  However, where does the
> "parameterless function" come from?  Is it a regular PLv8 (or for this
> purpose PL/Tcl) function expected to be present in pg_proc?
>
> 			


Yes, it's a regular PLv8 function.Here's an example. It presupposes that
there is a table called plv8_modules (modname text, code text,
load_on_start boolean).

    CREATE OR REPLACE FUNCTION public.plv8_startup()
     RETURNS void
     LANGUAGE plv8
    AS $function$
      if (typeof plv8_loaded_modules == 'undefined')
        plv8_loaded_modules = {};
      load_module = function(modname)
      {
        if (plv8_loaded_modules[modname])
            return;
        var rows = plv8.execute("SELECT code from plv8_modules " +
                                " where modname = $1", [modname]);
        for (var r = 0; r < rows.length; r++)
        {
            var code = rows[r].code;
            eval("(function() { " + code + "})")();
            // plv8.elog(NOTICE,"loaded module " + modname);
            plv8_loaded_modules[modname] = 1;
        }
           
      };
      reload_module = function(modname)
      {
        var rows = plv8.execute("SELECT code from plv8_modules " +
                                " where modname = $1", [modname]);
        for (var r = 0; r < rows.length; r++)
        {
            var code = rows[r].code;
            eval("(function() { " + code + "})")();
            // plv8.elog(NOTICE,"loaded module " + modname);
            plv8_loaded_modules[modname] = 1;
        }

      };

      var rows = plv8.execute("SELECT modname, code from plv8_modules
    where load_on_start");
      for (var r = 0; r < rows.length; r++)
      {
        var modname = rows[r].modname;
        if (plv8_loaded_modules[modname])
            continue;
        var code = rows[r].code;
        eval("(function() { " + code + "})")();
        plv8_loaded_modules[modname] = 1;
        // plv8.elog(NOTICE,"loaded module " + modname);
      };
    $function$;


cheers

andrew

-- 
Andrew Dunstan                https://www.2ndQuadrant.com
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services



-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers



view thread (17+ messages)  latest in thread

Message-ID: <33fe0208-74df-1a03-08af-9a4f0d1ed14d@2ndQuadrant.com>
Permalink:  ../33fe0208-74df-1a03-08af-9a4f0d1ed14d@2ndQuadrant.com/
Also on:    postgresql.org/message-id/33fe0208-74df-1a03-08af-9a4f0d1ed14d@2ndQuadrant.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: andrew.dunstan@2ndquadrant.com, tgl@sss.pgh.pa.us, jan@wi3ck.info
  Subject: Re: I propose killing PL/Tcl's "modules" infrastructure
  In-Reply-To: <33fe0208-74df-1a03-08af-9a4f0d1ed14d@2ndQuadrant.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox