Skip to content

ltree_plpython3u

transform between ltree and plpython3u

plpython3u · transform between ltree and plpython3u

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
3292ltree_plpython3uplpython3u1.0LANGPostgreSQLC
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d-rNoYesNoYesyesno

Packages

PG19PG18PG17PG16PG15
1.01.01.01.01.0
This contrib extension ships with PostgreSQL.

Install

Create this extension with:

CREATE EXTENSION ltree_plpython3u CASCADE; -- requires ltree, plpython3u

Usage

ltree_plpython3u: Transform between ltree and PL/Python3

Provides a transform for the ltree type for PL/Python3U. When loaded, ltree values are automatically converted to Python strings and vice versa.

CREATE EXTENSION ltree_plpython3u;

CREATE FUNCTION ltree_depth(val ltree) RETURNS integer
LANGUAGE plpython3u TRANSFORM FOR TYPE ltree AS $$
  # val is now a Python string like "a.b.c"
  return len(val.split('.'))
$$;

CREATE FUNCTION ltree_leaf(val ltree) RETURNS text
LANGUAGE plpython3u TRANSFORM FOR TYPE ltree AS $$
  return val.split('.')[-1]
$$;

SELECT ltree_depth('top.science.astronomy'::ltree);  -- 3
SELECT ltree_leaf('top.science.astronomy'::ltree);    -- astronomy

Table reader