From 4da2c4c88cc3a42ea04cefd9ebf70bffcbfd04e1 Mon Sep 17 00:00:00 2001
From: ChangAo Chen <cca5507@qq.com>
Date: Sun, 14 Dec 2025 20:19:16 +0800
Subject: [PATCH v1 3/3] Test pg_lfind8_nonzero().

---
 contrib/test_patch/Makefile            | 19 +++++++
 contrib/test_patch/test_patch--1.0.sql |  9 ++++
 contrib/test_patch/test_patch.c        | 71 ++++++++++++++++++++++++++
 contrib/test_patch/test_patch.control  |  3 ++
 4 files changed, 102 insertions(+)
 create mode 100644 contrib/test_patch/Makefile
 create mode 100644 contrib/test_patch/test_patch--1.0.sql
 create mode 100644 contrib/test_patch/test_patch.c
 create mode 100644 contrib/test_patch/test_patch.control

diff --git a/contrib/test_patch/Makefile b/contrib/test_patch/Makefile
new file mode 100644
index 00000000000..310157fdf84
--- /dev/null
+++ b/contrib/test_patch/Makefile
@@ -0,0 +1,19 @@
+MODULE_big = test_patch
+OBJS = \
+	$(WIN32RES) \
+	test_patch.o
+PGFILEDESC = "test_patch"
+
+EXTENSION = test_patch
+DATA = test_patch--1.0.sql
+
+ifdef USE_PGXS
+PG_CONFIG = pg_config
+PGXS := $(shell $(PG_CONFIG) --pgxs)
+include $(PGXS)
+else
+subdir = contrib/test_patch
+top_builddir = ../..
+include $(top_builddir)/src/Makefile.global
+include $(top_srcdir)/contrib/contrib-global.mk
+endif
diff --git a/contrib/test_patch/test_patch--1.0.sql b/contrib/test_patch/test_patch--1.0.sql
new file mode 100644
index 00000000000..0d062381a6e
--- /dev/null
+++ b/contrib/test_patch/test_patch--1.0.sql
@@ -0,0 +1,9 @@
+\echo Use "CREATE EXTENSION test_patch" to load this file. \quit
+
+CREATE FUNCTION test_head(natts int)
+	RETURNS pg_catalog.void
+	AS 'MODULE_PATHNAME' LANGUAGE C;
+
+CREATE FUNCTION test_patch(natts int)
+	RETURNS pg_catalog.void
+	AS 'MODULE_PATHNAME' LANGUAGE C;
diff --git a/contrib/test_patch/test_patch.c b/contrib/test_patch/test_patch.c
new file mode 100644
index 00000000000..5f3eea4895c
--- /dev/null
+++ b/contrib/test_patch/test_patch.c
@@ -0,0 +1,71 @@
+#include "postgres.h"
+
+#include "fmgr.h"
+#include "port/pg_lfind.h"
+#include "portability/instr_time.h"
+
+static bool isnull[2048];
+
+PG_MODULE_MAGIC;
+
+PG_FUNCTION_INFO_V1(test_head);
+Datum
+test_head(PG_FUNCTION_ARGS)
+{
+	int natts = PG_GETARG_INT32(0);
+    int loops = 1000;
+    bool hasnull = false;
+    int i;
+    instr_time start;
+    instr_time end;
+
+    INSTR_TIME_SET_CURRENT(start);
+
+    for (i = 0; i < loops; i++)
+    {
+        for (int j = 0; j < natts; j++)
+        {
+            if (isnull[j])
+            {
+                hasnull = true;
+                break;
+            }
+        }
+    }
+
+    INSTR_TIME_SET_CURRENT(end);
+
+    elog(INFO, "[HEAD] natts: %d, hasnull: %d, duration: %ld ns",
+         natts, hasnull,
+         INSTR_TIME_GET_NANOSEC(end) - INSTR_TIME_GET_NANOSEC(start));
+
+	PG_RETURN_VOID();
+}
+
+PG_FUNCTION_INFO_V1(test_patch);
+Datum
+test_patch(PG_FUNCTION_ARGS)
+{
+	int natts = PG_GETARG_INT32(0);
+    int loops = 1000;
+    bool hasnull = false;
+    int i;
+    instr_time start;
+    instr_time end;
+
+    INSTR_TIME_SET_CURRENT(start);
+
+    for (i = 0; i < loops; i++)
+    {
+        if (likely(natts > 0))
+            hasnull = pg_lfind8_nonzero((uint8 *) isnull, natts);
+    }
+
+    INSTR_TIME_SET_CURRENT(end);
+
+    elog(INFO, "[PATCH] natts: %d, hasnull: %d, duration: %ld ns",
+         natts, hasnull,
+         INSTR_TIME_GET_NANOSEC(end) - INSTR_TIME_GET_NANOSEC(start));
+
+	PG_RETURN_VOID();
+}
diff --git a/contrib/test_patch/test_patch.control b/contrib/test_patch/test_patch.control
new file mode 100644
index 00000000000..588092007ed
--- /dev/null
+++ b/contrib/test_patch/test_patch.control
@@ -0,0 +1,3 @@
+default_version = '1.0'
+module_pathname = '$libdir/test_patch'
+relocatable = true
-- 
2.52.0

