postgresql.git
5 years agoAdd prefix checks in exclude lists for pg_rewind, pg_checksums and base backups
Michael Paquier [Mon, 24 Feb 2020 09:13:25 +0000 (18:13 +0900)]
Add prefix checks in exclude lists for pg_rewind, pg_checksums and base backups

An instance of PostgreSQL crashing with a bad timing could leave behind
temporary pg_internal.init files, potentially causing failures when
verifying checksums.  As the same exclusion lists are used between
pg_rewind, pg_checksums and basebackup.c, all those tools are extended
with prefix checks to keep everything in sync, with dedicated checks
added for pg_internal.init.

Backpatch down to 11, where pg_checksums (pg_verify_checksums in 11) and
checksum verification for base backups have been introduced.

Reported-by: Michael Banck
Author: Michael Paquier
Reviewed-by: Kyotaro Horiguchi, David Steele
Discussion: https://postgr.es/m/62031974fd8e941dd8351fbc8c7eff60d59c5338.camel@credativ.de
Backpatch-through: 11

5 years agoFactor out InitControlFile() from BootStrapXLOG()
Peter Eisentraut [Mon, 17 Feb 2020 16:58:02 +0000 (17:58 +0100)]
Factor out InitControlFile() from BootStrapXLOG()

Right now this only makes BootStrapXLOG() a bit more manageable, but
in the future there may be external callers.

Discussion: https://www.postgresql.org/message-id/e8f86ba5-48f1-a80a-7f1d-b76bcb9c5c47@2ndquadrant.com

5 years agoReformat code comment
Peter Eisentraut [Mon, 17 Feb 2020 16:46:37 +0000 (17:46 +0100)]
Reformat code comment

Discussion: https://www.postgresql.org/message-id/e8f86ba5-48f1-a80a-7f1d-b76bcb9c5c47@2ndquadrant.com

5 years agopg_resetwal: Rename function to avoid potential conflict
Peter Eisentraut [Mon, 17 Feb 2020 16:35:48 +0000 (17:35 +0100)]
pg_resetwal: Rename function to avoid potential conflict

ReadControlFile() here conflicts with a function of the same name in
xlog.c.  There is no actual conflict right now, but since
pg_resetwal.c reaches deep inside backend headers, it's possible in
the future.

Discussion: https://www.postgresql.org/message-id/e8f86ba5-48f1-a80a-7f1d-b76bcb9c5c47@2ndquadrant.com

5 years agoAdjust Solution.pm to set HAVE_STDINT_H.
Tom Lane [Fri, 21 Feb 2020 21:14:09 +0000 (16:14 -0500)]
Adjust Solution.pm to set HAVE_STDINT_H.

We're not testing that anywhere anymore, but for consistency,
it should get defined.

5 years agoFix perlcritic warnings
Peter Eisentraut [Fri, 21 Feb 2020 21:03:05 +0000 (22:03 +0100)]
Fix perlcritic warnings

5 years agoAllow running src/tools/msvc/mkvcbuild.pl under not Windows
Peter Eisentraut [Fri, 21 Feb 2020 19:50:56 +0000 (20:50 +0100)]
Allow running src/tools/msvc/mkvcbuild.pl under not Windows

This to allow verifying the MSVC build file generation without having
to have Windows.

To do this, we avoid Windows-specific Perl modules and don't run the
"cl" compiler or "nmake".  The resulting build files won't actually be
completely correct, but it's useful enough.

Reviewed-by: Michael Paquier <michael@paquier.xyz>
Reviewed-by: Julien Rouhaud <rjuju123@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/d73b2c7b-f081-8357-8422-7564d55f1aac%402ndquadrant.com

5 years agoAssume that we have signed integral types and flexible array members.
Tom Lane [Fri, 21 Feb 2020 19:30:21 +0000 (14:30 -0500)]
Assume that we have signed integral types and flexible array members.

These compiler features are required by C99, so remove the configure
probes for them.

This is part of a series of commits to get rid of no-longer-relevant
configure checks and dead src/port/ code.  I'm committing them separately
to make it easier to back out individual changes if they prove less
portable than I expect.

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

5 years agoAssume that we have <wchar.h>.
Tom Lane [Fri, 21 Feb 2020 19:11:39 +0000 (14:11 -0500)]
Assume that we have <wchar.h>.

Windows has this, and so do all other live platforms according to the
buildfarm; it's been required by POSIX since SUSv2.  So remove the
configure probe and tests of HAVE_WCHAR_H.

This is part of a series of commits to get rid of no-longer-relevant
configure checks and dead src/port/ code.  I'm committing them separately
to make it easier to back out individual changes if they prove less
portable than I expect.

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

5 years agoAssume that we have utime() and <utime.h>.
Tom Lane [Fri, 21 Feb 2020 19:04:19 +0000 (14:04 -0500)]
Assume that we have utime() and <utime.h>.

These are required by POSIX since SUSv2, and no live platforms fail
to provide them.  On Windows, utime() exists and we bring our own
<utime.h>, so we're good there too.  So remove the configure probes
and ad-hoc substitute code.  We don't need to check for utimes()
anymore either, since that was only used as a substitute.

In passing, make the Windows build include <sys/utime.h> only where
we need it, not everywhere.

This is part of a series of commits to get rid of no-longer-relevant
configure checks and dead src/port/ code.  I'm committing them separately
to make it easier to back out individual changes if they prove less
portable than I expect.

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

5 years agoAssume that we have rint().
Tom Lane [Fri, 21 Feb 2020 18:47:40 +0000 (13:47 -0500)]
Assume that we have rint().

Windows has this since _MSC_VER >= 1200, and so do all other live
platforms according to the buildfarm, so remove the configure probe
and src/port/ substitution.

This is part of a series of commits to get rid of no-longer-relevant
configure checks and dead src/port/ code.  I'm committing them separately
to make it easier to back out individual changes if they prove less
portable than I expect.

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

5 years agoAssume that we have memmove().
Tom Lane [Fri, 21 Feb 2020 18:42:28 +0000 (13:42 -0500)]
Assume that we have memmove().

Windows has this, and so do all other live platforms according to the
buildfarm, so remove the configure probe and c.h's substitute code.

This is part of a series of commits to get rid of no-longer-relevant
configure checks and dead src/port/ code.  I'm committing them separately
to make it easier to back out individual changes if they prove less
portable than I expect.

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

5 years agoAssume that we have cbrt().
Tom Lane [Fri, 21 Feb 2020 18:24:21 +0000 (13:24 -0500)]
Assume that we have cbrt().

Windows has this, and so do all other live platforms according to the
buildfarm, so remove the configure probe and float.c's substitute code.

This is part of a series of commits to get rid of no-longer-relevant
configure checks and dead src/port/ code.  I'm committing them separately
to make it easier to back out individual changes if they prove less
portable than I expect.

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

5 years agoAssume that we have isinf().
Tom Lane [Fri, 21 Feb 2020 18:18:27 +0000 (13:18 -0500)]
Assume that we have isinf().

Windows has this, and so do all other live platforms according to the
buildfarm, so remove the configure probe and src/port/ substitution.

This also lets us get rid of some configure probes that existed only
to support src/port/isinf.c.  I kept the port.h hack to force using
__builtin_isinf() on clang, though.

This is part of a series of commits to get rid of no-longer-relevant
configure checks and dead src/port/ code.  I'm committing them separately
to make it easier to back out individual changes if they prove less
portable than I expect.

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

5 years agoAssume that we have functional, 64-bit fseeko()/ftello().
Tom Lane [Fri, 21 Feb 2020 17:49:42 +0000 (12:49 -0500)]
Assume that we have functional, 64-bit fseeko()/ftello().

Windows has this, and so do all other live platforms according to the
buildfarm, so remove the configure probe and src/port/ substitution.

Keep the probe that detects whether _LARGEFILE_SOURCE has to be
defined to get that, though ... that seems to be still relevant in
some places.

This is part of a series of commits to get rid of no-longer-relevant
configure checks and dead src/port/ code.  I'm committing them separately
to make it easier to back out individual changes if they prove less
portable than I expect.

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

5 years agoFix compiler warnings on 64-bit Windows
Peter Eisentraut [Fri, 21 Feb 2020 18:49:44 +0000 (19:49 +0100)]
Fix compiler warnings on 64-bit Windows

GCC reports various instances of

warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]

and MSVC equivalently

warning C4312: 'type cast': conversion from 'int' to 'void *' of greater size
warning C4311: 'type cast': pointer truncation from 'void *' to 'long'

in ECPG test files.  This is because void* and long are cast back and
forth, but on 64-bit Windows, these have different sizes.  Fix by
using intptr_t instead.

The code actually worked fine because the integer values in use are
all small.  So this is just to get the test code to compile warning-free.

This change is simplified by having made stdint.h required (commit
957338418b69e9774ccc1bab59f765a62f0aa6f9).  Before this it would have
been more complicated because the ecpg test source files don't use the
full pg_config.h.

Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://www.postgresql.org/message-id/flat/5d398bbb-262a-5fed-d839-d0e5cff3c0d7%402ndquadrant.com

5 years agoFixup for nodeAgg.c refactor.
Jeff Davis [Fri, 21 Feb 2020 01:04:19 +0000 (17:04 -0800)]
Fixup for nodeAgg.c refactor.

Commit 5b618e1f made an unintended behavior change.

5 years agoAvoid redundant checks in partition_bounds_copy().
Etsuro Fujita [Fri, 21 Feb 2020 11:00:45 +0000 (20:00 +0900)]
Avoid redundant checks in partition_bounds_copy().

Previously, partition_bounds_copy() checked whether the strategy for the
given partition bounds was hash or not, and then determined the number of
elements in the datums in the datums array for the partition bounds, on
each iteration of the loop for copying the datums array, but there is no
need to do that.  Perform the checks only once before the loop iteration.

Author: Etsuro Fujita
Reported-by: Amit Langote and Julien Rouhaud
Discussion: https://postgr.es/m/CAPmGK14Rvxrm8DHWvCjdoks6nwZuHBPvMnWZ6rkEx2KhFeEoPQ@mail.gmail.com

5 years agoRequire stdint.h
Peter Eisentraut [Fri, 21 Feb 2020 08:14:03 +0000 (09:14 +0100)]
Require stdint.h

stdint.h belongs to the compiler (as opposed to inttypes.h), so by
requiring a C99 compiler we can also require stdint.h
unconditionally.  Remove configure checks and other workarounds for
it.

This also removes a few steps in the required portability adjustments
to the imported time zone code, which can be applied on the next
import.

When using GCC on a platform that is otherwise pre-C99, this will now
require at least GCC 4.5, which is the first release that supplied a
standard-conforming stdint.h if the native platform didn't have it.

Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://www.postgresql.org/message-id/flat/5d398bbb-262a-5fed-d839-d0e5cff3c0d7%402ndquadrant.com

5 years agoDoc: Fix instructions to control build environment with MSVC
Michael Paquier [Fri, 21 Feb 2020 03:05:29 +0000 (12:05 +0900)]
Doc: Fix instructions to control build environment with MSVC

The documentation included some outdated instructions to change the
architecture, build type or target OS of a build done with MSVC.  This
commit updates the documentation to include the modern options
available, down to Visual Studio 2013.

Reported-by: Kyotaro Horiguchi
Author: Juan José Santamaría Flecha
Discussion: https://postgr.es/m/CAC+AXB0J7tAqW_2F1fCE4Dh2=Ccz96TcLpsGXOCvka7VvWG9Qw@mail.gmail.com
Backpatch-through: 12

5 years agoSimplify FK-to-partitioned regression test query
Alvaro Herrera [Thu, 20 Feb 2020 17:14:20 +0000 (14:14 -0300)]
Simplify FK-to-partitioned regression test query

Avoid a join between relations having the FK to detect FK violation.
The planner might optimize this considering the PK must exist on the
referenced side at some point, effectively masking a bug this test
tries to detect.

Tom Lane and Jehan-Guillaume de Rorthais
Discussion: https://postgr.es/m/467.1581270529@sss.pgh.pa.us

5 years agoRemove extra word from comment.
Etsuro Fujita [Thu, 20 Feb 2020 10:15:00 +0000 (19:15 +0900)]
Remove extra word from comment.

5 years agoCleanup more code related to ws2_32.dll loading in src/port/getaddrinfo.c
Michael Paquier [Thu, 20 Feb 2020 02:57:41 +0000 (11:57 +0900)]
Cleanup more code related to ws2_32.dll loading in src/port/getaddrinfo.c

e2e0219 has removed a code path for Windows 2000 that attempts to load
wship6.dll as fallback if ws2_32.dll is found but not getaddrinfo(),
leaving behind a dangling pointer as the library is freed.  However,
there is no point in this check as ws2_32.dll exists since Windows XP,
so just remove the duplicated check.

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

5 years agoDoc: discourage use of partial indexes for poor-man's-partitioning.
Tom Lane [Wed, 19 Feb 2020 23:52:18 +0000 (18:52 -0500)]
Doc: discourage use of partial indexes for poor-man's-partitioning.

Creating a bunch of non-overlapping partial indexes is generally
a bad idea, so add an example saying not to do that.

Back-patch to v10.  Before that, the alternative of using (real)
partitioning wasn't available, so that the tradeoff isn't quite
so clear cut.

Discussion: https://postgr.es/m/CAKVFrvFY-f7kgwMRMiPLbPYMmgjc8Y2jjUGK_Y0HVcYAmU6ymg@mail.gmail.com

5 years agoRemove support for upgrading extensions from "unpackaged" state.
Tom Lane [Wed, 19 Feb 2020 21:59:14 +0000 (16:59 -0500)]
Remove support for upgrading extensions from "unpackaged" state.

Andres Freund pointed out that allowing non-superusers to run
"CREATE EXTENSION ... FROM unpackaged" has security risks, since
the unpackaged-to-1.0 scripts don't try to verify that the existing
objects they're modifying are what they expect.  Just attaching such
objects to an extension doesn't seem too dangerous, but some of them
do more than that.

We could have resolved this, perhaps, by still requiring superuser
privilege to use the FROM option.  However, it's fair to ask just what
we're accomplishing by continuing to lug the unpackaged-to-1.0 scripts
forward.  None of them have received any real testing since 9.1 days,
so they may not even work anymore (even assuming that one could still
load the previous "loose" object definitions into a v13 database).
And an installation that's trying to go from pre-9.1 to v13 or later
in one jump is going to have worse compatibility problems than whether
there's a trivial way to convert their contrib modules into extension
style.

Hence, let's just drop both those scripts and the core-code support
for "CREATE EXTENSION ... FROM".

Discussion: https://postgr.es/m/20200213233015.r6rnubcvl4egdh5r@alap3.anarazel.de

5 years agoFix typo
Peter Eisentraut [Wed, 19 Feb 2020 19:52:42 +0000 (20:52 +0100)]
Fix typo

Reported-by: Daniel Verite <daniel@manitou-mail.org>
5 years agoFix confusion about event trigger vs. plain function in plpgsql.
Tom Lane [Wed, 19 Feb 2020 19:44:58 +0000 (14:44 -0500)]
Fix confusion about event trigger vs. plain function in plpgsql.

The function hash table keys made by compute_function_hashkey() failed
to distinguish event-trigger call context from regular call context.
This meant that once we'd successfully made a hash entry for an event
trigger (either by validation, or by normal use as an event trigger),
an attempt to call the trigger function as a plain function would
find this hash entry and thereby bypass the you-can't-do-that check in
do_compile().  Thus we'd attempt to execute the function, leading to
strange errors or even crashes, depending on function contents and
server version.

To fix, add an isEventTrigger field to PLpgSQL_func_hashkey,
paralleling the longstanding infrastructure for regular triggers.
This fits into what had been pad space, so there's no risk of an ABI
break, even assuming that any third-party code is looking at these
hash keys.  (I considered replacing isTrigger with a PLpgSQL_trigtype
enum field, but felt that that carried some API/ABI risk.  Maybe we
should change it in HEAD though.)

Per bug #16266 from Alexander Lakhin.  This has been broken since
event triggers were invented, so back-patch to all supported branches.

Discussion: https://postgr.es/m/16266-fcd7f838e97ba5d4@postgresql.org

5 years agoSet gen_random_uuid() to volatile
Peter Eisentraut [Wed, 19 Feb 2020 19:09:32 +0000 (20:09 +0100)]
Set gen_random_uuid() to volatile

It was set to immutable.  This was a mistake in the initial
commit (5925e5549890416bcf588334d9d0bc99f8ad6c7f).

Reported-by: hubert depesz lubaczewski <depesz@depesz.com>
Discussion: https://www.postgresql.org/message-id/flat/20200218185452.GA8710%40depesz.com

5 years agoMinor refactor of nodeAgg.c.
Jeff Davis [Wed, 19 Feb 2020 18:15:16 +0000 (10:15 -0800)]
Minor refactor of nodeAgg.c.

  * Separate calculation of hash value from the lookup.
  * Split build_hash_table() into two functions.
  * Change lookup_hash_entry() to return AggStatePerGroup. That's all
    the caller needed, anyway.

These changes are to support the upcoming Disk-based Hash Aggregation
work.

Discussion: https://postgr.es/m/31f5ab871a3ad5a1a91a7a797651f20e77ac7ce3.camel%40j-davis.com

5 years agologtape.c: allocate read buffer even for an empty tape.
Jeff Davis [Tue, 18 Feb 2020 20:31:24 +0000 (12:31 -0800)]
logtape.c: allocate read buffer even for an empty tape.

Prior to this commit, the read buffer was allocated at the time the tape
was rewound; but as an optimization, would not be allocated at all if
the tape was empty.

That optimization meant that it was valid to have a rewound tape with
the buffer set to NULL, but only if a number of conditions were met
and only if the API was used properly. After 7fdd919a refactored the
code to support lazily-allocating the buffer, Coverity started
complaining.

The optimization for empty tapes doesn't seem important, so just
allocate the buffer whether the tape has any data or not.

Discussion: https://postgr.es/m/20351.1581868306%40sss.pgh.pa.us

5 years agoFix mesurement of elapsed time during truncating heap in VACUUM.
Fujii Masao [Wed, 19 Feb 2020 11:37:26 +0000 (20:37 +0900)]
Fix mesurement of elapsed time during truncating heap in VACUUM.

VACUUM may truncate heap in several batches. The activity report
is logged for each batch, and contains the number of pages in the table
before and after the truncation, and also the elapsed time during
the truncation. Previously the elapsed time reported in each batch was
the total elapsed time since starting the truncation until finishing
each batch. For example, if the truncation was processed dividing into
three batches, the second batch reported the accumulated time elapsed
during both first and second batches. This is strange and confusing
because the number of pages in the table reported together is not
total. Instead, each batch should report the time elapsed during
only that batch.

The cause of this issue was that the resource usage snapshot was
initialized only at the beginning of the truncation and was never
reset later. This commit fixes the issue by changing VACUUM so that
the resource usage snapshot is reset at each batch.

Back-patch to all supported branches.

Reported-by: Tatsuhito Kasahara
Author: Tatsuhito Kasahara
Reviewed-by: Masahiko Sawada, Fujii Masao
Discussion: https://postgr.es/m/CAP0=ZVJsf=NvQuy+QXQZ7B=ZVLoDV_JzsVC1FRsF1G18i3zMGg@mail.gmail.com

5 years agoClean up some code, comments and docs referring to Windows 2000 and older
Michael Paquier [Wed, 19 Feb 2020 04:20:33 +0000 (13:20 +0900)]
Clean up some code, comments and docs referring to Windows 2000 and older

This fixes and updates a couple of comments related to outdated Windows
versions.  Particularly, src/common/exec.c had a fallback implementation
to read a file's line from a pipe because stdin/stdout/stderr does not
exist in Windows 2000 that is removed to simplify src/common/ as there
are unlikely versions of Postgres running on such platforms.

Author: Michael Paquier
Reviewed-by: Kyotaro Horiguchi, Juan José Santamaría Flecha
Discussion: https://postgr.es/m/20191219021526.GC4202@paquier.xyz

5 years agoStop demanding that top xact must be seen before subxact in decoding.
Amit Kapila [Wed, 12 Feb 2020 05:53:24 +0000 (11:23 +0530)]
Stop demanding that top xact must be seen before subxact in decoding.

Manifested as

ERROR:  subtransaction logged without previous top-level txn record

this check forbids legit behaviours like
 - First xl_xact_assignment record is beyond reading, i.e. earlier
   restart_lsn.
 - After restart_lsn there is some change of a subxact.
 - After that, there is second xl_xact_assignment (for another subxact)
   revealing the relationship between top and first subxact.

Such a transaction won't be streamed anyway because we hadn't seen it in
full.  Saying for sure whether xact of some record encountered after
the snapshot was deserialized can be streamed or not requires to know
whether it wrote something before deserialization point --if yes, it
hasn't been seen in full and can't be decoded. Snapshot doesn't have such
info, so there is no easy way to relax the check.

Reported-by: Hsu, John
Diagnosed-by: Arseny Sher
Author: Arseny Sher, Amit Kapila
Reviewed-by: Amit Kapila, Dilip Kumar
Backpatch-through: 9.5
Discussion: https://postgr.es/m/AB5978B2-1772-4FEE-A245-74C91704ECB0@amazon.com

5 years agoRemove obsolete _bt_compare() comment.
Peter Geoghegan [Wed, 19 Feb 2020 00:07:16 +0000 (16:07 -0800)]
Remove obsolete _bt_compare() comment.

btbuild() has nothing to say about how NULL values compare in nbtree.
Besides, there are _bt_compare() header comments that describe how NULL
values are handled.

5 years agoMake inherited LOCK TABLE perform access permission checks on parent table only.
Fujii Masao [Tue, 18 Feb 2020 04:13:15 +0000 (13:13 +0900)]
Make inherited LOCK TABLE perform access permission checks on parent table only.

Previously, LOCK TABLE command through a parent table checked
the permissions on not only the parent table but also the children
tables inherited from it. This was a bug and inherited queries should
perform access permission checks on the parent table only. This
commit fixes LOCK TABLE so that it does not check the permission
on children tables.

This patch is applied only in the master branch. We decided not to
back-patch because it's not hard to imagine that there are some
applications expecting the old behavior and the change breaks their
security.

Author: Amit Langote
Reviewed-by: Fujii Masao
Discussion: https://postgr.es/m/CAHGQGwE+GauyG7POtRfRwwthAGwTjPQYdFHR97+LzA4RHGnJxA@mail.gmail.com

5 years agoRemove duplicated words in comments
Michael Paquier [Tue, 18 Feb 2020 03:20:55 +0000 (12:20 +0900)]
Remove duplicated words in comments

Author: Daniel Gustafsson
Reviewed-by: Vik Fearing
Discussion: https://postgr.es/m/EBC3BFEB-664C-4063-81ED-29F1227DB012@yesql.se

5 years agoFix grammar in monitoring.sgml
Michael Paquier [Tue, 18 Feb 2020 01:49:44 +0000 (10:49 +0900)]
Fix grammar in monitoring.sgml

This is related to progress reporting for ANALYZE and partitioned
tables.

Author: Amit Langote
Reviewed-by: Daniel Gustafsson, Julien Rouhaud
Discussion: https://postgr.es/m/CA+HiwqGx6C=-bFTX=ryMThyvM7CcSC3b1x8_6zh4Uo41Kvu-zw@mail.gmail.com

5 years agoTeach pg_dump to dump comments on RLS policy objects.
Tom Lane [Mon, 17 Feb 2020 23:40:02 +0000 (18:40 -0500)]
Teach pg_dump to dump comments on RLS policy objects.

This was unaccountably omitted in the original RLS patch.
The SQL syntax is basically the same as for comments on triggers,
so crib code from dumpTrigger().

Per report from Marc Munro.  Back-patch to all supported branches.

Discussion: https://postgr.es/m/1581889298.18009.15.camel@bloodnok.com

5 years agoOptimize update of tables with generated columns
Peter Eisentraut [Mon, 17 Feb 2020 14:19:58 +0000 (15:19 +0100)]
Optimize update of tables with generated columns

When updating a table row with generated columns, only recompute those
generated columns whose base columns have changed in this update and
keep the rest unchanged.  This can result in a significant performance
benefit.  The required information was already kept in
RangeTblEntry.extraUpdatedCols; we just have to make use of it.

Reviewed-by: Pavel Stehule <pavel.stehule@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/b05e781a-fa16-6b52-6738-761181204567@2ndquadrant.com

5 years agoFill in extraUpdatedCols in logical replication
Peter Eisentraut [Mon, 17 Feb 2020 14:19:58 +0000 (15:19 +0100)]
Fill in extraUpdatedCols in logical replication

The extraUpdatedCols field of the target RTE records which generated
columns are affected by an update.  This is used in a variety of
places, including per-column triggers and foreign data wrappers.  When
an update was initiated by a logical replication subscription, this
field was not filled in, so such an update would not affect generated
columns in a way that is consistent with normal updates.  To fix,
factor out some code from analyze.c to fill in extraUpdatedCols in the
logical replication worker as well.

Reviewed-by: Pavel Stehule <pavel.stehule@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/b05e781a-fa16-6b52-6738-761181204567@2ndquadrant.com

5 years agoAdd description about GSSOpenServer wait event into document.
Fujii Masao [Mon, 17 Feb 2020 07:16:08 +0000 (16:16 +0900)]
Add description about GSSOpenServer wait event into document.

This commit also updates wait event enum into alphabetical order.
Previously the enum entry for GSSOpenServer was added out-of-order.

Back-patch to v12 where commit b0b39f72b9 introduced
GSSOpenServer wait event. In v12, the commit doesn't include
the update of wait event enum, not to break ABI.

Author: Fujii Masao
Reviewed-by: Michael Paquier
Discussion: https://postgr.es/m/949931aa-4ed4-d867-a7b5-de9c02b2292b@oss.nttdata.com

5 years agoAdd description about LogicalRewriteTruncate wait event into document.
Fujii Masao [Mon, 17 Feb 2020 06:33:32 +0000 (15:33 +0900)]
Add description about LogicalRewriteTruncate wait event into document.

Back-patch to v10 where commit 249cf070e3 introduced
LogicalRewriteTruncate wait event.

Author: Fujii Masao
Reviewed-by: Michael Paquier
Discussion: https://postgr.es/m/949931aa-4ed4-d867-a7b5-de9c02b2292b@oss.nttdata.com

5 years agoTry again to work around Windows' ERROR_SHARING_VIOLATION in pg_ctl.
Tom Lane [Sun, 16 Feb 2020 17:20:18 +0000 (12:20 -0500)]
Try again to work around Windows' ERROR_SHARING_VIOLATION in pg_ctl.

Commit 0da33c762 introduced an unfortunate regression in pg_ctl on
Windows: if the log file specified with -l doesn't exist yet, and
pg_ctl is running with Administrator privileges, then the log file
might get created with permissions that prevent the postmaster from
writing on it.  (It seems that whether this happens depends on whether
the log file is inside the user's home directory or not, and perhaps
on other phase-of-the-moon conditions, which may explain why we failed
to notice it sooner.)

To fix, just don't create the log file if it doesn't exist yet.  The
case where we need to wait obviously only occurs with a pre-existing
log file.

In passing, switch from using fopen() to plain open(), saving a few
cycles.

Per bug #16259 from Jonathan Katz and Heath Lord.  Back-patch to v12,
as the faulty commit was.

Alexander Lakhin

Discussion: https://postgr.es/m/16259-c5ebed32a262a8b1@postgresql.org

5 years agoUpdate obsolete comment.
Tom Lane [Sat, 15 Feb 2020 20:22:40 +0000 (15:22 -0500)]
Update obsolete comment.

Noted by Justin Pryzby, though I chose to just rip out the stale text,
as it's in no way relevant to this particular function.

Discussion: https://postgr.es/m/20200212182337.GZ1412@telsasoft.com

5 years agoClarify coding in Catalog::AddDefaultValues.
Tom Lane [Sat, 15 Feb 2020 20:13:44 +0000 (15:13 -0500)]
Clarify coding in Catalog::AddDefaultValues.

Make it a bit shorter and better-commented; no functional change.

Alvaro Herrera and Tom Lane

Discussion: https://postgr.es/m/20200212182337.GZ1412@telsasoft.com

5 years agoRun "make reformat-dat-files".
Tom Lane [Sat, 15 Feb 2020 19:58:30 +0000 (14:58 -0500)]
Run "make reformat-dat-files".

Mostly to make sure the previous commit didn't break this.

Discussion: https://postgr.es/m/20200212182337.GZ1412@telsasoft.com

5 years agoDon't require pg_class.dat to contain correct relnatts values.
Tom Lane [Sat, 15 Feb 2020 19:57:27 +0000 (14:57 -0500)]
Don't require pg_class.dat to contain correct relnatts values.

Practically everybody who's ever added a column to one of the bootstrap
catalogs has been burnt by the need to update the relnatts field in the
initial pg_class data to match.  Now that we use Perl scripts to
generate postgres.bki, we can have the machines take care of that,
by filling the field during genbki.pl.

While at it, use the BKI_DEFAULTS mechanism to eliminate repetitive
specifications of other column values in pg_class.dat, too.  They
weren't particularly a maintenance problem, but this way is prettier
(certainly the spotty previous usage of BKI_DEFAULTS wasn't pretty).

No catversion bump needed, since this doesn't actually change the
contents of postgres.bki.

Per gripe from Justin Pryzby, though this is quite different from
his originally proposed solution.

Amit Langote, John Naylor, Tom Lane

Discussion: https://postgr.es/m/20200212182337.GZ1412@telsasoft.com

5 years agoRecreate website's formatting for "website" doc builds.
Peter Geoghegan [Sat, 15 Feb 2020 02:38:35 +0000 (18:38 -0800)]
Recreate website's formatting for "website" doc builds.

The stylesheets used for the HTML documentation rendered on
postgresql.org have shifted, and no longer matched what was expected by
"make STYLE=website html" builds performed locally.  Local doc builds
did not reflect other aspects of the website, including font and
margins.

This patch updates the references to use the current set of stylesheets
that are used by the documentation on postgresql.org. This also wraps
the documentation preview in a HTML container so it can keep the content
within similar margins to those found on the website.

The documentation on building the docs is updated to reflect this
change, and to let the documentation builder know that an external
network connection is required to properly preview documentation built
with "make STYLE=website html" (which was true prior to this patch too,
but not mentioned).

Author: Jonathan Katz
Reported-By: Tom Lane
Discussion: https://postgr.es/m/1375.1581446233@sss.pgh.pa.us

5 years agoRemove pg_regress' --load-language option.
Tom Lane [Fri, 14 Feb 2020 16:20:07 +0000 (11:20 -0500)]
Remove pg_regress' --load-language option.

We haven't used this option since inventing extensions.  As of commit
50fc694e4 it's actually formally equivalent to --load-extension, so
let's just drop it.

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

5 years agoRemove some dead code in contrib/adminpack/
Michael Paquier [Fri, 14 Feb 2020 03:38:44 +0000 (12:38 +0900)]
Remove some dead code in contrib/adminpack/

Since its introduction in fe59e56, the code in charge of validating and
converting a file path includes some extra handling for absolute paths
pointing to an external log_directory, but this has never been used.

Author: Antonin Houska
Reviewed-by: Julien Rouhaud, Michael Paquier
Discussion: https://postgr.es/m/32663.1581592539@antos

5 years agoMark some contrib modules as "trusted".
Tom Lane [Thu, 13 Feb 2020 20:02:35 +0000 (15:02 -0500)]
Mark some contrib modules as "trusted".

This allows these modules to be installed into a database without
superuser privileges (assuming that the DBA or sysadmin has installed
the module's files in the expected place).  You only need CREATE
privilege on the current database, which by default would be
available to the database owner.

The following modules are marked trusted:

btree_gin
btree_gist
citext
cube
dict_int
earthdistance
fuzzystrmatch
hstore
hstore_plperl
intarray
isn
jsonb_plperl
lo
ltree
pg_trgm
pgcrypto
seg
tablefunc
tcn
tsm_system_rows
tsm_system_time
unaccent
uuid-ossp

In the future we might mark some more modules trusted, but there
seems to be no debate about these, and on the whole it seems wise
to be conservative with use of this feature to start out with.

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

5 years agoLogical Tape Set: lazily allocate read buffer.
Jeff Davis [Thu, 13 Feb 2020 17:43:51 +0000 (09:43 -0800)]
Logical Tape Set: lazily allocate read buffer.

The write buffer was already lazily-allocated, so this is more
symmetric. It also means that a freshly-rewound tape (whether for
reading or writing) is not consuming memory for the buffer.

Discussion: https://postgr.es/m/97c46a59c27f3c38e486ca170fcbc618d97ab049.camel%40j-davis.com

5 years agoAvoid a performance regression in float overflow/underflow detection.
Tom Lane [Thu, 13 Feb 2020 18:37:43 +0000 (13:37 -0500)]
Avoid a performance regression in float overflow/underflow detection.

Commit 6bf0bc842 replaced float.c's CHECKFLOATVAL() macro with static
inline subroutines, but that wasn't too well thought out.  In the original
coding, the unlikely condition (isinf(result) or result == 0) was checked
first, and the inf_is_valid or zero_is_valid condition only afterwards.
The inline-subroutine coding caused that to be swapped around, which is
pretty horrid for performance because (a) in common cases the is_valid
condition is twice as expensive to evaluate (e.g., requiring two isinf()
calls not one) and (b) in common cases the is_valid condition is false,
requiring us to perform the unlikely-condition check anyway.  Net result
is that one isinf() call becomes two or three, resulting in visible
performance loss as reported by Keisuke Kuroda.

The original fix proposal was to revert the replacement of the macro,
but on second thought, that macro was just a bad idea from the beginning:
if anything it's a net negative for readability of the code.  So instead,
let's just open-code all the overflow/underflow tests, being careful to
test the unlikely condition first (and mark it unlikely() to help the
compiler get the point).

Also, rather than having N copies of the actual ereport() calls, collapse
those into out-of-line error subroutines to save some code space.  This
does mean that the error file/line numbers won't be very helpful for
figuring out where the issue really is --- but we'd already burned that
bridge by putting the ereports into static inlines.

In HEAD, check_float[48]_val() are gone altogether.  In v12, leave them
present in float.h but unused in the core code, just in case some
extension is depending on them.

Emre Hasegeli, with some kibitzing from me and Andres Freund

Discussion: https://postgr.es/m/CANDwggLe1Gc1OrRqvPfGE=kM9K0FSfia0hbeFCEmwabhLz95AA@mail.gmail.com

5 years agoDoc: Restructure B-Tree support routine docs.
Peter Geoghegan [Wed, 12 Feb 2020 22:08:34 +0000 (14:08 -0800)]
Doc: Restructure B-Tree support routine docs.

Use a top-level "variablelist", with one item per B-Tree support
function.  This structure matches the structure used by various
"Extensibility" sections in other documentation chapters for other index
access methods.

An explicit list makes it much clearer where each item begins and ends.
This wasn't really a problem before now, but an upcoming patch that adds
deduplication to nbtree will need to have its own new B-Tree support
function.  Ease the burden of translators by tidying up btree.sgml ahead
of committing the deduplication patch.

5 years agoRemove long-dead comments.
Tom Lane [Wed, 12 Feb 2020 19:33:49 +0000 (14:33 -0500)]
Remove long-dead comments.

These should've been dropped by a8bb8eb58, but evidently were
missed.  Not important enough to back-patch.

5 years agoDoc: fix old oversights in GRANT/REVOKE documentation.
Tom Lane [Wed, 12 Feb 2020 19:13:13 +0000 (14:13 -0500)]
Doc: fix old oversights in GRANT/REVOKE documentation.

The GRANTED BY clause in GRANT/REVOKE ROLE has been there since 2005
but was never documented.  I'm not sure now whether that was just an
oversight or was intentional (given the limited capability of the
option).  But seeing that pg_dumpall does emit code that uses this
option, it seems like not documenting it at all is a bad idea.

Also, when we upgraded the syntax to allow CURRENT_USER/SESSION_USER
as the privilege recipient, the role form of GRANT was incorrectly
not modified to show that, and REVOKE's docs weren't touched at all.

Although I'm not that excited about GRANTED BY, the other oversight
seems serious enough to justify a back-patch.

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

5 years agoTry to harden insert-conflict-specconflict against autovacuum.
Andres Freund [Wed, 12 Feb 2020 05:14:08 +0000 (21:14 -0800)]
Try to harden insert-conflict-specconflict against autovacuum.

Looks like guaibasaurus had a autovacuum running during the
controller_print_speculative_locks step (just added in
43e08419708). Which does indeed seem quite possible.

Avoid the problem by only looking for the backends participating in
the test.

5 years agoAdd %x to default PROMPT1 and PROMPT2 in psql
Michael Paquier [Wed, 12 Feb 2020 04:31:14 +0000 (13:31 +0900)]
Add %x to default PROMPT1 and PROMPT2 in psql

%d can be used to track if the current connection is in a transaction
block or not, and adding it by default to the prompt has the advantage
to not need a modification of .psqlrc, something not possible depending
on the environment.

This discussion has happened across various sources, and there was a
strong consensus in favor of this change.

Author: Vik Fearing
Reviewed-by: Fabien Coelho
Discussion: https://postgr.es/m/09502c40-cfe1-bb29-10f9-4b3fa7b2bbb2@2ndquadrant.com

5 years agoTest additional speculative conflict scenarios.
Andres Freund [Wed, 12 Feb 2020 00:32:11 +0000 (16:32 -0800)]
Test additional speculative conflict scenarios.

Previously, the speculative insert tests did not cover the case when a
tuple t is inserted into a table with a unique index on a column but
before it can insert into the index, a concurrent transaction has
inserted a conflicting value into the index and the insertion of tuple t
must be aborted.

The basic permutation is one session successfully inserts into the table
and an associated unique index while a concurrent session successfully
inserts into the table but discovers a conflict before inserting into
the index and must abort the insertion.

Several variants on this include:
- swap which session is successful
- first session insert transaction does not commit, so second session
must wait on a transaction lock
- first session insert does not "complete", so second session must wait
on a speculative insertion lock

Also, refactor the existing TOAST table upsert test to be in the same
spec and reuse the steps.

Author: Melanie Plageman, Ashwin Agrawal, Andres Freund
Reviewed-by: Andres Freund, Taylor Vesely
Discussion: https://postgr.es/m/CAAKRu_ZRmxy_OEryfY3G8Zp01ouhgw59_-_Cm8n7LzRH5BAvng@mail.gmail.com

5 years agoFix bug in pg_basebackup -F plain -R.
Fujii Masao [Wed, 12 Feb 2020 00:08:22 +0000 (09:08 +0900)]
Fix bug in pg_basebackup -F plain -R.

Commit caba97a9d9 changed pg_basebackup -F plain -R so that
it overwrote postgresql.auto.conf in the backup, with new connection
setting. This could cause the existing postgresql.auto.conf settings
in the server to get lost unexpectedly. This is a bug.

This commit fixes the bug by making pg_basebackup -F plain -R
append the connection setting into postgresql.auto.conf in the backup.

Author: Fujii Masao
Reviewed-by: Sergei Kornilov
Discussion: https://postgr.es/m/250dcf2a-94e7-c05e-824a-73cfb38a48a4@oss.nttdata.com

5 years agoDocument the pg_upgrade -j/--jobs option as taking an argument
Peter Eisentraut [Tue, 11 Feb 2020 22:47:36 +0000 (23:47 +0100)]
Document the pg_upgrade -j/--jobs option as taking an argument

5 years agoUse pg_pwrite() in more places.
Thomas Munro [Tue, 11 Feb 2020 04:22:37 +0000 (17:22 +1300)]
Use pg_pwrite() in more places.

This removes some lseek() system calls.

Author: Thomas Munro
Reviewed-by: Andres Freund
Discussion: https://postgr.es/m/CA%2BhUKGJ%2BoHhnvqjn3%3DHro7xu-YDR8FPr0FL6LF35kHRX%3D_bUzg%40mail.gmail.com

5 years agoCanonicalize some URLs
Peter Eisentraut [Mon, 10 Feb 2020 19:47:50 +0000 (20:47 +0100)]
Canonicalize some URLs

5 years agopsql: Remove one use of HAVE_UNIX_SOCKETS
Peter Eisentraut [Mon, 10 Feb 2020 18:27:05 +0000 (19:27 +0100)]
psql: Remove one use of HAVE_UNIX_SOCKETS

This use was not protecting any unportable code, it was just omitting
the code because it wouldn't be used.  Remove the use to reduce code
complexity a bit.

Discussion: https://www.postgresql.org/message-id/flat/54bde68c-d134-4eb8-5bd3-8af33b72a010@2ndquadrant.com

5 years agoSimplify passing of configure arguments to pg_config
Peter Eisentraut [Mon, 10 Feb 2020 16:12:46 +0000 (17:12 +0100)]
Simplify passing of configure arguments to pg_config

The previous system had configure put the value into the makefiles and
then have the makefiles pass them to the build of pg_config.  That was
put in place when pg_config was a shell script.  We can simplify that
by having configure put the value into pg_config.h directly.  This
also makes the standard build system match how the MSVC build system
already does it.

Discussion: https://www.postgresql.org/message-id/flat/6e457870-cef5-5f1d-b57c-fc89cfb8a788%402ndquadrant.com

5 years agoChange signature of TupleHashTableHash().
Jeff Davis [Mon, 10 Feb 2020 18:20:10 +0000 (10:20 -0800)]
Change signature of TupleHashTableHash().

Commit 4eaea3db introduced TupleHashTableHash(), but the signature
didn't match the other exposed functions. Separate it into internal
and external versions. The external version hides the details behind
an API more consistent with the other external functions, and the
internal version is still suitable for simplehash.

5 years agocreateuser: fix parsing of --connection-limit argument
Alvaro Herrera [Mon, 10 Feb 2020 15:14:58 +0000 (12:14 -0300)]
createuser: fix parsing of --connection-limit argument

The original coding failed to quote the argument properly.

Reported-by: Daniel Gustafsson
Discussion: 1B8AE66C-85AB-4728-9BB4-612E8E61C219@yesql.se

5 years agoFix priv checks for ALTER <object> DEPENDS ON EXTENSION
Alvaro Herrera [Mon, 10 Feb 2020 14:47:09 +0000 (11:47 -0300)]
Fix priv checks for ALTER <object> DEPENDS ON EXTENSION

Marking an object as dependant on an extension did not have any
privilege check whatsoever; this allowed any user to mark objects as
droppable by anyone able to DROP EXTENSION, which could be used to cause
system-wide havoc.  Disallow by checking that the calling user owns the
mentioned object.

(No constraints are placed on the extension.)

Security: CVE-2020-1720
Reported-by: Tom Lane
Discussion: 31605.1566429043@sss.pgh.pa.us

5 years agoRevert "pg_upgrade: Fix quoting of some arguments in pg_ctl command"
Michael Paquier [Mon, 10 Feb 2020 06:48:21 +0000 (15:48 +0900)]
Revert "pg_upgrade: Fix quoting of some arguments in pg_ctl command"

This reverts commit d1c0b61.  The patch has some downsides that require
more attention, as discussed with Noah Misch.

Backpatch-through: 9.5

5 years agoFix typos.
Amit Kapila [Mon, 10 Feb 2020 04:01:18 +0000 (09:31 +0530)]
Fix typos.

Reported-by: Justin Pryzby
Author: Justin Pryzby
Discussion: https://postgr.es/m/20200206021432.GA24549@telsasoft.com

5 years agodoc: Spell checking
Amit Kapila [Mon, 10 Feb 2020 03:04:43 +0000 (08:34 +0530)]
doc: Spell checking

Reported-by: Justin Pryzby
Author: Justin Pryzby
Backpatch-through: 9.6
Discussion: https://postgr.es/m/20200206021432.GA24549@telsasoft.com

5 years agopg_upgrade: Fix quoting of some arguments in pg_ctl command
Michael Paquier [Mon, 10 Feb 2020 01:48:43 +0000 (10:48 +0900)]
pg_upgrade: Fix quoting of some arguments in pg_ctl command

The previous coding forgot to apply shell quoting to the socket
directory and the data folder, leading to failures when running
pg_upgrade.  This refactors the code generating the pg_ctl command
starting clusters to use a more correct shell quoting.  Failures are
easier to trigger in 12 and newer versions by using a value of
--socketdir that includes quotes, but it is also possible to cause
failures with quotes included in the default socket directory used by
pg_upgrade or the data folders of the clusters involved in the
upgrade.

As 9.4 is going to be EOL'd with the next minor release, nobody is
likely going to upgrade to it now so this branch is not included in the
set of branches fixed.

Author: Michael Paquier
Reviewed-by: Álvaro Herrera, Noah Misch
Backpatch-through: 9.5

5 years agopsql: Fix %w length in PROMPT2 when PROMPT1 contains a newline.
Thomas Munro [Mon, 10 Feb 2020 00:20:00 +0000 (13:20 +1300)]
psql: Fix %w length in PROMPT2 when PROMPT1 contains a newline.

The width of the invisible PROMPT2 must take into account, in order
for user input to be aligned with the first line, that PROMPT1 can
contain newlines.

Author: Maxence Ahlouche
Reviewed-by: Tom Lane
Discussion: https://postgr.es/m/CAJeaomVyLSP_Wj%3D0FtYNTuoopWHyFarhUtYKDHs0HHv%2Bb%3DN9sA%40mail.gmail.com

5 years agoRevert "docs: change "default role" wording to "predefined role""
Tom Lane [Sun, 9 Feb 2020 19:19:46 +0000 (14:19 -0500)]
Revert "docs: change "default role" wording to "predefined role""

This reverts commit 0e936a2148472e6c364aee8c3e298dc16dc4240a.

Per discussion, we can't change the section title without some
web-site work, so revert this change temporarily.

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

5 years agoStore the deletion horizon XID for a deleted GIN page on the right page.
Tom Lane [Sun, 9 Feb 2020 17:02:57 +0000 (12:02 -0500)]
Store the deletion horizon XID for a deleted GIN page on the right page.

Commit b10714080 moved the GinPageSetDeleteXid() call to a spot where
the "page" variable was pointing to the wrong page, causing the XID
to be inserted on a page that's not being deleted, thus allowing later
GinPageIsRecyclable tests to recycle the deleted page too soon.

It might be a good idea to stop using the single "page" variable for
multiple purposes in this function.  But for the moment I just moved
the GinPageSetDeleteXid() call down beside the GinPageSetDeleted()
call, which seems like a more logical place for it anyway.

Back-patch to v11, as the faulty patch was.  (Fortunately, the bug
hasn't made it into any release yet.)

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

5 years agoFix failure to create FKs correctly in partitions
Alvaro Herrera [Fri, 7 Feb 2020 21:27:18 +0000 (18:27 -0300)]
Fix failure to create FKs correctly in partitions

On a multi-level partioned table, when adding a partition not directly
connected to the root table, foreign key constraints referencing the
root were not cloned to the new partition, leading to the FK being
possibly inadvertently violated later on.

This was caused by fuzzy thinking in CloneFkReferenced (commit
f56f8f8da6af): it was skipping constraints marked as having parents on
the theory that cloning those would create duplicates; but that's only
correct for the top level of the partitioning hierarchy.  For levels
below that one, such constraints must still be considered and only
skipped if later on we see that we'd create duplicates.  Apparently, I
(Álvaro) wrote the comments right but the code implemented something
slightly different.

Author: Jehan-Guillaume de Rorthais
Discussion: https://postgr.es/m/20200206004948.238352db@firost

5 years agoFix TRUNCATE .. CASCADE on partitions
Alvaro Herrera [Fri, 7 Feb 2020 20:09:36 +0000 (17:09 -0300)]
Fix TRUNCATE .. CASCADE on partitions

When running TRUNCATE CASCADE on a child of a partitioned table
referenced by another partitioned table, the truncate was not applied to
partitions of the referencing table; this could leave rows violating the
constraint in the referencing partitioned table.  Repair by walking the
pg_constraint chain all the way up to the topmost referencing table.

Note: any partitioned tables containing FKs that reference other
partitioned tables should be checked for possible violating rows, if
TRUNCATE has occurred in partitions of the referenced table.

Reported-by: Christophe Courtois
Author: Jehan-Guillaume de Rorthais
Discussion: https://postgr.es/m/20200204183906.115f693e@firost

5 years agoFix bug in Tid scan.
Fujii Masao [Fri, 7 Feb 2020 13:00:21 +0000 (22:00 +0900)]
Fix bug in Tid scan.

Commit 147e3722f7 changed Tid scan so that it calls table_beginscan()
and uses the scan option for seq scan. This change caused two issues.

(1) The change caused Tid scan to take a predicate lock on the entire
       relation in serializable transaction even when relation-level
       lock is not necessary. This could lead to an unexpected
       serialization error.

(2) The change caused Tid scan to increment the number of seq_scan
       in pg_stat_*_tables views even though it's not seq scan. This
       could confuse the users.

This commit adds the scan option for Tid scan and makes Tid scan
use it, to avoid those issues.

Back-patch to v12, where the bug was introduced.

Author: Tatsuhito Kasahara
Reviewed-by: Kyotaro Horiguchi, Masahiko Sawada, Fujii Masao
Discussion: https://postgr.es/m/CAP0=ZVKy+gTbFmB6X_UW0pP3WaeJ-fkUWHoD-pExS=at3CY76g@mail.gmail.com

5 years agojit: Reference expression step functions via llvmjit_types.
Andres Freund [Fri, 7 Feb 2020 06:13:52 +0000 (22:13 -0800)]
jit: Reference expression step functions via llvmjit_types.

The main benefit of doing so is that this allows llvm to ensure that
types match - previously that'd only be detected by a crash within the
called function. There were a number of cases where we passed a
superfluous parameter...

To avoid needing to add all the functions to llvmjit.{c,h}, instead
get them from the llvm module for llvmjit_types.c. Also use that for
the functions from llvmjit_types already in llvmjit.h.

Author: Soumyadeep Chakraborty and Andres Freund
Discussion: https://postgr.es/m/CADwEdooww3wZv-sXSfatzFRwMuwa186LyTwkBfwEW6NjtooBPA@mail.gmail.com

5 years agoBump catalog version for the addition of leader_pid in pg_stat_activity
Michael Paquier [Fri, 7 Feb 2020 06:08:17 +0000 (15:08 +0900)]
Bump catalog version for the addition of leader_pid in pg_stat_activity

Oversight in commit b025f32.

Per private report from Julien Rouhaud.

5 years agoIntroduce TupleHashTableHash() and LookupTupleHashEntryHash().
Jeff Davis [Fri, 7 Feb 2020 03:39:47 +0000 (19:39 -0800)]
Introduce TupleHashTableHash() and LookupTupleHashEntryHash().

Expose two new entry points: one for only calculating the hash value
of a tuple, and another for looking up a hash entry when the hash
value is already known. This will be useful for disk-based Hash
Aggregation to avoid recomputing the hash value for the same tuple
after saving and restoring it from disk.

Discussion: https://postgr.es/m/37091115219dd522fd9ed67333ee8ed1b7e09443.camel%40j-davis.com

5 years agojit: Remove redundancies in expression evaluation code generation.
Andres Freund [Fri, 7 Feb 2020 03:45:45 +0000 (19:45 -0800)]
jit: Remove redundancies in expression evaluation code generation.

This merges the code emission for a number of opcodes by handling the
behavioural difference more locally. This reduces code, and also
improves the generated code a bit in some cases, by removing redundant
constants.

Author: Andres Freund
Discussion: https://postgr.es/m/20191023163849.sosqbfs5yenocez3@alap3.anarazel.de

5 years agojit: Reference functions by name in IOCOERCE steps.
Andres Freund [Fri, 7 Feb 2020 03:09:06 +0000 (19:09 -0800)]
jit: Reference functions by name in IOCOERCE steps.

Previously we used constant function pointer addresses, which prevents
inlining and other related optimizations.

Author: Andres Freund
Discussion: https://postgr.es/m/20191023163849.sosqbfs5yenocez3@alap3.anarazel.de

5 years agoexpression eval: Don't redundantly keep track of AggState.
Andres Freund [Fri, 7 Feb 2020 03:06:16 +0000 (19:06 -0800)]
expression eval: Don't redundantly keep track of AggState.

It's already tracked via ExprState->parent, so we don't need to also
include it in ExprEvalStep. When that code originally was written
ExprState->parent didn't exist, but it since has been introduced in
6719b238e8f.

Author: Andres Freund
Discussion: https://postgr.es/m/20191023163849.sosqbfs5yenocez3@alap3.anarazel.de

5 years agoexpression eval, jit: Minor code cleanups.
Andres Freund [Fri, 7 Feb 2020 03:04:50 +0000 (19:04 -0800)]
expression eval, jit: Minor code cleanups.

This mostly consists of using C99 style for loops, moving variables
into narrower scopes, and a smattering of other minor improvements.
Done separately to make it easier to review patches with actual
functional changes.

Author: Andres Freund
Discussion: https://postgr.es/m/20191023163849.sosqbfs5yenocez3@alap3.anarazel.de

5 years agoFix typo in proc.c
Michael Paquier [Fri, 7 Feb 2020 03:41:10 +0000 (12:41 +0900)]
Fix typo in proc.c

Author: Julien Rouhaud
Discussion: https://postgr.es/m/20200206082333.GA95343@nol

5 years agoRevert "Prevent running pg_basebackup as root"
Michael Paquier [Fri, 7 Feb 2020 01:51:17 +0000 (10:51 +0900)]
Revert "Prevent running pg_basebackup as root"

This reverts commit 7bae0ad, as this is not ideal with the tar format,
and we may want to explore more options like what is done by tar with
some equivalents of --owner and --group, but for pg_basebackup.

Per complaints from Magnus Hagander and Stephen Frost.

Discussion: https://postgr.es/m/20200205172259.GW3195@tamriel.snowman.net

5 years agoRevert "Add GUC checks for ssl_min_protocol_version and ssl_max_protocol_version"
Michael Paquier [Thu, 6 Feb 2020 23:10:40 +0000 (08:10 +0900)]
Revert "Add GUC checks for ssl_min_protocol_version and ssl_max_protocol_version"

This reverts commit 41aadee, as the GUC checks could run on older values
with the new values used, and result in incorrect errors if both
parameters are changed at the same time.

Per complaint from Tom Lane.

Discussion: https://postgr.es/m/27574.1581015893@sss.pgh.pa.us
Backpatch-through: 12

5 years agoEnsure relcache consistency around generated columns
Peter Eisentraut [Thu, 6 Feb 2020 20:21:04 +0000 (21:21 +0100)]
Ensure relcache consistency around generated columns

In certain transient states, it's possible that a table has attributes
with attgenerated set but no default expressions in pg_attrdef yet.
In that case, the old code path would not set
relation->rd_att->constr->has_generated_stored, unless
relation->rd_att->constr was also populated for some other reason.
There was probably no practical impact, but it's better to keep this
consistent.

Reported-by: Andres Freund <andres@anarazel.de>
Discussion: https://www.postgresql.org/message-id/flat/20200115181105.ad6ab6dlgyww3lb6%40alap3.anarazel.de

5 years agoRefactor hash_agg_entry_size().
Jeff Davis [Thu, 6 Feb 2020 19:49:56 +0000 (11:49 -0800)]
Refactor hash_agg_entry_size().

Consolidate the calculations for hash table size estimation. This will
help with upcoming Hash Aggregation work that will add additional call
sites.

5 years agoLogical Tape Set: use min heap for freelist.
Jeff Davis [Thu, 6 Feb 2020 18:06:25 +0000 (10:06 -0800)]
Logical Tape Set: use min heap for freelist.

Previously, the freelist of blocks was tracked as an
occasionally-sorted array. A min heap is more resilient to larger
freelists or more frequent changes between reading and writing.

Discussion: https://postgr.es/m/97c46a59c27f3c38e486ca170fcbc618d97ab049.camel%40j-davis.com

5 years agoFix typo.
Amit Kapila [Thu, 6 Feb 2020 10:27:02 +0000 (15:57 +0530)]
Fix typo.

Reported-by: Amit Langote
Author: Amit Langote
Backpatch-through: 9.6, where it was introduced
Discussion: https://postgr.es/m/CA+HiwqFNADeukaaGRmTqANbed9Fd81gLi08AWe_F86_942Gspw@mail.gmail.com

5 years agoFix bug in LWLock statistics mechanism.
Fujii Masao [Thu, 6 Feb 2020 05:43:21 +0000 (14:43 +0900)]
Fix bug in LWLock statistics mechanism.

Previously PostgreSQL built with -DLWLOCK_STATS could report
more than one LWLock statistics entries for the same backend
process and the same LWLock. This is strange and only one
statistics should be output in that case, instead.

The cause of this issue is that the key variable used for
LWLock stats hash table was not fully initialized. The key
consists of two fields and they were initialized. But
the following 4 bytes allocated in the key variable for
the alignment was not initialized. So even if the same key
was specified, hash_search(HASH_ENTER) could not find
the existing entry for that key and created new one.

This commit fixes this issue by initializing the key
variable with zero. As the side effect of this commit,
the volume of LWLock statistics output would be reduced
very much.

Back-patch to v10, where commit 3761fe3c20 introduced the issue.

Author: Fujii Masao
Reviewed-by: Julien Rouhaud, Kyotaro Horiguchi
Discussion: https://postgr.es/m/26359edb-798a-568f-d93a-6aafac49752d@oss.nttdata.com

5 years agoAdd leader_pid to pg_stat_activity
Michael Paquier [Thu, 6 Feb 2020 00:18:06 +0000 (09:18 +0900)]
Add leader_pid to pg_stat_activity

This new field tracks the PID of the group leader used with parallel
query.  For parallel workers and the leader, the value is set to the
PID of the group leader.  So, for the group leader, the value is the
same as its own PID.  Note that this reflects what PGPROC stores in
shared memory, so as leader_pid is NULL if a backend has never been
involved in parallel query.  If the backend is using parallel query or
has used it at least once, the value is set until the backend exits.

Author: Julien Rouhaud
Reviewed-by: Sergei Kornilov, Guillaume Lelarge, Michael Paquier, Tomas
Vondra
Discussion: https://postgr.es/m/CAOBaU_Yy5bt0vTPZ2_LUM6cUcGeqmYNoJ8-Rgto+c2+w3defYA@mail.gmail.com

5 years agoForce tuple conversion when the source has missing attributes.
Andrew Gierth [Wed, 5 Feb 2020 19:49:47 +0000 (19:49 +0000)]
Force tuple conversion when the source has missing attributes.

Tuple conversion incorrectly concluded that no conversion was needed
as long as all the attributes lined up. But if the source tuple has a
missing attribute (from addition of a column with default), then the
destination tupdesc might not reflect the same default. The typical
symptom was that the affected columns would be unexpectedly NULL.

Repair by always forcing conversion if the source has missing
attributes, which will be filled in by the deform operation. (In
theory we could optimize for when the destination has the same
default, but that seemed overkill.)

Backpatch to 11 where missing attributes were added.

Per bug #16242.

Vik Fearing (discovery, code, testing) and me (analysis, testcase).

Discussion: https://postgr.es/m/16242-d1c9fca28445966b@postgresql.org

5 years agoMake vacuum buffer counters 64 bits wide
Alvaro Herrera [Wed, 5 Feb 2020 19:59:29 +0000 (16:59 -0300)]
Make vacuum buffer counters 64 bits wide

Using 32 bit counters means they can now realistically wrap around when
vacuuming extremely large tables.  Because they're signed integers,
stats printed by vacuum look very odd when they do.

We'd love to backpatch this, but refrain because the variables are
exported and could cause third-party code to break.

Reviewed-by: Julien Rouhaud, Tom Lane, Michael Paquier
Discussion: https://postgr.es/m/20200131205926.GA16367@alvherre.pgsql

5 years agoALTER SUBSCRIPTION / REFRESH docs: explain copy_data
Alvaro Herrera [Wed, 5 Feb 2020 16:21:43 +0000 (13:21 -0300)]
ALTER SUBSCRIPTION / REFRESH docs: explain copy_data

The docs are ambiguous as to which tables would be copied over when the
copy_data parameter is true in ALTER SUBSCRIPTION ... REFRESH PUBLICATION.
Make it clear that it only applies to tables which are new in the
publication.

Author: David Christensen (reword by Álvaro Herrera)
Discussion: https://postgr.es/m/95339420-7F09-4F8C-ACC0-8F1CFAAD9CD7@endpoint.com

5 years agoWhen a TAP file has non-zero exit status, retain temporary directories.
Noah Misch [Wed, 5 Feb 2020 16:26:41 +0000 (08:26 -0800)]
When a TAP file has non-zero exit status, retain temporary directories.

PostgresNode already retained base directories in such cases.  Stop
using $SIG{__DIE__}, which is redundant with the exit status check, in
lieu of proliferating it to TestLib.  Back-patch to 9.6, where commit
88802e068017bee8cea7a5502a712794e761c7b5 introduced retention on
failure.

Reviewed by Daniel Gustafsson.

Discussion: https://postgr.es/m/20200202170155.GA3264196@rfd.leadboat.com

5 years agoAdd kqueue(2) support to the WaitEventSet API.
Thomas Munro [Wed, 5 Feb 2020 04:35:57 +0000 (17:35 +1300)]
Add kqueue(2) support to the WaitEventSet API.

Use kevent(2) to wait for events on the BSD family of operating
systems and macOS.  This is similar to the epoll(2) support added
for Linux by commit 98a64d0bd.

Author: Thomas Munro
Reviewed-by: Andres Freund, Marko Tiikkaja, Tom Lane
Tested-by: Mateusz Guzik, Matteo Beccati, Keith Fiske, Heikki Linnakangas, Michael Paquier, Peter Eisentraut, Rui DeSousa, Tom Lane, Mark Wong
Discussion: https://postgr.es/m/CAEepm%3D37oF84-iXDTQ9MrGjENwVGds%2B5zTr38ca73kWR7ez_tA%40mail.gmail.com

5 years agoHandle lack of DSM slots in parallel btree build, take 2.
Thomas Munro [Tue, 4 Feb 2020 23:21:03 +0000 (12:21 +1300)]
Handle lack of DSM slots in parallel btree build, take 2.

Commit 74618e77 added a new check intended to fix a bug, but put
it in the wrong place so that parallel btree build was always
disabled.  Do the check after we've actually tried to create
a DSM segment.  Back-patch to 11, like the earlier commit.

Reviewed-by: Peter Geoghegan
Discussion: https://postgr.es/m/CAH2-WzmDABkJzrNnvf%2BOULK-_A_j9gkYg_Dz-H62jzNv4eKQTw%40mail.gmail.com