Skip to content

Commit efe9733

Browse files
okbob@github.comCommitfest Bot
authored and
Commitfest Bot
committed
function pg_session_variables 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 e6ac333 commit efe9733

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

src/backend/commands/session_variable.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,3 +527,95 @@ ExecuteLetStmt(ParseState *pstate,
527527

528528
PopActiveSnapshot();
529529
}
530+
531+
/*
532+
* pg_session_variables - designed for testing
533+
*
534+
* This is a function designed for testing and debugging. It returns the
535+
* content of session variables as-is, and can therefore display data about
536+
* session variables that were dropped, but for which this backend didn't
537+
* process the shared invalidations yet.
538+
*/
539+
Datum
540+
pg_session_variables(PG_FUNCTION_ARGS)
541+
{
542+
#define NUM_PG_SESSION_VARIABLES_ATTS 8
543+
544+
InitMaterializedSRF(fcinfo, 0);
545+
546+
if (sessionvars)
547+
{
548+
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
549+
HASH_SEQ_STATUS status;
550+
SVariable svar;
551+
552+
hash_seq_init(&status, sessionvars);
553+
554+
while ((svar = (SVariable) hash_seq_search(&status)) != NULL)
555+
{
556+
Datum values[NUM_PG_SESSION_VARIABLES_ATTS];
557+
bool nulls[NUM_PG_SESSION_VARIABLES_ATTS];
558+
HeapTuple tp;
559+
bool var_is_valid = false;
560+
561+
memset(values, 0, sizeof(values));
562+
memset(nulls, 0, sizeof(nulls));
563+
564+
values[0] = ObjectIdGetDatum(svar->varid);
565+
values[3] = ObjectIdGetDatum(svar->typid);
566+
567+
/*
568+
* It is possible that the variable has been dropped from the
569+
* catalog, but not yet purged from the hash table.
570+
*/
571+
tp = SearchSysCache1(VARIABLEOID, ObjectIdGetDatum(svar->varid));
572+
573+
if (HeapTupleIsValid(tp))
574+
{
575+
Form_pg_variable varform = (Form_pg_variable) GETSTRUCT(tp);
576+
577+
/*
578+
* It is also possible that a variable has been dropped and
579+
* someone created a new variable with the same object ID. Use
580+
* the catalog information only if that is not the case.
581+
*/
582+
if (svar->create_lsn == varform->varcreate_lsn)
583+
{
584+
values[1] = CStringGetTextDatum(
585+
get_namespace_name(varform->varnamespace));
586+
587+
values[2] = CStringGetTextDatum(NameStr(varform->varname));
588+
values[4] = CStringGetTextDatum(format_type_be(svar->typid));
589+
values[5] = BoolGetDatum(false);
590+
591+
values[6] = BoolGetDatum(
592+
object_aclcheck(VariableRelationId, svar->varid,
593+
GetUserId(), ACL_SELECT) == ACLCHECK_OK);
594+
595+
values[7] = BoolGetDatum(
596+
object_aclcheck(VariableRelationId, svar->varid,
597+
GetUserId(), ACL_UPDATE) == ACLCHECK_OK);
598+
599+
var_is_valid = true;
600+
}
601+
602+
ReleaseSysCache(tp);
603+
}
604+
605+
/* if there is no matching catalog entry, return null values */
606+
if (!var_is_valid)
607+
{
608+
nulls[1] = true;
609+
nulls[2] = true;
610+
nulls[4] = true;
611+
values[5] = BoolGetDatum(true);
612+
nulls[6] = true;
613+
nulls[7] = true;
614+
}
615+
616+
tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
617+
}
618+
}
619+
620+
return (Datum) 0;
621+
}

src/include/catalog/pg_proc.dat

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12589,4 +12589,12 @@
1258912589
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}',
1259012590
prosrc => 'pg_get_aios' },
1259112591

12592+
# Session variables support
12593+
{ oid => '8488', descr => 'list of used session variables',
12594+
proname => 'pg_session_variables', prorows => '1000', proretset => 't',
12595+
provolatile => 's', proparallel => 'r', prorettype => 'record',
12596+
proargtypes => '', proallargtypes => '{oid,text,text,oid,text,bool,bool,bool}',
12597+
proargmodes => '{o,o,o,o,o,o,o,o}',
12598+
proargnames => '{varid,schema,name,typid,typname,removed,can_select,can_update}',
12599+
prosrc => 'pg_session_variables' },
1259212600
]

0 commit comments

Comments
 (0)