FreshPorts October speed-up

Earlier this month a botnet found its way through the FreshPorts search page and kept the production database at 100% CPU. Over the following days I limited search to logged-in users, made a series of database changes, and on 17 October deployed a release with several search fixes. This post looks at what all that did to page speed, as measured by FreshPorts itself.

For more information on what changes were made:
  1. Search improvements
  2. Precomputing “People watching this port, also watch”

Short version: search is about 12× faster. Port pages and the front page got faster at the slow end. The ports-* report pages didn’t change at all, and they are now the slowest pages on the site.

Where the numbers come from

Every page FreshPorts renders writes a row to page_load_detail: the script name, the URL, whether the visitor was logged in, and the rendering time. The clock starts when PHP starts and stops at the page footer. The figure therefore includes everything: database queries, PHP and building the HTML. That makes it a different view from pg_stat_statements, which only sees time inside the database.

Two limitations matter here:

  • Only fully rendered pages are recorded. Requests that are refused (login required, a challenge from the search gate, nginx limits) never reach the footer and never get a row. Neither do single-result searches, which redirect straight to the port page.
  • A nightly job rolls each day up into page_load_summary (count, min, max and average per script), then deletes detail older than seven days. The detail behind this post therefore covers 9–17 October, and anything older comes from the daily summary.

All times below are UTC. I’ve used medians and 95th percentiles rather than averages. A few very slow pages pull an average a long way up: for port pages the median is 0.025 s while the average is about 0.08 s.

Search, hour by hour

Line chart of average and 95th-percentile search rendering time per hour, 15 to 17 October 2026, on a log scale. Times are erratic and reach about 54 seconds during the bot period on 15 October, settle at about 2 seconds once search is limited to logged-in users, and drop to about 0.15 seconds after the database changes and release on the morning of 17 October.
Average and 95th-percentile rendering time per hour for every search with a query, on a log scale. Gaps are hours with no rendered searches.

The chart has three distinct phases:

  • Bot period, to about 22:00 on 15 October. Times are all over the place. The worst hour, 21:00 on the 15th, had 385 searches, 382 of them anonymous, with an average of 21 s and a 95th percentile of 54 s.
  • Logged-in users only, until the morning of 17 October. Anonymous searches drop to zero and the noise goes away, but search still takes about 2 s an hour, every hour. The bots made search worse; they weren’t the reason it was slow.
  • After the database changes and the release. From the 09:00 hour on 17 October, search takes about 0.15 s. There are no searches at all in the 08:00 hour, which is when the deploy happened.

The daily summary confirms that the ~2 s was normal. Average search time was mostly 1.3–1.7 s every day from August through mid-October.

Before and after

Here “before” is 9 October up to the 07:57 release on 17 October, and “after” is everything since. So far, only name searches have enough rows after the release for a firm comparison. The other rows show the direction of change, not a measurement to quote.

SearchSearches before / afterMedian beforeMedian after95th pct before95th pct after
name, match (logged in)1,610 / 1661.52 s0.12 s3.33 s0.27 s
name, match (anonymous)141 / 872.95 s0.09 s7.31 s0.42 s
maintainer, exact (logged in)10 / 40.42 s0.04 s1.21 s0.06 s
long description, match (logged in)18 / 42.54 s0.61 s3.97 s2.09 s
Search rendering time before and after 2026-10-17 07:57 UTC.

The bots

Stacked bar chart of rendered searches per day from 9 to 17 October 2026. Days are 200 to 330 searches, all by logged-in users, until 14 October (510, about half anonymous) and 15 October (1,128, mostly anonymous). 16 October has 179 and 17 October 326, with a small anonymous share.
Rendered searches per day, logged-in and anonymous.

Anonymous searches appear only on 14 and 15 October, plus a few on the 17th after the new search gate went live. Earlier bot traffic was turned away before any page was rendered, so it isn’t in these numbers, even though it still cost the database.

The searches that did get through have a clear pattern. Each IP made about one search, there were nearly as many networks as IPs, and the bots chose expensive search types:

Anonymous search typeSearchesIPsMedian95th pctMax
depends on this port (all)3703133.9 s51.2 s66.3 s
maintainer, exact3651192.5 s53.0 s60.4 s
committer, exact3183000.2 s4.3 s22.5 s
name, match141812.9 s7.3 s53.9 s
Anonymous searches, 14–15 October.

Logged-in users paid for that too. On 15 October the median logged-in name search took 2.3 s and the 95th percentile 8.4 s, up from about 1.4 s and 2.7 s earlier in the week.

The rest of the site

Category and port pages both arrive at the same PHP script, the URL parser behind nginx’s try_files, so I split them by URL depth.

PageViews beforeMedian beforeMedian after95th pct before95th pct after
Port page1,441,2650.026 s0.025 s0.49 s0.09 s
Front page58,3860.011 s0.013 s0.48 s0.05 s
Category page34,4070.027 s0.043 s4.40 s1.94 s
ports-new.php1,6941.60 s2.21 s18.0 s18.7 s
ports-broken.php6224.90 s3.77 s39.9 s37.5 s
Rendering time for other pages, before and after 2026-10-17 07:57 UTC.

Port pages were already fast in the typical case; the gain is at the slow end. The “before” column includes the bot days, but even on the four quiet days before them the port-page 95th percentile was about 0.3 s, so the drop to 0.09 s is real.

Category pages still have a long tail: the biggest categories take tens of seconds. The report pages didn’t move. Over the last eight days, these were the slowest pages on the site:

  • ports-deleted.php: 25.6 s average
  • ports-ignore.php: 13.6 s
  • ports-expiration-date.php: 12.9 s
  • ports-broken.php: 11.6 s
  • ports-deprecated.php: 11.5 s
  • ports-forbidden.php: 10.7 s
  • ports-new.php: 4.8 s

Two of them are getting worse. ports-broken.php averaged 4–10 s a day in August and 6–26 s in October. ports-new.php went from 1.4–4.9 s to 3.2–9.2 s. They’re next on my list.

What I can’t tell you yet

Three changes landed within a few minutes on 17 October: an ANALYZE at 07:53, jit = off, and the release at 07:57, which among other things puts the branch id into the search SQL as a literal. pg_stat_statements was reset at 07:57 as well, so its history doesn’t separate them.

What I can say is that the changes from 16 October, including a new index on port_dependencies, didn’t speed up search: it stayed at about 2 s all that day. Even simple searches carried a fixed cost of about 1.3 s, which looks a lot like JIT compilation, but I haven’t proven it. Running EXPLAIN (ANALYZE) with JIT turned back on in a single session would settle it.

The “after” figures for anything other than name searches are thin. I’ll re-run the numbers in a few days, once there’s more traffic through the new search gate.

A side discovery: 31 GB of old page names

page_load_summary should hold about 50 rows a day. Instead it holds 165 million rows and takes 31 GB. Only 126,732 of those rows date from 2018 or later.

The rest go back to the years when port pages were served through a 404 handler, /missing.php. The roll-up function stored each of those URLs as its own page name, every day. The result is a per-port view history from 2003 to 2017. That’s interesting in its own right, but it doesn’t belong in a live table. I’ll probably export it to a file and trim the table.

The detail table is small, about 2 million rows. But 200,000 inserts and 200,000 deletes a day have left its indexes four times the size of the data. A re-index, and probably daily partitioning, will fix that. Partitioning would also let me keep detail longer than seven days. That matters: the daily summary has no percentiles and can’t tell a port page from a category page, so it can’t support a before/after comparison like this one.

How I measured it

Everything above came from a handful of queries like this one, which gives search counts and timings per day and search type:

SELECT date,
       coalesce(substring(full_url FROM '[?&]stype=([^&#]*)'), 'name') AS stype,
       count(*)                   AS searches,
       count(DISTINCT ip_address) AS ips,
       round(percentile_cont(0.5)  WITHIN GROUP (ORDER BY extract(epoch FROM rendering_time))::numeric, 2) AS median_s,
       round(percentile_cont(0.95) WITHIN GROUP (ORDER BY extract(epoch FROM rendering_time))::numeric, 2) AS p95_s
  FROM page_load_detail
 WHERE page_name = '/search.php'
   AND full_url ~ '[?&]query=[^&]'
   AND date >= '2026-10-09'
 GROUP BY 1, 2
 ORDER BY 1, 2;

Filtering on date keeps each query on the index, so they cost a second or two on production. I’ve kept the set so I can re-run it after future releases.

Update, 18 October: what the page timings couldn’t see

page_load_detail only records pages that finish rendering, so I went to the nginx access log for the requests that didn’t. Since 10:17 on 17 October, nginx sends a search that has a query but no login cookie or search_ok cookie straight to the challenge page. PHP never runs search.php for it, and PostgreSQL never sees it.

PeriodRefused searches per hourSearches run per hour
17 Oct, 00:00–09:00 (login required)924–6,026 (401, after PHP and a database connection)0–19 (logged-in users)
17 Oct, from 10:00 (gate on)2,977 at 11:00, falling to about 800 (403, from nginx)15–68
18 Oct430–870 (403, from nginx)1–41
Searches with a query, from the nginx access log on www.freshports.org.

The bots are still trying. In about 25 hours, nginx refused 28,452 searches from 14,743 IP addresses, about two each. The busiest address managed 132. Before the gate, each refusal still started PHP and opened a database connection. Now each one costs a small page from search-gate.php.

The bots and the people look different. Most refused searches claim to be Windows Chrome 142–153, several versions behind current. The visitors who pressed “Continue to search” ran current Firefox, Chrome, Edge and Safari, with a lot of Linux Firefox. No user agent appears in both groups. 94 visitors from 93 addresses passed the challenge.

More people are searching. Searches that rendered went from 0–19 an hour (logged-in users only) to 15–68 an hour. pg_stat_statements counted 504 searches in the 28 hours after the release.

The database is quieter. Across the whole database, query time is 401 s per hour, against 471 before the release. Search is now a few seconds of that.

Two cautions for anyone measuring the same way:

  • pg_stat_statements stores query text with its original line breaks. A pattern like 'SELECT count(*) FROM ports P , categories C, element E%' silently matches nothing. Normalise first, with regexp_replace(query, '\s+', ' ', 'g').
  • search.php puts the search term into the SQL, so every distinct term is its own pg_stat_statements row. An average of the rows’ means isn’t weighted by calls; use sum(total_exec_time) / sum(calls).
SELECT sum(calls) AS searches,
       round((sum(total_exec_time) / sum(calls))::numeric, 1) AS mean_ms
  FROM pg_stat_statements
 WHERE dbid = (SELECT oid FROM pg_database WHERE datname = current_database())
   AND regexp_replace(query, '\s+', ' ', 'g')
       ILIKE '%SELECT count(*) FROM ports P , categories C, element E%';

One more kind of refusal doesn’t show up in the page timings. When the server’s load is high, checkLoadBeforeProceeding() turns away visitors who aren’t logged in, with a 503. People who pass the challenge aren’t logged in, so a few searches an hour now end that way, too few to matter.

Update, 21 October 2026: production results

After the follow-up fixes to the list pages, port pages, and sanity test failures, I compared two overnight windows on production of about the same hours: 14 hours before the release, and 16¾ hours starting the evening the last fix went out.. Each one includes the nightly backup. Database time fell from about 471 seconds per hour to about 188, a 60% reduction, with slightly more traffic than before.

QueryBefore (mean)After (mean)Before (s/hour)After (s/hour)
Category page listing1,479 ms129 ms1119.8
List pages (broken, deprecated, …): port count2,000–3,300 ms58 ms660.8
New ports (ports-new.php)3,642 ms16 ms250.07
Search: result count1,676 ms20 ms14.50.5
Port page: fetch the port6 ms3.9 ms5326
“Watchers also watched”1.2 ms0.07 ms10.30.7
All database activity471188

The list pages are now cached as well. They no longer run a separate query to find when the list last changed. Search counts are about 80 times faster, even though anonymous searches are allowed again and searches now run about three times as often.

The sanity test failures page had been showing every failure since 2006 on one page: 1,434 commits and 4.2 MB of HTML. Behind it, SanityTestFailures() built all 27,771 rows on every call, even to show the three rows of a single commit. The page is now paged, 100 commits at a time, and each page is cached. The function is now plain SQL, which lets PostgreSQL fetch only the commits on the page. On production, the page for one commit went from 638 ms to 1.4 ms, and a list page from 822 ms to about 65 ms. Together they used about 14.7 seconds of database time an hour, and now use about 0.2.

Update, 22 October 2026: the branch front page

The front page for a branch (/?branch=quarterly) was the costliest query left on production: about 4 seconds a call, 13 seconds of database time an hour. Its query was meant to fetch the 100 latest commits on the branch, but it counted (commit, port) pairs instead of commits. One commit touching hundreds of ports filled the limit, so the quarterly page showed 42 commits, not 100. Each pair was then joined back to every port in its commit, about 70,000 rows, before being cut down again. It now picks 100 commits, as the main front page always did. Release 2.16.10 went to www.freshports.org on 22 October. The query went from 4,011 ms to under 100 ms, and the page shows all 100 commits.

Website Pin Facebook Twitter Myspace Friendfeed Technorati del.icio.us Digg Google StumbleUpon Premium Responsive

Leave a Comment

Scroll to Top