
Using PL/Tcl from Advanced Server
====================================
The PL/Tcl procedural language allows Advanced Server users to use Tcl/Tk 
functions in Advanced Server applications. Before using PL/Tcl with Advanced 
Server you must install TCL.

For Linux and Windows, download and run the ActiveTcl installer available at:

     www.activestate.com/activetcl/downloads/

For Solaris, download and install any available 64bit TCL package.

Linux and Solaris users: after installing Tcl, you must update the LD_LIBRARY_PATH 
variable to include the path to the TCL shared library.  The library is named 
libtclx.x.so (where x.x indicates the library version). 
 
Use the command:

     export LD_LIBRARY_PATH=<path-to-tcl-library>:$LD_LIBRARY_PATH

This command does not persistently alter the value of LD_LIBRARY_PATH; consult the 
documentation for your distribution of Linux for information about persistently 
setting the value of LD_LIBRARY_PATH. 
    
After installing PL/Tcl and setting the library path (if required), you must restart 
the Advanced Server service.  See the Postgres Plus Advanced Server Installation Guide 
for information about restarting the Advanced Server service.    

PL/Tcl is distributed with Postgres Plus Advanced Server.  You must install 
PL/Tcl in each database (or in a template database) before creating a PL/Tcl function.  
Use the CREATE LANGUAGE command at the EDB-PSQL command line to install PL/Tcl.  

To open an EDB-PSQL terminal window navigate through the Postgres Plus Advanced 
Server menu, to the Run SQL Command Line menu, and select EDB-PSQL. After connecting 
to an instance of Advanced Server, enter the command:
  
    CREATE LANGUAGE pltcl;

  Advanced Server confirms that the language is loaded with the response:

    CREATE LANGUAGE        
    
You can now use the features of the PL/Tcl language from within Advanced Server.  The 
following PL/Tcl example creates a function named tcl_max that returns the larger of 
two integer values:

    CREATE OR REPLACE FUNCTION tcl_max(integer, integer) RETURNS integer AS $$
        if {[argisnull 1]} {
            if {[argisnull 2]} { return_null }
                return $2
        }
        if {[argisnull 2]} { return $1 }
            if {$1 > $2} {return $1}
	            return $2
    $$ LANGUAGE pltcl;

  Call the function with the following commands:

    SELECT tcl_max(1, 2);

  or

    SELECT tcl_max(null, 2);



Resources
---------
For more information about using the Tcl procedural language with Advanced Server, consult 
the official Postgres documentation, available from EnterpriseDB at:

    www.enterprisedb.com/products-services-training/products/documentation


