Exercise 2: How order_total_with_tax() Composes Two Chapters' Own Material — Possible Solution ==================================================================== THE POSTGRES1-8 (PL/PGSQL FUNCTION) COMPONENT ------------------------------ Per this chapter, order_total_with_tax() reuses "postgres1-8's own function-writing pattern" — specifically, the dollar-quoted function body (per postgres1-8's own dollar-quoting material), a DECLARE block defining local variables (subtotal, tax_total, item, cat_tax), and a FOR ... LOOP construct iterating over a query result — all real PL/pgSQL procedural constructs postgres1-8 introduced, applied here to loop through every line item on a given order. THE POSTGRES1-5 (RECURSIVE CTE) COMPONENT ------------------------------ Per this chapter, the function also uses "a nested recursive CTE inside the function." Inside the FOR loop, for each order line, a full WITH RECURSIVE cat_walk AS (...) query runs, walking up the category tree from that line's own product category to find the nearest ancestor category with a tax_rate actually set — the exact same upward-walking recursive pattern this chapter's own breadcrumb query uses (per Exercise 1), just applied here to find a tax rate instead of building a display path. WHY THIS IS A GENUINE COMPOSITION, NOT TWO SEPARATE FEATURES ------------------------------ The function isn't simply "a PL/pgSQL function that happens to also, separately, contain a recursive CTE somewhere in the file." The recursive CTE is genuinely NEEDED inside the function's own loop logic — the function's whole purpose (computing a real order total with tax) can't be achieved with either piece alone. A recursive CTE by itself has no way to loop across every item on an order and accumulate a running subtotal/tax total the way PL/pgSQL's own procedural constructs do. A plain PL/pgSQL function with no recursive query has no way to walk up an arbitrarily deep category tree to find the correct tax rate to apply. The two features are combined precisely because each one solves a piece of the same real problem the other piece can't solve alone — genuine synthesis, exactly as this chapter itself describes it. WHY THIS WORKS AS AN ANSWER ------------------------------ It identifies the specific PL/pgSQL constructs from postgres1-8 and the specific recursive CTE pattern from postgres1-5 present in the function, and explains WHY neither one alone could solve the actual problem the function was written to solve — the definition of a real composition rather than two features merely coexisting in the same file.