Bryn Llewellyn <bryn@yugabyte.com> writes:
> I've seen this pattern in use:
> create temp table if not exists pg_temp.flag(val boolean not null) on commit delete rows;
> insert into pg_temp.flag(val) values(true);
> But doing a DDL before every use of the session-state representation felt heavier than assuming that it's there and
creatingthe table only if it isn't. But I haven't done any timing tests. Is the "create… if not exists" so lightweight
whenthe to-be-created object does exist that I'm fussing over nothing?
Fair question. My gut feeling is that the subtransaction created by the
BEGIN ... EXCEPTION construct is more expensive than a no-op CREATE
IF NOT EXISTS. I've not measured it though; and I'm pretty sure that
the answer would vary depending on how often you expect the code to fall
through versus needing to create the table.
regards, tom lane