Optimize filter by topic queries #1297
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Restructure topic filter query to order and limit meta rows before blob join and update
message.Service.fetchEnvelopesto return[]queries.GatewayEnvelopesViewvia internal conversionUpdate the topic-based query to select and limit meta rows prior to joining blobs, introduce
queries.SelectGatewayEnvelopesByTopicsRowas the return type forSelectGatewayEnvelopesByTopics, and add amessage.transformRowshelper to convert rows to[]queries.GatewayEnvelopesViewformessage.Service.fetchEnvelopes. Adjust parameter order to$1/$2for cursor arrays,$3forrow_limit, and$4fortopics, and propagate the new row type through validation interfaces, implementations, tests, and mocks.📍Where to Start
Start with the SQL definition for
SelectGatewayEnvelopesByTopicsin envelopes_v2.sql, then review the Go bindings in envelopes_v2.sql.go, followed by themessage.Service.fetchEnvelopeschanges in service.go.📊 Macroscope summarized f6453c3. 5 files reviewed, 6 issues evaluated, 5 issues filtered, 0 comments posted
🗂️ Filtered Issues
pkg/api/message/service.go — 0 comments posted, 3 evaluated, 2 filtered
originatorspath,OriginatorNodeIdsare converted withint32(o). Ifquery.GetOriginatorNodeIds()contains values outside theint32range, this will silently truncate and yield incorrect IDs, causing wrong DB filtering or data leakage. Validate the range before casting or change the parameter type to accommodate larger IDs. [ Out of scope ]rowLimit: the code passesrowLimitdirectly to query params without guarding against negative values. If a negativerowLimitis provided, DB query behavior may be undefined or error-prone (e.g., database rejecting the limit, returning no rows, or causing an internal error). Add validation to ensurerowLimitis non-negative and handle zero appropriately (e.g., treat zero as no results or as a default limit). [ Out of scope ]pkg/db/queries/envelopes_v2.sql.go — 0 comments posted, 2 evaluated, 2 filtered
SelectGatewayEnvelopesByTopics. The new query now expects$1::INT[](node IDs) and$2::BIGINT[](sequence IDs) to build thecursorsCTE,$3::INTfor theLIMIT, and$4::BYTEA[]fortopicfiltering. Previously, the query expected$1::BYTEA[](topics),$2::INT(limit),$3::INT[](node IDs), and$4::BIGINT[](sequence IDs). If the generated Go method signature and its callers were not updated to match this new parameter ordering, runtime execution will fail with PostgreSQL type errors (e.g., attempting tounnest($1::INT[])when$1is actually aBYTEA[]of topics), or worse, produce incorrect results if some casts were accepted. This is a breaking contract change for the query name and is a runtime bug unless all call sites were updated to pass parameters in the new order. [ Low confidence ]ORDER BY ... LIMIT NULLIF($3::INT, 0)within thefilteredCTE, before performing theJOIN gateway_envelope_blobs AS bto fetchoriginator_envelope. If there exist meta rows without a corresponding blob row (e.g., due to transient inconsistency, delayed blob insertion, or partial replication), the inner join will drop those rows after the limit has already been applied. This can yield fewer than the requested limit of returned envelopes, and worse, it can permanently skip eligible later meta rows that do have blobs because they were excluded by the early limit. The previous implementation selected fromgateway_envelopes_view(which likely joins meta and blobs) and appliedLIMITat the final step, ensuring the limit applies to actual joined rows. This change risks under-delivery and starvation under realistic data inconsistencies. [ Low confidence ]pkg/mlsvalidate/service.go — 0 comments posted, 1 evaluated, 1 filtered
GetAssociationStateFromEnvelopesdoes not validatenewUpdatebefore passing it toGetAssociationState. IfnewUpdateisnil(which is reachable given callers likeidentity_update_storer.validateIdentityUpdatethat do not checkidentityUpdate.IdentityUpdatefor nil), the request will contain a slice with anilelement ([]*associations.IdentityUpdate{nil}). This can result in a malformed gRPC/protobuf request at marshal time or a server-side validation error. At minimum, this violates the implicit contract thatNewUpdatescontains valid messages. The function should explicitly checknewUpdate != niland return a clear error before making the RPC. [ Out of scope ]