CREATE_LANGUAGE(7pg16)

πŸ“¦ version 16.15, in opensuse-leap 16.0

πŸ’… CREATE_LANGUAGE πŸ’…

okay so bestie, welcome to the PostgreSQL 16.15 Documentation glow-up of CREATE LANGUAGE(7) β€” this is the page where we define a whole new procedural language inside a database, and honestly? it's giving main character energy πŸŽ€βœ¨


🌸 NAME 🌸

CREATE_LANGUAGE β€” define a new procedural language


✨ SYNOPSIS ✨

CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE name
    HANDLER call_handler [ INLINE inline_handler ] [ VALIDATOR valfunction ]
CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE name

πŸ’– DESCRIPTION πŸ’–

okay so picture this: CREATE LANGUAGE registers a brand-new procedural language with a PostgreSQL database. after that, you can literally define functions and procedures in this shiny new language. slay. ✨

CREATE LANGUAGE effectively associates the language name with handler function(s) that are responsible for executing functions written in the language. if you want the full lore on language handlers, go read Chapter 58 β€” it's giving required reading, ngl. πŸ“š

CREATE OR REPLACE LANGUAGE will either create a new language, or replace an existing definition. if the language already exists, its parameters get updated according to the command, but β€” and this is important bestie β€” the language's ownership and permissions settings do not change, and any existing functions written in the language are assumed to still be valid. so it's a glow-up, not a whole rebrand. πŸ’…

now, the privilege tea: one must have the PostgreSQL superuser privilege to register a new language or change an existing language's parameters. however, once the language is created it is valid to assign ownership of it to a non-superuser, who may then drop it, change its permissions, rename it, or assign it to a new owner. but 🚨 do not, however, assign ownership of the underlying C functions to a non-superuser β€” that would create a privilege escalation path for that user. like, absolutely not, no cap. πŸ™ˆ

the form of CREATE LANGUAGE that does not supply any handler function is obsolete. for backwards compatibility with old dump files, it is interpreted as CREATE EXTENSION. that will work if the language has been packaged into an extension of the same name, which is the conventional way to set up procedural languages. so it's giving legacy support, and we respect it. 🫢


πŸŽ€ PARAMETERS πŸŽ€

  • TRUSTED β€” TRUSTED specifies that the language does not grant access to data that the user would not otherwise have. if this key word is omitted when registering the language, only users with the PostgreSQL superuser privilege can use this language to create new functions. so like, trust is earned, bestie. πŸ”
  • PROCEDURAL β€” this is a noise word. literally just vibes. it's there, it does nothing, we move. πŸ’€
  • name β€” the name of the new procedural language. the name must be unique among the languages in the database. no duplicates, no copycats, periodt. πŸ‘‘
  • HANDLER call_handler β€” call_handler is the name of a previously registered function that will be called to execute the procedural language's functions. the call handler for a procedural language must be written in a compiled language such as C with version 1 call convention and registered with PostgreSQL as a function taking no arguments and returning the language_handler type, a placeholder type that is simply used to identify the function as a call handler. it's giving "I'm just here to be recognized," and honestly? iconic. ⭐
  • INLINE inline_handler β€” inline_handler is the name of a previously registered function that will be called to execute an anonymous code block (DO command) in this language. if no inline_handler function is specified, the language does not support anonymous code blocks. the handler function must take one argument of type internal, which will be the DO command's internal representation, and it will typically return void. the return value of the handler is ignored. so it does the work and then doesn't even ask for credit β€” humble king behavior. 🧚
  • VALIDATOR valfunction β€” valfunction is the name of a previously registered function that will be called when a new function in the language is created, to validate the new function. if no validator function is specified, then a new function will not be checked when it is created. the validator function must take one argument of type oid, which will be the OID of the to-be-created function, and will typically return void.

a validator function would typically inspect the function body for syntactical correctness, but it can also look at other properties of the function, for example if the language cannot handle certain argument types. to signal an error, the validator function should use the ereport() function. the return value of the function is ignored. so it's like a bouncer at the club checking IDs β€” if something's off, it calls ereport() and shuts it down. πŸšͺ✨


πŸ¦‹ NOTES πŸ¦‹

use DROP LANGUAGE to drop procedural languages. breakups happen, it's fine. πŸ’”

the system catalog pg_language (see Section 53.29) records information about the currently installed languages. also, the psql command \dL lists the installed languages. so you can literally just look at the roster. πŸ“‹

to create functions in a procedural language, a user must have the USAGE privilege for the language. by default, USAGE is granted to PUBLIC (i.e., everyone) for trusted languages. this can be revoked if desired. so it's giving open invite, but you can absolutely change the guest list. 🎟️

procedural languages are local to individual databases. however, a language can be installed into the template1 database, which will cause it to be available automatically in all subsequently-created databases. so you set it up once and it spreads like a good vibe. 🌈


πŸ”₯ EXAMPLES πŸ”₯

a minimal sequence for creating a new procedural language is:

CREATE FUNCTION plsample_call_handler() RETURNS language_handler
    AS '$libdir/plsample'
    LANGUAGE C;
CREATE LANGUAGE plsample
    HANDLER plsample_call_handler;

typically that would be written in an extension's creation script, and users would do this to install the extension:

CREATE EXTENSION plsample;

so like, the easy button is just CREATE EXTENSION plsample; and you're done. no cap. πŸ’…


πŸ‘‘ COMPATIBILITY πŸ‘‘

CREATE LANGUAGE is a PostgreSQL extension. so it's a PostgreSQL original, not a standard SQL thing β€” iykyk. ✨


🌟 SEE ALSO 🌟

ALTER LANGUAGE (ALTER_LANGUAGE(7)), CREATE FUNCTION (CREATE_FUNCTION(7)), DROP LANGUAGE (DROP_LANGUAGE(7)), GRANT(7), REVOKE(7)


bestie if you're still here, you're officially a PostgreSQL language-handler expert. go forth and register languages responsibly β€” and remember, do not assign ownership of the underlying C functions to a non-superuser. that's the one rule that matters most. πŸ«ΆπŸ’–

✨ that's the whole page, written by the PostgreSQL 16.15 girls, dated 2026 β€” stay slaying, stay secure, and always close your color tags ✨

πŸ“¦ where this page comes from