Skip to content

Commit 560a9ef

Browse files
okbob@github.comCommitfest Bot
authored andcommitted
function pg_get_session_variables_memory for cleaning tests
This is a function designed for testing and debugging. It returns the content of sessionvars as-is, and can therefore display entries about session variables that were dropped but for which this backend didn't process the shared invalidations yet.
1 parent 4b37638 commit 560a9ef

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

src/backend/commands/session_variable.c

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "executor/execdesc.h"
2323
#include "executor/executor.h"
2424
#include "executor/svariableReceiver.h"
25+
#include "funcapi.h"
2526
#include "miscadmin.h"
2627
#include "nodes/plannodes.h"
2728
#include "parser/parse_type.h"
@@ -600,3 +601,102 @@ ExecuteLetStmt(ParseState *pstate,
600601

601602
PopActiveSnapshot();
602603
}
604+
605+
/*
606+
* pg_get_session_variables_memory - designed for testing
607+
*
608+
* This is a function designed for testing and debugging. It returns the
609+
* content of session variables as-is, and can therefore display data about
610+
* session variables that were dropped, but for which this backend didn't
611+
* process the shared invalidations yet.
612+
*/
613+
Datum
614+
pg_get_session_variables_memory(PG_FUNCTION_ARGS)
615+
{
616+
#define NUM_PG_GET_SESSION_VARIABLES_MEMORY_ATTS 8
617+
618+
/*
619+
* Make sure syscache entries are flushed for recent catalog changes. For
620+
* stable behavior we need to reliably detect which variables were
621+
* dropped.
622+
*/
623+
AcceptInvalidationMessages();
624+
625+
InitMaterializedSRF(fcinfo, 0);
626+
627+
if (sessionvars)
628+
{
629+
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
630+
HASH_SEQ_STATUS status;
631+
SVariable svar;
632+
633+
hash_seq_init(&status, sessionvars);
634+
635+
while ((svar = (SVariable) hash_seq_search(&status)) != NULL)
636+
{
637+
Datum values[NUM_PG_GET_SESSION_VARIABLES_MEMORY_ATTS];
638+
bool nulls[NUM_PG_GET_SESSION_VARIABLES_MEMORY_ATTS];
639+
HeapTuple tp;
640+
bool var_is_valid = false;
641+
642+
memset(values, 0, sizeof(values));
643+
memset(nulls, 0, sizeof(nulls));
644+
645+
values[0] = ObjectIdGetDatum(svar->varid);
646+
values[3] = ObjectIdGetDatum(svar->typid);
647+
648+
/*
649+
* It is possible that the variable has been dropped from the
650+
* catalog, but not yet purged from the hash table.
651+
*/
652+
tp = SearchSysCache1(VARIABLEOID, ObjectIdGetDatum(svar->varid));
653+
654+
if (HeapTupleIsValid(tp))
655+
{
656+
Form_pg_variable varform = (Form_pg_variable) GETSTRUCT(tp);
657+
658+
/*
659+
* It is also possible that a variable has been dropped and
660+
* someone created a new variable with the same object ID.
661+
* Use the catalog information only if that is not the case.
662+
*/
663+
if (svar->create_lsn == varform->varcreate_lsn)
664+
{
665+
values[1] = CStringGetTextDatum(
666+
get_namespace_name(varform->varnamespace));
667+
668+
values[2] = CStringGetTextDatum(NameStr(varform->varname));
669+
values[4] = CStringGetTextDatum(format_type_be(svar->typid));
670+
values[5] = BoolGetDatum(false);
671+
672+
values[6] = BoolGetDatum(
673+
object_aclcheck(VariableRelationId, svar->varid,
674+
GetUserId(), ACL_SELECT) == ACLCHECK_OK);
675+
676+
values[7] = BoolGetDatum(
677+
object_aclcheck(VariableRelationId, svar->varid,
678+
GetUserId(), ACL_UPDATE) == ACLCHECK_OK);
679+
680+
var_is_valid = true;
681+
}
682+
683+
ReleaseSysCache(tp);
684+
}
685+
686+
/* if there is no matching catalog entry, return null values */
687+
if (!var_is_valid)
688+
{
689+
nulls[1] = true;
690+
nulls[2] = true;
691+
nulls[4] = true;
692+
values[5] = BoolGetDatum(true);
693+
nulls[6] = true;
694+
nulls[7] = true;
695+
}
696+
697+
tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
698+
}
699+
}
700+
701+
return (Datum) 0;
702+
}

src/include/catalog/pg_proc.dat

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12627,4 +12627,12 @@
1262712627
proargnames => '{pid,io_id,io_generation,state,operation,off,length,target,handle_data_len,raw_result,result,target_desc,f_sync,f_localmem,f_buffered}',
1262812628
prosrc => 'pg_get_aios' },
1262912629

12630+
# Session variables support
12631+
{ oid => '8068', descr => 'internal view of memory entries used by session variables (for debugging)',
12632+
proname => 'pg_get_session_variables_memory', prorows => '1000', proretset => 't',
12633+
provolatile => 's', proparallel => 'r', prorettype => 'record',
12634+
proargtypes => '', proallargtypes => '{oid,text,text,oid,text,bool,bool,bool}',
12635+
proargmodes => '{o,o,o,o,o,o,o,o}',
12636+
proargnames => '{varid,schema,name,typid,typname,dropped,can_select,can_update}',
12637+
prosrc => 'pg_get_session_variables_memory' },
1263012638
]

0 commit comments

Comments
 (0)