Re: pg_upgrade: backslash escaping in check_for_unicode_update()

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: Xueyu Gao <gaoxueyu_hope(at)163(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org, dgrowleyml(at)gmail(dot)com, Jeff Davis <pgsql(at)j-davis(dot)com>
Subject: Re: pg_upgrade: backslash escaping in check_for_unicode_update()
Date: 2026-12-02 01:44:17
Message-ID: CC884EE7-0F81-4AD6-A24F-C74BF31AD4AB@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On Dec 1, 2026, at 14:45, Xueyu Gao <gaoxueyu_hope(at)163(dot)com> wrote:
>
> Hi, David,
>
> While testing the v3 patch (v3-0001-Speed-up-pg_upgrade-get_rel_infos_query-processin.patch) with standard_conforming_strings = off on the source server, I found a separate issue in check_for_unicode_update().
>
> The two patterns for OPEXPR and FUNCEXPR in src/bin/pg_upgrade/check.c contain the following C string fragment:
>
> " ... || ' (:\\w+ \\w+ )*' || "
>
> This produces an ordinary SQL string literal containing \w. When standard_conforming_strings is off, the SQL parser removes the backslashes, so the
> regular expression receives (:w+ w+ )* instead of (:\w+ \w+ )*. It also emits the “nonstandard use of escape in a string literal” warnings.
>
> This can cause the Unicode dependency check to miss affected objects, rather than merely produce warnings. The issue is separate from the schema-
> name mismatch in get_rel_infos_query(); fixing the three replacement strings in info.c leaves these warnings in place.
>
> I verified this using a PG17 source cluster and the patched PG19 pg_upgrade. The fix is to use explicit escape string literals and adjust the C-
> level escaping in both patterns:
>
> " ... || E' (:\\\\w+ \\\\w+ )*' || "
>
> This preserves the intended regular expression with either setting of standard_conforming_strings.
>
> Please find attached patch for this issue.
>
>
> Here is the procedure I used to reproduce the WARNINGS during upgrade:
>
> 1. Initialize two database clusters and prepare data
>
> lab="/home/highgo/review"
> oldbin="/home/highgo/pg17/inst/bin"
> newbin="/home/highgo/pg19/inst/bin"
> mkdir -m 700 "/home/highgo/review/sock"
>
> "$oldbin/initdb" -D "$lab/data17" --locale=C -E UTF8 -A trust --data-checksums
> "$newbin/initdb" -D "$lab/data19-off" --locale=C -E UTF8 -A trust
>
> "$oldbin/pg_ctl" -D "$lab/data17" -l "$lab/pg17.log" \
> -o "-p 54117 -k $lab/sock -c listen_addresses='' -c standard_conforming_strings=off" \
> -w start
>
> "$oldbin/psql" -X -v ON_ERROR_STOP=1 -h "$lab/sock" -p 54117 -d postgres <<'SQL'
> SHOW standard_conforming_strings;
>
> CREATE SCHEMA "Mixed Case";
> CREATE TABLE "Mixed Case".t (id integer PRIMARY KEY);
> INSERT INTO "Mixed Case".t VALUES (1);
>
> SELECT nspname AS catalog_name,
> encode(convert_to(
> regexp_replace(oid::regnamespace::text, '^"(.*)"$', '\1'),
> 'UTF8'
> ), 'hex') AS v2_result_hex
> FROM pg_catalog.pg_namespace
> WHERE nspname = 'Mixed Case';
> SQL
>
> "$oldbin/pg_ctl" -D "$lab/data17" -m fast -w stop
>
>
> 2. Perform upgrade
>
> cd "$lab"
> set -o pipefail
>
> run_upgrade() {
> local target=$1
> shift
> "$newbin/pg_upgrade" \
> -b "$oldbin" -B "$newbin" \
> -d "$lab/data17" -D "$target" \
> -p 54117 -P 54119 -s "$lab/sock" \
> --copy --retain "$@"
> }
>
> if run_upgrade "$lab/data19-off" \
> -o '-c standard_conforming_strings=off' 2>&1 | tee "$lab/upgrade-off.log"
> then
> echo 'upgrade succeed'
> else
> printf 'upgrade exit %s\n' "$?"
> fi
>
>
>
> Performing Consistency Checks
> -----------------------------
> Checking cluster versions ok
> Checking database connection settings ok
> Checking for unsupported encodings ok
> Checking names of databases, roles, and tablespaces ok
> Checking database user is the install user ok
> Checking for prepared transactions ok
> Checking oldestXID and oldestMultiXid consistency ok
> Checking for contrib/isn with bigint-passing mismatch ok
> Checking logical replication slots ok
> Checking subscription state ok
> Checking data type usage ok
> Checking for objects affected by Unicode update WARNING: nonstandard use of escape in a string literal
> LINE 1: ...NION SELECT '{OPEXPR :opno ' || operid::text || ' (:\w+ \w...
> ^
> HINT: Use the escape string syntax for escapes, e.g., E'\r\n'.
> WARNING: nonstandard use of escape in a string literal
> LINE 1: ... SELECT '{FUNCEXPR :funcid ' || procid::text || ' (:\w+ \w...
> ^
> HINT: Use the escape string syntax for escapes, e.g., E'\r\n'.
> WARNING: nonstandard use of escape in a string literal
> LINE 1: ...NION SELECT '{OPEXPR :opno ' || operid::text || ' (:\w+ \w...
> ^
> HINT: Use the escape string syntax for escapes, e.g., E'\r\n'.
> WARNING: nonstandard use of escape in a string literal
> LINE 1: ... SELECT '{FUNCEXPR :funcid ' || procid::text || ' (:\w+ \w...
> ^
> HINT: Use the escape string syntax for escapes, e.g., E'\r\n'.
> ok
> Checking for not-null constraint inconsistencies ok
> Checking for uses of gist_inet_ops/gist_cidr_ops ok
> Creating dump of global objects ok
> Creating dump of database schemas ok
> Checking for presence of required libraries ok
> Checking database user is the install user ok
> Checking for prepared transactions ok
> Checking new cluster tablespace directories ok
>
> If pg_upgrade fails after this point, you must re-initdb the
> new cluster before continuing.
>
> Performing Upgrade
> ------------------
> Setting locale and encoding for new cluster ok
> Analyzing all rows in the new cluster ok
> Freezing all rows in the new cluster ok
> Deleting files from new pg_xact ok
> Copying old pg_xact to new server ok
> Setting oldest XID for new cluster ok
> Setting next transaction ID and epoch for new cluster ok
> Deleting files from new pg_multixact/members ok
> Deleting files from new pg_multixact/offsets ok
> Converting pg_multixact files ok
> Setting next multixact ID and offset for new cluster ok
> Resetting WAL archives ok
> Setting frozenxid and minmxid counters in new cluster ok
> Restoring global objects in the new cluster ok
> Restoring database schemas in the new cluster ok
> Copying user relation files ok
> Setting next OID for new cluster ok
> Sync data directory to disk ok
> Creating script to delete old cluster ok
> Checking for extension updates ok
>
> Upgrade Complete
> ----------------
> Some statistics are not transferred by pg_upgrade.
> Once you start the new server, consider running these two commands:
> /home/highgo/pg19/inst/bin/vacuumdb --all --analyze-in-stages --missing-stats-only
> /home/highgo/pg19/inst/bin/vacuumdb --all --analyze-only
> Running this script will delete the old cluster's data files:
> ./delete_old_cluster.sh
> upgrade succeed
>
>
> Best regards,
> Xueyu Gao

Since PG19, users can no longer turn off standard_conforming_strings:
```
evantest=# SET standard_conforming_strings = off;
ERROR: non-standard string literals are not supported
```

So, I switched to the REL_18_STABLE branch and ran the following tests:
```
evantest=# SET standard_conforming_strings = on;
SET
evantest=# SELECT E' (:\\w+ \\w+ )*';
?column?
---------------
(:\w+ \w+ )*
(1 row)

evantest=# SET standard_conforming_strings = off;
SET
evantest=# SELECT E' (:\\w+ \\w+ )*';
?column?
---------------
(:\w+ \w+ )*
(1 row)

evantest=# SET standard_conforming_strings = on;
SET
evantest=# SELECT ' (:\w+ \w+ )*';
?column?
---------------
(:\w+ \w+ )*
(1 row)

evantest=# SET standard_conforming_strings = off;
SET
evantest=# SELECT ' (:\w+ \w+ )*';
WARNING: nonstandard use of escape in a string literal
LINE 1: SELECT ' (:\w+ \w+ )*';
^
HINT: Use the escape string syntax for escapes, e.g., E'\r\n'.
?column?
-------------
(:w+ w+ )*
(1 row)
```

The escape string from the patch produces the same result no matter standard_conforming_strings is on or off, but the expression from the current code produces an incorrect regular expression when it is off. As we can see, the SQL statement itself raises a warning and gives a hint to use the escape string syntax.

So the fix looks good to me, and I think it should be back-patched to 18.

I was also curious why this bug hadn't been reported before. I think the reasons might include:

* check_for_unicode_update() was introduced in 18, so this bug is relatively new.
* check_for_unicode_update() skips source clusters running 16 or earlier. It also skips the query if the Unicode version hasn't changed, which further limits the upgrade scenarios in which this bug can occur.
* standard_conforming_strings defaults to on, and I guess not many users turn it off.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Tristan Su 2026-12-02 01:55:24 [PATCH] Reuse slot helpers in pg_get_replication_slots and slot copy
Previous Message Adesh Thakare 2026-12-02 01:40:38 Re: Postmaster PANIC on a standby with slot synchronization enabled