recursion in postgresql with working example

in example below you can see using recursion for compute factorial for

WITH RECURSIVE t(n,m)AS(-- this is CTESELECT5AS n,5AS m -- something like n=5UNIONALL-- this is needed for "join" iterationsSELECT n-1,(n-1)*m FROM t WHERE(n-1)>0-- for every iteration do value-- and make multiplication)SELECTMAX(n)ASNUMBER,MAX(m)AS factorial FROM t; -- finally show result