Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(clickhouse): release retained JSON UDF input rows (#96193)

Rory Shanks07c5f4c8468c
diff --git a/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main.go b/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main.go
index e6573059649f..b39e38085872 100644
--- a/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main.go
+++ b/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main.go
@@ -324,6 +324,16 @@ func (p *processor) recycle(v *value) {
}
v.s = ""
v.b = false
+ // Keep small arrays for reuse; bound clearing work for larger ones by this row's size.
+ if cap(v.entries) > max(16, 2*len(v.entries)) {
+ v.entries = nil
+ }
+ if cap(v.values) > max(16, 2*len(v.values)) {
+ v.values = nil
+ }
+ // Truncated entries can still hold borrowed keys that retain entire input rows.
+ clear(v.entries[:cap(v.entries)])
+ clear(v.values[:cap(v.values)])
v.entries = v.entries[:0]
v.values = v.values[:0]
p.free = append(p.free, v)
diff --git a/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main_test.go b/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main_test.go
index b6e92c3a67b9..11eea0ee65e8 100644
--- a/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main_test.go
+++ b/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main_test.go
@@ -5,8 +5,10 @@ import (
"fmt"
"os"
"path/filepath"
+ "runtime"
"strings"
"testing"
+ "weak"
)
func TestProcessLineCleansEventProperties(t *testing.T) {
@@ -253,6 +255,77 @@ func TestRunChunked(t *testing.T) {
}
}
+func TestProcessLineReleasesPreviousInput(t *testing.T) {
+ for _, kind := range []propertiesKind{eventProperties, personProperties, temporaryProperties} {
+ t.Run(fmt.Sprint(kind), func(t *testing.T) {
+ proc := processor{kind: kind}
+ var output bytes.Buffer
+ input := []byte(`{"$set":1,"discard":null}`)
+ previousInput := weak.Make(&input[0])
+ if err := proc.processLine(input, &output); err != nil {
+ t.Fatal(err)
+ }
+ input = nil
+ if err := proc.processLine([]byte(`{"$set":2}`), &output); err != nil {
+ t.Fatal(err)
+ }
+ runtime.GC()
+ if previousInput.Value() != nil {
+ t.Error("processor retains a previous input after garbage collection")
+ }
+ runtime.KeepAlive(&proc)
+ })
+ }
+}
+
+func TestProcessLineReleasesOversizedContainers(t *testing.T) {
+ var object strings.Builder
+ object.WriteByte('{')
+ for i := range 64 {
+ if i > 0 {
+ object.WriteByte(',')
+ }
+ fmt.Fprintf(&object, `"key%d":1`, i)
+ }
+ object.WriteByte('}')
+ for name, input := range map[string]string{
+ "object": object.String(),
+ "array": "[" + strings.Repeat("1,", 63) + "1]",
+ } {
+ t.Run(name, func(t *testing.T) {
+ var proc processor
+ var output bytes.Buffer
+ if err := proc.processLine([]byte(input), &output); err != nil {
+ t.Fatal(err)
+ }
+ var entries weak.Pointer[entry]
+ var values weak.Pointer[*value]
+ for _, v := range proc.free {
+ if cap(v.entries) >= 64 {
+ entries = weak.Make(&v.entries[:cap(v.entries)][0])
+ }
+ if cap(v.values) >= 64 {
+ values = weak.Make(&v.values[:cap(v.values)][0])
+ }
+ }
+ if entries.Value() == nil && values.Value() == nil {
+ t.Fatal("wide row did not retain a reusable container")
+ }
+ if err := proc.processLine([]byte(`{}`), &output); err != nil {
+ t.Fatal(err)
+ }
+ if output.String() != "{}" {
+ t.Fatalf("unexpected output: %s", output.String())
+ }
+ runtime.GC()
+ if entries.Value() != nil || values.Value() != nil {
+ t.Error("small row retains an oversized container after garbage collection")
+ }
+ runtime.KeepAlive(&proc)
+ })
+ }
+}
+
func TestShouldStringifyNumber(t *testing.T) {
tests := map[string]bool{
"18446744073709551615": false,
diff --git a/docs/internal/clickhouse-utility-udfs.md b/docs/internal/clickhouse-utility-udfs.md
index efba8425be65..e17d0f1fea0d 100644
--- a/docs/internal/clickhouse-utility-udfs.md
+++ b/docs/internal/clickhouse-utility-udfs.md
@@ -7,6 +7,10 @@ Flag values and person-property ordering follow the existing cleanup rules.
See [the utility UDF README](../../clickhouse-udfs/util/README.md) for build and integration-test commands.
+The event, person, and temporary cleaners reuse parser nodes across rows.
+Recycled nodes keep small backing arrays for reuse and release larger arrays whose capacity exceeds twice their used length, so a wide row does not make later small rows repeatedly clear oversized arrays.
+They clear references across the remaining backing arrays, including entries removed during cleanup, so borrowed property keys do not retain previously processed input rows.
+
### `JSONCleanPostHogTemporaryProperties(json)`
Accepts a JSON object and retains only the following top-level properties, including their dotted descendants. It uses the event cleaner's dotted-key expansion, null-object-field removal, duplicate handling, and integer protection, without coercing values to declared schema types. Non-object input fails.
diff --git a/posthog/user_scripts/json_clean_posthog_event_properties_udf_aarch64 b/posthog/user_scripts/json_clean_posthog_event_properties_udf_aarch64
index 68a0cdb10842..cf46caa7ebdb 100755
Binary files a/posthog/user_scripts/json_clean_posthog_event_properties_udf_aarch64 and b/posthog/user_scripts/json_clean_posthog_event_properties_udf_aarch64 differ
diff --git a/posthog/user_scripts/json_clean_posthog_event_properties_udf_x86_64 b/posthog/user_scripts/json_clean_posthog_event_properties_udf_x86_64
index a31aa371d252..c15af701705e 100755
Binary files a/posthog/user_scripts/json_clean_posthog_event_properties_udf_x86_64 and b/posthog/user_scripts/json_clean_posthog_event_properties_udf_x86_64 differ