pg_upgrade: backslash escaping in check_for_unicode_update()

From: "Xueyu Gao" <gaoxueyu_hope(at)163(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org, dgrowleyml(at)gmail(dot)com
Subject: pg_upgrade: backslash escaping in check_for_unicode_update()
Date: 2026-12-01 06:45:41
Message-ID: 35386aef.55f6.1a2305a1d3a.Coremail.gaoxueyu_hope@163.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

Attachment Content-Type Size
0001-Fix-backslash-escaping-in-pg_upgrade-s-Unicode-updat.patch application/octet-stream 1.6 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2026-12-01 06:54:37 Re: Preserve conflict log table during upgrade
Previous Message Ideriha, Takeshi 2026-12-01 06:34:39 Re: Doc: Fix BrinOpcInfo members to match the code