31 #include "utils/array.h"
49 char* coordinates_sql,
55 int64_t tries_per_temperature,
56 int64_t max_changes_per_temperature,
57 int64_t max_consecutive_non_changes,
59 double initial_temperature,
60 double final_temperature,
61 double cooling_factor,
66 size_t *result_count) {
72 if (initial_temperature < final_temperature) {
73 elog(ERROR,
"Condition not met: initial_temperature"
74 " > final_temperature");
76 if (final_temperature <= 0) {
77 elog(ERROR,
"Condition not met: final_temperature > 0");
79 if (cooling_factor <=0 || cooling_factor >=1) {
80 elog(ERROR,
"Condition not met: 0 < cooling_factor < 1");
82 if (tries_per_temperature < 0) {
83 elog(ERROR,
"Condition not met: tries_per_temperature >= 0");
85 if (max_changes_per_temperature < 1) {
86 elog(ERROR,
"Condition not met: max_changes_per_temperature > 0");
88 if (max_consecutive_non_changes < 1) {
89 elog(ERROR,
"Condition not met: max_consecutive_non_changes > 0");
92 elog(ERROR,
"Condition not met: max_processing_time >= 0");
97 size_t total_coordinates = 0;
100 if (total_coordinates == 0) {
101 PGR_DBG(
"No coordinates found");
103 (*result_tuples) = NULL;
109 clock_t start_t = clock();
110 char* log_msg = NULL;
111 char* notice_msg = NULL;
112 char* err_msg = NULL;
115 coordinates, total_coordinates,
121 tries_per_temperature,
122 max_changes_per_temperature,
123 max_consecutive_non_changes,
132 time_msg(
"euclideanTSP", start_t, clock());
134 if (err_msg && (*result_tuples)) {
135 pfree(*result_tuples);
136 (*result_tuples) = NULL;
142 if (log_msg) pfree(log_msg);
143 if (notice_msg) pfree(notice_msg);
144 if (err_msg) pfree(err_msg);
145 if (coordinates) pfree(coordinates);
155 FuncCallContext *funcctx;
156 TupleDesc tuple_desc;
162 size_t result_count = 0;
166 if (SRF_IS_FIRSTCALL()) {
167 MemoryContext oldcontext;
168 funcctx = SRF_FIRSTCALL_INIT();
169 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
195 text_to_cstring(PG_GETARG_TEXT_P(0)),
215 #if PGSQL_VERSION > 95
216 funcctx->max_calls = result_count;
218 funcctx->max_calls = (uint32_t)result_count;
221 funcctx->user_fctx = result_tuples;
222 if (get_call_result_type(fcinfo, NULL, &tuple_desc)
223 != TYPEFUNC_COMPOSITE) {
225 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
226 errmsg(
"function returning record called in context "
227 "that cannot accept type record")));
230 funcctx->tuple_desc = tuple_desc;
231 MemoryContextSwitchTo(oldcontext);
234 funcctx = SRF_PERCALL_SETUP();
235 tuple_desc = funcctx->tuple_desc;
238 if (funcctx->call_cntr < funcctx->max_calls) {
251 values = palloc(4 *
sizeof(Datum));
252 nulls = palloc(4 *
sizeof(
bool));
256 for (i = 0; i < 4; ++i) {
261 values[0] = Int32GetDatum(funcctx->call_cntr + 1);
262 values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].
node);
263 values[2] = Float8GetDatum(result_tuples[funcctx->call_cntr].
cost);
264 values[3] = Float8GetDatum(result_tuples[funcctx->call_cntr].
agg_cost);
267 tuple = heap_form_tuple(tuple_desc, values, nulls);
268 result = HeapTupleGetDatum(tuple);
269 SRF_RETURN_NEXT(funcctx, result);
271 SRF_RETURN_DONE(funcctx);