From eb0dc48f1734d3bb3ebf4026507345f5ecbcca56 Mon Sep 17 00:00:00 2001 From: Onur Tirtir Date: Fri, 12 May 2023 17:05:31 +0300 Subject: [PATCH] v1.2 --- src/backend/distributed/smart_hint.c | 31 +++++++++++++++++- .../distributed/sql/citus--11.3-1--12.0-1.sql | 32 +++++++++++++++---- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/src/backend/distributed/smart_hint.c b/src/backend/distributed/smart_hint.c index 8e054eb34..04f862625 100644 --- a/src/backend/distributed/smart_hint.c +++ b/src/backend/distributed/smart_hint.c @@ -55,7 +55,7 @@ static void RunHintAI(void) { elog(NOTICE, "Running the HintAI code..."); - system("psql -p 9700 -f /tmp/hint_ai_code.txt --single-transaction 2> /tmp/hint_ai_code_error.txt"); + system("psql -p 9700 -X -f /tmp/hint_ai_code.txt --single-transaction 2> /tmp/hint_ai_code_error.txt 1> /tmp/hint_ai_code_output.txt"); // read the first ERROR-DETAIL-HINT block from the error file FILE *fp = fopen("/tmp/hint_ai_code_error.txt", "r"); @@ -105,6 +105,8 @@ RunHintAI(void) } } + fclose(fp); + if (error) { ereport(ERROR, (errmsg("%s", error), @@ -112,6 +114,33 @@ RunHintAI(void) hint ? errhint("%s", hint) : 0)); } + // else, print lines from /tmp/hint_ai_code_output.txt + fp = fopen("/tmp/hint_ai_code_output.txt", "r"); + if (fp == NULL) + { + elog(ERROR, "Failed to open file"); + } + + StringInfo outBuf = makeStringInfo(); + + while (true) + { + char *line = NULL; + size_t len = 0; + ssize_t read = getline(&line, &len, fp); + if (read == -1) + { + break; + } + + appendStringInfoString(outBuf, line); + } + + if (outBuf->len > 0) + { + ereport(NOTICE, (errmsg("The output of \"RUN HINT\":\n%s", outBuf->data))); + } + fclose(fp); } diff --git a/src/backend/distributed/sql/citus--11.3-1--12.0-1.sql b/src/backend/distributed/sql/citus--11.3-1--12.0-1.sql index 43bbcb181..40156bf16 100644 --- a/src/backend/distributed/sql/citus--11.3-1--12.0-1.sql +++ b/src/backend/distributed/sql/citus--11.3-1--12.0-1.sql @@ -8,16 +8,36 @@ AS $$ DECLARE cluster_stats text; BEGIN - WITH citus_tables_arr AS ( - SELECT array_agg(row_to_json(t.*)) d FROM public.citus_tables t + WITH + citus_tables_arr AS ( + SELECT array_agg(row_to_json(t.*)) d FROM ( + SELECT table_name, distribution_column FROM public.citus_tables + ) t + ), + all_tables_arr AS ( + SELECT array_agg(tablename) d from pg_tables where schemaname='public' ), citus_nodes_arr AS ( - SELECT array_agg(row_to_json(t.*)) d FROM pg_catalog.pg_dist_node t + SELECT array_agg(row_to_json(t.*)) d FROM ( + SELECT nodename, nodeport FROM pg_catalog.pg_dist_node + ) t ), key_values AS ( - SELECT unnest(ARRAY['citus_tables', 'citus_nodes']) AS key, - unnest(ARRAY[to_json(citus_tables_arr.d), to_json(citus_nodes_arr.d)]::json[]) AS value - FROM citus_tables_arr, citus_nodes_arr + SELECT + unnest(ARRAY[ + 'citus_tables', + 'all_tables', + 'citus_nodes' + ]) AS key, + unnest(ARRAY[ + to_json(citus_tables_arr.d), + to_json(all_tables_arr.d), + to_json(citus_nodes_arr.d) + ]::json[]) AS value + FROM + citus_tables_arr, + all_tables_arr, + citus_nodes_arr ) SELECT json_object_agg(key, value)::text INTO cluster_stats FROM key_values;