Command tag

A keyword returned after successfully executing a command

A command tag is the keyword returned by PostgreSQL after successfully executing an SQL command other than SELECT.

Usually the command tag is the command which has just been executed, e.g.:

postgres=# BEGIN ;
BEGIN

postgres=*# CREATE TABLE foo(id INT);
CREATE TABLE

postgres=*# INSERT INTO foo values(1);
INSERT 0 1

postgres=*# UPDATE foo SET id = 2 WHERE id = 1;
UPDATE 1

postgres=*# DELETE FROM foo;
DELETE 1

SELECT (and TABLE) do not return a command tag, but INSERT/UPDATE/DELETE in combination with a RETURNING clause do:

postgres=# INSERT INTO foo values(1) RETURNING id;
 id
----
  1
(1 row)

INSERT 0 1

postgres=# SELECT * FROM foo;
 id
----
  1
(1 row)

In some cases, the command tag returned will be different, if the command executed leads to a different resolution, e.g.:

postgres=# BEGIN;
BEGIN
Time: 0.236 ms
postgres=*# SELECT foo;
ERROR:  column "foo" does not exist
LINE 1: SELECT foo;
               ^
Time: 0.158 ms
postgres=!# COMMIT;
ROLLBACK

There are also some cases where a command is actually an alias for another command; in that case the actual command will be returned as the command tag, e.g.:

postgres=*# END;
COMMIT

Internal implementation

The command tag is passed back to the client as part of the Frontend/Backend Protocol's CommandComplete message.

It is left the client to decide what to do with the command tag.

In libfq, the PQcmdStatus() function returns the command tag.

Categories

PostgreSQL concept