title: "ERROR: invalid transaction termination" when trying to execute a procedure with nested transaction control
body: <p>According to the documentation (<a href="https://www.postgresql.org/docs/current/app-psql.html" rel="nofollow noreferrer">https://www.postgresql.org/docs/current/app-psql.html</a>), even with <code>AUTOCOMMIT</code> set to off, PSQL issues an implicit <code>BEGIN</code> just before any command that is not already in a transaction block and is not itself a <code>BEGIN</code> or other transaction-control command, nor a command that cannot be executed inside a transaction block such as <code>VACUUM</code>. (Unfortunately <code>CALL</code> is not treated in the same way as <code>VACCUM</code>). And, according to Shaun Thomas (<a href="https://blog.2ndquadrant.com/pg-phriday-stored-procedures-postgres-11/" rel="nofollow noreferrer">https://blog.2ndquadrant.com/pg-phriday-stored-procedures-postgres-11/</a>), the invalid transaction termination error happens because it is not possible to close the current transaction (in this case the one initiated by PSQL) from within the procedure. I have tried with all the PSQL settings related to transaction control, but the invalid transaction termination error occurs with all of them; even if the commands file processed by PSQL contains only the <code>CALL</code> statement.</p>
  
  <p>This is the procedure I'm calling:</p>
  
  <pre><code>create or replace procedure producto$cargar_imagenes(_super$ bigint, _archivo$ character varying) as $$
  declare
      _msg character varying;
      _log rastro_proceso%ROWTYPE;
  begin
      perform rastro_proceso_temporal$insert(_super$);
      perform producto$cargar_imagenes$biz(_super$, _archivo$);
      if (_super$ is not null and _super$ &gt; 0) then
          perform producto$cargar_imagenes$log(_super$, _archivo$);
      else
          perform tarea_usuario$private$update(6519204281880642486, null);
      end if;
      commit;
  end;
  $$ language plpgsql set search_path = public;
  </code></pre>
  
  <p>It fails at the <code>commit</code> statement; it works if I comment it out. </p>