pltcl
PL/Tcl procedural language
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
pltcl | 1.0 | LANG | PostgreSQL | C |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 3240 | pltcl | No | Yes | No | Yes | No | No | - |
| 3250 | pltclu | No | No | No | Yes | No | No | - |
| Related | plpgsql plperl plpython3u pg_tle plv8 pllua pljava |
|---|
Version
| PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|
| 1.0 | 1.0 | 1.0 | 1.0 | 1.0 |
Install
Note: This is a built-in contrib extension of PostgreSQL
CREATE EXTENSION pltcl;
Usage
PL/Tcl allows writing PostgreSQL functions in the Tcl programming language. As a trusted language, it restricts access to the filesystem and other system resources.
CREATE EXTENSION pltcl;
-- Simple function returning a greeting
CREATE FUNCTION tcl_hello(name text) RETURNS text
LANGUAGE pltcl AS $$
return "Hello, $1!"
$$;
SELECT tcl_hello('world');
-- Function with conditional logic
CREATE FUNCTION tcl_max(a integer, b integer) RETURNS integer
LANGUAGE pltcl AS $$
if {$1 > $2} {return $1}
return $2
$$;
-- Set-returning function
CREATE FUNCTION tcl_sequence(count integer) RETURNS SETOF integer
LANGUAGE pltcl AS $$
for {set i 1} {$i <= $1} {incr i} {
return_next $i
}
$$;
-- Trigger function
CREATE FUNCTION tcl_audit() RETURNS trigger
LANGUAGE pltcl AS $$
set NEW(modified_at) [clock format [clock seconds] -format "%Y-%m-%d %H:%M:%S"]
return [array get NEW]
$$;
Database access from PL/Tcl uses the spi_exec command:
CREATE FUNCTION tcl_count_rows(tbl text) RETURNS integer
LANGUAGE pltcl AS $$
spi_exec "SELECT count(*) AS cnt FROM $1"
return $cnt
$$;
Feedback
Was this page helpful?
Thanks for the feedback! Please let us know how we can improve.
Sorry to hear that. Please let us know how we can improve.