VOOZH about

URL: https://thenewstack.io/let-sql-do-all-the-heavy-lifting-user-defined-functions/

⇱ Let SQL Do All the Work: User Defined Functions - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2023-01-31 03:00:21
Let SQL Do All the Work: User Defined Functions
Data / Software Development

Let SQL Do All the Work: User Defined Functions

SQL user-defined functions optimize application performance by moving operations from the application layer to the database layer.
Jan 31st, 2023 3:00am by Jessica Wachtel
👁 Featued image for: Let SQL Do All the Work: User Defined Functions

SQL user-defined functions are one way to optimize application performance, namely by moving operations from the application layer to the database layer.

There are numerous ways to optimize the application layer. For applications with SQL database layers, in some cases, it’s possible to just do less. When working with SQL databases, user-defined functions (UDF) is the “do less” option. The work still gets done, but the database layer is leaned on heavier, which has the end result of optimizing application performance, noted Charlie Custer, Cockroach Labs‘ senior technical content marketer, in a great introductory blog post about UDFs.

SQL UDFs are functions created to execute in a SQL database that can limit the amount of work going on between layers and optimize the application layer. UDFs can mutate data intentionally and unintentionally.

What Are SQL UDFs?

UDFs are functions. And they’re very similar to functions in all other programming languages, except they are executed by your SQL database software. Their characteristics include:

  • a name, needed for the function invocation.
  • arguments, inputs and their types accepted by the function.
  • an output/return statement.
  • a function body that includes the operations performed on the arguments to reach the outcome.

A SQL UDF create function in some databases looks like this:

👁 Image

  • CREATE FUNCTION add() lets the database software know a function named add is being created.
  • a INT, b INT lets the database software know to expect two arguments, both integers when running this function.
  • RETURNS INT lets the database software know that an integer is getting returned.
  • AS ‘SELECT a + b’ is the operation running in the function body.

The invocation looks like this:

👁 Image

Eight is the target output.

Here’s where the optimization kicks in. UDFs remove business logic from the application layer to the database which can optimize the application layer, “do less”, but still perform necessary operations. Reducing the amount and frequency of data traveling between application layers is also key for enhancing performance.

Consider that SQL UDF add() is invoked in the database with two values that exist in the database (a = 3, b = 5) making only one value returned to the application layer, 8. Without the UDF, two values would get returned and then the operation would take place in the application layer. More steps, less efficient.

But how useful is a function that adds two numbers and returns one? Not very. The function below is a UDF created by CockroachDB user Saquib Ali that takes timestamp data and converts into a more human-friendly, “x hours ago” format.

Side Effects

Similarly to other functions, UDFs can mutate data in SQL tables in unintended ways. Because of this, some databases offer space to provide information about it’s volatility, the extent to which the function will impact other data.

Example:

👁 Image

IMMUTABLE refers to the function not mutating any other data and LEAKPROOF means no side effects.

How to Create UDFs in SQL

Different SQL systems have different syntax but Custer provided some sample UDFs for various database systems:

PostgreSQL:

👁 Image

MySQL:

👁 Image

CockroachDB:

👁 Image

CockroachDB looks similar to PostgreSQL because CockroachDB is compatible with the Postgres wire protocol.

Conclusion

This is but a scratch on the topic of SQL UDFs. They aren’t only used to optimize the application layer but the database layer as well. Here are some other great articles on UDFs although the complexity jumps off pretty steeply from here. Google and Microsoft also have some posts (Google BigQuery, Microsoft UDF) that include some specs geared towards their own database systems as well.

TRENDING STORIES
Jessica Wachtel is a developer marketing writer at InfluxData where she creates content that helps make the world of time series data more understandable and accessible. Jessica has a background in software development and technical journalism.
Read more from Jessica Wachtel
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.