|
13 | 13 | * Portions Copyright (c) 1994, Regents of the University of California
|
14 | 14 | *
|
15 | 15 | * IDENTIFICATION
|
16 |
| - * $Header: /cvsroot/pgsql/src/backend/catalog/namespace.c,v 1.4 2002/03/31 06:26:30 tgl Exp $ |
| 16 | + * $Header: /cvsroot/pgsql/src/backend/catalog/namespace.c,v 1.5 2002/04/01 03:34:25 tgl Exp $ |
17 | 17 | *
|
18 | 18 | *-------------------------------------------------------------------------
|
19 | 19 | */
|
|
30 | 30 | #include "miscadmin.h"
|
31 | 31 | #include "nodes/makefuncs.h"
|
32 | 32 | #include "storage/backendid.h"
|
| 33 | +#include "utils/builtins.h" |
33 | 34 | #include "utils/fmgroids.h"
|
| 35 | +#include "utils/guc.h" |
34 | 36 | #include "utils/lsyscache.h"
|
35 | 37 | #include "utils/syscache.h"
|
36 | 38 |
|
@@ -71,6 +73,12 @@ static Oid defaultCreationNamespace = PG_CATALOG_NAMESPACE;
|
71 | 73 | */
|
72 | 74 | static Oid myTempNamespace = InvalidOid;
|
73 | 75 |
|
| 76 | +/* |
| 77 | + * This is the text equivalent of the search path --- it's the value |
| 78 | + * of the GUC variable 'search_path'. |
| 79 | + */ |
| 80 | +char *namespace_search_path = NULL; |
| 81 | + |
74 | 82 |
|
75 | 83 | /*
|
76 | 84 | * Deletion ordering constraint item.
|
@@ -702,3 +710,202 @@ RemoveTempRelationsCallback(void)
|
702 | 710 | CommitTransactionCommand();
|
703 | 711 | }
|
704 | 712 | }
|
| 713 | + |
| 714 | +/* |
| 715 | + * Routines for handling the GUC variable 'search_path'. |
| 716 | + */ |
| 717 | + |
| 718 | +/* parse_hook: is proposed value valid? */ |
| 719 | +bool |
| 720 | +check_search_path(const char *proposed) |
| 721 | +{ |
| 722 | + char *rawname; |
| 723 | + List *namelist; |
| 724 | + List *l; |
| 725 | + |
| 726 | + /* Need a modifiable copy of string */ |
| 727 | + rawname = pstrdup(proposed); |
| 728 | + |
| 729 | + /* Parse string into list of identifiers */ |
| 730 | + if (!SplitIdentifierString(rawname, ',', &namelist)) |
| 731 | + { |
| 732 | + /* syntax error in name list */ |
| 733 | + pfree(rawname); |
| 734 | + freeList(namelist); |
| 735 | + return false; |
| 736 | + } |
| 737 | + |
| 738 | + /* |
| 739 | + * If we aren't inside a transaction, we cannot do database access so |
| 740 | + * cannot verify the individual names. Must accept the list on faith. |
| 741 | + * (This case can happen, for example, when the postmaster reads a |
| 742 | + * search_path setting from postgresql.conf.) |
| 743 | + */ |
| 744 | + if (!IsTransactionState()) |
| 745 | + { |
| 746 | + pfree(rawname); |
| 747 | + freeList(namelist); |
| 748 | + return true; |
| 749 | + } |
| 750 | + |
| 751 | + /* |
| 752 | + * Verify that all the names are either valid namespace names or "$user". |
| 753 | + * (We do not require $user to correspond to a valid namespace; should we?) |
| 754 | + */ |
| 755 | + foreach(l, namelist) |
| 756 | + { |
| 757 | + char *curname = (char *) lfirst(l); |
| 758 | + |
| 759 | + if (strcmp(curname, "$user") == 0) |
| 760 | + continue; |
| 761 | + if (!SearchSysCacheExists(NAMESPACENAME, |
| 762 | + CStringGetDatum(curname), |
| 763 | + 0, 0, 0)) |
| 764 | + { |
| 765 | + pfree(rawname); |
| 766 | + freeList(namelist); |
| 767 | + return false; |
| 768 | + } |
| 769 | + } |
| 770 | + |
| 771 | + pfree(rawname); |
| 772 | + freeList(namelist); |
| 773 | + |
| 774 | + return true; |
| 775 | +} |
| 776 | + |
| 777 | +/* assign_hook: do extra actions needed when assigning to search_path */ |
| 778 | +void |
| 779 | +assign_search_path(const char *newval) |
| 780 | +{ |
| 781 | + char *rawname; |
| 782 | + List *namelist; |
| 783 | + List *oidlist; |
| 784 | + List *newpath; |
| 785 | + List *l; |
| 786 | + MemoryContext oldcxt; |
| 787 | + |
| 788 | + /* |
| 789 | + * If we aren't inside a transaction, we cannot do database access so |
| 790 | + * cannot look up the names. In this case, do nothing; the internal |
| 791 | + * search path will be fixed later by InitializeSearchPath. (We assume |
| 792 | + * this situation can only happen in the postmaster or early in backend |
| 793 | + * startup.) |
| 794 | + */ |
| 795 | + if (!IsTransactionState()) |
| 796 | + return; |
| 797 | + |
| 798 | + /* Need a modifiable copy of string */ |
| 799 | + rawname = pstrdup(newval); |
| 800 | + |
| 801 | + /* Parse string into list of identifiers */ |
| 802 | + if (!SplitIdentifierString(rawname, ',', &namelist)) |
| 803 | + { |
| 804 | + /* syntax error in name list */ |
| 805 | + /* this should not happen if GUC checked check_search_path */ |
| 806 | + elog(ERROR, "assign_search_path: invalid list syntax"); |
| 807 | + } |
| 808 | + |
| 809 | + /* |
| 810 | + * Convert the list of names to a list of OIDs. If any names are not |
| 811 | + * recognizable, just leave them out of the list. (This is our only |
| 812 | + * reasonable recourse when the already-accepted default is bogus.) |
| 813 | + */ |
| 814 | + oidlist = NIL; |
| 815 | + foreach(l, namelist) |
| 816 | + { |
| 817 | + char *curname = (char *) lfirst(l); |
| 818 | + Oid namespaceId; |
| 819 | + |
| 820 | + if (strcmp(curname, "$user") == 0) |
| 821 | + { |
| 822 | + /* $user --- substitute namespace matching user name, if any */ |
| 823 | + HeapTuple tuple; |
| 824 | + |
| 825 | + tuple = SearchSysCache(SHADOWSYSID, |
| 826 | + ObjectIdGetDatum(GetSessionUserId()), |
| 827 | + 0, 0, 0); |
| 828 | + if (HeapTupleIsValid(tuple)) |
| 829 | + { |
| 830 | + char *uname; |
| 831 | + |
| 832 | + uname = NameStr(((Form_pg_shadow) GETSTRUCT(tuple))->usename); |
| 833 | + namespaceId = GetSysCacheOid(NAMESPACENAME, |
| 834 | + CStringGetDatum(uname), |
| 835 | + 0, 0, 0); |
| 836 | + if (OidIsValid(namespaceId)) |
| 837 | + oidlist = lappendi(oidlist, namespaceId); |
| 838 | + ReleaseSysCache(tuple); |
| 839 | + } |
| 840 | + } |
| 841 | + else |
| 842 | + { |
| 843 | + /* normal namespace reference */ |
| 844 | + namespaceId = GetSysCacheOid(NAMESPACENAME, |
| 845 | + CStringGetDatum(curname), |
| 846 | + 0, 0, 0); |
| 847 | + if (OidIsValid(namespaceId)) |
| 848 | + oidlist = lappendi(oidlist, namespaceId); |
| 849 | + } |
| 850 | + } |
| 851 | + |
| 852 | + /* |
| 853 | + * Now that we've successfully built the new list of namespace OIDs, |
| 854 | + * save it in permanent storage. |
| 855 | + */ |
| 856 | + oldcxt = MemoryContextSwitchTo(TopMemoryContext); |
| 857 | + newpath = listCopy(oidlist); |
| 858 | + MemoryContextSwitchTo(oldcxt); |
| 859 | + |
| 860 | + /* Now safe to assign to state variable. */ |
| 861 | + freeList(namespaceSearchPath); |
| 862 | + namespaceSearchPath = newpath; |
| 863 | + |
| 864 | + /* |
| 865 | + * Update info derived from search path. |
| 866 | + */ |
| 867 | + pathContainsSystemNamespace = intMember(PG_CATALOG_NAMESPACE, |
| 868 | + namespaceSearchPath); |
| 869 | + |
| 870 | + if (namespaceSearchPath == NIL) |
| 871 | + defaultCreationNamespace = PG_CATALOG_NAMESPACE; |
| 872 | + else |
| 873 | + defaultCreationNamespace = (Oid) lfirsti(namespaceSearchPath); |
| 874 | + |
| 875 | + /* Clean up. */ |
| 876 | + pfree(rawname); |
| 877 | + freeList(namelist); |
| 878 | + freeList(oidlist); |
| 879 | +} |
| 880 | + |
| 881 | +/* |
| 882 | + * InitializeSearchPath: initialize module during InitPostgres. |
| 883 | + * |
| 884 | + * This is called after we are up enough to be able to do catalog lookups. |
| 885 | + */ |
| 886 | +void |
| 887 | +InitializeSearchPath(void) |
| 888 | +{ |
| 889 | + /* |
| 890 | + * In normal multi-user mode, we want the default search path to be |
| 891 | + * '$user,public' (or as much of that as exists, anyway; see the |
| 892 | + * error handling in assign_search_path); which is what guc.c has as |
| 893 | + * the wired-in default value. But in bootstrap or standalone-backend |
| 894 | + * mode, the default search path must be empty so that initdb correctly |
| 895 | + * creates everything in PG_CATALOG_NAMESPACE. Accordingly, adjust the |
| 896 | + * default setting if we are not running under postmaster. (If a |
| 897 | + * non-default setting has been supplied, this will not overwrite it.) |
| 898 | + */ |
| 899 | + if (!IsUnderPostmaster) |
| 900 | + { |
| 901 | + SetConfigOption("search_path", "", |
| 902 | + PGC_POSTMASTER, PGC_S_DEFAULT); |
| 903 | + } |
| 904 | + /* |
| 905 | + * If a search path setting was provided before we were able to execute |
| 906 | + * lookups, establish the internal search path now. |
| 907 | + */ |
| 908 | + if (namespace_search_path && *namespace_search_path && |
| 909 | + namespaceSearchPath == NIL) |
| 910 | + assign_search_path(namespace_search_path); |
| 911 | +} |
0 commit comments