public inbox for [email protected]  
help / color / mirror / Atom feed
[PATCH v29 03/11] Allow to prolong life span of transition tables until transaction end
6+ messages / 5 participants
[nested] [flat]

* [PATCH v29 03/11] Allow to prolong life span of transition tables until transaction end
@ 2019-12-20 01:09 Yugo Nagata <[email protected]>
  0 siblings, 0 replies; 6+ messages in thread

From: Yugo Nagata @ 2019-12-20 01:09 UTC (permalink / raw)

Originally, tuplestores of AFTER trigger's transition tables were
freed for each query depth. For our IVM implementation, we would like
to prolong life of the tuplestores because we have to preserve them
for a whole query assuming that some base tables might be changed
in some trigger functions.
---
 src/backend/commands/trigger.c | 83 ++++++++++++++++++++++++++++++++--
 src/include/commands/trigger.h |  2 +
 2 files changed, 81 insertions(+), 4 deletions(-)

diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.c
index 52177759ab..00b20f4b5b 100644
--- a/src/backend/commands/trigger.c
+++ b/src/backend/commands/trigger.c
@@ -3742,6 +3742,10 @@ typedef struct AfterTriggerEventList
  * end of the list, so it is relatively easy to discard them.  The event
  * list chunks themselves are stored in event_cxt.
  *
+ * prolonged_tuplestored is a list of transition table tuplestores whose
+ * life are prolonged to the end of the outmost query instead of each nested
+ * query.
+ *
  * query_depth is the current depth of nested AfterTriggerBeginQuery calls
  * (-1 when the stack is empty).
  *
@@ -3807,6 +3811,7 @@ typedef struct AfterTriggersData
 	SetConstraintState state;	/* the active S C state */
 	AfterTriggerEventList events;	/* deferred-event list */
 	MemoryContext event_cxt;	/* memory context for events, if any */
+	List   *prolonged_tuplestores;	/* list of prolonged tuplestores */
 
 	/* per-query-level data: */
 	AfterTriggersQueryData *query_stack;	/* array of structs shown below */
@@ -3842,6 +3847,7 @@ struct AfterTriggersTableData
 	bool		closed;			/* true when no longer OK to add tuples */
 	bool		before_trig_done;	/* did we already queue BS triggers? */
 	bool		after_trig_done;	/* did we already queue AS triggers? */
+	bool		prolonged;			/* are transition tables prolonged? */
 	AfterTriggerEventList after_trig_events;	/* if so, saved list pointer */
 
 	/*
@@ -3891,6 +3897,7 @@ static void TransitionTableAddTuple(EState *estate,
 									TupleTableSlot *original_insert_tuple,
 									Tuplestorestate *tuplestore);
 static void AfterTriggerFreeQuery(AfterTriggersQueryData *qs);
+static void release_or_prolong_tuplestore(Tuplestorestate *ts, bool prolonged);
 static SetConstraintState SetConstraintStateCreate(int numalloc);
 static SetConstraintState SetConstraintStateCopy(SetConstraintState origstate);
 static SetConstraintState SetConstraintStateAddItem(SetConstraintState state,
@@ -4768,6 +4775,45 @@ afterTriggerInvokeEvents(AfterTriggerEventList *events,
 }
 
 
+/*
+ * SetTransitionTablePreserved
+ *
+ * Prolong lifespan of transition tables corresponding specified relid and
+ * command type to the end of the outmost query instead of each nested query.
+ * This enables to use nested AFTER trigger's transition tables from outer
+ * query's triggers.  Currently, only immediate incremental view maintenance
+ * uses this.
+ */
+void
+SetTransitionTablePreserved(Oid relid, CmdType cmdType)
+{
+	AfterTriggersTableData *table;
+	AfterTriggersQueryData *qs;
+	bool		found = false;
+	ListCell   *lc;
+
+	/* Check state, like AfterTriggerSaveEvent. */
+	if (afterTriggers.query_depth < 0)
+		elog(ERROR, "SetTransitionTablePreserved() called outside of query");
+
+	qs = &afterTriggers.query_stack[afterTriggers.query_depth];
+
+	foreach(lc, qs->tables)
+	{
+		table = (AfterTriggersTableData *) lfirst(lc);
+		if (table->relid == relid && table->cmdType == cmdType &&
+			table->closed)
+		{
+			table->prolonged = true;
+			found = true;
+		}
+	}
+
+	if (!found)
+		elog(ERROR,"could not find table with OID %d and command type %d", relid, cmdType);
+}
+
+
 /*
  * GetAfterTriggersTableData
  *
@@ -4978,6 +5024,7 @@ AfterTriggerBeginXact(void)
 	 */
 	afterTriggers.firing_counter = (CommandId) 1;	/* mustn't be 0 */
 	afterTriggers.query_depth = -1;
+	afterTriggers.prolonged_tuplestores = NIL;
 
 	/*
 	 * Verify that there is no leftover state remaining.  If these assertions
@@ -5138,19 +5185,19 @@ AfterTriggerFreeQuery(AfterTriggersQueryData *qs)
 		ts = table->old_upd_tuplestore;
 		table->old_upd_tuplestore = NULL;
 		if (ts)
-			tuplestore_end(ts);
+			release_or_prolong_tuplestore(ts, table->prolonged);
 		ts = table->new_upd_tuplestore;
 		table->new_upd_tuplestore = NULL;
 		if (ts)
-			tuplestore_end(ts);
+			release_or_prolong_tuplestore(ts, table->prolonged);
 		ts = table->old_del_tuplestore;
 		table->old_del_tuplestore = NULL;
 		if (ts)
-			tuplestore_end(ts);
+			release_or_prolong_tuplestore(ts, table->prolonged);
 		ts = table->new_ins_tuplestore;
 		table->new_ins_tuplestore = NULL;
 		if (ts)
-			tuplestore_end(ts);
+			release_or_prolong_tuplestore(ts, table->prolonged);
 		if (table->storeslot)
 		{
 			TupleTableSlot *slot = table->storeslot;
@@ -5167,6 +5214,34 @@ AfterTriggerFreeQuery(AfterTriggersQueryData *qs)
 	 */
 	qs->tables = NIL;
 	list_free_deep(tables);
+
+	/* Release prolonged tuplestores at the end of the outmost query */
+	if (afterTriggers.query_depth == 0)
+	{
+		foreach(lc, afterTriggers.prolonged_tuplestores)
+		{
+			ts = (Tuplestorestate *) lfirst(lc);
+			if (ts)
+				tuplestore_end(ts);
+		}
+		afterTriggers.prolonged_tuplestores = NIL;
+	}
+}
+
+/*
+ * Release the tuplestore, or append it to the prolonged tuplestores list.
+ */
+static void
+release_or_prolong_tuplestore(Tuplestorestate *ts, bool prolonged)
+{
+	if (prolonged && afterTriggers.query_depth > 0)
+	{
+		MemoryContext oldcxt = MemoryContextSwitchTo(CurTransactionContext);
+		afterTriggers.prolonged_tuplestores = lappend(afterTriggers.prolonged_tuplestores, ts);
+		MemoryContextSwitchTo(oldcxt);
+	}
+	else
+		tuplestore_end(ts);
 }
 
 
diff --git a/src/include/commands/trigger.h b/src/include/commands/trigger.h
index 430e3ca7dd..48a21c4c51 100644
--- a/src/include/commands/trigger.h
+++ b/src/include/commands/trigger.h
@@ -265,6 +265,8 @@ extern void AfterTriggerEndSubXact(bool isCommit);
 extern void AfterTriggerSetState(ConstraintsSetStmt *stmt);
 extern bool AfterTriggerPendingOnRel(Oid relid);
 
+extern void SetTransitionTablePreserved(Oid relid, CmdType cmdType);
+
 
 /*
  * in utils/adt/ri_triggers.c
-- 
2.25.1


--Multipart=_Mon__28_Aug_2023_16_05_30_+0900_b1OvQD_3A3ZMTGvj
Content-Type: text/x-diff;
 name="v29-0004-Add-Incremental-View-Maintenance-support-to-pg_d.patch"
Content-Disposition: attachment;
 filename="v29-0004-Add-Incremental-View-Maintenance-support-to-pg_d.patch"
Content-Transfer-Encoding: 7bit



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: list of acknowledgments for PG16
@ 2023-08-22 13:29 Tom Lane <[email protected]>
  2023-08-22 13:40 ` Re: list of acknowledgments for PG16 Vik Fearing <[email protected]>
  2023-08-22 14:24 ` Re: list of acknowledgments for PG16 Peter Eisentraut <[email protected]>
  0 siblings, 2 replies; 6+ messages in thread

From: Tom Lane @ 2023-08-22 13:29 UTC (permalink / raw)
  To: Alvaro Herrera <[email protected]>; +Cc: Vik Fearing <[email protected]>; Peter Eisentraut <[email protected]>; pgsql-hackers

Alvaro Herrera <[email protected]> writes:
> Yeah, I've been proposing this kind of thing for many years; the
> problem, until not long ago, was that the tooling was unable to process
> non-Latin1 characters in all the output formats that we use.  But
> tooling has changed and the oldest platforms have disappeared, so maybe
> it works now; do you want to inject some Chinese, Cyrillic, Japanese
> names and give it a spin?  At least HTML and PDF need to work correctly.

I'm pretty sure the PDF toolchain still fails on non-Latin1 characters.
At least it does the way I have it installed; maybe adding some
non-default dependencies would help?

			regards, tom lane






^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: list of acknowledgments for PG16
  2023-08-22 13:29 Re: list of acknowledgments for PG16 Tom Lane <[email protected]>
@ 2023-08-22 13:40 ` Vik Fearing <[email protected]>
  2023-08-25 05:06   ` Re: list of acknowledgments for PG16 jian he <[email protected]>
  1 sibling, 1 reply; 6+ messages in thread

From: Vik Fearing @ 2023-08-22 13:40 UTC (permalink / raw)
  To: Tom Lane <[email protected]>; Alvaro Herrera <[email protected]>; +Cc: Peter Eisentraut <[email protected]>; pgsql-hackers

On 8/22/23 15:29, Tom Lane wrote:
> Alvaro Herrera <[email protected]> writes:
>> Yeah, I've been proposing this kind of thing for many years; the
>> problem, until not long ago, was that the tooling was unable to process
>> non-Latin1 characters in all the output formats that we use.  But
>> tooling has changed and the oldest platforms have disappeared, so maybe
>> it works now; do you want to inject some Chinese, Cyrillic, Japanese
>> names and give it a spin?  At least HTML and PDF need to work correctly.
> 
> I'm pretty sure the PDF toolchain still fails on non-Latin1 characters.
> At least it does the way I have it installed; maybe adding some
> non-default dependencies would help?

I am struggling to find documentation on how to build the pdfs with 
meson.  Any pointers?
-- 
Vik Fearing







^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: list of acknowledgments for PG16
  2023-08-22 13:29 Re: list of acknowledgments for PG16 Tom Lane <[email protected]>
  2023-08-22 13:40 ` Re: list of acknowledgments for PG16 Vik Fearing <[email protected]>
@ 2023-08-25 05:06   ` jian he <[email protected]>
  0 siblings, 0 replies; 6+ messages in thread

From: jian he @ 2023-08-25 05:06 UTC (permalink / raw)
  To: Vik Fearing <[email protected]>; +Cc: pgsql-hackers

On Tue, Aug 22, 2023 at 9:41 PM Vik Fearing <[email protected]> wrote:
>
>
> I am struggling to find documentation on how to build the pdfs with
> meson.  Any pointers?
> --
> Vik Fearing
>
>
>

ninja docs:
https://wiki.postgresql.org/wiki/Meson#Meson_documentation

ninja alldocs. which take some time, build all kinds of formats, some may fail.

there is another tricky usage:
type "ninja doc" then press Tab for complete twice, you will get all
the available options like following:
docs                                    doc/src/sgml/man3
doc/src/sgml/errcodes-table.sgml        doc/src/sgml/man7
doc/src/sgml/features-supported.sgml    doc/src/sgml/postgres-A4.fo
doc/src/sgml/features-unsupported.sgml  doc/src/sgml/postgres-A4.pdf
doc/src/sgml/html                       doc/src/sgml/postgres.epub
doc/src/sgml/INSTALL                    doc/src/sgml/postgres-full.xml
doc/src/sgml/install-html               doc/src/sgml/postgres.html
doc/src/sgml/INSTALL.html               doc/src/sgml/postgres.txt
doc/src/sgml/install-man                doc/src/sgml/postgres-US.fo
doc/src/sgml/INSTALL.xml                doc/src/sgml/postgres-US.pdf
doc/src/sgml/keywords-table.sgml        doc/src/sgml/wait_event_types.sgml
doc/src/sgml/man1






^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: list of acknowledgments for PG16
  2023-08-22 13:29 Re: list of acknowledgments for PG16 Tom Lane <[email protected]>
@ 2023-08-22 14:24 ` Peter Eisentraut <[email protected]>
  2023-08-28 18:36   ` Re: list of acknowledgments for PG16 Vik Fearing <[email protected]>
  1 sibling, 1 reply; 6+ messages in thread

From: Peter Eisentraut @ 2023-08-22 14:24 UTC (permalink / raw)
  To: Tom Lane <[email protected]>; Alvaro Herrera <[email protected]>; +Cc: Vik Fearing <[email protected]>; pgsql-hackers

On 22.08.23 15:29, Tom Lane wrote:
> Alvaro Herrera <[email protected]> writes:
>> Yeah, I've been proposing this kind of thing for many years; the
>> problem, until not long ago, was that the tooling was unable to process
>> non-Latin1 characters in all the output formats that we use.  But
>> tooling has changed and the oldest platforms have disappeared, so maybe
>> it works now; do you want to inject some Chinese, Cyrillic, Japanese
>> names and give it a spin?  At least HTML and PDF need to work correctly.
> 
> I'm pretty sure the PDF toolchain still fails on non-Latin1 characters.
> At least it does the way I have it installed; maybe adding some
> non-default dependencies would help?

See here: 
https://www.postgresql.org/message-id/[email protected]







^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: list of acknowledgments for PG16
  2023-08-22 13:29 Re: list of acknowledgments for PG16 Tom Lane <[email protected]>
  2023-08-22 14:24 ` Re: list of acknowledgments for PG16 Peter Eisentraut <[email protected]>
@ 2023-08-28 18:36   ` Vik Fearing <[email protected]>
  0 siblings, 0 replies; 6+ messages in thread

From: Vik Fearing @ 2023-08-28 18:36 UTC (permalink / raw)
  To: Peter Eisentraut <[email protected]>; Tom Lane <[email protected]>; Alvaro Herrera <[email protected]>; +Cc: pgsql-hackers

On 8/22/23 16:24, Peter Eisentraut wrote:
> On 22.08.23 15:29, Tom Lane wrote:
>> Alvaro Herrera <[email protected]> writes:
>>> Yeah, I've been proposing this kind of thing for many years; the
>>> problem, until not long ago, was that the tooling was unable to process
>>> non-Latin1 characters in all the output formats that we use.  But
>>> tooling has changed and the oldest platforms have disappeared, so maybe
>>> it works now; do you want to inject some Chinese, Cyrillic, Japanese
>>> names and give it a spin?  At least HTML and PDF need to work correctly.
>>
>> I'm pretty sure the PDF toolchain still fails on non-Latin1 characters.
>> At least it does the way I have it installed; maybe adding some
>> non-default dependencies would help?
> 
> See here: 
> https://www.postgresql.org/message-id/[email protected]

I applied that patch, and it works for Cyrillic text, but not for 
Japanese.  I am trying to figure out how to make it use a secondary 
font, but that might take me a while.
-- 
Vik Fearing







^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2023-08-28 18:36 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-12-20 01:09 [PATCH v29 03/11] Allow to prolong life span of transition tables until transaction end Yugo Nagata <[email protected]>
2023-08-22 13:29 Re: list of acknowledgments for PG16 Tom Lane <[email protected]>
2023-08-22 13:40 ` Re: list of acknowledgments for PG16 Vik Fearing <[email protected]>
2023-08-25 05:06   ` Re: list of acknowledgments for PG16 jian he <[email protected]>
2023-08-22 14:24 ` Re: list of acknowledgments for PG16 Peter Eisentraut <[email protected]>
2023-08-28 18:36   ` Re: list of acknowledgments for PG16 Vik Fearing <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox