Table rows handling with plpgsql