Tom Lane [Thu, 3 Sep 2020 20:52:09 +0000 (16:52 -0400)]     Avoid lockup of a parallel worker when reporting a long error message.
 
 Because sigsetjmp() will restore the initial state with signals blocked,
 the code path in bgworker.c for reporting an error and exiting would
 execute that way.  Usually this is fairly harmless; but if a parallel
 worker had an error message exceeding the shared-memory communication
 buffer size (16K) it would lock up, because it would wait for a
 resume-sending signal from its parallel leader which it would never
 detect.
 
 To fix, just unblock signals at the appropriate point.
 
 This can be shown to fail back to 9.6.  The lack of parallel query
 infrastructure makes it difficult to provide a simple test case for
 9.5; but I'm pretty sure the issue exists in some form there as well,
 so apply the code change there too.
 
 Vignesh C, reviewed by Bharath Rupireddy, Robert Haas, and myself
 
 Discussion: https://postgr.es/m/CALDaNm1d1hHPZUg3xU4XjtWBOLCrA+-2cJcLpw-cePZ=GgDVfA@mail.gmail.com
 
 
    Tom Lane [Thu, 3 Sep 2020 16:16:48 +0000 (12:16 -0400)]     Allow records to span multiple lines in pg_hba.conf and pg_ident.conf.
  A backslash at the end of a line now causes the next line to be appended
 to the current one (effectively, the backslash and newline are discarded).
 This allows long HBA entries to be created without legibility problems. 
 While we're here, get rid of the former hard-wired length limit on
 pg_hba.conf lines, by using an expansible StringInfo buffer instead
 of a fixed-size local variable. 
 Since the same code is used to read the ident map file, these changes
 apply there as well. 
 Fabien Coelho, reviewed by Justin Pryzby and David Zhang 
 Discussion: https://postgr.es/m/alpine.DEB.2.21.
2003251906140.15243@pseudo  
  Tom Lane [Thu, 3 Sep 2020 15:45:26 +0000 (11:45 -0400)]     Doc: mention packager-supplied tools for server start/stop, initdb, etc.
  The majority of our audience is probably using a pre-packaged Postgres
 build rather than raw sources.  For them, much of runtime.sgml is not
 too relevant, and they should be reading the packager's docs instead.
 Add some notes pointing that way in appropriate places. 
 Text by me; thanks to Daniel Gustafsson for review and discussion,
 and to Laurenz Albe for an earlier version. 
 Discussion: https://postgr.es/m/
159430831443.16535.
11360317280100947016@wrigleys.postgresql.org  
  Peter Eisentraut [Thu, 3 Sep 2020 11:15:53 +0000 (13:15 +0200)]     doc: Make SQL command names in the catalog documentation links
 
 In passing, fix the initdb references to be <application> rather than
 <command>, which is what we normally use.
 
 Author: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
 Discussion: https://www.postgresql.org/message-id/flat/87mu5xqc11.fsf@wibble.ilmari.org
 
 
    Peter Eisentraut [Thu, 3 Sep 2020 11:15:53 +0000 (13:15 +0200)]     doc: Add missing cross-links in system catalog documentation
 
 This makes the first mention of a system catalog or view in each
 paragraph in the system system catalog and view documentation pages
 hyperlinks, for easier navigation.
 
 Also linkify the first mention of pg_hba.conf in pg_hba_file_rules, as
 that's more specific and easier to spot than the link to the client
 authentication chapter.
 
 Author: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
 Discussion: https://www.postgresql.org/message-id/flat/87mu5xqc11.fsf@wibble.ilmari.org
 
 
    Peter Eisentraut [Thu, 3 Sep 2020 10:38:32 +0000 (12:38 +0200)]     Fix XML id to match containing page
 
 This was apparently a typo when this part of the documentation was
 first added.
 
 
    Amit Kapila [Thu, 3 Sep 2020 02:24:07 +0000 (07:54 +0530)]     Add support for streaming to built-in logical replication.
  To add support for streaming of in-progress transactions into the
 built-in logical replication, we need to do three things: 
 * Extend the logical replication protocol, so identify in-progress
 transactions, and allow adding additional bits of information (e.g.
 XID of subtransactions). 
 * Modify the output plugin (pgoutput) to implement the new stream
 API callbacks, by leveraging the extended replication protocol. 
 * Modify the replication apply worker, to properly handle streamed
 in-progress transaction by spilling the data to disk and then
 replaying them on commit. 
 We however must explicitly disable streaming replication during
 replication slot creation, even if the plugin supports it. We
 don't need to replicate the changes accumulated during this phase,
 and moreover we don't have a replication connection open so we
 don't have where to send the data anyway. 
 Author: Tomas Vondra, Dilip Kumar and Amit Kapila 
Reviewed-by: Amit Kapila, Kuntal Ghosh and Ajin Cherian Tested-by: Neha Sharma, Mahendra Singh Thalor and Ajin Cherian Discussion: https://postgr.es/m/
688b0b7f-2f6c-d827-c27b-
216a8e3ea700@2ndquadrant.com  
  Tom Lane [Wed, 2 Sep 2020 22:23:56 +0000 (18:23 -0400)]     Add string_to_table() function.
 
 This splits a string at occurrences of a delimiter.  It is exactly like
 string_to_array() except for producing a set of values instead of an
 array of values.  Thus, the relationship of these two functions is
 the same as between regexp_split_to_table() and regexp_split_to_array().
 
 Although the same results could be had from unnest(string_to_array()),
 this is somewhat faster than that, and anyway it seems reasonable to
 have it for symmetry with the regexp functions.
 
 Pavel Stehule, reviewed by Peter Smith
 
 Discussion: https://postgr.es/m/CAFj8pRD8HOpjq2TqeTBhSo_QkzjLOhXzGCpKJ4nCs7Y9SQkuPw@mail.gmail.com
 
 
    Peter Eisentraut [Wed, 2 Sep 2020 13:17:33 +0000 (15:17 +0200)]     Remove unused parameter
  unused since 
39bd3fd1db6f3aa3764d4a1bebcd71c4e9c00281  Discussion: https://www.postgresql.org/message-id/flat/
511bb100-f829-ba21-2f10-
9f952ec06ead%402ndquadrant.com  
  Michael Paquier [Wed, 2 Sep 2020 07:59:22 +0000 (16:59 +0900)]     Add access method names to \d[i|m|t]+ in psql
 
 Listing a full set of relations with those psql meta-commands, without a
 matching pattern, has never showed the access method associated with
 each relation.  This commit adds the access method of tables, indexes
 and matviews, masking it for relation kinds where it does not apply.
 
 Note that when HIDE_TABLEAM is enabled, the information does not show
 up.  This is available when connecting to a backend version of at least
 12, where table AMs have been introduced.
 
 Author: Georgios Kokolatos
 Reviewed-by: Vignesh C, Michael Paquier, Justin Pryzby
 Discussion: https://postgr.es/m/svaS1VTOEscES9CLKVTeKItjJP1EEJuBhTsA0ESOdlnbXeQSgycYwVlliL5zt8Jwcfo4ATYDXtEqsExxjkSkkhCSTCL8fnRgaCAJdr0unUg=@protonmail.com
 
 
    Michael Paquier [Wed, 2 Sep 2020 05:56:59 +0000 (14:56 +0900)]     Fix thinko with definition of REINDEXOPT_MISSING_OK
  This had no direct consequences, but let's be consistent and it would be
 confusing when adding new flags.  Oversight in 
1d65416.  
Reported-by: Justin Pryzby Discussion: https://postgr.es/m/
20200902024148.GB20149@telsasoft.com  
  Fujii Masao [Wed, 2 Sep 2020 01:55:55 +0000 (10:55 +0900)]     Avoid unnecessary acquisition of SyncRepLock in transaction commit time.
  In SyncRepWaitForLSN() routine called in transaction commit time,
 SyncRepLock is necessary to atomically both check the shared
 sync_standbys_defined flag and operate the sync replication wait-queue.
 On the other hand, when the flag is false, the lock is not necessary
 because the wait-queue is not touched. But due to the changes by
 commit 
48c9f49265, previously the lock was taken whatever the flag was.
 This could cause unnecessary performance overhead in every transaction
 commit time. Therefore this commit avoids that unnecessary aquisition
 of SyncRepLock. 
 Author: Fujii Masao 
Reviewed-by: Asim Praveen, Masahiko Sawada, Discussion: https://postgr.es/m/
20200406050332.nsscfqjzk2d57zyx@alap3.anarazel.de  
  Alvaro Herrera [Wed, 2 Sep 2020 00:43:23 +0000 (20:43 -0400)]     Fix typo in comment
  Introduced by 
8b08f7d4820f; backpatch to 11. 
 Discussion: https://postgr.es/m/
20200812214918.GA30353@alvherre.pgsql  
  Michael Paquier [Wed, 2 Sep 2020 00:08:12 +0000 (09:08 +0900)]     Improve handling of dropped relations for REINDEX DATABASE/SCHEMA/SYSTEM
  When multiple relations are reindexed, a scan of pg_class is done first
 to build the list of relations to work on.  However the REINDEX logic
 has never checked if a relation listed still exists when beginning the
 work on it, causing for example sudden cache lookup failures. 
 This commit adds safeguards against dropped relations for REINDEX,
 similarly to VACUUM or CLUSTER where we try to open the relation,
 ignoring it if it is missing.  A new option is added to the REINDEX
 routines to control if a missed relation is OK to ignore or not. 
 An isolation test, based on REINDEX SCHEMA, is added for the concurrent
 and non-concurrent cases. 
 Author: Michael Paquier 
Reviewed-by: Anastasia Lubennikova Discussion: https://postgr.es/m/
20200813043805.GE11663@paquier.xyz  
  Tom Lane [Tue, 1 Sep 2020 22:40:37 +0000 (18:40 -0400)]     Improve test coverage of ginvacuum.c.
  Add a test case that exercises vacuum's deletion of empty GIN
 posting pages.  Since this is a temp table, it should now work
 reliably to delete a bunch of rows and immediately VACUUM.
 Before the preceding commit, this would not have had the desired
 effect, at least not in parallel regression tests. 
 Discussion: https://postgr.es/m/
3490536.
1598629609@sss.pgh.pa.us  
  Tom Lane [Tue, 1 Sep 2020 22:37:12 +0000 (18:37 -0400)]     Set cutoff xmin more aggressively when vacuuming a temporary table.
  Since other sessions aren't allowed to look into a temporary table
 of our own session, we do not need to worry about the global xmin
 horizon when setting the vacuum XID cutoff.  Indeed, if we're not
 inside a transaction block, we may set oldestXmin to be the next
 XID, because there cannot be any in-doubt tuples in a temp table,
 nor any tuples that are dead but still visible to some snapshot of
 our transaction.  (VACUUM, of course, is never inside a transaction
 block; but we need to test that because CLUSTER shares the same code.) 
 This approach allows us to always clean out a temp table completely
 during VACUUM, independently of concurrent activity.  Aside from
 being useful in its own right, that simplifies building reproducible
 test cases. 
 Discussion: https://postgr.es/m/
3490536.
1598629609@sss.pgh.pa.us  
  Bruce Momjian [Tue, 1 Sep 2020 21:00:10 +0000 (17:00 -0400)]     doc:  clarify that max_wal_size is "during" checkpoints
  Previous wording was "between".  
Reported-by: Pavel Luzanov Discussion: https://postgr.es/m/
26906a54-d7cb-2f8e-eed7-
e31660024694@postgrespro.ru 
 Backpatch-through: 9.5  
  Alvaro Herrera [Tue, 1 Sep 2020 17:40:43 +0000 (13:40 -0400)]     Raise error on concurrent drop of partitioned index
  We were already raising an error for DROP INDEX CONCURRENTLY on a
 partitioned table, albeit a different and confusing one:
   ERROR:  DROP INDEX CONCURRENTLY must be first action in transaction 
 Change that to throw a more comprehensible error:
   ERROR:  cannot drop partitioned index \"%s\" concurrently 
 Michael Paquier authored the test case for indexes on temporary
 partitioned tables. 
 Backpatch to 11, where indexes on partitioned tables were added.  
Reported-by: Jan Mussler <jan.mussler@zalando.de> Reviewed-by: Michael Paquier <michael@paquier.xyz> Discussion: https://postgr.es/m/16594-
d2956ca909585067@postgresql.org  
  Tom Lane [Tue, 1 Sep 2020 17:14:44 +0000 (13:14 -0400)]     Teach libpq to handle arbitrary-length lines in .pgpass files.
  Historically there's been a hard-wired assumption here that no line of
 a .pgpass file could be as long as NAMEDATALEN*5 bytes.  That's a bit
 shaky to start off with, because (a) there's no reason to suppose that
 host names fit in NAMEDATALEN, and (b) this figure fails to allow for
 backslash escape characters.  However, it fails completely if someone
 wants to use a very long password, and we're now hearing reports of
 people wanting to use "security tokens" that can run up to several
 hundred bytes.  Another angle is that the file is specified to allow
 comment lines, but there's no reason to assume that long comment lines
 aren't possible. 
 Rather than guessing at what might be a more suitable limit, let's
 replace the fixed-size buffer with an expansible PQExpBuffer.  That
 adds one malloc/free cycle to the typical use-case, but that's surely
 pretty cheap relative to the I/O this code has to do. 
 Also, add TAP test cases to exercise this code, because there was no
 test coverage before. 
 This reverts most of commit 
2eb3bc588, as there's no longer a need for
 a warning message about overlength .pgpass lines.  (I kept the explicit
 check for comment lines, though.) 
 In HEAD and v13, this also fixes an oversight in 
74a308cf5: there's not
 much point in explicit_bzero'ing the line buffer if we only do so in two
 of the three exit paths. 
 Back-patch to all supported branches, except that the test case only
 goes back to v10 where src/test/authentication/ was added. 
 Discussion: https://postgr.es/m/
4187382.
1598909041@sss.pgh.pa.us  
  Amit Kapila [Tue, 1 Sep 2020 02:41:39 +0000 (08:11 +0530)]     Fix the SharedFileSetUnregister API.
  Commit 
808e13b282 introduced a few APIs to extend the existing Buffile
 interface. In SharedFileSetDeleteOnProcExit, it tries to delete the list
 element while traversing the list with 'foreach' construct which makes the
 behavior of list traversal unpredictable. 
 Author: Amit Kapila 
Reviewed-by: Dilip Kumar Tested-by: Dilip Kumar and Neha Sharma Discussion: https://postgr.es/m/CAA4eK1JhLatVcQ2OvwA_3s0ih6Hx9+kZbq107cXVsSWWukH7vA@mail.gmail.com  
  Bruce Momjian [Mon, 31 Aug 2020 22:48:38 +0000 (18:48 -0400)]     doc:  document how the backup manifest is transferred
  Reported-by: Bernd Helmle Discussion: https://postgr.es/m/
31acf8b0f1f701d53245e0cae38abdf5c3a0d559.camel@oopsware.de 
 Backpatch-through: 13  
  Bruce Momjian [Mon, 31 Aug 2020 22:33:37 +0000 (18:33 -0400)]     doc: add commas after 'i.e.' and 'e.g.'
  This follows the American format,
 https://jakubmarian.com/comma-after-i-e-and-e-g/. There is no intention
 of requiring this format for future text, but making existing text
 consistent every few years makes sense. 
 Discussion: https://postgr.es/m/
20200825183619.GA22369@momjian.us 
 Backpatch-through: 9.5  
  Bruce Momjian [Mon, 31 Aug 2020 21:36:23 +0000 (17:36 -0400)]     pg_upgrade doc:  mention saving postgresql.conf.auto files
  Also mention files included by postgresql.conf.  
Reported-by: Álvaro Herrera Discussion: https://postgr.es/m/
08AD4526-75AB-457B-B2DD-
099663F28040@yesql.se 
 Backpatch-through: 9.5  
  Alvaro Herrera [Mon, 31 Aug 2020 21:04:40 +0000 (17:04 -0400)]     doc: Update partitioning limitation on BEFORE triggers
 
 Reported-by: Erwin Brandstetter <brsaweda@gmail.com>
 Discussion: https://postgr.es/m/CAGHENJ6Le7S3qJJx2TvWvTwRNS3N=BtoNeb7AF2rZvfNBMeQcg@mail.gmail.com
 
 
    Bruce Momjian [Mon, 31 Aug 2020 21:05:53 +0000 (17:05 -0400)]     docs:  in mapping SQL to C data types, timestamp isn't a pointer
  It is an int64.  
Reported-by: ajulien@shaktiware.fr Discussion: https://postgr.es/m/
159845038271.24995.
15682121015698255155@wrigleys.postgresql.org 
 Backpatch-through: 9.5  
  Bruce Momjian [Mon, 31 Aug 2020 20:59:59 +0000 (16:59 -0400)]     doc: cross-link file-fdw and CSV config log sections
  There is an file-fdw example that reads the server config file, so cross
 link them.  
Reported-by: Oleg Samoilov Discussion: https://postgr.es/m/
159800192078.2886.
10431506404995508950@wrigleys.postgresql.org 
 Backpatch-through: 9.5  
  Bruce Momjian [Mon, 31 Aug 2020 20:21:03 +0000 (16:21 -0400)]     docs:  clarify intermediate certificate creation instructions
  Specifically, explain the v3_ca openssl specification. 
 Discussion: https://postgr.es/m/
20200824175653.GA32411@momjian.us 
 Backpatch-through: 9.5  
  Bruce Momjian [Mon, 31 Aug 2020 19:23:19 +0000 (15:23 -0400)]     docs:  replace "stable storage" with "durable" in descriptions
  For PG, "durable storage" has a clear meaning, while "stable storage"
 does not, so use the former. 
 Discussion: https://postgr.es/m/
20200817165222.GA31806@momjian.us 
 Backpatch-through: 9.5  
  Bruce Momjian [Mon, 31 Aug 2020 17:58:00 +0000 (13:58 -0400)]     C comment:  remove mention of use of t_hoff WAL structure member
  Reported-by: Antonin Houska Discussion: https://postgr.es/m/21643.
1595353537@antos 
 Backpatch-through: 9.5  
  Bruce Momjian [Mon, 31 Aug 2020 17:49:17 +0000 (13:49 -0400)]     doc:  improve description of subscripting of arrays
  It wasn't clear the non-integers are cast to integers for subscripting,
 rather than throwing an error.  
Reported-by: sean@materialize.io Discussion: https://postgr.es/m/
159538675800.624.
7728794628229799531@wrigleys.postgresql.org 
 Backpatch-through: 9.5  
  Bruce Momjian [Mon, 31 Aug 2020 17:43:05 +0000 (13:43 -0400)]     docs:  improve 'capitals' inheritance example
  Adds constraints and improves wording.  
Reported-by: 2552891@gmail.com Discussion: https://postgr.es/m/
159586122762.680.
1361378513036616007@wrigleys.postgresql.org 
 Backpatch-through: 9.5  
  Bruce Momjian [Mon, 31 Aug 2020 17:20:04 +0000 (13:20 -0400)]     doc:  clarify the useful features of procedures
 
 This was not clearly documented when procedures were added in PG 11.
 
 Reported-by: Robin Abbi
 Discussion: https://postgr.es/m/CAGmg_NX327KKVuJmbWZD=pGutYFxzZjX1rU+3ji8UuX=8ONn9Q@mail.gmail.com
 
 Backpatch-through: 11
 
 
    Magnus Hagander [Mon, 31 Aug 2020 11:03:54 +0000 (13:03 +0200)]     Fix docs bug stating file_fdw requires absolute paths
 
 It has always (since the first commit) worked with relative paths, so
 use the same wording as other parts of the documentation.
 
 Author: Bruce Momjian
 Discussion: https://postgr.es/m/CABUevExx-hm=cit+A9LeKBH39srvk8Y2tEZeEAj5mP8YfzNKUg@mail.gmail.com
 
 
    Tom Lane [Sun, 30 Aug 2020 18:37:24 +0000 (14:37 -0400)]     Mark factorial operator, and postfix operators in general, as deprecated.
  Per discussion, we're planning to remove parser support for postfix
 operators in order to simplify the grammar.  So it behooves us to
 put out a deprecation notice at least one release before that. 
 There is only one built-in postfix operator, ! for factorial.
 Label it deprecated in the docs and in pg_description, and adjust
 some examples that formerly relied on it.  (The sister prefix
 operator !! is also deprecated.  We don't really have to remove
 that one, but since we're suggesting that people use factorial()
 instead, it seems better to remove both operators.) 
 Also state in the CREATE OPERATOR ref page that postfix operators
 in general are going away. 
 Although this changes the initial contents of pg_description,
 I did not force a catversion bump; it doesn't seem essential. 
 In v13, also back-patch 
4c5cf5431, so that there's someplace for
 the <link>s to point to. 
 Mark Dilger and John Naylor, with some adjustments by me 
 Discussion: https://postgr.es/m/
BE2DF53D-251A-4E26-972F-
930E523580E9@enterprisedb.com  
  Tom Lane [Sun, 30 Aug 2020 16:21:51 +0000 (12:21 -0400)]     Redefine pg_class.reltuples to be -1 before the first VACUUM or ANALYZE.
  Historically, we've considered the state with relpages and reltuples
 both zero as indicating that we do not know the table's tuple density.
 This is problematic because it's impossible to distinguish "never yet
 vacuumed" from "vacuumed and seen to be empty".  In particular, a user
 cannot use VACUUM or ANALYZE to override the planner's normal heuristic
 that an empty table should not be believed to be empty because it is
 probably about to get populated.  That heuristic is a good safety
 measure, so I don't care to abandon it, but there should be a way to
 override it if the table is indeed intended to stay empty. 
 Hence, represent the initial state of ignorance by setting reltuples
 to -1 (relpages is still set to zero), and apply the minimum-ten-pages
 heuristic only when reltuples is still -1.  If the table is empty,
 VACUUM or ANALYZE (but not CREATE INDEX) will override that to
 reltuples = relpages = 0, and then we'll plan on that basis. 
 This requires a bunch of fiddly little changes, but we can get rid of
 some ugly kluges that were formerly needed to maintain the old definition. 
 One notable point is that FDWs' GetForeignRelSize methods will see
 baserel->tuples = -1 when no ANALYZE has been done on the foreign table.
 That seems like a net improvement, since those methods were formerly
 also in the dark about what baserel->tuples = 0 really meant.  Still,
 it is an API change. 
 I bumped catversion because code predating this change would get confused
 by seeing reltuples = -1. 
 Discussion: https://postgr.es/m/
F02298E0-6EF4-49A1-BCB6-
C484794D9ACC@thebuild.com  
  Michael Paquier [Sun, 30 Aug 2020 05:14:34 +0000 (14:14 +0900)]     Reset indisreplident for an invalid index in DROP INDEX CONCURRENTLY
  A failure when dropping concurrently an index used in a replica identity
 could leave in pg_index an index marked as !indisvalid and
 indisreplident.  Reindexing this index would switch back indisvalid to
 true, and if the replica identity of the parent relation was switched to
 use a different index, it would be possible to finish with more than one
 index marked as indisreplident.  If that were to happen, this could mess
 up with the relation cache as an incorrect index could be used for the
 replica identity. 
 Indexes marked as invalid are discarded as candidates for the replica
 identity, as of RelationGetIndexList(), so similarly to what is done
 with indisclustered, resetting indisreplident when the index is marked
 as invalid keeps things consistent.  REINDEX CONCURRENTLY's swapping
 already resets the flag for the old index, while the new index inherits
 the value of the old index to-be-dropped, so only DROP INDEX was an
 issue. 
 Even if this is a bug, the sequence able to reproduce a problem requires
 a failure while running DROP INDEX CONCURRENTLY, something unlikely
 going to happen in the field, so no backpatch is done. 
 Author: Michael Paquier 
Reviewed-by: Dmitry Dolgov Discussion: https://postgr.es/m/
20200827025721.GN2017@paquier.xyz  
  Michael Paquier [Fri, 28 Aug 2020 07:54:59 +0000 (16:54 +0900)]     doc: Rework tables for built-in operator classes of index AMs
  The tables listing all the operator classes available for BRIN, GIN,
 GiST and SP-GiST had a confusing format where the same operator could be
 listed multiple times, for different data types.  This improves the
 shape of these tables by adding the types associated to each operator,
 for their associated operator class. 
 Each table included previously the data type that could be used for an
 operator class in an extra column.  This is removed to reduce the width
 of the tables as this is now described within each operator.  This also
 makes the tables fit better in the PDF documentation.  
Reported-by: osdba Author: Michael Paquier 
Reviewed-by: Álvaro Herrera, Tom Lane, Bruce Momjian Discussion: https://postgr.es/m/
38d55061.9604.
173b32c60ec.Coremail.mailtch@163.com  
  Peter Eisentraut [Fri, 28 Aug 2020 06:19:12 +0000 (08:19 +0200)]     doc: Update cracklib URL
  Author: Daniel Gustafsson <daniel@yesql.se> 
Reviewed-by: Laurenz Albe <laurenz.albe@cybertec.at> Discussion: https://www.postgresql.org/message-id/flat/
f7266133-618a-0adc-52ef-
f43c78806b0e%402ndquadrant.com  
  Peter Eisentraut [Fri, 28 Aug 2020 06:16:32 +0000 (08:16 +0200)]     passwordcheck: Log cracklib diagnostics
  When calling cracklib to check the password, the diagnostic from
 cracklib was thrown away.  This would hide essential information such
 as no dictionary being installed.  Change this to show the cracklib
 error message using errdetail_log().  
Reviewed-by: Daniel Gustafsson <daniel@yesql.se> Reviewed-by: Laurenz Albe <laurenz.albe@cybertec.at> Discussion: https://www.postgresql.org/message-id/flat/
f7266133-618a-0adc-52ef-
f43c78806b0e%402ndquadrant.com  
  Tom Lane [Thu, 27 Aug 2020 21:36:13 +0000 (17:36 -0400)]     Fix code for re-finding scan position in a multicolumn GIN index.
  collectMatchBitmap() needs to re-find the index tuple it was previously
 looking at, after transiently dropping lock on the index page it's on.
 The tuple should still exist and be at its prior position or somewhere
 to the right of that, since ginvacuum never removes tuples but
 concurrent insertions could add one.  However, there was a thinko in
 that logic, to the effect of expecting any inserted tuples to have the
 same index "attnum" as what we'd been scanning.  Since there's no
 physical separation of tuples with different attnums, it's not terribly
 hard to devise scenarios where this fails, leading to transient "lost
 saved point in index" errors.  (While I've duplicated this with manual
 testing, it seems impossible to make a reproducible test case with our
 available testing technology.) 
 Fix by just continuing the scan when the attnum doesn't match. 
 While here, improve the error message used if we do fail, so that it
 matches the wording used in btree for a similar case. 
 collectMatchBitmap()'s posting-tree code path was previously not
 exercised at all by our regression tests.  While I can't make
 a regression test that exhibits the bug, I can at least improve
 the code coverage here, so do that.  The test case I made for this
 is an extension of one added by 
4b754d6c1, so it only works in
 HEAD and v13; didn't seem worth trying hard to back-patch it. 
 Per bug #16595 from Jesse Kinkead.  This has been broken since
 multicolumn capability was added to GIN (commit 
27cb66fdf),
 so back-patch to all supported branches. 
 Discussion: https://postgr.es/m/16595-
633118be8eef9ce2@postgresql.org  
  Michael Paquier [Thu, 27 Aug 2020 07:40:34 +0000 (16:40 +0900)]     Fix comment in procarray.c
  The description of GlobalVisDataRels was missing, GlobalVisCatalogRels
 being mentioned instead. 
 Author: Jim Nasby
 Discussion: https://postgr.es/m/
8e06c883-2858-1fd4-07c5-
560c28b08dcd@amazon.com  
  Tom Lane [Wed, 26 Aug 2020 21:08:11 +0000 (17:08 -0400)]     Suppress compiler warning in non-cassert builds.
  Oversight in 
808e13b28, reported by Bruce Momjian. 
 Discussion: https://postgr.es/m/
20200826160251.GB21909@momjian.us  
  Michael Paquier [Wed, 26 Aug 2020 11:42:27 +0000 (20:42 +0900)]     Add regression tests for REPLICA IDENTITY with dropped indexes
  REPLICA IDENTITY USING INDEX behaves the same way as NOTHING if the
 associated index is dropped, even if there is a primary key that could
 be used as a fallback for the changes generated.  There have never been
 any tests to cover such scenarios, so this commit closes the gap. 
 Author: Michael Paquier 
Reviewed-by: Masahiko Sawada, Rahila Syed, Euler Taveira Discussion: https://postgr.es/m/
20200522035028.GO2355@paquier.xyz  
  Amit Kapila [Wed, 26 Aug 2020 04:10:52 +0000 (09:40 +0530)]     Add additional information in the vacuum error context.
 
 The additional information added will be an offset number for heap
 operations. This information will help us in finding the exact tuple due
 to which the error has occurred.
 
 Author: Mahendra Singh Thalor and Amit Kapila
 Reviewed-by: Sawada Masahiko, Justin Pryzby and Amit Kapila
 Discussion: https://postgr.es/m/CAKYtNApK488TDF4bMbw+1QH8HJf9cxdNDXquhU50TK5iv_FtCQ@mail.gmail.com
 
 
    Amit Kapila [Wed, 26 Aug 2020 02:06:43 +0000 (07:36 +0530)]     Extend the BufFile interface.
  Allow BufFile to support temporary files that can be used by the single
 backend when the corresponding files need to be survived across the
 transaction and need to be opened and closed multiple times. Such files
 need to be created as a member of a SharedFileSet. 
 Additionally, this commit implements the interface for BufFileTruncate to
 allow files to be truncated up to a particular offset and extends the
 BufFileSeek API to support the SEEK_END case. This also adds an option to
 provide a mode while opening the shared BufFiles instead of always opening
 in read-only mode. 
 These enhancements in BufFile interface are required for the upcoming
 patch to allow the replication apply worker, to handle streamed
 in-progress transactions. 
 Author: Dilip Kumar, Amit Kapila 
Reviewed-by: Amit Kapila Tested-by: Neha Sharma Discussion: https://postgr.es/m/
688b0b7f-2f6c-d827-c27b-
216a8e3ea700@2ndquadrant.com  
  Fujii Masao [Wed, 26 Aug 2020 01:52:02 +0000 (10:52 +0900)]     Add regression test for pg_backend_memory_contexts.
  Author: Atsushi Torikoshi 
Reviewed-by: Michael Paquier, Fujii Masao Discussion: https://postgr.es/m/
20200819135545.GC19121@paquier.xyz  
  Fujii Masao [Wed, 26 Aug 2020 01:51:31 +0000 (10:51 +0900)]     Move codes for pg_backend_memory_contexts from mmgr/mcxt.c to adt/mcxtfuncs.c.
  Previously the codes for pg_backend_memory_contexts were in
 src/backend/utils/mmgr/mcxt.c. This commit moves them to
 src/backend/utils/adt/mcxtfuncs.c so that mcxt.c basically includes
 only the low-level interface for memory contexts. 
 Author: Atsushi Torikoshi 
Reviewed-by: Michael Paquier, Fujii Masao Discussion: https://postgr.es/m/
20200819135545.GC19121@paquier.xyz  
  Fujii Masao [Wed, 26 Aug 2020 01:50:02 +0000 (10:50 +0900)]     Prevent non-superusers from reading pg_backend_memory_contexts, by default.
  pg_backend_memory_contexts view contains some internal information of
 memory contexts. Since exposing them to any users by default may cause
 security issue, this commit allows only superusers to read this view,
 by default, like we do for pg_shmem_allocations view. 
 Bump catalog version. 
 Author: Atsushi Torikoshi 
Reviewed-by: Michael Paquier, Fujii Masao Discussion: https://postgr.es/m/
1414992.
1597849297@sss.pgh.pa.us  
  David Rowley [Tue, 25 Aug 2020 22:51:36 +0000 (10:51 +1200)]     Fixup some misusages of bms_num_members()
 
 It's a bit inefficient to test if a Bitmapset is empty by counting all the
 members and seeing if that number is zero. It's much better just to use
 bms_is_empty().  Likewise for checking if there are at least two members,
 just use bms_membership(), which does not need to do anything more after
 finding two members.
 
 Discussion: https://postgr.es/m/CAApHDvpvwm_QjbDOb5xga%2BKmX9XkN9xQavNGm3SvDbVnCYOerQ%40mail.gmail.com
 Reviewed-by: Tomas Vondra
 
    Bruce Momjian [Tue, 25 Aug 2020 13:53:12 +0000 (09:53 -0400)]     docs:  client certificates are always sent to the server
  They are not "requested" by the server.  
Reported-by: Kyotaro Horiguchi Discussion: https://postgr.es/m/
20200825.155320.
986648039251743210.horikyota.ntt@gmail.com 
 Backpatch-through: 9.5  
  Peter Eisentraut [Tue, 25 Aug 2020 05:29:05 +0000 (07:29 +0200)]     doc: Fix up title case
 
 This fixes some instances that were missed in earlier processings and
 that now look a bit strange because they are inconsistent with nearby
 titles.
 
 
    Michael Paquier [Mon, 24 Aug 2020 07:46:52 +0000 (16:46 +0900)]     doc: Fix some markups for support functions of index AMs
  All the documentation of index AMs has been using <replaceable> for
 local_relopts.  This is a structure, so <structname> is a much better
 choice. 
 Alexander has found the inconsistency for btree, while I have spotted
 the rest when applying the concept of consistency to the docs. 
 Author: Alexander Lakhin, Michael Paquier 
Reviewed-by: Tom Lane Discussion: https://postgr.es/m/
20200822133022.GC24782@paquier.xyz  
  Amit Kapila [Mon, 24 Aug 2020 02:46:19 +0000 (08:16 +0530)]     Improve the vacuum error context phase information.
 
 We were displaying the wrong phase information for 'info' message in the
 index clean up phase because we were switching to the previous phase a bit
 early. We were also not displaying context information for heap phase
 unless the block number is valid which is fine for error cases but for
 messages at 'info' or lower error level it appears to be inconsistent with
 index phase information.
 
 Reported-by: Sawada Masahiko
 Author: Sawada Masahiko
 Reviewed-by: Amit Kapila
 Backpatch-through: 13, where it was introduced
 Discussion: https://postgr.es/m/CA+fd4k4HcbhPnCs7paRTw1K-AHin8y4xKomB9Ru0ATw0UeTy2w@mail.gmail.com
 
 
    Tom Lane [Sat, 22 Aug 2020 18:46:40 +0000 (14:46 -0400)]     Avoid pushing quals down into sub-queries that have grouping sets.
  The trouble with doing this is that an apparently-constant subquery
 output column isn't really constant if it is a grouping column that
 appears in only some of the grouping sets.  A qual using such a
 column would be subject to incorrect const-folding after push-down,
 as seen in bug #16585 from Paul Sivash. 
 To fix, just disable qual pushdown altogether if the sub-query has
 nonempty groupingSets.  While we could imagine far less restrictive
 solutions, there is not much point in working harder right now,
 because subquery_planner() won't move HAVING clauses to WHERE within
 such a subquery.  If the qual stays in HAVING it's not going to be
 a lot more useful than if we'd kept it at the outer level. 
 Having said that, this restriction could be removed if we used a
 parsetree representation that distinguished such outputs from actual
 constants, which is something I hope to do in future.  Hence, make
 the patch a minimal addition rather than integrating it more tightly
 (e.g. by renumbering the existing items in subquery_is_pushdown_safe's
 comment). 
 Back-patch to 9.5 where grouping sets were introduced. 
 Discussion: https://postgr.es/m/16585-
9d8c340d23ade8c1@postgresql.org  
  Tom Lane [Sat, 22 Aug 2020 16:34:17 +0000 (12:34 -0400)]     Fix ALTER TABLE's scheduling rules for AT_AddConstraint subcommands.
  Commit 
1281a5c90 rearranged the logic in this area rather drastically,
 and it broke the case of adding a foreign key constraint in the same
 ALTER that adds the pkey or unique constraint it depends on.  While
 self-referential fkeys are surely a pretty niche case, this used to
 work so we shouldn't break it. 
 To fix, reorganize the scheduling rules in ATParseTransformCmd so
 that a transformed AT_AddConstraint subcommand will be delayed into
 a later pass in all cases, not only when it's been spit out as a
 side-effect of parsing some other command type. 
 Also tweak the logic so that we won't run ATParseTransformCmd twice
 while doing this.  It seems to work even without that, but it's
 surely wasting cycles to do so. 
 Per bug #16589 from Jeremy Evans.  Back-patch to v13 where the new
 code was introduced. 
 Discussion: https://postgr.es/m/16589-
31c8d981ca503896@postgresql.org  
  Michael Paquier [Sat, 22 Aug 2020 13:26:10 +0000 (22:26 +0900)]     doc: Fix format, incorrect structure names and markup inconsistencies
  Author: Alexander Lakhin
 Discussion: https://postgr.es/m/
a2345841-10a5-4eef-257c-
02302347cf39@gmail.com
 Backpatch-through: 13  
  Bruce Momjian [Sat, 22 Aug 2020 00:23:09 +0000 (20:23 -0400)]     docs:  improve description of how to handle multiple databases
  This is a redesign of the intro to the managing databases chapter. 
 Discussion: https://postgr.es/m/
159586122762.680.
1361378513036616007@wrigleys.postgresql.org 
 Author: David G. Johnston 
 Backpatch-through: 9.5  
  Bruce Momjian [Fri, 21 Aug 2020 22:29:37 +0000 (18:29 -0400)]     docs:  add COMMENT examples for new features, rename rtree
  Reported-by: Jürgen Purtz Discussion: https://postgr.es/m/
15ec5428-d46a-1725-f38d-
44986a977abb@purtz.de 
 Author: Jürgen Purtz 
 Backpatch-through: 11  
  Tom Lane [Fri, 21 Aug 2020 19:00:42 +0000 (15:00 -0400)]     Fix handling of CREATE TABLE LIKE with inheritance.
  If a CREATE TABLE command uses both LIKE and traditional inheritance,
 Vars in CHECK constraints and expression indexes that are absorbed
 from a LIKE parent table tended to get mis-numbered, resulting in
 wrong answers and/or bizarre error messages (though probably not any
 actual crashes, thanks to validation occurring in the executor). 
 In v12 and up, the same could happen to Vars in GENERATED expressions,
 even in cases with no LIKE clause but multiple traditional-inheritance
 parents. 
 The cause of the problem for LIKE is that parse_utilcmd.c supposed
 it could renumber such Vars correctly during transformCreateStmt(),
 which it cannot since we have not yet accounted for columns added via
 inheritance.  Fix that by postponing processing of LIKE INCLUDING
 CONSTRAINTS, DEFAULTS, GENERATED, INDEXES till after we've performed
 DefineRelation(). 
 The error with GENERATED and multiple inheritance is a simple oversight
 in MergeAttributes(); it knows it has to renumber Vars in inherited
 CHECK constraints, but forgot to apply the same processing to inherited
 GENERATED expressions (a/k/a defaults). 
 Per bug #16272 from Tom Gottfried.  The non-GENERATED variants of the
 issue are ancient, presumably dating right back to the addition of
 CREATE TABLE LIKE; hence back-patch to all supported branches. 
 Discussion: https://postgr.es/m/16272-
6e32da020e9a9381@postgresql.org  
  Fujii Masao [Fri, 21 Aug 2020 16:22:55 +0000 (01:22 +0900)]     Fix explain regression test failure.
  Commit 
9d701e624f caused the regression test for EXPLAIN to fail on
 the buildfarm member prion. This happened because of instability of
 test output, i.e., in text format, whether "Planning:" line is output
 varies depending on the system state. 
 This commit updated the regression test so that it ignores that
 "Planning:" line to produce more stable test output and get rid of
 the test failure. 
 Back-patch to v13. 
 Author: Fujii Masao
 Discussion: https://postgr.es/m/
1803897.
1598021621@sss.pgh.pa.us  
  Fujii Masao [Fri, 21 Aug 2020 11:48:59 +0000 (20:48 +0900)]     Rework EXPLAIN for planner's buffer usage.
  Commit 
ce77abe63c allowed EXPLAIN (BUFFERS) to report the information
 on buffer usage during planning phase. However three issues were
 reported regarding this feature. 
 (1) Previously, EXPLAIN option BUFFERS required ANALYZE. So the query
     had to be actually executed by specifying ANALYZE even when we
     want to see only the planner's buffer usage. This was inconvenient
     especially when the query was write one like DELETE. 
 (2) EXPLAIN included the planner's buffer usage in summary
     information. So SUMMARY option had to be enabled to report that.
     Also this format was confusing. 
 (3) The output structure for planning information was not consistent
     between TEXT format and the others. For example, "Planning" tag
     was output in JSON format, but not in TEXT format. 
 For (1), this commit allows us to perform EXPLAIN (BUFFERS) without
 ANALYZE to report the planner's buffer usage. 
 For (2), this commit changed EXPLAIN output so that the planner's
 buffer usage is reported before summary information. 
 For (3), this commit made the output structure for planning
 information more consistent between the formats. 
 Back-patch to v13 where the planner's buffer usage was allowed to
 be reported in EXPLAIN.  
Reported-by: Pierre Giraud, David Rowley Author: Fujii Masao 
Reviewed-by: David Rowley, Julien Rouhaud, Pierre Giraud Discussion: https://postgr.es/m/
07b226e6-fa49-687f-b110-
b7c37572f69e@dalibo.com  
  Fujii Masao [Fri, 21 Aug 2020 03:33:30 +0000 (12:33 +0900)]     Fix typos in comments.
 
 Author: Masahiko Sawada
 Reviewed-by: Fujii Masao
 Discussion: https://postgr.es/m/CA+fd4k4m9hFSrRLB3etPWO5_v5=MujVZWRtz63q+55hM0Dz25Q@mail.gmail.com
 
 
    David Rowley [Thu, 20 Aug 2020 21:33:56 +0000 (09:33 +1200)]     Fix a few typos in JIT comments and README
 
 Reviewed-by: Abhijit Menon-Sen
 Reviewed-by: Andres Freund
 Discussion: https://postgr.es/m/CAApHDvobgmCs6CohqhKTUf7D8vffoZXQTCBTERo9gbOeZmvLTw%40mail.gmail.com
 Backpatch-through: 11, where JIT was added
 
 
    Andres Freund [Thu, 20 Aug 2020 19:59:00 +0000 (12:59 -0700)]     Revert "Make vacuum a bit more verbose to debug BF failure."
  This reverts commit 
49967da65aec970fcda123acc681f1df5d70bfc6. 
 Enough time has passed that we can be confident that 
07f32fcd23a resolved the issue. Therefore we can remove the temporary debugging
 aids. 
 Author: Andres Freund <andres@anarazel.de>
 Discussion: https://postgr.es/m/E1k7tGP-0005V0-5k@gemulon.postgresql.org  
  Alvaro Herrera [Thu, 20 Aug 2020 17:49:04 +0000 (13:49 -0400)]     Revise REINDEX CONCURRENTLY recovery instructions
  When the leftover invalid index is "ccold", there's no need to re-run
 the command.  Reword the instructions to make that explicit. 
 Backpatch to 12, where REINDEX CONCURRENTLY appeared. 
 Author: Álvaro Herrera <alvherre@alvh.no-ip.org> 
Reviewed-by: Michael Paquier <michael@paquier.xyz> Reviewed-by: Julien Rouhaud <rjuju123@gmail.com> Discussion: https://postgr.es/m/
20200819211312.GA15497@alvherre.pgsql  
  Andres Freund [Thu, 20 Aug 2020 01:19:52 +0000 (18:19 -0700)]     Acquire ProcArrayLock exclusively in ProcArrayClearTransaction.
  This corrects an oversight by me in 
20729324078, which made
 ProcArrayClearTransaction() increment xactCompletionCount. That requires an
 exclusive lock, obviously. 
 There's other approaches that avoid the exclusive acquisition, but given that a
 2PC commit is fairly heavyweight, it doesn't seem worth doing so. I've not been
 able to measure a performance difference, unsurprisingly.  I did add a
 comment documenting that we could do so, should it ever become a bottleneck.  
Reported-By: Tom Lane <tgl@sss.pgh.pa.us> Author: Andres Freund <andres@anarazel.de>
 Discussion: https://postgr.es/m/
1355915.
1597794204@sss.pgh.pa.us  
  Tom Lane [Wed, 19 Aug 2020 18:07:49 +0000 (14:07 -0400)]     Suppress unnecessary RelabelType nodes in yet more cases.
  Commit 
a477bfc1d fixed eval_const_expressions() to ensure that it
 didn't generate unnecessary RelabelType nodes, but I failed to notice
 that some other places in the planner had the same issue.  Really
 noplace in the planner should be using plain makeRelabelType(), for
 fear of generating expressions that should be equal() to semantically
 equivalent trees, but aren't. 
 An example is that because canonicalize_ec_expression() failed
 to be careful about this, we could end up with an equivalence class
 containing both a plain Const, and a Const-with-RelabelType
 representing exactly the same value.  So far as I can tell this led to
 no visible misbehavior, but we did waste a bunch of cycles generating
 and evaluating "Const = Const-with-RelabelType" to prove such entries
 are redundant. 
 Hence, move the support function added by 
a477bfc1d to where it can
 be more generally useful, and use it in the places where planner code
 previously used makeRelabelType. 
 Back-patch to v12, like the previous patch.  While I have no concrete
 evidence of any real misbehavior here, it's certainly possible that
 I overlooked a case where equivalent expressions that aren't equal()
 could cause a user-visible problem.  In any case carrying extra
 RelabelType nodes through planning to execution isn't very desirable. 
 Discussion: https://postgr.es/m/
1311836.
1597781384@sss.pgh.pa.us  
  Fujii Masao [Wed, 19 Aug 2020 06:34:43 +0000 (15:34 +0900)]     Add pg_backend_memory_contexts system view.
  This view displays the usages of all the memory contexts of the server
 process attached to the current session. This information is useful to
 investigate the cause of backend-local memory bloat. 
 This information can be also collected by calling
 MemoryContextStats(TopMemoryContext) via a debugger. But this technique
 cannot be uesd in some environments because no debugger is available there.
 And it outputs lots of text messages and it's not easy to analyze them.
 So, pg_backend_memory_contexts view allows us to access to backend-local
 memory contexts information more easily. 
 Bump catalog version. 
 Author: Atsushi Torikoshi, Fujii Masao 
Reviewed-by: Tatsuhito Kasahara, Andres Freund, Daniel Gustafsson, Robert Haas, Michael Paquier Discussion: https://postgr.es/m/
72a656e0f71d0860161e0b3f67e4d771@oss.nttdata.com  
  Andres Freund [Tue, 18 Aug 2020 23:31:12 +0000 (16:31 -0700)]     Fix race condition in snapshot caching when 2PC is used.
 
 When preparing a transaction xactCompletionCount needs to be
 incremented, even though the transaction has not committed
 yet. Otherwise the snapshot used within the transaction otherwise can
 get reused outside of the prepared transaction. As GetSnapshotData()
 does not include the current xid when building a snapshot, reuse would
 not be correct.
 
 Somewhat surprisingly the regression tests only rarely show incorrect
 results without the fix. The reason for that is that often the
 snapshot's xmax will be >= the backend xid, yielding a snapshot that
 is correct, despite the bug.
 
 I'm working on a reliable test for the bug, but it seems worth seeing
 whether this fixes all the BF failures while I do.
 
 Author: Andres Freund <andres@anarazel.de>
 Discussion: https://postgr.es/m/E1k7tGP-0005V0-5k@gemulon.postgresql.org
 
 
    Heikki Linnakangas [Tue, 18 Aug 2020 10:13:09 +0000 (13:13 +0300)]     Avoid non-constant format string argument to fprintf().
  As Tom Lane pointed out, it could defeat the compiler's printf() format
 string verification. 
 Backpatch to v12, like that patch that introduced it. 
 Discussion: https://www.postgresql.org/message-id/
1069283.
1597672779%40sss.pgh.pa.us  
  Andres Freund [Tue, 18 Aug 2020 04:07:10 +0000 (21:07 -0700)]     snapshot scalability: cache snapshots using a xact completion counter.
  Previous commits made it faster/more scalable to compute snapshots. But not
 building a snapshot is still faster. Now that GetSnapshotData() does not
 maintain RecentGlobal* anymore, that is actually not too hard: 
 This commit introduces xactCompletionCount, which tracks the number of
 top-level transactions with xids (i.e. which may have modified the database)
 that completed in some form since the start of the server. 
 We can avoid rebuilding the snapshot's contents whenever the current
 xactCompletionCount is the same as it was when the snapshot was
 originally built.  Currently this check happens while holding
 ProcArrayLock. While it's likely possible to perform the check without
 acquiring ProcArrayLock, it seems better to do that separately /
 later, some careful analysis is required. Even with the lock this is a
 significant win on its own. 
 On a smaller two socket machine this gains another ~1.03x, on a larger
 machine the effect is roughly double (earlier patch version tested
 though).  If we were able to safely avoid the lock there'd be another
 significant gain on top of that. 
 Author: Andres Freund <andres@anarazel.de> 
Reviewed-By: Robert Haas <robertmhaas@gmail.com> Reviewed-By: Thomas Munro <thomas.munro@gmail.com> Reviewed-By: David Rowley <dgrowleyml@gmail.com> Discussion: https://postgr.es/m/
20200301083601.ews6hz5dduc3w2se@alap3.anarazel.de  
  Michael Paquier [Tue, 18 Aug 2020 03:24:22 +0000 (12:24 +0900)]     Fix use-after-release issue in PL/Sample
  Introduced in 
adbe62d0.  Per buildfarm member prion, when using
 RELCACHE_FORCE_RELEASE.  
  Michael Paquier [Tue, 18 Aug 2020 02:10:50 +0000 (11:10 +0900)]     Add PL/Sample to src/test/modules/
  PL/Sample is an example template of procedural-language handler.  This
 can be used as a base to implement a custom PL, or as a facility to test
 APIs dedicated to PLs.  Much more could be done in this module, like
 adding a simple validator, but this is left as future work. 
 The documentation included originally some C code to understand the
 basics of PL handler implementation, but it was outdated, and not really
 helpful either if trying to implement a new procedural language,
 particularly when it came to the integration of a PL installation with
 CREATE EXTENSION. 
 Author: Mark Wong 
Reviewed-by: Tom Lane, Michael Paquier Discussion: https://postgr.es/m/
20200612172648.GA3327@2ndQuadrant.com  
  Alvaro Herrera [Mon, 17 Aug 2020 20:20:06 +0000 (16:20 -0400)]     Disable autovacuum for BRIN test table
  This should improve stability in the tests. 
 Per buildfarm member hyrax (CLOBBER_CACHE_ALWAYS) via Tom Lane. 
 Discussion: https://postgr.es/m/871534.
1597503261@sss.pgh.pa.us  
  Tom Lane [Mon, 17 Aug 2020 19:40:07 +0000 (15:40 -0400)]     Doc: fix description of UNION/CASE/etc type unification.
  The description of what select_common_type() does was not terribly
 accurate.  Improve it. 
 David Johnston and Tom Lane 
 Discussion: https://postgr.es/m/
1019930.
1597613200@sss.pgh.pa.us  
  Heikki Linnakangas [Mon, 17 Aug 2020 07:52:58 +0000 (10:52 +0300)]     Mark commit and abort WAL records with XLR_SPECIAL_REL_UPDATE.
  If a commit or abort record includes "dropped relfilenodes", then replaying
 the record will remove data files. That is surely a "special rel update",
 but the records were not marked as such. Fix that, teach pg_rewind to
 expect and ignore them, and add a test case to cover it. 
 It's always been like this, but no backporting for fear of breaking
 existing applications. If an application parsed the WAL but was not
 handling commit/abort records, it would stop working. That might be a good
 thing if it really needed to handle the dropped rels, but it will be caught
 when the application is updated to work with PostgreSQL v14 anyway. 
 Discussion: https://www.postgresql.org/message-id/
07b33e2c-46a6-86a1-5f9e-
a7da73fddb95%40iki.fi 
Reviewed-by: Amit Kapila, Michael Paquier   Heikki Linnakangas [Mon, 17 Aug 2020 07:50:13 +0000 (10:50 +0300)]     Make xact.h usable in frontend.
 
 xact.h included utils/datetime.h, which cannot be used in the frontend
 (it includes fmgr.h, which needs Datum). But xact.h only needs the
 definition of TimestampTz from it, which is available directly in
 datatypes/timestamp.h. Change xact.h to include that instead of
 utils/datetime.h, so that it can be used in client programs.
 
 
    Heikki Linnakangas [Mon, 17 Aug 2020 06:27:29 +0000 (09:27 +0300)]     Fix printing last progress report line in client programs.
  A number of client programs have a "--progress" option that when printing
 to a TTY, updates the current line by printing a '\r' and overwriting it.
 After the last line, '\n' needs to be printed to move the cursor to the
 next line. pg_basebackup and pgbench got this right, but pg_rewind and
 pg_checksums were slightly wrong. pg_rewind printed the newline to stdout
 instead of stderr, and pg_checksums printed the newline even when not
 printing to a TTY. Fix them, and also add a 'finished' argument to
 pg_basebackup's progress_report() function, to keep it consistent with
 the other programs. 
 Backpatch to v12. pg_rewind's newline was broken with the logging changes
 in commit 
cc8d415117 in v12, and pg_checksums was introduced in v12. 
 Discussion: https://www.postgresql.org/message-id/
82b539e5-ae33-34b0-1aee-
22b3379fd3eb@iki.fi  
  Michael Paquier [Mon, 17 Aug 2020 01:23:17 +0000 (10:23 +0900)]     doc: Fix description about bgwriter and checkpoint in HA section
  Since 
806a2ae, the work of the bgwriter is split the checkpointer, but a
 portion of the documentation did not get the message. 
 Author: Masahiko Sawada
 Discussion: https://postgr.es/m/CA+fd4k6jXxjAtjMVC=wG3=QGpauZBtcgN3Jhw+oV7zXGKVLKzQ@mail.gmail.com
 Backpatch-through: 9.5  
  Andres Freund [Sun, 16 Aug 2020 21:21:37 +0000 (14:21 -0700)]     Fix use of wrong index in ComputeXidHorizons().
  This bug, recently introduced in 
941697c3c1a, at least lead to vacuum
 failing because it found tuples inserted by a running transaction, but
 below the freeze limit. The freeze limit in turn is directly affected
 by the aforementioned bug. 
 Thanks to Tom Lane figuring how to make the bug reproducible. 
 We should add a few more assertions to make sure this type of bug
 isn't as hard to notice, but it's not yet clear how to best do so.  
Co-Diagnosed-By: Tom Lane <tgl@sss.pgh.pa.us> Author: Andres Freund <andres@anarazel.de>
 Discussion: https://postgr.es/m/
1013484.
1597609043@sss.pgh.pa.us  
  Andres Freund [Sun, 16 Aug 2020 19:57:01 +0000 (12:57 -0700)]     Make vacuum a bit more verbose to debug BF failure.
  This is temporary. While possibly some more error checking / debugging
 in this path would be a good thing, it'll not look exactly like this. 
 Discussion: https://postgr.es/m/
20200816181604.l54m6kss5ntd6xow@alap3.anarazel.de  
  Noah Misch [Sun, 16 Aug 2020 03:21:52 +0000 (20:21 -0700)]     Correct several behavior descriptions in comments.
  Reuse cautionary language from src/test/ssl/README in
 src/test/kerberos/README.  SLRUs have had access to six-character
 segments names since commit 
73c986adde5d73a5e2555da9b5c8facedb146dcd,
 and recovery stopped calling HeapTupleHeaderAdvanceLatestRemovedXid() in
 commit 
558a9165e081d1936573e5a7d576f5febd7fb55a.  The other corrections
 are more self-evident.  
  Tom Lane [Sat, 15 Aug 2020 19:43:34 +0000 (15:43 -0400)]     Doc: various improvements for pg_basebackup reference page.
  Put the -r option in the right section (it certainly isn't an
 option controlling "the location and format of the output"). 
 Clarify the behavior of the tablespace and waldir options
 (that part per gripe from robert@interactive.co.uk). 
 Make a large number of small copy-editing fixes in text that
 visibly wasn't written by native speakers, and try to avoid
 grammatical inconsistencies between the descriptions of
 the different options. 
 Back-patch to v13, since HEAD hasn't meaningfully diverged yet. 
 Discussion: https://postgr.es/m/
159749418850.14322.
216503677134569752@wrigleys.postgresql.org  
  Noah Misch [Sat, 15 Aug 2020 17:15:53 +0000 (10:15 -0700)]     Prevent concurrent SimpleLruTruncate() for any given SLRU.
  The SimpleLruTruncate() header comment states the new coding rule.  To
 achieve this, add locktype "frozenid" and two LWLocks.  This closes a
 rare opportunity for data loss, which manifested as "apparent
 wraparound" or "could not access status of transaction" errors.  Data
 loss is more likely in pg_multixact, due to released branches' thin
 margin between multiStopLimit and multiWrapLimit.  If a user's physical
 replication primary logged ":  apparent wraparound" messages, the user
 should rebuild standbys of that primary regardless of symptoms.  At less
 risk is a cluster having emitted "not accepting commands" errors or
 "must be vacuumed" warnings at some point.  One can test a cluster for
 this data loss by running VACUUM FREEZE in every database.  Back-patch
 to 9.5 (all supported versions). 
 Discussion: https://postgr.es/m/
20190218073103.GA1434723@rfd.leadboat.com  
  Tom Lane [Sat, 15 Aug 2020 16:04:19 +0000 (12:04 -0400)]     Remove no-longer-usable hstore--1.0--1.1.sql update script.
  Since commit 
865f14a2d made "=>" unusable as an operator name,
 it's been impossible either to install hstore 1.0 or to execute
 this update script.  There's not much point in continuing
 to ship it. 
 Discussion: https://postgr.es/m/653936.
1597431032@sss.pgh.pa.us  
  Peter Eisentraut [Sat, 15 Aug 2020 09:23:18 +0000 (11:23 +0200)]     Remove obsolete cygwin.h hack
  The version being checked for is 20 years old.  
Reviewed-by: Marco Atzeri <marco.atzeri@gmail.com> Discussion: https://www.postgresql.org/message-id/flat/
aa266ede-baaa-f4e6-06cf-
5b1737610e9a%402ndquadrant.com  
  Peter Eisentraut [Sat, 15 Aug 2020 09:23:18 +0000 (11:23 +0200)]     Remove obsolete HAVE_BUGGY_SOLARIS_STRTOD
  Fixed more than 10 years ago.  
Reviewed-by: Noah Misch <noah@leadboat.com> Discussion: https://www.postgresql.org/message-id/flat/
aa266ede-baaa-f4e6-06cf-
5b1737610e9a%402ndquadrant.com  
  Amit Kapila [Sat, 15 Aug 2020 03:04:48 +0000 (08:34 +0530)]     Mark a few logical decoding related variables with PGDLLIMPORT.
  Commit 
7259736a6e added two variables CheckXidAlive and bsysscan to detect
 concurrent aborts and used these in inline functions that are part of the
 API that can be used by extensions. So it is better to mark them with
 PGDLLIMPORT.  
Reported-by: Thomas Munro Discussion: https://postgr.es/m/CA+hUKGJ7+HYupd=Pz9+QrXa-C_YnbC4rAYu8V+=OKg=UgdzMeg@mail.gmail.com  
  Tom Lane [Sat, 15 Aug 2020 02:14:03 +0000 (22:14 -0400)]     Be more careful about the shape of hashable subplan clauses.
  nodeSubplan.c expects that the testexpr for a hashable ANY SubPlan
 has the form of one or more OpExprs whose LHS is an expression of the
 outer query's, while the RHS is an expression over Params representing
 output columns of the subquery.  However, the planner only went as far
 as verifying that the clauses were all binary OpExprs.  This works
 99.99% of the time, because the clauses have the right shape when
 emitted by the parser --- but it's possible for function inlining to
 break that, as reported by PegoraroF10.  To fix, teach the planner
 to check that the LHS and RHS contain the right things, or more
 accurately don't contain the wrong things.  Given that this has been
 broken for years without anyone noticing, it seems sufficient to just
 give up hashing when it happens, rather than go to the trouble of
 commuting the clauses back again (which wouldn't necessarily work
 anyway). 
 While poking at that, I also noticed that nodeSubplan.c had a baked-in
 assumption that the number of hash clauses is identical to the number
 of subquery output columns.  Again, that's fine as far as parser output
 goes, but it's not hard to break it via function inlining.  There seems
 little reason for that assumption though --- AFAICS, the only thing
 it's buying us is not having to store the number of hash clauses
 explicitly.  Adding code to the planner to reject such cases would take
 more code than getting nodeSubplan.c to cope, so I fixed it that way. 
 This has been broken for as long as we've had hashable SubPlans,
 so back-patch to all supported branches. 
 Discussion: https://postgr.es/m/
1549209182255-0.post@n3.nabble.com  
  Andres Freund [Fri, 14 Aug 2020 21:30:38 +0000 (14:30 -0700)]     snapshot scalability: Move subxact info to ProcGlobal, remove PGXACT.
  Similar to the previous changes this increases the chance that data
 frequently needed by GetSnapshotData() stays in l2 cache. In many
 workloads subtransactions are very rare, and this makes the check for
 that considerably cheaper. 
 As this removes the last member of PGXACT, there is no need to keep it
 around anymore. 
 On a larger 2 socket machine this and the two preceding commits result
 in a ~1.07x performance increase in read-only pgbench. For read-heavy
 mixed r/w workloads without row level contention, I see about 1.1x. 
 Author: Andres Freund <andres@anarazel.de> 
Reviewed-By: Robert Haas <robertmhaas@gmail.com> Reviewed-By: Thomas Munro <thomas.munro@gmail.com> Reviewed-By: David Rowley <dgrowleyml@gmail.com> Discussion: https://postgr.es/m/
20200301083601.ews6hz5dduc3w2se@alap3.anarazel.de  
  Andres Freund [Wed, 15 Jul 2020 22:35:07 +0000 (15:35 -0700)]     snapshot scalability: Move PGXACT->vacuumFlags to ProcGlobal->vacuumFlags.
  Similar to the previous commit this increases the chance that data
 frequently needed by GetSnapshotData() stays in l2 cache. As we now
 take care to not unnecessarily write to ProcGlobal->vacuumFlags, there
 should be very few modifications to the ProcGlobal->vacuumFlags array. 
 Author: Andres Freund <andres@anarazel.de> 
Reviewed-By: Robert Haas <robertmhaas@gmail.com> Reviewed-By: Thomas Munro <thomas.munro@gmail.com> Reviewed-By: David Rowley <dgrowleyml@gmail.com> Discussion: https://postgr.es/m/
20200301083601.ews6hz5dduc3w2se@alap3.anarazel.de  
  Andres Freund [Fri, 14 Aug 2020 19:15:38 +0000 (12:15 -0700)]     snapshot scalability: Introduce dense array of in-progress xids.
  The new array contains the xids for all connected backends / in-use
 PGPROC entries in a dense manner (in contrast to the PGPROC/PGXACT
 arrays which can have unused entries interspersed). 
 This improves performance because GetSnapshotData() always needs to
 scan the xids of all live procarray entries and now there's no need to
 go through the procArray->pgprocnos indirection anymore. 
 As the set of running top-level xids changes rarely, compared to the
 number of snapshots taken, this substantially increases the likelihood
 of most data required for a snapshot being in l2 cache.  In
 read-mostly workloads scanning the xids[] array will sufficient to
 build a snapshot, as most backends will not have an xid assigned. 
 To keep the xid array dense ProcArrayRemove() needs to move entries
 behind the to-be-removed proc's one further up in the array. Obviously
 moving array entries cannot happen while a backend sets it
 xid. I.e. locking needs to prevent that array entries are moved while
 a backend modifies its xid. 
 To avoid locking ProcArrayLock in GetNewTransactionId() - a fairly hot
 spot already - ProcArrayAdd() / ProcArrayRemove() now needs to hold
 XidGenLock in addition to ProcArrayLock. Adding / Removing a procarray
 entry is not a very frequent operation, even taking 2PC into account. 
 Due to the above, the dense array entries can only be read or modified
 while holding ProcArrayLock and/or XidGenLock. This prevents a
 concurrent ProcArrayRemove() from shifting the dense array while it is
 accessed concurrently. 
 While the new dense array is very good when needing to look at all
 xids it is less suitable when accessing a single backend's xid. In
 particular it would be problematic to have to acquire a lock to access
 a backend's own xid. Therefore a backend's xid is not just stored in
 the dense array, but also in PGPROC. This also allows a backend to
 only access the shared xid value when the backend had acquired an
 xid. 
 The infrastructure added in this commit will be used for the remaining
 PGXACT fields in subsequent commits. They are kept separate to make
 review easier. 
 Author: Andres Freund <andres@anarazel.de> 
Reviewed-By: Robert Haas <robertmhaas@gmail.com> Reviewed-By: Thomas Munro <thomas.munro@gmail.com> Reviewed-By: David Rowley <dgrowleyml@gmail.com> Discussion: https://postgr.es/m/
20200301083601.ews6hz5dduc3w2se@alap3.anarazel.de  
  Alvaro Herrera [Fri, 14 Aug 2020 21:33:31 +0000 (17:33 -0400)]     pg_dump: fix dependencies on FKs to partitioned tables
  Parallel-restoring a foreign key that references a partitioned table
 with several levels of partitions can fail: 
 pg_restore: while PROCESSING TOC:
 pg_restore: from TOC entry 6684; 2606 29166 FK CONSTRAINT fk fk_a_fkey postgres
 pg_restore: error: could not execute query: ERROR:  there is no unique constraint matching given keys for referenced table "pk"
 Command was: ALTER TABLE fkpart3.fk
     ADD CONSTRAINT fk_a_fkey FOREIGN KEY (a) REFERENCES fkpart3.pk(a); 
 This happens in parallel restore mode because some index partitions
 aren't yet attached to the topmost partitioned index that the FK uses,
 and so the index is still invalid.  The current code marks the FK as
 dependent on the first level of index-attach dump objects; the bug is
 fixed by recursively marking the FK on their children. 
 Backpatch to 12, where FKs to partitioned tables were introduced.  
Reported-by: Tom Lane <tgl@sss.pgh.pa.us> Author: Álvaro Herrera <alvherre@alvh.no-ip.org>
 Discussion: https://postgr.es/m/
3170626.
1594842723@sss.pgh.pa.us
 Backpatch: 12-master  
  Peter Geoghegan [Fri, 14 Aug 2020 18:09:08 +0000 (11:09 -0700)]     Fix obsolete comment in xlogutils.c.
  Oversight in commit 
2c03216d831.  
  Tom Lane [Fri, 14 Aug 2020 17:26:57 +0000 (13:26 -0400)]     Fix postmaster's behavior during smart shutdown.
 
 Up to now, upon receipt of a SIGTERM ("smart shutdown" command), the
 postmaster has immediately killed all "optional" background processes,
 and subsequently refused to launch new ones while it's waiting for
 foreground client processes to exit.  No doubt this seemed like an OK
 policy at some point; but it's a pretty bad one now, because it makes
 for a seriously degraded environment for the remaining clients:
 
 * Parallel queries are killed, and new ones fail to launch. (And our
 parallel-query infrastructure utterly fails to deal with the case
 in a reasonable way --- it just hangs waiting for workers that are
 not going to arrive.  There is more work needed in that area IMO.)
 
 * Autovacuum ceases to function.  We can tolerate that for awhile,
 but if bulk-update queries continue to run in the surviving client
 sessions, there's eventually going to be a mess.  In the worst case
 the system could reach a forced shutdown to prevent XID wraparound.
 
 * The bgwriter and walwriter are also stopped immediately, likely
 resulting in performance degradation.
 
 Hence, let's rearrange things so that the only immediate change in
 behavior is refusing to let in new normal connections.  Once the last
 normal connection is gone, shut everything down as though we'd received
 a "fast" shutdown.  To implement this, remove the PM_WAIT_BACKUP and
 PM_WAIT_READONLY states, instead staying in PM_RUN or PM_HOT_STANDBY
 while normal connections remain.  A subsidiary state variable tracks
 whether or not we're letting in new connections in those states.
 
 This also allows having just one copy of the logic for killing child
 processes in smart and fast shutdown modes.  I moved that logic into
 PostmasterStateMachine() by inventing a new state PM_STOP_BACKENDS.
 
 Back-patch to 9.6 where parallel query was added.  In principle
 this'd be a good idea in 9.5 as well, but the risk/reward ratio
 is not as good there, since lack of autovacuum is not a problem
 during typical uses of smart shutdown.
 
 Per report from Bharath Rupireddy.
 
 Patch by me, reviewed by Thomas Munro
 
 Discussion: https://postgr.es/m/CALj2ACXAZ5vKxT9P7P89D87i3MDO9bfS+_bjMHgnWJs8uwUOOw@mail.gmail.com
 
 
    Heikki Linnakangas [Fri, 14 Aug 2020 07:40:50 +0000 (10:40 +0300)]     Fix typo in test comment.
 
 
    Michael Paquier [Fri, 14 Aug 2020 00:30:34 +0000 (09:30 +0900)]     Fix compilation warnings with libselinux 3.1 in contrib/sepgsql/
  Upstream SELinux has recently marked security_context_t as officially
 deprecated, causing warnings with -Wdeprecated-declarations.  This is
 considered as legacy code for some time now by upstream as
 security_context_t got removed from most of the code tree during the
 development of 2.3 back in 2014. 
 This removes all the references to security_context_t in sepgsql/ to be
 consistent with SELinux, fixing the warnings.  Note that this does not
 impact the minimum version of libselinux supported.  
Reviewed-by: Tom Lane Discussion: https://postgr.es/m/
20200813012735.GC11663@paquier.xyz  
  Tom Lane [Fri, 14 Aug 2020 00:00:38 +0000 (20:00 -0400)]     Doc: improve examples for json_populate_record() and related functions.
 
 Make these examples self-contained by providing declarations of the
 user-defined row types they rely on.  There wasn't room to do this
 in the old doc format, but now there is, and I think it makes the
 examples a good bit less confusing.
 
 
    Andres Freund [Thu, 13 Aug 2020 23:25:21 +0000 (16:25 -0700)]     snapshot scalability: Move PGXACT->xmin back to PGPROC.
  Now that xmin isn't needed for GetSnapshotData() anymore, it leads to
 unnecessary cacheline ping-pong to have it in PGXACT, as it is updated
 considerably more frequently than the other PGXACT members. 
 After the changes in 
dc7420c2c92, this is a very straight-forward change. 
 For highly concurrent, snapshot acquisition heavy, workloads this change alone
 can significantly increase scalability. E.g. plain pgbench on a smaller 2
 socket machine gains 1.07x for read-only pgbench, 1.22x for read-only pgbench
 when submitting queries in batches of 100, and 2.85x for batches of 100
 'SELECT';.  The latter numbers are obviously not to be expected in the
 real-world, but micro-benchmark the snapshot computation
 scalability (previously spending ~80% of the time in GetSnapshotData()). 
 Author: Andres Freund <andres@anarazel.de> 
Reviewed-By: Robert Haas <robertmhaas@gmail.com> Reviewed-By: Thomas Munro <thomas.munro@gmail.com> Reviewed-By: David Rowley <dgrowleyml@gmail.com> Discussion: https://postgr.es/m/
20200301083601.ews6hz5dduc3w2se@alap3.anarazel.de  
  Alvaro Herrera [Thu, 13 Aug 2020 21:33:49 +0000 (17:33 -0400)]     Handle new HOT chains in index-build table scans
  When a table is scanned by heapam_index_build_range_scan (née
 IndexBuildHeapScan) and the table lock being held allows concurrent data
 changes, it is possible for new HOT chains to sprout in a page that were
 unknown when the scan of a page happened.  This leads to an error such
 as
   ERROR:  failed to find parent tuple for heap-only tuple at (X,Y) in table "tbl"
 because the root tuple was not present when we first obtained the list
 of the page's root tuples.  This can be fixed by re-obtaining the list
 of root tuples, if we see that a heap-only tuple appears to point to a
 non-existing root. 
 This was reported by Anastasia as occurring for BRIN summarization
 (which exists since 9.5), but I think it could theoretically also happen
 with CREATE INDEX CONCURRENTLY (much older) or REINDEX CONCURRENTLY
 (very recent).  It seems a happy coincidence that BRIN forces us to
 backpatch this all the way to 9.5.  
Reported-by: Anastasia Lubennikova <a.lubennikova@postgrespro.ru> Diagnosed-by: Anastasia Lubennikova <a.lubennikova@postgrespro.ru> Co-authored-by: Anastasia Lubennikova <a.lubennikova@postgrespro.ru> Co-authored-by: Álvaro Herrera <alvherre@alvh.no-ip.org> Discussion: https://postgr.es/m/
602d8487-f0b2-5486-0088-
0f372b2549fa@postgrespro.ru
 Backpatch: 9.5 - master