Skip to content

PL/Python-style $_SD and native bytea transform (bytea_plphp)#34

Merged
jdatcmd merged 1 commit into
masterfrom
feature/sd-and-bytea
Jul 7, 2026
Merged

PL/Python-style $_SD and native bytea transform (bytea_plphp)#34
jdatcmd merged 1 commit into
masterfrom
feature/sd-and-bytea

Conversation

@jdatcmd

@jdatcmd jdatcmd commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

Closes the two PL/Python-parity gaps PL/php had. The session-global GD
equivalent already exists as $_SHARED; this adds the per-function private
dictionary and the native bytea mapping.

$_SD (per-function private data)

Each function now has an $_SD associative array that persists across calls to
that function within a session and is private to it, the per-function
counterpart to session-global $_SHARED. The pair mirrors PL/Python's SD/GD.

Implemented by prepending static $_SD = array(); to every generated wrapper.
The wrapped function is compiled once per session, so the static persists across
calls; a CREATE OR REPLACE installs a fresh op_array, so $_SD resets. No
per-call C lifecycle, no struct changes.

sql/sd.sql covers persistence across calls, per-function isolation, $_SD vs
$_SHARED, reset-on-redefine, structured values, and use inside a trigger.

bytea_plphp (native bytea transform)

A new transform extension mapping bytea to a raw, binary-safe PHP string and
back, mirroring PL/Python's bytea <-> bytes. The default text path routes
bytea through byteaout/byteain (a \x... hex string) and truncates on
embedded NULs; the transform passes the bytes themselves.

Modelled on jsonb_plphp, so it reuses the existing transform machinery and
also applies in nested contexts (a bytea column in a composite result, etc.).
Non-string returns are rejected with a clear error. varatt.h is included only
on PG 16+, where the varlena macros moved out of postgres.h.

Docs / CI

Language reference ($_SD section, bytea_plphp transform section), PL/Perl and
PL/Tcl comparison tables, README, and CHANGELOG ([Unreleased]) updated.
bytea_plphp is built and tested in both the CI matrix and the ASAN job,
alongside jsonb_plphp.

Testing

Verified in the plphp-build container on PG 18 / PHP 8.3: all 26 core tests
(including the new sd) plus the jsonb, hstore, and bytea suites pass.
The full PG 11-18 matrix runs here via CI.

🤖 Generated with Claude Code

Two PL/Python-parity features. PL/php already had the session-global GD
equivalent ($_SHARED); this adds the two pieces it lacked.

$_SD: a per-function static dictionary that persists across calls to the
same function within a session and is private to it. Implemented by
prepending "static $_SD = array();" to every generated wrapper: the
wrapped function is compiled once per session so the static persists,
and a CREATE OR REPLACE installs a fresh op_array so $_SD resets. No
per-call C lifecycle. Covered by sql/sd.sql (persistence, per-function
isolation, $_SD vs $_SHARED, reset-on-redefine, structured values).

bytea_plphp: a new transform extension mapping bytea to a raw, binary-safe
PHP string and back, mirroring PL/Python's bytea <-> bytes. The default
text path routes bytea through byteaout/byteain (a "\x..." hex string) and
truncates on embedded NULs; the transform passes the bytes themselves.
Reuses the existing transform machinery, so it also applies in nested
contexts (e.g. a bytea column in a composite result). Non-string returns
are rejected with a clear error. Modelled on jsonb_plphp; varatt.h is
included only on PG 16+ where the varlena macros moved out of postgres.h.

Docs (language reference, PL/Perl and PL/Tcl comparison tables, README,
CHANGELOG) and CI (bytea_plphp built and tested in the matrix and ASAN
jobs, alongside jsonb_plphp) updated.

Verified in the plphp-build container on PG 18 / PHP 8.3: all 26 core
tests (incl. sd) plus jsonb, hstore, and bytea suites green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@jdatcmd jdatcmd merged commit a7e7e6b into master Jul 7, 2026
12 checks passed
@jdatcmd jdatcmd deleted the feature/sd-and-bytea branch July 7, 2026 02:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants