Both truncate and delete can be dropped (as clearly described in the manual).
truncate - by nature - has some oddities about visibility.
See the manual for more details: http://www.postgresql.org/docs/current/static/sql-truncate.html (warning below)
If your application can live with the fact that table_foo is “empty” during this process, truncation is probably better (look at the big red box in the manual again for an explanation). If you do not want the application to notice, you need to use delete
To run these statements in a transaction, just put them in one:
begin transaction; delete from table_foo; insert into .... drop table_temp; commit;
Whether you do this in a function or not is up to you.
truncate / insert will be faster (than delete / insert ) as this will minimize the amount of WAL generated.
source share