users/andresfreund/postgres.git
4 years agofixup! aio: --- BASE PATCH -- (to-be-split). aio
Andres Freund [Wed, 13 Jan 2021 01:16:12 +0000 (17:16 -0800)]
fixup! aio: --- BASE PATCH -- (to-be-split).

4 years agoaio: util: pass AIO to streaming write completion callback.
Andres Freund [Tue, 12 Jan 2021 04:57:38 +0000 (20:57 -0800)]
aio: util: pass AIO to streaming write completion callback.

Not yet useful, because there's yet a way to access the result of the
write.

4 years agoaio: fix flush_range.offset/nbytes type confusion.
Andres Freund [Tue, 12 Jan 2021 04:25:02 +0000 (20:25 -0800)]
aio: fix flush_range.offset/nbytes type confusion.

4 years agoaio: move debug printing of IOs into callback.
Andres Freund [Tue, 12 Jan 2021 04:19:26 +0000 (20:19 -0800)]
aio: move debug printing of IOs into callback.

4 years agoaio: clean up use of volatile
Andres Freund [Tue, 12 Jan 2021 03:07:11 +0000 (19:07 -0800)]
aio: clean up use of volatile

The reason to use volatile (before being copy pasted into other
places) is just to prevent the compiler from doing flag updates
piecemeal. Add a macro wrapper with a comment to make that clearer.

4 years agoaio: callbackify IO action type specific handling, starting with retry.
Andres Freund [Tue, 12 Jan 2021 01:14:04 +0000 (17:14 -0800)]
aio: callbackify IO action type specific handling, starting with retry.

4 years agoaio: wal: extend pg_stat_wal.
Andres Freund [Thu, 31 Dec 2020 04:47:45 +0000 (20:47 -0800)]
aio: wal: extend pg_stat_wal.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agoaio: wal: padding of partial records.
Andres Freund [Thu, 31 Dec 2020 04:26:52 +0000 (20:26 -0800)]
aio: wal: padding of partial records.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agowip: xlog: async commit: reduce frequency of latch sets.
Andres Freund [Thu, 31 Dec 2020 04:21:49 +0000 (20:21 -0800)]
wip: xlog: async commit: reduce frequency of latch sets.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agoxlog: Use LWLockAcquireOrWait() in AdvanceXLInsertBuffer.
Andres Freund [Thu, 31 Dec 2020 04:04:10 +0000 (20:04 -0800)]
xlog: Use LWLockAcquireOrWait() in AdvanceXLInsertBuffer.

Otherwise a lot of backends may unnecessarily acquire the lock
exclusively one-by-one.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agoaio: WAL: WIP: concurrent WAL flushes
Andres Freund [Mon, 11 Jan 2021 21:52:06 +0000 (13:52 -0800)]
aio: WAL: WIP: concurrent WAL flushes

4 years agowip: bulk extend
Andres Freund [Mon, 9 Nov 2020 22:27:39 +0000 (14:27 -0800)]
wip: bulk extend

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agoaio: heapam prefetching.
Andres Freund [Mon, 9 Nov 2020 22:20:42 +0000 (14:20 -0800)]
aio: heapam prefetching.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agoaio: vacuum: Make first scan use prefetching.
Andres Freund [Mon, 25 May 2020 10:02:56 +0000 (03:02 -0700)]
aio: vacuum: Make first scan use prefetching.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agoaio: bitmap heap scan: Minimal and hacky improvement for prefetching.
Andres Freund [Thu, 28 May 2020 21:46:05 +0000 (14:46 -0700)]
aio: bitmap heap scan: Minimal and hacky improvement for prefetching.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agoaio: --- BASE PATCH -- (to-be-split).
Andres Freund [Thu, 29 Oct 2020 19:41:12 +0000 (12:41 -0700)]
aio: --- BASE PATCH -- (to-be-split).

4 years agolatch / signal perf stuff: generic
Andres Freund [Sat, 31 Oct 2020 00:12:09 +0000 (17:12 -0700)]
latch / signal perf stuff: generic

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agocondition variable: add ConditionVariableCancelSleepEx().
Andres Freund [Thu, 31 Dec 2020 04:42:08 +0000 (20:42 -0800)]
condition variable: add ConditionVariableCancelSleepEx().

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agoaio: allow lwlocks to be unowned.
Andres Freund [Tue, 5 Jan 2021 18:10:36 +0000 (10:10 -0800)]
aio: allow lwlocks to be unowned.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agoheapam: fastorder.
Andres Freund [Mon, 9 Nov 2020 22:21:09 +0000 (14:21 -0800)]
heapam: fastorder.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agoFix O(recycled_segments^2) stat() calls while recycling WAL files.
Andres Freund [Thu, 31 Dec 2020 01:35:26 +0000 (17:35 -0800)]
Fix O(recycled_segments^2) stat() calls while recycling WAL files.

Author: Michael Paquier <[email protected]>
Reviewed-By: Andres Freund <[email protected]>
Discussion: https://postgr.es/m/CAB7nPqTB3VcKSSrW2Qj59tYYR2H4+n=5pZbdWou+X9iqVNMCag@mail.gmail.com
Backpatch:

4 years agoaio: Add some error checking around pinning.
Andres Freund [Thu, 2 Jul 2020 02:06:45 +0000 (19:06 -0700)]
aio: Add some error checking around pinning.

4 years agoexecutor: Reduce overhead of instrumentation a bit by allowing more inlining.
Andres Freund [Thu, 28 May 2020 21:49:45 +0000 (14:49 -0700)]
executor: Reduce overhead of instrumentation a bit by allowing more inlining.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agoWIP: Change instr_time to just store nanoseconds, that's cheaper.
Andres Freund [Thu, 28 May 2020 21:52:48 +0000 (14:52 -0700)]
WIP: Change instr_time to just store nanoseconds, that's cheaper.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agoheapam: WIP: cacheline prefetching for hot pruning.
Andres Freund [Thu, 28 May 2020 21:51:58 +0000 (14:51 -0700)]
heapam: WIP: cacheline prefetching for hot pruning.

Author: Andres Freund, with contributions from Dmitry Dolgov
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

fixup! heapam: WIP: cacheline prefetching for hot pruning.

4 years agoheapam: Don't re-inquire block number for each tuple during pruning.
Andres Freund [Thu, 28 May 2020 21:50:50 +0000 (14:50 -0700)]
heapam: Don't re-inquire block number for each tuple during pruning.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agowalwriter: Separate sleep from delay.
Andres Freund [Mon, 25 May 2020 10:02:56 +0000 (03:02 -0700)]
walwriter: Separate sleep from delay.

We should probably instead wake walwriter when wal_writer_flush_after
has passed, even if wal_writer_delay wouldn't yet trigger.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agolwlock, xlog: Report caller wait event for LWLockWaitForVar.
Andres Freund [Thu, 29 Oct 2020 19:28:11 +0000 (12:28 -0700)]
lwlock, xlog: Report caller wait event for LWLockWaitForVar.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agoxlog: Avoid repeated scans of all xlog insert locks when replacing contents.
Andres Freund [Mon, 25 May 2020 10:02:56 +0000 (03:02 -0700)]
xlog: Avoid repeated scans of all xlog insert locks when replacing contents.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agotmp: work around occasional hangs in subscription/t/013_partition.pl
Andres Freund [Thu, 25 Jun 2020 00:12:46 +0000 (17:12 -0700)]
tmp: work around occasional hangs in subscription/t/013_partition.pl

I think this may be an issue in the test, but I'm not yet actually
sure.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agotmp: work around instability in tuplesort test.
Andres Freund [Mon, 25 May 2020 10:02:56 +0000 (03:02 -0700)]
tmp: work around instability in tuplesort test.

Not really clear why this changes right now.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agoilist.h debugging improvements.
Andres Freund [Mon, 25 May 2020 10:02:56 +0000 (03:02 -0700)]
ilist.h debugging improvements.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agoWIP: Fix (non-aio) bug leading to checkpointer not shutting down etc.
Andres Freund [Thu, 29 Oct 2020 19:15:55 +0000 (12:15 -0700)]
WIP: Fix (non-aio) bug leading to checkpointer not shutting down etc.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/20200517010005[email protected]
Backpatch:

4 years agoWIP: Increase default ringbuffer size.
Andres Freund [Mon, 25 May 2020 10:02:55 +0000 (03:02 -0700)]
WIP: Increase default ringbuffer size.

Otherwise they're too small to usefully prefetch into.

4 years agoAdd allocator support for larger allocation alignment & use for IO.
Andres Freund [Wed, 24 Jun 2020 23:37:39 +0000 (16:37 -0700)]
Add allocator support for larger allocation alignment & use for IO.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agoaio: WIP: align PGAlignedBlock to page size
Andres Freund [Wed, 24 Jun 2020 23:35:49 +0000 (16:35 -0700)]
aio: WIP: align PGAlignedBlock to page size

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agoAdd --with-liburing configure option.
Andres Freund [Mon, 25 May 2020 10:02:55 +0000 (03:02 -0700)]
Add --with-liburing configure option.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agoReplace buffer I/O locks with condition variables.
Andres Freund [Thu, 29 Oct 2020 17:57:34 +0000 (10:57 -0700)]
Replace buffer I/O locks with condition variables.

Author: Robert Haas
Discussion: https://postgr.es/m/CA+Tgmoaj2aPti0yho7FeEf2qt-JgQPRWb0gci_o1Hfr=C56Xng@mail.gmail.com

4 years agoxlog: bufixes for pg_stat_wal.
Andres Freund [Mon, 11 Jan 2021 22:52:32 +0000 (14:52 -0800)]
xlog: bufixes for pg_stat_wal.

Fixes: 01469241b2ff6f17fc0b3f20f01559145ffab29e
Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:

4 years agoFix function prototypes in dependency.h.
Thomas Munro [Mon, 11 Jan 2021 21:55:35 +0000 (10:55 +1300)]
Fix function prototypes in dependency.h.

Commit 257836a7 accidentally deleted a couple of
redundant-but-conventional "extern" keywords on function prototypes.
Put them back.

Reported-by: Alvaro Herrera <[email protected]>
4 years agoRethink SQLSTATE code for ERRCODE_IDLE_SESSION_TIMEOUT.
Tom Lane [Mon, 11 Jan 2021 19:53:42 +0000 (14:53 -0500)]
Rethink SQLSTATE code for ERRCODE_IDLE_SESSION_TIMEOUT.

Move it to class 57 (Operator Intervention), which seems like a
better choice given that from the client's standpoint it behaves
a heck of a lot like, e.g., ERRCODE_ADMIN_SHUTDOWN.

In a green field I'd put ERRCODE_IDLE_IN_TRANSACTION_SESSION_TIMEOUT
here as well.  But that's been around for a few years, so it's
probably too late to change its SQLSTATE code.

Discussion: https://postgr.es/m/763A0689-F189-459E-946F-F0EC4458980B@hotmail.com

4 years agoTry next host after a "cannot connect now" failure.
Tom Lane [Mon, 11 Jan 2021 19:12:31 +0000 (14:12 -0500)]
Try next host after a "cannot connect now" failure.

If a server returns ERRCODE_CANNOT_CONNECT_NOW, try the next host,
if multiple host names have been provided.  This allows dealing
gracefully with standby servers that might not be in hot standby mode
yet.

In the wake of the preceding commit, it might be plausible to retry
many more error cases than we do now, but I (tgl) am hesitant to
move too aggressively on that --- it's not clear it'd be desirable
for cases such as bad-password, for example.  But this case seems
safe enough.

Hubert Zhang, reviewed by Takayuki Tsunakawa

Discussion: https://postgr.es/m/BN6PR05MB3492948E4FD76C156E747E8BC9160@BN6PR05MB3492.namprd05.prod.outlook.com

4 years agoUniformly identify the target host in libpq connection failure reports.
Tom Lane [Mon, 11 Jan 2021 19:03:39 +0000 (14:03 -0500)]
Uniformly identify the target host in libpq connection failure reports.

Prefix "could not connect to host-or-socket-path:" to all connection
failure cases that occur after the socket() call, and remove the
ad-hoc server identity data that was appended to a few of these
messages.  This should produce much more intelligible error reports
in multiple-target-host situations, especially for error cases that
are off the beaten track to any degree (because none of those provided
any server identity info).

As an example of the change, formerly a connection attempt with a bad
port number such as "psql -p 12345 -h localhost,/tmp" might produce

psql: error: could not connect to server: Connection refused
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 12345?
could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 12345?
could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/tmp/.s.PGSQL.12345"?

Now it looks like

psql: error: could not connect to host "localhost" (::1), port 12345: Connection refused
        Is the server running on that host and accepting TCP/IP connections?
could not connect to host "localhost" (127.0.0.1), port 12345: Connection refused
        Is the server running on that host and accepting TCP/IP connections?
could not connect to socket "/tmp/.s.PGSQL.12345": No such file or directory
        Is the server running locally and accepting connections on that socket?

This requires adjusting a couple of regression tests to allow for
variation in the contents of a connection failure message.

Discussion: https://postgr.es/m/BN6PR05MB3492948E4FD76C156E747E8BC9160@BN6PR05MB3492.namprd05.prod.outlook.com

4 years agoAllow pg_regress.c wrappers to postprocess test result files.
Tom Lane [Mon, 11 Jan 2021 18:43:19 +0000 (13:43 -0500)]
Allow pg_regress.c wrappers to postprocess test result files.

Add an optional callback to regression_main() that, if provided,
is invoked on each test output file before we try to compare it
to the expected-result file.

The main and isolation test programs don't need this (yet).
In pg_regress_ecpg, add a filter that eliminates target-host
details from "could not connect" error reports.  This filter
doesn't do anything as of this commit, but it will be needed
by the next one.

In the long run we might want to provide some more general,
perhaps pattern-based, filtering mechanism for test output.
For now, this will solve the immediate problem.

Discussion: https://postgr.es/m/BN6PR05MB3492948E4FD76C156E747E8BC9160@BN6PR05MB3492.namprd05.prod.outlook.com

4 years agoIn libpq, always append new error messages to conn->errorMessage.
Tom Lane [Mon, 11 Jan 2021 18:12:09 +0000 (13:12 -0500)]
In libpq, always append new error messages to conn->errorMessage.

Previously, we had an undisciplined mish-mash of printfPQExpBuffer and
appendPQExpBuffer calls to report errors within libpq.  This commit
establishes a uniform rule that appendPQExpBuffer[Str] should be used.
conn->errorMessage is reset only at the start of an application request,
and then accumulates messages till we're done.  We can remove no less
than three different ad-hoc mechanisms that were used to get the effect
of concatenation of error messages within a sequence of operations.

Although this makes things quite a bit cleaner conceptually, the main
reason to do it is to make the world safer for the multiple-target-host
feature that was added awhile back.  Previously, there were many cases
in which an error occurring during an individual host connection attempt
would wipe out the record of what had happened during previous attempts.
(The reporting is still inadequate, in that it can be hard to tell which
host got the failure, but that seems like a matter for a separate commit.)

Currently, lo_import and lo_export contain exceptions to the "never
use printfPQExpBuffer" rule.  If we changed them, we'd risk reporting
an incidental lo_close failure before the actual read or write
failure, which would be confusing, not least because lo_close happened
after the main failure.  We could improve this by inventing an
internal version of lo_close that doesn't reset the errorMessage; but
we'd also need a version of PQfn() that does that, and it didn't quite
seem worth the trouble for now.

Discussion: https://postgr.es/m/BN6PR05MB3492948E4FD76C156E747E8BC9160@BN6PR05MB3492.namprd05.prod.outlook.com

4 years agoUse vectored I/O to fill new WAL segments.
Thomas Munro [Mon, 11 Jan 2021 01:41:39 +0000 (14:41 +1300)]
Use vectored I/O to fill new WAL segments.

Instead of making many block-sized write() calls to fill a new WAL file
with zeroes, make a smaller number of pwritev() calls (or various
emulations).  The actual number depends on the OS's IOV_MAX, which
PG_IOV_MAX currently caps at 32.  That means we'll write 256kB per call
on typical systems.  We may want to tune the number later with more
experience.

Reviewed-by: Tom Lane <[email protected]>
Reviewed-by: Andres Freund <[email protected]>
Discussion: https://postgr.es/m/CA%2BhUKGJA%2Bu-220VONeoREBXJ9P3S94Y7J%2BkqCnTYmahvZJwM%3Dg%40mail.gmail.com

4 years agoProvide pg_preadv() and pg_pwritev().
Thomas Munro [Mon, 11 Jan 2021 01:37:13 +0000 (14:37 +1300)]
Provide pg_preadv() and pg_pwritev().

Provide synchronous vectored file I/O routines.  These map to preadv()
and pwritev(), with fallback implementations for systems that don't have
them.  Also provide a wrapper pg_pwritev_with_retry() that automatically
retries on short writes.

Reviewed-by: Tom Lane <[email protected]>
Reviewed-by: Andres Freund <[email protected]>
Discussion: https://postgr.es/m/CA%2BhUKGJA%2Bu-220VONeoREBXJ9P3S94Y7J%2BkqCnTYmahvZJwM%3Dg%40mail.gmail.com

4 years agodoc: expand description of how non-SELECT queries are processed
Bruce Momjian [Sat, 9 Jan 2021 17:11:16 +0000 (12:11 -0500)]
doc:  expand description of how non-SELECT queries are processed

The previous description of how the executor processes non-SELECT
queries was very dense, causing lack of clarity.  This expanded text
spells it out more simply.

Reported-by: [email protected]
Discussion: https://postgr.es/m/160912275508.676.17469511338925622905@wrigleys.postgresql.org

Backpatch-through: 9.5

4 years agoFix the test for decoding of two-phase transactions.
Amit Kapila [Sat, 9 Jan 2021 10:15:38 +0000 (15:45 +0530)]
Fix the test for decoding of two-phase transactions.

Commit 5a3574d7b3 added the test for decoding of two-phase transactions
during the build of a consistent snapshot. The test forgot to skip empty
xacts which can lead to decoding of extra empty transactions due to
background activity by autovacuum.

Per report by buildfarm.

Reported-by: Tom Lane
Discussion: https://postgr.es/m/363512.1610171267@sss.pgh.pa.us

4 years agoFix plpgsql tests for debug_invalidate_system_caches_always.
Tom Lane [Fri, 8 Jan 2021 23:12:07 +0000 (18:12 -0500)]
Fix plpgsql tests for debug_invalidate_system_caches_always.

Commit c9d529848 resulted in having a couple more places where
the error context stack for a failure varies depending on
debug_invalidate_system_caches_always (nee CLOBBER_CACHE_ALWAYS).
This is not very surprising, since we have to re-parse cached
plans if the plan cache is clobbered.  Stabilize the expected
test output by hiding the context stack in these places,
as we've done elsewhere in this test script.

(Another idea worth considering, now that we have
debug_invalidate_system_caches_always, is to force it to zero for
these test cases.  That seems like it'd risk reducing the coverage
of cache-clobber testing, which might or might not be worth being
able to verify that we get the expected error output in normal
cases.  For the moment I just stuck with the existing technique.)

In passing, update comments that referred to CLOBBER_CACHE_ALWAYS.

Per buildfarm member hyrax.

4 years agoFix ancient bug in parsing of BRE-mode regular expressions.
Tom Lane [Fri, 8 Jan 2021 17:16:00 +0000 (12:16 -0500)]
Fix ancient bug in parsing of BRE-mode regular expressions.

brenext(), when parsing a '*' quantifier, forgot to return any "value"
for the token; per the equivalent case in next(), it should return
value 1 to indicate that greedy rather than non-greedy behavior is
wanted.  The result is that the compiled regexp could behave like 'x*?'
rather than the intended 'x*', if we were unlucky enough to have
a zero in v->nextvalue at this point.  That seems to happen with some
reliability if we have '.*' at the beginning of a BRE-mode regexp,
although that depends on the initial contents of a stack-allocated
struct, so it's not guaranteed to fail.

Found by Alexander Lakhin using valgrind testing.  This bug seems
to be aboriginal in Spencer's code, so back-patch all the way.

Discussion: https://postgr.es/m/16814-6c5e3edd2bdf0d50@postgresql.org

4 years agoFix and simplify some code related to cryptohashes
Michael Paquier [Fri, 8 Jan 2021 01:37:03 +0000 (10:37 +0900)]
Fix and simplify some code related to cryptohashes

This commit addresses two issues:
- In pgcrypto, MD5 computation called pg_cryptohash_{init,update,final}
without checking for the result status.
- Simplify pg_checksum_raw_context to use only one variable for all the
SHA2 options available in checksum manifests.

Reported-by: Heikki Linnakangas
Discussion: https://postgr.es/m/f62f26bb-47a5-8411-46e5-4350823e06a5@iki.fi

4 years agoAdjust createdb TAP tests to work on recent OpenBSD.
Tom Lane [Fri, 8 Jan 2021 01:36:09 +0000 (20:36 -0500)]
Adjust createdb TAP tests to work on recent OpenBSD.

We found last February that the error-case tests added by commit
008cf0409 failed on OpenBSD, because that platform doesn't really
check locale names.  At the time it seemed that that was only an issue
for LC_CTYPE, but testing on a more recent version of OpenBSD shows
that it's now equally lax about LC_COLLATE.

Rather than dropping the LC_COLLATE test too, put back LC_CTYPE
(reverting c4b0edb07), and adjust these tests to accept the different
error message that we get if setlocale() doesn't reject a bogus locale
name.  The point of these tests is not really what the backend does
with the locale name, but to show that createdb quotes funny locale
names safely; so we're not losing test reliability this way.

Back-patch as appropriate.

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

4 years agoFurther second thoughts about idle_session_timeout patch.
Tom Lane [Thu, 7 Jan 2021 16:45:08 +0000 (11:45 -0500)]
Further second thoughts about idle_session_timeout patch.

On reflection, the order of operations in PostgresMain() is wrong.
These timeouts ought to be shut down before, not after, we do the
post-command-read CHECK_FOR_INTERRUPTS, to guarantee that any
timeout error will be detected there rather than at some ill-defined
later point (possibly after having wasted a lot of work).

This is really an error in the original idle_in_transaction_timeout
patch, so back-patch to 9.6 where that was introduced.

4 years agoMinor fixes in COPY progress docs
Tomas Vondra [Thu, 7 Jan 2021 16:31:58 +0000 (17:31 +0100)]
Minor fixes in COPY progress docs

Author: Justin Pryzby
Discussion: https://postgr.es/m/CAFp7Qwr6_FmRM6pCO0x_a0mymOfX_Gg+FEKet4XaTGSW=LitKQ@mail.gmail.com

4 years agoAdd GUC to log long wait times on recovery conflicts.
Fujii Masao [Thu, 7 Jan 2021 15:47:03 +0000 (00:47 +0900)]
Add GUC to log long wait times on recovery conflicts.

This commit adds GUC log_recovery_conflict_waits that controls whether
a log message is produced when the startup process is waiting longer than
deadlock_timeout for recovery conflicts. This is useful in determining
if recovery conflicts prevent the recovery from applying WAL.

Note that currently a log message is produced only when recovery conflict
has not been resolved yet even after deadlock_timeout passes, i.e.,
only when the startup process is still waiting for recovery conflict
even after deadlock_timeout.

Author: Bertrand Drouvot, Masahiko Sawada
Reviewed-by: Alvaro Herrera, Kyotaro Horiguchi, Fujii Masao
Discussion: https://postgr.es/m/9a60178c-a853-1440-2cdc-c3af916cff59@amazon.com

4 years agoFix bogus link in test comments.
Tom Lane [Thu, 7 Jan 2021 03:09:00 +0000 (22:09 -0500)]
Fix bogus link in test comments.

I apparently copied-and-pasted the wrong link in commit ca8217c10.
Point it where it was meant to go.

4 years agoImprove commentary in timeout.c.
Tom Lane [Thu, 7 Jan 2021 03:02:35 +0000 (22:02 -0500)]
Improve commentary in timeout.c.

On re-reading I realized that I'd missed one race condition in the new
timeout code.  It's safe, but add a comment explaining it.

Discussion: https://postgr.es/m/CA+hUKG+o6pbuHBJSGnud=TadsuXySWA7CCcPgCt2QE9F6_4iHQ@mail.gmail.com

4 years agoFix allocation logic of cryptohash context data with OpenSSL
Michael Paquier [Thu, 7 Jan 2021 01:21:02 +0000 (10:21 +0900)]
Fix allocation logic of cryptohash context data with OpenSSL

The allocation of the cryptohash context data when building with OpenSSL
was happening in the memory context of the caller of
pg_cryptohash_create(), which could lead to issues with resowner cleanup
if cascading resources are cleaned up on an error.  Like other
facilities using resowners, move the base allocation to TopMemoryContext
to ensure a correct cleanup on failure.

The resulting code gets simpler with this commit as the context data is
now hold by a unique opaque pointer, so as there is only one single
allocation done in TopMemoryContext.

After discussion, also change the cryptohash subroutines to return an
error if the caller provides NULL for the context data to ease error
detection on OOM.

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

4 years agoAdd idle_session_timeout.
Tom Lane [Wed, 6 Jan 2021 23:28:42 +0000 (18:28 -0500)]
Add idle_session_timeout.

This GUC variable works much like idle_in_transaction_session_timeout,
in that it kills sessions that have waited too long for a new client
query.  But it applies when we're not in a transaction, rather than
when we are.

Li Japin, reviewed by David Johnston and Hayato Kuroda, some
fixes by me

Discussion: https://postgr.es/m/763A0689-F189-459E-946F-F0EC4458980B@hotmail.com

4 years agoImprove timeout.c's handling of repeated timeout set/cancel.
Tom Lane [Wed, 6 Jan 2021 23:23:03 +0000 (18:23 -0500)]
Improve timeout.c's handling of repeated timeout set/cancel.

A very common usage pattern is that we set a timeout that we don't
expect to reach, cancel it after a little bit, and later repeat.
With the original implementation of timeout.c, this results in one
setitimer() call per timeout set or cancel.  We can do a lot better
by being lazy about changing the timeout interrupt request, namely:
(1) never cancel the outstanding interrupt, even when we have no
active timeout events;
(2) if we need to set an interrupt, but there already is one pending
at or before the required time, leave it alone.  When the interrupt
happens, the signal handler will reschedule it at whatever time is
then needed.

For example, with a one-second setting for statement_timeout, this
method results in having to interact with the kernel only a little
more than once a second, no matter how many statements we execute
in between.  The mainline code might never call setitimer() at all
after the first time, while each time the signal handler fires,
it sees that the then-pending request is most of a second away,
and that's when it sets the next interrupt request for.  Each
mainline timeout-set request after that will observe that the time
it wants is past the pending interrupt request time, and do nothing.

This also works pretty well for cases where a few different timeout
lengths are in use, as long as none of them are very short.  But
that describes our usage well.

Idea and original patch by Thomas Munro; I fixed a race condition
and improved the comments.

Discussion: https://postgr.es/m/CA+hUKG+o6pbuHBJSGnud=TadsuXySWA7CCcPgCt2QE9F6_4iHQ@mail.gmail.com

4 years agoReport progress of COPY commands
Tomas Vondra [Wed, 6 Jan 2021 20:46:26 +0000 (21:46 +0100)]
Report progress of COPY commands

This commit introduces a view pg_stat_progress_copy, reporting progress
of COPY commands.  This allows rough estimates how far a running COPY
progressed, with the caveat that the total number of bytes may not be
available in some cases (e.g. when the input comes from the client).

Author: Josef Å imánek
Reviewed-by: Fujii Masao, Bharath Rupireddy, Vignesh C, Matthias van de Meent
Discussion: https://postgr.es/m/CAFp7QwqMGEi4OyyaLEK9DR0+E+oK3UtA4bEjDVCa4bNkwUY2PQ@mail.gmail.com
Discussion: https://postgr.es/m/CAFp7Qwr6_FmRM6pCO0x_a0mymOfX_Gg+FEKet4XaTGSW=LitKQ@mail.gmail.com

4 years agoAdd a test module for the regular expression package.
Tom Lane [Wed, 6 Jan 2021 15:51:14 +0000 (10:51 -0500)]
Add a test module for the regular expression package.

This module provides a function test_regex() that is functionally
rather like regexp_matches(), but with additional debugging-oriented
options and additional output.  The debug options are somewhat obscure;
they are chosen to match the API of the test harness that Henry Spencer
wrote way-back-when for use in Tcl.  With this, we can import all the
test cases that Spencer wrote originally, even for regex functionality
that we don't currently expose in Postgres.  This seems necessary
because we can no longer rely on Tcl to act as upstream and verify
any fixes or improvements that we make.

In addition to Spencer's tests, I added a few for lookbehind
constraints (which we added in 2015, and Tcl still hasn't absorbed)
that are modeled on his tests for lookahead constraints.  After looking
at code coverage reports, I also threw in a couple of tests to more
fully exercise our "high colormap" logic.

According to my testing, this brings the check-world coverage
for src/backend/regex/ from 71.1% to 86.7% of lines.
(coverage.postgresql.org shows a slightly different number,
which I think is because it measures a non-assert build.)

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

4 years agoReplace CLOBBER_CACHE_ALWAYS with run-time GUC
Peter Eisentraut [Wed, 6 Jan 2021 09:15:19 +0000 (10:15 +0100)]
Replace CLOBBER_CACHE_ALWAYS with run-time GUC

Forced cache invalidation (CLOBBER_CACHE_ALWAYS) has been impractical
to use for testing in PostgreSQL because it's so slow and because it's
toggled on/off only at build time.  It is helpful when hunting bugs in
any code that uses the sycache/relcache because causes cache
invalidations to be injected whenever it would be possible for an
invalidation to occur, whether or not one was really pending.

Address this by providing run-time control over cache clobber
behaviour using the new debug_invalidate_system_caches_always GUC.
Support is not compiled in at all unless assertions are enabled or
CLOBBER_CACHE_ENABLED is explicitly defined at compile time.  It
defaults to 0 if compiled in, so it has negligible effect on assert
build performance by default.

When support is compiled in, test code can now set
debug_invalidate_system_caches_always=1 locally to a backend to test
specific queries, functions, extensions, etc.  Or tests can toggle it
globally for a specific test case while retaining normal performance
during test setup and teardown.

For backwards compatibility with existing test harnesses and scripts,
debug_invalidate_system_caches_always defaults to 1 if
CLOBBER_CACHE_ALWAYS is defined, and to 3 if CLOBBER_CACHE_RECURSIVE
is defined.

CLOBBER_CACHE_ENABLED is now visible in pg_config_manual.h, as is the
related RECOVER_RELATION_BUILD_MEMORY setting for the relcache.

Author: Craig Ringer <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/CAMsr+YF=+ctXBZj3ywmvKNUjWpxmuTuUKuv-rgbHGX5i5pLstQ@mail.gmail.com

4 years agoDetect the deadlocks between backends and the startup process.
Fujii Masao [Wed, 6 Jan 2021 03:39:18 +0000 (12:39 +0900)]
Detect the deadlocks between backends and the startup process.

The deadlocks that the recovery conflict on lock is involved in can
happen between hot-standby backends and the startup process.
If a backend takes an access exclusive lock on the table and which
finally triggers the deadlock, that deadlock can be detected
as expected. On the other hand, previously, if the startup process
took an access exclusive lock and which finally triggered the deadlock,
that deadlock could not be detected and could remain even after
deadlock_timeout passed. This is a bug.

The cause of this bug was that the code for handling the recovery
conflict on lock didn't take care of deadlock case at all. It assumed
that deadlocks involving the startup process and backends were able
to be detected by the deadlock detector invoked within backends.
But this assumption was incorrect. The startup process also should
have invoked the deadlock detector if necessary.

To fix this bug, this commit makes the startup process invoke
the deadlock detector if deadlock_timeout is reached while handling
the recovery conflict on lock. Specifically, in that case, the startup
process requests all the backends holding the conflicting locks to
check themselves for deadlocks.

Back-patch to v9.6. v9.5 has also this bug, but per discussion we decided
not to back-patch the fix to v9.5. Because v9.5 doesn't have some
infrastructure codes (e.g., 37c54863cf) that this bug fix patch depends on.
We can apply those codes for the back-patch, but since the next minor
version release is the final one for v9.5, it's risky to do that. If we
unexpectedly introduce new bug to v9.5 by the back-patch, there is no
chance to fix that. We determined that the back-patch to v9.5 would give
more risk than gain.

Author: Fujii Masao
Reviewed-by: Bertrand Drouvot, Masahiko Sawada, Kyotaro Horiguchi
Discussion: https://postgr.es/m/4041d6b6-cf24-a120-36fa-1294220f8243@oss.nttdata.com

4 years agoFix typos in decode.c and logical.c.
Amit Kapila [Wed, 6 Jan 2021 03:26:19 +0000 (08:56 +0530)]
Fix typos in decode.c and logical.c.

Per report by Ajin Cherian in email:
https://postgr.es/m/CAFPTHDYnRKDvzgDxoMn_CKqXA-D0MtrbyJvfvjBsO4G=UHDXkg@mail.gmail.com

4 years agodoc: Fix description about default behavior of recovery_target_timeline.
Fujii Masao [Wed, 6 Jan 2021 02:58:23 +0000 (11:58 +0900)]
doc: Fix description about default behavior of recovery_target_timeline.

The default value of recovery_target_timeline was changed in v12,
but the description about the default behavior of that was not updated.

Back-patch to v12 where the default behavior of recovery_target_timeline
was changed.

Author: Benoit Lobréau
Reviewed-by: Fujii Masao
Discussion: https://postgr.es/m/CAPE8EZ7c3aruEmM24GYkj8y8WmHKD1m9TtPtgCF0nQ3zw4LCkQ@mail.gmail.com

4 years agoPromote --data-checksums to the common set of options in initdb --help
Michael Paquier [Wed, 6 Jan 2021 01:52:26 +0000 (10:52 +0900)]
Promote --data-checksums to the common set of options in initdb --help

This was previously part of the section dedicated to less common
options, but it is an option commonly used these days.

Author: Michael Banck
Reviewed-by: Stephen Frost, Michael Paquier
Discussion: https://postgr.es/m/d7938aca4d4ea8e8c72c33bd75efe9f8218fe390[email protected]

4 years agoRevert unstable test cases from commit 7d80441d2.
Tom Lane [Wed, 6 Jan 2021 00:03:56 +0000 (19:03 -0500)]
Revert unstable test cases from commit 7d80441d2.

I momentarily forgot that the "owner" column wouldn't be stable
in the buildfarm.  Oh well, these tests weren't very valuable
anyway.

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

4 years agoAllow psql's \dt and \di to show TOAST tables and their indexes.
Tom Lane [Tue, 5 Jan 2021 23:41:50 +0000 (18:41 -0500)]
Allow psql's \dt and \di to show TOAST tables and their indexes.

Formerly, TOAST objects were unconditionally suppressed, but since
\d is able to print them it's not very clear why these variants
should not.  Instead, use the same rules as for system catalogs:
they can be seen if you write the 'S' modifier or a table name
pattern.  (In practice, since hardly anybody would keep pg_toast
in their search_path, it's really down to whether you use a pattern
that can match pg_toast.*.)

No docs change seems necessary because the docs already say that
this happens for "system objects"; we're just classifying TOAST
tables as being that.

Justin Pryzby, reviewed by Laurenz Albe

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

4 years agoIntroduce a new GUC_REPORT setting "in_hot_standby".
Tom Lane [Tue, 5 Jan 2021 21:18:01 +0000 (16:18 -0500)]
Introduce a new GUC_REPORT setting "in_hot_standby".

Aside from being queriable via SHOW, this value is sent to the client
immediately at session startup, and again later on if the server gets
promoted to primary during the session.  The immediate report will be
used in an upcoming patch to avoid an extra round trip when trying to
connect to a primary server.

Haribabu Kommi, Greg Nancarrow, Tom Lane; reviewed at various times
by Laurenz Albe, Takayuki Tsunakawa, Peter Smith.

Discussion: https://postgr.es/m/CAF3+xM+8-ztOkaV9gHiJ3wfgENTq97QcjXQt+rbFQ6F7oNzt9A@mail.gmail.com

4 years agodoc: improve NLS instruction wording
Bruce Momjian [Tue, 5 Jan 2021 19:26:37 +0000 (14:26 -0500)]
doc:  improve NLS instruction wording

Reported-by: "Tang, Haiying"
Discussion: https://postgr.es/m/bbbccf7a3c2d436e85d45869d612fd6b@G08CNEXMBPEKD05.g08.fujitsu.local

Author: "Tang, Haiying"

Backpatch-through: 9.5

4 years agoAdd an explicit cast to double when using fabs().
Dean Rasheed [Tue, 5 Jan 2021 11:52:42 +0000 (11:52 +0000)]
Add an explicit cast to double when using fabs().

Commit bc43b7c2c0 used fabs() directly on an int variable, which
apparently requires an explicit cast on some platforms.

Per buildfarm.

4 years agoFix numeric_power() when the exponent is INT_MIN.
Dean Rasheed [Tue, 5 Jan 2021 11:15:28 +0000 (11:15 +0000)]
Fix numeric_power() when the exponent is INT_MIN.

In power_var_int(), the computation of the number of significant
digits to use in the computation used log(Abs(exp)), which isn't safe
because Abs(exp) returns INT_MIN when exp is INT_MIN. Use fabs()
instead of Abs(), so that the exponent is cast to a double before the
absolute value is taken.

Back-patch to 9.6, where this was introduced (by 7d9a4737c2).

Discussion: https://postgr.es/m/CAEZATCVd6pMkz=BrZEgBKyqqJrt2xghr=fNc8+Z=5xC6cgWrWA@mail.gmail.com

4 years agoTest decoding of two-phase transactions during the build of a consistent snapshot.
Amit Kapila [Tue, 5 Jan 2021 09:35:22 +0000 (15:05 +0530)]
Test decoding of two-phase transactions during the build of a consistent snapshot.

Commit a271a1b50e added the capability to allow decoding at prepare time.
This adds an isolation testcase to test that decoding happens at commit
time when the consistent snapshot state is reached after prepare but
before commit prepared.

Author: Ajin Cherian
Reviewed-by: Amit Kapila
Discussion:
https://postgr.es/m/02DA5F5E-CECE-4D9C-8B4B-418077E2C010@postgrespro.ru
https://postgr.es/m/CAMGcDxeqEpWj3fTXwqhSwBdXd2RS9jzwWscO-XbeCfso6ts3+Q@mail.gmail.com

4 years agodoc: Document how to run regression tests with custom server settings
Peter Eisentraut [Tue, 5 Jan 2021 07:17:40 +0000 (08:17 +0100)]
doc: Document how to run regression tests with custom server settings

Author: Craig Ringer <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/CAMsr+YF=+ctXBZj3ywmvKNUjWpxmuTuUKuv-rgbHGX5i5pLstQ@mail.gmail.com

4 years agoStandardize one aspect of rmgr desc output.
Peter Geoghegan [Tue, 5 Jan 2021 03:46:11 +0000 (19:46 -0800)]
Standardize one aspect of rmgr desc output.

Bring heap and hash rmgr desc output in line with nbtree and GiST desc
output by using the name latestRemovedXid for all fields that output the
contents of the latestRemovedXid field from the WAL record's C struct
(stop using local variants).

This seems like a clear improvement because latestRemovedXid is a symbol
name that already appears across many different source files, and so is
probably much more recognizable.

Discussion: https://postgr.es/m/CAH2-Wzkt_Rs4VqPSCk87nyjPAAEmWL8STU9zgET_83EF5YfrLw@mail.gmail.com

4 years agoFix typo in origin.c.
Amit Kapila [Tue, 5 Jan 2021 02:35:08 +0000 (08:05 +0530)]
Fix typo in origin.c.

Author: Peter Smith
Discussion: https://postgr.es/m/CAHut+PsReyuvww_Fn1NN_Vsv0wBP1bnzuhzRFr_2=y1nNZrG7w@mail.gmail.com

4 years agoFix typo in reorderbuffer.c.
Amit Kapila [Tue, 5 Jan 2021 02:26:40 +0000 (07:56 +0530)]
Fix typo in reorderbuffer.c.

Author: Zhijie Hou
Reviewed-by: Sawada Masahiko
Discussion: https://postgr.es/m/ba88bb58aaf14284abca16aec04bf279@G08CNEXMBPEKD05.g08.fujitsu.local

4 years agoReplace remaining uses of "whitelist".
Thomas Munro [Tue, 5 Jan 2021 01:00:16 +0000 (14:00 +1300)]
Replace remaining uses of "whitelist".

Instead describe the action that the list effects, or just use "list"
where the meaning is obvious from context.

Author: Dagfinn Ilmari MannsÃ¥ker <[email protected]>
Discussion: https://postgr.es/m/20200615182235.x7lch5n6kcjq4aue%40alap3.anarazel.de

4 years agopgindent: whitelist/blacklist -> additional/excluded.
Thomas Munro [Tue, 5 Jan 2021 00:27:06 +0000 (13:27 +1300)]
pgindent: whitelist/blacklist -> additional/excluded.

Author: Dagfinn Ilmari MannsÃ¥ker <[email protected]>
Discussion: https://postgr.es/m/20200615182235.x7lch5n6kcjq4aue%40alap3.anarazel.de

4 years agoRename "enum blacklist" to "uncommitted enums".
Thomas Munro [Mon, 4 Jan 2021 23:06:15 +0000 (12:06 +1300)]
Rename "enum blacklist" to "uncommitted enums".

We agreed to remove this terminology and use something more descriptive.

Discussion: https://postgr.es/m/20200615182235.x7lch5n6kcjq4aue%40alap3.anarazel.de

4 years agoFix integer-overflow corner cases in substring() functions.
Tom Lane [Mon, 4 Jan 2021 23:32:40 +0000 (18:32 -0500)]
Fix integer-overflow corner cases in substring() functions.

If the substring start index and length overflow when added together,
substring() misbehaved, either throwing a bogus "negative substring
length" error on a case that should succeed, or failing to complain that
a negative length is negative (and instead returning the whole string,
in most cases).  Unsurprisingly, the text, bytea, and bit variants of
the function all had this issue.  Rearrange the logic to ensure that
negative lengths are always rejected, and add an overflow check to
handle the other case.

Also install similar guards into detoast_attr_slice() (nee
heap_tuple_untoast_attr_slice()), since it's far from clear that
no other code paths leading to that function could pass it values
that would overflow.

Patch by myself and Pavel Stehule, per bug #16804 from Rafi Shamim.

Back-patch to v11.  While these bugs are old, the common/int.h
infrastructure for overflow-detecting arithmetic didn't exist before
commit 4d6ad3125, and it doesn't seem like these misbehaviors are bad
enough to justify developing a standalone fix for the older branches.

Discussion: https://postgr.es/m/16804-f4eeeb6c11ba71d4@postgresql.org

4 years agoRemove unused function prototypes.
Thomas Munro [Mon, 4 Jan 2021 22:40:03 +0000 (11:40 +1300)]
Remove unused function prototypes.

Cleanup for commit dee663f7.

Reported-by: Tomas Vondra <[email protected]>
Discussion: https://postgr.es/m/CA+hUKGLJ=84YT+NvhkEEDAuUtVHMfQ9i-N7k_o50JmQ6Rpj_OQ@mail.gmail.com

4 years agoRethink the "read/write parameter" mechanism in pl/pgsql.
Tom Lane [Mon, 4 Jan 2021 17:39:27 +0000 (12:39 -0500)]
Rethink the "read/write parameter" mechanism in pl/pgsql.

Performance issues with the preceding patch to re-implement array
element assignment within pl/pgsql led me to realize that the read/write
parameter mechanism is misdesigned.  Instead of requiring the assignment
source expression to be such that *all* its references to the target
variable could be passed as R/W, we really want to identify *one*
reference to the target variable to be passed as R/W, allowing any other
ones to be passed read/only as they would be by default.  As long as the
R/W reference is a direct argument to the top-level (hence last to be
executed) function in the expression, there is no harm in R/O references
being passed to other lower parts of the expression.  Nor is there any
use-case for more than one argument of the top-level function being R/W.

Hence, rewrite that logic to identify one single Param that references
the target variable, and make only that Param pass a read/write
reference, not any other Params referencing the target variable.

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

4 years agoRemove PLPGSQL_DTYPE_ARRAYELEM datum type within pl/pgsql.
Tom Lane [Mon, 4 Jan 2021 17:14:37 +0000 (12:14 -0500)]
Remove PLPGSQL_DTYPE_ARRAYELEM datum type within pl/pgsql.

In the wake of the previous commit, we don't really need this anymore,
since array assignment is primarily handled by the core code.

The only way that that code could still be reached is that a GET
DIAGNOSTICS target variable could be an array element.  But that
doesn't seem like a particularly essential feature.  I'd added it
in commit 55caaaeba, but just because it was easy not because
anyone had actually asked for it.  Hence, revert that patch and
then remove the now-unreachable stuff.  (If we really had to,
we could probably reimplement GET DIAGNOSTICS using the new
assignment machinery; but the cost/benefit ratio looks very poor,
and it'd likely be a bit slower.)

Note that PLPGSQL_DTYPE_RECFIELD remains.  It's possible that we
could get rid of that too, but maintaining the existing behaviors
for RECORD-type variables seems like it might be difficult.  Since
there's not any functional limitation in those code paths as there
was in the ARRAYELEM code, I've not pursued the idea.

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

4 years agoRe-implement pl/pgsql's expression and assignment parsing.
Tom Lane [Mon, 4 Jan 2021 16:52:00 +0000 (11:52 -0500)]
Re-implement pl/pgsql's expression and assignment parsing.

Invent new RawParseModes that allow the core grammar to handle
pl/pgsql expressions and assignments directly, and thereby get rid
of a lot of hackery in pl/pgsql's parser.  This moves a good deal
of knowledge about pl/pgsql into the core code: notably, we have to
invent a CoercionContext that matches pl/pgsql's (rather dubious)
historical behavior for assignment coercions.  That's getting away
from the original idea of pl/pgsql as an arm's-length extension of
the core, but really we crossed that bridge a long time ago.

The main advantage of doing this is that we can now use the core
parser to generate FieldStore and/or SubscriptingRef nodes to handle
assignments to pl/pgsql variables that are records or arrays.  That
fixes a number of cases that had never been implemented in pl/pgsql
assignment, such as nested records and array slicing, and it allows
pl/pgsql assignment to support the datatype-specific subscripting
behaviors introduced in commit c7aba7c14.

There are cosmetic benefits too: when a syntax error occurs in a
pl/pgsql expression, the error report no longer includes the confusing
"SELECT" keyword that used to get prefixed to the expression text.
Also, there seem to be some small speed gains.

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

4 years agoAdd the ability for the core grammar to have more than one parse target.
Tom Lane [Mon, 4 Jan 2021 16:03:22 +0000 (11:03 -0500)]
Add the ability for the core grammar to have more than one parse target.

This patch essentially allows gram.y to implement a family of related
syntax trees, rather than necessarily always parsing a list of SQL
statements.  raw_parser() gains a new argument, enum RawParseMode,
to say what to do.  As proof of concept, add a mode that just parses
a TypeName without any other decoration, and use that to greatly
simplify typeStringToTypeName().

In addition, invent a new SPI entry point SPI_prepare_extended() to
allow SPI users (particularly plpgsql) to get at this new functionality.
In hopes of making this the last variant of SPI_prepare(), set up its
additional arguments as a struct rather than direct arguments, and
promise that future additions to the struct can default to zero.
SPI_prepare_cursor() and SPI_prepare_params() can perhaps go away at
some point.

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

4 years agoSimplify some comments in xml.c
Michael Paquier [Mon, 4 Jan 2021 10:47:58 +0000 (19:47 +0900)]
Simplify some comments in xml.c

Author: Justin Pryzby
Discussion: https://postgr.es/m/X/[email protected]

4 years agoAllow decoding at prepare time in ReorderBuffer.
Amit Kapila [Mon, 4 Jan 2021 03:04:50 +0000 (08:34 +0530)]
Allow decoding at prepare time in ReorderBuffer.

This patch allows PREPARE-time decoding of two-phase transactions (if the
output plugin supports this capability), in which case the transactions
are replayed at PREPARE and then committed later when COMMIT PREPARED
arrives.

Now that we decode the changes before the commit, the concurrent aborts
may cause failures when the output plugin consults catalogs (both system
and user-defined).

We detect such failures with a special sqlerrcode
ERRCODE_TRANSACTION_ROLLBACK introduced by commit 7259736a6e and stop
decoding the remaining changes. Then we rollback the changes when rollback
prepared is encountered.

Author: Ajin Cherian and Amit Kapila based on previous work by Nikhil Sontakke and Stas Kelvich
Reviewed-by: Amit Kapila, Peter Smith, Sawada Masahiko, Arseny Sher, and Dilip Kumar
Tested-by: Takamichi Osumi
Discussion:
https://postgr.es/m/02DA5F5E-CECE-4D9C-8B4B-418077E2C010@postgrespro.ru
https://postgr.es/m/CAMGcDxeqEpWj3fTXwqhSwBdXd2RS9jzwWscO-XbeCfso6ts3+Q@mail.gmail.com

4 years agoUpdate copyright for 2021
Bruce Momjian [Sat, 2 Jan 2021 18:06:25 +0000 (13:06 -0500)]
Update copyright for 2021

Backpatch-through: 9.5

4 years agoDoc: improve explanation of EXTRACT(EPOCH) for timestamp without tz.
Tom Lane [Fri, 1 Jan 2021 20:51:09 +0000 (15:51 -0500)]
Doc: improve explanation of EXTRACT(EPOCH) for timestamp without tz.

Try to be clearer about what computation is actually happening here.

Per bug #16797 from Dana Burd.

Discussion: https://postgr.es/m/16797-f264b0b980b53b8b@postgresql.org

4 years agoGet heap page max offset with buffer lock held.
Peter Geoghegan [Thu, 31 Dec 2020 01:21:42 +0000 (17:21 -0800)]
Get heap page max offset with buffer lock held.

On further reflection it seems better to call PageGetMaxOffsetNumber()
after acquiring a buffer lock on the page.  This shouldn't really
matter, but doing it this way is cleaner.

Follow-up to commit 42288174.

Backpatch: 12-, just like commit 42288174

4 years agoFix index deletion latestRemovedXid bug.
Peter Geoghegan [Thu, 31 Dec 2020 00:29:05 +0000 (16:29 -0800)]
Fix index deletion latestRemovedXid bug.

The logic for determining the latest removed XID for the purposes of
generating recovery conflicts in REDO routines was subtly broken.  It
failed to follow links from HOT chains, and so failed to consider all
relevant heap tuple headers in some cases.

To fix, expand the loop that deals with LP_REDIRECT line pointers to
also deal with HOT chains.  The new version of the loop is loosely based
on a similar loop from heap_prune_chain().

The impact of this bug is probably quite limited, since the horizon code
necessarily deals with heap tuples that are pointed to by LP_DEAD-set
index tuples.  The process of setting LP_DEAD index tuples (e.g. within
the kill_prior_tuple mechanism) is highly correlated with opportunistic
pruning of pointed-to heap tuples.  Plus the question of generating a
recovery conflict usually comes up some time after index tuple LP_DEAD
bits were initially set, unlike heap pruning, where a latestRemovedXid
is generated at the point of the pruning operation (heap pruning has no
deferred "would-be page split" style processing that produces conflicts
lazily).

Only backpatch to Postgres 12, the first version where this logic runs
during original execution (following commit 558a9165e08).  The index
latestRemovedXid mechanism has had the same bug since it first appeared
over 10 years ago (in commit a760893d), but backpatching to all
supported versions now seems like a bad idea on balance.  Running the
new improved code during recovery seems risky, especially given the lack
of complaints from the field.

Author: Peter Geoghegan <[email protected]>
Discussion: https://postgr.es/m/CAH2-Wz=Eib393+HHcERK_9MtgNS7Ew1HY=RDC_g6GL46zM5C6Q@mail.gmail.com
Backpatch: 12-

4 years agoDoc: spell out comparison behaviors for the date/time types.
Tom Lane [Wed, 30 Dec 2020 22:48:43 +0000 (17:48 -0500)]
Doc: spell out comparison behaviors for the date/time types.

The behavior of cross-type comparisons among date/time data types was
not really explained anywhere.  You could probably infer it if you
recognized the applicability of comments elsewhere about datatype
conversions, but it seems worthy of explicit documentation.

Per bug #16797 from Dana Burd.

Discussion: https://postgr.es/m/16797-f264b0b980b53b8b@postgresql.org

4 years agoMore fixups for pg_upgrade cross-version tests.
Tom Lane [Wed, 30 Dec 2020 19:15:41 +0000 (14:15 -0500)]
More fixups for pg_upgrade cross-version tests.

Commit 7ca37fb04 removed regress_putenv from the regress.so library,
so reloading a SQL function dependent on that would not work.
Fix similarly to 52202bb39.

Per buildfarm.

4 years agoRefactor multirange_in()
Alexander Korotkov [Wed, 30 Dec 2020 18:11:31 +0000 (21:11 +0300)]
Refactor multirange_in()

This commit preserves the logic of multirange_in() but makes it more clear
what's going on.  Also, this commit fixes the compiler warning spotted by the
buildfarm.

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

4 years agoUse setenv() in preference to putenv().
Tom Lane [Wed, 30 Dec 2020 17:55:59 +0000 (12:55 -0500)]
Use setenv() in preference to putenv().

Since at least 2001 we've used putenv() and avoided setenv(), on the
grounds that the latter was unportable and not in POSIX.  However,
POSIX added it that same year, and by now the situation has reversed:
setenv() is probably more portable than putenv(), since POSIX now
treats the latter as not being a core function.  And setenv() has
cleaner semantics too.  So, let's reverse that old policy.

This commit adds a simple src/port/ implementation of setenv() for
any stragglers (we have one in the buildfarm, but I'd not be surprised
if that code is never used in the field).  More importantly, extend
win32env.c to also support setenv().  Then, replace usages of putenv()
with setenv(), and get rid of some ad-hoc implementations of setenv()
wannabees.

Also, adjust our src/port/ implementation of unsetenv() to follow the
POSIX spec that it returns an error indicator, rather than returning
void as per the ancient BSD convention.  I don't feel a need to make
all the call sites check for errors, but the portability stub ought
to match real-world practice.

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

4 years agoFix selectivity estimation @> (anymultirange, anyrange) operator
Alexander Korotkov [Wed, 30 Dec 2020 17:19:15 +0000 (20:19 +0300)]
Fix selectivity estimation @> (anymultirange, anyrange) operator

Attempt to get selectivity estimation for @> (anymultirange, anyrange) operator
caused an error in buildfarm, because this operator was missed in switch()
of calc_hist_selectivity().  Fix that and also make regression tests reliably
check that selectivity estimation for (multi)ranges doesn't fall.  Previously,
whether we test selectivity estimation for (multi)ranges depended on
whether autovacuum managed to gather concurrently to the test.

Reported-by: Michael Paquier
Discussion: https://postgr.es/m/X%2BwmgjRItuvHNBeV%40paquier.xyz

4 years agoFix up usage of krb_server_keyfile GUC parameter.
Tom Lane [Wed, 30 Dec 2020 16:38:42 +0000 (11:38 -0500)]
Fix up usage of krb_server_keyfile GUC parameter.

secure_open_gssapi() installed the krb_server_keyfile setting as
KRB5_KTNAME unconditionally, so long as it's not empty.  However,
pg_GSS_recvauth() only installed it if KRB5_KTNAME wasn't set already,
leading to a troubling inconsistency: in theory, clients could see
different sets of server principal names depending on whether they
use GSSAPI encryption.  Always using krb_server_keyfile seems like
the right thing, so make both places do that.  Also fix up
secure_open_gssapi()'s lack of a check for setenv() failure ---
it's unlikely, surely, but security-critical actions are no place
to be sloppy.

Also improve the associated documentation.

This patch does nothing about secure_open_gssapi()'s use of setenv(),
and indeed causes pg_GSS_recvauth() to use it too.  That's nominally
against project portability rules, but since this code is only built
with --with-gssapi, I do not feel a need to do something about this
in the back branches.  A fix will be forthcoming for HEAD though.

Back-patch to v12 where GSSAPI encryption was introduced.  The
dubious behavior in pg_GSS_recvauth() goes back further, but it
didn't have anything to be inconsistent with, so let it be.

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