Postgres Custom Functions
Postgres custom functions allow you to customize your database schema by defining a set of operations that can include several statements such as declarations, assignments and conditional workflows. Postgres functions are similar to views but allow more procedural computations and can take arguments. Custom SQL functions are also referred to as stored procedures.
For more information on Postgres custom functions, please refer to the Postgres documentation.
We can create the following function that we can call later to search articles based on the input text argument
CREATE FUNCTION search_articles(search text)
RETURNS SETOF article AS $$
title ilike ('%' || search || '%')
OR content ilike ('%' || search || '%')
$$ LANGUAGE sql STABLE;
Let's break this function apart:
- Function name:
- Parameters: there is one parameter where
searchis the name and
textis the type
- Return type:
- Function body: Block from
SELECTuntil the end of the
- Language: The response is returned in the
Postgres custom functions & Hasura
Postgres custom functions can be exposed in Hasura's GraphQL schema as a top-level field or as a computed field for a table. They are typically used for performing custom business logic in the database.