Bird
Raised Fist0
PostgreSQLquery~20 mins

VARIADIC parameters in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
VARIADIC Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of a function using VARIADIC integer array
Consider the following PostgreSQL function that sums all integers passed as VARIADIC parameters:

CREATE OR REPLACE FUNCTION sum_variadic(VARIADIC nums integer[]) RETURNS integer AS $$
DECLARE
  total integer := 0;
  n integer;
BEGIN
  FOREACH n IN ARRAY nums LOOP
    total := total + n;
  END LOOP;
  RETURN total;
END;
$$ LANGUAGE plpgsql;

What is the output of the query SELECT sum_variadic(1, 2, 3, 4);?
PostgreSQL
CREATE OR REPLACE FUNCTION sum_variadic(VARIADIC nums integer[]) RETURNS integer AS $$
DECLARE
  total integer := 0;
  n integer;
BEGIN
  FOREACH n IN ARRAY nums LOOP
    total := total + n;
  END LOOP;
  RETURN total;
END;
$$ LANGUAGE plpgsql;

SELECT sum_variadic(1, 2, 3, 4);
A10
BARRAY[1,2,3,4]
C1
DERROR: function sum_variadic(integer, integer, integer, integer) does not exist
Attempts:
2 left
💡 Hint
VARIADIC parameters allow passing multiple arguments as an array inside the function.
📝 Syntax
intermediate
2:00remaining
Identify the correct syntax to declare a VARIADIC parameter
Which of the following is the correct way to declare a VARIADIC parameter in a PostgreSQL function that accepts text arguments?
ACREATE FUNCTION example(args text VARIADIC) RETURNS void AS $$ BEGIN END; $$ LANGUAGE plpgsql;
BCREATE FUNCTION example(args VARIADIC text[]) RETURNS void AS $$ BEGIN END; $$ LANGUAGE plpgsql;
CCREATE FUNCTION example(VARIADIC args text[]) RETURNS void AS $$ BEGIN END; $$ LANGUAGE plpgsql;
DCREATE FUNCTION example(VARIADIC args text) RETURNS void AS $$ BEGIN END; $$ LANGUAGE plpgsql;
Attempts:
2 left
💡 Hint
VARIADIC must appear before the parameter name and type as an array.
optimization
advanced
2:00remaining
Optimizing a function using VARIADIC parameters for concatenation
You have this function that concatenates text arguments passed as VARIADIC parameters:

CREATE OR REPLACE FUNCTION concat_variadic(VARIADIC texts text[]) RETURNS text AS $$
DECLARE
  result text := '';
  t text;
BEGIN
  FOREACH t IN ARRAY texts LOOP
    result := result || t;
  END LOOP;
  RETURN result;
END;
$$ LANGUAGE plpgsql;

Which option provides a more efficient way to concatenate all VARIADIC text arguments?
AConvert texts to JSON and then concatenate the JSON strings.
BUse a nested loop to concatenate each character of each string.
CUse string_agg(texts, '') inside the function.
DUse array_to_string(texts, '') to concatenate all elements at once.
Attempts:
2 left
💡 Hint
PostgreSQL has built-in functions to join array elements efficiently.
🧠 Conceptual
advanced
2:00remaining
Understanding VARIADIC parameter behavior with arrays
Given this function:

CREATE OR REPLACE FUNCTION test_variadic(VARIADIC vals integer[]) RETURNS integer AS $$
BEGIN
  RETURN array_length(vals, 1);
END;
$$ LANGUAGE plpgsql;

What is the result of the query SELECT test_variadic(ARRAY[1,2,3]);?
PostgreSQL
CREATE OR REPLACE FUNCTION test_variadic(VARIADIC vals integer[]) RETURNS integer AS $$
BEGIN
  RETURN array_length(vals, 1);
END;
$$ LANGUAGE plpgsql;

SELECT test_variadic(ARRAY[1,2,3]);
A1
B3
CNULL
DERROR: function test_variadic(integer[]) does not exist
Attempts:
2 left
💡 Hint
Passing an array to a VARIADIC parameter treats the whole array as one argument unless VARIADIC is specified in the call.
🔧 Debug
expert
2:00remaining
Diagnose the error when calling a VARIADIC function
You have this function:

CREATE OR REPLACE FUNCTION multiply_variadic(VARIADIC nums integer[]) RETURNS integer AS $$
DECLARE
  product integer := 1;
  n integer;
BEGIN
  FOREACH n IN ARRAY nums LOOP
    product := product * n;
  END LOOP;
  RETURN product;
END;
$$ LANGUAGE plpgsql;

When you run SELECT multiply_variadic(ARRAY[2,3,4]); you get an error:

ERROR: function multiply_variadic(integer[]) does not exist

What is the cause of this error?
AThe function does not accept any arguments, so passing an array causes an error.
BThe function expects separate integer arguments, but an integer array was passed without VARIADIC keyword.
CThe function expects a single integer, but an array was passed.
DThe function is missing a RETURNS clause causing the error.
Attempts:
2 left
💡 Hint
Check how VARIADIC functions are called with arrays.

Practice

(1/5)
1. What does the VARIADIC keyword do in a PostgreSQL function?
easy
A. Specifies that the function is a trigger
B. Defines a function that returns multiple result sets
C. Allows the function to accept a variable number of arguments as an array
D. Indicates the function is immutable

Solution

  1. Step 1: Understand the role of VARIADIC

    VARIADIC allows a function to accept any number of arguments as a single array parameter.
  2. Step 2: Compare with other options

    Options B, C, and D describe unrelated function features.
  3. Final Answer:

    Allows the function to accept a variable number of arguments as an array -> Option C
  4. Quick Check:

    VARIADIC = variable arguments as array [OK]
Hint: VARIADIC means flexible argument count packed as array [OK]
Common Mistakes:
  • Thinking VARIADIC returns multiple result sets
  • Confusing VARIADIC with triggers
  • Assuming VARIADIC changes function volatility
2. Which of the following is the correct syntax to declare a VARIADIC parameter in a PostgreSQL function?
easy
A. CREATE FUNCTION example(VARIADIC nums int[]) RETURNS int AS $$ ... $$ LANGUAGE sql;
B. CREATE FUNCTION example(nums VARIADIC int) RETURNS int AS $$ ... $$ LANGUAGE sql;
C. CREATE FUNCTION example(nums int VARIADIC) RETURNS int AS $$ ... $$ LANGUAGE sql;
D. CREATE FUNCTION example(nums int[]) VARIADIC RETURNS int AS $$ ... $$ LANGUAGE sql;

Solution

  1. Step 1: Identify correct VARIADIC syntax

    VARIADIC must precede the parameter name and the parameter type must be an array type, e.g., VARIADIC nums int[].
  2. Step 2: Check each option

    CREATE FUNCTION example(VARIADIC nums int[]) RETURNS int AS $$ ... $$ LANGUAGE sql; correctly places VARIADIC before the parameter name and uses an array type. Options A, B, and C misuse the position or syntax of VARIADIC.
  3. Final Answer:

    CREATE FUNCTION example(VARIADIC nums int[]) RETURNS int AS $$ ... $$ LANGUAGE sql; -> Option A
  4. Quick Check:

    VARIADIC before param name + array type = correct syntax [OK]
Hint: VARIADIC goes before param name and uses array type [OK]
Common Mistakes:
  • Placing VARIADIC after parameter name
  • Using non-array type with VARIADIC
  • Putting VARIADIC after RETURNS keyword
3. Given the function:
CREATE FUNCTION sum_all(VARIADIC nums int[]) RETURNS int AS $$
  SELECT SUM(n) FROM unnest(nums) AS n;
$$ LANGUAGE sql;

What is the result of SELECT sum_all(2, 4, 6);?
medium
A. ARRAY[2,4,6]
B. 12
C. Error: wrong number of arguments
D. NULL

Solution

  1. Step 1: Understand VARIADIC input

    The function accepts any number of integers as separate arguments, packed into an array named nums.
  2. Step 2: Calculate the sum

    Input arguments 2, 4, 6 become array [2,4,6]. The function sums these values: 2 + 4 + 6 = 12.
  3. Final Answer:

    12 -> Option B
  4. Quick Check:

    Sum of 2,4,6 = 12 [OK]
Hint: VARIADIC packs inputs as array; sum them inside function [OK]
Common Mistakes:
  • Expecting an array output instead of sum
  • Thinking VARIADIC requires explicit array input
  • Assuming function errors on multiple arguments
4. Consider this function definition:
CREATE FUNCTION concat_texts(VARIADIC texts text) RETURNS text AS $$
  SELECT string_agg(t, ',') FROM unnest(texts) AS t;
$$ LANGUAGE sql;

Which error will occur if you call SELECT concat_texts('hello', 'world');?
medium
A. Error: VARIADIC parameter must be declared as an array type
B. No error, returns 'hello,world'
C. Error: missing FROM-clause entry for table 'texts'
D. Error: function does not exist

Solution

  1. Step 1: Check VARIADIC parameter type

    The parameter texts is declared as text, not text[]. VARIADIC requires an array type.
  2. Step 2: Identify error cause

    PostgreSQL will raise a syntax error because VARIADIC must be followed by an array type, e.g., VARIADIC texts text[].
  3. Final Answer:

    Error: VARIADIC parameter must be declared as an array type -> Option A
  4. Quick Check:

    VARIADIC needs array type parameter [OK]
Hint: VARIADIC param must be array type, else syntax error [OK]
Common Mistakes:
  • Declaring VARIADIC param as scalar type
  • Expecting no error with wrong type
  • Confusing error with missing FROM clause
5. You want to create a function that accepts a fixed integer and then any number of text arguments variadically. Which of these function declarations is correct?
hard
A. CREATE FUNCTION example(fixed int, VARIADIC texts text) RETURNS text AS $$ ... $$ LANGUAGE sql;
B. CREATE FUNCTION example(VARIADIC texts text[], fixed int) RETURNS text AS $$ ... $$ LANGUAGE sql;
C. CREATE FUNCTION example(fixed int, texts VARIADIC text[]) RETURNS text AS $$ ... $$ LANGUAGE sql;
D. CREATE FUNCTION example(fixed int, VARIADIC texts text[]) RETURNS text AS $$ ... $$ LANGUAGE sql;

Solution

  1. Step 1: Recall VARIADIC position rule

    VARIADIC parameter must be the last parameter in the function declaration.
  2. Step 2: Check parameter types and order

    CREATE FUNCTION example(fixed int, VARIADIC texts text[]) RETURNS text AS $$ ... $$ LANGUAGE sql; places fixed int first and VARIADIC texts text[] last, which is correct. CREATE FUNCTION example(VARIADIC texts text[], fixed int) RETURNS text AS $$ ... $$ LANGUAGE sql; places VARIADIC first, which is invalid. CREATE FUNCTION example(fixed int, texts VARIADIC text[]) RETURNS text AS $$ ... $$ LANGUAGE sql; misplaces VARIADIC keyword. CREATE FUNCTION example(fixed int, VARIADIC texts text) RETURNS text AS $$ ... $$ LANGUAGE sql; uses non-array type with VARIADIC.
  3. Final Answer:

    CREATE FUNCTION example(fixed int, VARIADIC texts text[]) RETURNS text AS $$ ... $$ LANGUAGE sql; -> Option D
  4. Quick Check:

    VARIADIC last and array type = correct [OK]
Hint: VARIADIC param must be last and array type [OK]
Common Mistakes:
  • Placing VARIADIC parameter before fixed parameters
  • Using scalar type with VARIADIC
  • Misplacing VARIADIC keyword after parameter name