diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index b19d1cf..319d6cd 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -2056,6 +2056,14 @@ heap_insert(Relation relation, HeapTuple tup, CommandId cid, heap_freetuple(heaptup); } + /* + * We want to identify TEMP tables that have had any rows inserted into them + * in this transaction, so that at the end of transaction we can skip + * truncating those TEMP tables that have no rows. + */ + if (RelationUsesLocalBuffers(relation)) + relation->rd_rows_inserted = true; + return HeapTupleGetOid(tup); } diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index cad8311..ad30c55 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -10118,8 +10118,18 @@ PreCommit_on_commit_actions(void) /* Do nothing (there shouldn't be such entries, actually) */ break; case ONCOMMIT_DELETE_ROWS: - oids_to_truncate = lappend_oid(oids_to_truncate, oc->relid); - break; + { + Relation rel = heap_open(oc->relid, AccessShareLock); + + if (rel->rd_rows_inserted) + { + rel->rd_rows_inserted = false; + oids_to_truncate = lappend_oid(oids_to_truncate, oc->relid); + } + + heap_close(rel, AccessShareLock); + break; + } case ONCOMMIT_DROP: { ObjectAddress object; diff --git a/src/include/utils/rel.h b/src/include/utils/rel.h index bde5f17..4bc0600 100644 --- a/src/include/utils/rel.h +++ b/src/include/utils/rel.h @@ -174,6 +174,8 @@ typedef struct RelationData */ Oid rd_toastoid; /* Real TOAST table's OID, or InvalidOid */ + bool rd_rows_inserted;/* Did we insert any rows into this relation */ + /* use "struct" here to avoid needing to include pgstat.h: */ struct PgStat_TableStatus *pgstat_info; /* statistics collection area */ } RelationData;