Skip to content

Commit 0ffedcd

Browse files
Alexandra Bondardaria-barsukovasandy-bes
committed
Add index scan
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>
1 parent 217467a commit 0ffedcd

6 files changed

Lines changed: 784 additions & 225 deletions

File tree

src/backend/catalog/ag_label.c

Lines changed: 84 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "access/genam.h"
2323
#include "catalog/indexing.h"
24+
#include "catalog/namespace.h"
2425
#include "executor/executor.h"
2526
#include "nodes/makefuncs.h"
2627
#include "utils/builtins.h"
@@ -297,46 +298,104 @@ List *get_all_edge_labels_per_graph(EState *estate, Oid graph_oid)
297298
HeapTuple tuple;
298299
TupleTableSlot *slot;
299300
ResultRelInfo *resultRelInfo;
301+
Oid index_oid;
300302

301-
/* setup scan keys to get all edges for the given graph oid */
302-
ScanKeyInit(&scan_keys[1], Anum_ag_label_graph, BTEqualStrategyNumber,
303-
F_OIDEQ, ObjectIdGetDatum(graph_oid));
304-
ScanKeyInit(&scan_keys[0], Anum_ag_label_kind, BTEqualStrategyNumber,
305-
F_CHAREQ, CharGetDatum(LABEL_TYPE_EDGE));
303+
index_oid = get_relname_relid("ag_label_graph_oid_index",
304+
get_namespace_oid("ag_catalog", false));
306305

307306
/* setup the table to be scanned */
308307
ag_label = table_open(ag_label_relation_id(), RowExclusiveLock);
309-
scan_desc = table_beginscan(ag_label, estate->es_snapshot, 2, scan_keys);
310308

311309
resultRelInfo = create_entity_result_rel_info(estate, "ag_catalog",
312310
"ag_label");
313311

314-
slot = ExecInitExtraTupleSlot(
315-
estate, RelationGetDescr(resultRelInfo->ri_RelationDesc),
316-
&TTSOpsHeapTuple);
317-
318-
/* scan through the results and get all the label names. */
319-
while(true)
312+
if (OidIsValid(index_oid))
313+
{
314+
Relation index_rel;
315+
IndexScanDesc index_scan_desc;
316+
317+
slot = ExecInitExtraTupleSlot(
318+
estate, RelationGetDescr(resultRelInfo->ri_RelationDesc),
319+
&TTSOpsBufferHeapTuple);
320+
321+
index_rel = index_open(index_oid, RowExclusiveLock);
322+
323+
ScanKeyInit(&scan_keys[0], Anum_ag_label_name, BTEqualStrategyNumber,
324+
F_OIDEQ, ObjectIdGetDatum(graph_oid));
325+
326+
index_scan_desc = index_beginscan(ag_label, index_rel, estate->es_snapshot, NULL, 1, 0);
327+
index_rescan(index_scan_desc, scan_keys, 1, NULL, 0);
328+
329+
while (index_getnext_slot(index_scan_desc, ForwardScanDirection, slot))
330+
{
331+
Name label;
332+
Name lval;
333+
bool isNull;
334+
Datum datum;
335+
char kind;
336+
337+
/*There isn't field kind in index. So we should check it by hands*/
338+
datum = slot_getattr(slot, Anum_ag_label_kind, &isNull);
339+
if (isNull)
340+
continue;
341+
342+
kind = DatumGetChar(datum);
343+
344+
if (kind != LABEL_TYPE_EDGE)
345+
continue;
346+
347+
datum = slot_getattr(slot, Anum_ag_label_name, &isNull);
348+
if (!isNull)
349+
{
350+
label = DatumGetName(datum);
351+
lval = (Name) palloc(NAMEDATALEN);
352+
namestrcpy(lval, NameStr(*label));
353+
labels = lappend(labels, lval);
354+
}
355+
}
356+
357+
index_endscan(index_scan_desc);
358+
index_close(index_rel, RowExclusiveLock);
359+
} else
320360
{
321-
Name label;
322-
bool isNull;
323-
Datum datum;
361+
slot = ExecInitExtraTupleSlot(
362+
estate, RelationGetDescr(resultRelInfo->ri_RelationDesc),
363+
&TTSOpsHeapTuple);
324364

325-
tuple = heap_getnext(scan_desc, ForwardScanDirection);
365+
// setup scan keys to get all edges for the given graph oid
366+
ScanKeyInit(&scan_keys[1], Anum_ag_label_graph, BTEqualStrategyNumber,
367+
F_OIDEQ, ObjectIdGetDatum(graph_oid));
368+
ScanKeyInit(&scan_keys[0], Anum_ag_label_kind, BTEqualStrategyNumber,
369+
F_CHAREQ, CharGetDatum(LABEL_TYPE_EDGE));
326370

327-
/* no more labels to process */
328-
if (!HeapTupleIsValid(tuple))
329-
break;
371+
scan_desc = table_beginscan(ag_label, estate->es_snapshot, 2, scan_keys);
330372

331-
ExecStoreHeapTuple(tuple, slot, false);
373+
// scan through the results and get all the label names.
374+
while(true)
375+
{
376+
Name label;
377+
Name lval;
378+
bool isNull;
379+
Datum datum;
332380

333-
datum = slot_getattr(slot, Anum_ag_label_name, &isNull);
334-
label = DatumGetName(datum);
381+
tuple = heap_getnext(scan_desc, ForwardScanDirection);
335382

336-
labels = lappend(labels, label);
337-
}
383+
// no more labels to process
384+
if (!HeapTupleIsValid(tuple))
385+
break;
386+
387+
ExecStoreHeapTuple(tuple, slot, false);
338388

339-
table_endscan(scan_desc);
389+
datum = slot_getattr(slot, Anum_ag_label_name, &isNull);
390+
label = DatumGetName(datum);
391+
392+
lval = (Name) palloc(NAMEDATALEN);
393+
namestrcpy(lval, NameStr(*label));
394+
labels = lappend(labels, lval);
395+
}
396+
397+
table_endscan(scan_desc);
398+
}
340399

341400
destroy_entity_result_rel_info(resultRelInfo);
342401
table_close(resultRelInfo->ri_RelationDesc, RowExclusiveLock);

0 commit comments

Comments
 (0)