You're on the right track with needing a CTE. Something like:
WITH tree AS
(SELECT g.group_id AS root,
g.group_id AS parent,
p.person_id AS person
FROM "group" AS g
LEFT JOIN person AS p ON g.group_id = p.parent_group
WHERE g.group_id = @desired_group
UNION ALL
SELECT t.root, g.group_id, p.person_id
FROM tree AS t
JOIN "group" AS g ON t.parent = g.parent_group_id
LEFT JOIN person AS p on g.group_id = p.parent_group)
SELECT count(DISTINCT person)
FROM tree;
Start by selecting the desired group and its members, and then recursively select all members of groups with the given parent group. Finally, count all the unique users that were found.
db<>fiddle example.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…