Skip to content

Commit

Permalink
Merge pull request #334 from EngineeredVirus/main
Browse files Browse the repository at this point in the history
PG-354: pg_stat_monitor: Remove pg_stat_monitor_settings view
  • Loading branch information
Ibrar Ahmed authored Dec 21, 2022
2 parents 8dffa8c + 2917ae6 commit 7c5ad48
Show file tree
Hide file tree
Showing 5 changed files with 307 additions and 198 deletions.
2 changes: 1 addition & 1 deletion pg_stat_monitor--1.0--2.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ DROP FUNCTION pgsm_create_11_view CASCADE;
DROP FUNCTION pgsm_create_13_view CASCADE;
DROP FUNCTION pgsm_create_14_view CASCADE;
DROP FUNCTION pgsm_create_view CASCADE;
DROP FUNCTION pg_stat_monitor_settings CASCADE;

-- pg_stat_monitor internal function, must not call outside from this file.
CREATE FUNCTION pg_stat_monitor_internal(
Expand Down Expand Up @@ -369,7 +370,6 @@ SELECT pgsm_create_view();

REVOKE ALL ON FUNCTION range FROM PUBLIC;
REVOKE ALL ON FUNCTION get_cmd_type FROM PUBLIC;
REVOKE ALL ON FUNCTION pg_stat_monitor_settings FROM PUBLIC;
REVOKE ALL ON FUNCTION decode_error_level FROM PUBLIC;
REVOKE ALL ON FUNCTION pg_stat_monitor_internal FROM PUBLIC;
REVOKE ALL ON FUNCTION get_histogram_timings FROM PUBLIC;
Expand Down
26 changes: 0 additions & 26 deletions pg_stat_monitor--2.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,6 @@ SELECT
$$
LANGUAGE SQL PARALLEL SAFE;

CREATE FUNCTION pg_stat_monitor_settings(
OUT name text,
OUT value text,
OUT default_value text,
OUT description text,
OUT minimum INTEGER,
OUT maximum INTEGER,
OUT options text,
OUT restart text
)
RETURNS SETOF record
AS 'MODULE_PATHNAME', 'pg_stat_monitor_settings'
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;

CREATE VIEW pg_stat_monitor_settings AS SELECT
name,
value,
default_value,
description,
minimum,
maximum,
options,
restart
FROM pg_stat_monitor_settings();

CREATE FUNCTION decode_error_level(elevel int)
RETURNS text
AS
Expand Down Expand Up @@ -465,7 +440,6 @@ $$ LANGUAGE plpgsql;
SELECT pgsm_create_view();
REVOKE ALL ON FUNCTION range FROM PUBLIC;
REVOKE ALL ON FUNCTION get_cmd_type FROM PUBLIC;
REVOKE ALL ON FUNCTION pg_stat_monitor_settings FROM PUBLIC;
REVOKE ALL ON FUNCTION decode_error_level FROM PUBLIC;
REVOKE ALL ON FUNCTION pg_stat_monitor_internal FROM PUBLIC;
REVOKE ALL ON FUNCTION get_histogram_timings FROM PUBLIC;
Expand Down
131 changes: 0 additions & 131 deletions pg_stat_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ PG_FUNCTION_INFO_V1(pg_stat_monitor_reset);
PG_FUNCTION_INFO_V1(pg_stat_monitor_1_0);
PG_FUNCTION_INFO_V1(pg_stat_monitor_2_0);
PG_FUNCTION_INFO_V1(pg_stat_monitor);
PG_FUNCTION_INFO_V1(pg_stat_monitor_settings);
PG_FUNCTION_INFO_V1(get_histogram_timings);
PG_FUNCTION_INFO_V1(pg_stat_monitor_hook_stats);

Expand Down Expand Up @@ -3115,136 +3114,6 @@ intarray_get_datum(int32 arr[], int len)
}


Datum
pg_stat_monitor_settings(PG_FUNCTION_ARGS)
{
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
TupleDesc tupdesc;
Tuplestorestate *tupstore;
MemoryContext per_query_ctx;
MemoryContext oldcontext;
int i;

/* Safety check... */
if (!IsSystemInitialized())
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("pg_stat_monitor: must be loaded via shared_preload_libraries")));

/* check to see if caller supports us returning a tuplestore */
if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("pg_stat_monitor: set-valued function called in context that cannot accept a set")));

/* Switch into long-lived context to construct returned data structures */
per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
oldcontext = MemoryContextSwitchTo(per_query_ctx);

/* Build a tuple descriptor for our result type */
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
{
elog(ERROR, "pg_stat_monitor_settings: return type must be a row type");
return (Datum) 0;
}

if (tupdesc->natts != 8)
{
elog(ERROR, "pg_stat_monitor_settings: incorrect number of output arguments, required: 7, found %d", tupdesc->natts);
return (Datum) 0;
}

tupstore = tuplestore_begin_heap(true, false, work_mem);
rsinfo->returnMode = SFRM_Materialize;
rsinfo->setResult = tupstore;
rsinfo->setDesc = tupdesc;

MemoryContextSwitchTo(oldcontext);

for (i = 0; i < MAX_SETTINGS; i++)
{
Datum values[8];
bool nulls[8];
int j = 0;
char options[1024] = "";
GucVariable *conf;

memset(values, 0, sizeof(values));
memset(nulls, 0, sizeof(nulls));

conf = get_conf(i);

values[j++] = CStringGetTextDatum(conf->guc_name);

/* Handle current and default values. */
switch (conf->type)
{
case PGC_ENUM:
values[j++] = CStringGetTextDatum(conf->guc_options[conf->guc_variable]);
values[j++] = CStringGetTextDatum(conf->guc_options[conf->guc_default]);
break;

case PGC_INT:
{
char value[32];

sprintf(value, "%d", conf->guc_variable);
values[j++] = CStringGetTextDatum(value);

sprintf(value, "%d", conf->guc_default);
values[j++] = CStringGetTextDatum(value);
break;
}

case PGC_BOOL:
values[j++] = CStringGetTextDatum(conf->guc_variable ? "yes" : "no");
values[j++] = CStringGetTextDatum(conf->guc_default ? "yes" : "no");
break;

default:
Assert(false);
}

values[j++] = CStringGetTextDatum(get_conf(i)->guc_desc);

/* Minimum and maximum displayed only for integers or real numbers. */
if (conf->type != PGC_INT)
{
nulls[j++] = true;
nulls[j++] = true;
}
else
{
values[j++] = Int32GetDatum(get_conf(i)->guc_min);
values[j++] = Int32GetDatum(get_conf(i)->guc_max);
}

if (conf->type == PGC_ENUM)
{
size_t i;

strcat(options, conf->guc_options[0]);
for (i = 1; i < conf->n_options; ++i)
{
strcat(options, ", ");
strcat(options, conf->guc_options[i]);
}
}
else if (conf->type == PGC_BOOL)
{
strcat(options, "yes, no");
}

values[j++] = CStringGetTextDatum(options);
values[j++] = CStringGetTextDatum(get_conf(i)->guc_restart ? "yes" : "no");
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
}
/* clean up and return the tuplestore */
tuplestore_donestoring(tupstore);
return (Datum) 0;
}


Datum
pg_stat_monitor_hook_stats(PG_FUNCTION_ARGS)
{
Expand Down
Loading

0 comments on commit 7c5ad48

Please sign in to comment.