In this article we formulate and solve the optimal design problem of a defined contribution public pension fund, in a highly stylized but still rather general non-stationary framework. We adopt the viewpoint of a benevolent social planner who aims at treating in a fair manner the successive overlapping generations participating to such a long-term mandatory system. Using the approach of El Karoui and Jeanblanc (1998) for the optimal consumption and portfolio choice problem with random income in a complete market, we exhibit a solution to our intertemporal stochastic control problem where each generation receives a fair (lumpsum) retirement benefit: it is proportional to the contributions she has paid during her active worklife and follows a fixed common rule (although her pension value itself may depend on variables only observable at her retirement time). We next relax the assumption that the collective pension system is mandatory and investigate the performance of individual investment plans in the market. Comparing the outcomes of both alternatives, we derive a condition under which the collective fund can be expected to overperform the individual plan. In the special case of a stationary economy, such a possibility has been pointed out by Gollier (2008). In fact this effect results from the possibility for the collective fund to borrow today against contributions of future generations, which allows to implement riskier strategies and may improve its performance.