~ubuntu-branches/ubuntu/trusty/postgresql-9.3/trusty-proposed

« back to all changes in this revision

Viewing changes to src/backend/utils/cache/relcache.c

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2014-07-24 16:13:59 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20140724161359-uk325qfv03euxuuh
Tags: 9.3.5-0ubuntu0.14.04.1
* New upstream bug fix release: (LP: #1348176)
  - pg_upgrade: Users who upgraded to version 9.3 using pg_upgrade may have
    an issue with transaction information which causes VACUUM to eventually
    fail. These users should run the script provided in the release notes to
    determine if their installation is affected, and then take the remedy
    steps outlined there.
  - Various data integrity and other bug fixes.
  - Secure Unix-domain sockets of temporary postmasters started during make
    check.
    Any local user able to access the socket file could connect as the
    server's bootstrap superuser, then proceed to execute arbitrary code as
    the operating-system user running the test, as we previously noted in
    CVE-2014-0067. This change defends against that risk by placing the
    server's socket in a temporary, mode 0700 subdirectory of /tmp.
  - See release notes for details:
    http://www.postgresql.org/about/news/1534/
* Remove pg_regress patches to support --host=/path, obsolete with above
  upstream changes and not applicable any more.
* Drop tcl8.6 patch, applied upstream.
* Add missing logrotate test dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
124
124
 
125
125
/*
126
126
 * This counter counts relcache inval events received since backend startup
127
 
 * (but only for rels that are actually in cache).      Presently, we use it only
 
127
 * (but only for rels that are actually in cache).  Presently, we use it only
128
128
 * to detect whether data about to be written by write_relcache_init_file()
129
129
 * might already be obsolete.
130
130
 */
472
472
                                Int16GetDatum(0));
473
473
 
474
474
        /*
475
 
         * Open pg_attribute and begin a scan.  Force heap scan if we haven't yet
 
475
         * Open pg_attribute and begin a scan.  Force heap scan if we haven't yet
476
476
         * built the critical relcache entries (this includes initdb and startup
477
477
         * without a pg_internal.init file).
478
478
         */
535
535
 
536
536
        /*
537
537
         * The attcacheoff values we read from pg_attribute should all be -1
538
 
         * ("unknown").  Verify this if assert checking is on.  They will be
 
538
         * ("unknown").  Verify this if assert checking is on.  They will be
539
539
         * computed when and if needed during tuple access.
540
540
         */
541
541
#ifdef USE_ASSERT_CHECKING
549
549
 
550
550
        /*
551
551
         * However, we can easily set the attcacheoff value for the first
552
 
         * attribute: it must be zero.  This eliminates the need for special cases
 
552
         * attribute: it must be zero.  This eliminates the need for special cases
553
553
         * for attnum=1 that used to exist in fastgetattr() and index_getattr().
554
554
         */
555
555
        if (relation->rd_rel->relnatts > 0)
605
605
 * each relcache entry that has associated rules.  The context is used
606
606
 * just for rule info, not for any other subsidiary data of the relcache
607
607
 * entry, because that keeps the update logic in RelationClearRelation()
608
 
 * manageable.  The other subsidiary data structures are simple enough
 
608
 * manageable.  The other subsidiary data structures are simple enough
609
609
 * to be easy to free explicitly, anyway.
610
610
 */
611
611
static void
714
714
 
715
715
                /*
716
716
                 * We want the rule's table references to be checked as though by the
717
 
                 * table owner, not the user referencing the rule.      Therefore, scan
 
717
                 * table owner, not the user referencing the rule.  Therefore, scan
718
718
                 * through the rule's actions and set the checkAsUser field on all
719
 
                 * rtable entries.      We have to look at the qual as well, in case it
 
719
                 * rtable entries.  We have to look at the qual as well, in case it
720
720
                 * contains sublinks.
721
721
                 *
722
722
                 * The reason for doing this when the rule is loaded, rather than when
1059
1059
        amsupport = aform->amsupport;
1060
1060
 
1061
1061
        /*
1062
 
         * Make the private context to hold index access info.  The reason we need
 
1062
         * Make the private context to hold index access info.  The reason we need
1063
1063
         * a context, and not just a couple of pallocs, is so that we won't leak
1064
1064
         * any subsidiary info attached to fmgr lookup records.
1065
1065
         *
1107
1107
 
1108
1108
        /*
1109
1109
         * indcollation cannot be referenced directly through the C struct,
1110
 
         * because it comes after the variable-width indkey field.      Must extract
 
1110
         * because it comes after the variable-width indkey field.  Must extract
1111
1111
         * the datum the hard way...
1112
1112
         */
1113
1113
        indcollDatum = fastgetattr(relation->rd_indextuple,
1132
1132
 
1133
1133
        /*
1134
1134
         * Fill the support procedure OID array, as well as the info about
1135
 
         * opfamilies and opclass input types.  (aminfo and supportinfo are left
 
1135
         * opfamilies and opclass input types.  (aminfo and supportinfo are left
1136
1136
         * as zeroes, and are filled on-the-fly when used)
1137
1137
         */
1138
1138
        IndexSupportInitialize(indclass, relation->rd_support,
1220
1220
 * Note there is no provision for flushing the cache.  This is OK at the
1221
1221
 * moment because there is no way to ALTER any interesting properties of an
1222
1222
 * existing opclass --- all you can do is drop it, which will result in
1223
 
 * a useless but harmless dead entry in the cache.      To support altering
 
1223
 * a useless but harmless dead entry in the cache.  To support altering
1224
1224
 * opclass membership (not the same as opfamily membership!), we'd need to
1225
1225
 * be able to flush this cache as well as the contents of relcache entries
1226
1226
 * for indexes.
1329
1329
        heap_close(rel, AccessShareLock);
1330
1330
 
1331
1331
        /*
1332
 
         * Scan pg_amproc to obtain support procs for the opclass.      We only fetch
 
1332
         * Scan pg_amproc to obtain support procs for the opclass.  We only fetch
1333
1333
         * the default ones (those with lefttype = righttype = opcintype).
1334
1334
         */
1335
1335
        if (numSupport > 0)
1829
1829
                FreeTupleDesc(relation->rd_att);
1830
1830
        list_free(relation->rd_indexlist);
1831
1831
        bms_free(relation->rd_indexattr);
 
1832
        bms_free(relation->rd_keyattr);
1832
1833
        FreeTriggerDesc(relation->trigdesc);
1833
1834
        if (relation->rd_options)
1834
1835
                pfree(relation->rd_options);
1855
1856
 *
1856
1857
 *       NB: when rebuilding, we'd better hold some lock on the relation,
1857
1858
 *       else the catalog data we need to read could be changing under us.
1858
 
 *       Also, a rel to be rebuilt had better have refcnt > 0.  This is because
 
1859
 *       Also, a rel to be rebuilt had better have refcnt > 0.  This is because
1859
1860
 *       an sinval reset could happen while we're accessing the catalogs, and
1860
1861
 *       the rel would get blown away underneath us by RelationCacheInvalidate
1861
1862
 *       if it has zero refcnt.
1878
1879
        /*
1879
1880
         * Make sure smgr and lower levels close the relation's files, if they
1880
1881
         * weren't closed already.  If the relation is not getting deleted, the
1881
 
         * next smgr access should reopen the files automatically.      This ensures
 
1882
         * next smgr access should reopen the files automatically.  This ensures
1882
1883
         * that the low-level file access state is updated after, say, a vacuum
1883
1884
         * truncation.
1884
1885
         */
1890
1891
         * in case it is a mapped relation whose mapping changed.
1891
1892
         *
1892
1893
         * If it's a nailed index, then we need to re-read the pg_class row to see
1893
 
         * if its relfilenode changed.  We can't necessarily do that here, because
 
1894
         * if its relfilenode changed.  We can't necessarily do that here, because
1894
1895
         * we might be in a failed transaction.  We assume it's okay to do it if
1895
1896
         * there are open references to the relcache entry (cf notes for
1896
1897
         * AtEOXact_RelationCache).  Otherwise just mark the entry as possibly
1951
1952
                 * over from the old entry).  This is to avoid trouble in case an
1952
1953
                 * error causes us to lose control partway through.  The old entry
1953
1954
                 * will still be marked !rd_isvalid, so we'll try to rebuild it again
1954
 
                 * on next access.      Meanwhile it's not any less valid than it was
 
1955
                 * on next access.  Meanwhile it's not any less valid than it was
1955
1956
                 * before, so any code that might expect to continue accessing it
1956
1957
                 * isn't hurt by the rebuild failure.  (Consider for example a
1957
1958
                 * subtransaction that ALTERs a table and then gets canceled partway
2140
2141
/*
2141
2142
 * RelationCacheInvalidate
2142
2143
 *       Blow away cached relation descriptors that have zero reference counts,
2143
 
 *       and rebuild those with positive reference counts.      Also reset the smgr
 
2144
 *       and rebuild those with positive reference counts.  Also reset the smgr
2144
2145
 *       relation cache and re-read relation mapping data.
2145
2146
 *
2146
2147
 *       This is currently used only to recover from SI message buffer overflow,
2153
2154
 *       We do this in two phases: the first pass deletes deletable items, and
2154
2155
 *       the second one rebuilds the rebuildable items.  This is essential for
2155
2156
 *       safety, because hash_seq_search only copes with concurrent deletion of
2156
 
 *       the element it is currently visiting.  If a second SI overflow were to
 
2157
 *       the element it is currently visiting.  If a second SI overflow were to
2157
2158
 *       occur while we are walking the table, resulting in recursive entry to
2158
2159
 *       this routine, we could crash because the inner invocation blows away
2159
2160
 *       the entry next to be visited by the outer scan.  But this way is OK,
2313
2314
         * For simplicity, eoxact_list[] entries are not deleted till end of
2314
2315
         * top-level transaction, even though we could remove them at
2315
2316
         * subtransaction end in some cases, or remove relations from the list if
2316
 
         * they are cleared for other reasons.  Therefore we should expect the
 
2317
         * they are cleared for other reasons.  Therefore we should expect the
2317
2318
         * case that list entries are not found in the hashtable; if not, there's
2318
2319
         * nothing to do for them.
2319
2320
         */
2363
2364
         * transaction calls.  (That seems bogus, but it's not worth fixing.)
2364
2365
         *
2365
2366
         * Note: ideally this check would be applied to every relcache entry, not
2366
 
         * just those that have eoxact work to do.      But it's not worth forcing a
 
2367
         * just those that have eoxact work to do.  But it's not worth forcing a
2367
2368
         * scan of the whole relcache just for this.  (Moreover, doing so would
2368
2369
         * mean that assert-enabled testing never tests the hash_search code path
2369
2370
         * above, which seems a bad idea.)
2667
2668
 
2668
2669
        /*
2669
2670
         * Insert relation physical and logical identifiers (OIDs) into the right
2670
 
         * places.      For a mapped relation, we set relfilenode to zero and rely on
 
2671
         * places.  For a mapped relation, we set relfilenode to zero and rely on
2671
2672
         * RelationInitPhysicalAddr to consult the map.
2672
2673
         */
2673
2674
        rel->rd_rel->relisshared = shared_relation;
2910
2911
        oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
2911
2912
 
2912
2913
        /*
2913
 
         * Try to load the shared relcache cache file.  If unsuccessful, bootstrap
 
2914
         * Try to load the shared relcache cache file.  If unsuccessful, bootstrap
2914
2915
         * the cache with pre-made descriptors for the critical shared catalogs.
2915
2916
         */
2916
2917
        if (!load_relcache_init_file(true))
2990
2991
 
2991
2992
        /*
2992
2993
         * If we didn't get the critical system indexes loaded into relcache, do
2993
 
         * so now.      These are critical because the catcache and/or opclass cache
 
2994
         * so now.  These are critical because the catcache and/or opclass cache
2994
2995
         * depend on them for fetches done during relcache load.  Thus, we have an
2995
 
         * infinite-recursion problem.  We can break the recursion by doing
 
2996
         * infinite-recursion problem.  We can break the recursion by doing
2996
2997
         * heapscans instead of indexscans at certain key spots. To avoid hobbling
2997
2998
         * performance, we only want to do that until we have the critical indexes
2998
2999
         * loaded into relcache.  Thus, the flag criticalRelcachesBuilt is used to
3009
3010
         * RewriteRelRulenameIndexId and TriggerRelidNameIndexId are not critical
3010
3011
         * in the same way as the others, because the critical catalogs don't
3011
3012
         * (currently) have any rules or triggers, and so these indexes can be
3012
 
         * rebuilt without inducing recursion.  However they are used during
 
3013
         * rebuilt without inducing recursion.  However they are used during
3013
3014
         * relcache load when a rel does have rules or triggers, so we choose to
3014
3015
         * nail them for performance reasons.
3015
3016
         */
3040
3041
         *
3041
3042
         * DatabaseNameIndexId isn't critical for relcache loading, but rather for
3042
3043
         * initial lookup of MyDatabaseId, without which we'll never find any
3043
 
         * non-shared catalogs at all.  Autovacuum calls InitPostgres with a
 
3044
         * non-shared catalogs at all.  Autovacuum calls InitPostgres with a
3044
3045
         * database OID, so it instead depends on DatabaseOidIndexId.  We also
3045
3046
         * need to nail up some indexes on pg_authid and pg_auth_members for use
3046
3047
         * during client authentication.
3472
3473
 
3473
3474
        /*
3474
3475
         * We build the list we intend to return (in the caller's context) while
3475
 
         * doing the scan.      After successfully completing the scan, we copy that
 
3476
         * doing the scan.  After successfully completing the scan, we copy that
3476
3477
         * list into the relcache entry.  This avoids cache-context memory leakage
3477
3478
         * if we get some sort of error partway through.
3478
3479
         */
3510
3511
 
3511
3512
                /*
3512
3513
                 * indclass cannot be referenced directly through the C struct,
3513
 
                 * because it comes after the variable-width indkey field.      Must
 
3514
                 * because it comes after the variable-width indkey field.  Must
3514
3515
                 * extract the datum the hard way...
3515
3516
                 */
3516
3517
                indclassDatum = heap_getattr(htup,
3594
3595
 * correctly with respect to the full index set.  It is up to the caller
3595
3596
 * to ensure that a correct rd_indexattr set has been cached before first
3596
3597
 * calling RelationSetIndexList; else a subsequent inquiry might cause a
3597
 
 * wrong rd_indexattr set to get computed and cached.
 
3598
 * wrong rd_indexattr set to get computed and cached.  Likewise, we do not
 
3599
 * touch rd_keyattr.
3598
3600
 */
3599
3601
void
3600
3602
RelationSetIndexList(Relation relation, List *indexIds, Oid oidIndex)
3875
3877
 
3876
3878
        list_free(indexoidlist);
3877
3879
 
3878
 
        /* Now save a copy of the bitmap in the relcache entry. */
 
3880
        /*
 
3881
         * Now save copies of the bitmaps in the relcache entry.  We intentionally
 
3882
         * set rd_indexattr last, because that's the one that signals validity of
 
3883
         * the values; if we run out of memory before making that copy, we won't
 
3884
         * leave the relcache entry looking like the other ones are valid but
 
3885
         * empty.
 
3886
         */
3879
3887
        oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
 
3888
        relation->rd_keyattr = bms_copy(uindexattrs);
3880
3889
        relation->rd_indexattr = bms_copy(indexattrs);
3881
 
        relation->rd_keyattr = bms_copy(uindexattrs);
3882
3890
        MemoryContextSwitchTo(oldcxt);
3883
3891
 
3884
3892
        /* We return our original working copy for caller to play with */
4060
4068
 * given directly rather than extracted from the relation's catalog data.
4061
4069
 *
4062
4070
 * Don't use this directly unless errtablecol() is inconvenient for some
4063
 
 * reason.      This might possibly be needed during intermediate states in ALTER
 
4071
 * reason.  This might possibly be needed during intermediate states in ALTER
4064
4072
 * TABLE, for instance.
4065
4073
 */
4066
4074
int
4423
4431
                rel->rd_indexvalid = 0;
4424
4432
                rel->rd_indexlist = NIL;
4425
4433
                rel->rd_indexattr = NULL;
 
4434
                rel->rd_keyattr = NULL;
4426
4435
                rel->rd_oidindex = InvalidOid;
4427
4436
                rel->rd_createSubid = InvalidSubTransactionId;
4428
4437
                rel->rd_newRelfilenodeSubid = InvalidSubTransactionId;
4480
4489
        return true;
4481
4490
 
4482
4491
        /*
4483
 
         * init file is broken, so do it the hard way.  We don't bother trying to
 
4492
         * init file is broken, so do it the hard way.  We don't bother trying to
4484
4493
         * free the clutter we just allocated; it's not in the relcache so it
4485
4494
         * won't hurt.
4486
4495
         */
4545
4554
        }
4546
4555
 
4547
4556
        /*
4548
 
         * Write a magic number to serve as a file version identifier.  We can
 
4557
         * Write a magic number to serve as a file version identifier.  We can
4549
4558
         * change the magic number whenever the relcache layout changes.
4550
4559
         */
4551
4560
        magic = RELCACHE_INIT_FILEMAGIC;
4770
4779
 *
4771
4780
 * We used to keep the init files across restarts, but that is unsafe in PITR
4772
4781
 * scenarios, and even in simple crash-recovery cases there are windows for
4773
 
 * the init files to become out-of-sync with the database.      So now we just
 
4782
 * the init files to become out-of-sync with the database.  So now we just
4774
4783
 * remove them during startup and expect the first backend launch to rebuild
4775
4784
 * them.  Of course, this has to happen in each database of the cluster.
4776
4785
 */