DEFERRABLE
A deferrable constraint is checked at COMMIT instead of after each statement, so rows may arrive in any order inside the transaction.
task
Make child.pid a foreign key that is checked at COMMIT, then insert the child row before its parent in one transaction.