Conditional where for postgres stored procedure
You can use Boolean expressions, that return true, if a parameter is null or empty, otherwise the result of a LIKE operation.

...
SELECT DISTINCT
       t.serialnumber as sn,
       t.productid as tid,
       t.merchantid as mid
       FROM tbterminal as t
       WHERE (coalesce(p_mid, '') = ''
               OR t.merchantid LIKE '%' || p_mid || '%')
             AND (coalesce(p_tid, '') = ''
                   OR t.productid LIKE '%' || p_tid || '%')
             AND (coalesce(p_sn, '') = ''
                   OR t.serialnumber LIKE '%' || p_sn || '%')
       ORDER BY idterminal ASC
       LIMIT p_limit
       OFFSET p_offset;
...