Skip to content

create views with UDFs #20019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
- A `RECORD`-returning UDF cannot be created without a `RETURN` statement in the root block, which would restrict the wildcard type to a concrete one. [#122945](https://github.com/cockroachdb/cockroach/issues/122945)
- User-defined functions are not currently supported in:
- Expressions (column, index, constraint) in tables. [#87699](https://github.com/cockroachdb/cockroach/issues/87699)
- Views. [#87699](https://github.com/cockroachdb/cockroach/issues/87699)
- User-defined functions cannot call themselves recursively. [#93049](https://github.com/cockroachdb/cockroach/issues/93049)
- The `setval` function cannot be resolved when used inside UDF bodies. [#110860](https://github.com/cockroachdb/cockroach/issues/110860)
- Casting subqueries to [user-defined types]({% link {{ page.version.version }}/create-type.md %}) in UDFs is not supported. [#108184](https://github.com/cockroachdb/cockroach/issues/108184)
67 changes: 67 additions & 0 deletions src/current/v25.3/create-view.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,73 @@ Executing the query is as easy as `SELECT`ing from the view, as you would from a
(3 rows)
~~~

### Create a view that references routines

{% include_cached new-in.html version="v25.3" %} Views can call both scalar and set-returning [user-defined functions (UDFs)]({% link {{ page.version.version }}/user-defined-functions.md %}) in their `SELECT` statements.

The following example builds a view over a table and two UDFs.

Create and populate a table:

{% include_cached copy-clipboard.html %}
~~~ sql
CREATE TABLE xy (x INT, y INT);
INSERT INTO xy VALUES (1, 2), (3, 4), (5, 6);
~~~

Define a scalar and a set-returning UDF:

{% include_cached copy-clipboard.html %}
~~~ sql
CREATE FUNCTION f_scalar() RETURNS INT LANGUAGE SQL AS $$
SELECT count(*) FROM xy;
$$;
~~~

{% include_cached copy-clipboard.html %}
~~~ sql
CREATE FUNCTION f_setof() RETURNS SETOF xy LANGUAGE SQL AS $$
SELECT * FROM xy;
$$;
~~~

Create a view that references both functions:

{% include_cached copy-clipboard.html %}
~~~ sql
CREATE VIEW v_xy AS
SELECT x, y, f_scalar() AS total_rows
FROM f_setof();
~~~

Query the view:

{% include_cached copy-clipboard.html %}
~~~ sql
SELECT * FROM v_xy ORDER BY x;
~~~

~~~
x | y | total_rows
----+---+-------------
1 | 2 | 3
3 | 4 | 3
5 | 6 | 3
(3 rows)
~~~

Because the view depends on `f_scalar` and `f_setof`, attempting to rename either function returns an error:

{% include_cached copy-clipboard.html %}
~~~ sql
ALTER FUNCTION f_scalar RENAME TO f_scalar_renamed;
~~~

~~~
ERROR: cannot rename function "f_scalar" because other functions or views ([movr.public.v_xy]) still depend on it
SQLSTATE: 0A000
~~~

## See also

- [Selection Queries]({% link {{ page.version.version }}/selection-queries.md %})
Expand Down
6 changes: 6 additions & 0 deletions src/current/v25.3/user-defined-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ SELECT add(3,5) as sum;
(1 row)
~~~

### Call a UDF from a view

You can include UDF calls in a [view]({% link {{ page.version.version }}/views.md %}) definition. The function is invoked each time the view is queried. A [materialized view]({% link {{ page.version.version }}/views.md %}#materialized-views) will invoke the function only when the view is created or refreshed.

Refer to [Create a view that references routines]({% link {{ page.version.version }}/create-view.md %}#create-a-view-that-references-routines).

### Create a UDF using PL/pgSQL

{% include {{ page.version.version }}/sql/udf-plpgsql-example.md %}
Expand Down
Loading