postgresql.git
2 years agoAdd new GUC createrole_self_grant.
Robert Haas [Tue, 10 Jan 2023 17:44:49 +0000 (12:44 -0500)]
Add new GUC createrole_self_grant.

Can be set to the empty string, or to either or both of "set" or
"inherit". If set to a non-empty value, a non-superuser who creates
a role (necessarily by relying up the CREATEROLE privilege) will
grant that role back to themselves with the specified options.

This isn't a security feature, because the grant that this feature
triggers can also be performed explicitly. Instead, it's a user experience
feature. A superuser would necessarily inherit the privileges of any
created role and be able to access all such roles via SET ROLE;
with this patch, you can configure createrole_self_grant = 'set, inherit'
to provide a similar experience for a user who has CREATEROLE but not
SUPERUSER.

Discussion: https://postgr.es/m/CA+TgmobN59ct+Emmz6ig1Nua2Q-_o=r6DSD98KfU53kctq_kQw@mail.gmail.com

2 years agoRestrict the privileges of CREATEROLE users.
Robert Haas [Tue, 10 Jan 2023 17:44:30 +0000 (12:44 -0500)]
Restrict the privileges of CREATEROLE users.

Previously, CREATEROLE users were permitted to make nearly arbitrary
changes to roles that they didn't create, with certain exceptions,
particularly superuser roles.  Instead, allow CREATEROLE users to make such
changes to roles for which they possess ADMIN OPTION, and to
grant membership only in roles for which they possess ADMIN OPTION.

When a CREATEROLE user who is not a superuser creates a role, grant
ADMIN OPTION on the newly-created role to the creator, so that they
can administer roles they create or for which they have been given
privileges.

With these changes, CREATEROLE users still have very significant
powers that unprivileged users do not receive: they can alter, rename,
drop, comment on, change the password for, and change security labels
on roles.  However, they can now do these things only for roles for
which they possess appropriate privileges, rather than all
non-superuser roles; moreover, they cannot grant a role such as
pg_execute_server_program unless they themselves possess it.

Patch by me, reviewed by Mark Dilger.

Discussion: https://postgr.es/m/CA+TgmobN59ct+Emmz6ig1Nua2Q-_o=r6DSD98KfU53kctq_kQw@mail.gmail.com

2 years agoFix MERGE's test for unreachable WHEN clauses.
Dean Rasheed [Tue, 10 Jan 2023 14:17:47 +0000 (14:17 +0000)]
Fix MERGE's test for unreachable WHEN clauses.

The former code would only detect an unreachable WHEN clause if it had
an AND condition. Fix, so that unreachable unconditional WHEN clauses
are also detected.

Back-patch to v15, where MERGE was added.

Discussion: https://postgr.es/m/CAEZATCVQ=7E2z4cSBB49jjeGGsB6WeoYQY32NDeSvcHiLUZ=ow@mail.gmail.com

2 years agoNew header varatt.h split off from postgres.h
Peter Eisentraut [Tue, 10 Jan 2023 04:48:59 +0000 (05:48 +0100)]
New header varatt.h split off from postgres.h

This new header contains all the variable-length data types support
(TOAST support) from postgres.h, which isn't needed by large parts of
the backend code.

Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://www.postgresql.org/message-id/flat/ddcce239-0f29-6e62-4b47-1f8ca742addf%40enterprisedb.com

2 years agoDocument the newly added wait events added by commit 216a784829.
Amit Kapila [Tue, 10 Jan 2023 04:08:08 +0000 (09:38 +0530)]
Document the newly added wait events added by commit 216a784829.

Author: Shinoda, Noriyoshi
Reviewed-by: Hou Zhijie
Discussion: https://postgr.es/m/DM4PR84MB173460530245F56364E1DAF7EEFE9@DM4PR84MB1734.NAMPRD84.PROD.OUTLOOK.COM

2 years agoRound off random_normal() test results one more decimal place.
Tom Lane [Tue, 10 Jan 2023 03:44:16 +0000 (22:44 -0500)]
Round off random_normal() test results one more decimal place.

As I suspected, some machines have even more low-order-bit
inaccuracy than the ones I tested.  Tweak new test so that
(hopefully) it will pass everywhere.  Per buildfarm.

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

2 years agoFix the display of lock information for specktoken.
Amit Kapila [Tue, 10 Jan 2023 03:23:12 +0000 (08:53 +0530)]
Fix the display of lock information for specktoken.

A transaction id is now displayed in the transactionid field and
speculative insertion token is displayed in the objid field.

Author: Sawada Masahiko
Reviewed-by: Amit Kapila
Discussion: https://postgr.es/m/CAD21AoCEKxZztULP1CDm45aSNNR1QO-Bh1q6LMTspQ78PBuJrw@mail.gmail.com

2 years agoRemove pg_regress' never-documented "ignore" feature.
Tom Lane [Tue, 10 Jan 2023 01:34:26 +0000 (20:34 -0500)]
Remove pg_regress' never-documented "ignore" feature.

We aren't using this anymore in the wake of commit 09d517773,
so delete it.  We can always revert this if some future use
emerges, but I think our standards for test quality are now
high enough that that will never happen.

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

2 years agoUpgrade the random.sql regression test.
Tom Lane [Tue, 10 Jan 2023 01:30:25 +0000 (20:30 -0500)]
Upgrade the random.sql regression test.

We had some pretty ad-hoc and inefficient code here.  To make
matters worse, it didn't test the properties of the random()
function very thoroughly, and it had a test failure rate of
one in every few tens of thousands of runs.  Replace the
script altogether with new test cases that prove much more
about random()'s output, run faster, and can be calculated
to have test failure rates on the order of 1e-9.

Having done that, the failure rate of this script should be
negligible in comparison to other causes of test failures,
so remove the "ignore" marker for it in parallel_schedule.
(If it does fail, we'd like to know about that, so "ignore"
was always pretty counterproductive.)

Tom Lane and Dean Rasheed

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

2 years agoDoc: add XML ID attributes to <sectN> and <varlistentry> tags.
Tom Lane [Mon, 9 Jan 2023 20:08:24 +0000 (15:08 -0500)]
Doc: add XML ID attributes to <sectN> and <varlistentry> tags.

This doesn't have any external effect at the moment, but it
will allow adding useful link-discoverability features later.

Brar Piening, reviewed by Karl Pinc.

Discussion: https://postgr.es/m/CAB8KJ=jpuQU9QJe4+RgWENrK5g9jhoysMw2nvTN_esoOU0=a_w@mail.gmail.com

2 years agoInvent random_normal() to provide normally-distributed random numbers.
Tom Lane [Mon, 9 Jan 2023 17:44:00 +0000 (12:44 -0500)]
Invent random_normal() to provide normally-distributed random numbers.

There is already a version of this in contrib/tablefunc, but it
seems sufficiently widely useful to justify having it in core.

Paul Ramsey

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

2 years agoRemove redundant setting of tuplesort status
John Naylor [Mon, 9 Jan 2023 09:53:21 +0000 (16:53 +0700)]
Remove redundant setting of tuplesort status

Also add an explanatory comment to match other similar coding within
tuplesort_performsort().

Xing Guo

Reviewed by Richard Guo and Cary Huang
Discussion: https://www.postgresql.org/message-id/CACpMh%2BAQ4GXRKKi9ib2ioUH%2BqwNaSAVbetssJ0tMPfxAWuL2yg%40mail.gmail.com

2 years agoFix the file mode of worker.c changed by the commit 216a784829.
Amit Kapila [Mon, 9 Jan 2023 08:32:38 +0000 (14:02 +0530)]
Fix the file mode of worker.c changed by the commit 216a784829.

Reported-by: Japin Li
Discussion: https://postgr.es/m/MEYP282MB166970D1559B7CC74D3E339BB6FE9@MEYP282MB1669.AUSP282.PROD.OUTLOOK.COM

2 years agoAllow left join removals and unique joins on partitioned tables
David Rowley [Mon, 9 Jan 2023 04:15:08 +0000 (17:15 +1300)]
Allow left join removals and unique joins on partitioned tables

This allows left join removals and unique joins to work with partitioned
tables.  The planner just lacked sufficient proofs that a given join
would not cause any row duplication.  Unique indexes currently serve as
that proof, so have get_relation_info() populate the indexlist for
partitioned tables too.

Author: Arne Roland
Reviewed-by: Alvaro Herrera, Zhihong Yu, Amit Langote, David Rowley
Discussion: https://postgr.es/m/c3b2408b7a39433b8230bbcd02e9f302@index.de

2 years agoPerform apply of large transactions by parallel workers.
Amit Kapila [Mon, 9 Jan 2023 01:30:39 +0000 (07:00 +0530)]
Perform apply of large transactions by parallel workers.

Currently, for large transactions, the publisher sends the data in
multiple streams (changes divided into chunks depending upon
logical_decoding_work_mem), and then on the subscriber-side, the apply
worker writes the changes into temporary files and once it receives the
commit, it reads from those files and applies the entire transaction. To
improve the performance of such transactions, we can instead allow them to
be applied via parallel workers.

In this approach, we assign a new parallel apply worker (if available) as
soon as the xact's first stream is received and the leader apply worker
will send changes to this new worker via shared memory. The parallel apply
worker will directly apply the change instead of writing it to temporary
files. However, if the leader apply worker times out while attempting to
send a message to the parallel apply worker, it will switch to
"partial serialize" mode -  in this mode, the leader serializes all
remaining changes to a file and notifies the parallel apply workers to
read and apply them at the end of the transaction. We use a non-blocking
way to send the messages from the leader apply worker to the parallel
apply to avoid deadlocks. We keep this parallel apply assigned till the
transaction commit is received and also wait for the worker to finish at
commit. This preserves commit ordering and avoid writing to and reading
from files in most cases. We still need to spill if there is no worker
available.

This patch also extends the SUBSCRIPTION 'streaming' parameter so that the
user can control whether to apply the streaming transaction in a parallel
apply worker or spill the change to disk. The user can set the streaming
parameter to 'on/off', or 'parallel'. The parameter value 'parallel' means
the streaming will be applied via a parallel apply worker, if available.
The parameter value 'on' means the streaming transaction will be spilled
to disk. The default value is 'off' (same as current behaviour).

In addition, the patch extends the logical replication STREAM_ABORT
message so that abort_lsn and abort_time can also be sent which can be
used to update the replication origin in parallel apply worker when the
streaming transaction is aborted. Because this message extension is needed
to support parallel streaming, parallel streaming is not supported for
publications on servers < PG16.

Author: Hou Zhijie, Wang wei, Amit Kapila with design inputs from Sawada Masahiko
Reviewed-by: Sawada Masahiko, Peter Smith, Dilip Kumar, Shi yu, Kuroda Hayato, Shveta Mallik
Discussion: https://postgr.es/m/CAA4eK1+wyN6zpaHUkCLorEWNx75MG0xhMwcFhvjqm2KURZEAGw@mail.gmail.com

2 years agoDoc: improve commentary about providing our own definitions of M_PI.
Tom Lane [Sun, 8 Jan 2023 21:25:25 +0000 (16:25 -0500)]
Doc: improve commentary about providing our own definitions of M_PI.

2 years agoImprove GIN cost estimation
Alexander Korotkov [Sun, 8 Jan 2023 19:34:59 +0000 (22:34 +0300)]
Improve GIN cost estimation

GIN index scans were not taking any descent CPU-based cost into account.  That
made them look cheaper than other types of indexes when they shouldn't be.

We use the same heuristic as for btree indexes, but multiply it by the number
of searched entries.

Additionally, the CPU cost for the tree was based largely on a
genericcostestimate.  For a GIN index, we should not charge index quals per
tuple, but per entry. On top of this, charge cpu_index_tuple_cost per actual
tuple.

This should fix the cases where a GIN index is preferred over a btree and
the ones where a memoize node is not added on top of the GIN index scan
because it seemed too cheap.

We don't packpatch this to evade unexpected plan changes in stable versions.

Discussion: https://postgr.es/m/CABs3KGQnOkyQ42-zKQqiE7M0Ks9oWDSee%3D%2BJx3-TGq%3D68xqWYw%40mail.gmail.com
Discussion: https://postgr.es/m/3188617.44csPzL39Z%40aivenronan
Author: Ronan Dunklau
Reported-By: Hung Nguyen
Reviewed-by: Tom Lane, Alexander Korotkov
2 years agoExtract the multiplier for CPU process cost of index page into a macro
Alexander Korotkov [Sun, 8 Jan 2023 19:37:33 +0000 (22:37 +0300)]
Extract the multiplier for CPU process cost of index page into a macro

B-tree, GiST and SP-GiST all charge 50.0 * cpu_operator_cost for processing
an index page.  Extract this to a macro to avoid repeated magic numbers.

Discussion: https://mail.google.com/mail/u/0/?ik=a20b091faa&view=om&permmsgid=msg-f%3A1751459697261369543
Author: Ronan Dunklau

2 years agoCheck relkind before using TABLESAMPLE in postgres_fdw
Tomas Vondra [Sat, 7 Jan 2023 13:22:09 +0000 (14:22 +0100)]
Check relkind before using TABLESAMPLE in postgres_fdw

Check the remote relkind before trying to use TABLESAMPLE to acquire
sample from the remote relation. Even if the remote server version has
TABLESAMPLE support, the foreign table may point to incompatible relkind
(e.g. a view or a sequence).

If the relkind does not support TABLESAMPLE, error out if TABLESAMPLE
was requested specifically (as system/bernoulli), or fallback to random
just like we do for old server versions.

We currently end up disabling sampling for such relkind values anyway,
due to reltuples being -1 or 1, but that seems rather accidental, and
might get broken by improving reltuples estimates, etc.  So better to
make the check explicit.

Reported-by: Tom Lane
Discussion: https://postgr.es/m/951485.1672461744%40sss.pgh.pa.us

2 years agopsql: Add support for \dpS and \zS.
Dean Rasheed [Sat, 7 Jan 2023 11:09:26 +0000 (11:09 +0000)]
psql: Add support for \dpS and \zS.

This allows an optional "S" modifier to be added to \dp and \z, to
have them include system objects in the list.

Note that this also changes the behaviour of a bare \dp or \z without
the "S" modifier to include temp objects in the list, and exclude
information_schema objects, making them consistent with other psql
meta-commands.

Nathan Bossart, reviewed by Maxim Orlov.

Discussion: https://postgr.es/m/20221206193606.GB3078082@nathanxps13

2 years agoRemove the streaming files for incomplete xacts after restart.
Amit Kapila [Sat, 7 Jan 2023 06:47:14 +0000 (12:17 +0530)]
Remove the streaming files for incomplete xacts after restart.

After restart, we try to stream the changes for large transactions that
were not sent before server crash and restart. However, we forget to send
the abort message for such transactions. This leads to spurious streaming
files on the subscriber which won't be cleaned till the apply worker or
the subscriber server restarts.

Reported-by: Dilip Kumar
Author: Hou Zhijie
Reviewed-by: Dilip Kumar and Amit Kapila
Backpatch-through: 14
Discussion: https://postgr.es/m/OS0PR01MB5716A773F46768A1B75BE24394FB9@OS0PR01MB5716.jpnprd01.prod.outlook.com

2 years agoAdd additional regression tests for select_active_windows
David Rowley [Sat, 7 Jan 2023 02:24:35 +0000 (15:24 +1300)]
Add additional regression tests for select_active_windows

During the development of 728202b63, which was aimed at reducing the
number of sorts required to evaluate multiple window functions with
different WindowClause definitions, the code written sorted the
WindowClauses in reverse tleSortGroupRef order.  There appears to be no
discussion in the thread which was opened to discuss the development of
this patch and no comments mentioning the fact that having the
WindowClauses in reverse tleSortGroupRef order makes it more likely that
the final WindowClause to be evaluated will provide presorted input to
the query's DISTINCT or ORDER BY clause.  The reason for this is that the
tleSortGroupRef indexes are assigned for the DISTINCT and ORDER BY clauses
before they are for the WindowClauses PARTITION BY and ORDER BY clauses.
Putting the WindowClause with the lowest tleSortGroupRef last means that
it's more likely that no additional sorting is required for the query's
DISTINCT or ORDER BY clause.

All we're doing here is adding some tests and a comment to help ensure
that remains true and that we don't accidentally forget to consider this
again should we ever rewrite that code.

Author: Ankit Kumar Pandey, David Rowley
Discussion: https://postgr.es/m/CAApHDvq=g2=ny59f1bvwRVvupsgPHK-KjLPBsSL25fVuGZ4idQ@mail.gmail.com

2 years agoWake up a subscription's replication worker processes after DDL.
Tom Lane [Fri, 6 Jan 2023 21:08:20 +0000 (16:08 -0500)]
Wake up a subscription's replication worker processes after DDL.

Waken related worker processes immediately at commit of a transaction
that has performed ALTER SUBSCRIPTION (including the RENAME and
OWNER variants).  This reduces the response time for such operations.
In the real world that might not be worth much, but it shaves several
seconds off the runtime for the subscription test suite.

In the case of PREPARE, we just throw away this notification state;
it doesn't seem worth the work to preserve it.  The workers will
still react after the eventual COMMIT PREPARED, but not as quickly.

Nathan Bossart

Discussion: https://postgr.es/m/20221122004119.GA132961@nathanxps13

2 years agoCheck for two_phase change at end of process_syncing_tables_for_apply.
Tom Lane [Fri, 6 Jan 2023 20:35:31 +0000 (15:35 -0500)]
Check for two_phase change at end of process_syncing_tables_for_apply.

Previously this function checked to see if we were ready to switch
to two_phase mode at its start, but that's silly: we should check
at the end, after we've done the work that might make us ready.
This simple change removes one sleep cycle from the time needed to
switch to two_phase mode.  In the real world that might not be
worth much, but it shaves a few seconds off the runtime for the
subscription test suite.

Nathan Bossart

Discussion: https://postgr.es/m/20221122004119.GA132961@nathanxps13

2 years agoAllow hyphens in ltree labels
Andrew Dunstan [Fri, 6 Jan 2023 21:03:19 +0000 (16:03 -0500)]
Allow hyphens in ltree labels

Also increase the allowed length of labels to 1000 characters

Garen Torikian

Discussion: https://postgr.es/m/CAGXsc+-mNg9Gc0rp-ER0sv+zkZSZp2wE9-LX6XcoWSLVz22tZA@mail.gmail.com

2 years agoAdd options to control whether VACUUM runs vac_update_datfrozenxid.
Tom Lane [Fri, 6 Jan 2023 19:17:25 +0000 (14:17 -0500)]
Add options to control whether VACUUM runs vac_update_datfrozenxid.

VACUUM normally ends by running vac_update_datfrozenxid(), which
requires a scan of pg_class.  Therefore, if one attempts to vacuum a
database one table at a time --- as vacuumdb has done since v12 ---
we will spend O(N^2) time in vac_update_datfrozenxid().  That causes
serious performance problems in databases with tens of thousands of
tables, and indeed the effect is measurable with only a few hundred.
To add insult to injury, only one process can run
vac_update_datfrozenxid at the same time per DB, so this behavior
largely defeats vacuumdb's -j option.

Hence, invent options SKIP_DATABASE_STATS and ONLY_DATABASE_STATS
to allow applications to postpone vac_update_datfrozenxid() until the
end of a series of VACUUM requests, and teach vacuumdb to use them.

Per bug #17717 from Gunnar L.  Sadly, this answer doesn't seem
like something we'd consider back-patching, so the performance
problem will remain in v12-v15.

Tom Lane and Nathan Bossart

Discussion: https://postgr.es/m/17717-6c50eb1c7d23a886@postgresql.org

2 years agoInvalidate pgoutput's replication-decisions cache upon schema rename.
Tom Lane [Fri, 6 Jan 2023 16:11:51 +0000 (11:11 -0500)]
Invalidate pgoutput's replication-decisions cache upon schema rename.

A schema rename should cause reporting the new qualified names of
tables to logical replication subscribers, but that wasn't happening.
Flush the RelationSyncCache to make it happen.

(If you ask me, the new test case shows that the behavior in this area
is still pretty dubious, but apparently it's operating as designed.)

Vignesh C

Discussion: https://postgr.es/m/CALDaNm32vLRv5KdrDFeVC-CU+4Wg1daA55hMqOxDGJBzvd76-w@mail.gmail.com

2 years agoFix stale comment about sample_frac adjustment
Tomas Vondra [Fri, 6 Jan 2023 13:47:02 +0000 (14:47 +0100)]
Fix stale comment about sample_frac adjustment

A comment was left behind referencing sample rate adjustment removed
from 8ad51b5f44. So clean that up. While at it also remove the sample
rate clamping which should not be necessary without the clamping, and
just check that with an assert.

Reported-by: Tom Lane
Discussion: https://postgr.es/m/951485.1672461744%40sss.pgh.pa.us

2 years agoFix typo
Peter Eisentraut [Fri, 6 Jan 2023 13:25:19 +0000 (14:25 +0100)]
Fix typo

This doesn't affect the correctness of the code, but it was clearly
inconsistent before this change.

2 years agoFix tab completion of ALTER FUNCTION/PROCEDURE/ROUTINE ... SET SCHEMA.
Dean Rasheed [Fri, 6 Jan 2023 11:18:44 +0000 (11:18 +0000)]
Fix tab completion of ALTER FUNCTION/PROCEDURE/ROUTINE ... SET SCHEMA.

The ALTER DATABASE|FUNCTION|PROCEDURE|ROLE|ROUTINE|USER ... SET <name>
case in psql tab completion failed to exclude <name> = "SCHEMA", which
caused ALTER FUNCTION|PROCEDURE|ROUTINE ... SET SCHEMA to complete
with "FROM CURRENT" and "TO", which won't work.

Fix that, so that those cases now complete with the list of schemas,
like other ALTER ... SET SCHEMA commands.

Noticed while testing the recent patch to improve tab completion for
ALTER FUNCTION/PROCEDURE/ROUTINE, but this is not directly related to
that patch. Rather, this is a long-standing bug, so back-patch to all
supported branches.

Discussion: https://postgr.es/m/CALDaNm0s7GQmkLP_mx5Cvk=UzYMnjhPmXBxU8DsHEunFbC5sTg@mail.gmail.com

2 years agoImprove tab completion for ALTER FUNCTION/PROCEDURE/ROUTINE.
Dean Rasheed [Fri, 6 Jan 2023 09:57:41 +0000 (09:57 +0000)]
Improve tab completion for ALTER FUNCTION/PROCEDURE/ROUTINE.

This adds psql tab completion for each of the actions supported by
ALTER FUNCTION/PROCEDURE/ROUTINE.

Vignesh C, reviewed by Dong Wook Lee, Michael Paquier, Melih Mutlu and
me.

Discussion: https://postgr.es/m/CALDaNm0s7GQmkLP_mx5Cvk=UzYMnjhPmXBxU8DsHEunFbC5sTg@mail.gmail.com

2 years agoFix pg_truncate() on Windows.
Thomas Munro [Fri, 6 Jan 2023 03:38:46 +0000 (16:38 +1300)]
Fix pg_truncate() on Windows.

Commit 57faaf376 added pg_truncate(const char *path, off_t length), but
"length" was ignored under WIN32 and the file was unconditionally
truncated to 0.

There was no live bug, since the only caller passes 0.

Fix, and back-patch to 14 where the function arrived.

Author: Justin Pryzby <pryzby@telsasoft.com>
Discussion: https://postgr.es/m/20230106031652.GR3109%40telsasoft.com

2 years agoUse unnamed POSIX semaphores on Cygwin.
Thomas Munro [Thu, 5 Jan 2023 21:09:02 +0000 (10:09 +1300)]
Use unnamed POSIX semaphores on Cygwin.

Testing on CI showed that Cygwin's semctl() can fail with EAGAIN
(possibly due to resource limits in cygserver that could be tuned, not
examined).  Switch to so-called POSIX semaphores instead, which don't
seem to fail in that way (possibly due to a more direct implementation
using Windows semaphore primitives instead of talking to cygserver,
based on a cursory glance at the source).

Other known problems still prevent PostgreSQL from running on Cygwin
without random crashes, but this rarer problem was noticed while
testing.

Discussion: https://postgr.es/m/CA%2BhUKG%2BQ6DU4Ov9LrvUyDcF3oHS4KMRVSKmVGaeePq-kOyG9gA%40mail.gmail.com

2 years agoPass down current user ID to AddRoleMems and DelRoleMems.
Robert Haas [Thu, 5 Jan 2023 19:33:35 +0000 (14:33 -0500)]
Pass down current user ID to AddRoleMems and DelRoleMems.

This is just refactoring; there should be no functonal change. It
might have the effect of slightly reducing the number of calls to
GetUserId(), but the real point is to facilitate future work in
this area.

Patch by me, reviewed by Mark Dilger.

Discussion: http://postgr.es/m/CA+TgmobFzTLkLwOquFrAcdsWBsOWDr-_H-jw+qBvfx-wSzMwDA@mail.gmail.com

2 years agoRefactor permissions-checking for role grants.
Robert Haas [Thu, 5 Jan 2023 19:30:40 +0000 (14:30 -0500)]
Refactor permissions-checking for role grants.

Instead of having checks in AddRoleMems() and DelRoleMems(), have
the callers perform checks where it's required. In some cases it
isn't, either because the caller has already performed a check for
the same condition, or because the check couldn't possibly fail.

The "Skip permission check if nothing to do" check in each of
AddRoleMems() and DelRoleMems() is pointless. Some call sites
can't pass an empty list. Others can, but in those cases, the role
being modified is one that the current user has just created.
Therefore, they must have permission to modify it, and so no
permission check is required at all.

This patch is intended to have no user-visible consequences. It is
intended to simplify future work in this area.

Patch by me, reviewed by Mark Dilger.

Discussion: http://postgr.es/m/CA+TgmobFzTLkLwOquFrAcdsWBsOWDr-_H-jw+qBvfx-wSzMwDA@mail.gmail.com

2 years agoFix calculation of which GENERATED columns need to be updated.
Tom Lane [Thu, 5 Jan 2023 19:12:17 +0000 (14:12 -0500)]
Fix calculation of which GENERATED columns need to be updated.

We were identifying the updatable generated columns of inheritance
children by transposing the calculation made for their parent.
However, there's nothing that says a traditional-inheritance child
can't have generated columns that aren't there in its parent, or that
have different dependencies than are in the parent's expression.
(At present it seems that we don't enforce that for partitioning
either, which is likely wrong to some degree or other; but the case
clearly needs to be handled with traditional inheritance.)

Hence, drop the very-klugy-anyway "extraUpdatedCols" RTE field
in favor of identifying which generated columns depend on updated
columns during executor startup.  In HEAD we can remove
extraUpdatedCols altogether; in back branches, it's still there but
always empty.  Another difference between the HEAD and back-branch
versions of this patch is that in HEAD we can add the new bitmap field
to ResultRelInfo, but that would cause an ABI break in back branches.
Like 4b3e37993, add a List field at the end of struct EState instead.

Back-patch to v13.  The bogus calculation is also being made in v12,
but it doesn't have the same visible effect because we don't use it
to decide which generated columns to recalculate; as a consequence of
which the patch doesn't apply easily.  I think that there might still
be a demonstrable bug associated with trigger firing conditions, but
that's such a weird corner-case usage that I'm content to leave it
unfixed in v12.

Amit Langote and Tom Lane

Discussion: https://postgr.es/m/CA+HiwqFshLKNvQUd1DgwJ-7tsTp=dwv7KZqXC4j2wYBV1aCDUA@mail.gmail.com
Discussion: https://postgr.es/m/2793383.1672944799@sss.pgh.pa.us

2 years agoRemove extra regress check arguments from test_pg_db_role_setting
Alexander Korotkov [Thu, 5 Jan 2023 10:11:40 +0000 (13:11 +0300)]
Remove extra regress check arguments from test_pg_db_role_setting

They were accidentally copied from test_oat_hooks.

Reported-by: Justin Pryzby
Discussion: https://postgr.es/m/20230102154240.GL1153%40telsasoft.com
Reviewed-by: Pavel Borisov
2 years agomeson: Add 'running' test setup, as a replacement for installcheck
Alexander Korotkov [Thu, 5 Jan 2023 10:11:28 +0000 (13:11 +0300)]
meson: Add 'running' test setup, as a replacement for installcheck

Do the same as 3f0e786ccbf5 for test_pg_db_role_setting.

Discussion: https://postgr.es/m/20221227065456.GU1153@telsasoft.com
Author: Pavel Borisov
Reviewed-by: Justin Pryzby, Tom Lane
2 years agoFix some compiler warnings in aset.c and generation.c
David Rowley [Wed, 4 Jan 2023 23:56:17 +0000 (12:56 +1300)]
Fix some compiler warnings in aset.c and generation.c

This fixes a couple of unused variable warnings that could be seen when
compiling with MEMORY_CONTEXT_CHECKING but not USE_ASSERT_CHECKING.
Defining MEMORY_CONTEXT_CHECKING without asserts is a little unusual,
however, we shouldn't be producing any warnings from such a build.

Author: Richard Guo
Discussion: https://postgr.es/m/CAMbWs4_D-vgLEh7eO47p=73u1jWO78NWf6Qfv1FndY1kG-Q-jA@mail.gmail.com

2 years agoCheck that xmax didn't commit in freeze check.
Peter Geoghegan [Wed, 4 Jan 2023 05:48:27 +0000 (21:48 -0800)]
Check that xmax didn't commit in freeze check.

We cannot rely on TransactionIdDidAbort here, since in general it may
report transactions that were in-progress at the time of an earlier hard
crash as not aborted, effectively behaving as if they were still in
progress even after crash recovery completes.  Go back to defensively
verifying that xmax didn't commit instead.

Oversight in commit 79d4bf4e.

Author: Peter Geoghegan <pg@bowt.ie>
Reported-By: Andres Freund <andres@anarazel.de>
Discussion: https://postgr.es/m/20230104035636.hy5djyr2as4gbc4q@awork3.anarazel.de

2 years agoUpdate obsolete multixact.c comments.
Peter Geoghegan [Wed, 4 Jan 2023 00:54:35 +0000 (16:54 -0800)]
Update obsolete multixact.c comments.

Commit 4f627f89 switched SLRU truncation for multixacts back to being a
task performed during VACUUM, but missed some comments that continued to
reference truncation happening as part of checkpointing.  Update those
comments now.

Also update comments that became obsolete when commit c3ffa731 changed
the way that vacuum_multixact_freeze_min_age is applied by VACUUM as it
computes its MultiXactCutoff cutoff (which is used by VACUUM to decide
what to freeze).  Explain the same issues by referencing how OldestMxact
is the latest valid value that relminmxid can ever be advanced to at the
end of a VACUUM (following the work in commit 0b018fab).

2 years agoDuring pg_dump startup, acquire table locks in batches.
Tom Lane [Tue, 3 Jan 2023 22:56:37 +0000 (17:56 -0500)]
During pg_dump startup, acquire table locks in batches.

Combine multiple LOCK TABLE commands to reduce the number of
round trips to the server.  This is particularly helpful when
dumping from a remote server, but it seems useful even without
that.  In particular, shortening the time from seeing a table
in pg_class to acquiring lock on it reduces the window for
trouble from concurrent DDL.

Aleksander Alekseev, reviewed by Fabrízio de Royes Mello,
Gilles Darold, and Andres Freund

Discussion: https://postgr.es/m/CAJ7c6TO4z1+OBa-R+fC8FnaUgbEWJUf2Kq=nRngTW5EXtKru2g@mail.gmail.com

2 years agoFix typo in memutils_memorychunk.h
David Rowley [Tue, 3 Jan 2023 20:23:19 +0000 (09:23 +1300)]
Fix typo in memutils_memorychunk.h

Author: Richard Guo
Discussion: https://postgr.es/m/CAMbWs483CYjHoLH32_hd3Yq1NJfravNdL2zy7+e7pwvFPJF1RQ@mail.gmail.com

2 years agoImprove documentation of the CREATEROLE attibute.
Robert Haas [Tue, 3 Jan 2023 19:50:40 +0000 (14:50 -0500)]
Improve documentation of the CREATEROLE attibute.

In user-manag.sgml, document precisely what privileges are conveyed
by CREATEROLE. Make particular note of the fact that it allows
changing passwords and granting access to high-privilege roles.
Also remove the suggestion of using a user with CREATEROLE and
CREATEDB instead of a superuser, as there is no real security
advantage to this approach.

Elsewhere in the documentation, adjust text that suggests that
<literal>CREATEROLE</literal> only allows for role creation, and
refer to the documentation in user-manag.sgml as appropriate.

Patch by me, reviewed by Álvaro Herrera

Discussion: http://postgr.es/m/CA+TgmoZBsPL8nPhvYecx7iGo5qpDRqa9k_AcaW1SbOjugAY1Ag@mail.gmail.com

2 years agovacuumlazy.c: Save get_database_name() in vacrel.
Peter Geoghegan [Tue, 3 Jan 2023 19:48:47 +0000 (11:48 -0800)]
vacuumlazy.c: Save get_database_name() in vacrel.

This brings dbname strings in line with namespace and relation name
strings.

Author: Peter Geoghegan <pg@bowt.ie>
Discussion: https://postgr.es/m/CAH2-WzkQ1TKU-DdNvnGeL870di3+CU1UTo-7nw7xFDpVE-XGjA@mail.gmail.com

2 years agoDelay commit status checks until freezing executes.
Peter Geoghegan [Tue, 3 Jan 2023 19:22:36 +0000 (11:22 -0800)]
Delay commit status checks until freezing executes.

pg_xact lookups are relatively expensive.  Move the xmin/xmax commit
status checks from the point that freeze plans are prepared to the point
that they're actually executed.  Otherwise we'll repeat many commit
status checks whenever multiple successive VACUUM operations scan the
same pages and decide against freezing each time, which is a waste of
cycles.

Oversight in commit 1de58df4, which added page-level freezing.

Author: Peter Geoghegan <pg@bowt.ie>
Discussion: https://postgr.es/m/CAH2-WzkZpe4K6qMfEt8H4qYJCKc2R7TPvKsBva7jc9w7iGXQSw@mail.gmail.com

2 years agoRefine the definition of page-level freezing.
Peter Geoghegan [Tue, 3 Jan 2023 18:08:55 +0000 (10:08 -0800)]
Refine the definition of page-level freezing.

Improve comments added by commit 1de58df4 which describe the
lazy_scan_prune "freeze the page" path.  These newly revised comments
are based on suggestions from Jeff Davis.

In passing, remove nearby visibility_cutoff_xid comments left over from
commit 6daeeb1f.

Author: Peter Geoghegan <pg@bowt.ie>
Reviewed-By: Jeff Davis <pgsql@j-davis.com>
Discussion: https://postgr.es/m/ebc857107fe3edd422ef8a65191ca4a8da568b9b.camel@j-davis.com

2 years agoWindows support in pg_import_system_collations
Peter Eisentraut [Tue, 3 Jan 2023 13:21:40 +0000 (14:21 +0100)]
Windows support in pg_import_system_collations

Windows can enumerate the locales that are either installed or
supported by calling EnumSystemLocalesEx(), similar to what is already
done in the READ_LOCALE_A_OUTPUT switch.  We can refactor some of the
logic already used in that switch into a new function
create_collation_from_locale().

The enumerated locales have BCP 47 shape, that is with a hyphen
between language and territory, instead of POSIX's underscore.  The
created collations will retain the BCP 47 shape, but we will also
create a POSIX alias, so xx-YY will have an xx_YY alias.

A new test collate.windows.win1252 is added that is like
collate.linux.utf8.

Author: Juan Jose Santamaria Flecha <juanjo.santamaria@gmail.com>
Reviewed-by: Dmitry Koval <d.koval@postgrespro.ru>
Reviewed-by: Peter Eisentraut <peter.eisentraut@enterprisedb.com>
Discussion: https://www.postgresql.org/message-id/flat/0050ec23-34d9-2765-9015-98c04f0e18ac@postgrespro.ru

2 years agoFix typos in comments, code and documentation
Michael Paquier [Tue, 3 Jan 2023 07:26:14 +0000 (16:26 +0900)]
Fix typos in comments, code and documentation

While on it, newlines are removed from the end of two elog() strings.
The others are simple grammar mistakes.  One comment in pg_upgrade
referred incorrectly to sequences since a7e5457.

Author: Justin Pryzby
Discussion: https://postgr.es/m/20221230231257.GI1153@telsasoft.com
Backpatch-through: 11

2 years agoAvoid reference to nonexistent array element in ExecInitAgg().
Tom Lane [Mon, 2 Jan 2023 21:17:00 +0000 (16:17 -0500)]
Avoid reference to nonexistent array element in ExecInitAgg().

When considering an empty grouping set, we fetched
phasedata->eqfunctions[-1].  Because the eqfunctions array is
palloc'd, that would always be an aset pointer in released versions,
and thus the code accidentally failed to malfunction (since it would
do nothing unless it found a null pointer).  Nonetheless this seems
like trouble waiting to happen, so add a check for length == 0.

It's depressing that our valgrind testing did not catch this.
Maybe we should reconsider the choice to not mark that word NOACCESS?

Richard Guo

Discussion: https://postgr.es/m/CAMbWs4-vZuuPOZsKOYnSAaPYGKhmacxhki+vpOKk0O7rymccXQ@mail.gmail.com

2 years agoUpdate copyright for 2023
Bruce Momjian [Mon, 2 Jan 2023 20:00:37 +0000 (15:00 -0500)]
Update copyright for 2023

Backpatch-through: 11

2 years agoAdjust VACUUM hastup LP_REDIRECT comments.
Peter Geoghegan [Mon, 2 Jan 2023 18:18:22 +0000 (10:18 -0800)]
Adjust VACUUM hastup LP_REDIRECT comments.

The term "truncation" has been ambiguous since commit 10a8d13823 added
line pointer array truncation during heap pruning.  Clear things up by
specifying that we're talking about rel truncation here, to match nearby
comments that apply to tuples with storage.

2 years agoAvoid special XID snapshotConflictHorizon values.
Peter Geoghegan [Mon, 2 Jan 2023 18:16:51 +0000 (10:16 -0800)]
Avoid special XID snapshotConflictHorizon values.

Don't allow VACUUM to WAL-log the value FrozenTransactionId as the
snapshotConflictHorizon of freezing or visibility map related WAL
records.

The only special XID value that's an allowable snapshotConflictHorizon
is InvalidTransactionId, which is interpreted as "record definitely
doesn't require a recovery conflict".

Author: Peter Geoghegan <pg@bowt.ie>
Discussion: https://postgr.es/m/CAH2-WznuNGSzF8v6OsgjaC5aYsb3cZ6HW6MLm30X0d65cmSH6A@mail.gmail.com

2 years agoAdd bt_multi_page_stats() function to contrib/pageinspect.
Tom Lane [Mon, 2 Jan 2023 18:02:29 +0000 (13:02 -0500)]
Add bt_multi_page_stats() function to contrib/pageinspect.

This is like the existing bt_page_stats() function, but it can
report on a range of pages rather than just one at a time.

I don't have a huge amount of faith in the portability of the
new test cases, but they do pass in a 32-bit FreeBSD VM here.
Further adjustment may be needed depending on buildfarm results.

Hamid Akhtar, reviewed by Naeem Akhter, Bertrand Drouvot,
Bharath Rupireddy, and myself

Discussion: https://postgr.es/m/CANugjht-=oGMRmNJKMqnBC69y7vr+wHDmm0ZK6-1pJsxoBKBbA@mail.gmail.com

2 years agoPush lpp variable closer to usage in heapgetpage()
Peter Eisentraut [Mon, 2 Jan 2023 08:39:04 +0000 (09:39 +0100)]
Push lpp variable closer to usage in heapgetpage()

Author: Melanie Plageman <melanieplageman@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/CAAKRu_YSOnhKsDyFcqJsKtBSrd32DP-jjXmv7hL0BPD-z0TGXQ@mail.gmail.com

2 years agodoc: Re-order Table 28.2 "Collected Statistics Views"
Peter Eisentraut [Mon, 2 Jan 2023 07:45:17 +0000 (08:45 +0100)]
doc: Re-order Table 28.2 "Collected Statistics Views"

Make the order more sensible and repeatable.  Comments have been added
to guide future additions.

Author: Peter Smith <peter.b.smith@fujitsu.com>
Reviewed-by: David G. Johnston <david.g.johnston@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/CAHut+Pv8Oa7v06hJb3+HzCtM2u-3oHWMdvXVHhvi7ofB83pNbg@mail.gmail.com

2 years agoAccept "+infinity" in date and timestamp[tz] input.
Tom Lane [Sun, 1 Jan 2023 19:16:07 +0000 (14:16 -0500)]
Accept "+infinity" in date and timestamp[tz] input.

The float and numeric types accept this variant spelling of
"infinity", so it seems like the datetime types should too.

Vik Fearing, some cosmetic mods by me

Discussion: https://postgr.es/m/d0bef637-2dbd-0a5d-e539-48243b6f6c5e@postgresfriends.org

2 years agoIn plpgsql, don't preassign portal names to bound cursor variables.
Tom Lane [Sun, 1 Jan 2023 18:22:34 +0000 (13:22 -0500)]
In plpgsql, don't preassign portal names to bound cursor variables.

A refcursor variable that is bound to a specific query (by declaring
it with "CURSOR FOR") now chooses a portal name in the same way as an
unbound, plain refcursor variable.  Its string value starts out as
NULL, and unless that's overridden by manual assignment, it will be
replaced by a unique-within-session portal name during OPEN.

The previous behavior was to initialize such variables to contain
their own name, resulting in that also being the portal name unless
the user overwrote it before OPEN.  The trouble with this is that
it causes failures due to conflicting portal names if the same
cursor variable name is used in different functions.  It is pretty
non-orthogonal to have bound and unbound refcursor variables behave
differently on this point, too, so let's change it.

This change can cause compatibility problems for applications that
open a bound cursor in a plpgsql function and then use it in the
calling code without explicitly passing back the refcursor value
(portal name).  If the calling code simply assumes that the portal
name matches the called function's variable name, it will now fail.
That can be fixed by explicitly assigning a string value to the
refcursor variable before OPEN, e.g.

    DECLARE myc CURSOR FOR SELECT ...;
    BEGIN
      myc := 'myc';  -- add this
      OPEN myc;

We have no documentation examples showing the troublesome usage
pattern, so we can hope it's rare in practice.

Patch by me; thanks to Pavel Stehule and Jan Wieck for review.

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

2 years agoci: Change macOS builds from Intel to ARM.
Thomas Munro [Sat, 31 Dec 2022 21:45:18 +0000 (10:45 +1300)]
ci: Change macOS builds from Intel to ARM.

Cirrus is about to shut down its macOS-on-Intel support, so it's time to
move our CI testing over to ARM instances.  The Homebrew package manager
changed its default installation prefix for the new architecture, so a
couple of tests need tweaks to find binaries.

Back-patch to 15, where in-tree CI began.

Author: Justin Pryzby <pryzby@telsasoft.com>
Discussion: https://postgr.es/m/20221122225744.GF11463%40telsasoft.com

2 years agoSample postgres_fdw tables remotely during ANALYZE
Tomas Vondra [Fri, 30 Dec 2022 22:14:53 +0000 (23:14 +0100)]
Sample postgres_fdw tables remotely during ANALYZE

When collecting ANALYZE sample on foreign tables, postgres_fdw fetched
all rows and performed the sampling locally. For large tables this means
transferring and immediately discarding large amounts of data.

This commit allows the sampling to be performed on the remote server,
transferring only the much smaller sample. The sampling is performed
using the built-in TABLESAMPLE methods (system, bernoulli) or random()
function, depending on the remote server version.

Remote sampling can be enabled by analyze_sampling on the foreign server
and/or foreign table, with supported values 'off', 'auto', 'system',
'bernoulli' and 'random'. The default value is 'auto' which uses either
'bernoulli' (TABLESAMPLE method) or 'random' (for remote servers without
TABLESAMPLE support).

2 years agoFix assert in BRIN build_distances
Tomas Vondra [Fri, 30 Dec 2022 18:44:48 +0000 (19:44 +0100)]
Fix assert in BRIN build_distances

When brin_minmax_multi_union merges summaries, we may end up with just a
single range after merge_overlapping_ranges. The summaries may contain
just one range each, and they may overlap (or be exactly the same).

With a single range there's no distance to calculate, but we happen to
call build_distances anyway - which is fine, we don't calculate the
distance in this case, except that with asserts this failed due to a
check there are at least two ranges.

The assert is unnecessarily strict, so relax it a bit and bail out if
there's just a single range. The relaxed assert would be enough, but
this way we don't allocate unnecessary memory for distance.

Backpatch to 14, where minmax-multi opclasses were introduced.

Reported-by: Jaime Casanova
Backpatch-through: 14
Discussion: https://postgr.es/m/YzVA55qS0hgz8P3r@ahch-to

2 years agoFix precision handling for some COERCE_SQL_SYNTAX functions
Michael Paquier [Fri, 30 Dec 2022 11:47:57 +0000 (20:47 +0900)]
Fix precision handling for some COERCE_SQL_SYNTAX functions

f193883 has been incorrectly setting up the precision used in the
timestamp compilations returned by the following functions:
- LOCALTIME
- LOCALTIMESTAMP
- CURRENT_TIME
- CURRENT_TIMESTAMP

Specifying an out-of-range precision for CURRENT_TIMESTAMP and
LOCALTIMESTAMP was raising a WARNING without adjusting the precision,
leading to a subsequent error.  LOCALTIME and CURRENT_TIME raised a
WARNING without an error, still the precision given to the internal
routines was not correct, so let's be clean.

Ian has reported the problems in timestamp.c, while I have noticed the
ones in date.c.  Regression tests are added for all of them with
precisions high enough to provide coverage for the warnings, something
that went missing up to this commit.

Author: Ian Lawrence Barwick, Michael Paquier
Discussion: https://postgr.es/m/CAB8KJ=jQEnn9sYG+N752spt68wMrhmT-ocHCh4oeNmHF82QMWA@mail.gmail.com

2 years agoChange argument of appendBinaryStringInfo from char * to void *
Peter Eisentraut [Fri, 30 Dec 2022 09:59:25 +0000 (10:59 +0100)]
Change argument of appendBinaryStringInfo from char * to void *

There is some code that uses this function to assemble some kind of
packed binary layout, which requires a bunch of casts because of this.
Functions taking binary data plus length should take void * instead,
like memcpy() for example.

Discussion: https://www.postgresql.org/message-id/flat/a0086cfc-ff0f-2827-20fe-52b591d2666c%40enterprisedb.com

2 years agoUse appendStringInfoString instead of appendBinaryStringInfo where possible
Peter Eisentraut [Fri, 30 Dec 2022 09:59:25 +0000 (10:59 +0100)]
Use appendStringInfoString instead of appendBinaryStringInfo where possible

For the jsonpath output, we don't need to squeeze out every bit of
performance, so instead use a more robust coding style.  There are
similar calls in jsonb.c, which we leave alone here since there is
indeed a performance impact for bulk exports.

Discussion: https://www.postgresql.org/message-id/flat/a0086cfc-ff0f-2827-20fe-52b591d2666c%40enterprisedb.com

2 years agoAdd const to BufFileWrite
Peter Eisentraut [Fri, 30 Dec 2022 09:02:59 +0000 (10:02 +0100)]
Add const to BufFileWrite

Make data buffer argument to BufFileWrite a const pointer and bubble
this up to various callers and related APIs.  This makes the APIs
clearer and more consistent.

Discussion: https://www.postgresql.org/message-id/flat/11dda853-bb5b-59ba-a746-e168b1ce4bdb%40enterprisedb.com

2 years agoRemove unnecessary casts
Peter Eisentraut [Fri, 30 Dec 2022 09:02:59 +0000 (10:02 +0100)]
Remove unnecessary casts

Some code carefully cast all data buffer arguments for data write and
read function calls to void *, even though the respective arguments
are already void *.  Remove this unnecessary clutter.

Discussion: https://www.postgresql.org/message-id/flat/11dda853-bb5b-59ba-a746-e168b1ce4bdb%40enterprisedb.com

2 years agoperl: Hide warnings inside perl.h when using gcc compatible compiler
Andres Freund [Thu, 29 Dec 2022 20:47:29 +0000 (12:47 -0800)]
perl: Hide warnings inside perl.h when using gcc compatible compiler

New versions of perl trigger warnings within perl.h with our compiler
flags. At least -Wdeclaration-after-statement, -Wshadow=compatible-local are
known to be problematic.

To avoid these warnings, conditionally use #pragma GCC system_header before
including plperl.h.

Alternatively, we could add the include paths for problematic headers with
-isystem, but that is a larger hammer and is harder to search for.

A more granular alternative would be to use #pragma GCC diagnostic
push/ignored/pop, but gcc warns about unknown warnings being ignored, so every
to-be-ignored-temporarily compiler warning would require its own pg_config.h
symbol and #ifdef.

As the warnings are voluminous, it makes sense to backpatch this change. But
don't do so yet, we first want gather buildfarm coverage - it's e.g. possible
that some compiler claiming to be gcc compatible has issues with the pragma.

Author: Andres Freund <andres@anarazel.de>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: Discussion: https://postgr.es/m/20221228182455.hfdwd22zztvkojy2@awork3.anarazel.de

2 years agoFix oversight in 7a05425d96
Andrew Dunstan [Thu, 29 Dec 2022 12:16:41 +0000 (07:16 -0500)]
Fix oversight in 7a05425d96

This patch was changed as a result of review but one line didn't get the
message. Mea Culpa.

2 years agoAdd page-level freezing to VACUUM.
Peter Geoghegan [Wed, 28 Dec 2022 16:50:47 +0000 (08:50 -0800)]
Add page-level freezing to VACUUM.

Teach VACUUM to decide on whether or not to trigger freezing at the
level of whole heap pages.  Individual XIDs and MXIDs fields from tuple
headers now trigger freezing of whole pages, rather than independently
triggering freezing of each individual tuple header field.

Managing the cost of freezing over time now significantly influences
when and how VACUUM freezes.  The overall amount of WAL written is the
single most important freezing related cost, in general.  Freezing each
page's tuples together in batch allows VACUUM to take full advantage of
the freeze plan WAL deduplication optimization added by commit 9e540599.

Also teach VACUUM to trigger page-level freezing whenever it detects
that heap pruning generated an FPI.  We'll have already written a large
amount of WAL just to do that much, so it's very likely a good idea to
get freezing out of the way for the page early.  This only happens in
cases where it will directly lead to marking the page all-frozen in the
visibility map.

In most cases "freezing a page" removes all XIDs < OldestXmin, and all
MXIDs < OldestMxact.  It doesn't quite work that way in certain rare
cases involving MultiXacts, though.  It is convenient to define "freeze
the page" in a way that gives FreezeMultiXactId the leeway to put off
the work of processing an individual tuple's xmax whenever it happens to
be a MultiXactId that would require an expensive second pass to process
aggressively (allocating a new multi is especially worth avoiding here).
FreezeMultiXactId is eager when processing is cheap (as it usually is),
and lazy in the event of an individual multi that happens to require
expensive second pass processing.  This avoids regressions related to
processing of multis that page-level freezing might otherwise cause.

Author: Peter Geoghegan <pg@bowt.ie>
Reviewed-By: Jeff Davis <pgsql@j-davis.com>
Reviewed-By: Andres Freund <andres@anarazel.de>
Discussion: https://postgr.es/m/CAH2-WzkFok_6EAHuK39GaW4FjEFQsY=3J0AAd6FXk93u-Xq3Fg@mail.gmail.com

2 years agoConvert contrib/ltree's input functions to report errors softly
Andrew Dunstan [Wed, 28 Dec 2022 14:58:04 +0000 (09:58 -0500)]
Convert contrib/ltree's input functions to report errors softly

Reviewed by Tom Lane and Amul Sul

Discussion: https://postgr.es/m/49e598c2-cfe8-0928-b6fb-d0cc51aab626@dunslane.net

2 years agoConvert contrib/intarray's bqarr_in() to report errors softly
Andrew Dunstan [Wed, 28 Dec 2022 14:53:00 +0000 (09:53 -0500)]
Convert contrib/intarray's bqarr_in() to report errors softly

Reviewed by Tom Lane and Amul Sul

Discussion: https://postgr.es/m/49e598c2-cfe8-0928-b6fb-d0cc51aab626@dunslane.net

2 years agoReorder some object files in makefiles
Peter Eisentraut [Wed, 28 Dec 2022 13:20:53 +0000 (14:20 +0100)]
Reorder some object files in makefiles

This restores some once-intended alphabetical orders and makes the
lists consistent between the different build systems.

2 years agoSuppress uninitialized-variable warning from a61b1f748.
Tom Lane [Tue, 27 Dec 2022 23:07:48 +0000 (18:07 -0500)]
Suppress uninitialized-variable warning from a61b1f748.

Some compilers complain about sub_rteperminfos not being
initialized, evidently because they don't detect that it
is only used and set if isGeneralSelect is true.
Make it follow the long-established pattern for its
sibling variable sub_rtable.

Per reports from Pavel Stehule and the buildfarm.

Discussion: https://postgr.es/m/CAFj8pRDOvGOi-n616kM0Cc7qSbg_nGoS=-haB+D785sUXADqSg@mail.gmail.com

2 years agoConvert hstore_in to report errors softly.
Tom Lane [Tue, 27 Dec 2022 19:50:56 +0000 (14:50 -0500)]
Convert hstore_in to report errors softly.

The error reporting here was not only old and crufty, but untested.
I took the opportunity to bring the messages into some sort of
compliance with our message style guidelines.

Discussion: https://postgr.es/m/6B6A5C77-60AD-4A71-9F3A-B2C026A281A6@dunslane.net

2 years agoRemove new locale dependency in regproc regression test.
Tom Lane [Tue, 27 Dec 2022 18:06:42 +0000 (13:06 -0500)]
Remove new locale dependency in regproc regression test.

The modified error message for regcollationin failure includes
the database encoding, which it should've occurred to me is a
portability hazard for the regression tests.  Adjust the test
so the expected output doesn't include that.

In passing, fix a comment typo introduced in b8c0ffbd2.

Per buildfarm.

2 years agoSimplify the implementations of the to_reg* functions.
Tom Lane [Tue, 27 Dec 2022 17:33:04 +0000 (12:33 -0500)]
Simplify the implementations of the to_reg* functions.

Given the soft-input-error feature, we can reduce these functions
to be just thin wrappers around a soft-error call of the
corresponding datatype input function.  This means less code and
more certainty that the to_reg* functions match the normal input
behavior.

Notably, it also means that they will accept numeric OID input,
which they didn't before.  It's not clear to me if that omission
had more than laziness behind it, but it doesn't seem like
something we need to work hard to preserve.

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

2 years agoConvert the reg* input functions to report (most) errors softly.
Tom Lane [Tue, 27 Dec 2022 17:26:01 +0000 (12:26 -0500)]
Convert the reg* input functions to report (most) errors softly.

This is not really complete, but it catches most cases of practical
interest.  The main omissions are:

* regtype, regprocedure, and regoperator parse type names by
calling the main grammar, so any grammar-detected syntax error
will still be a hard error.  Also, if one includes a type
modifier in such a type specification, errors detected by the
typmodin function will be hard errors.

* Lookup errors are handled just by passing missing_ok = true
to the relevant catalog lookup function.  Because we've used
quite a restrictive definition of "missing_ok", this means that
edge cases such as "the named schema exists, but you lack
USAGE permission on it" are still hard errors.

It would make sense to me to replace most/all missing_ok
parameters with an escontext parameter and then allow these
additional lookup failure cases to be trapped too.  But that's
a job for some other day.

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

2 years agoConvert tsqueryin and tsvectorin to report errors softly.
Tom Lane [Tue, 27 Dec 2022 17:00:31 +0000 (12:00 -0500)]
Convert tsqueryin and tsvectorin to report errors softly.

This is slightly tedious because the adjustments cascade through
a couple of levels of subroutines, but it's not very hard.
I chose to avoid changing function signatures more than absolutely
necessary, by passing the escontext pointer in existing structs
where possible.

tsquery's nuisance NOTICEs about empty queries are suppressed in
soft-error mode, since they're not errors and we surely don't want
them to be shown to the user anyway.  Maybe that whole behavior
should be reconsidered.

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

2 years agoDetect bad input for types xid, xid8, and cid.
Tom Lane [Tue, 27 Dec 2022 16:40:01 +0000 (11:40 -0500)]
Detect bad input for types xid, xid8, and cid.

Historically these input functions just called strtoul or strtoull
and returned the result, with no error detection whatever.  Upgrade
them to reject garbage input and out-of-range values, similarly to
our other numeric input routines.

To share the code for this with type oid, adjust the existing
"oidin_subr" to be agnostic about the SQL name of the type it is
handling, and move it to numutils.c; then clone it for 64-bit types.

Because the xid types previously accepted hex and octal input by
reason of calling strtoul[l] with third argument zero, I made the
common subroutine do that too, with the consequence that type oid
now also accepts hex and octal input.  In view of 6fcda9aba, that
seems like a good thing.

While at it, simplify the existing over-complicated handling of
syntax errors from strtoul: we only need one ereturn not three.

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

2 years agoRemove overzealous MultiXact freeze assertion.
Peter Geoghegan [Tue, 27 Dec 2022 07:36:02 +0000 (23:36 -0800)]
Remove overzealous MultiXact freeze assertion.

When VACUUM determines that an existing MultiXact should use a freeze
plan that sets xmax to InvalidTransactionId, the original Multi may or
may not be before OldestMxact.  Remove an incorrect assertion that
expected it to always be from before OldestMxact.

Oversight in commit 4ce3af.

Author: Peter Geoghegan <pg@bowt.ie>
Reported-By: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Discussion: https://postgr.es/m/TYAPR01MB5866B24104FD80B5D7E65C3EF5ED9@TYAPR01MB5866.jpnprd01.prod.outlook.com

2 years agoAdd custom filtering rules to the TAP tests of pg_upgrade
Michael Paquier [Tue, 27 Dec 2022 05:35:56 +0000 (14:35 +0900)]
Add custom filtering rules to the TAP tests of pg_upgrade

002_pg_upgrade.pl gains support for a new environment variable called
"filter_rules", that can be used to point to a file that includes a
set of custom regular expressions that would be applied to the dumps of
the origin and target clusters when doing a cross-version test (aka when
defining olddump and oldinstall), to give the possibility to reshape
dynamically the dumps in the same way as the internals of the buildfarm
code so as the tests are able to pass in scenarios where one expects
them to even if pg_dump generates slightly-different outputs depending
on the versions involved.

This option is not used when pg_upgrade runs with the same version for
the origin and target clusters, and it is the last piece I see as
required to be able to plug-in more efficiently the TAP tests of
pg_upgrade with the buildfarm or just a CI.

Author: Anton A. Melnikov
Discussion: https://postgr.es/m/49f389ba-95ce-8a9b-09ae-f60650c0e7c7@inbox.ru

2 years agoFix incorrect copy-pasto in error message of pg_waldump.c
Michael Paquier [Tue, 27 Dec 2022 03:16:24 +0000 (12:16 +0900)]
Fix incorrect copy-pasto in error message of pg_waldump.c

The error message used on fclose() failure was incorrect, so fix it.
Oversight in d497093, that I have somehow managed to miss.

2 years agopg_waldump: Add --save-fullpage=PATH to save full page images from WAL records
Michael Paquier [Mon, 26 Dec 2022 23:27:53 +0000 (08:27 +0900)]
pg_waldump: Add --save-fullpage=PATH to save full page images from WAL records

This option extracts (potentially decompressing) full-page images
included in WAL records into a given target directory.  These images are
subject to the same filtering rules as the normal display of the WAL
records, hence with --relation one can for example extract only the FPIs
issued on the relation defined.  By default, the records are printed or
their stats computed (--stats), using --quiet would only save the images
without any output generated.

This is a tool aimed mostly for very experienced users, useful for
fixing page-level corruption or just analyzing the past state of a page,
and there were no easy way to do that with the in-core tools up to now
when looking at WAL.

Each block is saved in a separate file, to ease their manipulation, with
the file respecting <lsn>.<ts>.<db>.<rel>.<blk>_<fork> with as format.

For instance, 00000000-010000C0.1663.1.6117.123_main refers to:
- WAL record LSN in hexa format (00000000-010000C0).
- Tablespace OID (1663).
- Database OID (1).
- Relfilenode (6117).
- Block number (123).
- Fork name of the file this block came from (_main).

Author: David Christensen
Reviewed-by: Sho Kato, Justin Pryzby, Bharath Rupireddy, Matthias van de
Meent
Discussion: https://postgr.es/m/CAOxo6XKjQb2bMSBRpePf3ZpzfNTwjQUc4Tafh21=jzjX6bX8CA@mail.gmail.com

2 years agoAdd 'logical_decoding_mode' GUC.
Amit Kapila [Mon, 26 Dec 2022 03:28:16 +0000 (08:58 +0530)]
Add 'logical_decoding_mode' GUC.

This enables streaming or serializing changes immediately in logical
decoding. This parameter is intended to be used to test logical decoding
and replication of large transactions for which otherwise we need to
generate the changes till logical_decoding_work_mem is reached.

This helps in reducing the timing of existing tests related to logical
replication of in-progress transactions and will help in writing tests for
for the upcoming feature for parallelly applying large in-progress
transactions.

Author: Shi yu
Reviewed-by: Sawada Masahiko, Shveta Mallik, Amit Kapila, Dilip Kumar, Kuroda Hayato, Kyotaro Horiguchi
Discussion: https://postgr.es/m/OSZPR01MB63104E7449DBE41932DB19F1FD1B9@OSZPR01MB6310.jpnprd01.prod.outlook.com

2 years agoSwitch query fixing aclitems in ~15 from O(N^2) to O(N) in upgrade_adapt.sql
Michael Paquier [Sun, 25 Dec 2022 23:00:55 +0000 (08:00 +0900)]
Switch query fixing aclitems in ~15 from O(N^2) to O(N) in upgrade_adapt.sql

f4f2f2b was doing a sequential scan of pg_class before checking if a
relation had attributes dependent on aclitem as data typewhen building
the set of ALTER TABLE queries, but it would be costly on a regression
database.

While on it, make the query style more consistent with the rest.

Reported-by: Justin Pryzby
Discussion: https://postgr.es/m/20221223032724.GQ1153@telsasoft.com

2 years agoConvert enum_in() to report errors softly.
Tom Lane [Sun, 25 Dec 2022 19:32:02 +0000 (14:32 -0500)]
Convert enum_in() to report errors softly.

I missed this in my initial survey, probably because I examined
the contents of pg_type in the postgres database, which lacks
any enumerated types.

Discussion: https://postgr.es/m/CAAJ_b97KeDWUdpTKGOaFYPv0OicjOu6EW+QYWj-Ywrgj_aEy1g@mail.gmail.com

2 years agoFix thinko in 720e0327bc
Andrew Dunstan [Sun, 25 Dec 2022 09:36:58 +0000 (04:36 -0500)]
Fix thinko in 720e0327bc

2 years agoConvert contrib/isn's input functions to report errors softly
Andrew Dunstan [Sat, 24 Dec 2022 20:28:13 +0000 (15:28 -0500)]
Convert contrib/isn's input functions to report errors softly

2 years agoConvert jsonpath's input function to report errors softly
Andrew Dunstan [Sat, 24 Dec 2022 20:19:14 +0000 (15:19 -0500)]
Convert jsonpath's input function to report errors softly

Reviewed by Tom Lane

Discussion: https://postgr.es/m/a8dc5700-c341-3ba8-0507-cc09881e6200@dunslane.net

2 years agoMake the numeric-OID cases of regprocin and friends be non-throwing.
Tom Lane [Sat, 24 Dec 2022 20:01:21 +0000 (15:01 -0500)]
Make the numeric-OID cases of regprocin and friends be non-throwing.

While at it, use a common subroutine already.

This doesn't move the needle very far in terms of making these
functions non-throwing; the only case we're now able to trap is
numeric-OID-is-out-of-range.  Still, it seems like a pretty
non-controversial step in that direction.

2 years agoFix recent accidental omission in pg_proc.dat
David Rowley [Sat, 24 Dec 2022 00:18:35 +0000 (13:18 +1300)]
Fix recent accidental omission in pg_proc.dat

ed1a88dda added support functions for the ntile(), percent_rank() and
cume_dist() window functions but neglected to actually add these support
functions to the pg_proc entry for the corresponding window function.

Also, take this opportunity to add these window functions to one of the
regression tests added in ed1a88dda to give the support functions a little
bit of exercise.  If I'd done that in the first place then the omission
would have been more obvious.

Bump the catversion, again.

2 years agoRework <warning> box about column list combining in logical replication
Alvaro Herrera [Fri, 23 Dec 2022 16:49:51 +0000 (17:49 +0100)]
Rework <warning> box about column list combining in logical replication

After some copy-edit I made in commit 3a06a79cd137, we have a <sect2>
that only contains a warning box.  This doesn't look good.  Rework by
moving the sect2 title to be the warning's title, and put the 'id' to it
as well, so that the external reference continues to work.

Backpatch to 15.

In branch master, I also take the opportunity to add titles to a couple
of other warning boxes elsewhere in the documentation.

Discussion: https://postgr.es/m/20221219164713.ccnlvtkyj6lmshqq@alvherre.pgsql

2 years agoFix end LSN determination in recently added test
Alvaro Herrera [Fri, 23 Dec 2022 16:27:05 +0000 (17:27 +0100)]
Fix end LSN determination in recently added test

The test added in commit e44dae07f931 has a thinko: it wants to read
info about a few WAL records, but it obtains the LSN of the final record
to read by asking for the WAL insert position; however,
pg_get_wal_records_info only accepts to read up to the flush position
(cf. IsFutureLSN()).  In normal conditions there is no difference, since
the last record written by the preceding loop is known flushed and it's
the one the test wants; but it's possible to have some other process
insert another WAL record that isn't flushed, and that causes the whole
test to explode.

Fix by having pg_get_wal_records_info() read only up to the flushed
position.  Backpatch to 15, which is where pg_walinspect appeared.

Author: Karina Litskevich <litskevichkarina@gmail.com>
Discussion: https://postgr.es/m/a5559c95-52c3-5eea-cd63-9b4f1c70ff96@gmail.com

2 years agoConvert contrib/seg's input function to report errors softly
Andrew Dunstan [Fri, 23 Dec 2022 14:17:24 +0000 (09:17 -0500)]
Convert contrib/seg's input function to report errors softly

Reviewed by Tom Lane

Discussion: https://postgr.es/m/a8dc5700-c341-3ba8-0507-cc09881e6200@dunslane.net

2 years agoFix event trigger example
Alvaro Herrera [Fri, 23 Dec 2022 12:21:41 +0000 (13:21 +0100)]
Fix event trigger example

Commit 2f9661311b changed command tags from strings to numbers, but
forgot to adjust the code in the event trigger example, which
consequently failed to compile.

While fixing that, improve the indentation to adhere to pgindent style.

Backpatch to v13, where the change was introduced.

Author: Laurenz Albe
Discussion: https://postgr.es/m/81e36ac17dc80489e74dc5b6914afa6ccdb1a99d.camel@cybertec.at

2 years agoFix bug in translate_col_privs_multilevel
David Rowley [Fri, 23 Dec 2022 11:58:34 +0000 (00:58 +1300)]
Fix bug in translate_col_privs_multilevel

Fix incorrect code which was trying to convert a Bitmapset of columns at
the attnums according to a parent table and transform them into the
equivalent Bitmapset with same attnums according to the given child table.
This code is new as of a61b1f748 and was failing to do the correct
translation when there was an intermediate parent table between 'rel' and
'top_parent_rel'.

Reported-by: Ranier Vilela
Author: Richard Guo, Amit Langote
Discussion: https://postgr.es/m/CAEudQArohfB_Gy%2BhcH2-bANUkxgjJiP%3DABq01_LgTNTbcNijag%40mail.gmail.com

2 years agoAllow parent's WaitEventSets to be freed after fork().
Thomas Munro [Fri, 23 Dec 2022 07:26:52 +0000 (20:26 +1300)]
Allow parent's WaitEventSets to be freed after fork().

An epoll fd belonging to the parent should be closed in the child.  A
kqueue fd is automatically closed by fork(), but we should still adjust
our counter.  For poll and Windows systems, nothing special is required.
On all systems we free the memory.

No caller yet, but we'll need this if we start using WaitEventSet in the
postmaster as planned.

Reviewed-by: Andres Freund <andres@anarazel.de>
Discussion: https://postgr.es/m/CA%2BhUKG%2BZ-HpOj1JsO9eWUP%2Bar7npSVinsC_npxSy%2BjdOMsx%3DGg%40mail.gmail.com

2 years agoDon't leak a signalfd when using latches in the postmaster.
Thomas Munro [Fri, 23 Dec 2022 07:24:41 +0000 (20:24 +1300)]
Don't leak a signalfd when using latches in the post

At the time of commit 6a2a70a02 we didn't use latch infrastructure in
the postmaster.  We're planning to start doing that, so we'd better make
sure that the signalfd inherited from a postmaster is not duplicated and
then leaked in the child.

Reviewed-by: Andres Freund <andres@anarazel.de>
Reviewed-by: Justin Pryzby <pryzby@telsasoft.com>
Discussion: https://postgr.es/m/CA%2BhUKG%2BZ-HpOj1JsO9eWUP%2Bar7npSVinsC_npxSy%2BjdOMsx%3DGg%40mail.gmail.com

2 years agoAdd WL_SOCKET_ACCEPT event to WaitEventSet API.
Thomas Munro [Fri, 23 Dec 2022 07:21:47 +0000 (20:21 +1300)]
Add WL_SOCKET_ACCEPT event to WaitEventSet API.

To be able to handle incoming connections on a server socket with
the WaitEventSet API, we'll need a new kind of event to indicate that
the the socket is ready to accept a connection.

On Unix, it's just the same as WL_SOCKET_READABLE, but on Windows there
is a different underlying kernel event that we need to map our
abstraction to.

No user yet, but a proposed patch would use this.

Reviewed-by: Andres Freund <andres@anarazel.de>
Discussion: https://postgr.es/m/CA%2BhUKG%2BZ-HpOj1JsO9eWUP%2Bar7npSVinsC_npxSy%2BjdOMsx%3DGg%40mail.gmail.com

2 years agoUpdate upgrade_adapt.sql to handle tables using aclitem as data type
Michael Paquier [Fri, 23 Dec 2022 02:36:57 +0000 (11:36 +0900)]
Update upgrade_adapt.sql to handle tables using aclitem as data type

The regression test suite includes a table called "tab_core_types" that
has one attribute based on the type "aclitem".  Keeping this attribute
as-is causes hard failures when running pg_upgrade with an origin on
~15.  This commit updates upgrade_adapt.sql to automatically detect the
tables with such attributes and switch them to text so as pg_upgrade
is able to go through its run.

This does not provide the same detection coverage as pg_upgrade, where
we are able to find out aclitems used in arrays, domains or even
composite types, but this is (I guess) enough for most things like an
instance that had installcheck run on before the upgrade with a dump
generated from it.

Note that the buildfarm code has taken the simplest approach of just
dropping "tab_core_types", so what we have here is more modular.

Author: Anton A. Melnikov
Discussion: https://postgr.es/m/49f389ba-95ce-8a9b-09ae-f60650c0e7c7@inbox.ru