Skip to content

plphp

Embed PHP 8 as an untrusted PostgreSQL procedural language

plphp · Embed PHP 8 as an untrusted PostgreSQL procedural language

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
3170plphpplphp2.6LANGPostgreSQLC
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d--NoYesNoYesnono
Relationships
See Alsoplruby plperl plpython3u pllua plv8
Uses non-ZTS PHP embed. EL8/EL9 RPMs require the PHP 8.2/8.3 module streams respectively; DEBs pin the matching PHP embed runtime for each OS. No preload or fixed schema. The three optional transforms are not included in this package.

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXTPIGSTY2.619 18 17 16 15plphp-
RPMPIGSTY2.619 18 17 16 15plphp_$vphp-embedded
DEBPIGSTY2.619 18 17 16 15postgresql-$v-plphplibphp-embed
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6
el8.aarch64N/APIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6
el9.x86_64PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6
el9.aarch64PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6
el10.x86_64PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6
el10.aarch64PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6
d12.x86_64PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6
d12.aarch64PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6
d13.x86_64PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6
d13.aarch64PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6
u22.x86_64PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6
u22.aarch64PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6
u24.x86_64PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6
u24.aarch64PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6
u26.x86_64PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6
u26.aarch64PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6PIGSTY 2.6

Source

pig build pkg plphp;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

pig repo add pgsql -u   # add both repo and update cache

Install this extension with pig:

pig install plphp;		# install by package name

pig install plphp -v 19;   # install for PG 19
pig install plphp -v 18;   # install for PG 18
pig install plphp -v 17;   # install for PG 17
pig install plphp -v 16;   # install for PG 16
pig install plphp -v 15;   # install for PG 15

Create this extension with:

CREATE EXTENSION plphp;

Usage

Sources:

plphp embeds PHP as an untrusted PostgreSQL procedural language for functions, procedures, triggers, event triggers, and anonymous blocks. Use it only when PHP APIs are worth running with the operating-system privileges of the PostgreSQL server process; it is not a sandboxed language.

Core Workflow

CREATE EXTENSION plphp;

CREATE FUNCTION php_square(integer)
RETURNS integer
LANGUAGE plphp
AS $$
    return $args[0] * $args[0];
$$;

SELECT php_square(12);

PL/php 2.6 supports PHP 8.1 through 8.4 using the embed SAPI without ZTS and PostgreSQL 11 through 18. Installation and creation require a superuser because the language is deliberately untrusted.

Main Interfaces

  • spi_exec(...), spi_prepare(...), spi_exec_prepared(...), spi_query(...), and cursor helpers access PostgreSQL through SPI.
  • return_next() emits rows from set-returning functions.
  • spi_commit() and spi_rollback() provide transaction control inside procedures.
  • subtransaction(...) creates a catchable subtransaction boundary.
  • $_TD exposes trigger and event-trigger context.
  • $_SHARED is session-global storage, while $_SD is private per-function session storage.
  • plphp.on_init, plphp.start_proc, and plphp_modules initialize a session interpreter.

The optional jsonb_plphp, hstore_plphp, and bytea_plphp extensions add native transforms. A function must declare TRANSFORM FOR TYPE ... to use a transform; installing a companion does not change every PL/php function automatically.

Security and Operations

A PL/php function can read or write files, open network connections, invoke PHP facilities, and affect anything accessible to the PostgreSQL operating-system account. Only grant function ownership and creation rights to roles trusted like a server administrator. Apply normal SECURITY DEFINER and search_path hardening when a privileged wrapper is unavoidable.

SPI query text, dynamic identifiers, session initialization code, loaded modules, and persistent interpreter state all require review. Long-running PHP code blocks a PostgreSQL backend, and memory retained in $_SHARED or $_SD lasts for the session. Use prepared statements for untrusted values, bound resource use, test cancellation/error paths, and recycle sessions when application code retains excessive state.

Version 2.6 adds per-function $_SD and the binary-safe bytea_plphp transform. PostgreSQL tracks the control version as 2.6 while the source distribution is 2.6.0.

Table reader