36 PGDLLEXPORT Datum
_pgr_ksp(PG_FUNCTION_ARGS);
54 size_t k = (size_t)p_k;
58 size_t total_edges = 0;
61 if (start_vertex == end_vertex) {
67 PGR_DBG(
"Total %ld edges in query:", total_edges);
69 if (total_edges == 0) {
76 PGR_DBG(
"Calling do_pgr_ksp\n");
77 PGR_DBG(
"heap_paths = %i\n", heap_paths);
79 clock_t start_t = clock();
81 char *notice_msg = NULL;
97 time_msg(
" processing KSP", start_t, clock());
99 if (err_msg && (*result_tuples)) {
100 pfree(*result_tuples);
101 (*result_tuples) = NULL;
107 if (log_msg) pfree(log_msg);
108 if (notice_msg) pfree(notice_msg);
109 if (err_msg) pfree(err_msg);
121 FuncCallContext *funcctx;
122 TupleDesc tuple_desc;
124 size_t result_count = 0;
126 if (SRF_IS_FIRSTCALL()) {
127 MemoryContext oldcontext;
128 funcctx = SRF_FIRSTCALL_INIT();
129 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
143 text_to_cstring(PG_GETARG_TEXT_P(0)),
151 PGR_DBG(
"Total number of tuples to be returned %ld \n", result_count);
154 #if PGSQL_VERSION > 95
155 funcctx->max_calls = result_count;
157 funcctx->max_calls = (uint32_t)result_count;
159 funcctx->user_fctx = path;
160 if (get_call_result_type(fcinfo, NULL, &tuple_desc)
161 != TYPEFUNC_COMPOSITE)
163 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
164 errmsg(
"function returning record called in context "
165 "that cannot accept type record\n")));
167 funcctx->tuple_desc = tuple_desc;
168 MemoryContextSwitchTo(oldcontext);
172 funcctx = SRF_PERCALL_SETUP();
175 tuple_desc = funcctx->tuple_desc;
178 if (funcctx->call_cntr < funcctx->max_calls) {
184 values = palloc(7 *
sizeof(Datum));
185 nulls = palloc(7 *
sizeof(
bool));
189 for (i = 0; i < 7; ++i) {
193 values[0] = Int32GetDatum(funcctx->call_cntr + 1);
194 values[1] = Int32GetDatum(path[funcctx->call_cntr].
start_id + 1);
195 values[2] = Int32GetDatum(path[funcctx->call_cntr].
seq);
196 values[3] = Int64GetDatum(path[funcctx->call_cntr].
node);
197 values[4] = Int64GetDatum(path[funcctx->call_cntr].
edge);
198 values[5] = Float8GetDatum(path[funcctx->call_cntr].
cost);
199 values[6] = Float8GetDatum(path[funcctx->call_cntr].
agg_cost);
201 tuple = heap_form_tuple(tuple_desc, values, nulls);
202 result = HeapTupleGetDatum(tuple);
203 SRF_RETURN_NEXT(funcctx, result);
205 SRF_RETURN_DONE(funcctx);