Re: documentation question regarding REFRESH MATERIALIZED VIEW CONCURRENTLY

2025-02-23 Thread Greg Sabino Mullane
On Sat, Feb 22, 2025 at 8:58 PM Tobias McNulty 
wrote:

> "Without this option a refresh which affects a lot of rows will tend to
> use fewer resources"

...

> either that (1) the refresh operation actually updates the contents of a
> lot of rows in the materialized view


This is the correct interpretation. A regular refresh simply runs the query
and replaces the old view, regardless of the number of rows that have
changed. A concurrent refresh runs the query and updates the rows in place,
so it is very sensitive as to how many of those rows have changed. This
also means that many concurrent refreshes can lead to table bloat. And it
will generate more WAL than a regular refresh.

My takeaway: use regular refresh when you can. Switch to concurrent if the
number of changes is very small, or if constant client access to the view
is very important.

-- 
Cheers,
Greg

--
Crunchy Data - https://www.crunchydata.com
Enterprise Postgres Software Products & Tech Support


Re: documentation question regarding REFRESH MATERIALIZED VIEW CONCURRENTLY

2025-02-23 Thread Tobias McNulty
On Sun, Feb 23, 2025 at 10:21 AM Greg Sabino Mullane  wrote:
> This is the correct interpretation. A regular refresh simply runs the query 
> and replaces the old view, regardless of the number of rows that have 
> changed. A concurrent refresh runs the query and updates the rows in place, 
> so it is very sensitive as to how many of those rows have changed. This also 
> means that many concurrent refreshes can lead to table bloat. And it will 
> generate more WAL than a regular refresh.
>
> My takeaway: use regular refresh when you can. Switch to concurrent if the 
> number of changes is very small, or if constant client access to the view is 
> very important.

This makes sense to me. Many thanks.

Cheers,
Tobias




Default Value Retention After Dropping Default

2025-02-23 Thread Marcelo Fernandes
Hi folks,

I am experiencing an interesting behavior in PostgreSQL and would like to seek
some clarification.

In the following snippet, I first add a column with a default value, then drop
that default. However, when I query the table, the column still retains the
dropped default for existing rows:

  SET client_min_messages=debug1;

  DROP TABLE IF EXISTS foo CASCADE;
  CREATE TABLE foo (id SERIAL PRIMARY KEY);

  INSERT INTO foo (id) SELECT generate_series(1, 1);

  ALTER TABLE foo ADD COLUMN bar varchar(255) NOT NULL DEFAULT 'default';
  ALTER TABLE foo ALTER COLUMN bar DROP DEFAULT;

  SELECT * from foo order by id desc limit 5;
  --   id   |   bar
  -- ---+-
  --  1 | default
  --    | default
  --   9998 | default
  --   9997 | default
  --   9996 | default

In this example, even after dropping the default value from the bar column, the
rows that were previously inserted (prior to dropping the default) still show
'default' as their value in the bar column.

It does not see that the table has been rewritten or rescanned, otherwise the
debug1 messages would be triggered.

Can anyone explain how PostgreSQL "knows about" the default value that has just
been dropped and what is happened under the scenes? I am keen on a deep
understanding on how Postgres achieves this.

Here is what I could find in the docs, but it does not satisfy my question:

> From PostgreSQL 11, adding a column with a constant default value no longer
> means that each row of the table needs to be updated when the ALTER TABLE
> statement is executed. Instead, the default value will be returned the next
> time the row is accessed, and applied when the table is rewritten, making the
> ALTER TABLE very fast even on large tables.

https://www.postgresql.org/docs/current/ddl-alter.html#DDL-ALTER-ADDING-A-COLUMN