Question: I have following table:

```
CREATE TABLE IF NOT EXISTS categories
(
    id SERIAL PRIMARY KEY,
    title CHARACTER VARYING(100) NOT NULL,
    description CHARACTER VARYING(200) NULL,
    category_type CHARACTER VARYING(100) NOT NULL
);

```

I am using `pg-promise`, and I want to provide optional update of columns:

```
categories.update = function (categoryTitle, toUpdateCategory) {
  return this.db.oneOrNone(sql.update, [
          categoryTitle,
          toUpdateCategory.title, toUpdateCategory.category_type, toUpdateCategory.description,
        ])
}

```

* `categoryName` - is required
* `toUpdateCategory.title` - is required
* `toUpdateCategory.category_type` - is optional (can be passed or undefined)
* `toUpdateCategory.description` - is optional (can be passed or undefined)

I want to build `UPDATE` query for updating only provided columns:

```
UPDATE categories
SET title=$2,
// ... SET category_type=$3 if $3 is no NULL otherwise keep old category_type value
// ... SET description=$4 if $4 is no NULL otherwise keep old description value
WHERE title = $1
RETURNING *;

```

How can I achieve this optional column update in Postgres?

Answer: 
You could `coalesce` between the old and the new values:

```
UPDATE categories
SET title=$2,
    category_type = COALESCE($3, category_type),
    description = COALESCE($4, description) -- etc...
WHERE title = $1

```
The [helpers](http://vitaly-t.github.io/pg-promise/helpers.html) syntax is best for any sort of dynamic logic with [pg-promise](https://github.com/vitaly-t/pg-promise):

```
/* logic for skipping columns: */
const skip = c => c.value === null || c.value === undefined;

/* reusable/static ColumnSet object: */
const cs = new pgp.helpers.ColumnSet(
    [
        'title',
        {name: 'category_type', skip},
        {name: 'description', skip}
    ],
    {table: 'categories'});

categories.update = function(title, category) {
   const condition = pgp.as.format(' WHERE title = $1', title);
   const update = () => pgp.helpers.update(category, cs) + condition;
   return this.db.none(update);
}

```

And if your optional column-properties do not even exist on the object when they are not specified, you can simplify the skip logic to just this (see [Column](http://vitaly-t.github.io/pg-promise/helpers.Column.html) logic):

```
const skip = c => !c.exists;

```

Used API: [ColumnSet](http://vitaly-t.github.io/pg-promise/helpers.ColumnSet.html), [helpers.update](http://vitaly-t.github.io/pg-promise/helpers.html#.update).

See also a very similar question: [Skip update columns with pg-promise](https://stackoverflow.com/questions/40697330/skip-update-columns-with-pg-promise).