PostgreSQL: Why we should use ‘$$’ double dollar in PG/pgSQL Block

Actually, The funda of $$ – double dollar is very simple, but Postgres new comers have always doubted about it.

When you are writing a PL/pgSQL function, the body of the function has to pass in the form of a string literal.

For example:

Create a sample table:

Create a function without $$ – double dollar:

We can create a function using single quote and We do not require $$ – double dollar in above example.
But we should not do this, because It is not suggestible to write your function code in single quotes.

When you create your function using single quote, you have to escape all internal single quote in the body.

$$ – Double Dollar:

It is nothing, but just a substitute of a single quote. Using $$ – Double Dollar we can avoid all types of quoting issues.
We can also put token between $$ Double Dollar like: $rnd$ which makes it unique.

For Example:
Create a function with $$ – double dollar:

Anvesh Patel

Leave a Reply

Be the First to Comment!

Notify of