Conversation
This commit fixes performance degradation during insertion scenarios by replacing SeqScan with IndexScan. Co-authored-by: Daria Barsukova <d.barsukova@g.nsu.ru> Co-authored-by: Alexandra Bondar <s6311704@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR targets a load-test regression where insertion throughput degraded as label tables grew, by replacing O(N) sequential scans with index-backed lookups (primarily around entity_exists and other hot paths that check entity/label presence).
Changes:
- Switch
entity_exists()to use an index scan when a suitable index is available (fallback to seq scan otherwise). - Refactor several other table lookups (vertex fetch, label enumeration, SET/DELETE code paths) to prefer index scans.
- Add index discovery logic to locate a usable index when a PK index isn’t present.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
src/backend/utils/adt/agtype.c |
Uses index scan to fetch a vertex tuple by graphid, with fallback to sequential scan. |
src/backend/utils/adt/age_global_graph.c |
Uses index scan to enumerate labels for a graph_oid, filtering kind in the heap. |
src/backend/executor/cypher_utils.c |
Reworks entity_exists() to prefer index scans over seq scans. |
src/backend/executor/cypher_set.c |
Uses index scan to find the tuple to update (SET/REMOVE) instead of seq scan. |
src/backend/executor/cypher_delete.c |
Uses index scan to find tuples for DELETE and to accelerate connected-edge checks where possible. |
src/backend/catalog/ag_label.c |
Uses ag_label_graph_oid_index to accelerate edge-label enumeration for a graph. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@sandy-bes Please address the above Copilot suggestions. Additionally, please make sure to note any AI usage, if used. |
|
@sandy-bes Please see this as well (Opus 4.6 review). All of these items need to be address. Review of PR #2351 — Add index scanThe core idea — replacing O(N) sequential scans with O(log N) index scans in Critical Issues1. RLS bypass in The index-scan branch in 2. heap_tuple = ExecFetchSlotHeapTuple(index_slot, true, &shouldFree);
// ...
heap_tuple = update_entity_tuple(resultRelInfo, slot, estate, heap_tuple);
// ...
if (shouldFree)
heap_freetuple(heap_tuple); // <-- frees the UPDATE result, not the fetched tupleAfter Correctness Issues3. The index path correctly uses 4. Index attno vs heap attno confusion in
Performance Issue5. Index discovery runs per-tuple in hot paths In Code Quality Issues6. Massive code duplication The index discovery pattern (iterate Oid find_usable_index_for_attr(Relation rel, AttrNumber attnum);7. DETACH DELETE duplication in The two-pass connected-edge check (START_ID pass + END_ID pass) duplicates ~120 lines of nearly identical ACL check + RLS check + delete + error logic. A helper function would reduce this significantly. 8. C++ style comments New code uses 9. Brace style inconsistency The PR mixes }
else
{Please follow the existing style. 10. Inconsistent lock levels Index open calls use Summary
The performance improvement is real and valuable. With the security fix (#1), memory safety fix (#2), correctness fixes (#3-4), and a refactoring pass to deduplicate the index discovery logic (#5-7), this will be in good shape. |
|
I've updated the PR description with the steps to reproduce the tests locally. |
I was able to validate the perf claims with Opus earlier. But, more importantly, I can't do anything with this PR until all of the above issues (Copilot & Opus) have been addressed. Including noting any use of AI. Btw, using AI is fine, it just needs to be noted. |
|
Yes, absolutely, I understand that the review process needs to be finalized first. I will do my best to address all the feedback over the next few days. Regarding the use of AI agents: I did not use any AI agents while writing this code. |
|
All review comments and suggestions provided by GitHub Copilot and Opus 4.6 have been addressed and integrated into this patch. |
|
@sandy-bes A lot of regression tests failed. Are you running the installcheck locally to verify the build? |
1fdce2c to
61659f2
Compare
|
I forgot to run installcheck for the latest change in the commit, my bad! It turned out there was a lock mismatch. I just fixed it, the tests are passing now, and I force-pushed the update. |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated 6 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@sandy-bes Sorry, you know the drill by now, more Copilot,... I keep hoping it stops complaining :( But, I think we're close. |
|
@jrgemignani No worries at all, I completely understand! As much as it complains, I have to admit Copilot actually gives some really good hints and catches subtle things. |
|
@sandy-bes I think if I keep having Copilot review it, it will never stop finding "something" to complain about. So, I think I'm done with it for this PR. Now I'll review the changes manually and go from there. |
|
@sandy-bes Okay, one small-ish ask. We used to not have this coding rule, so code exists that doesn't follow it. However, we try to enforce that all new code always uses braces {} even for one line of code. If you could make that change in your code, please :) |

Motivation / Problem:
As a result of load testing, a significant performance degradation was found in insertion scenarios. The scenarios used were taken from an open-source benchmark and rewritten in pure SQL. Examples of the queries can be found here:
Perf analysis showed that the main bottleneck is the entity_exists function. The root cause lies in the use of a Sequential Scan (SeqScan) to check for the existence of an entity prior to insertion. The time complexity of a SeqScan is O(N), meaning the search time grows linearly as the number of rows in the table increases. The larger the graph became, the longer each individual insertion took. This led to a drop in TPS regardless of the concurrency level (the issue was consistently reproduced with both 1 and 30 threads).
Profile Analysis (Flame Graphs & perf): I am attaching Flame Graphs for comparison.
entity_existsfunction and its underlying sequential scan operations.Changes Made:
Performance Impact:
Benchmarks were conducted on a server with 30 CPU cores and 32 GB of RAM, using a graph ranging from 20,000 to 200,000 objects over a 2-minute duration. The transition to index access completely eliminated the performance degradation associated with data volume growth:
Here are the scripts and commands for local reproduction:
1. Graph creation and population:
The
sfparameter here defines the scale factor of the generated graph.psql -d your_database -f generate_graph.sql -v sf=1
generate_graph.sql
2. Creating wrapper functions for the workload:
This script sets up the environment and creates functions with the correct parameter mapping for Apache AGE.
psql -d your_database -f setup_func_for_workload.sql
setup_func_for_workload.sql
3. Running the load test:
Running 8 clients for 120 seconds.
pgbench -d your_database -f workload_update.sql -D sf=1 -c 8 -T 120 -P
workload_update.sql
Acknowledgments: