Comments are blocks of text that, through special character sequences, can embed non-SQL text within SQL
code. These can be used within blocks of code, because PostgreSQL removes the commented areas from the input stream and
treats it as whitespace. There are two styles of comments available: single-line comments, and multiline comments.
Single-line comments are preceded by two dashes (- -) and may either be on a line by
themselves, or they may follow valid SQL tokens. (The comments themselves are not considered tokens to PostgreSQL's parser, as
any character data following the - - sequence, up to the end of the line, is treated as
whitespace.) This is demonstrated in Example 3-11.
Example 3-11. Single-line comments
testdb=# SELECT 'Test' -- This can follow valid SQL tokens,
testdb-# -- or be on a line of it own.
testdb-# AS example;
example
---------
Test
(1 row)
Multiline comments begin with a sequential slash-asterisk
(/*) sequence, and terminate
with a sequential asterisk-slash (*/)
sequence. This style of commenting may already be familiar to C
programmers, but there is one key difference between PostgreSQL's
interpreter and the C language interpreter: PostgreSQL comments may be
nested
. Therefore, when you create a multiline
comment within another multiline comment, the */
used to close the inner comment does not also close the outer comment.
Example 3-12 provides a comment explanation.
Example 3-12. Multiline comments
testdb=# SELECT 'Multi' /* This comment extends across
testdb*# * numerous lines, and can be
testdb*# * /* nested safely */ */
testdb-# || '-test' AS example;
example
------------
Multi-test
(1 row)
Nesting comments can be useful if you have a file containing SQL syntax of which you wish to comment a
large portion before sending to PostgreSQL for interpreting and execution. If you have already used multiline comments
within that document and you wish to comment a large section which includes those comments, PostgreSQL is intelligent
enough to recognize that a closing comment sequence (*/) closes only the most recently
opened comment, not the entire commented region.
Note: The asterisk character by itself (without an adjacent slash character) has no special meaning within a comment.
The extra asterisks in Example 3-12 on multiline comments are provided only for aesthetic purposes
and readability.