Fix efficiency problems in tuplestore_trim().
Fix efficiency problems in tuplestore_trim().

The original coding in tuplestore_trim() was only meant to work efficiently
in cases where each trim call deleted most of the tuples in the store.
Which, in fact, was the pattern of the original usage with a Material node
supporting mark/restore operations underneath a MergeJoin.  However,
WindowAgg now uses tuplestores and it has considerably less friendly
trimming behavior.  In particular it can attempt to trim one tuple at a
time off a large tuplestore.  tuplestore_trim() had O(N^2) runtime in this
situation because of repeatedly shifting its tuple pointer array.  Fix by
avoiding shifting the array until a reasonably large number of tuples have
been deleted.  This can waste some pointer space, but we do still reclaim
the tuples themselves, so the percentage wastage should be pretty small.

Per Jie Li's report of slow percent_rank() evaluation.  cume_dist() and
ntile() would certainly be affected as well, along with any other window
function that has a moving frame start and requires reading substantially
ahead of the current row.

Back-patch to 8.4, where window functions were introduced.  There's no
need to tweak it before that.

yazun/postgres-xl,yazun/postgres-xl,arcivanov/postgres-xl,pavanvd/postgres-xl,xinzweb/gpdb,zeroae/postgres-xl,snaga/postgres-xl,xinzweb/gpdb,yazun/postgres-xl,xinzweb/gpdb,zeroae/postgres-xl,kmjungersen/PostgresXL,ovr/postgres-xl,ovr/postgres-xl,Postgres-XL/Postgres-XL,xinzweb/gpdb,pavanvd/postgres-xl,arcivanov/postgres-xl,oberstet/postgres-xl,greenplum-db/gpdb,xinzweb/gpdb,yazun/postgres-xl,lisakowen/gpdb,adam8157/gpdb,oberstet/postgres-xl,50wu/gpdb,ashwinstar/gpdb,ashwinstar/gpdb,zeroae/postgres-xl,greenplum-db/gpdb,techdragon/Postgres-XL,snaga/postgres-xl,xinzweb/gpdb,greenplum-db/gpdb,ovr/postgres-xl,kmjungersen/PostgresXL,kmjungersen/PostgresXL,adam8157/gpdb,lisakowen/gpdb,postmind-net/postgres-xl,arcivanov/postgres-xl,pavanvd/postgres-xl,tpostgres-projects/tPostgres,ashwinstar/gpdb,arcivanov/postgres-xl,postmind-net/postgres-xl,adam8157/gpdb,50wu/gpdb,ashwinstar/gpdb,arcivanov/postgres-xl,techdragon/Postgres-XL,50wu/gpdb,greenplum-db/gpdb,ashwinstar/gpdb,pavanvd/postgres-xl,zeroae/postgres-xl,techdragon/Postgres-XL,xinzweb/gpdb,50wu/gpdb,50wu/gpdb,jmcatamney/gpdb,snaga/postgres-xl,adam8157/gpdb,jmcatamney/gpdb,lisakowen/gpdb,greenplum-db/gpdb,jmcatamney/gpdb,lisakowen/gpdb,snaga/postgres-xl,greenplum-db/gpdb,oberstet/postgres-xl,adam8157/gpdb,kmjungersen/PostgresXL,yazun/postgres-xl,postmind-net/postgres-xl,ashwinstar/gpdb,Postgres-XL/Postgres-XL,lisakowen/gpdb,ashwinstar/gpdb,adam8157/gpdb,kmjungersen/PostgresXL,jmcatamney/gpdb,oberstet/postgres-xl,tpostgres-projects/tPostgres,techdragon/Postgres-XL,lisakowen/gpdb,jmcatamney/gpdb,lisakowen/gpdb,pavanvd/postgres-xl,jmcatamney/gpdb,xinzweb/gpdb,50wu/gpdb,jmcatamney/gpdb,lisakowen/gpdb,Postgres-XL/Postgres-XL,tpostgres-projects/tPostgres,techdragon/Postgres-XL,jmcatamney/gpdb,snaga/postgres-xl,arcivanov/postgres-xl,ashwinstar/gpdb,ovr/postgres-xl,adam8157/gpdb,tpostgres-projects/tPostgres,postmind-net/postgres-xl,oberstet/postgres-xl,ovr/postgres-xl,postmind-net/postgres-xl,tpostgres-projects/tPostgres,Postgres-XL/Postgres-XL,zeroae/postgres-xl,adam8157/gpdb,Postgres-XL/Postgres-XL,50wu/gpdb,greenplum-db/gpdb,greenplum-db/gpdb,50wu/gpdb