Skip to content

Commit

Permalink
Add new columns to pg_store_plans view
Browse files Browse the repository at this point in the history
Likewise pg_stat_statements, add temp_blk_read_time and
temp_blk_write_time to pg_store_plans view.
Version is changed to 1.7.
  • Loading branch information
horiguti committed Dec 14, 2022
1 parent a0594fa commit 55c2991
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 35 deletions.
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# pg_stat_plan/Makefile

MODULES = pg_store_plans
STOREPLANSVER = 1.6.1
STOREPLANSVER = 1.7

MODULE_big = pg_store_plans
OBJS = pg_store_plans.o pgsp_json.o pgsp_json_text.o pgsp_explain.o
Expand All @@ -10,7 +10,7 @@ EXTENSION = pg_store_plans

PG_VERSION := $(shell pg_config --version | sed "s/^PostgreSQL //" | sed "s/\.[0-9]*$$//")

DATA = pg_store_plans--1.6.sql pg_store_plans--1.6--1.6.1.sql
DATA = pg_store_plans--1.7.sql

REGRESS = convert store
REGRESS_OPTS = --temp-config=regress.conf
Expand All @@ -25,8 +25,8 @@ include $(top_builddir)/src/Makefile.global
include $(top_srcdir)/contrib/contrib-global.mk
endif

STARBALL14 = pg_store_plans14-$(STOREPLANSVER).tar.gz
STARBALLS = $(STARBALL14)
STARBALL15 = pg_store_plans15-$(STOREPLANSVER).tar.gz
STARBALLS = $(STARBALL15)

TARSOURCES = Makefile *.c *.h \
pg_store_plans--*.sql \
Expand All @@ -40,7 +40,7 @@ endif
## These entries need running server
DBNAME = postgres

rpms: rpm14
rpms: rpm15

$(STARBALLS): $(TARSOURCES)
if [ -h $(subst .tar.gz,,$@) ]; then rm $(subst .tar.gz,,$@); fi
Expand All @@ -52,8 +52,8 @@ $(STARBALLS): $(TARSOURCES)
tar -chzf $@ $(addprefix $(subst .tar.gz,,$@)/, $^)
rm $(subst .tar.gz,,$@)

rpm14: $(STARBALL14)
MAKE_ROOT=`pwd` rpmbuild -bb SPECS/pg_store_plans14.spec
rpm15: $(STARBALL15)
MAKE_ROOT=`pwd` rpmbuild -bb SPECS/pg_store_plans15.spec

testfiles: convert.out convert.sql

Expand Down
35 changes: 18 additions & 17 deletions SPECS/pg_store_plans14.spec → SPECS/pg_store_plans15.spec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPEC file for pg_store_plans
# Copyright(c) 2021, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
# Copyright(c) 2022, NIPPON TELEGRAPH AND TELEPHONE CORPORATION

%define _pgdir /usr/pgsql-14
%define _pgdir /usr/pgsql-15
%define _bindir %{_pgdir}/bin
%define _libdir %{_pgdir}/lib
%define _datadir %{_pgdir}/share
Expand All @@ -14,9 +14,9 @@
%endif

## Set general information for pg_store_plans.
Summary: Record executed plans on PostgreSQL 14
Name: pg_store_plans14
Version: 1.6.1
Summary: Record executed plans on PostgreSQL 15
Name: pg_store_plans15
Version: 1.7
Release: 1%{?dist}
License: BSD
Group: Applications/Databases
Expand All @@ -26,28 +26,28 @@ BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-%(%{__id_u} -n)
Vendor: NIPPON TELEGRAPH AND TELEPHONE CORPORATION

## We use postgresql-devel package
BuildRequires: postgresql14-devel
Requires: postgresql14-libs
BuildRequires: postgresql15-devel
Requires: postgresql15-libs

## Description for "pg_store_plans"
%description

pg_store_plans provides capability to record statistics for every plan
executed on PostgreSQL.

Note that this package is available for only PostgreSQL 14.
Note that this package is available for only PostgreSQL 15.

%package llvmjit
Requires: postgresql14-server, postgresql14-llvmjit
Requires: pg_store_plans14 = 1.6.1
Summary: Just-in-time compilation support for pg_store_plans14
Requires: postgresql15-server, postgresql15-llvmjit
Requires: pg_store_plans15 = 1.7
Summary: Just-in-time compilation support for pg_store_plans15

%description llvmjit
Just-in-time compilation support for pg_store_plans14
Just-in-time compilation support for pg_store_plans15

## pre work for build pg_store_plans
%prep
PATH=/usr/pgsql-14/bin:$PATH
PATH=/usr/pgsql-15/bin:$PATH
if [ "${MAKE_ROOT}" != "" ]; then
pushd ${MAKE_ROOT}
make clean %{name}-%{version}.tar.gz
Expand All @@ -58,14 +58,14 @@ if [ ! -d %{_rpmdir} ]; then mkdir -p %{_rpmdir}; fi

## Set variables for build environment
%build
PATH=/usr/pgsql-14/bin:$PATH
PATH=/usr/pgsql-15/bin:$PATH
pg_config
make USE_PGXS=1 %{?_smp_mflags}

## Set variables for install
%install
rm -rf %{buildroot}
PATH=/usr/pgsql-14/bin:$PATH
PATH=/usr/pgsql-15/bin:$PATH
make install DESTDIR=%{buildroot}

%clean
Expand All @@ -75,8 +75,7 @@ rm -rf %{buildroot}
%defattr(0755,root,root)
%{_libdir}/pg_store_plans.so
%defattr(0644,root,root)
%{_datadir}/extension/pg_store_plans--1.6.sql
%{_datadir}/extension/pg_store_plans--1.6--1.6.1.sql
%{_datadir}/extension/pg_store_plans--1.7.sql
%{_datadir}/extension/pg_store_plans.control

%files llvmjit
Expand All @@ -86,6 +85,8 @@ rm -rf %{buildroot}

# History of pg_store_plans.
%changelog
* Wed Dec 14 2022 Kyotaro Horiguchi
- Version 1.7. Support PostgreSQL 15.
* Thu Feb 03 2022 Kyotaro Horiguchi
- Version 1.6.1.
* Mon Jan 17 2022 Tatsuro Yamada, Julien Rouhaud, Kyotaro Horiguchi
Expand Down
12 changes: 12 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,18 @@ <H2 CLASS="SECT2">1. The <TT CLASS="STRUCTNAME">pg_store_plans</TT>

<TD>Total time the statement using the plan spent writing blocks, in milliseconds (if <A HREF="http://www.postgresql.org/docs/current/static/runtime-config-statistics.html#GUC-TRACK-IO-TIMING">track_io_timing</A> is enabled, otherwise zero)</TD>
</TR>
<TR><TD><TT CLASS="STRUCTFIELD">temp_blk_read_time</TT></TD>
<TD><TT CLASS="TYPE">double precision</TT></TD>
<TD>&nbsp;</TD>

<TD>Total time the statement using the plan spent reading temporary file blocks, in milliseconds (if <A HREF="http://www.postgresql.org/docs/current/static/runtime-config-statistics.html#GUC-TRACK-IO-TIMING">track_io_timing</A> is enabled, otherwise zero)</TD>
</TR>
<TR><TD><TT CLASS="STRUCTFIELD">temp_blk_write_time</TT></TD>
<TD><TT CLASS="TYPE">double precision</TT></TD>
<TD>&nbsp;</TD>

<TD>Total time the statement using the plan spent writing temporary file blocks, in milliseconds (if <A HREF="http://www.postgresql.org/docs/current/static/runtime-config-statistics.html#GUC-TRACK-IO-TIMING">track_io_timing</A> is enabled, otherwise zero)</TD>
</TR>
<TR><TD><TT CLASS="STRUCTFIELD">first_call</TT></TD>
<TD><TT CLASS="TYPE">timestamp with time zone</TT></TD>
<TD>&nbsp;</TD>
Expand Down
4 changes: 0 additions & 4 deletions pg_store_plans--1.6--1.6.1.sql

This file was deleted.

6 changes: 4 additions & 2 deletions pg_store_plans--1.6.sql → pg_store_plans--1.7.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* pg_store_plans/pg_store_plans--1.6.sql */
/* pg_store_plans/pg_store_plans--1.7.sql */

-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION pg_store_plans" to load this file. \quit
Expand Down Expand Up @@ -82,11 +82,13 @@ CREATE FUNCTION pg_store_plans(
OUT temp_blks_written int8,
OUT blk_read_time float8,
OUT blk_write_time float8,
OUT temp_blk_read_time float8,
OUT temp_blk_write_time float8,
OUT first_call timestamptz,
OUT last_call timestamptz
)
RETURNS SETOF record
AS 'MODULE_PATHNAME', 'pg_store_plans_1_6'
AS 'MODULE_PATHNAME', 'pg_store_plans_1_7'
LANGUAGE C
VOLATILE PARALLEL SAFE;

Expand Down
34 changes: 30 additions & 4 deletions pg_store_plans.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ PG_MODULE_MAGIC;
static const uint32 PGSP_PG_MAJOR_VERSION = PG_VERSION_NUM / 100;

/* This constant defines the magic number in the stats file header */
static const uint32 PGSP_FILE_HEADER = 0x20211125;
static const uint32 PGSP_FILE_HEADER = 0x20221214;
static int max_plan_len = 5000;

/* XXX: Should USAGE_EXEC reflect execution time and/or buffer usage? */
Expand All @@ -98,7 +98,8 @@ typedef uint32 queryid_t;
typedef enum pgspVersion
{
PGSP_V1_5 = 0,
PGSP_V1_6
PGSP_V1_6,
PGSP_V1_7
} pgspVersion;

/*
Expand Down Expand Up @@ -141,6 +142,10 @@ typedef struct Counters
int64 temp_blks_written; /* # of temp blocks written */
double blk_read_time; /* time spent reading, in msec */
double blk_write_time; /* time spent writing, in msec */
double temp_blk_read_time; /* time spent reading temp blocks,
in msec */
double temp_blk_write_time;/* time spent writing temp blocks,
in msec */
TimestampTz first_call; /* timestamp of first call */
TimestampTz last_call; /* timestamp of last call */
double usage; /* usage factor */
Expand Down Expand Up @@ -313,6 +318,7 @@ PG_FUNCTION_INFO_V1(pg_store_plans_reset);
PG_FUNCTION_INFO_V1(pg_store_plans_hash_query);
PG_FUNCTION_INFO_V1(pg_store_plans);
PG_FUNCTION_INFO_V1(pg_store_plans_1_6);
PG_FUNCTION_INFO_V1(pg_store_plans_1_7);
PG_FUNCTION_INFO_V1(pg_store_plans_shorten);
PG_FUNCTION_INFO_V1(pg_store_plans_normalize);
PG_FUNCTION_INFO_V1(pg_store_plans_jsonplan);
Expand Down Expand Up @@ -386,7 +392,6 @@ _PG_init(void)
*/
if (!process_shared_preload_libraries_in_progress)
return;

#if PG_VERSION_NUM >= 140000
/*
* Inform the postmaster that we want to enable query_id calculation if
Expand Down Expand Up @@ -1348,8 +1353,12 @@ pgsp_store(char *plan, queryid_t queryId,
e->counters.local_blks_written += bufusage->local_blks_written;
e->counters.temp_blks_read += bufusage->temp_blks_read;
e->counters.temp_blks_written += bufusage->temp_blks_written;

e->counters.blk_read_time += INSTR_TIME_GET_MILLISEC(bufusage->blk_read_time);
e->counters.blk_write_time += INSTR_TIME_GET_MILLISEC(bufusage->blk_write_time);
e->counters.temp_blk_read_time += INSTR_TIME_GET_MILLISEC(bufusage->temp_blk_read_time);
e->counters.temp_blk_write_time += INSTR_TIME_GET_MILLISEC(bufusage->temp_blk_write_time);

e->counters.last_call = GetCurrentTimestamp();
e->counters.usage += USAGE_EXEC(total_time);

Expand Down Expand Up @@ -1380,11 +1389,20 @@ pg_store_plans_reset(PG_FUNCTION_ARGS)
/* Number of output arguments (columns) for various API versions */
#define PG_STORE_PLANS_COLS_V1_5 27
#define PG_STORE_PLANS_COLS_V1_6 26
#define PG_STORE_PLANS_COLS 27 /* maximum of above */
#define PG_STORE_PLANS_COLS_V1_7 28
#define PG_STORE_PLANS_COLS 28 /* maximum of above */

/*
* Retrieve statement statistics.
*/
Datum
pg_store_plans_1_7(PG_FUNCTION_ARGS)
{
pg_store_plans_internal(fcinfo, PGSP_V1_7);

return (Datum) 0;
}

Datum
pg_store_plans_1_6(PG_FUNCTION_ARGS)
{
Expand Down Expand Up @@ -1637,11 +1655,19 @@ pg_store_plans_internal(FunctionCallInfo fcinfo,
values[i++] = Int64GetDatumFast(tmp.temp_blks_written);
values[i++] = Float8GetDatumFast(tmp.blk_read_time);
values[i++] = Float8GetDatumFast(tmp.blk_write_time);

if (api_version >= PGSP_V1_7)
{
values[i++] = Float8GetDatumFast(tmp.temp_blk_read_time);
values[i++] = Float8GetDatumFast(tmp.temp_blk_write_time);
}

values[i++] = TimestampTzGetDatum(tmp.first_call);
values[i++] = TimestampTzGetDatum(tmp.last_call);

Assert(i == (api_version == PGSP_V1_5 ? PG_STORE_PLANS_COLS_V1_5 :
api_version == PGSP_V1_6 ? PG_STORE_PLANS_COLS_V1_6 :
api_version == PGSP_V1_7 ? PG_STORE_PLANS_COLS_V1_7 :
-1 /* fail if you forget to update this assert */ ));

tuplestore_putvalues(tupstore, tupdesc, values, nulls);
Expand Down
2 changes: 1 addition & 1 deletion pg_store_plans.control
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# pg_store_plans extension
comment = 'track plan statistics of all SQL statements executed'
default_version = '1.6.1'
default_version = '1.7'
module_pathname = '$libdir/pg_store_plans'
relocatable = true

0 comments on commit 55c2991

Please sign in to comment.