sandy-bes commented on code in PR #2351:
URL: https://github.com/apache/age/pull/2351#discussion_r3013694013
##########
src/backend/utils/adt/agtype.c:
##########
@@ -5646,19 +5649,74 @@ static Datum get_vertex(const char *graph, const char
*vertex_label,
aclcheck_error(aclresult, OBJECT_TABLE, vertex_label);
}
- /* initialize the scan key */
- ScanKeyInit(&scan_keys[0], 1, BTEqualStrategyNumber, F_OIDEQ,
- Int64GetDatum(graphid));
-
- /* open the relation (table), begin the scan, and get the tuple */
+ /* open the relation (table) */
graph_vertex_label = table_open(vertex_label_table_oid, ShareLock);
- scan_desc = table_beginscan(graph_vertex_label, snapshot, 1, scan_keys);
- tuple = heap_getnext(scan_desc, ForwardScanDirection);
+
+ index_oid = RelationGetPrimaryKeyIndex(graph_vertex_label, false);
+
+ if (!OidIsValid(index_oid))
+ {
+ List *idx_list = RelationGetIndexList(graph_vertex_label);
+ ListCell *lc;
+ foreach(lc, idx_list)
+ {
+ Oid curr = lfirst_oid(lc);
+ Relation idx_rel = index_open(curr, ShareLock);
+
+ if (idx_rel->rd_index->indisvalid &&
+ idx_rel->rd_index->indnatts >= 1 &&
+ idx_rel->rd_index->indkey.values[0] == 1)
+ {
+ index_oid = curr;
+ index_close(idx_rel, ShareLock);
+ break;
+ }
+ index_close(idx_rel, ShareLock);
+ }
+ list_free(idx_list);
+ }
+
+ if (OidIsValid(index_oid))
+ {
+ IndexScanDesc index_scan_desc;
+ Relation index_rel;
+
+ index_rel = index_open(index_oid, ShareLock);
+ slot = table_slot_create(graph_vertex_label, NULL);
+
+ /* initialize the scan key using GRAPHIDEQ for index */
+ ScanKeyInit(&scan_keys[0], 1, BTEqualStrategyNumber,
+ F_GRAPHIDEQ, Int64GetDatum(graphid));
+
+ index_scan_desc = index_beginscan(graph_vertex_label, index_rel,
+ snapshot, NULL, 1, 0);
+ index_rescan(index_scan_desc, scan_keys, 1, NULL, 0);
+
+ if (index_getnext_slot(index_scan_desc, ForwardScanDirection, slot))
+ {
+ tuple = ExecCopySlotHeapTuple(slot);
+ should_free_tuple = true;
+ }
+
+ index_endscan(index_scan_desc);
+ index_close(index_rel, ShareLock);
+ ExecDropSingleTupleTableSlot(slot);
+ }
+ else
+ {
+ /* fallback to sequential scan */
+ ScanKeyInit(&scan_keys[0], 1, BTEqualStrategyNumber, F_OIDEQ,
+ Int64GetDatum(graphid));
Review Comment:
Done! I've refactored this as requested. I also left a note about fixing
this on a similar comment during the second review iteration.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]