Hi Antonin,

v04 fixes two of the four cases from my last mail. The two COPY
trigger cases are still there, and two related cases are now worse
than on master. All of this is on master 9ebc9a458c3 with v04-0001
and v04-0002, using the attached script. The session reads its own
progress row, so it gives the same output on every run.

> Should work in v04 [1].

It does. After the failed two-scan query the session reports progress
again, and the Assert in xact_callback() no longer fires. The cursor
case is fixed too, and so are the rows Adam and Alberto saw left
behind by CREATE INDEX CONCURRENTLY and REINDEX CONCURRENTLY.

> This should also work in v04.

Not for the two COPY trigger cases:

1. If a COPY fails inside a two-scan query run by its trigger, its
   row stays after the error and through later commands. While the
   next CREATE INDEX runs, pg_stat_progress_create_index has no row
   for it, and the session still shows the failed COPY. On master the
   row goes away with the error.

2. A two-scan query that succeeds inside a COPY trigger still removes
   the outer COPY's row for the rest of that COPY. Master does the
   same, so this one is not a regression.

The two related cases: if the two-scan query fails inside a
subtransaction and the error is caught, either in a PL/pgSQL
EXCEPTION block or with SAVEPOINT and ROLLBACK TO, the session stops
reporting progress altogether. A later COPY or CREATE INDEX in that
session has no row at all. On master both show up normally, and a
new session is fine with v04 too.

The parallel BRIN build still looks the way Adam described, which I
take it you already plan to drop.

Regards,
Manu
#!/usr/bin/env bash
# v04-cases.sh <bindir> <workdir>
# Progress tracking of sub-commands, v04: the session reads its own progress
# rows (prog(), after pg_stat_clear_snapshot()) at fixed points, so every
# result is deterministic.  "C ..." = pg_stat_progress_copy,
# "I ..." = pg_stat_progress_create_index, "(no row)" = none.
# Needs file_fdw installed in <bindir>.  Throw-away cluster in <workdir>.
set -euo pipefail
BIN=$1 W=$2; rm -rf "$W"; mkdir -p "$W"
PGDATA=$W/data; PORT=$((61000 + RANDOM % 4000)); export PGHOST=$W PGPORT=$PORT 
PGDATABASE=postgres
"$BIN/initdb" -A trust -N -D "$PGDATA" >/dev/null
"$BIN/pg_ctl" -D "$PGDATA" -o "-p $PORT -k $W -c autovacuum=off" -l "$W/log" -w 
start >/dev/null
trap '"$BIN/pg_ctl" -D "$PGDATA" -w -m immediate stop >/dev/null 2>&1 || true' 
EXIT
seq 100000 >"$W/n.txt"
{ seq 100000; echo oops; } >"$W/bad.txt"
seq 3 >"$W/three.txt"
"$BIN/psql" -X -q -v ON_ERROR_STOP=1 <<SQL
CREATE EXTENSION file_fdw;
CREATE SERVER f FOREIGN DATA WRAPPER file_fdw;
CREATE FOREIGN TABLE ft (a int) SERVER f OPTIONS (filename '$W/n.txt');
CREATE FOREIGN TABLE ft_bad (a int) SERVER f OPTIONS (filename '$W/bad.txt');
CREATE TABLE heapx AS SELECT generate_series(1, 100000) a;
CREATE FUNCTION prog() RETURNS text LANGUAGE plpgsql AS \$\$
DECLARE r text;
BEGIN
  PERFORM pg_stat_clear_snapshot();
  SELECT concat_ws(' | ',
    (SELECT 'C ' || command || ' relid=' || relid::regclass
       FROM pg_stat_progress_copy WHERE pid = pg_backend_pid()),
    (SELECT 'I ' || coalesce(command, 'command NULL') || ' relid=' || 
relid::regclass
       FROM pg_stat_progress_create_index WHERE pid = pg_backend_pid())) INTO r;
  RETURN coalesce(nullif(r, ''), '(no row)');
END \$\$;
-- two file_fdw scans in one query (merge join), the bad one fails
CREATE FUNCTION two_scans_fail() RETURNS void LANGUAGE plpgsql
  SET enable_hashjoin = off SET enable_nestloop = off AS
  \$\$ BEGIN PERFORM count(*) FROM ft_bad a JOIN ft b USING (a); END \$\$;
-- two file_fdw scans in one query that succeeds
CREATE FUNCTION two_scans_ok() RETURNS void LANGUAGE plpgsql
  SET enable_hashjoin = off SET enable_nestloop = off AS
  \$\$ BEGIN PERFORM count(*) FROM ft a JOIN ft b USING (a); END \$\$;
-- the same, error caught in a subtransaction
CREATE FUNCTION two_scans_caught() RETURNS void LANGUAGE plpgsql AS
  \$\$ BEGIN PERFORM two_scans_fail(); EXCEPTION WHEN others THEN NULL; END 
\$\$;
-- index expression that reports what this session shows while the index builds
CREATE FUNCTION probe(x int) RETURNS int LANGUAGE plpgsql IMMUTABLE AS
  \$\$ BEGIN IF x = 1 THEN RAISE NOTICE 'index build: %', public.prog(); END 
IF; RETURN x; END \$\$;
-- trigger: at row 2 run <what>; at every row report what this session shows
CREATE FUNCTION trg() RETURNS trigger LANGUAGE plpgsql AS \$\$
BEGIN
  IF NEW.a = 2 THEN EXECUTE 'SELECT ' || TG_ARGV[0] || '()'; END IF;
  RAISE NOTICE 'row %: %', NEW.a, prog();
  RETURN NEW;
END \$\$;
CREATE TABLE t_fail (a int);
CREATE TRIGGER t_fail_tr BEFORE INSERT ON t_fail FOR EACH ROW EXECUTE FUNCTION 
trg('two_scans_fail');
CREATE TABLE t_ok (a int);
CREATE TRIGGER t_ok_tr BEFORE INSERT ON t_ok FOR EACH ROW EXECUTE FUNCTION 
trg('two_scans_ok');
CREATE TABLE t_plain (a int);
CREATE TRIGGER t_plain_tr BEFORE INSERT ON t_plain FOR EACH ROW EXECUTE 
FUNCTION trg('now');
SQL
run() { echo "######## $1"; shift; "$BIN/psql" -X -a 2>&1 <<<"$*" | grep -v 
'^$'; }
run "A. A COPY fails inside a two-scan query in its trigger" "
COPY t_fail FROM '$W/three.txt';
SELECT prog();
CREATE INDEX heapx_p ON heapx (probe(a));
SELECT prog();
COPY t_plain FROM '$W/three.txt';
SELECT prog();
DROP INDEX heapx_p;"
run "B. A two-scan query that succeeds inside a COPY trigger" "
COPY t_ok FROM '$W/three.txt';
SELECT prog();"
run "C. Two scans fail inside a subtransaction, error caught" "
SELECT two_scans_caught();
SELECT prog();
COPY t_plain FROM '$W/three.txt';
CREATE INDEX heapx_p ON heapx (probe(a));
DROP INDEX heapx_p;"
run "D. Same, with SAVEPOINT / ROLLBACK TO" "
BEGIN; SAVEPOINT s; SELECT two_scans_fail(); ROLLBACK TO s; COMMIT;
SELECT prog();
COPY t_plain FROM '$W/three.txt';"
run "E. Control: a fresh session" "
COPY t_plain FROM '$W/three.txt';
CREATE INDEX heapx_p ON heapx (probe(a));
DROP INDEX heapx_p;"
echo "######## server log: TRAP / PANIC / terminated"
grep -E 'TRAP|PANIC|terminated by signal' "$W/log" || echo none

Reply via email to