Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the VARIADIC function in PostgreSQL

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to use the VARIADIC function in PostgreSQL. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

1. Example

CREATE OR REPLACE FUNCTION test_v (p1 int, VARIADIC p2 any []) RETURNS void AS$test_v$BEGINNULL;END;$test_v$LANGUAGE plpgsql

Here, the VARIADIC parameter must be an array, and the array

2. Use

Call mode 1

SELECT test_v (1, 2, 3)

Call mode 2

SELECT test_v (1meme VARIADIC'{2pm 3je 4}':: int [])

The first way is actually converted to the second way at the semantic level, and the logic in the function ParseFuncOrColumn can be checked by yourself if you are interested.

3. The syntax of the second invocation mode

| | func_name'('VARIADIC func_arg_expr opt_sort_clause')'{FuncCall * n = makeFuncCall ($1, list_make1 ($4), @ 1); n-> func_variadic = true; n-> agg_order = $5; $= (Node *) n | | | func_name'('func_arg_list', 'VARIADIC func_arg_expr opt_sort_clause')'{FuncCall * n = makeFuncCall ($1, lappend ($3, $6), @ 1); n-> func_variadic = true; n-> agg_order = $7; $= (Node *) n;}

It just specifies that this is a VARIADIC function call, and the parameters are not handled specially.

4. Actual parameter list

At the grammatical level, the first invocation has three constant parameters: integer integer, and the second has two parameters: integer integer array. The first invocation is in the ParseFuncOrColumn function mentioned above, and the latter two are combined into one parameter: array expression (ArrayExpr).

5. Multiple parameter types cannot be mixed.

We can implement func (int, int, int...), but not func (int, text, int, text...). Arrays do not allow this, and errors will be reported at the semantic level and cannot continue.

Of course, this restriction does not affect our use of PG, and it is not impossible to define functions because of it. The main impact is migration, such as Oracle applications will use functions such as decode, under the existing mechanism, there is no perfect solution to implement.

6 、 PL/pgSQL

PL/pgSQL 's support for the VARIADIC parameter doesn't seem to be mentioned in the documentation (perhaps, without looking at it carefully), but it can be handled. And from the above, the VARIADIC parameter ends up passing an array, nothing special.

Examples in regression testing:

-- test variadic functionscreate or replace function vari (variadic int []) returns void as $$begin for i in array_lower ($1jue 1). Array_upper ($1Magne1) loop raise notice'%', $1 [I]; after reading the above, do you have any further understanding of how to use the VARIADIC function in PostgreSQL? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report