Manu <[email protected]> wrote: > Only case 2 from my last mail, which master has too, so it is not > a regression. In plain SQL, after "seq 1000 > /tmp/n.txt" and > "seq 3 > /tmp/three.txt": > > CREATE EXTENSION file_fdw; > CREATE SERVER f FOREIGN DATA WRAPPER file_fdw; > CREATE FOREIGN TABLE ft (a int) SERVER f > OPTIONS (filename '/tmp/n.txt'); > CREATE TABLE t (a int); > CREATE FUNCTION trg() RETURNS trigger LANGUAGE plpgsql > SET enable_hashjoin = off SET enable_nestloop = off AS $$ > BEGIN > IF NEW.a = 2 THEN > -- two file_fdw scans in one query (a merge join) > PERFORM count(*) FROM ft x JOIN ft y USING (a); > END IF; > PERFORM pg_stat_clear_snapshot(); > RAISE NOTICE 'row %: % row(s) in pg_stat_progress_copy', > NEW.a, (SELECT count(*) FROM pg_stat_progress_copy > WHERE pid = pg_backend_pid()); > RETURN NEW; > END $$; > CREATE TRIGGER tr BEFORE INSERT ON t > FOR EACH ROW EXECUTE FUNCTION trg(); > COPY t FROM '/tmp/three.txt'; > > On both v05 and master: > > NOTICE: row 1: 1 row(s) in pg_stat_progress_copy > NOTICE: row 2: 0 row(s) in pg_stat_progress_copy > NOTICE: row 3: 0 row(s) in pg_stat_progress_copy > > The COPY's row is gone from the moment the two-scan query in its > trigger finishes until the end of the COPY. I don't think it needs > to block v05.
v06 fixes this too (hopefully it doesn't break the previous fixes). Attached is also the change between v05 and v06 separate. The problem is that only the COPY FROM is executed by file_fdw, so file_fdw alone cannot guard against this case. v06 introduces a concept of a "dummy command" - see the is_dummy field in PgBackendProgress. 0002 is still needed because file_fdw can run multiple COPY FROM commands at the same time. Thanks again for thorough testing! -- Antonin Houska Web: https://www.cybertec-postgresql.com
v06-0001-Allow-progress-tracking-of-sub-commands.patch
Description: application/mbox
v06-0002-Do-not-report-progress-of-two-or-more-COPY-commands-.patch
Description: application/mbox
diff --git a/src/backend/utils/activity/backend_progress.c b/src/backend/utils/activity/backend_progress.c
index 0a259907d55..796ba15bebc 100644
--- a/src/backend/utils/activity/backend_progress.c
+++ b/src/backend/utils/activity/backend_progress.c
@@ -73,6 +73,7 @@ pgstat_progress_start_command(ProgressCommandType cmdtype, Oid relid)
volatile PgBackendStatus *beentry = MyBEEntry;
MemoryContext oldcxt;
PgBackendProgresElt *parent_new = NULL;
+ bool is_dummy = false;
if (!beentry || !pgstat_track_activities || !pgstat_track_progress)
return;
@@ -104,11 +105,28 @@ pgstat_progress_start_command(ProgressCommandType cmdtype, Oid relid)
if (beentry->st_progress_top.command == PROGRESS_COMMAND_INVALID)
beentry->st_progress_top = beentry->st_progress;
}
+
+ /*
+ * Now check the stack to see if a command of the same type is already
+ * being monitored.
+ */
+ for (PgBackendProgresElt *stack_elt = parent_commands; stack_elt != NULL;
+ stack_elt = stack_elt->parent)
+ {
+ if (stack_elt->progress.command == cmdtype)
+ {
+ /* Cannot really monitor the new command. */
+ is_dummy = true;
+ break;
+ }
+ }
+
beentry->st_progress.command = cmdtype;
beentry->st_progress.command_target = relid;
for (int i = 0; i < PGSTAT_NUM_PROGRESS_PARAM; i++)
beentry->st_progress.param[i] = 0;
beentry->st_progress.xact_level = xact_level;
+ beentry->st_progress.is_dummy = is_dummy;
PGSTAT_END_WRITE_ACTIVITY(beentry);
if (parent_new)
pfree(parent_new);
@@ -127,7 +145,8 @@ pgstat_progress_update_param(int index, int64 val)
Assert(index >= 0 && index < PGSTAT_NUM_PROGRESS_PARAM);
- if (!beentry || !pgstat_track_activities || !pgstat_track_progress)
+ if (!beentry || beentry->st_progress.is_dummy ||
+ !pgstat_track_activities || !pgstat_track_progress)
return;
/* Sub-commands introduce special cases that need to be tested. */
@@ -152,7 +171,8 @@ pgstat_progress_incr_param(int index, int64 incr)
Assert(index >= 0 && index < PGSTAT_NUM_PROGRESS_PARAM);
- if (!beentry || !pgstat_track_activities)
+ if (!beentry || beentry->st_progress.is_dummy ||
+ !pgstat_track_activities || !pgstat_track_progress)
return;
PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
@@ -202,8 +222,8 @@ pgstat_progress_update_multi_param(int nparam, const int *index,
volatile PgBackendStatus *beentry = MyBEEntry;
int i;
- if (!beentry || !pgstat_track_activities || !pgstat_track_progress ||
- nparam == 0)
+ if (!beentry || beentry->st_progress.is_dummy ||
+ !pgstat_track_activities || !pgstat_track_progress || nparam == 0)
return;
PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
@@ -274,6 +294,8 @@ pgstat_progress_end_command(bool xact)
beentry->st_progress_top.command = PROGRESS_COMMAND_INVALID;
beentry->st_progress_top.command_target = InvalidOid;
beentry->st_progress_top.xact_level = 0;
+ /* The top command should never become dummy, but be tidy. */
+ beentry->st_progress_top.is_dummy = false;
}
}
else
@@ -287,6 +309,8 @@ pgstat_progress_end_command(bool xact)
beentry->st_progress.command = PROGRESS_COMMAND_INVALID;
beentry->st_progress.command_target = InvalidOid;
beentry->st_progress.xact_level = 0;
+ /* See above. */
+ beentry->st_progress.is_dummy = false;
}
break;
diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c
index 3ba2f0808a8..85510a1fe77 100644
--- a/src/backend/utils/adt/pgstatfuncs.c
+++ b/src/backend/utils/adt/pgstatfuncs.c
@@ -383,12 +383,17 @@ pg_stat_get_progress_info(PG_FUNCTION_ARGS)
/*
* Report values for only those backends which are running the given
- * command.
+ * command. (Dummy entry has no data to report.)
*/
- if (beentry->st_progress.command == cmdtype)
+ if (!beentry->st_progress.is_dummy &&
+ beentry->st_progress.command == cmdtype)
st_progress = &beentry->st_progress;
else if (beentry->st_progress_top.command == cmdtype)
+ {
+ Assert(!beentry->st_progress_top.is_dummy);
+
st_progress = &beentry->st_progress_top;
+ }
else
continue;
diff --git a/src/include/utils/backend_status.h b/src/include/utils/backend_status.h
index 2f511a5992e..3c79f4ba72e 100644
--- a/src/include/utils/backend_status.h
+++ b/src/include/utils/backend_status.h
@@ -96,6 +96,20 @@ typedef struct PgBackendProgress
Oid command_target;
/* Transaction nesting level the monitoring started at. */
int xact_level;
+
+ /*
+ * If a backend tries to start monitoring of a command of and another
+ * command of the same type is already being monitored, mark the new
+ * command as dummy, so that it's not actually used. (We still need the
+ * command in the stack so that pgstat_progress_end_command() works
+ * correctly.)
+ *
+ * The typical scenario is COPY TO running on a table having an insert
+ * trigger that scans a file_fdw table (which in turn runs COPY FROM). In
+ * this case, we can only monitor the "outer" COPY TO.
+ */
+ bool is_dummy;
+
int64 param[PGSTAT_NUM_PROGRESS_PARAM];
} PgBackendProgress;
