Skip to content

Commit

Permalink
Format sources (#475)
Browse files Browse the repository at this point in the history
* Temporary disable workflows

* Add indent target to makefiel

* Add CI workflow to check if sources formatted

* Fix

* Fix

* Fix

* Fix

* Fix

* Fix

* Fix

* Fix

* Fix

* Format sources

* Add comments

* Revert "Temporary disable workflows"

This reverts commit 7e11cf6.

* Revert "Format sources"

This reverts commit 6ef992d.

* Use PG17 for code formatt

* Format sources

* Revert "Format sources"

This reverts commit 34061e1.

* Format sources
  • Loading branch information
artemgavrilov authored Aug 7, 2024
1 parent 130d6b5 commit 3bb6579
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 86 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,49 @@ jobs:
set -x
cppcheck --enable=all --inline-suppr --template='{file}:{line},{severity},{id},{message}' --error-exitcode=1 --suppress=missingIncludeSystem --suppress=missingInclude --suppress=unmatchedSuppression:pg_stat_monitor.c --check-config .
format:
name: Format
runs-on: ubuntu-22.04

steps:
- name: Clone postgres repository
uses: actions/checkout@v4
with:
repository: 'postgres/postgres'
ref: 'REL_17_STABLE'

- name: Checkout sources
uses: actions/checkout@v4
with:
path: 'contrib/pg_stat_monitor'

- name: Configure postgres
run: ./configure

- name: Install perltidy
run: sudo cpan -T SHANCOCK/Perl-Tidy-20230309.tar.gz

- name: Install pg_bsd_indent
working-directory: src/tools/pg_bsd_indent
run: sudo make install

- name: Add pg_bsd_indent and pgindent to path
run: |
echo "/usr/local/pgsql/bin" >> $GITHUB_PATH
echo "${{ github.workspace }}/src/tools/pgindent" >> $GITHUB_PATH
- name: Format sources
working-directory: contrib/pg_stat_monitor
run: |
make update-typedefs
make indent
- name: Check files are formatted and no source code changes
working-directory: contrib/pg_stat_monitor
run: |
git status
git diff --exit-code
license:
name: License
runs-on: ubuntu-22.04
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ dkms.conf
## .vscode
.vscode/
.vscode/*

# tools files
typedefs-full.list
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,14 @@ top_builddir = ../..
include $(top_builddir)/src/Makefile.global
include $(top_srcdir)/contrib/contrib-global.mk
endif

# Fetches typedefs list for PostgreSQL core and merges it with typedefs defined in this project.
# https://wiki.postgresql.org/wiki/Running_pgindent_on_non-core_code_or_development_code
update-typedefs:
wget -q -O - "https://buildfarm.postgresql.org/cgi-bin/typedefs.pl?branch=REL_17_STABLE" | cat - typedefs.list | sort | uniq > typedefs-full.list

# Indents projects sources.
indent:
pgindent --typedefs=typedefs-full.list .

.PHONY: update-typedefs indent
2 changes: 1 addition & 1 deletion guc.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static bool check_overflow_targer(int *newval, void **extra, GucSource source);
void
init_guc(void)
{
pgsm_track = PGSM_TRACK_TOP;
pgsm_track = PGSM_TRACK_TOP;

DefineCustomIntVariable("pg_stat_monitor.pgsm_max", /* name */
"Sets the maximum size of shared memory in (MB) used for statement's metadata tracked by pg_stat_monitor.", /* short_desc */
Expand Down
23 changes: 12 additions & 11 deletions hash_query.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
#include "pg_stat_monitor.h"

static pgsmLocalState pgsmStateLocal;
static PGSM_HASH_TABLE_HANDLE pgsm_create_bucket_hash(pgsmSharedState * pgsm, dsa_area *dsa);
static PGSM_HASH_TABLE_HANDLE pgsm_create_bucket_hash(pgsmSharedState *pgsm, dsa_area *dsa);
static Size pgsm_get_shared_area_size(void);
static void InitializeSharedState(pgsmSharedState * pgsm);
static void InitializeSharedState(pgsmSharedState *pgsm);

#define PGSM_BUCKET_INFO_SIZE (sizeof(TimestampTz) * pgsm_max_buckets)
#define PGSM_SHARED_STATE_SIZE (sizeof(pgsmSharedState) + PGSM_BUCKET_INFO_SIZE)
Expand Down Expand Up @@ -142,9 +142,9 @@ pgsm_startup(void)
*/
dsa_detach(dsa);

pgsmStateLocal.pgsm_mem_cxt = AllocSetContextCreate(TopMemoryContext,
"pg_stat_monitor local store",
ALLOCSET_DEFAULT_SIZES);
pgsmStateLocal.pgsm_mem_cxt = AllocSetContextCreate(TopMemoryContext,
"pg_stat_monitor local store",
ALLOCSET_DEFAULT_SIZES);
}

#ifdef BENCHMARK
Expand All @@ -161,7 +161,7 @@ pgsm_startup(void)
}

static void
InitializeSharedState(pgsmSharedState * pgsm)
InitializeSharedState(pgsmSharedState *pgsm)
{
pg_atomic_init_u64(&pgsm->current_wbucket, 0);
pg_atomic_init_u64(&pgsm->prev_bucket_sec, 0);
Expand All @@ -172,7 +172,7 @@ InitializeSharedState(pgsmSharedState * pgsm)
* Create the classic or dshahs hash table for storing the query statistics.
*/
static PGSM_HASH_TABLE_HANDLE
pgsm_create_bucket_hash(pgsmSharedState * pgsm, dsa_area *dsa)
pgsm_create_bucket_hash(pgsmSharedState *pgsm, dsa_area *dsa)
{
PGSM_HASH_TABLE_HANDLE bucket_hash;

Expand Down Expand Up @@ -238,7 +238,8 @@ pgsm_attach_shmem(void)
MemoryContextSwitchTo(oldcontext);
}

MemoryContext GetPgsmMemoryContext(void)
MemoryContext
GetPgsmMemoryContext(void)
{
return pgsmStateLocal.pgsm_mem_cxt;
}
Expand Down Expand Up @@ -287,7 +288,7 @@ pgsm_shmem_shutdown(int code, Datum arg)
}

pgsmEntry *
hash_entry_alloc(pgsmSharedState * pgsm, pgsmHashKey * key, int encoding)
hash_entry_alloc(pgsmSharedState *pgsm, pgsmHashKey *key, int encoding)
{
pgsmEntry *entry = NULL;
bool found = false;
Expand Down Expand Up @@ -393,7 +394,7 @@ IsSystemOOM(void)
*/

void *
pgsm_hash_find_or_insert(PGSM_HASH_TABLE * shared_hash, pgsmHashKey * key, bool *found)
pgsm_hash_find_or_insert(PGSM_HASH_TABLE * shared_hash, pgsmHashKey *key, bool *found)
{
#if USE_DYNAMIC_HASH
void *entry;
Expand All @@ -406,7 +407,7 @@ pgsm_hash_find_or_insert(PGSM_HASH_TABLE * shared_hash, pgsmHashKey * key, bool
}

void *
pgsm_hash_find(PGSM_HASH_TABLE * shared_hash, pgsmHashKey * key, bool *found)
pgsm_hash_find(PGSM_HASH_TABLE * shared_hash, pgsmHashKey *key, bool *found)
{
#if USE_DYNAMIC_HASH
return dshash_find(shared_hash, key, false);
Expand Down
67 changes: 35 additions & 32 deletions pg_stat_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ typedef enum pgsmVersion
PGSM_V1_0 = 0,
PGSM_V2_0,
PGSM_V2_1
} pgsmVersion;
} pgsmVersion;

PG_MODULE_MAGIC;

Expand Down Expand Up @@ -201,9 +201,9 @@ DECLARE_HOOK(void pgsm_ProcessUtility, PlannedStmt *pstmt, const char *queryStri
static uint64 pgsm_hash_string(const char *str, int len);
char *unpack_sql_state(int sql_state);

static pgsmEntry * pgsm_create_hash_entry(uint64 bucket_id, uint64 queryid, PlanInfo * plan_info);
static void pgsm_add_to_list(pgsmEntry * entry, char *query_text, int query_len);
static pgsmEntry * pgsm_get_entry_for_query(uint64 queryid, PlanInfo * plan_info, const char *query_text, int query_len, bool create);
static pgsmEntry *pgsm_create_hash_entry(uint64 bucket_id, uint64 queryid, PlanInfo *plan_info);
static void pgsm_add_to_list(pgsmEntry *entry, char *query_text, int query_len);
static pgsmEntry *pgsm_get_entry_for_query(uint64 queryid, PlanInfo *plan_info, const char *query_text, int query_len, bool create);
static uint64 get_pgsm_query_id_hash(const char *norm_query, int len);

static void pgsm_cleanup_callback(void *arg);
Expand All @@ -217,13 +217,13 @@ MemoryContextCallback mem_cxt_reset_callback =
};
volatile bool callback_setup = false;

static void pgsm_update_entry(pgsmEntry * entry,
static void pgsm_update_entry(pgsmEntry *entry,
const char *query,
char *comments,
int comments_len,
PlanInfo * plan_info,
SysInfo * sys_info,
ErrorInfo * error_info,
PlanInfo *plan_info,
SysInfo *sys_info,
ErrorInfo *error_info,
double plan_total_time,
double exec_total_time,
uint64 rows,
Expand All @@ -232,7 +232,7 @@ static void pgsm_update_entry(pgsmEntry * entry,
const struct JitInstrumentation *jitusage,
bool reset,
pgsmStoreKind kind);
static void pgsm_store(pgsmEntry * entry);
static void pgsm_store(pgsmEntry *entry);

static void pg_stat_monitor_internal(FunctionCallInfo fcinfo,
pgsmVersion api_version,
Expand All @@ -259,7 +259,7 @@ static char *generate_normalized_query(JumbleState *jstate, const char *query,
static void fill_in_constant_lengths(JumbleState *jstate, const char *query, int query_loc);
static int comp_location(const void *a, const void *b);

static uint64 get_next_wbucket(pgsmSharedState * pgsm);
static uint64 get_next_wbucket(pgsmSharedState *pgsm);

/*
* Module load callback
Expand Down Expand Up @@ -696,7 +696,7 @@ pgsm_ExecutorEnd(QueryDesc *queryDesc)
/* Extract the plan information in case of SELECT statement */
if (queryDesc->operation == CMD_SELECT && pgsm_enable_query_plan)
{
int rv;
int rv;
MemoryContext oldctx;

/*
Expand Down Expand Up @@ -806,9 +806,9 @@ pgsm_ExecutorCheckPerms(List *rt, List *rp, bool abort)

if (rte->rtekind != RTE_RELATION
#if PG_VERSION_NUM >= 160000
&& (rte->rtekind != RTE_SUBQUERY && rte->relkind != 'v')
&& (rte->rtekind != RTE_SUBQUERY && rte->relkind != 'v')
#endif
)
)
continue;

if (i < REL_LST)
Expand Down Expand Up @@ -1417,13 +1417,13 @@ pg_get_client_addr(bool *ok)
}

static void
pgsm_update_entry(pgsmEntry * entry,
pgsm_update_entry(pgsmEntry *entry,
const char *query,
char *comments,
int comments_len,
PlanInfo * plan_info,
SysInfo * sys_info,
ErrorInfo * error_info,
PlanInfo *plan_info,
SysInfo *sys_info,
ErrorInfo *error_info,
double plan_total_time,
double exec_total_time,
uint64 rows,
Expand All @@ -1439,7 +1439,10 @@ pgsm_update_entry(pgsmEntry * entry,
int sqlcode_len = error_info ? strlen(error_info->sqlcode) : 0;
int plan_text_len = plan_info ? plan_info->plan_len : 0;

/* Start collecting data for next bucket and reset all counters and timestamps */
/*
* Start collecting data for next bucket and reset all counters and
* timestamps
*/
if (reset)
{
memset(&entry->counters, 0, sizeof(Counters));
Expand All @@ -1449,7 +1452,7 @@ pgsm_update_entry(pgsmEntry * entry,

/* volatile block */
{
volatile pgsmEntry *e = (volatile pgsmEntry *) entry;
volatile pgsmEntry *e = (volatile pgsmEntry *) entry;

if (kind == PGSM_STORE)
SpinLockAcquire(&e->mutex);
Expand Down Expand Up @@ -1575,7 +1578,7 @@ pgsm_update_entry(pgsmEntry * entry,
}
else
{
Assert(!DsaPointerIsValid(e->counters.info.parent_query));
Assert(!DsaPointerIsValid(e->counters.info.parent_query));
}
}

Expand Down Expand Up @@ -1715,7 +1718,7 @@ pgsm_store_error(const char *query, ErrorData *edata)
}

static void
pgsm_add_to_list(pgsmEntry * entry, char *query_text, int query_len)
pgsm_add_to_list(pgsmEntry *entry, char *query_text, int query_len)
{
/* Switch to pgsm memory context */
MemoryContext oldctx = MemoryContextSwitchTo(GetPgsmMemoryContext());
Expand All @@ -1726,7 +1729,7 @@ pgsm_add_to_list(pgsmEntry * entry, char *query_text, int query_len)
}

static pgsmEntry *
pgsm_get_entry_for_query(uint64 queryid, PlanInfo * plan_info, const char *query_text, int query_len, bool create)
pgsm_get_entry_for_query(uint64 queryid, PlanInfo *plan_info, const char *query_text, int query_len, bool create)
{
pgsmEntry *entry = NULL;
ListCell *lc = NULL;
Expand Down Expand Up @@ -1784,7 +1787,7 @@ pgsm_cleanup_callback(void *arg)
* The bucket_id may not be known at this stage. So pass any value that you may wish.
*/
static pgsmEntry *
pgsm_create_hash_entry(uint64 bucket_id, uint64 queryid, PlanInfo * plan_info)
pgsm_create_hash_entry(uint64 bucket_id, uint64 queryid, PlanInfo *plan_info)
{
pgsmEntry *entry;
int sec_ctx;
Expand Down Expand Up @@ -1870,7 +1873,7 @@ pgsm_create_hash_entry(uint64 bucket_id, uint64 queryid, PlanInfo * plan_info)
* query string. total_time, rows, bufusage are ignored in this case.
*/
static void
pgsm_store(pgsmEntry * entry)
pgsm_store(pgsmEntry *entry)
{
pgsmEntry *shared_hash_entry;
pgsmSharedState *pgsm;
Expand Down Expand Up @@ -1946,8 +1949,8 @@ pgsm_store(pgsmEntry * entry)
memcpy(&jitusage.emission_counter, &entry->counters.jitinfo.instr_emission_counter, sizeof(instr_time));


// Update parent id if needed
if(pgsm_track == PGSM_TRACK_ALL && nesting_level > 0 && nesting_level < max_stack_depth)
/* Update parent id if needed */
if (pgsm_track == PGSM_TRACK_ALL && nesting_level > 0 && nesting_level < max_stack_depth)
{
entry->key.parentid = nested_queryids[nesting_level - 1];
}
Expand Down Expand Up @@ -2033,9 +2036,9 @@ pgsm_store(pgsmEntry * entry)
{
ereport(WARNING,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("[pg_stat_monitor] pgsm_store: Hash table is out of memory and can no longer store queries!"),
errdetail("You may reset the view or when the buckets are deallocated, pg_stat_monitor will resume saving " \
"queries. Alternatively, try increasing the value of pg_stat_monitor.pgsm_max.")));
errmsg("[pg_stat_monitor] pgsm_store: Hash table is out of memory and can no longer store queries!"),
errdetail("You may reset the view or when the buckets are deallocated, pg_stat_monitor will resume saving " \
"queries. Alternatively, try increasing the value of pg_stat_monitor.pgsm_max.")));
} PGSM_END_DISABLE_ERROR_CAPTURE();
}

Expand Down Expand Up @@ -2243,7 +2246,7 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
bool nulls[PG_STAT_MONITOR_COLS] = {0};
int i = 0;
Counters tmp;
pgsmHashKey tmpkey;
pgsmHashKey tmpkey;
double stddev;
uint64 queryid = entry->key.queryid;
int64 bucketid = entry->key.bucket_id;
Expand Down Expand Up @@ -2276,7 +2279,7 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,

/* copy counters to a local variable to keep locking time short */
{
volatile pgsmEntry *e = (volatile pgsmEntry *) entry;
volatile pgsmEntry *e = (volatile pgsmEntry *) entry;

SpinLockAcquire(&e->mutex);
tmp = e->counters;
Expand Down Expand Up @@ -2603,7 +2606,7 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
}

static uint64
get_next_wbucket(pgsmSharedState * pgsm)
get_next_wbucket(pgsmSharedState *pgsm)
{
struct timeval tv;
uint64 current_bucket_sec;
Expand Down
Loading

0 comments on commit 3bb6579

Please sign in to comment.