listmonk/cmd
Kailash Nadh cea65c009d Fix and refactor subscriber batch fetching in campaign processing.
This has been a hair-pulling rabbit hole of an issue. #1931 and others.
When the `next-campaign-subscribers` query that fetches $n subscribers
per batch for a campaign returns no results, the manager assumes
that the campaign is done and marks as finished.

Marathon debugging revealed fundamental flaws in qyery's logic that
would incorrectly return 0 rows under certain conditions.
- Based on the "layout" of subscribers for eg: a series of blocklisted
  subscribers between confirmed subscribers.
  A series of unconfirmed subscribers in a batch belonging to a double
  opt-in list.
- Bulk import blocklisting users, but not marking their subscriptions
  as 'unsubscribed'.
- Conditions spread across multiple CTEs resulted in returning an
  arbitrary number of rows and $N per batch as the selected $N rows
  would get filtered out elsewhere, possibly even becoming 0.

After fixing this and testing it on our prod instance that has
15 million subscribers and ~70 million subscriptions in the
`subscriber_lists` table, ended up discovered significant inefficiences
in Postgres query planning. When `subscriber_lists` and campaign list IDs
are joined dynamically (CTE or ANY() or any kind of JOIN that involves)
a query, the Postgres query planner is unable to use the right indexes.

After testing dozens of approaches, discovered that statically passing
the values to join on (hardcoding or passing via parametrized $1 vars),
the query uses the right indexes. The difference is staggering.
For the particular scenario on our large prod DB to pull a batch,
~15 seconds vs. ~50ms, a whopping 300x improvement!

This patch splits `next-campaign-subscribers` into two separate queries,
one which fetches campaign metadata and list_ids, whose values are then
passed statically to the next query to fetch subscribers by batch.

In addition, it fixes and refactors broken filtering and counting logic
in `create-campaign` and `next-campaign` queries.

Closes #1931, #1993, #1986.
2024-10-13 17:03:59 +05:30
..
admin.go Add warning on admin UI for legacy creds in the TOML file. 2024-10-13 16:59:52 +05:30
archive.go Add 'slug' (permalink) support for campaign archives. Closes #1394. 2024-01-09 23:34:08 +05:30
auth.go User legacy (TOML) admin credentials as API creds for backwards compatibility. 2024-10-13 16:59:52 +05:30
bounce.go chore: remove refs to deprecated io/ioutil (#1593) 2023-11-16 13:57:00 +05:30
campaigns.go Increase campaign subject char limit. Closes #1909. 2024-07-17 19:38:11 +05:30
events.go Refactor the core concurrent campaign manager logic. 2024-01-02 14:57:04 +05:30
handlers.go Add support for "list roles". 2024-10-13 17:03:58 +05:30
i18n.go
import.go Fix typo and formatting (#2028) 2024-08-30 13:24:45 +05:30
init.go Add support for "list roles". 2024-10-13 17:03:58 +05:30
install.go Add support for "list roles". 2024-10-13 17:03:58 +05:30
lists.go Refactor subscriber APIs list permission filtering. 2024-10-13 17:03:59 +05:30
main.go Add public login page and auth middleware and handlers. 2024-10-13 16:59:51 +05:30
maintenance.go Add maintenance options. 2022-09-03 14:58:25 +05:30
manager_store.go Fix and refactor subscriber batch fetching in campaign processing. 2024-10-13 17:03:59 +05:30
media.go Ensure unique upload filenames by adding a suffix (#1963) 2024-08-08 15:42:29 +05:30
notifications.go Make static e-mail template subjects scriptable. Closes #1727. 2024-03-25 15:49:40 +05:30
public.go Fix list auth by adding an explicit 'getAll' flag to query. 2024-10-13 17:03:59 +05:30
roles.go Add support for "list roles". 2024-10-13 17:03:58 +05:30
settings.go Add a settings UI for OIDC. 2024-10-13 16:59:50 +05:30
subscribers.go Refactor subscriber APIs list permission filtering. 2024-10-13 17:03:59 +05:30
templates.go Fix tx template not being cached on update. Closes #2061. 2024-10-05 09:51:56 +05:30
tx.go chore: remove refs to deprecated io/ioutil (#1593) 2023-11-16 13:57:00 +05:30
updates.go Fix update check looping on failed HTTP requests. 2024-10-13 16:59:52 +05:30
upgrade.go Rename migration to v4.0.0 2024-10-13 17:03:59 +05:30
users.go Add support for "list roles". 2024-10-13 17:03:58 +05:30
utils.go Ensure unique upload filenames by adding a suffix (#1963) 2024-08-08 15:42:29 +05:30