users/gsingh/postgres.git
5 years agoUpdate header comments for wchar.c and encnames.c.
Tom Lane [Thu, 16 Jan 2020 20:58:24 +0000 (15:58 -0500)]
Update header comments for wchar.c and encnames.c.

Bring these into common style (including having proper copyright
notices) and adjust their self-declaration of where they live.

Discussion: https://postgr.es/m/CA+TgmoYO8oq-iy8E02rD8eX25T-9SmyxKWqqks5OMHxKvGXpXQ@mail.gmail.com

5 years agoMove wchar.c and encnames.c to src/common/.
Tom Lane [Thu, 16 Jan 2020 20:56:32 +0000 (15:56 -0500)]
Move wchar.c and encnames.c to src/common/.

Formerly, various frontend directories symlinked these two sources
and then built them locally.  That's an ancient, ugly hack, and
we now have a much better way: put them into libpgcommon.
So do that.  (The immediate motivation for this is the prospect
of having to introduce still more symlinking if we don't.)

This commit moves these two files absolutely verbatim, for ease of
reviewing the git history.  There's some follow-on work to be done
that will modify them a bit.

Robert Haas, Tom Lane

Discussion: https://postgr.es/m/CA+TgmoYO8oq-iy8E02rD8eX25T-9SmyxKWqqks5OMHxKvGXpXQ@mail.gmail.com

5 years agoFix problems with "read only query" checks, and refactor the code.
Robert Haas [Thu, 16 Jan 2020 17:11:31 +0000 (12:11 -0500)]
Fix problems with "read only query" checks, and refactor the code.

Previously, check_xact_readonly() was responsible for determining
which types of queries could not be run in a read-only transaction,
standard_ProcessUtility() was responsibility for prohibiting things
which were allowed in read only transactions but not in recovery, and
utility commands were basically prohibited in bulk in parallel mode by
calls to CommandIsReadOnly() in functions.c and spi.c.  This situation
was confusing and error-prone. Accordingly, move all the checks to a
new function ClassifyUtilityCommandAsReadOnly(), which determines the
degree to which a given statement is read only.

In the old code, check_xact_readonly() inadvertently failed to handle
several statement types that actually should have been prohibited,
specifically T_CreatePolicyStmt, T_AlterPolicyStmt, T_CreateAmStmt,
T_CreateStatsStmt, T_AlterStatsStmt, and T_AlterCollationStmt.  As a
result, thes statements were erroneously allowed in read only
transactions, parallel queries, and standby operation. Generally, they
would fail anyway due to some lower-level error check, but we
shouldn't rely on that.  In the new code structure, future omissions
of this type should cause ClassifyUtilityCommandAsReadOnly() to
complain about an unrecognized node type.

As a fringe benefit, this means we can allow certain types of utility
commands in parallel mode, where it's safe to do so. This allows
ALTER SYSTEM, CALL, DO, CHECKPOINT, COPY FROM, EXPLAIN, and SHOW.
It might be possible to allow additional commands with more work
and thought.

Along the way, document the thinking process behind the current set
of checks, as per discussion especially with Peter Eisentraut. There
is some interest in revising some of these rules, but that seems
like a job for another patch.

Patch by me, reviewed by Tom Lane, Stephen Frost, and Peter
Eisentraut.

Discussion: http://postgr.es/m/CA+TgmoZ_rLqJt5sYkvh+JpQnfX0Y+B2R+qfi820xNih6x-FQOQ@mail.gmail.com

5 years agoMinor code beautification in regexp.c.
Tom Lane [Thu, 16 Jan 2020 16:31:30 +0000 (11:31 -0500)]
Minor code beautification in regexp.c.

Remove duplicated code (apparently introduced by commit c8ea87e4b).
Also get rid of some PG_USED_FOR_ASSERTS_ONLY variables we don't
really need to have.

Li Japin, Tom Lane

Discussion: https://postgr.es/m/PS1PR0601MB3770A5595B6E5E3FD6F35724B6360@PS1PR0601MB3770.apcprd06.prod.outlook.com

5 years agoRestructure ALTER TABLE execution to fix assorted bugs.
Tom Lane [Wed, 15 Jan 2020 23:49:24 +0000 (18:49 -0500)]
Restructure ALTER TABLE execution to fix assorted bugs.

We've had numerous bug reports about how (1) IF NOT EXISTS clauses in
ALTER TABLE don't behave as-expected, and (2) combining certain actions
into one ALTER TABLE doesn't work, though executing the same actions as
separate statements does.  This patch cleans up all of the cases so far
reported from the field, though there are still some oddities associated
with identity columns.

The core problem behind all of these bugs is that we do parse analysis
of ALTER TABLE subcommands too soon, before starting execution of the
statement.  The root of the bugs in group (1) is that parse analysis
schedules derived commands (such as a CREATE SEQUENCE for a serial
column) before it's known whether the IF NOT EXISTS clause should cause
a subcommand to be skipped.  The root of the bugs in group (2) is that
earlier subcommands may change the catalog state that later subcommands
need to be parsed against.

Hence, postpone parse analysis of ALTER TABLE's subcommands, and do
that one subcommand at a time, during "phase 2" of ALTER TABLE which
is the phase that does catalog rewrites.  Thus the catalog effects
of earlier subcommands are already visible when we analyze later ones.
(The sole exception is that we do parse analysis for ALTER COLUMN TYPE
subcommands during phase 1, so that their USING expressions can be
parsed against the table's original state, which is what we need.
Arguably, these bugs stem from falsely concluding that because ALTER
COLUMN TYPE must do early parse analysis, every other command subtype
can too.)

This means that ALTER TABLE itself must deal with execution of any
non-ALTER-TABLE derived statements that are generated by parse analysis.
Add a suitable entry point to utility.c to accept those recursive
calls, and create a struct to pass through the information needed by
the recursive call, rather than making the argument lists of
AlterTable() and friends even longer.

Getting this to work correctly required a little bit of fiddling
with the subcommand pass structure, in particular breaking up
AT_PASS_ADD_CONSTR into multiple passes.  But otherwise it's mostly
a pretty straightforward application of the above ideas.

Fixing the residual issues for identity columns requires refactoring of
where the dependency link from an identity column to its sequence gets
set up.  So that seems like suitable material for a separate patch,
especially since this one is pretty big already.

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

5 years agoReport progress of ANALYZE commands
Alvaro Herrera [Wed, 15 Jan 2020 14:02:09 +0000 (11:02 -0300)]
Report progress of ANALYZE commands

This uses the progress reporting infrastructure added by c16dc1aca5e0,
adding support for ANALYZE.

Co-authored-by: Álvaro Herrera <alvherre@alvh.no-ip.org>
Co-authored-by: Tatsuro Yamada <tatsuro.yamada.tf@nttcom.co.jp>
Reviewed-by: Julien Rouhaud, Robert Haas, Anthony Nowocien, Kyotaro Horiguchi,
Vignesh C, Amit Langote

5 years agoRemove libpq.rc, use win32ver.rc for libpq
Peter Eisentraut [Wed, 15 Jan 2020 09:15:06 +0000 (10:15 +0100)]
Remove libpq.rc, use win32ver.rc for libpq

For historical reasons, libpq used a separate libpq.rc file for the
Windows builds while all other components use a common file
win32ver.rc.  With a bit of tweaking, the libpq build can also use the
win32ver.rc file.  This removes a bit of duplicative code.

Reviewed-by: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Discussion: https://www.postgresql.org/message-id/flat/ad505e61-a923-e114-9f38-9867d161073f@2ndquadrant.com

5 years agoFix buggy logic in isTempNamespaceInUse()
Michael Paquier [Wed, 15 Jan 2020 04:58:33 +0000 (13:58 +0900)]
Fix buggy logic in isTempNamespaceInUse()

The logic introduced in this routine as of 246a6c8 would report an
incorrect result when a session calls it to check if the temporary
namespace owned by the session is in use or not.  It is possible to
optimize more the routine in this case to avoid a PGPROC lookup, but
let's keep the logic simple.  As this routine is used only by autovacuum
for now, there were no live bugs, still let's be correct for any future
code involving it.

Author: Michael Paquier
Reviewed-by: Julien Rouhaud
Discussion: https://postgr.es/m/20200113093703.GA41902@paquier.xyz
Backpatch-through: 11

5 years agoIntroduce IndexAM fields for parallel vacuum.
Amit Kapila [Wed, 15 Jan 2020 01:54:14 +0000 (07:24 +0530)]
Introduce IndexAM fields for parallel vacuum.

Introduce new fields amusemaintenanceworkmem and amparallelvacuumoptions
in IndexAmRoutine for parallel vacuum.  The amusemaintenanceworkmem tells
whether a particular IndexAM uses maintenance_work_mem or not.  This will
help in controlling the memory used by individual workers as otherwise,
each worker can consume memory equal to maintenance_work_mem.  The
amparallelvacuumoptions tell whether a particular IndexAM participates in
a parallel vacuum and if so in which phase (bulkdelete, vacuumcleanup) of
vacuum.

Author: Masahiko Sawada and Amit Kapila
Reviewed-by: Dilip Kumar, Amit Kapila, Tomas Vondra and Robert Haas
Discussion:
https://postgr.es/m/CAD21AoDTPMgzSkV4E3SFo1CH_x50bf5PqZFQf4jmqjk-C03BWg@mail.gmail.com
https://postgr.es/m/CAA4eK1LmcD5aPogzwim5Nn58Ki+74a6Edghx4Wd8hAskvHaq5A@mail.gmail.com

5 years agoFix compiler warning about format on Windows
Peter Eisentraut [Tue, 14 Jan 2020 22:59:18 +0000 (23:59 +0100)]
Fix compiler warning about format on Windows

On 64-bit Windows, pid_t is long long int, so a %d format isn't
enough.

5 years agodocs: change "default role" wording to "predefined role"
Bruce Momjian [Tue, 14 Jan 2020 18:13:04 +0000 (13:13 -0500)]
docs: change "default role" wording to "predefined role"

The new wording was determined to be more accurate.  Also, update
release note links that reference these sections.

Reported-by: rirans@comcast.net
Discussion: https://postgr.es/m/157742545062.1149.11052653770497832538@wrigleys.postgresql.org

Backpatch-through: 9.6

5 years agoRevert copyright script changes to binary *.key files
Bruce Momjian [Tue, 14 Jan 2020 16:28:07 +0000 (11:28 -0500)]
Revert copyright script changes to binary *.key files

This reverts part of commit 7559d8ebfa.  The copyright script has
already been updated to skip *.key files.

Reported-by: Alvaro Herrera
Discussion: https://postgr.es/m/20200102184059.GA25435@alvherre.pgsql

Backpatch-through: master

5 years agotools/copyright.pl: skip copyright changes for *.key files
Bruce Momjian [Tue, 14 Jan 2020 15:51:58 +0000 (10:51 -0500)]
tools/copyright.pl:  skip copyright changes for *.key files

Reported-by: Alvaro Herrera
Discussion: https://postgr.es/m/20200102184059.GA25435@alvherre.pgsql

Backpatch-through: master

5 years agowalreceiver uses a temporary replication slot by default
Peter Eisentraut [Tue, 14 Jan 2020 13:07:11 +0000 (14:07 +0100)]
walreceiver uses a temporary replication slot by default

If no permanent replication slot is configured using
primary_slot_name, the walreceiver now creates and uses a temporary
replication slot.  A new setting wal_receiver_create_temp_slot can be
used to disable this behavior, for example, if the remote instance is
out of replication slots.

Reviewed-by: Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
Discussion: https://www.postgresql.org/message-id/CA%2Bfd4k4dM0iEPLxyVyme2RAFsn8SUgrNtBJOu81YqTY4V%2BnqZA%40mail.gmail.com

5 years agoExpose PQbackendPID() through walreceiver API
Peter Eisentraut [Tue, 14 Jan 2020 13:05:25 +0000 (14:05 +0100)]
Expose PQbackendPID() through walreceiver API

This will be used by a subsequent patch.

Reviewed-by: Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
Discussion: https://www.postgresql.org/message-id/CA%2Bfd4k4dM0iEPLxyVyme2RAFsn8SUgrNtBJOu81YqTY4V%2BnqZA%40mail.gmail.com

5 years agoALTER TABLE ... ALTER COLUMN ... DROP EXPRESSION
Peter Eisentraut [Tue, 14 Jan 2020 12:09:31 +0000 (13:09 +0100)]
ALTER TABLE ... ALTER COLUMN ... DROP EXPRESSION

Add an ALTER TABLE subcommand for dropping the generated property from
a column, per SQL standard.

Reviewed-by: Sergei Kornilov <sk@zsrv.org>
Discussion: https://www.postgresql.org/message-id/flat/2f7f1d9c-946e-0453-d841-4f38eb9d69b6%402ndquadrant.com

5 years agoMake rewriter prevent auto-updates on views with conditional INSTEAD rules.
Dean Rasheed [Tue, 14 Jan 2020 09:52:21 +0000 (09:52 +0000)]
Make rewriter prevent auto-updates on views with conditional INSTEAD rules.

A view with conditional INSTEAD rules and no unconditional INSTEAD
rules or INSTEAD OF triggers is not auto-updatable. Previously we
relied on a check in the executor to catch this, but that's
problematic since the planner may fail to properly handle such a query
and thus return a particularly unhelpful error to the user, before
reaching the executor check.

Instead, trap this in the rewriter and report the correct error there.
Doing so also allows us to include more useful error detail than the
executor check can provide. This doesn't change the existing behaviour
of updatable views; it merely ensures that useful error messages are
reported when a view isn't updatable.

Per report from Pengzhou Tang, though not adopting that suggested fix.
Back-patch to all supported branches.

Discussion: https://postgr.es/m/CAG4reAQn+4xB6xHJqWdtE0ve_WqJkdyCV4P=trYr4Kn8_3_PEA@mail.gmail.com

5 years agoRevert test added by commit d207038053.
Amit Kapila [Sat, 11 Jan 2020 04:54:48 +0000 (10:24 +0530)]
Revert test added by commit d207038053.

This test was trying to test the mechanism to release kernel FDs as needed
to get us under the max_safe_fds limit in case of spill files.  To do that,
it needs to set max_files_per_process to a very low value which doesn't
even permit starting of the server in the case when there are a few already
opened files.  This test also won't work on platforms where we use one FD
per semaphore.

Backpatch-through: 10, till where this test was added
Discussion:
https://postgr.es/m/CAA4eK1LHhERi06Q+MmP9qBXBBboi+7WV3910J0aUgz71LcnKAw@mail.gmail.com
https://postgr.es/m/6485.1578583522@sss.pgh.pa.us

5 years agoReduce size of backend scanner's tables.
Tom Lane [Mon, 13 Jan 2020 20:04:31 +0000 (15:04 -0500)]
Reduce size of backend scanner's tables.

Previously, the core scanner's yy_transition[] array had 37045 elements.
Since that number is larger than INT16_MAX, Flex generated the array to
contain 32-bit integers.  By reimplementing some of the bulkier scanner
rules, this patch reduces the array to 20495 elements.  The much smaller
total length, combined with the consequent use of 16-bit integers for
the array elements reduces the binary size by over 200kB.  This was
accomplished in two ways:

1. Consolidate handling of quote continuations into a new start condition,
rather than duplicating that logic for five different string types.

2. Treat Unicode strings and identifiers followed by a UESCAPE sequence
as three separate tokens, rather than one.  The logic to de-escape
Unicode strings is moved to the filter code in parser.c, which already
had the ability to provide special processing for token sequences.
While we could have implemented the conversion in the grammar, that
approach was rejected for performance and maintainability reasons.

Performance in microbenchmarks of raw parsing seems equal or slightly
faster in most cases, and it's reasonable to expect that in real-world
usage (with more competition for the CPU cache) there will be a larger
win.  The exception is UESCAPE sequences; lexing those is about 10%
slower, primarily because the scanner now has to be called three times
rather than one.  This seems acceptable since that feature is very
rarely used.

The psql and epcg lexers are likewise modified, primarily because we
want to keep them all in sync.  Since those lexers don't use the
space-hogging -CF option, the space savings is much less, but it's
still good for perhaps 10kB apiece.

While at it, merge the ecpg lexer's handling of C-style comments used
in SQL and in C.  Those have different rules regarding nested comments,
but since we already have the ability to keep track of the previous
start condition, we can use that to handle both cases within a single
start condition.  This matches the core scanner more closely.

John Naylor

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

5 years agoFix base backup with database OIDs larger than INT32_MAX
Peter Eisentraut [Mon, 13 Jan 2020 12:27:39 +0000 (13:27 +0100)]
Fix base backup with database OIDs larger than INT32_MAX

The use of pg_atoi() for parsing a string into an Oid fails for values
larger than INT32_MAX, since OIDs are unsigned.  Instead, use
atooid().  While this has less error checking, the contents of the
data directory are expected to be trustworthy, so we don't need to go
out of our way to do full error checking.

Discussion: https://www.postgresql.org/message-id/flat/dea47fc8-6c89-a2b1-07e3-754ff1ab094b%402ndquadrant.com

5 years agoFix typo.
Amit Kapila [Mon, 13 Jan 2020 09:14:55 +0000 (14:44 +0530)]
Fix typo.

Reported-by: Antonin Houska
Author: Antonin Houska
Backpatch-through: 11, where it was introduced
Discussion: https://postgr.es/m/2246.1578900133@antos

5 years agoFix comment in heapam.c
Michael Paquier [Mon, 13 Jan 2020 08:57:38 +0000 (17:57 +0900)]
Fix comment in heapam.c

Improvement per suggestion from Tom Lane.

Author: Daniel Gustafsson
Discussion: https://postgr.es/m/FED18699-4270-4778-8DA8-10F119A5ECF3@yesql.se

5 years agoOnly superuser can set sslcert/sslkey in postgres_fdw user mappings
Andrew Dunstan [Mon, 13 Jan 2020 07:38:09 +0000 (18:08 +1030)]
Only superuser can set sslcert/sslkey in postgres_fdw user mappings

Othrwise there is a security risk.

Discussion: https://postgr.es/m/20200109103014.GA4192@msg.df7cb.de

5 years agoDelete empty pages in each pass during GIST VACUUM.
Amit Kapila [Mon, 13 Jan 2020 02:29:44 +0000 (07:59 +0530)]
Delete empty pages in each pass during GIST VACUUM.

Earlier, we use to postpone deleting empty pages till the second stage of
vacuum to amortize the cost of scanning internal pages.  However, that can
sometimes (say vacuum is canceled or errored between first and second
stage) delay the pages to be recycled.

Another thing is that to facilitate deleting empty pages in the second
stage, we need to share the information about internal and empty pages
between different stages of vacuum.  It will be quite tricky to share this
information via DSM which is required for the upcoming parallel vacuum
patch.

Also, it will bring the logic to reclaim deleted pages closer to nbtree
where we delete empty pages in each pass.

Overall, the advantages of deleting empty pages in each pass outweigh the
advantages of postponing the same.

Author: Dilip Kumar, with changes by Amit Kapila
Reviewed-by: Sawada Masahiko and Amit Kapila
Discussion: https://postgr.es/m/CAA4eK1LGr+MN0xHZpJ2dfS8QNQ1a_aROKowZB+MPNep8FVtwAA@mail.gmail.com

5 years agoApply multiple multivariate MCV lists when possible
Tomas Vondra [Mon, 13 Jan 2020 00:20:57 +0000 (01:20 +0100)]
Apply multiple multivariate MCV lists when possible

Until now we've only used a single multivariate MCV list per relation,
covering the largest number of clauses. So for example given a query

    SELECT * FROM t WHERE a = 1 AND b =1 AND c = 1 AND d = 1

and extended statistics on (a,b) and (c,d), we'd only pick and use one
of them. This commit improves this by repeatedly picking and applying
the best statistics (matching the largest number of remaining clauses)
until no additional statistics is applicable.

This greedy algorithm is simple, but may not be optimal. A different
choice of statistics may leave fewer clauses unestimated and/or give
better estimates for some other reason.

This can however happen only when there are overlapping statistics, and
selecting one makes it impossible to use the other. E.g. with statistics
on (a,b), (c,d), (b,c,d), we may pick either (a,b) and (c,d) or (b,c,d).
But it's not clear which option is the best one.

We however assume cases like this are rare, and the easiest solution is
to define statistics covering the whole group of correlated columns. In
the future we might support overlapping stats, using some of the clauses
as conditions (in conditional probability sense).

Author: Tomas Vondra
Reviewed-by: Mark Dilger, Kyotaro Horiguchi
Discussion: https://postgr.es/m/20191028152048.jc6pqv5hb7j77ocp@development

5 years agoApply all available functional dependencies
Tomas Vondra [Mon, 13 Jan 2020 00:20:57 +0000 (01:20 +0100)]
Apply all available functional dependencies

When considering functional dependencies during selectivity estimation,
it's not necessary to bother with selecting the best extended statistic
object and then use just dependencies from it. We can simply consider
all applicable functional dependencies at once.

This means we need to deserialie all (applicable) dependencies before
applying them to the clauses. This is a bit more expensive than picking
the best statistics and deserializing dependencies for it. To minimize
the additional cost, we ignore statistics that are not applicable.

Author: Tomas Vondra
Reviewed-by: Mark Dilger
Discussion: https://postgr.es/m/20191028152048.jc6pqv5hb7j77ocp@development

5 years agoFix edge-case crashes and misestimation in range containment selectivity.
Tom Lane [Sun, 12 Jan 2020 19:36:59 +0000 (14:36 -0500)]
Fix edge-case crashes and misestimation in range containment selectivity.

When estimating the selectivity of "range_var <@ range_constant" or
"range_var @> range_constant", if the upper (or respectively lower)
bound of the range_constant was above the last bin of the range_var's
histogram, the code would access uninitialized memory and potentially
crash (though it seems the probability of a crash is quite low).
Handle the endpoint cases explicitly to fix that.

While at it, be more paranoid about the possibility of getting NaN
or other silly results from the range type's subdiff function.
And improve some comments.

Ordinarily we'd probably add a regression test case demonstrating
the bug in unpatched code.  But it's too hard to get it to crash
reliably because of the uninitialized-memory dependence, so skip that.

Per bug #16122 from Adam Scott.  It's been broken from the beginning,
apparently, so backpatch to all supported branches.

Diagnosis by Michael Paquier, patch by Andrey Borodin and Tom Lane.

Discussion: https://postgr.es/m/16122-eb35bc248c806c15@postgresql.org

5 years agoRemove incorrect assertion for INSERT in logical replication's publisher
Michael Paquier [Sun, 12 Jan 2020 13:43:45 +0000 (22:43 +0900)]
Remove incorrect assertion for INSERT in logical replication's publisher

On the publisher, it was assumed that an INSERT change cannot happen for
a relation with no replica identity.  However this is true only for a
change that needs references to old rows, aka UPDATE or DELETE, so
trying to use logical replication with a relation that has no replica
identity led to an assertion failure in the publisher when issuing an
INSERT.  This commit removes the incorrect assertion, and adds more
regression tests to provide coverage for relations without replica
identity.

Reported-by: Neha Sharma
Author: Dilip Kumar, Michael Paquier
Reviewed-by: Andres Freund
Discussion: https://postgr.es/m/CANiYTQsL1Hb8_Km08qd32svrqNumXLJeoGo014O7VZymgOhZEA@mail.gmail.com
Backpatch-through: 10

5 years agoExtensive code review for GSSAPI encryption mechanism.
Tom Lane [Sat, 11 Jan 2020 22:14:08 +0000 (17:14 -0500)]
Extensive code review for GSSAPI encryption mechanism.

Fix assorted bugs in handling of non-blocking I/O when using GSSAPI
encryption.  The encryption layer could return the wrong status
information to its caller, resulting in effectively dropping some data
(or possibly in aborting a not-broken connection), or in a "livelock"
situation where data remains to be sent but the upper layers think
transmission is done and just go to sleep.  There were multiple small
thinkos contributing to that, as well as one big one (failure to think
through what to do when a send fails after having already transmitted
data).  Note that these errors could cause failures whether the client
application asked for non-blocking I/O or not, since both libpq and
the backend always run things in non-block mode at this level.

Also get rid of use of static variables for GSSAPI inside libpq;
that's entirely not okay given that multiple connections could be
open at once inside a single client process.

Also adjust a bunch of random small discrepancies between the frontend
and backend versions of the send/receive functions -- except for error
handling, they should be identical, and now they are.

Also extend the Kerberos TAP tests to exercise cases where nontrivial
amounts of data need to be pushed through encryption.  Before, those
tests didn't provide any useful coverage at all for the cases of
interest here.  (They still might not, depending on timing, but at
least there's a chance.)

Per complaint from pmc@citylink and subsequent investigation.
Back-patch to v12 where this code was introduced.

Discussion: https://postgr.es/m/20200109181822.GA74698@gate.oper.dinoex.org

5 years agoMake lsn argument of walrcv_create_slot() optional
Peter Eisentraut [Sat, 11 Jan 2020 08:00:19 +0000 (09:00 +0100)]
Make lsn argument of walrcv_create_slot() optional

Some callers are not using it, so it's wasteful to have to specify it.

Reviewed-by: Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
Discussion: https://www.postgresql.org/message-id/CA+fd4k4BcYrYucNfTnK-CQX3+jsG+PRPEhHAUSo-W4P0Lec57A@mail.gmail.com

5 years agoRemove STATUS_FOUND
Peter Eisentraut [Sun, 29 Dec 2019 08:09:20 +0000 (09:09 +0100)]
Remove STATUS_FOUND

Replace the solitary use with a bool.

Reviewed-by: Michael Paquier <michael@paquier.xyz>
Discussion: https://www.postgresql.org/message-id/flat/a6f91ead-0ce4-2a34-062b-7ab9813ea308%402ndquadrant.com

5 years agoMaintain valid md.c state when FileClose() fails.
Noah Misch [Sat, 11 Jan 2020 02:31:22 +0000 (18:31 -0800)]
Maintain valid md.c state when FileClose() fails.

FileClose() failure ordinarily causes a PANIC.  Suppose the user
disables that PANIC via data_sync_retry=on.  After mdclose() issued a
FileClose() that failed, calls into md.c raised SIGSEGV.  This fix adds
repalloc() calls during mdclose(); update a comment about ignoring
repalloc() cost.  The rate of relation segment count change is a minor
factor; more relevant to overall performance is the rate of mdclose()
and subsequent re-opening of segments.  Back-patch to v10, where commit
45e191e3aa62d47a8bc1a33f784286b2051f45cb introduced the bug.

Reviewed by Kyotaro Horiguchi.

Discussion: https://postgr.es/m/20191222091930.GA1280238@rfd.leadboat.com

5 years agonbtree: Rename BT_HEAP_TID_ATTR.
Peter Geoghegan [Fri, 10 Jan 2020 21:15:28 +0000 (13:15 -0800)]
nbtree: Rename BT_HEAP_TID_ATTR.

Author: Peter Geoghegan
Reviewed-By: Heikki Linnakangas
5 years agonbtree: BTREE_[MIN|NOVAC]_VERSION comment tweaks.
Peter Geoghegan [Fri, 10 Jan 2020 21:12:50 +0000 (13:12 -0800)]
nbtree: BTREE_[MIN|NOVAC]_VERSION comment tweaks.

Author: Peter Geoghegan
Reviewed-By: Heikki Linnakangas
5 years agoClean up representation of flags in struct ReorderBufferTXN
Alvaro Herrera [Fri, 10 Jan 2020 20:46:57 +0000 (17:46 -0300)]
Clean up representation of flags in struct ReorderBufferTXN

This simplifies addition of further flags.

Author: Nikhil Sontakke
Discussion: https://postgr.es/m/CAMGcDxeViP+R-OL7QhzUV9eKCVjURobuY1Zijik4Ay_Ddwo4Cg@mail.gmail.com

5 years agodoc: Fix naming of SELinux
Michael Paquier [Fri, 10 Jan 2020 00:36:55 +0000 (09:36 +0900)]
doc: Fix naming of SELinux

Reported-by: Tham Nguyen
Discussion: https://postgr.es/m/157851402876.29175.12977878383183540468@wrigleys.postgresql.org
Backpatch-through: 9.4

5 years agopgbench: Make more debug messages use common logging API
Michael Paquier [Fri, 10 Jan 2020 00:02:25 +0000 (09:02 +0900)]
pgbench: Make more debug messages use common logging API

This is a follow-up of 30a3e772, making the output more consistent when
using --debug for meta-command execution.

Author: Michael Paquier
Reviewed-by: Fabien Coelho
Discussion: https://postgr.es/m/alpine.DEB.2.21.1912241100390.3339@pseudo

5 years agoSkip tab-completion tests if envar SKIP_READLINE_TESTS is defined.
Tom Lane [Thu, 9 Jan 2020 21:46:05 +0000 (16:46 -0500)]
Skip tab-completion tests if envar SKIP_READLINE_TESTS is defined.

Experience so far suggests that getting these tests to pass on
all libedit versions that are out there may be impossible, or
require dumbing down the tests to the point of uselessness.
So we need to provide a way to skip them when the user knows they'll
fail.  An environment variable is probably the most convenient way
to deal with this; it's easy for, e.g., a buildfarm animal's
configuration to set up.

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

5 years agoReconsider the representation of join alias Vars.
Tom Lane [Thu, 9 Jan 2020 16:56:59 +0000 (11:56 -0500)]
Reconsider the representation of join alias Vars.

The core idea of this patch is to make the parser generate join alias
Vars (that is, ones with varno pointing to a JOIN RTE) only when the
alias Var is actually different from any raw join input, that is a type
coercion and/or COALESCE is necessary to generate the join output value.
Otherwise just generate varno/varattno pointing to the relevant join
input column.

In effect, this means that the planner's flatten_join_alias_vars()
transformation is already done in the parser, for all cases except
(a) columns that are merged by JOIN USING and are transformed in the
process, and (b) whole-row join Vars.  In principle that would allow
us to skip doing flatten_join_alias_vars() in many more queries than
we do now, but we don't have quite enough infrastructure to know that
we can do so --- in particular there's no cheap way to know whether
there are any whole-row join Vars.  I'm not sure if it's worth the
trouble to add a Query-level flag for that, and in any case it seems
like fit material for a separate patch.  But even without skipping the
work entirely, this should make flatten_join_alias_vars() faster,
particularly where there are nested joins that it previously had to
flatten recursively.

An essential part of this change is to replace Var nodes'
varnoold/varoattno fields with varnosyn/varattnosyn, which have
considerably more tightly-defined meanings than the old fields: when
they differ from varno/varattno, they identify the Var's position in
an aliased JOIN RTE, and the join alias is what ruleutils.c should
print for the Var.  This is necessary because the varno change
destroyed ruleutils.c's ability to find the JOIN RTE from the Var's
varno.

Another way in which this change broke ruleutils.c is that it's no
longer feasible to determine, from a JOIN RTE's joinaliasvars list,
which join columns correspond to which columns of the join's immediate
input relations.  (If those are sub-joins, the joinaliasvars entries
may point to columns of their base relations, not the sub-joins.)
But that was a horrid mess requiring a lot of fragile assumptions
already, so let's just bite the bullet and add some more JOIN RTE
fields to make it more straightforward to figure that out.  I added
two integer-List fields containing the relevant column numbers from
the left and right input rels, plus a count of how many merged columns
there are.

This patch depends on the ParseNamespaceColumn infrastructure that
I added in commit 5815696bc.  The biggest bit of code change is
restructuring transformFromClauseItem's handling of JOINs so that
the ParseNamespaceColumn data is propagated upward correctly.

Other than that and the ruleutils fixes, everything pretty much
just works, though some processing is now inessential.  I grabbed
two pieces of low-hanging fruit in that line:

1. In find_expr_references, we don't need to recurse into join alias
Vars anymore.  There aren't any except for references to merged USING
columns, which are more properly handled when we scan the join's RTE.
This change actually fixes an edge-case issue: we will now record a
dependency on any type-coercion function present in a USING column's
joinaliasvar, even if that join column has no references in the query
text.  The odds of the missing dependency causing a problem seem quite
small: you'd have to posit somebody dropping an implicit cast between
two data types, without removing the types themselves, and then having
a stored rule containing a whole-row Var for a join whose USING merge
depends on that cast.  So I don't feel a great need to change this in
the back branches.  But in theory this way is more correct.

2. markRTEForSelectPriv and markTargetListOrigin don't need to recurse
into join alias Vars either, because the cases they care about don't
apply to alias Vars for USING columns that are semantically distinct
from the underlying columns.  This removes the only case in which
markVarForSelectPriv could be called with NULL for the RTE, so adjust
the comments to describe that hack as being strictly internal to
markRTEForSelectPriv.

catversion bump required due to changes in stored rules.

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

5 years agoAdd pg_shmem_allocations view.
Robert Haas [Thu, 9 Jan 2020 15:59:07 +0000 (10:59 -0500)]
Add pg_shmem_allocations view.

This tells you about allocations that have been made from the main
shared memory segment. The original patch also tried to show information
about dynamic shared memory allocation as well, but I decided to
leave that problem for another time.

Andres Freund and Robert Haas, reviewed by Michael Paquier, Marti
Raudsepp, Tom Lane, Álvaro Herrera, and Kyotaro Horiguchi.

Discussion: http://postgr.es/m/20140504114417.GM12715@awork2.anarazel.de

5 years agoRemove bogus 'return'.
Robert Haas [Thu, 9 Jan 2020 14:01:37 +0000 (09:01 -0500)]
Remove bogus 'return'.

Per the buildfarm, via Michael Paquier.

Discussion: http://postgr.es/m/20200108032648.GE3413@paquier.xyz

5 years agoClarify that pg_trgm is used in example
Magnus Hagander [Thu, 9 Jan 2020 09:48:22 +0000 (10:48 +0100)]
Clarify that pg_trgm is used in example

Reported-by: Octopus ZHANG
Author: Daniel Gustafsson

5 years agoAdd support for automatically updating Unicode derived files
Peter Eisentraut [Thu, 9 Jan 2020 08:54:47 +0000 (09:54 +0100)]
Add support for automatically updating Unicode derived files

We currently have several sets of files generated from data provided
by Unicode.  These all have ad hoc rules and instructions for updating
when new Unicode versions appear, and it's not done consistently.

This patch centralizes and automates the process and makes it part of
the release checklist.  The Unicode and CLDR versions are specified in
Makefile.global.in.  There is a new make target "update-unicode" that
downloads all the relevant files and runs the generation script.

There is also a new script for generating the table of combining
characters for ucs_wcwidth().  That table is now in a separate include
file rather than hardcoded into the middle of other code.  This is
based on the script that was used for generating
d8594d123c155aeecd47fc2450f62f5100b2fbf0, but the script itself wasn't
committed at that time.

Reviewed-by: John Naylor <john.naylor@2ndquadrant.com>
Discussion: https://www.postgresql.org/message-id/flat/c8d05f42-443e-6c23-819b-05b31759a37c@2ndquadrant.com

5 years agoAllow 'sslkey' and 'sslcert' in postgres_fdw user mappings
Andrew Dunstan [Thu, 9 Jan 2020 08:09:54 +0000 (18:39 +1030)]
Allow 'sslkey' and 'sslcert' in postgres_fdw user mappings

This allows different users to authenticate with different certificates.

Author: Craig Ringer

5 years agoModernize Python exception syntax in tests
Peter Eisentraut [Wed, 8 Jan 2020 20:48:44 +0000 (21:48 +0100)]
Modernize Python exception syntax in tests

Change the exception syntax used in the tests to use the more current

    except Exception as ex:

rather than the old

    except Exception, ex:

Since support for Python <2.6 has been removed, all supported versions
now support the new style, and we can save one step in the Python 3
compatibility conversion.

Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://www.postgresql.org/message-id/flat/98b69261-298c-13d2-f34d-836fd9c29b21%402ndquadrant.com

5 years agoRemove support for Python older than 2.6
Peter Eisentraut [Wed, 8 Jan 2020 20:48:44 +0000 (21:48 +0100)]
Remove support for Python older than 2.6

Supporting very old Python versions is a maintenance burden,
especially with the several variant test files to maintain for Python
<2.6.

Since we have dropped support for older OpenSSL versions in
7b283d0e1d1d79bf1c962d790c94d2a53f3bb38a, RHEL 5 is now effectively
desupported, and that was also the only mainstream operating system
still using Python versions before 2.6, so it's a good time to drop
those as well.

Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://www.postgresql.org/message-id/flat/98b69261-298c-13d2-f34d-836fd9c29b21%402ndquadrant.com

5 years agoReimplement nullification of walsender timestamp
Alvaro Herrera [Wed, 8 Jan 2020 17:33:49 +0000 (14:33 -0300)]
Reimplement nullification of walsender timestamp

Make the value null only at pg_stat_activity-output time, as suggested
by Tom Lane, instead of messing with the internal state.  This should
appease buildfarm members with force_parallel_mode=regress, which are
running parallel queries on logical replication walsenders.

The fact that walsenders can run parallel queries should perhaps be
studied more carefully, but for the moment let's get rid of the red
blots in buildfarm.

Backpatch to pg10, like the previous commit.

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

5 years agoImprove the handling of result type coercions in SQL functions.
Tom Lane [Wed, 8 Jan 2020 16:07:53 +0000 (11:07 -0500)]
Improve the handling of result type coercions in SQL functions.

Use the parser's standard type coercion machinery to convert the
output column(s) of a SQL function's final SELECT or RETURNING
to the type(s) they should have according to the function's declared
result type.  We'll allow any case where an assignment-level
coercion is available.  Previously, we failed unless the required
coercion was a binary-compatible one (and the documentation ignored
this, falsely claiming that the types must match exactly).

Notably, the coercion now accounts for typmods, so that cases where
a SQL function is declared to return a composite type whose columns
are typmod-constrained now behave as one would expect.  Arguably
this aspect is a bug fix, but the overall behavioral change here
seems too large to consider back-patching.

A nice side-effect is that functions can now be inlined in a
few cases where we previously failed to do so because of type
mismatches.

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

5 years agoImprove GSSAPI Encryption startup comment in libpq
Stephen Frost [Wed, 8 Jan 2020 15:57:09 +0000 (10:57 -0500)]
Improve GSSAPI Encryption startup comment in libpq

The original comment was a bit confusing, pointed out by Alvaro Herrera.

Thread: https://postgr.es/m/20191224151520.GA16435%40alvherre.pgsql

5 years agoFix handling of generated columns in ALTER TABLE.
Tom Lane [Wed, 8 Jan 2020 14:42:53 +0000 (09:42 -0500)]
Fix handling of generated columns in ALTER TABLE.

ALTER TABLE failed if a column referenced in a GENERATED expression
had been added or changed in type earlier in the ALTER command.
That's because the GENERATED expression needs to be evaluated
against the table's updated tuples, but it was being evaluated
against the original tuples.  (Fortunately the executor has adequate
cross-checks to notice the mismatch, so we just got an obscure error
message and not anything more dangerous.)

Per report from Andreas Joseph Krogh.  Back-patch to v12 where
GENERATED was added.

Discussion: https://postgr.es/m/VisenaEmail.200.231b0a41523275d0.16ea7f800c7@tc7-visena

5 years agopgbench: Use common logging API
Peter Eisentraut [Wed, 8 Jan 2020 13:23:55 +0000 (14:23 +0100)]
pgbench: Use common logging API

Author: Fabien COELHO <coelho@cri.ensmp.fr>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Reviewed-by: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
Discussion: https://www.postgresql.org/message-id/flat/alpine.DEB.2.21.1912241100390.3339@pseudo

5 years agoRevert "Forbid DROP SCHEMA on temporary namespaces"
Michael Paquier [Wed, 8 Jan 2020 01:36:12 +0000 (10:36 +0900)]
Revert "Forbid DROP SCHEMA on temporary namespaces"

This reverts commit a052f6c, following complains from Robert Haas and
Tom Lane.  Backpatch down to 9.4, like the previous commit.

Discussion: https://postgr.es/m/CA+TgmobL4npEX5=E5h=5Jm_9mZun3MT39Kq2suJFVeamc9skSQ@mail.gmail.com
Backpatch-through: 9.4

5 years agoRemove dependency to system calls for memory allocation in refint
Michael Paquier [Wed, 8 Jan 2020 01:02:55 +0000 (10:02 +0900)]
Remove dependency to system calls for memory allocation in refint

Failures in allocations could lead to crashes with NULL pointer
dereferences .  Memory context TopMemoryContext is used instead to keep
alive the plans allocated in the session.  A more specific context could
be used here, but this is left for later.

Reported-by: Jian Zhang
Author: Michael Paquier
Reviewed-by: Tom Lane, Andres Freund
Discussion: https://postgr.es/m/16190-70181c803641c3dc@postgresql.org

5 years agopg_stat_activity: show NULL stmt start time for walsenders
Alvaro Herrera [Tue, 7 Jan 2020 20:38:48 +0000 (17:38 -0300)]
pg_stat_activity: show NULL stmt start time for walsenders

Returning a non-NULL time is pointless, sinc a walsender is not a
process that would be running normal transactions anyway, but the code
was unintentionally exposing the process start time intermittently,
which was not only bogus but it also confused monitoring systems looking
for idle transactions.  Fix by avoiding all updates in walsenders.

Backpatch to 11, where walsenders started appearing in pg_stat_activity.

Reported-by: Tomas Vondra
Discussion: https://postgr.es/m/20191209234409.exe7osmyalwkt5j4@development

5 years agotableam: New callback relation_fetch_toast_slice.
Robert Haas [Tue, 7 Jan 2020 19:35:48 +0000 (14:35 -0500)]
tableam: New callback relation_fetch_toast_slice.

Instead of always calling heap_fetch_toast_slice during detoasting,
invoke a table AM callback which, when the toast table is a heap
table, will be heap_fetch_toast_slice.

This makes it possible for a table AM other than heap to be used
as a TOAST table. It also completes the series of commits intended
to improve the interaction of tableam with TOAST that began with
commit 8b94dab06617ef80a0901ab103ebd8754427ef5a; detoast.c is
now, hopefully, fully AM-independent.

Patch by me, reviewed by Andres Freund and Peter Eisentraut.

Discussion: http://postgr.es/m/CA+TgmoZv-=2iWM4jcw5ZhJeL18HF96+W1yJeYrnGMYdkFFnEpQ@mail.gmail.com

5 years agotableam: Allow choice of toast AM.
Robert Haas [Tue, 7 Jan 2020 19:23:25 +0000 (14:23 -0500)]
tableam: Allow choice of toast AM.

Previously, the toast table had to be implemented by the same AM that
was used for the main table, which was bad, because the detoasting
code won't work with anything but heap. This commit doesn't fix the
latter problem, although there's another patch coming which does,
but it does let you pick something that works (i.e. heap, right now).

Patch by me, reviewed by Andres Freund.

Discussion: http://postgr.es/m/CA+TgmoZv-=2iWM4jcw5ZhJeL18HF96+W1yJeYrnGMYdkFFnEpQ@mail.gmail.com

5 years agoIncrease the maximum value of track_activity_query_size.
Robert Haas [Tue, 7 Jan 2020 17:14:19 +0000 (12:14 -0500)]
Increase the maximum value of track_activity_query_size.

This one-line change provoked a lot of discussion, but ultimately
the consensus seems to be that allowing a larger value might be
useful to somebody, and probably won't hurt anyone who chooses
not to take advantage of the higher maximum limit.

Vyacheslav Makarov, reviewed by many people.

Discussion: http://postgr.es/m/7b5ecc5a9991045e2f13c84e3047541d@postgrespro.ru

5 years agoClean up management of IP addresses in our SSL tests.
Tom Lane [Tue, 7 Jan 2020 01:56:32 +0000 (20:56 -0500)]
Clean up management of IP addresses in our SSL tests.

Instead of hard-wiring the netmask as /32, allow it to be specified
where we specify the server address.  This will ease changing the
test to use IPv6, when/if somebody wants to do that.

Also remove the hard-wired pg_hba.conf entries for IPv6 (::1/128).
These have never had any usefulness, because the client side
of the tests has always explicitly connected to $SERVERHOSTADDR
which has always been set to IPv4 (127.0.0.1).  All they accomplish
is to break the test on non-IPv6-supporting hosts, and besides
that they violate the express intent of the code to minimize the
server's range of allowed connections.

This could be back-patched, perhaps, but for now I don't see
a need to.

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

5 years agoReduce the number of GetFlushRecPtr() calls done by walsenders.
Tom Lane [Mon, 6 Jan 2020 21:42:20 +0000 (16:42 -0500)]
Reduce the number of GetFlushRecPtr() calls done by walsenders.

Since the WAL flush position only moves forward, it's safe to cache
its previous value within each walsender process, and update from
shared memory only once we've caught up to the previously-seen value.
When there are many active walsenders, this makes for a very significant
reduction in the amount of contention on the XLogCtl->info_lck spinlock.

This patch also adjusts the logic so that we update our idea of the
flush position after processing a WAL record, rather than beforehand.
This may cause us to realize we're not caught up when the preceding
coding would've thought that we were, but that seems all to the good;
it may avoid a useless sleep-and-wakeup cycle.

Back-patch to v12.  The contention problem exists in prior branches,
but it's much less severe (due to inefficiencies elsewhere) so there
seems no need to take any risk of back-patching further.

Pierre Ducroquet, reviewed by Julien Rouhaud

Discussion: https://postgr.es/m/2931018.Vxl9zapr77@pierred-pdoc

5 years agoAdd functions min_scale(numeric) and trim_scale(numeric).
Tom Lane [Mon, 6 Jan 2020 17:13:53 +0000 (12:13 -0500)]
Add functions min_scale(numeric) and trim_scale(numeric).

These allow better control of trailing zeroes in numeric values.

Pavel Stehule, based on an old proposal of Marko Tiikkaja's;
review by Karl Pinc

Discussion: https://postgr.es/m/CAFj8pRDjs-navGASeF0Wk74N36YGFJ+v=Ok9_knRa7vDc-qugg@mail.gmail.com

5 years agoHave logical replication subscriber fire column triggers
Peter Eisentraut [Mon, 6 Jan 2020 07:21:14 +0000 (08:21 +0100)]
Have logical replication subscriber fire column triggers

The logical replication apply worker did not fire per-column update
triggers because the updatedCols bitmap in the RTE was not populated.
This fixes that.

Reviewed-by: Euler Taveira <euler@timbira.com.br>
Discussion: https://www.postgresql.org/message-id/flat/21673e2d-597c-6afe-637e-e8b10425b240%402ndquadrant.com

5 years agoRemove support for OpenSSL 0.9.8 and 1.0.0
Michael Paquier [Mon, 6 Jan 2020 03:51:44 +0000 (12:51 +0900)]
Remove support for OpenSSL 0.9.8 and 1.0.0

Support is out of scope from all the major vendors for these versions
(for example RHEL5 uses a version based on 0.9.8, and RHEL6 uses 1.0.1),
and it created some extra maintenance work.  Upstream has stopped
support of 0.9.8 in December 2015 and of 1.0.0 in February 2016.

Since b1abfec, note that the default SSL protocol version set with
ssl_min_protocol_version is TLSv1.2, whose support was added in OpenSSL
1.0.1, so there is no point to enforce ssl_min_protocol_version to TLSv1
in the SSL tests.

Author: Michael Paquier
Reviewed-by: Daniel Gustafsson, Tom Lane
Discussion: https://postgr.es/m/20191205083252.GE5064@paquier.xyz

5 years agoRemove redundant incomplete split assertion.
Peter Geoghegan [Mon, 6 Jan 2020 01:42:13 +0000 (17:42 -0800)]
Remove redundant incomplete split assertion.

The fastpath insert optimization's incomplete split flag Assert() is
redundant.  We'll reach the more general Assert() within
_bt_findinsertloc() in all cases. (Besides, Assert()'ing that the
rightmost page doesn't have the flag set never made much sense.)

5 years agoMinor style improvements for tab-completion test.
Tom Lane [Sun, 5 Jan 2020 16:35:45 +0000 (11:35 -0500)]
Minor style improvements for tab-completion test.

Use qr// syntax for regex values.
Include the regex that failed to match in diagnostic reports.

Dagfinn Ilmari Mannsåker

Discussion: https://postgr.es/m/87k16610xk.fsf@wibble.ilmari.org

5 years agoDocs: use more standard terminology "round-to-nearest-even" instead of "round-to...
Tatsuo Ishii [Sun, 5 Jan 2020 10:45:37 +0000 (19:45 +0900)]
Docs: use more standard terminology "round-to-nearest-even" instead of "round-to-even".

Per suggestion from Tom Lane.
Discussion: https://postgr.es/m/flat/20191230.093451.1762483750956466101.t-ishii%40sraoss.co.jp

5 years agoAvoid reading ~/.inputrc in tab-completion test, and revert other changes.
Tom Lane [Sun, 5 Jan 2020 02:33:34 +0000 (21:33 -0500)]
Avoid reading ~/.inputrc in tab-completion test, and revert other changes.

The true explanation for Peter Geoghegan's trouble report turns out
to be that he has a ~/.inputrc that affects readline's behavior
enough to break this test.  Prevent readline from reading that file.

Also, the best way to prevent TERM from affecting the results seems
to be to unset it altogether, not to set it to "xterm".  The latter
choice licenses readline to emit xterm escape sequences, and there's
a lot of variation in exactly what it will emit.

Revert changes that attempted to account exactly for xterm escape
sequences.  We shouldn't need that with TERM unset, and it was not
looking like a maintainable solution anyway.

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

5 years agoDon't try to force TERM to a fixed value in tab-completion test.
Tom Lane [Sat, 4 Jan 2020 21:40:56 +0000 (16:40 -0500)]
Don't try to force TERM to a fixed value in tab-completion test.

Right at the moment, this is making things worse not better in the
buildfarm.  I'm not happy with anything about the current state,
but let's at least try to have a green buildfarm report while further
investigation continues.

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

5 years agoIn tab-completion test, print out the value of TERM before changing it.
Tom Lane [Sat, 4 Jan 2020 20:05:24 +0000 (15:05 -0500)]
In tab-completion test, print out the value of TERM before changing it.

I'm curious to see what values are prevailing in the buildfarm.

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

5 years agoSkip memcpy(x, x) in qunique().
Noah Misch [Sat, 4 Jan 2020 19:31:42 +0000 (11:31 -0800)]
Skip memcpy(x, x) in qunique().

It has undefined behavior.  Follow the precedent of commit
9a9473f3cce1a21c25d6cc7569710e832d2b180b.  No back-patch, since the
master branch alone has this function.

Discussion: https://postgr.es/m/20191229070221.GA13873@gust.leadboat.com

5 years agoMake tab-completion tests more robust.
Tom Lane [Sat, 4 Jan 2020 19:29:28 +0000 (14:29 -0500)]
Make tab-completion tests more robust.

Depending on as-yet-incompletely-explained factors, readline/libedit
might choose to emit screen-control escape sequences as part of
repainting the display.  I'd tried to make the test patterns avoid
matching parts of the output that are likely to contain such, but
it seems that there's really no way around matching them explicitly
in some places, unless we want to just give up testing some behaviors
such as display of alternatives.

Per report from Peter Geoghegan.

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

5 years agoMake better use of ParseState in ProcessUtility
Peter Eisentraut [Sat, 4 Jan 2020 10:56:58 +0000 (11:56 +0100)]
Make better use of ParseState in ProcessUtility

Pass ParseState into the functions called from
standard_ProcessUtility() instead passing the query string and query
environment separately.  No functionality change, but it makes the
notation consistent.  We had already started moving things into
that direction piece by piece, and this completes it.

Reviewed-by: Pavel Stehule <pavel.stehule@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/6e7aa4a1-be6a-1a75-b1f9-83a678e5184a@2ndquadrant.com

5 years agoAdd xl_btree_delete optimization.
Peter Geoghegan [Fri, 3 Jan 2020 20:18:13 +0000 (12:18 -0800)]
Add xl_btree_delete optimization.

Commit 558a9165e08 taught _bt_delitems_delete() to produce its own XID
horizon on the primary.  Standbys no longer needed to generate their own
latestRemovedXid, since they could just use the explicitly logged value
from the primary instead.  The deleted offset numbers array from the
xl_btree_delete WAL record was no longer used by the REDO routine for
anything other than deleting the items.

This enables a minor optimization:  We now treat the array as buffer
state, not generic WAL data, following _bt_delitems_vacuum()'s example.
This should be a minor win, since it allows us to avoid including the
deleted items array in cases where XLogInsert() stores the whole buffer
anyway.  The primary goal here is to make the code more maintainable,
though.  Removing inessential differences between the two functions
highlights the fundamental differences that remain.

Also change xl_btree_delete to use uint32 for the size of the array of
item offsets being deleted.  This brings xl_btree_delete closer to
xl_btree_vacuum.  Furthermore, it seems like a good idea to use an
explicit-width integer type (the field was previously an "int").

Bump XLOG_PAGE_MAGIC because xl_btree_delete changed.

Discussion: https://postgr.es/m/CAH2-Wzkz4TjmezzfAbaV1zYrh=fr0bCpzuJTvBe5iUQ3aUPsCQ@mail.gmail.com

5 years agoFurther fixes for tab-completion TAP tests.
Tom Lane [Fri, 3 Jan 2020 17:54:13 +0000 (12:54 -0500)]
Further fixes for tab-completion TAP tests.

Escape non-printable characters in failure reports, by using Data::Dumper
in Useqq mode.  Also, bump $Test::Builder::Level so the diagnostic
references the calling line, and use diag() instad of note(),
so it shows even in non-verbose mode (per request from Christoph Berg).

Also, give up on trying to test for the specific way that readline
chooses to overwrite existing text in the \DRD -> \drds test.
There are too many variants, it seems, at least on the libedit
side of things.

Dagfinn Ilmari Mannsåker and Tom Lane

Discussion: https://postgr.es/m/20200103110128.GA28967@msg.df7cb.de

5 years agoAdd an ugly workaround for a bug in some recent libedit versions.
Tom Lane [Fri, 3 Jan 2020 16:15:26 +0000 (11:15 -0500)]
Add an ugly workaround for a bug in some recent libedit versions.

Debian unstable is shipping a broken version of libedit: it de-escapes
words before passing them to the application's tab completion function,
preventing us from recognizing backslash commands.  Fortunately,
we have enough information available to dig the original text out of
rl_line_buffer, so ignore the string argument and do that.

I view this as a temporary workaround to get the affected buildfarm
members back to green in the wake of 7c015045b.  I hope we can get
rid of it once somebody fixes Debian's libedit; hence, no back-patch,
at least for now.

Discussion: https://postgr.es/m/20200103110128.GA28967@msg.df7cb.de

5 years agopgbench: Improve test description
Peter Eisentraut [Fri, 3 Jan 2020 09:44:13 +0000 (10:44 +0100)]
pgbench: Improve test description

Author: Fabien COELHO <coelho@cri.ensmp.fr>

5 years agoFix typos in parallel query docs.
Amit Kapila [Fri, 3 Jan 2020 05:22:46 +0000 (10:52 +0530)]
Fix typos in parallel query docs.

Reported-by: Jon Jensen
Author: Jon Jensen
Reviewed-by: Amit Kapila and Robert Haas
Backpatch-through: 10
Discussion: https://postgr.es/m/nycvar.YSQ.7.76.1912301807510.9899@ybpnyubfg

5 years agoClear up btree_xlog_split() alignment comment.
Peter Geoghegan [Fri, 3 Jan 2020 02:30:25 +0000 (18:30 -0800)]
Clear up btree_xlog_split() alignment comment.

Adjust a comment that describes how alignment of the new left page high
key works in btree_xlog_split(), the nbtree page split REDO routine.
The wording used before commit 2c03216d831 is much clearer, so go back
to that.

5 years agoMinor portability fixes for new TAP script.
Tom Lane [Fri, 3 Jan 2020 00:44:43 +0000 (19:44 -0500)]
Minor portability fixes for new TAP script.

Satisfy perlcritic, mostly.  Per buildfarm.

5 years agoCorrect _bt_delitems_vacuum() lock comments.
Peter Geoghegan [Thu, 2 Jan 2020 21:30:40 +0000 (13:30 -0800)]
Correct _bt_delitems_vacuum() lock comments.

The expectation within _bt_delitems_vacuum() is that caller has a
super-exclusive/cleanup buffer lock (not just a pin and a write lock).

5 years agoFix cloning of row triggers to sub-partitions
Alvaro Herrera [Thu, 2 Jan 2020 20:04:24 +0000 (17:04 -0300)]
Fix cloning of row triggers to sub-partitions

When row triggers exist in partitioned partitions that are not either
part of FKs or deferred unique constraints, they are not correctly
cloned to their partitions.  That's because they are marked "internal",
and those are purposefully skipped when doing the clone triggers dance.
Fix by relaxing the condition on which internal triggers are skipped.

Amit Langote initially diagnosed the problem and proposed a fix, but I
used a different approach.

Reported-by: Petr Fedorov
Discussion: https://postgr.es/m/6b3f0646-ba8c-b3a9-c62d-1c6651a1920f@phystech.edu

5 years agoAdd basic TAP tests for psql's tab-completion logic.
Tom Lane [Thu, 2 Jan 2020 20:02:21 +0000 (15:02 -0500)]
Add basic TAP tests for psql's tab-completion logic.

Up to now, psql's tab-complete.c has had exactly no regression test
coverage.  This patch is an experimental attempt to add some.

This needs Perl's IO::Pty module, which isn't installed everywhere,
so the test script just skips all tests if that's not present.
There may be other portability gotchas too, so I await buildfarm
results with interest.

So far this just covers a few very basic keyword-completion and
query-driven-completion scenarios, which should be enough to let us
get a feel for whether this is practical at all from a portability
standpoint.  If it is, there's lots more that can be done.

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

5 years agoFix typmod exposed for scalar function in FROM, too.
Tom Lane [Thu, 2 Jan 2020 19:02:46 +0000 (14:02 -0500)]
Fix typmod exposed for scalar function in FROM, too.

On further reflection about commit 4d02eb017, it occurs to me that
expandRTE() had better agree with what addRangeTableEntryForFunction()
is doing.  So teach that about functions possibly having typmods, too.

5 years agoReorder two nbtree.h function prototypes.
Peter Geoghegan [Thu, 2 Jan 2020 18:57:15 +0000 (10:57 -0800)]
Reorder two nbtree.h function prototypes.

Make the function prototype order consistent with the definition order
in nbtpage.c.

5 years agoFix collation exposed for scalar function in FROM.
Tom Lane [Thu, 2 Jan 2020 18:48:54 +0000 (13:48 -0500)]
Fix collation exposed for scalar function in FROM.

One code path in addRangeTableEntryForFunction() neglected to assign
a collation to the tupdesc entry it constructs (which is a bit odd
considering the other path did do so).  This didn't matter before commit
5815696bc, because nothing would look at the type data in this tupdesc;
but now it does.

While at it, make sure we assign the correct typmod as well.  Most
function expressions don't have a determinate typmod, but some do.

Per buildfarm, which showed failures in non-C collations, a case
I'd not thought to test for this patch :-(

5 years agoMake parser rely more heavily on the ParseNamespaceItem data structure.
Tom Lane [Thu, 2 Jan 2020 16:29:01 +0000 (11:29 -0500)]
Make parser rely more heavily on the ParseNamespaceItem data structure.

When I added the ParseNamespaceItem data structure (in commit 5ebaaa494),
it wasn't very tightly integrated into the parser's APIs.  In the wake of
adding p_rtindex to that struct (commit b541e9acc), there is a good reason
to make more use of it: by passing around ParseNamespaceItem pointers
instead of bare RTE pointers, we can get rid of various messy methods for
passing back or deducing the rangetable index of an RTE during parsing.
Hence, refactor the addRangeTableEntryXXX functions to build and return
a ParseNamespaceItem struct, not just the RTE proper; and replace
addRTEtoQuery with addNSItemToQuery, which is passed a ParseNamespaceItem
rather than building one internally.

Also, add per-column data (a ParseNamespaceColumn array) to each
ParseNamespaceItem.  These arrays are built during addRangeTableEntryXXX,
where we have column type data at hand so that it's nearly free to fill
the data structure.  Later, when we need to build Vars referencing RTEs,
we can use the ParseNamespaceColumn info to avoid the rather expensive
operations done in get_rte_attribute_type() or expandRTE().
get_rte_attribute_type() is indeed dead code now, so I've removed it.
This makes for a useful improvement in parse analysis speed, around 20%
in one moderately-complex test query.

The ParseNamespaceColumn structs also include Var identity information
(varno/varattno).  That info isn't actually being used in this patch,
except that p_varno == 0 is a handy test for a dropped column.
A follow-on patch will make more use of it.

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

5 years agoFix comment in test
Peter Eisentraut [Thu, 2 Jan 2020 13:40:18 +0000 (14:40 +0100)]
Fix comment in test

The comment was apparently copy-and-pasted and did not reflect the
actual test outcome.

5 years agoFix running out of file descriptors for spill files.
Amit Kapila [Sat, 14 Dec 2019 06:11:37 +0000 (11:41 +0530)]
Fix running out of file descriptors for spill files.

Currently while decoding changes, if the number of changes exceeds a
certain threshold, we spill those to disk.  And this happens for each
(sub)transaction.  Now, while reading all these files, we don't close them
until we read all the files.  While reading these files, if the number of
such files exceeds the maximum number of file descriptors, the operation
errors out.

Use PathNameOpenFile interface to open these files as that internally has
the mechanism to release kernel FDs as needed to get us under the
max_safe_fds limit.

Reported-by: Amit Khandekar
Author: Amit Khandekar
Reviewed-by: Amit Kapila
Backpatch-through: 9.4
Discussion: https://postgr.es/m/CAJ3gD9c-sECEn79zXw4yBnBdOttacoE-6gAyP0oy60nfs_sabQ@mail.gmail.com

5 years agoRevise BTP_HAS_GARBAGE nbtree VACUUM comments.
Peter Geoghegan [Thu, 2 Jan 2020 01:29:41 +0000 (17:29 -0800)]
Revise BTP_HAS_GARBAGE nbtree VACUUM comments.

_bt_delitems_vacuum() comments claimed that it isn't worth another scan
of the page to avoid falsely unsetting the BTP_HAS_GARBAGE page flag
hint (this happens to be the same wording that was removed from
_bt_delitems_delete() by my recent commit fe97c61c).  The comments made
little sense, though.  The issue can't have much to do with performing a
second scan of the target leaf page, since an LP_DEAD test could easily
be performed in the first scan of the page anyway (the scan that takes
place in btvacuumpage() caller).

Revise the explanation.  It makes much more sense to frame this as an
issue about recovery conflicts.  _bt_delitems_vacuum() cannot easily
generate an XID cutoff in the same way that _bt_delitems_delete() is
designed to.

Falsely unsetting the page flag is not ideal, and is likely to happen
more often than was supposed by the original comments.  Explain why it
usually isn't a problem in practice.  There may be an argument for
_bt_delitems_vacuum() not clearing the BTP_HAS_GARBAGE bit, removing the
question of it being falsely unset by VACUUM (there may even be an
argument for not using a page level hint at all).  This can be revisited
later.

5 years agoTest GROUP BY matching of join columns that are type-coerced by USING.
Tom Lane [Thu, 2 Jan 2020 00:31:41 +0000 (19:31 -0500)]
Test GROUP BY matching of join columns that are type-coerced by USING.

If we have, say, an int column that is left-joined to a bigint column
with USING, the merged column is the int column promoted to bigint.
GROUP BY's tests for whether grouping on the merged column allows a
reference to the underlying column, or vice versa, should know about
that relationship --- and they do.  But I nearly broke this case with
an ill-advised optimization, so the lack of any test coverage for it
seems like a bad idea.

5 years agoUpdate btree_xlog_delete() comments.
Peter Geoghegan [Wed, 1 Jan 2020 19:32:07 +0000 (11:32 -0800)]
Update btree_xlog_delete() comments.

Commit fe97c61c updated LP_DEAD item deletion comments, but missed a
minor discrepancy on the REDO side.  Fix it now.

In passing, don't talk about the btree_xlog_vacuum() behavior within
btree_xlog_delete().  The reliance on XLOG_HEAP2_CLEANUP_INFO records
for recovery conflicts is already discussed within btvacuumpage() and
mentioned again in passing above btree_xlog_vacuum(), which seems
sufficient.

5 years agoUpdate copyrights for 2020
Bruce Momjian [Wed, 1 Jan 2020 17:21:45 +0000 (12:21 -0500)]
Update copyrights for 2020

Backpatch-through: update all files in master, backpatch legal files through 9.4

5 years agoModernize Python exception syntax in documentation
Peter Eisentraut [Tue, 31 Dec 2019 09:35:16 +0000 (10:35 +0100)]
Modernize Python exception syntax in documentation

Change the exception syntax used in the documentation to use the more
current

    except Exception as ex:

rather than the old

    except Exception, ex:

We keep the old syntax in the test code since Python <2.6 is still
supported there, but the documentation might as well use the modern
syntax.

5 years agoMicro-optimize AllocSetFreeIndex() by reference to pg_bitutils code.
Tom Lane [Sat, 28 Dec 2019 22:21:17 +0000 (17:21 -0500)]
Micro-optimize AllocSetFreeIndex() by reference to pg_bitutils code.

Use __builtin_clz() where available.  Where it isn't, we can still win
a little by using the pg_leftmost_one_pos[] lookup table instead of
having a private table.

Also drop the initial right shift by ALLOC_MINBITS in favor of
subtracting ALLOC_MINBITS from the leftmost-one-pos result.  This
is a win because the compiler can fold that adjustment into other
constants it'd have to add anyway, making the shift-removal free.

Also, we can explain this coding as an unrolled form of
pg_leftmost_one_pos32(), even though that's a bit ahistorical
since it long predates pg_bitutils.h.

John Naylor, with some cosmetic adjustments by me

Discussion: https://postgr.es/m/CACPNZCuNUGMxjK7WTn_=WZnRbfASDdBxmjsVf2+m9MdmeNw_sg@mail.gmail.com

5 years agoAdd pg_dump test for triggers on partitioned tables
Alvaro Herrera [Fri, 27 Dec 2019 21:34:30 +0000 (18:34 -0300)]
Add pg_dump test for triggers on partitioned tables

This currently works, but add this test to ensure it continues to work.
Lack of this test became evident after a recent bugfix submission that
would have inadvertently broken it, in
https://postgr.es/m/CA+HiwqFM2=i+uHB9o4OkLbE2S3sjPHoVe2wXuAD1GLJ4+Pk9eg@mail.gmail.com

5 years agodoc: add examples of creative use of unique expression indexes
Bruce Momjian [Fri, 27 Dec 2019 19:49:08 +0000 (14:49 -0500)]
doc:  add examples of creative use of unique expression indexes

Unique expression indexes can constrain data in creative ways, so show
two examples.

Reported-by: Tuomas Leikola
Discussion: https://postgr.es/m/156760275564.1127.12321702656456074572@wrigleys.postgresql.org

Backpatch-through: 9.4

5 years agodocs: clarify infinite range values from data-type infinities
Bruce Momjian [Fri, 27 Dec 2019 19:33:30 +0000 (14:33 -0500)]
docs:  clarify infinite range values from data-type infinities

The previous docs referenced these distinct ideas confusingly.

Reported-by: Eugen Konkov
Discussion: https://postgr.es/m/376945611.20191026161529@yandex.ru

Backpatch-through: 9.4

5 years agoForbid DROP SCHEMA on temporary namespaces
Michael Paquier [Fri, 27 Dec 2019 08:58:43 +0000 (17:58 +0900)]
Forbid DROP SCHEMA on temporary namespaces

This operation was possible for the owner of the schema or a superuser.
Down to 9.4, doing this operation would cause inconsistencies in a
session whose temporary schema was dropped, particularly if trying to
create new temporary objects after the drop.  A more annoying
consequence is a crash of autovacuum on an assertion failure when
logging information about an orphaned temp table dropped.  Note that
because of 246a6c8 (present in v11~), which has made the removal of
orphaned temporary tables more aggressive, the failure could be
triggered more easily, but it is possible to reproduce down to 9.4.

Reported-by: Mahendra Singh, Prabhat Sahu
Author: Michael Paquier
Reviewed-by: Kyotaro Horiguchi, Mahendra Singh
Discussion: https://postgr.es/m/CAKYtNAr9Zq=1-ww4etHo-VCC-k120YxZy5OS01VkaLPaDbv2tg@mail.gmail.com
Backpatch-through: 9.4

5 years agoRevert "Rename files and headers related to index AM"
Michael Paquier [Thu, 26 Dec 2019 23:09:00 +0000 (08:09 +0900)]
Revert "Rename files and headers related to index AM"

This follows multiple complains from Peter Geoghegan, Andres Freund and
Alvaro Herrera that this issue ought to be dug more before actually
happening, if it happens.

Discussion: https://postgr.es/m/20191226144606.GA5659@alvherre.pgsql

5 years agoFix possible loss of sync between rectypeid and underlying PLpgSQL_type.
Tom Lane [Thu, 26 Dec 2019 20:19:39 +0000 (15:19 -0500)]
Fix possible loss of sync between rectypeid and underlying PLpgSQL_type.

When revalidate_rectypeid() acts to update a stale record type OID
in plpgsql's data structures, it fixes the active PLpgSQL_rec struct
as well as the PLpgSQL_type struct it references.  However, the latter
is shared across function executions while the former is not.  In a
later function execution, the PLpgSQL_rec struct would be reinitialized
by copy_plpgsql_datums and would then contain a stale type OID,
typically leading to "could not open relation with OID NNNN" errors.
revalidate_rectypeid() can easily fix this, fortunately, just by
treating typ->typoid as authoritative.

Per report and diagnosis from Ashutosh Sharma, though this is not his
suggested fix.  Back-patch to v11 where this code came in.

Discussion: https://postgr.es/m/CAE9k0Pkd4dZwt9J5pS9xhJFWpUtqs05C9xk_GEwPzYdV=GxwWg@mail.gmail.com

5 years agoImprove comments in utils/rel.h.
Tom Lane [Thu, 26 Dec 2019 16:20:05 +0000 (11:20 -0500)]
Improve comments in utils/rel.h.

Mark the fields that should be accessed via partitioning-related
functions, as we already did for some other fields.

Amit Langote

Discussion: https://postgr.es/m/CA+HiwqFnK6LbVMACMCaqwWrvoSFTecZzufKRahg2qGvLPYMX=g@mail.gmail.com