Hello, I recently noticed a unusually large memory consumption in pgoutput where "RelationSyncCache" is maintaining approximately 3 GB of memory while handling 15,000 tables.
Upon investigating the cause, I found that "tts_tupleDescriptor" in both "old_slot" and "new_slot" wouldn't be freed before the reusing the entry. The refcount of the tuple descriptor is initialized as -1 in init_tuple_slot(), which means it will not be refcounted. So, when cleaning the outdated slots, ExecDropSingleTupleTableSlot() would omit tuple descriptors. To address this issue, calling FreeTupleDesc() to release the tuple descriptor before ExecDropSingleTupleTableSlot() might be a suitable solution. However, I am uncertain whether this approach is appropriate. Reproduct ========= It's easy to reproduce the issue with sysbench: 1. Create emtpy tables on primary. ``` sysbench oltp_read_only --db-driver=pgsql \ --pgsql-port=5432 \ --pgsql-db=postgres \ --pgsql-user=postgres \ --tables=15000 --table_size=0 \ --report-interval=1 \ --threads=10 prepare ``` 2. Create a standby and promote it. 3. Create publication on primary and subscription on standby. ``` CREATE PUBLICATION pub1 FOR ALL TABLES; CREATE SUBSCRIPTION sub1 CONNECTION 'port=5432 user=postgres dbname=postgres' PUBLICATION pub1 WITH (enabled, create_slot, slot_name='pub1_to_sub1', copy_data=false); ``` 4. Bench on primary. ``` sysbench oltp_write_only --db-driver=pgsql \ --pgsql-port=5432 \ --pgsql-db=postgres \ --pgsql-user=postgres \ --tables=15000 --table_size=100 \ --report-interval=1 \ --threads=10 run \ --time=180 ``` Tested in PostgreSQL 17, the memory consumption of walsender is 804MB after the benchmark, and it decreases to 441MB after applying the patch. Thanks for your feedback. Kind Regards, Boyu Yang
0001-Fix-memory-leak-in-pgoutput.patch
Description: 0001-Fix-memory-leak-in-pgoutput.patch