Please tell me how to sort in PostgreSQL (15+) such a table:
id |  name  | next_id
1  | Test01 |  2
2  | Test02 |  3
3  | Test03 |  6
4  | Test04 |  5
5  | Test05 |  null
6  | Test06 |  4
7  | Test07 |  null

It takes an SQL query to order by the next_id column - it's a pointer to the next row. The result I expect is:
id |  name  | next_id
1  | Test01 |  2
2  | Test02 |  3
3  | Test03 |  6
6  | Test06 |  4
4  | Test04 |  5
5  | Test05 |  null
7  | Test07 |  null

It is necessary to read the next_id field for each record, find this record and display it after the existing one. After all, this field can change, so sorting in ascending order will not work and other tricks.
So far, there are no ideas how to do it gracefully. Except for loops and temporary tables.

SQL ORDER BY next
The following demonstrates an approach to sorting as requested:
WITH RECURSIVE t(id, name, next_id) AS (
  VALUES (1  , 'Test01',   2),
         (2  , 'Test02',   3),
         (3  , 'Test03',   6),
         (4  , 'Test04',   5),
         (5  , 'Test05',   NULL),
         (6  , 'Test06',   4),
         (7  , 'Test07',   NULL)),
cte AS (
  SELECT t.id, t.name, t.next_id, t.id AS root_id, 1 AS depth
    FROM t
   WHERE NOT EXISTS (SELECT * FROM t t2 WHERE t.id = t2.next_id)
  UNION ALL
  SELECT t.id, t.name, t.next_id, cte.root_id, cte.depth + 1 AS depth
    FROM t
    JOIN cte ON (t.id = cte.next_id))
SELECT cte.id, cte.name, cte.next_id
  FROM cte
 ORDER BY cte.root_id, cte.depth, cte.id;

This query assumes that there are no cycles in the path defined by next_id.
The query first initializes the recursive CTE by identifying the rows that are not referenced by any next_id, memoizing the root id, and initializing depth. The recursive portion of the CTE finds the rows referenced by these root rows and incrementing depth. The recursive part repeats using the rows discovered by the prior iteration until no rows are found.
The final step sorts by root_id, depth, and id. Including id in the sort is done to create a deterministic order for the case of ids with multiple next_ids (a tree structure instead of linear paths): it can be left out if only linear paths are allowed.
