factorial()

A function returning the factorial of the supplied integer

factorial() is a system function returning the factorial of the supplied integer.

factorial() was added in PostgreSQL 7.0.

Usage

factorial ( bigint ) → numeric

Change history

Examples

Usage example for factorial():

postgres=# SELECT x, factorial(x) FROM generate_series(0,10) AS x;
 x  | factorial 
----+-----------
  0 |         1
  1 |         1
  2 |         2
  3 |         6
  4 |        24
  5 |       120
  6 |       720
  7 |      5040
  8 |     40320
  9 |    362880
 10 |   3628800
(11 rows)

Attempting to obtain the factorial of a negative number (PostgreSQL 14 and later):

postgres=# SELECT factorial(-1);
ERROR:  factorial of a negative number is undefined

Note that in PostgreSQL 13 and earlier, the function will erroneously return 1:

postgres=# SELECT factorial(-1);
 factorial 
-----------
         1
(1 row)

Categories

System function