postgresql.git
16 months agoSupport identity columns in partitioned tables
Peter Eisentraut [Tue, 16 Jan 2024 16:16:14 +0000 (17:16 +0100)]
Support identity columns in partitioned tables

Previously, identity columns were disallowed on partitioned tables.
(The reason was mainly that no one had gotten around to working
through all the details to make it work.)  This makes it work now.

Some details on the behavior:

* A newly created partition inherits identity property

  The partitions of a partitioned table are integral part of the
  partitioned table.  A partition inherits identity columns from the
  partitioned table.  An identity column of a partition shares the
  identity space with the corresponding column of the partitioned
  table.  In other words, the same identity column across all
  partitions of a partitioned table share the same identity space.
  This is effected by sharing the same underlying sequence.

  When INSERTing directly into a partition, the sequence associated
  with the topmost partitioned table is used to calculate the value of
  the corresponding identity column.

  In regular inheritance, identity columns and their properties in a
  child table are independent of those in its parent tables.  A child
  table does not inherit identity columns or their properties
  automatically from the parent.  (This is unchanged.)

* Attached partition inherits identity column

  A table being attached as a partition inherits the identity property
  from the partitioned table.  This should be fine since we expect
  that the partition table's column has the same type as the
  partitioned table's corresponding column.  If the table being
  attached is a partitioned table, the identity properties are
  propagated down its partition hierarchy.

  An identity column in the partitioned table is also marked as NOT
  NULL.  The corresponding column in the partition needs to be marked
  as NOT NULL for the attach to succeed.

* Drop identity property when detaching partition

  A partition's identity column shares the identity space
  (i.e. underlying sequence) as the corresponding column of the
  partitioned table.  If a partition is detached it can longer share
  the identity space as before.  Hence the identity columns of the
  partition being detached loose their identity property.

  When identity of a column of a regular table is dropped it retains
  the NOT NULL constraint that came with the identity property.
  Similarly the columns of the partition being detached retain the NOT
  NULL constraints that came with identity property, even though the
  identity property itself is lost.

  The sequence associated with the identity property is linked to the
  partitioned table (and not the partition being detached).  That
  sequence is not dropped as part of detach operation.

* Partitions with their own identity columns are not allowed.

* The usual ALTER operations (add identity column, add identity
  property to existing column, alter properties of an indentity
  column, drop identity property) are supported for partitioned
  tables.  Changing a column only in a partitioned table or a
  partition is not allowed; the change needs to be applied to the
  whole partition hierarchy.

Author: Ashutosh Bapat <[email protected]>
Reviewed-by: Peter Eisentraut <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/CAExHW5uOykuTC+C6R1yDSp=o8Q83jr8xJdZxgPkxfZ1Ue5RRGg@mail.gmail.com

16 months agoDecorate WITH with literal markup tags
Daniel Gustafsson [Tue, 16 Jan 2024 12:51:15 +0000 (13:51 +0100)]
Decorate WITH with literal markup tags

One instance of "WITH clause" was not using <literal> tags around
WITH, while others were, so add markup to the last one to ensure
consistency.  Backpatch to v15 where MERGE was added.

Reported-by: jian he <[email protected]>
Discussion: https://postgr.es/m/CACJufxGJKY9ZCPV2WDM6xFsXq9C8r7r3vU6AkScN+p9k6CEpMw@mail.gmail.com
Backpatch-through: v15

16 months agoAdd missing PGDLLIMPORT markings
Heikki Linnakangas [Tue, 16 Jan 2024 11:53:28 +0000 (13:53 +0200)]
Add missing PGDLLIMPORT markings

Since commit 8ec569479f, we have a policy of marking all backend
variables with PGDLLIMPORT.

Reported-by: Anton A. Melnikov
Discussion: https://www.postgresql.org/message-id/0b78546c-ffef-4cd9-9ba1-d1e6aab88cea@postgrespro.ru

16 months agostruct XmlTableRoutine: use C99 designated initializers
Alvaro Herrera [Tue, 16 Jan 2024 11:48:30 +0000 (12:48 +0100)]
struct XmlTableRoutine: use C99 designated initializers

As in c27f8621eed et al.

Not as critical as other cases we've handled, but I figure if we're
going to add JsonbTableRoutine using TableFuncRoutine, this makes it
easier to jump around the code.

16 months agoDon't test already-referenced pointer for nullness
Alvaro Herrera [Tue, 16 Jan 2024 11:27:52 +0000 (12:27 +0100)]
Don't test already-referenced pointer for nullness

Commit b8ba7344e9eb added in PQgetResult a derefence to a pointer
returned by pqPrepareAsyncResult(), before some other code that was
already testing that pointer for nullness.  But since commit
618c16707a6d (in Postgres 15), pqPrepareAsyncResult() doesn't ever
return NULL (a statically-allocated result is returned if OOM).  So in
branches 15 and up, we can remove the redundant pointer check with no
harm done.

However, in branch 14, pqPrepareAsyncResult() can indeed return NULL if
it runs out of memory.  Fix things there by adding a null pointer check
before dereferencing the pointer.  This should hint Coverity that the
preexisting check is not redundant but necessary.

Backpatch to 14, like b8ba7344e9eb.

Per Coverity.

16 months agodoc: Add Identity Column section under Data Definition chapter
Peter Eisentraut [Tue, 16 Jan 2024 08:42:40 +0000 (09:42 +0100)]
doc: Add Identity Column section under Data Definition chapter

This seems to be missing since identity column support was added.  Add
the same.  This gives a place to document various pieces of
information in one place that are currently distributed over several
command reference pages or just not documented (e.g., inheritance
behavior).

Author: Ashutosh Bapat <[email protected]>
Author: Peter Eisentraut <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/CAExHW5uOykuTC+C6R1yDSp=o8Q83jr8xJdZxgPkxfZ1Ue5RRGg@mail.gmail.com

16 months agoAssert that partition inherits from only one parent in MergeAttributes()
Peter Eisentraut [Tue, 16 Jan 2024 07:57:35 +0000 (08:57 +0100)]
Assert that partition inherits from only one parent in MergeAttributes()

A partition inherits only from one partitioned table and thus inherits
a column definition only once.  Assert the same in MergeAttributes()
and simplify a condition accordingly.

Similar definition exists about line 3068 in the same function.

Author: Ashutosh Bapat <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/CAExHW5uOykuTC+C6R1yDSp=o8Q83jr8xJdZxgPkxfZ1Ue5RRGg@mail.gmail.com

16 months agodoc: Decorate PostgreSQL with productname tag
Peter Eisentraut [Tue, 16 Jan 2024 07:56:00 +0000 (08:56 +0100)]
doc: Decorate PostgreSQL with productname tag

... in the section about Generated Columns.

Author: Ashutosh Bapat <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/CAExHW5uOykuTC+C6R1yDSp=o8Q83jr8xJdZxgPkxfZ1Ue5RRGg@mail.gmail.com

16 months agolibpq: Add PQsendPipelineSync()
Michael Paquier [Tue, 16 Jan 2024 01:13:42 +0000 (10:13 +0900)]
libpq: Add PQsendPipelineSync()

This new function is equivalent to PQpipelineSync(), except that it does
not flush anything to the server except if the size threshold of the
output buffer is reached; the user must subsequently call PQflush()
instead.

Its purpose is to reduce the system call overhead of pipeline mode, by
giving to applications more control over the timing of the flushes when
manipulating commands in pipeline mode.

Author: Anton Kirilov
Reviewed-by: Jelte Fennema-Nio, Robert Haas, Álvaro Herrera, Denis
Laxalde, Michael Paquier
Discussion: https://postgr.es/m/CACV6eE5arHFZEA717=iKEa_OewpVFfWJOmsOdGrqqsr8CJVfWQ@mail.gmail.com

16 months agoFix a typo and some doc indentation related to libpq pipeline functions
Michael Paquier [Tue, 16 Jan 2024 00:17:51 +0000 (09:17 +0900)]
Fix a typo and some doc indentation related to libpq pipeline functions

Noticed while reviewing the area for a different patch.  This is
cosmetic, so no backpatch is done.

16 months agoFix typos.
Robert Haas [Mon, 15 Jan 2024 16:50:44 +0000 (11:50 -0500)]
Fix typos.

Alexander Lakhin

Discussion: http://postgr.es/m/212b0987-83e5-e2ae-c5e8-b8170fdaf3a0@gmail.com

16 months agoFix 'negative bitmapset member' error
Alexander Korotkov [Mon, 15 Jan 2024 15:45:16 +0000 (17:45 +0200)]
Fix 'negative bitmapset member' error

When removing a useless join, we'd remove PHVs that are not used at join
partner rels or above the join.  A PHV that references the join's relid
in ph_eval_at is logically "above" the join and thus should not be
removed.  We have the following check for that:

    !bms_is_member(ojrelid, phinfo->ph_eval_at)

However, in the case of SJE removing a useless inner join, 'ojrelid' is
set to -1, which would trigger the "negative bitmapset member not
allowed" error in bms_is_member().

Fix it by skipping examining ojrelid for inner joins in this check.

Reported-by: Zuming Jiang
Bug: #18260
Discussion: https://postgr.es/m/18260-1b6a0c4ae311b837%40postgresql.org
Author: Richard Guo
Reviewed-by: Andrei Lepikhov
16 months agoAvoid useless ReplicationOriginExitCleanup locking
Alvaro Herrera [Mon, 15 Jan 2024 12:02:03 +0000 (13:02 +0100)]
Avoid useless ReplicationOriginExitCleanup locking

When session_replication_state is NULL, we can know there's nothing to
do with no lock acquisition.  Do that.

Author: Bharath Rupireddy <[email protected]>
Discussion: https://postgr.es/m/CALj2ACX+YaeRU5xJqR4C7kLsTO_F7DBRNF8WgeHvJZcKtNuK_A@mail.gmail.com

16 months agoReduce dependency to money data type in main regression test suite
Michael Paquier [Mon, 15 Jan 2024 00:30:16 +0000 (09:30 +0900)]
Reduce dependency to money data type in main regression test suite

Most of these tests have been introduced in 6dd8b0080787, to check for
behaviors related to hashing and hash plans, and money is a data type
with btree support but no hash functions.  These tests are switched to
use varbit instead, to provide the same coverage.

Some other tests historically used money but don't really need it for
what they wanted to test (see rules.sql).  Plans and coverage are
unchanged after the modifications done here.

Support for money may be removed a a later point, but this needs more
discussion.

Discussion: https://postgr.es/m/18240-c5da758d7dc1ecf0@postgresql.org

16 months agoPrevent access to an unpinned buffer in BEFORE ROW UPDATE triggers.
Tom Lane [Sun, 14 Jan 2024 17:38:41 +0000 (12:38 -0500)]
Prevent access to an unpinned buffer in BEFORE ROW UPDATE triggers.

When ExecBRUpdateTriggers switches to a new target tuple as a result
of the EvalPlanQual logic, it must form a new proposed update tuple.
Since commit 86dc90056, that tuple (the result of
ExecGetUpdateNewTuple) has been a virtual tuple that might contain
pointers to by-ref fields of the new target tuple (in "oldslot").
However, immediately after that we materialize oldslot, causing it to
drop its buffer pin, whereupon the by-ref pointers are unsafe to use.
This is a live bug only when the new target tuple is in a different
page than the original target tuple, since we do still hold a pin on
the original one.  (Before 86dc90056, there was no bug because the
EPQ plantree would hold a pin on the new target tuple; but now that's
not assured.)  To fix, forcibly materialize the new tuple before we
materialize oldslot.  This costs nothing since we would have done that
shortly anyway.

The real-world impact of this is probably minimal.  A visible failure
could occur if the new target tuple's buffer were recycled for some
other page in the short interval before we materialize newslot within
the trigger-calling loop; but that's quite unlikely given that we'd
just touched that page.  There's a larger hazard that some other
process could prune and repack that page within the window.  We have
lock on the new target tuple, but that wouldn't prevent it being moved
on the page.

Alexander Lakhin and Tom Lane, per bug #17798 from Alexander Lakhin.
Back-patch to v14 where 86dc90056 came in.

Discussion: https://postgr.es/m/17798-0907404928dcf0dd@postgresql.org

16 months agopg_dump: Remove obsolete trigger support
Peter Eisentraut [Sun, 14 Jan 2024 06:53:59 +0000 (07:53 +0100)]
pg_dump: Remove obsolete trigger support

Remove for dumping triggers from pre-9.2 servers.  This should have
been removed as part of 30e7c175b81.

Reviewed-by: Tom Lane <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/56c8f5bf-de47-48c1-a592-588fb526e9e6%40eisentraut.org

16 months agodoc: Update catalog documentation for attstattarget nullable
Peter Eisentraut [Sun, 14 Jan 2024 06:46:01 +0000 (07:46 +0100)]
doc: Update catalog documentation for attstattarget nullable

Fixup for 4f622503d6.

16 months agoRemove useless Assert
Peter Eisentraut [Sun, 14 Jan 2024 06:29:45 +0000 (07:29 +0100)]
Remove useless Assert

It's already included in the subsequent intVal() call.  Fixup for
4f622503d6.

16 months agoEscape output of pg_amcheck test
Peter Eisentraut [Sat, 13 Jan 2024 19:29:01 +0000 (20:29 +0100)]
Escape output of pg_amcheck test

The pg_amcheck test reports a skip message if the layout of the index
does not match expectations.  That message includes the bytes that
were expected and the ones that were found.  But the found ones are
arbitrary bytes, which can have funny effects on the terminal when
they are printed.  To avoid that, escape non-word characters before
printing.

Reviewed-by: Aleksander Alekseev <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/3f96f079-64e5-468a-8a19-cb481f0d31e5%40eisentraut.org

16 months agoAdd previous commit to .git-blame-ignore-revs.
Tom Lane [Sat, 13 Jan 2024 18:56:46 +0000 (13:56 -0500)]
Add previous commit to .git-blame-ignore-revs.

Discussion: https://postgr.es/m/1393953.1698353013@sss.pgh.pa.us
Discussion: https://postgr.es/m/CAGjhLkOoBEC9mLsnB42d3CO1vcMx71MLSEuigeABbQ8oRdA6gw@mail.gmail.com

16 months agoRe-pgindent catcache.c after previous commit.
Tom Lane [Sat, 13 Jan 2024 18:54:11 +0000 (13:54 -0500)]
Re-pgindent catcache.c after previous commit.

Discussion: https://postgr.es/m/1393953.1698353013@sss.pgh.pa.us
Discussion: https://postgr.es/m/CAGjhLkOoBEC9mLsnB42d3CO1vcMx71MLSEuigeABbQ8oRdA6gw@mail.gmail.com

16 months agoCope with catcache entries becoming stale during detoasting.
Tom Lane [Sat, 13 Jan 2024 18:46:27 +0000 (13:46 -0500)]
Cope with catcache entries becoming stale during detoasting.

We've long had a policy that any toasted fields in a catalog tuple
should be pulled in-line before entering the tuple in a catalog cache.
However, that requires access to the catalog's toast table, and we'll
typically do AcceptInvalidationMessages while opening the toast table.
So it's possible that the catalog tuple is outdated by the time we
finish detoasting it.  Since no cache entry exists yet, we can't
mark the entry stale during AcceptInvalidationMessages, and instead
we'll press forward and build an apparently-valid cache entry.  The
upshot is that we have a race condition whereby an out-of-date entry
could be made in a backend's catalog cache, and persist there
indefinitely causing indeterminate misbehavior.

To fix, use the existing systable_recheck_tuple code to recheck
whether the catalog tuple is still up-to-date after we finish
detoasting it.  If not, loop around and restart the process of
searching the catalog and constructing cache entries from the top.
The case is rare enough that this shouldn't create any meaningful
performance penalty, even in the SearchCatCacheList case where
we need to tear down and reconstruct the whole list.

Indeed, the case is so rare that AFAICT it doesn't occur during
our regression tests, and there doesn't seem to be any easy way
to build a test that would exercise it reliably.  To allow
testing of the retry code paths, add logic (in USE_ASSERT_CHECKING
builds only) that randomly pretends that the recheck failed about
one time out of a thousand.  This is enough to ensure that we'll
pass through the retry paths during most regression test runs.

By adding an extra level of looping, this commit creates a need
to reindent most of SearchCatCacheMiss and SearchCatCacheList.
I'll do that separately, to allow putting those changes in
.git-blame-ignore-revs.

Patch by me; thanks to Alexander Lakhin for having built a test
case to prove the bug is real, and to Xiaoran Wang for review.
Back-patch to all supported branches.

Discussion: https://postgr.es/m/1393953.1698353013@sss.pgh.pa.us
Discussion: https://postgr.es/m/CAGjhLkOoBEC9mLsnB42d3CO1vcMx71MLSEuigeABbQ8oRdA6gw@mail.gmail.com

16 months agoMake attstattarget nullable
Peter Eisentraut [Sat, 13 Jan 2024 17:14:53 +0000 (18:14 +0100)]
Make attstattarget nullable

This changes the pg_attribute field attstattarget into a nullable
field in the variable-length part of the row.  If no value is set by
the user for attstattarget, it is now null instead of previously -1.
This saves space in pg_attribute and tuple descriptors for most
practical scenarios.  (ATTRIBUTE_FIXED_PART_SIZE is reduced from 108
to 104.)  Also, null is the semantically more correct value.

The ANALYZE code internally continues to represent the default
statistics target by -1, so that that code can avoid having to deal
with null values.  But that is now contained to the ANALYZE code.
Only the DDL code deals with attstattarget possibly null.

For system columns, the field is now always null.  The ANALYZE code
skips system columns anyway.

To set a column's statistics target to the default value, the new
command form ALTER TABLE ... SET STATISTICS DEFAULT can be used.  (SET
STATISTICS -1 still works.)

Reviewed-by: Alvaro Herrera <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/4da8d211-d54d-44b9-9847-f2a9f1184c76@eisentraut.org

16 months agoFix memory leak in connection string validation.
Jeff Davis [Sat, 13 Jan 2024 05:38:16 +0000 (21:38 -0800)]
Fix memory leak in connection string validation.

Introduced in commit c3afe8cf5a.

Discussion: https://postgr.es/m/066a65233d3cb4ea27a9e0778d2f1d0dc764b222[email protected]
Reviewed-by: Nathan Bossart, Tom Lane
Backpatch-through: 16

16 months agoAdd empty placeholder LINGUAS file for pg_walsummary
Alvaro Herrera [Fri, 12 Jan 2024 22:20:03 +0000 (23:20 +0100)]
Add empty placeholder LINGUAS file for pg_walsummary

Like bbf1f1340800.

16 months agoRe-validate connection string in libpqrcv_connect().
Jeff Davis [Fri, 12 Jan 2024 21:41:36 +0000 (13:41 -0800)]
Re-validate connection string in libpqrcv_connect().

A superuser may create a subscription with password_required=true, but
which uses a connection string without a password.

Previously, if the owner of such a subscription was changed to a
non-superuser, the non-superuser was able to utilize a password from
another source (like a password file or the PGPASSWORD environment
variable), which should not have been allowed.

This commit adds a step to re-validate the connection string before
connecting.

Reported-by: Jeff Davis
Author: Vignesh C
Reviewed-by: Peter Smith, Robert Haas, Amit Kapila
Discussion: https://www.postgresql.org/message-id/flat/e5892973ae2a80a1a3e0266806640dae3c428100.camel%40j-davis.com
Backpatch-through: 16

16 months agoRefactor ATExecAddColumn() to use BuildDescForRelation()
Peter Eisentraut [Fri, 12 Jan 2024 15:05:15 +0000 (16:05 +0100)]
Refactor ATExecAddColumn() to use BuildDescForRelation()

BuildDescForRelation() has all the knowledge for converting a
ColumnDef into pg_attribute/tuple descriptor.  ATExecAddColumn() can
make use of that, instead of duplicating all that logic.  We just pass
a one-element list of ColumnDef and we get back exactly the data
structure we need.  Note that we don't even need to touch
BuildDescForRelation() to make this work.

Reviewed-by: Alvaro Herrera <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/52a125e4-ff9a-95f5-9f61-b87cf447e4da@eisentraut.org

16 months agoFix typo.
Robert Haas [Fri, 12 Jan 2024 14:46:46 +0000 (09:46 -0500)]
Fix typo.

Noriyoshi Shinoda

Discussion: http://postgr.es/m/DM4PR84MB17347D9466419413698105D8EE6F2@DM4PR84MB1734.NAMPRD84.PROD.OUTLOOK.COM

16 months agoAdded literal tag for RETURNING
Alvaro Herrera [Fri, 12 Jan 2024 11:44:20 +0000 (12:44 +0100)]
Added literal tag for RETURNING

This is an old mistake (92e38182d7c8); backpatch all the way back.

Author: Atsushi Torikoshi <[email protected]>
Reviewed-by: Ashutosh Bapat <[email protected]>
Discussion: https://postgr.es/m/c0aa00b60a16c0ea2a4c5123b013acb9@oss.nttdata.com

16 months agoFix pg_walsummary's .gitignore
Michael Paquier [Fri, 12 Jan 2024 04:40:58 +0000 (13:40 +0900)]
Fix pg_walsummary's .gitignore

It missed a entry for tmp_check/ generated by the tests.  While on it,
append a slash at the beginning of "pg_walsummary" to restrict its check
to the current directory, like anywhere else.

Oversights in ee1bfd168390.

16 months agoRefactor code checking for file existence
Michael Paquier [Fri, 12 Jan 2024 03:04:51 +0000 (12:04 +0900)]
Refactor code checking for file existence

jit.c and dfgr.c had a copy of the same code to check if a file exists
or not, with a twist: jit.c did not check for EACCES when failing the
stat() call for the path whose existence is tested.  This refactored
routine will be used by an upcoming patch.

Reviewed-by: Ashutosh Bapat
Discussion: https://postgr.es/m/[email protected]

16 months agoRework how logirep launchers are stopped during pg_upgrade
Michael Paquier [Thu, 11 Jan 2024 23:23:07 +0000 (08:23 +0900)]
Rework how logirep launchers are stopped during pg_upgrade

This is a rework of 7021d3b17664, where we relied on forcing
max_logical_replication_workers to 0 in the postgres command.  This
commit now prevents logical replication launchers to start using -b and
a backend-side check based on IsBinaryUpgrade, effective when upgrading
from 17 and newer versions.

This commit improves the comments explaining why this restriction is
necessary.

This discussion was on hold until we were sure how to add support for
subscribers in pg_upgrade, something now done thanks to 9a17be1e244a.

Reviewed-by: Álvaro Herrera, Amit Kapila, Tom Lane
Discussion: https://postgr.es/m/[email protected]

16 months agoFix some inconsistent whitespace in Perl file
Peter Eisentraut [Thu, 11 Jan 2024 21:23:44 +0000 (22:23 +0100)]
Fix some inconsistent whitespace in Perl file

16 months agoFix incorrect format placeholder
Peter Eisentraut [Thu, 11 Jan 2024 20:49:00 +0000 (21:49 +0100)]
Fix incorrect format placeholder

16 months agoCleanup for unicode-update build target and test.
Jeff Davis [Thu, 11 Jan 2024 20:20:25 +0000 (12:20 -0800)]
Cleanup for unicode-update build target and test.

In preparation for adding more Unicode tables.

Discussion: https://postgr.es/m/63cd8625-68fa-4760-844a-6b7f643336f2@ardentperf.com
Reviewed-by: Jeremy Schneider
16 months agoAllow subquery pullup to wrap a PlaceHolderVar in another one.
Tom Lane [Thu, 11 Jan 2024 20:28:13 +0000 (15:28 -0500)]
Allow subquery pullup to wrap a PlaceHolderVar in another one.

The code for wrapping subquery output expressions in PlaceHolderVars
believed that if the expression already was a PlaceHolderVar, it was
never necessary to wrap that in another one.  That's wrong if the
expression is underneath an outer join and involves a lateral
reference to outside that scope: failing to add an additional PHV
risks evaluating the expression at the wrong place and hence not
forcing it to null when the outer join should do so.  This is an
oversight in commit 9e7e29c75, which added logic to forcibly wrap
lateral-reference Vars in PlaceHolderVars, but didn't see that the
adjacent case for PlaceHolderVars needed the same treatment.

The test case we have for this doesn't fail before 4be058fe9, but now
that I see the problem I wonder if it is possible to demonstrate
related errors before that.  That's moot though, since all such
branches are out of support.

Per bug #18284 from Holger Reise.  Back-patch to all supported
branches.

Discussion: https://postgr.es/m/18284-47505a20c23647f8@postgresql.org

16 months agoTry to fix pg_walsummary buildfarm failures.
Robert Haas [Thu, 11 Jan 2024 20:01:51 +0000 (15:01 -0500)]
Try to fix pg_walsummary buildfarm failures.

Apparently the new tuple isn't guaranteed to end up at the end of
the relation, so make the test not depend on that happening.

16 months agoRemove hastup from LVPagePruneState.
Robert Haas [Thu, 11 Jan 2024 18:22:04 +0000 (13:22 -0500)]
Remove hastup from LVPagePruneState.

Instead, just have lazy_scan_prune() and lazy_scan_noprune() update
LVRelState->nonempty_pages directly. This makes the two functions
more similar and also removes makes lazy_scan_noprune need one fewer
output parameters.

Melanie Plageman, reviewed by Andres Freund, Michael Paquier, and me

Discussion: http://postgr.es/m/CAAKRu_btji_wQdg=ok-5E4v_bGVxKYnnFFe7RA6Frc1EcOwtSg@mail.gmail.com

16 months agoReindent after commit d9ef650fca7bc574586f4171cd929cfd5240326e.
Robert Haas [Thu, 11 Jan 2024 18:24:35 +0000 (13:24 -0500)]
Reindent after commit d9ef650fca7bc574586f4171cd929cfd5240326e.

16 months agoRepair various defects in dc212340058b4e7ecfc5a7a81ec50e7a207bf288.
Robert Haas [Thu, 11 Jan 2024 18:06:10 +0000 (13:06 -0500)]
Repair various defects in dc212340058b4e7ecfc5a7a81ec50e7a207bf288.

pg_combinebackup had various problems:

* strncpy was used in various places where strlcpy should be used
  instead, to avoid any possibility of the result not being
  \0-terminated.
* scan_for_existing_tablespaces() failed to close the directory,
  and an error when opening the directory was reported with the
  wrong pathname.
* write_reconstructed_file() contained some redundant and therefore
  dead code.
* flush_manifest() didn't check the result of pg_checksum_update()
  as we do in other places, and misused a local pathname variable
  that shouldn't exist at all.

In pg_basebackup, the wrong variable name was used in one place,
due to a copy and paste that was not properly adjusted.

In blkreftable.c, the loop incorrectly doubled chunkno instead of
max_chunks. Fix that. Also remove a nearby assertion per repeated
off-list complaints from Tom Lane.

Per Coverity and subsequent code inspection by me and by Tom Lane.

Discussion: http://postgr.es/m/CA+Tgmobvqqj-DW9F7uUzT-cQqs6wcVb-Xhs=w=hzJnXSE-kRGw@mail.gmail.com

16 months agoAdd new pg_walsummary tool.
Robert Haas [Thu, 11 Jan 2024 17:47:28 +0000 (12:47 -0500)]
Add new pg_walsummary tool.

This can dump the contents of the WAL summary files found in
pg_wal/summaries. Normally, this shouldn't really be something anyone
needs to do, but it may be needed for debugging problems with
incremental backup, or could possibly be useful to external tools.

Discussion: http://postgr.es/m/CA+Tgmobvqqj-DW9F7uUzT-cQqs6wcVb-Xhs=w=hzJnXSE-kRGw@mail.gmail.com

16 months agoAdd new function pg_get_wal_summarizer_state().
Robert Haas [Thu, 11 Jan 2024 17:41:18 +0000 (12:41 -0500)]
Add new function pg_get_wal_summarizer_state().

This makes it possible to access information about the progress
of WAL summarization from SQL. The previously-added functions
pg_available_wal_summaries() and pg_wal_summary_contents() only
examine on-disk state, but this function exposes information from
the server's shared memory.

Discussion: http://postgr.es/m/CA+Tgmobvqqj-DW9F7uUzT-cQqs6wcVb-Xhs=w=hzJnXSE-kRGw@mail.gmail.com

16 months agoClarify which xml tools are missing in meson error message
Magnus Hagander [Thu, 11 Jan 2024 13:55:43 +0000 (14:55 +0100)]
Clarify which xml tools are missing in meson error message

16 months agoFix omission in partitioning limitation documentation
Magnus Hagander [Thu, 11 Jan 2024 13:27:10 +0000 (14:27 +0100)]
Fix omission in partitioning limitation documentation

UNIQUE and PRIMARY KEY constraints can be created on ONLY the
partitioned table.  We already had an example demonstrating that,
but forgot to mention it in the documentation of the limits of
partitioning.

Author: Laurenz Albe
Reviewed-By: shihao zhong, Shubham Khanna, Ashutosh Bapat
Backpatch-through: 12
Discussion: https://postgr.es/m/167299368731.659.16130012959616771853@wrigleys.postgresql.org

16 months agoFix an intermetant BF failure in 003_logical_slots.
Amit Kapila [Thu, 11 Jan 2024 03:26:27 +0000 (08:56 +0530)]
Fix an intermetant BF failure in 003_logical_slots.

During upgrade, when pg_restore performs CREATE DATABASE, bgwriter or
checkpointer may flush buffers and hold a file handle for pg_largeobject,
so later TRUNCATE pg_largeobject command will fail if OS (such as older
Windows versions) doesn't remove an unlinked file completely till it's
open. The probability of seeing this behavior is higher in this test
because we use wal_level as logical via allows_streaming => 'logical'
which in turn set shared_buffers as 1MB and make it more probable for
bgwriter to hold the file handle.

Diagnosed-by: Alexander Lakhin
Author: Hayato Kuroda, Amit Kapila
Reviewed-by: Alexander Lakhin
Discussion: https://postgr.es/m/TYAPR01MB5866AB7FD922CE30A2565B8BF5A8A@TYAPR01MB5866.jpnprd01.prod.outlook.com

16 months agoUpdate documentation of default fdw_tuple_cost
John Naylor [Thu, 11 Jan 2024 01:58:18 +0000 (08:58 +0700)]
Update documentation of default fdw_tuple_cost

Follow up to cac169d68

Umair Shahid

Reviewed by Richard Guo and Chris Travers

Discussion: https://postgr.es/m/CALVUYo9RECc5gwD%2B4SRM5Es%2Bbg9cpNfhd3_qUjf7kVTGyLpFJg%40mail.gmail.com

16 months agopg_regress: Disable autoruns for cmd.exe on Windows
Michael Paquier [Thu, 11 Jan 2024 01:39:58 +0000 (10:39 +0900)]
pg_regress: Disable autoruns for cmd.exe on Windows

This is similar to 9886744a361b, to prevent the execution of other
programs due to autorun configurations which could influence the
postmaster startup.

Like the other change, no backpatch is done.

Discussion: https://postgr.es/m/20230922.161551.320043332510268554[email protected]

16 months agoRestore initdb's old behavior of always setting the lc_xxx GUCs.
Tom Lane [Wed, 10 Jan 2024 23:09:29 +0000 (18:09 -0500)]
Restore initdb's old behavior of always setting the lc_xxx GUCs.

In commit 3e51b278d I (tgl) caused initdb to leave lc_messages and
other lc_xxx GUCs commented-out in the installed postgresql.conf file
if they were going to be set to 'C'.  This was a hack for cosmetic
purposes, and it was buggy because lc_messages' wired-in default is
not 'C' but '' (empty string).  That led to --no-locale not having
the expected effect, since the postmaster would then obtain
lc_messages from its startup environment.

Let's just revert to the prior behavior of always de-commenting the
lc_xxx entries; the argument for changing that longstanding behavior
was weak in the first place.

Also, fix postgresql.conf.sample's erroneous claim that the default
value of lc_messages is 'C'.  I suspect that was what misled me into
making this mistake in the first place.

Report and patch by Kyotaro Horiguchi.  Back-patch to v16 where
the problem was introduced.

Discussion: https://postgr.es/m/20231122.162700.1995154567625541112[email protected]

16 months agoAllow noise semicolons ending psql \sf, \ef, \sv, \ev commands.
Tom Lane [Wed, 10 Jan 2024 19:20:09 +0000 (14:20 -0500)]
Allow noise semicolons ending psql \sf, \ef, \sv, \ev commands.

Many psql backslash commands tolerate trailing semicolons, even
though that's not part of the official syntax.  These did not.
They tried to, by passing semicolon = true to psql_scan_slash_option,
but that function ignored this parameter in OT_WHOLE_LINE mode.
Teach it to do the right thing, and remove the now-duplicative
logic in exec_command_help.

Discussion: https://postgr.es/m/2012251.1704746912@sss.pgh.pa.us

16 months agoFix Asserts in calc_non_nestloop_required_outer().
Tom Lane [Wed, 10 Jan 2024 18:51:36 +0000 (13:51 -0500)]
Fix Asserts in calc_non_nestloop_required_outer().

These were not testing the same thing as the comparable Assert
in calc_nestloop_required_outer(), because we neglected to map
the given Paths' relids to top-level relids.  When considering
a partition child join the latter is the correct thing to do.

This oversight is old, but since it's only an overly-weak Assert
check there doesn't seem to be much value in back-patching.

Richard Guo (with cosmetic changes and comment updates by me)

Discussion: https://postgr.es/m/CAMbWs49sqbe9GBZ8sy8dSfKRNURgicR85HX8vgzcgQsPF0XY1w@mail.gmail.com

16 months agoHandle WindowClause.runCondition in tree walker/mutator functions.
Tom Lane [Wed, 10 Jan 2024 18:36:33 +0000 (13:36 -0500)]
Handle WindowClause.runCondition in tree walker/mutator functions.

Commit 9d9c02ccd, which added the notion of a "run condition" for
window functions, neglected to teach nodeFuncs.c to process the new
field.  Remarkably, that doesn't seem to have had any ill effects
before we invented Var.varnullingrels, but now it can cause visible
failures in join-removal scenarios.

I have no faith that there's not reachable problems in v15 too,
so back-patch the code change to v15 where 9d9c02ccd came in.
The test case seems irrelevant to v15, though.

Per bug #18277 from Zuming Jiang.  Diagnosis and patch by
Richard Guo.

Discussion: https://postgr.es/m/18277-089ead83b329a2fd@postgresql.org

16 months agodoc: clarify who owns the initdb-created cluster, by default
Bruce Momjian [Wed, 10 Jan 2024 16:20:15 +0000 (11:20 -0500)]
doc:  clarify who owns the initdb-created cluster, by default

Discussion: https://postgr.es/m/[email protected]

Backpatch-through: 16

16 months agoShow the default checkpoint method in the pg_basebackup help message
Magnus Hagander [Wed, 10 Jan 2024 12:30:10 +0000 (13:30 +0100)]
Show the default checkpoint method in the pg_basebackup help message

Author: Michael Banck
Reviewed-By: Aleksander Alekseev, Michael Paquier, Peter Eisentraut
Discussion: https://postgr.es/m/6530f954.170a0220[email protected]

16 months agopg_ctl: Disable autoruns for cmd.exe on Windows
Michael Paquier [Wed, 10 Jan 2024 01:41:57 +0000 (10:41 +0900)]
pg_ctl: Disable autoruns for cmd.exe on Windows

On Windows, cmd.exe is used to launch the postmaster process to ease its
redirection setup.  However, cmd.exe may execute other programs at
startup due to autorun configurations, which could influence the
postmaster startup.  This patch adds /D flag to the launcher cmd.exe
command line to disable autorun settings written in the registry.

This is arguably a bug, but no backpatch is done now out of caution.

Reported-by: Hayato Kuroda
Author: Kyotaro Horiguchi
Reviewed-by: Robert Haas, Michael Paquier
Discussion: https://postgr.es/m/20230922.161551.320043332510268554[email protected]

16 months agoDocument WAL summarization information functions.
Robert Haas [Tue, 9 Jan 2024 18:04:46 +0000 (13:04 -0500)]
Document WAL summarization information functions.

Commit 174c480508ac25568561443e6d4a82d5c1103487 added two new
information functions but failed to document them anywhere.

Discussion: http://postgr.es/m/CA+Tgmobvqqj-DW9F7uUzT-cQqs6wcVb-Xhs=w=hzJnXSE-kRGw@mail.gmail.com

16 months agoFix documentation for wal_summary_keep_time.
Nathan Bossart [Tue, 9 Jan 2024 17:35:10 +0000 (11:35 -0600)]
Fix documentation for wal_summary_keep_time.

The documentation for this parameter lists its type as boolean, but
it is actually an integer.  Furthermore, there is no mention of how
the value is interpreted when specified without units.  This commit
fixes these oversights in commit 174c480508.

Co-authored-by: Hubert Depesz Lubaczewski
Discussion: https://postgr.es/m/ZZwkujFihO2uqKwp%40depesz.com

16 months agoCross-check lists of predefined LWLocks.
Nathan Bossart [Tue, 9 Jan 2024 17:05:19 +0000 (11:05 -0600)]
Cross-check lists of predefined LWLocks.

Both lwlocknames.txt and wait_event_names.txt contain a list of all
the predefined LWLocks, i.e., those with predefined positions
within MainLWLockArray.  It is easy to miss one or the other,
especially since the list in wait_event_names.txt omits the "Lock"
suffix from all the LWLock wait events.  This commit adds a cross-
check of these lists to the script that generates lwlocknames.h.
If the lists do not match exactly, building will fail.

Suggested-by: Robert Haas
Reviewed-by: Robert Haas, Michael Paquier, Bertrand Drouvot
Discussion: https://postgr.es/m/20240102173120.GA1061678%40nathanxps13

16 months agoAdd new function, PQchangePassword(), to libpq
Joe Conway [Tue, 9 Jan 2024 14:16:48 +0000 (09:16 -0500)]
Add new function, PQchangePassword(), to libpq

Essentially this moves the non-interactive part of psql's "\password"
command into an exported client function. The password is not sent to the
server in cleartext because it is "encrypted" (in the case of scram and md5
it is actually hashed, but we have called these encrypted passwords for a
long time now) on the client side. This is good because it ensures the
cleartext password is never known by the server, and therefore won't end up
in logs, pg_stat displays, etc.

In other words, it exists for the same reason as PQencryptPasswordConn(), but
is more convenient as it both builds and runs the "ALTER USER" command for
you. PQchangePassword() uses PQencryptPasswordConn() to do the password
encryption. PQencryptPasswordConn() is passed a NULL for the algorithm
argument, hence encryption is done according to the server's
password_encryption setting.

Also modify the psql client to use the new function. That provides a builtin
test case. Ultimately drivers built on top of libpq should expose this
function and its use should be generally encouraged over doing ALTER USER
directly for password changes.

Author: Joe Conway
Reviewed-by: Tom Lane
Discussion: https://postgr.es/m/flat/b75955f7-e8cc-4bbd-817f-ef536bacbe93%40joeconway.com

16 months agoDoc: fix character_sets view.
Tatsuo Ishii [Tue, 9 Jan 2024 10:43:19 +0000 (19:43 +0900)]
Doc: fix character_sets view.

The note regarding character encoding form in "The Information Schema"
said that LATIN1 character repertoires only use one encoding form
LATIN1. This is not correct because LATIN1 has another encoding form
ISO-2022-JP-2. To fix this, replace LATIN1 with LATIN2, which is not
supported by ISO-2022-JP-2, thus it can be said that LATIN2 only uses
one encoding form.

Back-patch to supported branches.

Author: Tatsuo Ishii
Reviewed-by: Daniel Gustafsson
Discussion: https://postgr.es/m/flat/20240102.153925.1147403616414525145.t-ishii%40sranhm.sra.co.jp

16 months agoAn addition to 8c441c08279
Alexander Korotkov [Tue, 9 Jan 2024 08:12:14 +0000 (10:12 +0200)]
An addition to 8c441c08279

Given that now SJE doesn't work with result relation, turn a code dealing with
that into an assert that it shouldn't happen.

16 months agoForbid SJE with result relation
Alexander Korotkov [Tue, 9 Jan 2024 08:01:22 +0000 (10:01 +0200)]
Forbid SJE with result relation

The target relation for INSERT/UPDATE/DELETE/MERGE has a different behavior
than other relations in EvalPlanQual() and RETURNING clause.  This is why we
forbid target relation to be either source or target relation in SJE.
It's not clear if we could ever support this.

Reported-by: Alexander Lakhin
Discussion: https://postgr.es/m/b9e8f460-f9a6-0e9b-e8ba-60d59f0bc22c%40gmail.com

16 months agoFix misuse of RelOptInfo.unique_for_rels cache by SJE
Alexander Korotkov [Mon, 8 Jan 2024 22:08:35 +0000 (00:08 +0200)]
Fix misuse of RelOptInfo.unique_for_rels cache by SJE

When SJE uses RelOptInfo.unique_for_rels cache, it passes filtered quals to
innerrel_is_unique_ext().  That might lead to an invalid match to cache entries
made by previous non self-join checking calls.  Add UniqueRelInfo.self_join
flag to prevent such cases.  Also, fix that SJE should require a strict match
of outerrelids to make sure UniqueRelInfo.extra_clauses are valid.

Reported-by: Alexander Lakhin
Discussion: https://postgr.es/m/4788f781-31bd-9796-d7d6-588a751c8787%40gmail.com

16 months agoMake dblink interruptible, via new libpqsrv APIs.
Noah Misch [Mon, 8 Jan 2024 19:39:56 +0000 (11:39 -0800)]
Make dblink interruptible, via new libpqsrv APIs.

This replaces dblink's blocking libpq calls, allowing cancellation and
allowing DROP DATABASE (of a database not involved in the query).  Apart
from explicit dblink_cancel_query() calls, dblink still doesn't cancel
the remote side.  The replacement for the blocking calls consists of
new, general-purpose query execution wrappers in the libpqsrv facility.
Out-of-tree extensions should adopt these.  Use them in postgres_fdw,
replacing a local implementation from which the libpqsrv implementation
derives.  This is a bug fix for dblink.  Code inspection identified the
bug at least thirteen years ago, but user complaints have not appeared.
Hence, no back-patch for now.

Discussion: https://postgr.es/m/20231122012945[email protected]

16 months agoRemove excess #include "utils/wait_event.h".
Noah Misch [Mon, 8 Jan 2024 19:39:56 +0000 (11:39 -0800)]
Remove excess #include "utils/wait_event.h".

This simplifies copying libpq/libpq-be-fe-helpers.h into extensions,
because some supported PostgreSQL versions lack the other header.

Discussion: https://postgr.es/m/20231122012945[email protected]

16 months agoFix missing word in comment.
Noah Misch [Mon, 8 Jan 2024 19:39:56 +0000 (11:39 -0800)]
Fix missing word in comment.

16 months agoFix indentation in ExecParallelHashIncreaseNumBatches()
Alexander Korotkov [Mon, 8 Jan 2024 17:43:05 +0000 (19:43 +0200)]
Fix indentation in ExecParallelHashIncreaseNumBatches()

Backpatch-through: 12

16 months agoAllow examine_simple_variable() to work on INSERT RETURNING Vars.
Tom Lane [Mon, 8 Jan 2024 16:48:44 +0000 (11:48 -0500)]
Allow examine_simple_variable() to work on INSERT RETURNING Vars.

Since commit 599b33b94, this function assumed that every RTE_RELATION
RangeTblEntry would have an associated RelOptInfo.  But that's not so:
we only build RelOptInfos for relations that are scanned by the query.
In particular the target of an INSERT won't have one, so that Vars
appearing in an INSERT ... RETURNING list will not have an associated
RelOptInfo.  This apparently wasn't a problem before commit f7816aec2
taught examine_simple_variable() to drill down into CTEs containing
INSERT RETURNING, but it is now.

To fix, add a fallback code path that gets the userid to use directly
from the RTEPermissionInfo associated with the RTE.  (Sadly, we must
have two code paths, because not every RTE has a RTEPermissionInfo
either.)

Per report from Alexander Lakhin.  No back-patch, since the case is
apparently unreachable before f7816aec2.

Discussion: https://postgr.es/m/608a4886-6c60-0f9e-97d5-591256bd4150@gmail.com

16 months agoStrengthen tests for 5ef34a8fc3
Alexander Korotkov [Mon, 8 Jan 2024 13:00:42 +0000 (15:00 +0200)]
Strengthen tests for 5ef34a8fc3

The test query in 5ef34a8fc3 is running over the empty emp1 table giving the
same (empty) return both with and without the fix.  Add one row to that table
to make not just the test query plan, but also the test query result different.

Reported-by: Richard Guo
Bug: #18261
Discussion: https://postgr.es/m/CAMbWs49igjcszLgicb4D1N21_5iNDoxheJ7KFmAcs_z%3DLx6jhg%40mail.gmail.com

16 months agoFix integer-overflow problem in intarray's g_int_decompress().
Tom Lane [Sun, 7 Jan 2024 20:19:50 +0000 (15:19 -0500)]
Fix integer-overflow problem in intarray's g_int_decompress().

An array element equal to INT_MAX gave this code indigestion,
causing an infinite loop that surely ended in SIGSEGV.  We fixed
some nearby problems awhile ago (cf 757c5182f) but missed this.

Report and diagnosis by Alexander Lakhin (bug #18273); patch by me

Discussion: https://postgr.es/m/18273-9a832d1da122600c@postgresql.org

16 months agoFix oversized memory allocation in Parallel Hash Join
Alexander Korotkov [Sun, 7 Jan 2024 07:03:55 +0000 (09:03 +0200)]
Fix oversized memory allocation in Parallel Hash Join

During the calculations of the maximum for the number of buckets, take into
account that later we round that to the next power of 2.

Reported-by: Karen Talarico
Bug: #16925
Discussion: https://postgr.es/m/16925-ec96d83529d0d629%40postgresql.org
Author: Thomas Munro, Andrei Lepikhov, Alexander Korotkov
Reviewed-by: Alena Rybakina
Backpatch-through: 12

16 months agoFix the issue that SJE mistakenly omits qual clauses
Alexander Korotkov [Sat, 6 Jan 2024 12:09:39 +0000 (14:09 +0200)]
Fix the issue that SJE mistakenly omits qual clauses

When the SJE code handles the transfer of qual clauses from the removed
relation to the remaining one, it replaces the Vars of the removed
relation with the Vars of the remaining relation for each clause, and
then reintegrates these clauses into the appropriate restriction or join
clause lists, while attempting to avoid duplicates.

However, the code compares RestrictInfo->clause to determine if two
clauses are duplicates.  This is just flat wrong.  Two RestrictInfos
with the same clause can have different required_relids,
incompatible_relids, is_pushed_down, and so on.  This can cause qual
clauses to be mistakenly omitted, leading to wrong results.

This patch fixes it by comparing the entire RestrictInfos not just their
clauses ignoring 'rinfo_serial' field (otherwise almost all RestrictInfos will
be unique).  Making 'rinfo_serial' equal_ignore would break other code.  This
is why this commit implements our own comparison function for checking the
equality of RestrictInfos.

Reported-by: Zuming Jiang
Bug: #18261
Discussion: https://postgr.es/m/18261-2a75d748c928609b%40postgresql.org
Author: Richard Guo

16 months agoClean up some edge cases in plpgsql's %TYPE parsing.
Tom Lane [Fri, 5 Jan 2024 19:32:34 +0000 (14:32 -0500)]
Clean up some edge cases in plpgsql's %TYPE parsing.

Support referencing a composite-type variable in %TYPE.

Remove the undocumented, untested, and pretty useless ability
to have the subject of %TYPE be an (unqualified) type name.
You get the same result by just not writing %TYPE.

Add or adjust some test cases to improve code coverage here.

Discussion: https://postgr.es/m/716852.1704402127@sss.pgh.pa.us

16 months agoAdd copyright notices to a few perl scripts that don't have them
Andrew Dunstan [Fri, 5 Jan 2024 13:15:50 +0000 (13:15 +0000)]
Add copyright notices to a few perl scripts that don't have them

16 months agoFix corruption of local buffer state during extend of temp relation
Michael Paquier [Fri, 5 Jan 2024 11:08:34 +0000 (20:08 +0900)]
Fix corruption of local buffer state during extend of temp relation

A typo has been introduced by 31966b151e6a when updating the state of a
local buffer when a temporary relation is extended, for the case of a
block included in the relation range extended, when it is already found
in the hash table holding the local buffers.  In this case, BM_VALID
should be cleared, but the buffer state was changed so as BM_VALID
remained while clearing the other flags.

As reported on the thread, it was possible to corrupt the state of the
local buffers on ENOSPC, but the states would be corrupted on any kind
of ERROR during the relation extend (like partial writes or some other
errno).

Reported-by: Alexander Lakhin
Author: Tender Wang
Reviewed-by: Richard Guo, Alexander Lakhin, Michael Paquier
Discussion: https://postgr.es/m/18259-6e256429825dd435@postgresql.org
Backpatch-through: 16

16 months agoTeach estimate_array_length() to use statistics where available.
Tom Lane [Thu, 4 Jan 2024 23:36:19 +0000 (18:36 -0500)]
Teach estimate_array_length() to use statistics where available.

If we have DECHIST statistics about the argument expression, use
the average number of distinct elements as the array length estimate.
(It'd be better to use the average total number of elements, but
that is not currently calculated by compute_array_stats(), and
it's unclear that it'd be worth extra effort to get.)

To do this, we have to change the signature of estimate_array_length
to pass the "root" pointer.  While at it, also change its result
type to "double".  That's probably not really necessary, but it
avoids any risk of overflow of the value extracted from DECHIST.
All existing callers are going to use the result in a "double"
calculation anyway.

Paul Jungwirth, reviewed by Jian He and myself

Discussion: https://postgr.es/m/CA+renyUnM2d+SmrxKpDuAdpiq6FOM=FByvi6aS6yi__qyf6j9A@mail.gmail.com

16 months agoAdd macros for looping through a List without a ListCell.
Nathan Bossart [Thu, 4 Jan 2024 22:09:34 +0000 (16:09 -0600)]
Add macros for looping through a List without a ListCell.

Many foreach loops only use the ListCell pointer to retrieve the
content of the cell, like so:

    ListCell   *lc;

    foreach(lc, mylist)
    {
        int         myint = lfirst_int(lc);

        ...
    }

This commit adds a few convenience macros that automatically
declare the loop variable and retrieve the current cell's contents.
This allows us to rewrite the previous loop like this:

    foreach_int(myint, mylist)
    {
        ...
    }

This commit also adjusts a few existing loops in order to add
coverage for the new/adjusted macros.  There is presently no plan
to bulk update all foreach loops, as that could introduce a
significant amount of back-patching pain.  Instead, these macros
are primarily intended for use in new code.

Author: Jelte Fennema-Nio
Reviewed-by: David Rowley, Alvaro Herrera, Vignesh C, Tom Lane
Discussion: https://postgr.es/m/CAGECzQSwXKnxGwW1_Q5JE%2B8Ja20kyAbhBHO04vVrQsLcDciwXA%40mail.gmail.com

16 months agoIn plpgsql, allow %TYPE and %ROWTYPE to be followed by array decoration.
Tom Lane [Thu, 4 Jan 2024 20:24:15 +0000 (15:24 -0500)]
In plpgsql, allow %TYPE and %ROWTYPE to be followed by array decoration.

This provides the useful ability to declare a variable that is an array
of the type of some other variable or some table column.

Quan Zongliang, Pavel Stehule

Discussion: https://postgr.es/m/ec4523e1-9e7e-f3ef-f9ce-bafd680ad6f6@yeah.net

16 months agoALTER TABLE command to change generation expression
Peter Eisentraut [Thu, 4 Jan 2024 14:45:35 +0000 (15:45 +0100)]
ALTER TABLE command to change generation expression

This adds a new ALTER TABLE subcommand ALTER COLUMN ... SET EXPRESSION
that changes the generation expression of a generated column.

The syntax is not standard but was adapted from other SQL
implementations.

This command causes a table rewrite, using the usual ALTER TABLE
mechanisms.  The implementation is similar to and makes use of some of
the infrastructure of the SET DATA TYPE subcommand (for example,
rebuilding constraints and indexes afterwards).  The new command
requires a new pass in AlterTablePass, and the ADD COLUMN pass had to
be moved earlier so that combinations of ADD COLUMN and SET EXPRESSION
can work.

Author: Amul Sul <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/CAAJ_b94yyJeGA-5M951_Lr+KfZokOp-2kXicpmEhi5FXhBeTog@mail.gmail.com

16 months agoFix use of incorrect TupleTableSlot in DISTINCT aggregates
David Rowley [Thu, 4 Jan 2024 07:38:25 +0000 (20:38 +1300)]
Fix use of incorrect TupleTableSlot in DISTINCT aggregates

1349d2790 added code to allow DISTINCT and ORDER BY aggregates to work
more efficiently by using presorted input.  That commit added some code
that made use of the AggState's tmpcontext and adjusted the
ecxt_outertuple and ecxt_innertuple slots before checking if the current
row is distinct from the previously seen row.  That code forgot to set the
TupleTableSlots back to what they were originally, which could result in
errors such as:

ERROR:  attribute 1 of type record has wrong type

This only affects aggregate functions which have multiple arguments when
DISTINCT is used.  For example: string_agg(DISTINCT col, ', ')

Thanks to Tom Lane for identifying the breaking commit.

Bug: #18264
Reported-by: Vojtěch Beneš
Discussion: https://postgr.es/m/18264-e363593d7e9feb7d@postgresql.org
Backpatch-through: 16, where 1349d2790 was added

16 months agoTrack conflict_reason in pg_replication_slots.
Amit Kapila [Thu, 4 Jan 2024 02:51:51 +0000 (08:21 +0530)]
Track conflict_reason in pg_replication_slots.

This patch changes the existing 'conflicting' field to 'conflict_reason'
in pg_replication_slots. This new field indicates the reason for the
logical slot's conflict with recovery. It is always NULL for physical
slots, as well as for logical slots which are not invalidated. The
non-NULL values indicate that the slot is marked as invalidated. Possible
values are:

wal_removed = required WAL has been removed.
rows_removed = required rows have been removed.
wal_level_insufficient = the primary doesn't have a wal_level sufficient
to perform logical decoding.

The existing users of 'conflicting' column can get the same answer by
using 'conflict_reason' IS NOT NULL.

Author: Shveta Malik
Reviewed-by: Amit Kapila, Bertrand Drouvot, Michael Paquier
Discussion: https://postgr.es/m/[email protected]

16 months agoUpdate copyright for 2024
Bruce Momjian [Thu, 4 Jan 2024 01:49:05 +0000 (20:49 -0500)]
Update copyright for 2024

Reported-by: Michael Paquier
Discussion: https://postgr.es/m/[email protected]

Backpatch-through: 12

16 months agodoc: fix typo "vertexes" -> "vertices"
Michael Paquier [Wed, 3 Jan 2024 23:47:34 +0000 (08:47 +0900)]
doc: fix typo "vertexes" -> "vertices"

The "vertexes" spelling is also valid, but we consistently use
"vertices" elsewhere.

Author: Dagfinn Ilmari Mannsåker
Reviewed-by: Shubham Khanna
Discussion: https://postgr.es/m/[email protected]

16 months agoAvoid masking EOF (no-password-supplied) conditions in auth.c.
Tom Lane [Wed, 3 Jan 2024 22:40:38 +0000 (17:40 -0500)]
Avoid masking EOF (no-password-supplied) conditions in auth.c.

CheckPWChallengeAuth() would return STATUS_ERROR if the user does not
exist or has no password assigned, even if the client disconnected
without responding to the password challenge (as libpq often will,
for example).  We should return STATUS_EOF in that case, and the
lower-level functions do, but this code level got it wrong since the
refactoring done in 7ac955b34.  This breaks the intent of not logging
anything for EOF cases (cf. comments in auth_failed()) and might
also confuse users of ClientAuthentication_hook.

Per report from Liu Lang.  Back-patch to all supported versions.

Discussion: https://postgr.es/m/b725238c-539d-cb09-2bff-b5e6cb2c069c@esgyn.cn

16 months agoSecond attempt at organizing jsonpath operators and methods
Peter Eisentraut [Wed, 3 Jan 2024 20:56:41 +0000 (21:56 +0100)]
Second attempt at organizing jsonpath operators and methods

Second attempt at 283a95da923.  Since we can't reorder the enum values
of JsonPathItemType, instead reorder the switch cases where they are
used to generally follow the order of the enum values, for better
maintainability.

16 months agoRevert "Reorganise jsonpath operators and methods"
Peter Eisentraut [Wed, 3 Jan 2024 20:02:49 +0000 (21:02 +0100)]
Revert "Reorganise jsonpath operators and methods"

This reverts commit 283a95da923605c1cc148155db2d865d0801b419.

The reordering of JsonPathItemType affects the binary on-disk
compatibility of the jsonpath type, so we must not change it.  Revert
for now and consider.

16 months agoDoc: Python's control flow construct is try/except not try/catch.
Tom Lane [Wed, 3 Jan 2024 17:22:00 +0000 (12:22 -0500)]
Doc: Python's control flow construct is try/except not try/catch.

Very ancient thinko, dating evidently to 22690719e.
Spotted by gweatherby.

Discussion: https://postgr.es/m/170423637139.1288848.11840082988774620003@wrigleys.postgresql.org

16 months agoFix defects in PrepareForIncrementalBackup.
Robert Haas [Wed, 3 Jan 2024 14:59:46 +0000 (09:59 -0500)]
Fix defects in PrepareForIncrementalBackup.

Swap the arguments to TimestampDifferenceMilliseconds so that we get
a positive answer instead of zero.

Then use the result of that computation instead of ignoring it.

Per reports from Alexander Lakhin.

Discussion: http://postgr.es/m/8b686764-7ac1-74c3-70f9-b64685a2535f@gmail.com

16 months agoMake Perl warnings fatal in newly added TAP tests
Peter Eisentraut [Wed, 3 Jan 2024 12:16:55 +0000 (13:16 +0100)]
Make Perl warnings fatal in newly added TAP tests

New TAP tests added by commits 9a17be1e and 4710b67d missed to convert
warnings to FATAL.  This commit fixes that.

Author: Bharath Rupireddy <[email protected]>

16 months agoReorganise jsonpath operators and methods
Peter Eisentraut [Wed, 3 Jan 2024 10:25:33 +0000 (11:25 +0100)]
Reorganise jsonpath operators and methods

Various jsonpath operators and methods add various keywords, switch
cases, and documentation entries in some order.  However, they are not
consistent; reorder them for better maintainability or readability.

Author: Jeevan Chalke <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/CAM2+6=XjTyqrrqHAOj80r0wVQxJSxc0iyib9bPC55uFO9VKatg@mail.gmail.com

16 months agoAdd numeric_int8_opt_error() to optionally suppress errors
Peter Eisentraut [Wed, 3 Jan 2024 09:05:35 +0000 (10:05 +0100)]
Add numeric_int8_opt_error() to optionally suppress errors

This matches the existing numeric_int4_opt_error() (see commit
16d489b0fe).  It will be used by a future JSON-related patch, which
wants to report errors in its own way and thus does not want the
internal functions to throw any error.

Author: Jeevan Chalke <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/CAM2+6=XjTyqrrqHAOj80r0wVQxJSxc0iyib9bPC55uFO9VKatg@mail.gmail.com

16 months agoRefactor: separate function to find all objects depending on a column
Peter Eisentraut [Wed, 3 Jan 2024 07:48:09 +0000 (08:48 +0100)]
Refactor: separate function to find all objects depending on a column

Move code from ATExecAlterColumnType() that finds the all the objects
that depend on the column to a separate function.  A future patch will
reuse this code.

Author: Amul Sul <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/CAAJ_b94yyJeGA-5M951_Lr+KfZokOp-2kXicpmEhi5FXhBeTog@mail.gmail.com

16 months agoFix some typos
Michael Paquier [Wed, 3 Jan 2024 05:22:54 +0000 (14:22 +0900)]
Fix some typos

Author: Dagfinn Ilmari Mannsåker
Reviewed-by: Shubham Khanna
Discussion: https://postgr.es/m/[email protected]

16 months agoRemove unnecessary PGDATABASE settings from TAP tests
Michael Paquier [Wed, 3 Jan 2024 01:28:05 +0000 (10:28 +0900)]
Remove unnecessary PGDATABASE settings from TAP tests

Some of the TAP tests have been historically setting the environment
variable PGDATABASE to 'postgres', which is not needed because
PostgreSQL::Test::Cluster already sets it when initialized.  This commit
removes these explicit setups.

Note that the dependency of cluster -a with PGDATABASE (from
1caef31d9e55) is still documented.

Author: Bharath Rupireddy
Discussion: https://postgr.es/m/CALj2ACXLAz5dW3ZP+Fec8g6jQMMmDyCVT+qdbye2h7QJJmhsdw@mail.gmail.com

16 months agoMinor fixes for search path cache code.
Tom Lane [Tue, 2 Jan 2024 19:57:21 +0000 (14:57 -0500)]
Minor fixes for search path cache code.

Avoid leaving a dangling pointer in the unlikely event that
nsphash_create fails.  Improve comments, and fix formatting by
adding typedefs.list entries.

Discussion: https://postgr.es/m/3972900.1704145107@sss.pgh.pa.us

16 months agoRemove Lock suffix from WALSummarizerLock in wait_event_names.txt
Robert Haas [Tue, 2 Jan 2024 18:17:23 +0000 (13:17 -0500)]
Remove Lock suffix from WALSummarizerLock in wait_event_names.txt

Nathan Bossart

Discussion: https://postgr.es/m/20240102173120.GA1061678@nathanxps13

16 months agojsonpath_exec: fix typo "absense" -> "absence"
Robert Haas [Tue, 2 Jan 2024 17:27:38 +0000 (12:27 -0500)]
jsonpath_exec: fix typo "absense" -> "absence"

Dagfinn Ilmari Mannsåker, reviewed by Shubham Khanna.

Discussion: http://postgr.es/m/[email protected]

16 months agolibpq: fix typo "occurences" -> "occurrences" in tests
Robert Haas [Tue, 2 Jan 2024 17:27:09 +0000 (12:27 -0500)]
libpq: fix typo "occurences" -> "occurrences" in tests

Dagfinn Ilmari Mannsåker, reviewed by Shubham Khanna.

Discussion: http://postgr.es/m/[email protected]

16 months agogist: fix typo "split(t)ed" -> "split"
Robert Haas [Tue, 2 Jan 2024 17:24:28 +0000 (12:24 -0500)]
gist: fix typo "split(t)ed" -> "split"

Dagfinn Ilmari Mannsåker, reviewed by Shubham Khanna.

Discussion: http://postgr.es/m/[email protected]

16 months agotsquery: fix typo "rewrited" -> "rewritten"
Robert Haas [Tue, 2 Jan 2024 17:23:36 +0000 (12:23 -0500)]
tsquery: fix typo "rewrited" -> "rewritten"

Dagfinn Ilmari Mannsåker, reviewed by Shubham Khanna.

Discussion: http://postgr.es/m/[email protected]

16 months agoFix typos in comments and in one isolation test.
Robert Haas [Tue, 2 Jan 2024 16:56:02 +0000 (11:56 -0500)]
Fix typos in comments and in one isolation test.

Dagfinn Ilmari Mannsåker, reviewed by Shubham Khanna. Some subtractions
by me.

Discussion: http://postgr.es/m/[email protected]