PostgreSQL Functions: A Beginner’s Guide - Part One

Need to learn PostgreSQL functions in a pinch? Here’s where to start.

Jon McEwen
4 min readOct 1, 2022
Photo by Michael Dziedzic on Unsplash

What is a PostgreSQL function?

PostgreSQL functions allow you to package SQL statements together and then later invoke those statements by calling the function.

For example, when a new user joins a social media app, the backend database may need to create several related objects, an account, profile, and inbox, and then ultimately return the new unique identifier for the user.

A software engineer can encapsulate this setup within a single function and then call that function each time a new user joins the platform.

Understanding the lay of the land

Before I dive into the anatomy of PostgreSQL functions, here are a few contextual details that will help you get your footing.

Nomenclature

Other SQL databases refer to functions as stored procedures or user-defined functions. In PostgreSQL, they are just called functions.

Functions are functions

Functions can accept arguments, declare variables, and perform loops, just like programming languages like…

--

--

No responses yet