From df54dee594b12ffd6a6e7dd8f05969e712dbcefc Mon Sep 17 00:00:00 2001
From: Lukas Fittl <lukas@fittl.com>
Date: Thu, 12 Feb 2026 01:09:48 -0800
Subject: [PATCH v7 3/4] Timing: Use Time-Stamp Counter (TSC) on x86-64 for
 faster measurements

This allows the direct use of the Time-Stamp Counter (TSC) value retrieved
from the CPU using RDTSC/RDTSC instructions, instead of APIs like
clock_gettime() on POSIX systems. This reduces the overhead of EXPLAIN with
ANALYZE and TIMING ON. Tests showed that runtime when instrumented can be
reduced by up to 10% for queries moving lots of rows through the plan.

To control use of the TSC, the new "timing_clock_source" GUC is introduced,
whose default ("auto") automatically uses the TSC when running on Linux/x86-64,
in case the system clocksource is reported as "tsc". The use of the system
APIs can be enforced by setting "system", or on x86-64 architectures the
use of TSC can be enforced by explicitly setting "tsc".

Note, that we further split the use of the TSC into the RDTSC CPU instruction
which does not wait for out-of-order execution (faster, less precise)
and the RDTSCP instruction, which waits for outstanding instructions to
retire. RDTSCP is deemed to have little benefit in the typical
InstrStartNode() / InstrStopNode() use case of EXPLAIN, and can be up to
twice as slow. To separate these use cases, the new macro
INSTR_TIME_SET_CURRENT_FAST() is introduced, which uses RDTSC.

The original macro INSTR_TIME_SET_CURRENT() uses RDTSCP and is supposed
to be used when precision is more important than performance. When the
system timing clock source is used both of these macros instead utilize
the system APIs (clock_gettime / QueryPerformanceCounter) like before.

Author: David Geier <geidav.pg@gmail.com>
Author: Andres Freund <andres@anarazel.de>
Author: Lukas Fittl <lukas@fittl.com>
Reviewed-by:
Discussion: https://www.postgresql.org/message-id/flat/20200612232810.f46nbqkdhbutzqdg%40alap3.anarazel.de
---
 doc/src/sgml/config.sgml                      |  55 ++++
 src/backend/executor/instrument.c             |  12 +-
 src/backend/postmaster/postmaster.c           |   3 -
 src/backend/utils/misc/guc_parameters.dat     |  11 +
 src/backend/utils/misc/guc_tables.c           |  11 +
 src/backend/utils/misc/postgresql.conf.sample |   4 +
 src/common/instr_time.c                       | 293 +++++++++++++++++-
 src/include/portability/instr_time.h          | 109 ++++++-
 src/include/utils/guc_hooks.h                 |   3 +
 src/include/utils/guc_tables.h                |   1 +
 10 files changed, 475 insertions(+), 27 deletions(-)

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index 6bc2690ce07..a23f4e11b44 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -2523,6 +2523,61 @@ include_dir 'conf.d'
      </variablelist>
     </sect2>
 
+    <sect2 id="runtime-config-resource-time">
+     <title>Timing</title>
+
+     <variablelist>
+     <varlistentry id="guc-timing-clock-source" xreflabel="timing_clock_source">
+      <term><varname>timing_clock_source</varname> (<type>enum</type>)
+      <indexterm>
+       <primary><varname>timing_clock_source</varname> configuration parameter</primary>
+      </indexterm>
+      </term>
+      <listitem>
+       <para>
+        Selects the method for making timing measurements using the OS or specialized CPU
+        instructions. Possible values are:
+         <itemizedlist>
+          <listitem>
+           <para>
+            <literal>auto</literal> (automatically chooses TSC clock source for Linux-based
+            x86-64 systems that utilize "tsc" as their system clock source, otherwise uses
+            the OS system clock)
+           </para>
+          </listitem>
+          <listitem>
+           <para>
+            <literal>system</literal> (measures timing using the OS system clock)
+           </para>
+          </listitem>
+          <listitem>
+           <para>
+            <literal>tsc</literal> (measures timing using the x86-64 Time-Stamp Counter (TSC)
+            by directly executing RDTSC/RDTSCP instructions, see below)
+           </para>
+          </listitem>
+         </itemizedlist>
+         The default is <literal>auto</literal>.
+        </para>
+        <para>
+          If enabled, the TSC clock source will use the RDTSC instruction for the x86-64
+          Time-Stamp Counter (TSC) to perform certain time measurements, for example during
+          EXPLAIN ANALYZE. The RDTSC instruction has less overhead than going through the OS
+          clock source, which for an EXPLAIN ANALYZE statement will show timing closer to the
+          actual runtime when timing is off. For timings that require higher precision the
+          RDTSCP instruction is used, which avoids inaccuracies due to CPU instruction re-ordering.
+          Use of RDTSC/RDTSC is not supported on Windows or on other architectures, and is not
+          advised on systems that utilize an emulated TSC.
+        </para>
+        <para>
+          To help decide which clock source to use on an x86-64 system you can run the
+          <application>pg_test_timing</application> utility to check TSC availability, and
+          perform timing measurements.
+        </para>
+      </listitem>
+     </varlistentry>
+     </variablelist>
+    </sect2>
 
     <sect2 id="runtime-config-resource-background-writer">
      <title>Background Writer</title>
diff --git a/src/backend/executor/instrument.c b/src/backend/executor/instrument.c
index edab92a0ebe..ebdad31ca3b 100644
--- a/src/backend/executor/instrument.c
+++ b/src/backend/executor/instrument.c
@@ -67,9 +67,13 @@ InstrInit(Instrumentation *instr, int instrument_options)
 void
 InstrStartNode(Instrumentation *instr)
 {
-	if (instr->need_timer &&
-		!INSTR_TIME_SET_CURRENT_LAZY(instr->starttime))
-		elog(ERROR, "InstrStartNode called twice in a row");
+	if (instr->need_timer)
+	{
+		if (!INSTR_TIME_IS_ZERO(instr->starttime))
+			elog(ERROR, "InstrStartNode called twice in a row");
+		else
+			INSTR_TIME_SET_CURRENT_FAST(instr->starttime);
+	}
 
 	/* save buffer usage totals at node entry, if needed */
 	if (instr->need_bufusage)
@@ -95,7 +99,7 @@ InstrStopNode(Instrumentation *instr, double nTuples)
 		if (INSTR_TIME_IS_ZERO(instr->starttime))
 			elog(ERROR, "InstrStopNode called without start");
 
-		INSTR_TIME_SET_CURRENT(endtime);
+		INSTR_TIME_SET_CURRENT_FAST(endtime);
 		INSTR_TIME_ACCUM_DIFF(instr->counter, endtime, instr->starttime);
 
 		INSTR_TIME_SET_ZERO(instr->starttime);
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 0ee2e67a30a..d6133bfebc6 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -588,9 +588,6 @@ PostmasterMain(int argc, char *argv[])
 	 */
 	InitializeGUCOptions();
 
-	/* initialize timing infrastructure (required for INSTR_* calls) */
-	pg_initialize_timing();
-
 	opterr = 1;
 
 	/*
diff --git a/src/backend/utils/misc/guc_parameters.dat b/src/backend/utils/misc/guc_parameters.dat
index 271c033952e..7828c8dcbf5 100644
--- a/src/backend/utils/misc/guc_parameters.dat
+++ b/src/backend/utils/misc/guc_parameters.dat
@@ -2988,6 +2988,17 @@
   assign_hook => 'assign_timezone_abbreviations',
 },
 
+{ name => 'timing_clock_source', type => 'enum', context => 'PGC_USERSET', group => 'RESOURCES_TIME',
+  short_desc => 'Controls the clock source used for collecting timing measurements.',
+  long_desc => 'This enables the use of specialized clock sources, specifically the RDTSC clock source on x86-64 systems (if available), to support timing measurements with lower overhead during EXPLAIN and other instrumentation.',
+  variable => 'timing_clock_source',
+  boot_val => 'TIMING_CLOCK_SOURCE_AUTO',
+  options => 'timing_clock_source_options',
+  check_hook => 'check_timing_clock_source',
+  assign_hook => 'assign_timing_clock_source',
+  show_hook => 'show_timing_clock_source',
+},
+
 { name => 'trace_connection_negotiation', type => 'bool', context => 'PGC_POSTMASTER', group => 'DEVELOPER_OPTIONS',
   short_desc => 'Logs details of pre-authentication connection handshake.',
   flags => 'GUC_NOT_IN_SAMPLE',
diff --git a/src/backend/utils/misc/guc_tables.c b/src/backend/utils/misc/guc_tables.c
index 741fce8dede..241dcb2810d 100644
--- a/src/backend/utils/misc/guc_tables.c
+++ b/src/backend/utils/misc/guc_tables.c
@@ -91,6 +91,7 @@
 #include "tcop/tcopprot.h"
 #include "tsearch/ts_cache.h"
 #include "utils/builtins.h"
+#include "portability/instr_time.h"
 #include "utils/bytea.h"
 #include "utils/float.h"
 #include "utils/guc_hooks.h"
@@ -372,6 +373,15 @@ static const struct config_enum_entry huge_pages_options[] = {
 	{NULL, 0, false}
 };
 
+static const struct config_enum_entry timing_clock_source_options[] = {
+	{"auto", TIMING_CLOCK_SOURCE_AUTO, false},
+	{"system", TIMING_CLOCK_SOURCE_SYSTEM, false},
+#if defined(__x86_64__)
+	{"tsc", TIMING_CLOCK_SOURCE_TSC, false},
+#endif
+	{NULL, 0, false}
+};
+
 static const struct config_enum_entry huge_pages_status_options[] = {
 	{"off", HUGE_PAGES_OFF, false},
 	{"on", HUGE_PAGES_ON, false},
@@ -722,6 +732,7 @@ const char *const config_group_names[] =
 	[CONN_AUTH_TCP] = gettext_noop("Connections and Authentication / TCP Settings"),
 	[CONN_AUTH_AUTH] = gettext_noop("Connections and Authentication / Authentication"),
 	[CONN_AUTH_SSL] = gettext_noop("Connections and Authentication / SSL"),
+	[RESOURCES_TIME] = gettext_noop("Resource Usage / Time"),
 	[RESOURCES_MEM] = gettext_noop("Resource Usage / Memory"),
 	[RESOURCES_DISK] = gettext_noop("Resource Usage / Disk"),
 	[RESOURCES_KERNEL] = gettext_noop("Resource Usage / Kernel Resources"),
diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample
index f938cc65a3a..c11d88348f0 100644
--- a/src/backend/utils/misc/postgresql.conf.sample
+++ b/src/backend/utils/misc/postgresql.conf.sample
@@ -193,6 +193,10 @@
 #max_files_per_process = 1000           # min 64
                                         # (change requires restart)
 
+# - Time -
+
+#timing_clock_source = auto             # auto, system, tsc (if supported)
+
 # - Background Writer -
 
 #bgwriter_delay = 200ms                 # 10-10000ms between rounds
diff --git a/src/common/instr_time.c b/src/common/instr_time.c
index 3e8ee2d6db2..dbb70305bec 100644
--- a/src/common/instr_time.c
+++ b/src/common/instr_time.c
@@ -20,8 +20,8 @@
  * Stores what the number of ticks needs to be multiplied with to end up
  * with nanoseconds using integer math.
  *
- * On certain platforms (currently Windows) the ticks to nanoseconds conversion
- * requires floating point math because:
+ * In certain cases (TSC on x86-64, and QueryPerformanceCounter on Windows)
+ * the ticks to nanoseconds conversion requires floating point math because:
  *
  * sec = ticks / frequency_hz
  * ns  = ticks / frequency_hz * 1,000,000,000
@@ -39,7 +39,7 @@
  * division. We utilize unsigned integers even though ticks are stored as a
  * signed value because that encourages compilers to generate better assembly.
  *
- * On all other platforms we are using clock_gettime(), which uses nanoseconds
+ * In all other cases we are using clock_gettime(), which uses nanoseconds
  * as ticks. Hence, we set the multiplier to zero, which causes pg_ticks_to_ns
  * to return the original value.
  */
@@ -48,16 +48,57 @@ uint64		max_ticks_no_overflow = 0;
 
 static void set_ticks_per_ns(void);
 
+int			timing_clock_source = TIMING_CLOCK_SOURCE_AUTO;
+
+#if defined(__x86_64__)
+/* Indicates if TSC instructions (RDTSC and RDTSCP) are usable. */
+extern bool has_usable_tsc;
+
+static void tsc_initialize(void);
+static bool tsc_use_by_default(void);
+static void set_ticks_per_ns_for_tsc(void);
+static bool set_tsc_frequency_khz(void);
+static bool is_rdtscp_available(void);
+#endif
+
 void
 pg_initialize_timing()
 {
+#if defined(__x86_64__)
+	tsc_initialize();
+#endif
+
+	set_ticks_per_ns();
+}
+
+bool
+pg_set_timing_clock_source(TimingClockSourceType source)
+{
+#if defined(__x86_64__)
+	switch (source)
+	{
+		case TIMING_CLOCK_SOURCE_AUTO:
+			use_tsc = has_usable_tsc && tsc_use_by_default();
+			break;
+		case TIMING_CLOCK_SOURCE_SYSTEM:
+			use_tsc = false;
+			break;
+		case TIMING_CLOCK_SOURCE_TSC:
+			if (!has_usable_tsc)	/* Tell caller TSC is not usable */
+				return false;
+			use_tsc = true;
+			break;
+	}
+#endif
 	set_ticks_per_ns();
+	timing_clock_source = source;
+	return true;
 }
 
 #ifndef WIN32
 
 static void
-set_ticks_per_ns()
+set_ticks_per_ns_system()
 {
 	ticks_per_ns_scaled = 0;
 	max_ticks_no_overflow = 0;
@@ -76,10 +117,252 @@ GetTimerFrequency(void)
 }
 
 static void
-set_ticks_per_ns()
+set_ticks_per_ns_system()
 {
 	ticks_per_ns_scaled = INT64CONST(1000000000) * TICKS_TO_NS_PRECISION / GetTimerFrequency();
 	max_ticks_no_overflow = PG_INT64_MAX / ticks_per_ns_scaled;
 }
 
 #endif							/* WIN32 */
+
+static void
+set_ticks_per_ns()
+{
+#if defined(__x86_64__)
+	if (use_tsc)
+		set_ticks_per_ns_for_tsc();
+	else
+		set_ticks_per_ns_system();
+#else
+	set_ticks_per_ns_system();
+#endif
+}
+
+/* GUC handling */
+
+#ifndef FRONTEND
+
+#include "utils/guc_hooks.h"
+
+bool
+check_timing_clock_source(int *newval, void **extra, GucSource source)
+{
+#if defined(__x86_64__)
+	pg_initialize_timing();
+
+	if (*newval == TIMING_CLOCK_SOURCE_TSC && !has_usable_tsc)
+	{
+		GUC_check_errdetail("TSC is not supported as fast clock source");
+		return false;
+	}
+#endif
+
+	return true;
+}
+
+void
+assign_timing_clock_source(int newval, void *extra)
+{
+	/*
+	 * Ignore the return code since the check hook already verified TSC is
+	 * usable if its explicitly requested
+	 */
+	pg_set_timing_clock_source(newval);
+}
+
+const char *
+show_timing_clock_source()
+{
+#if defined(__x86_64__)
+	TimingClockSourceType effective_source = pg_current_timing_clock_source();
+
+	switch (timing_clock_source)
+	{
+		case TIMING_CLOCK_SOURCE_AUTO:
+			if (effective_source == TIMING_CLOCK_SOURCE_TSC)
+				return "auto (tsc)";
+			else
+				return "auto (system)";
+		case TIMING_CLOCK_SOURCE_SYSTEM:
+			return "system";
+		case TIMING_CLOCK_SOURCE_TSC:
+			return "tsc";
+	}
+#else
+	switch (timing_clock_source)
+	{
+		case TIMING_CLOCK_SOURCE_AUTO:
+			return "auto (system)";
+		case TIMING_CLOCK_SOURCE_SYSTEM:
+			return "system";
+	}
+#endif
+
+	/* unreachable */
+	return "?";
+}
+
+#endif							/* !FRONTEND */
+
+/* TSC specific logic */
+
+#if defined(__x86_64__)
+
+#if defined(HAVE__GET_CPUID) || (defined(HAVE__CPUIDEX) && !defined(_MSC_VER))
+#include <cpuid.h>
+#endif
+
+#if defined(HAVE__CPUID) || (defined(HAVE__CPUIDEX) && defined(_MSC_VER))
+#include <intrin.h>
+#endif
+
+bool		has_usable_tsc = false;
+bool		use_tsc = false;
+
+static uint32 tsc_frequency_khz = 0;
+
+/*
+ * Decide whether we use the RDTSC/RDTSCP instructions at runtime, for Linux/x86-64,
+ * instead of incurring the overhead of a full clock_gettime() call.
+ *
+ * This can't be reliably determined at compile time, since the
+ * availability of an "invariant" TSC (that is not affected by CPU
+ * frequency changes) is dependent on the CPU architecture. Additionally,
+ * there are cases where TSC availability is impacted by virtualization,
+ * where a simple cpuid feature check would not be enough.
+ */
+static void
+tsc_initialize(void)
+{
+	/*
+	 * Compute baseline CPU peformance, determines speed at which the TSC
+	 * advances.
+	 */
+	if (!set_tsc_frequency_khz())
+		return;
+
+	has_usable_tsc = is_rdtscp_available();
+}
+
+/*
+ * Decides whether to use TSC clock source if the user did not specify it
+ * one way or the other, and it is available (checked separately).
+ *
+ * Currently only enabled by default on Linux, since Linux already does a
+ * significant amount of work to determine whether TSC is a viable clock
+ * source.
+ */
+static bool
+tsc_use_by_default()
+{
+#if defined(__linux__)
+	FILE	   *fp = fopen("/sys/devices/system/clocksource/clocksource0/current_clocksource", "r");
+	char		buf[128];
+
+	if (!fp)
+		return false;
+
+	if (fgets(buf, sizeof(buf), fp) != NULL && strcmp(buf, "tsc\n") == 0)
+	{
+		fclose(fp);
+		return true;
+	}
+
+	fclose(fp);
+#endif
+
+	return false;
+}
+
+static void
+set_ticks_per_ns_for_tsc()
+{
+	ticks_per_ns_scaled = INT64CONST(1000000) * TICKS_TO_NS_PRECISION / tsc_frequency_khz;
+	max_ticks_no_overflow = PG_INT64_MAX / ticks_per_ns_scaled;
+}
+
+#define CPUID_HYPERVISOR_VMWARE(words) (words[1] == 0x61774d56 && words[2] == 0x4d566572 && words[3] == 0x65726177) /* VMwareVMware */
+#define CPUID_HYPERVISOR_KVM(words) (words[1] == 0x4b4d564b && words[2] == 0x564b4d56 && words[3] == 0x0000004d)	/* KVMKVMKVM */
+
+static bool
+set_tsc_frequency_khz()
+{
+	uint32		r[4] = {0, 0, 0, 0};
+
+#if defined(HAVE__GET_CPUID)
+	__get_cpuid(0x15, &r[0] /* denominator */ , &r[1] /* numerator */ , &r[2] /* hz */ , &r[3]);
+#elif defined(HAVE__CPUID)
+	__cpuid(r, 0x15);
+#else
+#error cpuid instruction not available
+#endif
+
+	if (r[2] > 0)
+	{
+		if (r[0] == 0 || r[1] == 0)
+			return false;
+
+		tsc_frequency_khz = r[2] / 1000 * r[1] / r[0];
+		return true;
+	}
+
+	/* Some CPUs only report frequency in 16H */
+
+#if defined(HAVE__GET_CPUID)
+	__get_cpuid(0x16, &r[0] /* base_mhz */ , &r[1], &r[2], &r[3]);
+#elif defined(HAVE__CPUID)
+	__cpuid(r, 0x16);
+#else
+#error cpuid instruction not available
+#endif
+
+	if (r[0] > 0)
+	{
+		tsc_frequency_khz = r[0] * 1000;
+		return true;
+	}
+
+	/*
+	 * Check if we have a KVM or VMware Hypervisor passing down TSC frequency
+	 * to us in a guest VM
+	 *
+	 * Note that accessing the 0x40000000 leaf for Hypervisor info requires
+	 * use of __cpuidex to set ECX to 0. The similar __get_cpuid_count
+	 * function does not work as expected since it contains a check for
+	 * __get_cpuid_max, which has been observed to be lower than the special
+	 * Hypervisor leaf.
+	 */
+#if defined(HAVE__CPUIDEX)
+	__cpuidex((int32 *) r, 0x40000000, 0);
+	if (r[0] >= 0x40000010 && (CPUID_HYPERVISOR_VMWARE(r) || CPUID_HYPERVISOR_KVM(r)))
+	{
+		__cpuidex((int32 *) r, 0x40000010, 0);
+		if (r[0] > 0)
+		{
+			tsc_frequency_khz = r[0];
+			return true;
+		}
+	}
+#endif
+
+	return false;
+}
+
+static bool
+is_rdtscp_available()
+{
+	uint32		r[4] = {0, 0, 0, 0};
+
+#if defined(HAVE__GET_CPUID)
+	if (!__get_cpuid(0x80000001, &r[0], &r[1], &r[2], &r[3]))
+		return false;
+#elif defined(HAVE__CPUID)
+	__cpuid(r, 0x80000001);
+#else
+#error cpuid instruction not available
+#endif
+
+	return (r[3] & (1 << 27)) != 0;
+}
+
+#endif							/* defined(__x86_64__) */
diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index f8145ae2af7..14d0cad518a 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -4,9 +4,10 @@
  *	  portable high-precision interval timing
  *
  * This file provides an abstraction layer to hide portability issues in
- * interval timing.  On Unix we use clock_gettime(), and on Windows we use
- * QueryPerformanceCounter().  These macros also give some breathing room to
- * use other high-precision-timing APIs.
+ * interval timing. On x86 we use the RDTSC/RDTSCP instruction directly in
+ * certain cases, or alternatively clock_gettime() on Unix-like systems and
+ * QueryPerformanceCounter() on Windows. These macros also give some breathing
+ * room to use other high-precision-timing APIs.
  *
  * The basic data type is instr_time, which all callers should treat as an
  * opaque typedef.  instr_time can store either an absolute time (of
@@ -17,10 +18,11 @@
  *
  * INSTR_TIME_SET_ZERO(t)			set t to zero (memset is acceptable too)
  *
- * INSTR_TIME_SET_CURRENT(t)		set t to current time
+ * INSTR_TIME_SET_CURRENT_FAST(t)	set t to current time without waiting
+ * 									for instructions in out-of-order window
  *
- * INSTR_TIME_SET_CURRENT_LAZY(t)	set t to current time if t is zero,
- *									evaluates to whether t changed
+ * INSTR_TIME_SET_CURRENT(t)		set t to current time while waiting for
+ * 									instructions in OOO to retire
  *
  * INSTR_TIME_ADD(x, y)				x += y
  *
@@ -93,13 +95,54 @@ typedef struct instr_time
 extern PGDLLIMPORT uint64 ticks_per_ns_scaled;
 extern PGDLLIMPORT uint64 max_ticks_no_overflow;
 
+#if defined(__x86_64__)
+#include <x86intrin.h>
+
+/* Whether to actually use RDTSC/RDTSCP based on availability and GUC settings. */
+extern PGDLLIMPORT bool use_tsc;
+#endif
+
+typedef enum
+{
+	TIMING_CLOCK_SOURCE_AUTO,
+	TIMING_CLOCK_SOURCE_SYSTEM,
+	TIMING_CLOCK_SOURCE_TSC
+}			TimingClockSourceType;
+
+extern int	timing_clock_source;
+
+/*
+ * Returns the current timing clock source effectively in use, resolving
+ * TIMING_CLOCK_SOURCE_AUTO to either TIMING_CLOCK_SOURCE_SYSTEM or
+ * TIMING_CLOCK_SOURCE_TSC.
+ */
+static inline TimingClockSourceType pg_current_timing_clock_source(void)
+{
+#if defined(__x86_64__)
+	return use_tsc ? TIMING_CLOCK_SOURCE_TSC : TIMING_CLOCK_SOURCE_SYSTEM;
+#else
+	return TIMING_CLOCK_SOURCE_SYSTEM;
+#endif
+}
+
 /*
  * Initialize timing infrastructure
  *
- * This must be called at least once before using INSTR_TIME_SET_CURRENT* macros.
+ * This must be called at least once by frontend programs before using
+ * INSTR_TIME_SET_CURRENT* macros. Backend programs automatically initialize
+ * this through the GUC check hook.
  */
 extern void pg_initialize_timing(void);
 
+/*
+ * Sets the time source to be used. Mainly intended for frontend programs,
+ * the backend should set it via the timing_clock_source GUC instead.
+ *
+ * Returns false if the clock source could not be set, for example when TSC
+ * is not available despite being explicitly set.
+ */
+extern bool pg_set_timing_clock_source(TimingClockSourceType source);
+
 #ifndef WIN32
 
 /* On POSIX, use clock_gettime() for system clock source */
@@ -117,22 +160,25 @@ extern void pg_initialize_timing(void);
  * than CLOCK_MONOTONIC.  In particular, as of macOS 10.12, Apple provides
  * CLOCK_MONOTONIC_RAW which is both faster to read and higher resolution than
  * their version of CLOCK_MONOTONIC.
+ *
+ * Note this does not get used in case the TSC clock source logic is used,
+ * which directly calls architecture specific timing instructions (e.g. RDTSC).
  */
 #if defined(__darwin__) && defined(CLOCK_MONOTONIC_RAW)
-#define PG_INSTR_CLOCK	CLOCK_MONOTONIC_RAW
+#define PG_INSTR_SYSTEM_CLOCK	CLOCK_MONOTONIC_RAW
 #elif defined(CLOCK_MONOTONIC)
-#define PG_INSTR_CLOCK	CLOCK_MONOTONIC
+#define PG_INSTR_SYSTEM_CLOCK	CLOCK_MONOTONIC
 #else
-#define PG_INSTR_CLOCK	CLOCK_REALTIME
+#define PG_INSTR_SYSTEM_CLOCK	CLOCK_REALTIME
 #endif
 
 static inline instr_time
-pg_get_ticks(void)
+pg_get_ticks_system(void)
 {
 	instr_time	now;
 	struct timespec tmp;
 
-	clock_gettime(PG_INSTR_CLOCK, &tmp);
+	clock_gettime(PG_INSTR_SYSTEM_CLOCK, &tmp);
 	now.ticks = tmp.tv_sec * NS_PER_S + tmp.tv_nsec;
 
 	return now;
@@ -143,7 +189,7 @@ pg_get_ticks(void)
 /* On Windows, use QueryPerformanceCounter() for system clock source */
 
 static inline instr_time
-pg_get_ticks(void)
+pg_get_ticks_system(void)
 {
 	instr_time	now;
 	LARGE_INTEGER tmp;
@@ -199,6 +245,39 @@ pg_ticks_to_ns(int64 ticks)
 #endif
 }
 
+static inline instr_time
+pg_get_ticks_fast(void)
+{
+#if defined(__x86_64__)
+	if (likely(use_tsc))
+	{
+		instr_time	now;
+
+		now.ticks = __rdtsc();
+		return now;
+	}
+#endif
+
+	return pg_get_ticks_system();
+}
+
+static inline instr_time
+pg_get_ticks(void)
+{
+#if defined(__x86_64__)
+	if (likely(use_tsc))
+	{
+		instr_time	now;
+		uint32		unused;
+
+		now.ticks = __rdtscp(&unused);
+		return now;
+	}
+#endif
+
+	return pg_get_ticks_system();
+}
+
 /*
  * Common macros
  */
@@ -207,8 +286,8 @@ pg_ticks_to_ns(int64 ticks)
 
 #define INSTR_TIME_SET_ZERO(t)	((t).ticks = 0)
 
-#define INSTR_TIME_SET_CURRENT_LAZY(t) \
-	(INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)
+#define INSTR_TIME_SET_CURRENT_FAST(t) \
+	((t) = pg_get_ticks_fast())
 
 #define INSTR_TIME_SET_CURRENT(t) \
 	((t) = pg_get_ticks())
diff --git a/src/include/utils/guc_hooks.h b/src/include/utils/guc_hooks.h
index 9c90670d9b8..a396e746415 100644
--- a/src/include/utils/guc_hooks.h
+++ b/src/include/utils/guc_hooks.h
@@ -162,6 +162,9 @@ extern const char *show_timezone(void);
 extern bool check_timezone_abbreviations(char **newval, void **extra,
 										 GucSource source);
 extern void assign_timezone_abbreviations(const char *newval, void *extra);
+extern void assign_timing_clock_source(int newval, void *extra);
+extern bool check_timing_clock_source(int *newval, void **extra, GucSource source);
+extern const char *show_timing_clock_source(void);
 extern bool check_transaction_buffers(int *newval, void **extra, GucSource source);
 extern bool check_transaction_deferrable(bool *newval, void **extra, GucSource source);
 extern bool check_transaction_isolation(int *newval, void **extra, GucSource source);
diff --git a/src/include/utils/guc_tables.h b/src/include/utils/guc_tables.h
index 71a80161961..63440b8e36c 100644
--- a/src/include/utils/guc_tables.h
+++ b/src/include/utils/guc_tables.h
@@ -60,6 +60,7 @@ enum config_group
 	CONN_AUTH_TCP,
 	CONN_AUTH_AUTH,
 	CONN_AUTH_SSL,
+	RESOURCES_TIME,
 	RESOURCES_MEM,
 	RESOURCES_DISK,
 	RESOURCES_KERNEL,
-- 
2.47.1

