internal/postgres: refine trigger for symbol search

Refine the trigger that updates symbol_search_documents.imported_by_count
when search_documents is updated.

Only fire the trigger when search_documents.imported_by_count changes.

This change results in over 100x speedup for updating module import
counts, and about 44x speedup for updating module package counts (using
logic in a later CL).

For golang/go#81637.

Change-Id: Ic9f5be10dfa42f2ff254a9f0f57e23066a6a6964
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/836405
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
kokoro-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Ethan Lee <ethanalee@google.com>
diff --git a/migrations/000159_refine_imported_by_trigger.down.sql b/migrations/000159_refine_imported_by_trigger.down.sql
new file mode 100644
index 0000000..3fba463
--- /dev/null
+++ b/migrations/000159_refine_imported_by_trigger.down.sql
@@ -0,0 +1,12 @@
+-- Copyright 2026 The Go Authors. All rights reserved.
+-- Use of this source code is governed by a BSD-style
+-- license that can be found in the LICENSE file.
+
+BEGIN;
+
+CREATE OR REPLACE TRIGGER set_symbol_search_documents_imported_by_count
+AFTER INSERT OR UPDATE ON search_documents
+FOR EACH ROW
+EXECUTE FUNCTION trigger_modify_symbol_search_documents_imported_by_count();
+
+END;
diff --git a/migrations/000159_refine_imported_by_trigger.up.sql b/migrations/000159_refine_imported_by_trigger.up.sql
new file mode 100644
index 0000000..854c71c
--- /dev/null
+++ b/migrations/000159_refine_imported_by_trigger.up.sql
@@ -0,0 +1,13 @@
+-- Copyright 2026 The Go Authors. All rights reserved.
+-- Use of this source code is governed by a BSD-style
+-- license that can be found in the LICENSE file.
+
+BEGIN;
+
+CREATE OR REPLACE TRIGGER set_symbol_search_documents_imported_by_count
+AFTER UPDATE OF imported_by_count ON search_documents
+FOR EACH ROW
+WHEN (OLD.imported_by_count IS DISTINCT FROM NEW.imported_by_count)
+EXECUTE FUNCTION trigger_modify_symbol_search_documents_imported_by_count();
+
+END;