liveBook · Manning

Database function

As explained in PostgreSQL Mistakes and How to Avoid Them

A database function is a named routine stored in the database that accepts inputs and produces a result or side effect.

A database function is a named routine stored in the database. It accepts inputs and returns a result or produces a side effect, so its privileges matter as much as its SQL. section 8.4

The function that could drop production

A development-only function named reset_schema() dropped every table in the current schema. Because it was created by a superuser and executable by an unprivileged developer, the function ran with superuser privileges and destroyed production data. The safer lesson is to restrict both ownership and execution rights rather than treating function visibility as harmless. section 8.4

A function executes within the database’s permission model. If a privileged account creates and owns it, granting ordinary users permission to execute it can expose the creator’s effective privileges during execution. A buggy function can therefore cause damage even without malicious intent. section 8.4

Use database functions with these cautions:

  • Grant only the permissions necessary for the intended task; avoid making regular database ownership and operations depend on superuser accounts. section 8.4
  • Treat side-effecting routines as operationally dangerous, especially when they can drop tables or otherwise alter the database. section 8.4
  • Check function behavior and data types during migrations; similarly named functions or types across DBMSs are not necessarily compatible. section 10.5
  • Remember that ordinary functions such as count() have defined handling for NULL values: count() ignores NULL values in a nullable column. section 2.11
CREATE DATABASE frogge_next OWNER postgres;

Inspect the database ownership setup that makes privileged function execution especially dangerous. section 8.4

How PostgreSQL Mistakes and How to Avoid Them covers it

How deep it goes
Explains execution privileges through a destructive scenario but stops short of function syntax or internal implementation.
Examples
Uses reset_schema(), count(), and data type conversion functions as examples of side effects, NULL handling, and migration compatibility.
What it assumes
Assumes familiarity with databases, permissions, superusers, SQL routines, and data types rather than teaching these concepts from zero.
What you can do after
Operate database functions more safely by reviewing ownership, execution rights, side effects, NULL behavior, and migration compatibility.
How it connects
Connects database functions with superusers, stored procedures, privileges, security checks, and data type conversion.

Also explained in

Compare how 3 books explain database function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of PostgreSQL Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.