Re: How to surround a selected value with double quotes?

hubert depesz lubaczewski <depesz@depesz.com>

From: hubert depesz lubaczewski <depesz@depesz.com>
To: "Subramanian,Ramachandran" <ramachandran.subramanian@alte-leipziger.de>
Cc: "pgsql-novice@lists.postgresql.org" <pgsql-novice@lists.postgresql.org>
Date: 2026-06-22T10:32:21Z
Lists: pgsql-novice
On Mon, Jun 22, 2026 at 07:46:09AM +0000, Subramanian,Ramachandran wrote:
> #Build the SQL needed to get the list of tables that are approaching the SAFE_XID_GAP
> LIST_OLDEST_UNFROZEN_XID_SQL=" SELECT   \
>        cast (age(PGCL.relfrozenxid) as integer) as GAP, \
>        INTB.table_catalog as DB_NAME,   \
>        "$PORT_NO" , \
>        CONCAT('"',INTB.table_schema,'"') || '.' ||  \
>        CONCAT('"',PGCL.relname,'"') as TABLE_NAME,      \
>        pg_table_size(PGCL.oid) as TABLE_SIZE \
> FROM                                                         \
>       pg_class PGCL,                                         \
>       information_schema.tables INTB                         \
> WHERE                                                        \
>      PGCL.relname=INTB.table_name                            \
> AND                                                          \
>      PGCL.relkind='r'                                       \
> AND                                                          \
>      age(PGCL.relfrozenxid)  > "$SAFE_XID_GAP" \
> ORDER BY                                                    \
>         1 DESC ;"

Some more comments, this time related to how you write bash/shell scripts.

1. you don't need to end line with \ if the value is in quotes - this is
   needed only if you split single command and it's options
2. if you have long string, multiline, it's better to use here-doc.
   like:

psql … << _END_OF_SQL_
SELECT
       cast (age(PGCL.relfrozenxid) as integer) as GAP,
       INTB.table_catalog as DB_NAME,
       "$PORT_NO" ,
       CONCAT('"',INTB.table_schema,'"') || '.' ||
       CONCAT('"',PGCL.relname,'"') as TABLE_NAME,
       pg_table_size(PGCL.oid) as TABLE_SIZE
FROM
      pg_class PGCL,
      information_schema.tables INTB
WHERE
     PGCL.relname=INTB.table_name
AND
     PGCL.relkind='r'
AND
     age(PGCL.relfrozenxid)  > "$SAFE_XID_GAP"
ORDER BY
        1 DESC
_END_OF_SQL_

3. if you really need to put the multi-line value in a variable, you can
   do it using:

read -r -d '' LIST_OLDEST_UNFROZEN_XID_SQL << _END_OF_SQL_
…
_END_OF_SQL_


Best regards,

depesz